2020-10-16 06:48 AM
I have a problem using CAN on STM32F103C8T6. I use this MCU on a bluepill board,
a SN64HVD230 based CAN PHY and on the other end of the CAN bus a PiCAN2 board on
Raspberry Pi. I drew a picture to show my setup:
I want to have a 500 kbit/s-CAN-connection, I use the MCU with 16 MHz.
My initialization code (I use the STM32-HAL-API) for the CAN is the following:
#define CAN_RX_PORT GPIOA
#define CAN_RX_PIN LL_GPIO_PIN_11
#define CAN_TX_PORT GPIOA
#define CAN_TX_PIN LL_GPIO_PIN_12
void init_can_gpios(void){
LL_GPIO_SetPinMode(CAN_RX_PORT, CAN_RX_PIN, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(CAN_RX_PORT,CAN_RX_PIN,LL_GPIO_PULL_UP);
LL_GPIO_SetPinMode(CAN_TX_PORT, CAN_TX_PIN, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetPinSpeed(CAN_TX_PORT, CAN_TX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(CAN_TX_PORT,CAN_TX_PIN,LL_GPIO_OUTPUT_OPENDRAIN);
}
void init_can_periph(void){
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_CAN1);
__HAL_RCC_CAN1_CLK_ENABLE();
hcan1.Instance = CAN1;
hcan1.Init.Prescaler=2;
hcan1.Init.TimeSeg1=CAN_BS1_8TQ
hcan1.Init.TimeSeg2=CAN_BS2_7TQ;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = ENABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
HAL_CAN_DeInit(&hcan1);
HAL_StatusTypeDef retval = HAL_CAN_Init(&hcan1);
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING|CAN_IT_TX_MAILBOX_EMPTY);
}
The call of HAL_CAN_INIT(&hcan1) returns HAL_OK.
I try to send a message with:
void can_send_dummy_data(void){
//We send on ID 123, 4 Bytes: 0x3456789A
CAN_TxHeaderTypeDef tx_header;
tx_header.DLC=4;
tx_header.ExtId=0;
tx_header.StdId=0x123;
tx_header.IDE=CAN_ID_STD;
tx_header.RTR=CAN_RTR_DATA;
//Now data
uint8_t tx_data[4]={0x34,0x56,0x78,0x9A};
//Now send
uint32_t tx_mailbox;
HAL_StatusTypeDef retval=HAL_CAN_AddTxMessage(&hcan1,&tx_header,tx_data,&tx_mailbox);
}
The retval ist also HAL_OK. But no message appears on the other side of the CAN Bus.
When I look into the
CAN_MSR:
0b0000 0000 0000 0000 0000 1100 0000 0000
RXM and TXM are set
CAN-TSR:
0b0001 1100 0000 0000 0000 0000 0000 0100
Transmit Mailbox 2 empty
Transmit Mailbox 1 empty
Transmit Mailbox 0 empty
Arbitration lost to mailbox 0
CAN-ESR
0b1000 0010 0000 0000 0000 0000 00010011
Receive error counter = 130 -> Error passive
LEC = Stuff Error
Error Passive flag
Error warning flag
So I have arbitration lost and stuff error and error passive.
If I exchange my Bluepill assembly by an STM8-Discovery-Board running a CAN Hello
world application on STM8, I can send CAN-Messages (500 kbit/s) without problem.
So I assume I made mistakes in my CAN initialization code on STM32.
Solved! Go to Solution.
2020-10-19 01:05 AM
Finally, I got it. I power the bluepill board via an external 3V3 source and not via USB any more. It may have something to do with the double usage of pins PA11 and PA12 with USB. I tried to remap CAN-RX/CAN-TX it to the alternative PB8/PB9, but it did not work.
2020-10-19 12:33 AM
Additional step: If I pull out the USB cable, which means depowering the bluepill board, I can see the CAN message on the bus. How to interpret this?
2020-10-19 12:38 AM
Probably some potential shift relative to GND, because of different power supplies.
You could try to disconnect CAN_GND (leaving only CAN_HI and CAN_LO), which usually works as well.
2020-10-19 01:05 AM
Finally, I got it. I power the bluepill board via an external 3V3 source and not via USB any more. It may have something to do with the double usage of pins PA11 and PA12 with USB. I tried to remap CAN-RX/CAN-TX it to the alternative PB8/PB9, but it did not work.
2020-10-22 08:47 AM
Hi @DNeum.1 ,
I assume that the article "Concurrent use of USB and CAN with STM32F103" provides the explanation for what you see as strange behavior.
-Amel
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2020-10-22 09:20 AM
Since I used only powering by USB, but not the USB peripheral, I thought it would be no problem. But it was... Thank you for your patience.