0
votes

I'm trying to use the CAN of STM32F3, but I'm having some troubles. I read some tutorials and followed the instructions, but I think I'm making some mistakes. The code bellow is running as a freeRTOS task, and should work as polling to CAN Rx.

#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "stdio.h"
#include "stm32f3xx_hal_can.h"
#include "can.h"
#include "usart.h"

void canRx(void const *argument)
{
    /* USER CODE BEGIN canRx */
    /* Infinite loop */

    uint8_t receivedMessage[8];
    CAN_RxHeaderTypeDef RxHeader;

    HAL_CAN_Start(&hcan);

    for (;;)
    {
        if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
        {
            HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, receivedMessage);
        }
        HAL_UART_Transmit(&huart2, receivedMessage, 8, portMAX_DELAY);

        osDelay(200);
    }
    /* USER CODE END canRx */
}

I just want to read the Rx data sent from a arduino which works as an other CAN node. The arduino loop has a counter which is incremented of 1 each 200ms and is sent to STM via CAN.

I want receive in the UART2 the ID and Message from CAN receiver(the STM32). I'll compare these data with the data from the sender, I get this data from an Arduino Serial, and check the hit rate(I hope 100%).

How to make it work well?

1
Have you initialized the CAN structure (hcan) with HAL_CAN_Init ?Benoît
Yes, the CAN Init was generated by the CubeMX.Elias Elnatã
Have you correctly wired your CAN bus with 120 Ohms termination resistors? If you did, can you monitor the CAN bus with an oscillator or anything else to see if the arduino is actually transmitting something?Benoît
Yes, the CAN nodes work well. I'm using 2 MCP 2551 with the resistor at H and L. This assembly work in others CAN applications, I'm trying discovery how work with CAN with STM32.Elias Elnatã
Could you give us more details on your program output? For example, can you tell what you can observe on the UART bus? I invite you to edit your post to include this information.Benoît

1 Answers

0
votes

Make sure to clear the flag before each transmit so that you don't send unnecessary information.