cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407G-Disc1 --> Controller Area Network receiver is not working in loopback mode

sriramdhivakar
Associate II

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));"

1 ACCEPTED SOLUTION

Accepted Solutions
Karl Yamashita
Lead II

Use the code sample format your code so it's easier to read

UseCode Snippet.png

 

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

If you find my answers useful, click the accept button so that way others can see the solution.

View solution in original post

2 REPLIES 2
Karl Yamashita
Lead II

Use the code sample format your code so it's easier to read

UseCode Snippet.png

 

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

If you find my answers useful, click the accept button so that way others can see the solution.

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.

sriramdhivakar_0-1699593065819.png

Thank You for your support.