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
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
Up vote any posts that you find helpful, it shows what's working..
Nicolas Cmb
Associate III
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);

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

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.

Posted on June 26, 2017 at 19:26

Thank you for your response. 

That does make things easier.

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.
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 <

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
Up vote any posts that you find helpful, it shows what's working..
Posted on June 28, 2017 at 23:27

Actually, I think you just have to change the Pull configuration.

GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP; //GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF9_TIM1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

I tested this IO config with your CAN_Config() on a F302R8 Nucleo and I was able to verify transmission activity on PA12 pin. I'm attaching my test project for your reference.

________________

Attachments :

CAN_Test2_code.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyaJ&d=%2Fa%2F0X0000000b9n%2FNY0nxzQ6mgJt9f1xCvZRdZAMFBJemysEivdp3BCsPEM&asPdf=false
Mark Shoe
Senior
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.