Skip to content

Commit

Permalink
Adopted to new CAN driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Teeed committed May 18, 2020
1 parent e3b6340 commit 267e48f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ T T T T
void canpybara_configure_filters(CAN_HandleTypeDef* hcan);
void canpybara_can_init(void);

void canpybara_can_tx(CanTxMsgTypeDef *can_tx);
void canpybara_can_tx(CAN_TxHeaderTypeDef* can_tx_header, uint8_t can_tx_data[8]);

void canpybara_can_tx_complete(void);
void canpybara_can_error(void);
Expand Down
59 changes: 35 additions & 24 deletions Core/Src/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ void canpybara_bootloader_enable(void)

void canpybara_bootloader_send_discovery(void)
{
static CanTxMsgTypeDef can_tx;
can_tx.StdId = CANPYBARA_REPORT_BOOTLOADER;
can_tx.ExtId = 0;
can_tx.IDE = CAN_ID_STD;
can_tx.RTR = CAN_RTR_DATA;
CAN_TxHeaderTypeDef can_tx_header;
uint8_t can_tx_data[8];

can_tx_header.StdId = CANPYBARA_REPORT_BOOTLOADER;
can_tx_header.ExtId = 0;
can_tx_header.IDE = CAN_ID_STD;
can_tx_header.RTR = CAN_RTR_DATA;
can_tx_header.DLC = 1;
can_tx_header.TransmitGlobalTime = DISABLE;

can_tx.DLC = 1;
int i = 0;
can_tx.Data[i++] = 0x00; // protocol version 00
can_tx_data[i++] = 0x00; // protocol version 00

canpybara_can_tx(&can_tx);
canpybara_can_tx(&can_tx_header, can_tx_data);
}

static void canpybara_bootloader_active_loop(void)
Expand Down Expand Up @@ -101,17 +104,21 @@ void canpybara_bootloader_erase(uint32_t addr)
HAL_FLASHEx_Erase(&erase_init_struct, &page_error);
HAL_FLASH_Lock();

static CanTxMsgTypeDef can_tx;
can_tx.StdId = CANPYBARA_REPORT_BOOTLOADER_ERASE;
can_tx.ExtId = 0;
can_tx.IDE = CAN_ID_STD;
can_tx.RTR = CAN_RTR_DATA;
CAN_TxHeaderTypeDef can_tx_header;
uint8_t can_tx_data[8];

can_tx_header.StdId = CANPYBARA_REPORT_BOOTLOADER_ERASE;
can_tx_header.ExtId = 0;
can_tx_header.IDE = CAN_ID_STD;
can_tx_header.RTR = CAN_RTR_DATA;
can_tx_header.DLC = 1;
can_tx_header.TransmitGlobalTime = DISABLE;

can_tx.DLC = 1;
int i = 0;
can_tx.Data[i++] = page_error == 0xFFFFFFFF;
can_tx_data[i++] = page_error == 0xFFFFFFFF;

canpybara_can_tx(&can_tx_header, can_tx_data);

canpybara_can_tx(&can_tx);
canpybara_bootloader_last_active = HAL_GetTick();
canpybara_bootloader_addr = addr;
}
Expand All @@ -128,17 +135,21 @@ void canpybara_bootloader_write(uint64_t bytes)
HAL_StatusTypeDef result = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, canpybara_bootloader_addr, bytes);
HAL_FLASH_Lock();

static CanTxMsgTypeDef can_tx;
can_tx.StdId = CANPYBARA_REPORT_BOOTLOADER_WRITE;
can_tx.ExtId = 0;
can_tx.IDE = CAN_ID_STD;
can_tx.RTR = CAN_RTR_DATA;
CAN_TxHeaderTypeDef can_tx_header;
uint8_t can_tx_data[8];

can_tx_header.StdId = CANPYBARA_REPORT_BOOTLOADER_WRITE;
can_tx_header.ExtId = 0;
can_tx_header.IDE = CAN_ID_STD;
can_tx_header.RTR = CAN_RTR_DATA;
can_tx_header.DLC = 1;
can_tx_header.TransmitGlobalTime = DISABLE;

can_tx.DLC = 1;
int i = 0;
can_tx.Data[i++] = result != HAL_ERROR;
can_tx_data[i++] = result != HAL_ERROR;

canpybara_can_tx(&can_tx_header, can_tx_data);

canpybara_can_tx(&can_tx);
canpybara_bootloader_last_active = HAL_GetTick();
canpybara_bootloader_addr += sizeof(uint64_t);
}
Expand Down
118 changes: 57 additions & 61 deletions Core/Src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
extern CAN_HandleTypeDef hcan;
extern IWDG_HandleTypeDef hiwdg;

CanTxMsgTypeDef hcan_CAN_Tx;

CanRxMsgTypeDef hcan_CAN_Rx0;
CanRxMsgTypeDef hcan_CAN_Rx1;


uint16_t canpybara_can_get_my_address(void)
{
uint16_t result = 0;
Expand All @@ -34,7 +28,7 @@ uint16_t canpybara_can_get_my_address(void)
if(result == 0)
{
LOG("Invalid module address: %d", result);
_Error_Handler(__FILE__, __LINE__);
Error_Handler();
}

return result;
Expand All @@ -58,8 +52,8 @@ void canpybara_configure_filters(CAN_HandleTypeDef* hcan)
{
uint16_t my_address = canpybara_can_get_my_address();

CAN_FilterConfTypeDef filter_config;
filter_config.FilterNumber = 0;
CAN_FilterTypeDef filter_config;
filter_config.FilterBank = 0;
filter_config.FilterMode = CAN_FILTERMODE_IDMASK;
filter_config.FilterScale = CAN_FILTERSCALE_16BIT;

Expand All @@ -75,80 +69,88 @@ void canpybara_configure_filters(CAN_HandleTypeDef* hcan)

filter_config.FilterFIFOAssignment = CAN_FILTER_FIFO0;
filter_config.FilterActivation = ENABLE;
filter_config.BankNumber = 0;

HAL_StatusTypeDef result = HAL_CAN_ConfigFilter(hcan, &filter_config);

LOG("Configuring CAN filters my_addr: %d, status: %d", my_address, result);
if(result != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}

void canpybara_reload_canrx(CAN_HandleTypeDef* hcan)
{
__HAL_UNLOCK(hcan);
HAL_StatusTypeDef result = HAL_CAN_Receive_IT(hcan, CAN_FIFO0);

if(result != HAL_OK)
{
LOG("CAN REGISTER STATUS: %"PRIu32, hcan->State);
LOG("CAN_Receive_IT called status: %d", result);
_Error_Handler(__FILE__, __LINE__);
LOG("Error configuring CAN filter");
Error_Handler();
}
}

void canpybara_can_init(void)
{
LOG("Initializing CAN");

hcan.pRxMsg = &hcan_CAN_Rx0;
hcan.pRx1Msg = &hcan_CAN_Rx1;

canpybara_configure_filters(&hcan);
canpybara_reload_canrx(&hcan);

if(HAL_CAN_Start(&hcan) != HAL_OK)
{
LOG("Error starting CAN");
Error_Handler();
}

if(HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
LOG("Error activating CAN notification");
Error_Handler();
}
}


// TODO: Remove or make these counters really count things..
void capybara_can_report_status(void)
{
static CanTxMsgTypeDef can_tx;
can_tx.StdId = CANPYBARA_REPORT_STATUS;
can_tx.ExtId = 0;
can_tx.IDE = CAN_ID_STD;
can_tx.RTR = CAN_RTR_DATA;
CAN_TxHeaderTypeDef can_tx_header;
uint8_t can_tx_data[8];

can_tx_header.StdId = CANPYBARA_REPORT_STATUS;
can_tx_header.ExtId = 0;
can_tx_header.IDE = CAN_ID_STD;
can_tx_header.RTR = CAN_RTR_DATA;
can_tx_header.DLC = 7;
can_tx_header.TransmitGlobalTime = DISABLE;

can_tx.DLC = 7;
int i = 0;
can_tx.Data[i++] = 0x02; // bootloader
can_tx_data[i++] = 0x02; // bootloader

// TX
can_tx.Data[i++] = canpybara_tx_frames >> 8;
can_tx.Data[i++] = canpybara_tx_frames;
can_tx_data[i++] = canpybara_tx_frames >> 8;
can_tx_data[i++] = canpybara_tx_frames;

// RX
can_tx.Data[i++] = canpybara_rx_frames >> 8;
can_tx.Data[i++] = canpybara_rx_frames;
can_tx_data[i++] = canpybara_rx_frames >> 8;
can_tx_data[i++] = canpybara_rx_frames;

// ERR
can_tx.Data[i++] = canpybara_errors >> 8;
can_tx.Data[i++] = canpybara_errors;
can_tx_data[i++] = canpybara_errors >> 8;
can_tx_data[i++] = canpybara_errors;

canpybara_can_tx(&can_tx);
canpybara_can_tx(&can_tx_header, can_tx_data);
}

void canpybara_can_rx(CAN_HandleTypeDef* hcan)
{
canpybara_rx_frames++;

int request_id = CANPYBARA_CONTROLLER_REQUESTID(hcan->pRxMsg->StdId);
CAN_RxHeaderTypeDef can_rx_header;
uint8_t can_rx_data[8];
if(HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &can_rx_header, can_rx_data) != HAL_OK)
{
LOG("Error when getting CAN rx message");
Error_Handler();
}

int request_id = CANPYBARA_CONTROLLER_REQUESTID(can_rx_header.StdId);

LOG("Request id: %d for %"PRIu32, request_id, hcan->pRxMsg->StdId);
LOG("Request id: %d for %"PRIu32, request_id, can_rx_header.StdId);

switch(request_id)
{
case CANPYBARA_REQUEST_STATUS:
if (hcan->pRxMsg->RTR == CAN_RTR_REMOTE)
if (can_rx_header.RTR == CAN_RTR_REMOTE)
{
capybara_can_report_status();
}
Expand All @@ -164,36 +166,30 @@ void canpybara_can_rx(CAN_HandleTypeDef* hcan)
break;

case CANPYBARA_REQUEST_BOOTLOADER_ERASE:
if (hcan->pRxMsg->DLC == 4)
if (can_rx_header.DLC == 4)
{
canpybara_bootloader_erase(*((uint32_t*)hcan->pRxMsg->Data));
canpybara_bootloader_erase(*((uint32_t*)can_rx_data));
}
break;

case CANPYBARA_REQUEST_BOOTLOADER_WRITE:
if (hcan->pRxMsg->DLC == 8)
if (can_rx_header.DLC == 8)
{
canpybara_bootloader_write(*((uint64_t*)hcan->pRxMsg->Data));
canpybara_bootloader_write(*((uint64_t*)can_rx_data));
}
break;

default:
LOG("Unknown request ID: %d", request_id);
}

canpybara_reload_canrx(hcan);
}


void canpybara_can_tx(CanTxMsgTypeDef *can_tx)
void canpybara_can_tx(CAN_TxHeaderTypeDef* can_tx_header, uint8_t data[8])
{
hcan.pTxMsg = can_tx;

if(HAL_CAN_Transmit(&hcan, 100) != HAL_OK)
uint32_t mailbox_no;
if(HAL_CAN_AddTxMessage(&hcan, can_tx_header, data, &mailbox_no) != HAL_OK)
{
LOG("Can transmit failure");
canpybara_can_error();
LOG("CAN add message failure");
Error_Handler();
}

canpybara_can_tx_complete();
}
}
2 changes: 1 addition & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */

LOG("ERROR HANDLER -> %s:%d", file, line);
LOG("ERROR HANDLER");

/* User can add his own implementation to report the HAL error return state */
while(1)
Expand Down
27 changes: 1 addition & 26 deletions Core/Src/stm32f1xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,40 +315,15 @@ void CAN1_SCE_IRQHandler(void)

/* USER CODE BEGIN 1 */

void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan)
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef* hcan)
{
// LOG("CAN transmisison complete");
// canpybara_can_tx_ready(hcan);
canpybara_can_tx_complete();
}

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
{
// LOG("Received CAN message");

canpybara_can_rx(hcan);
}

void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
canpybara_can_error();

__HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
HAL_CAN_Receive_IT(hcan, CAN_FIFO0);
// LOG("CAN error");
}

void HAL_RCC_CSSCallback(void)
{
LOG("Ceramic resonator has failed");
}

void HAL_SYSTICK_Callback(void)
{
#ifdef WIEGAND_ENABLED
canpybara_wiegand_systick_interrupt();
#endif
}

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 comments on commit 267e48f

Please sign in to comment.