2023-11-08 10:32 AM
Hi Everyone,
i'm using two boards for my hobby purpose(stm32f108cxx bluepill board and stm32f407g discovery board). i tried the controller area network loopback mode in bluepill is working fine but in the stm32f407 discovery board controller area network is not working. For CAN transceiver i'm using sn65hvd230 IC. when absorb in the debugging mode transmitter is working fine but when pointer comes to receiver its not working. i pasted the code below
static void MX_CAN1_CONFIG(void)
{
CAN_FilterTypeDef can_filter;
can_filter.FilterActivation = CAN_FILTER_ENABLE;
can_filter.FilterBank = 0;
can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
can_filter.FilterIdHigh = 0x0000;
can_filter.FilterIdLow = 0x0000;
can_filter.FilterMaskIdLow = 0x0000;
can_filter.FilterMaskIdLow = 0x0000;
can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
can_filter.SlaveStartFilterBank = 14;
if(HAL_CAN_ConfigFilter(&hcan1, &can_filter) != HAL_OK)
{
Error_Handler();
}
if(HAL_UART_Transmit(&huart2,(uint8_t *)msg[2], strlen(msg[2]), 10) != HAL_OK)
{
Error_Handler();
}
}
static void MX_CAN1_TX(void)
{
CAN_TxHeaderTypeDef Tx_Handler;
uint8_t tmsg[] = "Hello";
uint32_t TxMailbox;
Tx_Handler.DLC = 8;
Tx_Handler.ExtId = 0;
Tx_Handler.IDE = CAN_ID_STD;
Tx_Handler.RTR = CAN_RTR_DATA;
Tx_Handler.StdId = 0x65d;
Tx_Handler.TransmitGlobalTime = DISABLE;
if(HAL_CAN_AddTxMessage(&hcan1, &Tx_Handler, tmsg,&TxMailbox ) != HAL_OK)
{
Error_Handler();
}
while(HAL_CAN_IsTxMessagePending(&hcan1, TxMailbox));
if(HAL_UART_Transmit(&huart2,(uint8_t *)msg[3], strlen(msg[3]), 10) != HAL_OK)
{
Error_Handler();
}
}
static void MX_CAN1_RX(void)
{
CAN_RxHeaderTypeDef can_rxheader;
uint8_t rx_data[5];
while(!HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_FILTER_FIFO0));
if(HAL_CAN_GetRxMessage(&hcan1,CAN_FILTER_FIFO0,&can_rxheader, rx_data) != HAL_OK)
{
Error_Handler();
}
if(HAL_UART_Transmit(&huart2,(uint8_t *)msg[4], strlen(msg[4]), 10) != HAL_OK)
{
Error_Handler();
}
}
code is stuck in the receiver section
"while(!HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_FILTER_FIFO0));"
Solved! Go to Solution.
2023-11-08 11:09 PM
Use the code sample format your code so it's easier to read
Though not the reason why you're not receiving CAN data, your array rx_data size is only 5 but you're transmitting DLC is 8 bytes. So it's more than likely going to hard fault when you call HAL_CAN_GetRxMessage.and it tries to fit "Hello" and 3 extra unknown bytes into an array of 5
2023-11-08 11:09 PM
Use the code sample format your code so it's easier to read
Though not the reason why you're not receiving CAN data, your array rx_data size is only 5 but you're transmitting DLC is 8 bytes. So it's more than likely going to hard fault when you call HAL_CAN_GetRxMessage.and it tries to fit "Hello" and 3 extra unknown bytes into an array of 5
2023-11-09 09:15 PM - edited 2023-11-09 09:16 PM
Hi Karl Yamashita,
I got it. Mistake in my code only. That is in the CAN configuration block two times written Filtermask_low. need to give one filtermask_high and filtermask_low. so i corrected now its data is receiving.
Thank You for your support.