cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive a message via CAN with STM32F105

Sandro Sartoni
Associate II
Posted on June 21, 2017 at 16:00

Hi,

This is my first time programming a microcontroller, so a lot of things are still new to me and I'd like to have some infos about how to correctly receive a message via CAN.

I've read the HAL Driver documentation of my uC and what I've understood so far is that if I want to receive data in the interrupt mode, I have to use HAL_CAN_Receive_IT() function and HAL_CAN_IRQHandler() under the CAN Interrupt subroutine: everything is clear in theory, but in practice I don't know what to do.

I've initialized the CAN so that only messages from IDs 0x106 and 0x10A can be received:

static void MX_CAN1_Init(void)

{

  CAN_FilterConfTypeDef sFilterConfig;

  static CanRxMsgTypeDef RxMsg;

  hcan1.pRxMsg=&RxMsg;

  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 11;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SJW = CAN_SJW_1TQ;

  hcan1.Init.BS1 = CAN_BS1_1TQ;

  hcan1.Init.BS2 = CAN_BS2_1TQ;

  hcan1.Init.TTCM = DISABLE;

  hcan1.Init.ABOM = DISABLE;

  hcan1.Init.AWUM = DISABLE;

  hcan1.Init.NART = DISABLE;

  hcan1.Init.RFLM = DISABLE;

  hcan1.Init.TXFP = DISABLE;

  sFilterConfig.FilterNumber=0;

  sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;

  sFilterConfig.FilterScale=CAN_FILTERSCALE_16BIT;

  sFilterConfig.FilterIdHigh=0x010A;

  sFilterConfig.FilterIdLow=0x0106;

  sFilterConfig.FilterMaskIdHigh=0x0000;

  sFilterConfig.FilterMaskIdLow=0x0000;

  sFilterConfig.FilterFIFOAssignment=0;

  sFilterConfig.FilterActivation=ENABLE;

  sFilterConfig.BankNumber=0;

  HAL_CAN_ConfigFilter(&hcan1,&sFilterConfig);

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    Error_Handler();

  }

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

  {

    Error_Handler();

  }

}

But I don't know to proceed now. My while(1) section logic is:

while(1) {

   if there's a new msg then

      save it and use it to do some elaborations

   else

      do something else

}

Thanks for your help.

#stm32f105r8tx
0 REPLIES 0