2021-07-23 01:05 AM
Hello,
I am trying to put in place CAN protocol on my Nucleo but I am facing a problem. The callback function is never called consequently the buffer of reception is always empty.
I try a lot of ways to fix the problem but no way.
Any help or suggestion will be appreciated!!
Here is my code!!
Thank you !!
CAN_HandleTypeDef hcan1;
UART_HandleTypeDef huart3;
CAN_TxHeaderTypeDef pHeader;
CAN_RxHeaderTypeDef rHeader;
uint32_t pTxMailbox;
uint8_t count[8],r[8];
uint8_t flag =0;
uint32_t RxFifo;
// main function
int main(void)
{
CAN_FilterTypeDef sFilterConfig;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_CAN1_Init();
printf("Debug \r\n");
pHeader.DLC= 8;
pHeader.IDE =CAN_ID_STD;
pHeader.RTR=CAN_RTR_DATA;
pHeader.StdId=0x00;
sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;
sFilterConfig.FilterIdHigh=0;
sFilterConfig.FilterIdLow=0;
sFilterConfig.FilterMaskIdHigh=0;
sFilterConfig.FilterMaskIdLow=0x00;
sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterActivation=CAN_FILTER_ENABLE;
sFilterConfig.SlaveStartFilterBank = 0;
if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
Error_Handler();
if (HAL_CAN_Start(&hcan1) !=HAL_OK)
Error_Handler();
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
Error_Handler();
while (1)
{
if (HAL_GPIO_ReadPin(USER_Btn_GPIO_Port, USER_Btn_Pin) == NOPRESSED)
{
count[0] = 0xAB;
count[1] = 0xAD;
printf("%d", count[0]);
printf("Debug 2 \r\n");
if (HAL_CAN_AddTxMessage(&hcan1, &pHeader, count, &pTxMailbox) != HAL_OK)
Error_Handler();
HAL_GPIO_WritePin(GPIOB, LD1_Pin,GPIO_PIN_SET);
HAL_Delay(500);
}
HAL_GPIO_WritePin(GPIOB, LD1_Pin,GPIO_PIN_RESET);
if (flag)
{
flag = 0;
printf("%d", r[0]);
HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_SET);
HAL_Delay(10);
}
HAL_GPIO_WritePin(GPIOB, LD3_Pin, GPIO_PIN_RESET);
}
}