2017-10-19 02:11 AM
(note this is my first question on ST community If I can do anything to improve the question, please let me know).
Setup
I am using an STM32F103C8T6 (aka Blue Pill).
With the STM32Cube I set CAN_RX to PB8 and CAN_TX9 to PB9 (these are defaults/nonchangeable).The generated CAN settings are:
hcan.Instance = CAN1;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_4TQ;
hcan.Init.BS2 = CAN_BS2_5TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;�?�?�?�?�?�?�?�?�?�?�?�?
Program
(removed STM HAL generated comments blocks) int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN_Init();
/* USER CODE BEGIN 2 */
hcan.pRxMsg = &_rxMessage;
hcan.pTxMsg = &_txMessage;
_sFilterConfig.FilterActivation = ENABLE;
_sFilterConfig.FilterNumber = 0;
_sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
_sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
_sFilterConfig.FilterIdHigh = 0x0000;
_sFilterConfig.FilterIdLow = 0x0000;
_sFilterConfig.FilterMaskIdHigh = 0x0000;
_sFilterConfig.FilterMaskIdLow = 0x0000;
_sFilterConfig.FilterFIFOAssignment = 0;
_sFilterConfig.BankNumber = 14;
if (HAL_CAN_ConfigFilter(&hcan, &_sFilterConfig) != HAL_OK)
{
Error_Handler();
}
hcan.pTxMsg->StdId = 0x321;
hcan.pTxMsg->ExtId = 0x01;
hcan.pTxMsg->RTR = CAN_RTR_DATA;
hcan.pTxMsg->IDE = CAN_ID_STD;
hcan.pTxMsg->DLC = 2;
hcan.pTxMsg->Data[0] = 0;
while (1)
{
hcan.pTxMsg->Data[0]++;
if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(1000);
if (HAL_CAN_Receive_IT(&hcan, CAN_FIFO0) != HAL_OK)
{
Error_Handler();
}
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
Loopback mode
When I use loopback mode:hcan.Init.Mode = CAN_MODE_LOOPBACK
instead of Normal mode, I can transmit and receive messages (and the hcan shows the correct data in the received message).
Problem
However, in Normal mode (as shown in the code fragment above) I always get a timeout in the next command:if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
The function returns: HAL_CAN_STATE_TIMEOUT
All initialization seems to be ok (all functions return HAL_OK).
Analysis
What I tried was:- Using another STM32: no difference
- Using another transceiver: no difference - Played a bit with the SJW/BS1/BS2 time quantities: no difference - Making sure the time quantities were equal - Playing with different data values and filters: no difference - Checked the output of PB9 (CAN transmit): it seems not to change at all (so this is a problem): no difference - Removing the wire from GPIO PB9 (CAN Tx) to the TX of my CAN transceiver : no difference. - Checking the Transmit is cancelled (which is needed and was an old bug, but has already been fixed by the HAL library I'm using).Observations
- ESR is 111110000000000001010111, which means LEC[2:0] (see [Reference manual, page 682][1]), is 101 which is Bit dominant error
- BTR is 11101010000000001001111, which means bit 30 LKBM: Loop back Mode (debug) is 1, which is strange because in the setup I assigned CAN_MODE_NORMAL. - Directly after the command, the State is HAL_CAN_STATE_TIMEOUT, the Lock is HAL_UNLOCKED, the ErrorCode is 0.Questions
1. Why do I get a timeout? I'm having a hard time trying to find more to check what I already did. What bothers me is that the PB9 GPIO does not change, so I'm sure it's a software problem, but since there is no error code (it's 0) and initialization is OK, I'm getting out of clues.Related question:
2. Am I in loopback mode or not (according to the Setup I put CAN_MODE_NORMAL, but the BTR bit 30: LKBM shows otherwise.
[1]:
#can #stm32f103 #stm32cube2017-10-20 08:15 AM
Hi
michelkeijzers
,I appreciate the way you are describing your problem. This makes it easier to understand.I recommend you also to use the ''Syntax Highlighter'' to make the code formatted:
Then, regarding the usage of the CAN in Normal mode, I recommend you to review the exampleSTM32Cube_FW_F1_V1.6.0\Projects\STM3210E_EVAL\Examples\CAN\CAN_Networking.
Can you test this example (after porting it to your own HW)?
Then, I would like to come back to this note in your description:
''
With the STM32Cube I set CAN_RX to PB8 and CAN_TX9 to PB9 (these are defaults/nonchangeable).''
This is not right: You can check other choices putting cursor on the selected pin by CubeMX, then <Ctrl + click>.
-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.
2017-10-20 08:34 AM
>>Why do I get a timeout?
You need something on the bus to respond that it actually received the message, sending it into the abyss won't work. If you have another responding device then you need to make sure they are both configured at the same speed.
2017-10-20 10:37 AM
Using the Syntax Highlighter always puts things into Moderation, it is a conversation killer if that takes 4 or 8 hours to clear. Ideally such post shouldn't flag, but the delivery target should be under 5 minutes.
2017-10-20 03:09 PM
Thank you ... I will try it (not sure if I have much time this weekend). I'm a beginner with STM32 so hope I can port it.
I don't think I'm able to edit my question (I cannot find the option). But for new code I will use it, thanks.And you are right, it's possible to change the pins (I didn't know the Control-shortcut). I can try it, but I doubt it will make a difference.2017-10-20 03:10 PM
I have two microcontrollers with receivers and they both run the same program (thus same speed).
2017-10-21 03:48 AM
(Since I followed up the advice to edit the question because of code-coloring, I lost all replies :( ) .... Someone wrote me to check the examples, which I will do.
2017-10-22 07:27 AM
To edit you need to go to the thread view not the inbox view. Click on the thread title, and Edit should be under the Action options.
2017-10-22 07:33 AM
Thanks Clive ... I'm new to ST community (and STM) ... but luckily I have all the emails from you and Amel N.
Today if I have time (not sure) I try to check with an official example from STM10E (which is somewhat similar to STM32F103).2017-10-22 05:41 PM
st.mcu
I copied as much as possible from the example, but without any difference. I found however something stupid: my CAN receiver works on 5V and my STM32 is 3.3V, so I added a voltage supply that gives 5V and use that for the CAN transceivers (and connecting all grounds together). But still no difference. I ordered specifically 3.3V CAN transceivers and will try again when they arrive (can take some time).