2017-06-25 07:33 PM
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 */
}}
2017-06-25 09:35 PM
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.
2017-06-26 02:53 AM
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);/***************************************************************************************************/
2017-06-26 12:25 PM
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.
2017-06-26 12:26 PM
Thank you for your response.
That does make things easier.
2017-06-27 12:26 PM
There's a CAN example in the
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.2017-06-27 03:39 PM
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 <
2017-06-27 04:12 PM
Make sure the GPIOA clock is enabled before configuration.
Step in debug and check the RCC, CAN, GPIO in peripheral view.
2017-06-28 04:27 PM
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=false2017-06-29 08:14 AM
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: 13time seg2: 248mhz / 1+13+2 = 3mhz / 24 = 125khz
time for one bit: 8000ns = 125khzThe 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.