cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3xx CAN (controller area network) HAL_CAN_Receive

abdou si
Associate III
Posted on April 27, 2017 at 21:01

Hello 

I am working with CAN of STM32F303K8. I can transmit data but I can't receive data; the function HAL_CAN_Receive() returns HAL_TIMEOUT. I am working with 1M and 32 clock.

can any one tell me how i can receive data frome CAN 

#stm32f3 #can
8 REPLIES 8
Imen.D
ST Employee
Posted on April 28, 2017 at 16:47

Hi,

You may refer to the last version of STM32CubeF3 V1.8.0 where you can find CAN example that can help you get inspiration to develop your application:

 STM32Cube_FW_F3_V1.8.0\Projects\STM32303C_EVAL\Examples\CAN\CAN_Networking

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on April 28, 2017 at 17:33

thanks for your reply

but i see this exemple and i did the same thingbut it didn't work i need exemple for may STM32F303K8 Nucluo or any Doucument about how to use CAN Function 

Posted on April 28, 2017 at 22:58

Hi abdou si,

I checked your other post so I notice a couple of things on the codes:

First at all, you have to configure a filter for the CAN reception and the parameters for the transmition as the STD id, Ext id, RTR, IDE and DLC, an example code is in the example projects of the STM32F3 HAL and is the following. This filter configuration allows to you to received any message, with any ID and some very important, the message is stored in FIFO0, so when you use the Receive function you should select FIFO0. From the transmition parameters the most you should take care with DLC, because it refers to data long in bytes, it must be a value from 1 to 8, and for testing keep RTR and IDE as the example. You should put this code after the CAN configuration.

CAN_FilterConfTypeDef sFilterConfig;

static CanTxMsgTypeDef TxMessage;

static CanRxMsgTypeDef RxMessage;

/*##-2- Configure the CAN Filter ###########################################*/

sFilterConfig.FilterNumber = 0;

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

sFilterConfig.FilterIdHigh = 0x0000;

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdHigh = 0x0000;

sFilterConfig.FilterMaskIdLow = 0x0000;

sFilterConfig.FilterFIFOAssignment = 0;

sFilterConfig.FilterActivation = ENABLE;

sFilterConfig.BankNumber = 14;

if (HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)

{

/* Filter configuration Error */

Error_Handler();

}

/*##-3- Configure Transmission process #####################################*/

CanHandle.pTxMsg->StdId = 0x321;

CanHandle.pTxMsg->ExtId = 0x01;

CanHandle.pTxMsg->RTR = CAN_RTR_DATA;

CanHandle.pTxMsg->IDE = CAN_ID_STD;

CanHandle.pTxMsg->DLC = 2;

In main source code I suggest to you the following:

Declare all the variables out of the while(1), better at begining of the main function

Keep into the while(1) just receiving data from CAN

Put an else condition for status == HAL_TIMEOUT

Change the FIFO1 to FIFO0

HAL_StatusTypeDef status;

int i=0;

while (1)

{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

status = HAL_CAN_Receive(&hcan, CAN_FIFO0, 100);

if (status == HAL_TIMEOUT)

{

   while(1){}

}

else{

   for (i=0;i<=7;i++)

   {

      printf(''CAN Receive data[%d ]: %x \n '',i,hcan.pRxMsg->Data[i]);

   }

}

/* USER CODE END 3 */

}

Other common issues are from time configurations, I suggest to you to use the CubeMX to inicialice the CAN port, in the clock tab make the configurations of the board clocks and crystals, and in Configuration tab set all the parameters well:

0690X00000605w7QAA.png0690X00000606rQQAQ.png0690X00000606tzQAA.png

Some really really important is in the last image, you must to take on count that both Tx and Rx module must have the SAME configuration!, if one of them don't have the same configuration, the modules will never communicate well.

One last checking that is always important is to make sure that all the hardware is fine, remember to use the can bus chips in both modules, I'm using the 

SN65HVD230 and works really fine, make sure that there is the 120ohms bus resistors, and if you have an oscilloscope check also the Tx and Rx signal, from both  MCUs and in the CAN bus.

Posted on April 29, 2017 at 12:18

Hi

thank you vert matche about your replying 

first i am sure that my can works because i tested using mbed.h

i didn't understunde '

both Tx and Rx module must have the SAME configuration!'

how i can do it 

Posted on April 29, 2017 at 13:27

i can config jut the CAN bus but i dont know how i can confugurat eche one alone 

Posted on April 29, 2017 at 20:19

Hi abdou si,

For making test of CAN conmmunication you should have at least 2 CAN modules, I call them Tx and Rx (in practice them both could perform functions of sending or receiving data). One good test that I did for make sure that the CAN configuration is right is program the 2 modules with the same code, in the code when you press a button send a CAN frame, and also is always trying to receive a frame. Could you tell to me how are you testing the CAN module?

0690X00000606r6QAA.png
Posted on April 29, 2017 at 21:04

they have one CAN module in STM32F303K8

Posted on May 02, 2017 at 17:31

Hi abdou si,

For making a good test of the CAN module you should get other board and try to make a communication between them. If there in not other board that send data, the received function always 

returns HAL_TIMEOUT.