cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F302R8 CANBus Not Working

Marshal Haupt
Associate II
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 */

}

}

16 REPLIES 16
Marshal Haupt
Associate II
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.

Posted on July 18, 2017 at 02:24

Joe, 

Thank you for sending that code. Unfortunately, I am still failing. I meticulously recreated the project you had by generating code from the MXCube file for the OpenSTM32 software (Eclipse), and then copied your source and include folders over those that were generated. It built and loaded on the board no problem. However, still nothing from PA12 (sixth pin down on right outside of Nucleo). I don't have it connected to a working bus, but I assumed you meant you were able to get it working with it not connected to a transceiver - please correct me if I'm wrong.

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.

T J
Lead
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.

Posted on July 18, 2017 at 05:59

Correct, I was able to get it working with it not connected to a transceiver. Did you try running the project out of the box?  You should see some activity on the bus.

Posted on July 18, 2017 at 06:00

So, not too long after telling you it wasn't working, it is working. I must be measuring the pin voltage wrong (how, I have no idea, as I use this o-scope more than my car), but when I went ahead and connected it to the bus, it started talking. Thank you for your help

Marshal Haupt
Associate II
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.