Skip to main content
Marshal Haupt
Associate II
June 26, 2017
Question

STM32F302R8 CANBus Not Working

  • June 26, 2017
  • 16 replies
  • 2912 views
Posted on June 26, 2017 at 04:33

I need help with the integrated CANBus controller on the NUCLEO STM32F302R8. We are using STM32CubeMX to generate our setup code with the  STM32Cube_FW_F3_V1.8.0v Firmware package provided by ST. We are then using the transmit and receive functions provided by the STM32F3xx_HAL_Driver. The problem is nothing is showing up on the pins. There is nothing on the Tx pin when we set the device to continuously send, and it is not receiving messages from a known-good CANBus network. I suspect the problem is in the setup of the GPIO pins. I have attached my code, but below is a function found in stm32f3xx_hal_msp.c. I suspect that GPIO_InitStruct.Alternate should be GPIO_AF9_CAN.

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(hcan->Instance==CAN)

{

/* USER CODE BEGIN CAN_MspInit 0 */

/* USER CODE END CAN_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_CAN1_CLK_ENABLE();

/**CAN GPIO Configuration

PA11 ------> CAN_RX

PA12 ------> CAN_TX

*/

GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF9_TIM1

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USER CODE BEGIN CAN_MspInit 1 */

/* USER CODE END CAN_MspInit 1 */

}

}

    This topic has been closed for replies.

    16 replies

    Tesla DeLorean
    Guru
    June 26, 2017
    Posted on June 26, 2017 at 06:35

    AF9 is a setting common to several peripherals, the end results is writing 9 into a mux register, I'd assume the define will be the same/equivalent.

    Lost a semicolon somewhere in the process.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Marshal Haupt
    Associate II
    June 26, 2017
    Posted on June 26, 2017 at 19:25

    Thank you for your response. I kind of suspected that about the AF9 setting. The semicolon got lost in the copy and paste. I know the problem is something very simple and that I'm going to feel stupid when I figure it out, but I can not find it. Thank you, again, for taking the time to respond.

    Nicolas Cmb
    Associate III
    June 26, 2017
    Posted on June 26, 2017 at 11:53

    I think you have got trouible with your CAN initialize, btw you can simplified your programm by changing:

    /***********************************************************************************/

    // error indicator ------------------------

    GPIOB->BSRR = LD2_Pin; // turn on ld2

    HAL_Delay(500);

    GPIOB->BRR = LD2_Pin; // turn off ld2

    HAL_Delay(500);

    /***********************************************************************************/

    by 

    /************************************ CHANGE ***********************************************/

    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);

    HAL_Delay(500);

    /***************************************************************************************************/

    Marshal Haupt
    Associate II
    June 26, 2017
    Posted on June 26, 2017 at 19:26

    Thank you for your response. 

    That does make things easier.

    AME MCU Support Center JT
    Associate
    June 27, 2017
    Posted on June 27, 2017 at 21:26

    There's a CAN example in the

    http://www.st.com/en/embedded-software/stm32cubef3.html

    package under 'STM32Cube_FW_F3_V1.7.0\Projects\STM32303C_EVAL\Examples\CAN\CAN_Networking'. Please start by comparing your project to this one. You pin configuration looks correct, so please check your CAN configuration.
    Marshal Haupt
    Associate II
    June 27, 2017
    Posted on June 27, 2017 at 22:39

    Thank you for your assistance, but I am very familiar with both examples

    provided in the the firmware package (v1.7 and v1.8). Looking at those, one

    would assume our code would work. However, it doesn't. I suspect it may be

    something at the API level, wherein the hardware is not properly setup and

    accessed.

    Best regards,

    Marshal Haupt, PE

    Chief Technical Officer

    SD3D

    7925 Silverton Ave. Suite 510

    San Diego, CA 92126

    p: 800-530-9140 <(800)%20530-9140>

    c: 5 <818-635-6613>30-848-1995

    www.sd3d.com <http://www.sd3dprinting.com/>

    <https://www.facebook.com/sd3dprinting>[image: Twitter]

    <https://twitter.com/sd3d>[image: Instagram]

    <https://instagram.com/sd3d_printing/>

    <http://sd3d.com/>

    Automat3D Manufacturing

    On Tue, Jun 27, 2017 at 12:27 PM, tijerina.joe <

    Tesla DeLorean
    Guru
    June 27, 2017
    Posted on June 27, 2017 at 23:12

    Make sure the GPIOA clock is enabled before configuration.

    Step in debug and check the RCC, CAN, GPIO in peripheral view.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Mark Shoe
    Associate III
    June 29, 2017
    Posted on June 29, 2017 at 17:14

    Thje CubeMX is not very helpful calculating the baudrate. You need a prescaler and seg1,and seg2 Here a website that caluclates the values:

    http://www.bittiming.can-wiki.info/

     

    In my case i have a 48mhz cpu and 125khz CAN. The calculated values are:

    prescaler 24

    time seg1: 13

    time seg2: 2

    48mhz / 1+13+2 = 3mhz / 24 = 125khz

    time for one bit: 8000ns = 125khz

    The CAN works fine however got a lot of problem with error flags that cannot be cleared and others. See my other messages, no replies on that.

    Marshal Haupt
    Associate II
    July 18, 2017
    Posted on July 18, 2017 at 03:02

    Thank you. I was able to get the parameters for a 500K baud rate - prescaler 4, BS1 15, BS2 2. Still not working, but that helps.

    Marshal Haupt
    Associate II
    July 18, 2017
    Posted on July 18, 2017 at 03:00

    I apologize for the delay in responding, but I wanted to make sure I tried all the suggestions. At this point, I have built a new project and implemented all the code from the STM32F303C_Eval example project. I debugged, and found the 

    point that the code is failing. The code in the image below is from the stm32f3xx_hal_msp.c file. It is in the CAN_Init function.

     0690X00000603x0QAA.jpg

    On line 297, it puts out a call for a response, then waits for an acknowledgement on line 304. It never gets that bit set, even when I connect to a known working bus. I have two other devices communicating without error, and just listening for something from the nucleo.

    T J
    Senior III
    July 18, 2017
    Posted on July 18, 2017 at 07:10

    There are several primary issues for the hardware BxCAN;

    The RxCAN pin, needs a pullup,

           the internal pullup on the processor pin is good enough.  <- this will stop your code at initialisation.

          where is that problem bit, ABOM .. it needs to be set.      <- this will look like no packets received*

    Then the filters, befuddled in gobbledygook left bewildered we are.

    there are two concepts for filters

    1. bits can be ignored, in this example only the 2 bits are checked

            sFilterConfig.FilterMaskIdHigh    = 0x300 << 5;            //11-bit ID  in top bits

            This register sets the filtering on only 2 bits. 011 0000 0000 <- mask for pattern match

    2. bits must be set in a pattern, in this example, if those 2 bits don't match, the filter will ignore the packet.

            sFilterConfig.FilterIdHigh = 0x100 << 5;   

            the only pattern allowed here is                     001 0000 0000

    address ranges that will match:  

    0x0100 - 0x01FF

    0x0500 - 0x05FF

    other than that, ISOLATED CANBUS for long hauls, to alleviate the ground noise spikes.

    Marshal Haupt
    Associate II
    July 23, 2017
    Posted on July 24, 2017 at 01:50

    Thank you everyone for your suggestions. I was able to recreate the code that Joe provided above and get it to run. Essentially the only difference is the removal of the additional USART that gets added automatically in MXCube for the Nucleo board. I will have to figure out if this was the problem or not eventually, but if anyone knows why this could be, it would help. Thank you, again.