Skip to main content
Visitor II
July 5, 2024
Question

Unable to receive data using CAN

  • July 5, 2024
  • 4 replies
  • 2559 views

Hello, 

I am using NUCLEO F767 microcontroller. i want to receive data using CAN. I am using CAN2. pins PB12 Rx and PB13 TX. I have tried bunch of things but i am unable to receive data. So, please help me regarding this 

Code : 

int main(void)

{



/* USER CODE BEGIN 1 */



/* USER CODE END 1 */



/* MCU Configuration--------------------------------------------------------*/



/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();



/* USER CODE BEGIN Init */



/* USER CODE END Init */



/* Configure the system clock */

SystemClock_Config();



/* USER CODE BEGIN SysInit */



/* USER CODE END SysInit */



/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_CAN2_Init();

/* USER CODE BEGIN 2 */



if (HAL_CAN_Start(&hcan2) != HAL_OK)

{

Error_Handler();

}



sFilterConfig.FilterActivation = CAN_FILTER_ENABLE;///CAN_FILTER_

sFilterConfig.FilterBank = 0; // which filter bank to use from the assigned ones

sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO1;

sFilterConfig.FilterIdHigh = 0x114<<5;//<<5;

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdHigh = 0x114<<5;//0x1<<13;

sFilterConfig.FilterMaskIdLow = 0x0000;

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

sFilterConfig.SlaveStartFilterBank = 27;



if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig) != HAL_OK)

{

Error_Handler();

}



if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK) //CAN_IT_RX_FIFO0_MSG_PENDING

{

Error_Handler();

}



/* USER CODE END 2 */



/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */



/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}



/* USER CODE BEGIN 4 */

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

{

HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO1, &RxHeader, RxData);

}
    This topic has been closed for replies.

    4 replies

    Technical Moderator
    July 5, 2024

    Hello @Mukesh_Patil and welcome to the community.

    Please review our recommendations in this link on thread posting in this community especially how to share your code and on which forum you post your question (this is not CubeIDE subject but it's related to the MCU usage itself). I'm editing it in order to follow these recommendations and moving the post to the correct forum.

     

     

    Technical Moderator
    July 5, 2024

    You have an issue with CAN filter configuration especially with this parameter:

     

    sFilterConfig.SlaveStartFilterBank = 27

     

    if you are not using CAN1 you can set it to 0:

     

    sFilterConfig.SlaveStartFilterBank = 0

     

    See this thread (you are in case 2):

    https://community.st.com/t5/stm32-mcus-products/how-to-configure-can-filters-to-use-can1-and-can2-parallelly/td-p/56553

    Hope it does answer your question.

    Visitor II
    July 5, 2024

    Thank you for your assistance.

    I have tried sFilterConfig.SlaveStartFilterBank = 0. But I still haven't gotten any result. and i am only using one CAN that is CAN2

     

    Technical Moderator
    July 5, 2024

    You have also another issue. The usage FIFO is wrong:

    Here you assigned the filter to FIFO1 and you activated the interrupt on FIFO1.

     

    sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO1
    if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK)

     

    But here you are using FIFO0 callback and in the callback you are receiving data from FIFO1 which doeasn't make sense:

     

    void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
    
    {
    
    HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO1, &RxHeader, RxData);
    
    }

     

    Here you are using the incorrect callback. So, replace HAL_CAN_RxFifo0MsgPendingCallback by HAL_CAN_RxFifo1MsgPendingCallback

    I suggest you also to start with a filter passing all IDs:

     

    sFilterConfig.FilterIdHigh = 0x0000;
    sFilterConfig.FilterIdLow = 0x0000;
    sFilterConfig.FilterMaskIdHigh = 0x0000;
    sFilterConfig.FilterMaskIdLow = 0x0000;
    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

     

     

    Visitor II
    July 6, 2024

    I am sharing Main.c file

     

    Visitor II
    July 6, 2024

    Here are some pictures of IOC file configuration for CAN:

    Screenshot (28).pngScreenshot (29).pngScreenshot (30).pngScreenshot (31).png

    Technical Moderator
    July 8, 2024

    Hello,

    Please attach your project including your ioc file.