STM32f103 / f303 CAN TX works only in loopback mode
Posted: Wed Mar 24, 2021 5:12 pm
Hello,
I am trying to get the can bus running in both directions (TX and RX) on stm32f103rb and stm32f303re nucleo 64 boards. I have got things running in one direction including CAN filters, but only in the loopback mode, i.e. if BRT_LBKM is set. The same results were achieved with the examples given in testhal, where in a first trial I modified the board.h file for the f103 nucleo board according to the provided Olimex example. In a second trial I used the original board-files but was setting the ports as follows:
static const CANConfig cancfg = {
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
// CAN_BTR_LBKM |
CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
CAN_BTR_TS1(4) | CAN_BTR_BRP(17)
};
and for the
halInit();
chSysInit();
palSetPadMode (GPIOB, 8, PAL_MODE_INPUT);
palSetPadMode (GPIOA, 9, PAL_MODE_OUTPUT_PUSHPULL);
AFIO->MAPR |= AFIO_MAPR_CAN_REMAP_REMAP2; //CAN PB8/PB9
canStart(&CAND1, &cancfg);
for the strm32f103 board and
palSetPadMode (GPIOA, 11, PAL_MODE_INPUT);
palSetPadMode (GPIOA, 12, PAL_MODE_OUTPUT_PUSHPULL); // order matters, alternate(9) 2nd
palSetPadMode (GPIOA, 12, PAL_MODE_ALTERNATE(9)); // f303re
for the stm32f303re.
According to a first advise from Giovanni I defined the output pin for the f303 as
palSetPadMode(GPIOA, 12, (PAL_STM32_MODE_ALTERNATE | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_ALTERNATE(9))),
without any effect, since palSetPadMode (GPIOA, 12, PAL_MODE_ALTERNATE(9)) was already sufficient to get a can signal in loopback mode: In case of not properly set outputs I should not have been able to receive any messages by a second f103 on the can bus. The problem seems to be related to the setup of LBKM:
If LBKM is set, the LED blinks and a signal can be measured at the TX pins. If LBKM is not set,
in case of the f103 the messages are send with very short delays between the messages at PA12, i.e. 0.1 ms instead of the required 100ms. If PB9 is used, some short periodic pulse sequences can be seen, but no CAN signal. In case of the f303 no output can be observed without setting LBKM.
Since I am anything else but an embedded computer expert, I would be highly thankful, if anyone could modify the basic testhal-examples (basic nucleo 64 boards or the blue pill) for running TX and RX simultaneously out of the box.
Thanks for help
Hartmut, j10
I am trying to get the can bus running in both directions (TX and RX) on stm32f103rb and stm32f303re nucleo 64 boards. I have got things running in one direction including CAN filters, but only in the loopback mode, i.e. if BRT_LBKM is set. The same results were achieved with the examples given in testhal, where in a first trial I modified the board.h file for the f103 nucleo board according to the provided Olimex example. In a second trial I used the original board-files but was setting the ports as follows:
static const CANConfig cancfg = {
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
// CAN_BTR_LBKM |
CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
CAN_BTR_TS1(4) | CAN_BTR_BRP(17)
};
and for the
halInit();
chSysInit();
palSetPadMode (GPIOB, 8, PAL_MODE_INPUT);
palSetPadMode (GPIOA, 9, PAL_MODE_OUTPUT_PUSHPULL);
AFIO->MAPR |= AFIO_MAPR_CAN_REMAP_REMAP2; //CAN PB8/PB9
canStart(&CAND1, &cancfg);
for the strm32f103 board and
palSetPadMode (GPIOA, 11, PAL_MODE_INPUT);
palSetPadMode (GPIOA, 12, PAL_MODE_OUTPUT_PUSHPULL); // order matters, alternate(9) 2nd
palSetPadMode (GPIOA, 12, PAL_MODE_ALTERNATE(9)); // f303re
for the stm32f303re.
According to a first advise from Giovanni I defined the output pin for the f303 as
palSetPadMode(GPIOA, 12, (PAL_STM32_MODE_ALTERNATE | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_ALTERNATE(9))),
without any effect, since palSetPadMode (GPIOA, 12, PAL_MODE_ALTERNATE(9)) was already sufficient to get a can signal in loopback mode: In case of not properly set outputs I should not have been able to receive any messages by a second f103 on the can bus. The problem seems to be related to the setup of LBKM:
If LBKM is set, the LED blinks and a signal can be measured at the TX pins. If LBKM is not set,
in case of the f103 the messages are send with very short delays between the messages at PA12, i.e. 0.1 ms instead of the required 100ms. If PB9 is used, some short periodic pulse sequences can be seen, but no CAN signal. In case of the f303 no output can be observed without setting LBKM.
Since I am anything else but an embedded computer expert, I would be highly thankful, if anyone could modify the basic testhal-examples (basic nucleo 64 boards or the blue pill) for running TX and RX simultaneously out of the box.
Thanks for help
Hartmut, j10