cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 & CAN bus - problem with communication

anulka
Associate
Posted on March 24, 2014 at 19:33

I have STM32F429-Disco board and I'm trying to connect two CAN interfaces, which this board has. 

 

 

My parts of the code are:

/* CAN1 init function */

void MX_CAN1_Init(void)

{

  hcan1.Instance = CAN1;  

  hcan1.Init.Prescaler = 8; // Specifies the length of a time quantum.

  hcan1.Init.Mode = CAN_MODE_NORMAL; // Specifies the CAN operating mode.

  hcan1.Init.SJW = CAN_SJW_1TQ; // Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or shorten a bit to perform resynchronization.

  hcan1.Init.BS1 = CAN_BS1_10TQ; // Specifies the number of time quanta in Bit Segment 1. 

  hcan1.Init.BS2 = CAN_BS2_7TQ; // Specifies the number of time quanta in Bit Segment 2.

  hcan1.Init.TTCM = DISABLE; // Enable or disable the time triggered communication mode.

  hcan1.Init.ABOM = DISABLE; // Enable or disable the automatic bus-off management.

  hcan1.Init.AWUM = DISABLE; // Enable or disable the automatic wake-up mode.

  hcan1.Init.NART = DISABLE; // Enable or disable the non-automatic retransmission mode.

  hcan1.Init.RFLM = DISABLE; // Enable or disable the receive FIFO Locked mode.

  hcan1.Init.TXFP = DISABLE; // Enable or disable the transmit FIFO priority.

hcan1.pTxMsg = &hcan1Transmit;

hcan1.pRxMsg = &hcan1Receive;

  HAL_CAN_Init(&hcan1);

hcan1Filter.FilterNumber = 0; // Specifies the filter which will be initialized

hcan1Filter.FilterMode = CAN_FILTERMODE_IDMASK; // Specifies the filter mode to be initialized.

hcan1Filter.FilterScale = CAN_FILTERSCALE_32BIT; // Specifies the filter scale.

hcan1Filter.FilterIdHigh = 0x00; // Specifies the filter identification number (MSBs for a 32-bit configuration, first one for a 16-bit configuration).

hcan1Filter.FilterIdLow = 0x00; // Specifies the filter identification number (LSBs for a 32-bit configuration, second one for a 16-bit configuration).

hcan1Filter.FilterMaskIdHigh = 0x00; // Specifies the filter mask number or identification number, according to the mode (MSBs for a 32-bit configuration, first one for a 16-bit configuration).

hcan1Filter.FilterMaskIdLow = 0x00; // Specifies the filter mask number or identification number, according to the mode (LSBs for a 32-bit configuration, second one for a 16-bit configuration).

hcan1Filter.FilterFIFOAssignment = 0; // Specifies the FIFO (0 or 1) which will be assigned to the filter.

hcan1Filter.FilterActivation = ENABLE; // Enable or disable the filter.

hcan1Filter.BankNumber = 0; // Select the start slave bank filter

HAL_CAN_ConfigFilter(&hcan1, &hcan1Filter);

 void MX_CAN2_Init(void)

{

  hcan2.Instance = CAN2;

  hcan2.Init.Prescaler = 8;

  hcan2.Init.Mode = CAN_MODE_NORMAL;

  hcan2.Init.SJW = CAN_SJW_1TQ;

  hcan2.Init.BS1 = CAN_BS1_10TQ;

  hcan2.Init.BS2 = CAN_BS2_7TQ;

  hcan2.Init.TTCM = DISABLE;

  hcan2.Init.ABOM = DISABLE;

  hcan2.Init.AWUM = DISABLE;

  hcan2.Init.NART = DISABLE;

  hcan2.Init.RFLM = DISABLE;

  hcan2.Init.TXFP = DISABLE;

hcan2.pTxMsg = &hcan2Transmit;

hcan2.pRxMsg = &hcan2Receive;

  HAL_CAN_Init(&hcan2);

hcan2Filter.FilterNumber = 0; // Specifies the filter which will be initialized

hcan2Filter.FilterMode = CAN_FILTERMODE_IDMASK; // Specifies the filter mode to be initialized.

hcan2Filter.FilterScale = CAN_FILTERSCALE_32BIT; // Specifies the filter scale.

hcan2Filter.FilterIdHigh = 0x00; // Specifies the filter identification number (MSBs for a 32-bit configuration, first one for a 16-bit configuration).

hcan2Filter.FilterIdLow = 0x00; // Specifies the filter identification number (LSBs for a 32-bit configuration, second one for a 16-bit configuration).

hcan2Filter.FilterMaskIdHigh = 0x00; // Specifies the filter mask number or identification number, according to the mode (MSBs for a 32-bit configuration, first one for a 16-bit configuration).

hcan2Filter.FilterMaskIdLow = 0x00; // Specifies the filter mask number or identification number, according to the mode (LSBs for a 32-bit configuration, second one for a 16-bit configuration).

hcan2Filter.FilterFIFOAssignment = 0; // Specifies the FIFO (0 or 1) which will be assigned to the filter.

hcan2Filter.FilterActivation = ENABLE; // Enable or disable the filter.

hcan2Filter.BankNumber = 0; // Select the start slave bank filter

HAL_CAN_ConfigFilter(&hcan2, &hcan2Filter);

}

 /* Transmit Structure preparation CAN1 */

hcan1Transmit.StdId = 0x321; // Specifies the standard identifier.

hcan1Transmit.ExtId = 0x02; // Specifies the extended identifier.

hcan1Transmit.RTR = CAN_RTR_DATA; // Specifies the type of frame for the message that will be transmitted. < Data frame */

hcan1Transmit.DLC = 1; // Specifies the length of the frame that will be transmitted.

hcan1Transmit.IDE = CAN_ID_STD; // Specifies the type of identifier for the message that will be transmitted. < Standard Id */

hcan1Transmit.Data[0] = 0;

Init_RxMessage(&hcan1Receive);

/* Enable CAN1 interrupt request */

__HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_FMP0);

/* Transmit Structure preparation CAN2 */

hcan2Transmit.StdId = 0x320; // Specifies the standard identifier.

hcan2Transmit.ExtId = 0x01; // Specifies the extended identifier.

hcan2Transmit.RTR = CAN_RTR_DATA; // Specifies the type of frame for the message that will be transmitted. < Data frame */

hcan2Transmit.DLC = 1; // Specifies the length of the frame that will be transmitted.

hcan2Transmit.IDE = CAN_ID_STD; // Specifies the type of identifier for the message that will be transmitted. < Standard Id */

hcan2Transmit.Data[0] = 0;

Init_RxMessage(&hcan2Receive);

/* Enable CAN2 interrupt request */

__HAL_CAN_ENABLE_IT(&hcan2, CAN_IT_FMP0);

 

I'm connecting these two CAN interfaces through MCP2551 transceiver. 

 If I change CAN mode to LOOPBACK and test, everything works ok, but when it is in NORMAL mode, it seems there's a problem. I cannot transmit anything through the bus. 

Where am I wrong?

#bxcan #can #can #filter #stm32f2 #metoo #can-bus-normal-mode-problem-init
7 REPLIES 7
Posted on March 24, 2014 at 19:57

CAN2 using Filter 0? Are you sure?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jpeacock2399
Associate II
Posted on March 25, 2014 at 14:45

Is the CAN bus terminated?  Are you using twisted-pair wiring?  Are both nodes connected and enabled?  CAN won't run with only one node.

Look at the ESR error status register, especially what error codes youa re getting in the LEC.  If you see bit dominant or bit recessive errors check the cable and termination.  Are both nodes at the same speed, and is the bit timing set correctly for the data rate?

Make sure the filter banks are allocated properly.  You need to tell the CAN controller where to split banks between CAN1 and CAN2.

  Jack Peacock
anulka
Associate
Posted on March 25, 2014 at 16:15

0690X000006056SQAQ.png

These is my scheme. I'll check with CAN split banks...

LEC gave me 0x03 error but now there's none.
jpeacock2399
Associate II
Posted on March 25, 2014 at 23:48

Setting filters has to be through CAN1, not CAN2.  The two controllers are not fully independent.  CAN2 cannot access filter memory.

hcan1Filter.BankNumber = 0; // Select the start slave bank filter

HAL_CAN_ConfigFilter(&hcan1, &hcan1Filter);

Split your banks at 14 instead of 0 - filters 0-13 for CAN1, and filters 14-27 for CAN2.  You must set this using CAN1 since it's filtering.  Set this once for oth CAN1 and CAN2

HAL_CAN_ConfigFilter(&hcan2, &hcan2Filter);

You have to use hcan1 to configure a filter for CAN1 or CAN2.

  Jack Peacock

mubinicyer
Associate II
Posted on May 07, 2014 at 00:00

Hi,

Your connection is wrong. You have to connect RX of CAN transciever to the RX pin of STM32, not TX pin. Same apply for TX pin. 

MCP2551.RX ---> STM32.RX

MCP2551.TX----> STM32.TX

zwitch
Associate
Posted on August 14, 2014 at 18:16

Hello! You solved your problem? I have similar difficulties, can't solve yet. You couldn't show all code which carries out an exchange on CAN?

Posted on August 14, 2014 at 18:25

Hello! You solved your problem? I have similar difficulties, can't solve yet. You couldn't show all code which carries out an exchange on CAN?

Some of us have significantly fewer problems. [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Can-Bus%20Error&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=122]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Can-Bus%20Error&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=122

Suggest you state your specific issues, with information about what boards you're using, and how they are connected.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..