cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CAN Communication

akshayamk
Associate II
Posted on January 04, 2016 at 06:12

Hello,

I have a custom made STM32F302 board where I am using the MCU and pinout but it is implemented on a custom made board. I am trying to establish CAN communication with the controller but I am unable to as I keep getting acknowledgement error (ACK error) on LEC. However, when I tried communication between two boards where one board is listener and one board is sender, communication was successful.

Any suggestions on how to solve this??

#lacks-specificity
9 REPLIES 9
jpeacock
Associate II
Posted on January 04, 2016 at 15:56

CAN requires two nodes to work, which is why there are no errors when you have two boards.  Only one node should report an error since there is no network.  This is intentional and the way it should work, reporting a fault.

  Jack Peacock

Posted on January 04, 2016 at 19:10

Pity we couldn't have kept this in the [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32%20CAN%20Normal%20Mode&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx?Paged%3DTRUE%26p_StickyPost%3D%26p_DiscussionLastUpdated%3D20151230%252012%253a19%253a26%26p_ID%3D61558%26View%3D%257bF47A9ED8%252dE726%252d42BE%252dACED%252d732F13B66581%257d%26FolderCTID%3D0x012001%26PageFirstRow%3D41&currentviews=61]original thread...

Any suggestions on how to solve this??

Yes, it suggests you have your baud rate settings WRONG. So look at your APB clock rate, the prescaler, and the bit quanta settings, and then use the correct rate for the equipment you are trying to work with.

Think about the details you are missing from your descriptions to this point, and what details might be important to relate in solving your problem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
akshayamk
Associate II
Posted on January 05, 2016 at 08:45

Hi Clive,

I have checked the baud rate and tried changing it but it still does not work. My clock settings are as follows:

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;

// RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV16;

  RCC_OscInitStruct.LSIState = RCC_LSI_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL5;

// RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;

  HAL_RCC_OscConfig(&RCC_OscInitStruct);

 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2; /*12.02 us*/

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_TIM15|RCC_PERIPHCLK_TIM16

                              |RCC_PERIPHCLK_TIM17;

  PeriphClkInit.Tim15ClockSelection = RCC_TIM15CLK_HCLK;

  PeriphClkInit.Tim16ClockSelection = RCC_TIM16CLK_HCLK;

  PeriphClkInit.Tim17ClockSelection = RCC_TIM17CLK_HCLK;

  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

  HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_SYSCLK, RCC_MCO_DIV1);

  __SYSCFG_CLK_ENABLE();

akshayamk
Associate II
Posted on January 05, 2016 at 08:53

Hi Jack,

As you mentioned, two nodes are required for CAN communication. But I cannot establish successful communication when I am using one of my custom made STM32 boards with 1769-ECR Allen Bradley controller. I only get error handler and when I checked the Error register, I am getting ACK error.

pkumar1883
Associate II
Posted on January 05, 2016 at 09:55

Dear mk.shasha,

Please verify the follwing ---

Hardware:--

1. you are using right CAN Transceiver with STM32. Because STM32 CAN works at TTL logics. Dont know what level 1769 ecr allen bradley does support. So both end should have same logics.

2. Both end have proper termination resistance(120 E).

 Firmware:--

1. You have configured you STM32 CAN peripheral with right baud settings.

    So check at what speed at what speed 1769 ecr allen bradley is working.So configure accordingly.

akshayamk
Associate II
Posted on January 05, 2016 at 10:27

Hi kumar.pushpendra,

These are my CAN settings:

 // hcan.Init.Prescaler = 4;

 hcan.Init.Prescaler = 16;

  hcan.Init.Mode = CAN_MODE_NORMAL;

 //hcan.Init.Mode = CAN_MODE_LOOPBACK;

  hcan.Init.SJW = CAN_SJW_2TQ;

  hcan.Init.BS1 = CAN_BS1_7TQ;

  hcan.Init.BS2 = CAN_BS2_2TQ;

  hcan.Init.TTCM = DISABLE;

  hcan.Init.ABOM = DISABLE;

  hcan.Init.AWUM = DISABLE;

  hcan.Init.NART = DISABLE;

 // hcan.Init.NART = ENABLE;

 hcan.Init.RFLM = DISABLE;

  hcan.Init.TXFP = DISABLE;

  HAL_CAN_Init(&hcan);

I have configured to set baud rate at 125kbps and I have also set the controller baud rate to 12kbps but yet I am not able to establish communication.

Posted on January 05, 2016 at 13:46

To get 125Kbps from that you'd need the APB running at 22 MHz

I'm not able to deduce the speed of your system from the scant details provided, but guessing it's not 44 or 22 MHz

Please provide specific details of the board, the input clocks, and the expectations for the internal clocks. These would help significantly in a technical discussion of the problem you are encountering.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pkumar1883
Associate II
Posted on January 06, 2016 at 09:23

Dear mk.shasha,

The following piece of code is tested with STM32F103x (running at 72 MHz, 8 MHz HSE ) at 125 kbps -

CAN_InitTypeDef CAN_InitStructure;

RCC_APB1PeriphClockCmd(  RCC_APB1Periph_CAN1 , ENABLE);//| RCC_APB1Periph_I2C1 |

   /* CAN register init */

   CAN_DeInit(CAN1);

   CAN_StructInit(&CAN_InitStructure);

   /* CAN cell init 125kbps*/

   CAN_InitStructure.CAN_TTCM = DISABLE;

   CAN_InitStructure.CAN_ABOM = DISABLE;

   CAN_InitStructure.CAN_AWUM = DISABLE;

   CAN_InitStructure.CAN_NART = DISABLE;

   CAN_InitStructure.CAN_RFLM = DISABLE;

   CAN_InitStructure.CAN_TXFP = DISABLE;

   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

   CAN_InitStructure.CAN_BS1 = CAN_BS1_2tq;

   CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;

   CAN_InitStructure.CAN_Prescaler = 48;

   CAN_Init(CAN1, &CAN_InitStructure);

Just try to get setting for 125kbps.

akshayamk
Associate II
Posted on January 06, 2016 at 10:35

Hello Clive,

Thanks, I was able to transmit and receive successfully. After doing a power cycle, the controller was able to communicate with my board. I realised the main difference was changing the prescaler under CAN setting. Thanks for your help!