2024-04-17 04:22 PM
Hey, I have a problem with the callback for FDCAN1. I am sending frames through ucandevices converter but the callback for FIFO0 is not called. The frames are definitely arriving, I added a test FreeRTOS and a task to receive and send frames to CAN1 and it worked very well, frames were received and sent back to the bus.
I have NVIC interrupts set up, I fiddled with the clock for CANFD and speeds, also with no effect.
Is anyone able to help me?
I am sending my project as an attachment
2024-04-17 07:03 PM
Maybe your sample point could be an issue? Try these values which is at 87%
You've already defined your variables as global. When you call HAL_FDCAN_GetRxMessage, the values are save into the local variables. The local variables inside the callback will get destroyed when it returns from the callback. The global variables won't get updated.
void HAL_FDCAN_RxFifo0MsgPendingCallback(FDCAN_HandleTypeDef *hfdcan)
{
LED1_ON();
if(hfdcan->Instance == FDCAN1)
{
// already defined as global
//FDCAN_RxHeaderTypeDef RxHeader;
//uint8_t RxData[8];
// Pobierz ramkę
if(HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK)
{
}
}
}
You sure that the UCAN has the same baud rate of 100kbs?
2024-04-18 04:17 AM - edited 2024-04-18 04:19 AM
Hello,
I added a test FreeRTOS and a task to receive and send frames to CAN1 and it worked very well, frames were received and sent back to the bus.
I'm confused about this statement. You said that the callback is not called but you're receiving well the frames.
Could you please clarify this point?
Moreover, as I see from your clock configuration, FDCAN clock is provided by PLL1Q having the clock source HSI.
It's not recommended to use HSI for CAN/FDCAN communication. Use HSE with an external crystal instead.
PS: You are setting your bitrate at 100kb/s. Are you sure it's not 1Mb/s?
2024-04-18 04:34 AM
Another question came to my mind: are you using STM32H750B-DK board?
If yes, there is a discrepancy with your GPIOs settings: PB8/PB9 in CubeMx vs PH13/PH14 in the schematics.
PS: FDCAN2 GPIOs are correct.
2024-04-18 04:43 AM - edited 2024-04-18 05:28 AM
Hello, I am using the ART-PI development board, I am sending the output schematic and documentation. I'll be back from work in 2 hours and I'll still try to change the configuration as my colleague above wrote, currently I'm at a loss.... I need to make a bridge between CAN1 and CAN2, the FreeRTOS task will not be a good solution here to transmit frames in real time. As I mentioned, such code in RTOS task works fine and I can receive and send back to the bus frames.... :(
@EDIT
@SofLit
The canbus communication works, I can receive and send frames on the can bus, but only through the RTOS task or through the while() loop in the main class. I have a problem with the callback I am creating. It is not called despite the NVIC interrupt setting :(
I've been testing different speeds, ultimately I need to work on 500 kBit/s speed, because that's the bus I'll be plugging into in my BMW.
My RTOS task (this example is working):
I'm using TJA1050 from this site:
Transceiver CAN TJA1050 - Sklep msalamon
2024-04-18 07:57 AM
I come back to you with answers. I have switched the clock for FDCAN to HSE, it is set to 25MHz.
I switched the prescaller and Time seg1, Time seg2 to as the colleague above wrote. This unfortunately had no effect. When debugging, HAL_FDCAN_ActivateNotification does not return errors. Sending to can works or receiving when I do it in the main while loop in the main() method.
In the HAL_FDCAN_RxFifo0MsgPendingCallback method I set the LED to light up as a signal that it has been called, no other operations there for the time being, as it makes no sense. I am sending in the attachment once again my project. The speed is scaled to 100kBit/s. I have UCAN set to 100k, so the speeds agree, besides I can see the frames sent by the stm32 from the while loop on it.
2024-04-18 08:37 AM - edited 2024-04-18 08:42 AM
Hello,
System clock is not yet set to HSE (25MHz) in your latest attached project. Still set to HSI.
+ You lost some user code from this latest version.
But I would understand more the case. So, let's summarize:
You are sending frames @100kb/s from a CAN node called "ucan" to FDCAN1 of your STM32H750 MCU.
The callback is not called (using interrupt) but when you are in polling mode (FreeRTOS / without interrupt) you are well receiving messages.
Could you please confirm?
2024-04-18 08:55 AM
Suggestion:
Could you please make a very simple project no RTOS no other stuff and peripherals. Just FDCAN1 in loopback mode. So FDCAN1 will send receive his own frames.
Could you please do this test?
2024-04-18 09:43 AM - edited 2024-04-18 09:45 AM
@SofLit wrote:+ You lost some user code from this latest version.
But I would understand more the case. So, let's summarize:
You are sending frames @100kb/s from a CAN node called "ucan" to FDCAN1 of your STM32H750 MCU.
The callback is not called (using interrupt) but when you are in polling mode (FreeRTOS / without interrupt) you are well receiving messages.
Could you please confirm?
To begin with, I would like to confirm that, exactly that. In the FreeRTOS task, I can receive and send frames.
Now the funniest thing !
I rewrote the clock and inserted a method:
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
This method gets called!!! BUT... It gives me an error when trying to read a message from the FIFO.
I previously inserted the method, without the uint32_t RxFifo0ITs parameter:
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan)
/* Update error code */
hfdcan->ErrorCode |= HAL_FDCAN_ERROR_NOT_STARTED;
2024-04-18 10:05 AM - edited 2024-04-18 10:06 AM
@SofLit Problem solved!!! I don't know what I would have done without your help, in truth you have guided me to this solution.
I found in another topic on the forum information to add in the file stm32h7xx_hal_msp.c
HAL_NVIC_SetPriority(FDCAN1_IT1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(FDCAN1_IT1_IRQn;)
Now the callback receives frames and sends back to the bus without any problems.
One more question, as I mentioned earlier.
I want to build a bridge between CAN1 and CAN2, I understand that for CAN1 I define FIFO0 and for CAN2 I define FIFO1 and based on the callback I transfer data between them, however, one thing bothers me that I am not completely familiar with.... Clock configuration and prescaller for 500kB/s. Can you help me still in this thread with the correct configuration of Prescaller, Time seg 1 and Time seg 2? I am very much asking...
void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* hfdcan)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(hfdcan->Instance==FDCAN1)
{
/* USER CODE BEGIN FDCAN1_MspInit 0 */
/* USER CODE END FDCAN1_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
PeriphClkInitStruct.FdcanClockSelection = RCC_FDCANCLKSOURCE_HSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_FDCAN_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**FDCAN1 GPIO Configuration
PB9 ------> FDCAN1_TX
PB8 ------> FDCAN1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* FDCAN1 interrupt Init */
HAL_NVIC_SetPriority(FDCAN1_IT0_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(FDCAN1_IT0_IRQn);
/* USER CODE BEGIN FDCAN1_MspInit 1 */
HAL_NVIC_SetPriority(FDCAN1_IT1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(FDCAN1_IT1_IRQn);
/* USER CODE END FDCAN1_MspInit 1 */
}
}