AnsweredAssumed Answered

Receive CAN msg in interrupt mode with STM32F105 mcu

Question asked by Sandro Sartoni on Jul 7, 2017
Latest reply on Jul 8, 2017 by Sandro Sartoni

Hi everyone,

I'm trying to receive a msg via CAN-bus in interrupt mode with STM32F105 mcu. I set the CAN filter so that only 0x0106 and 0x010A IDs are allowed. This is the first time I use this microcontroller so I'm not completely sure of the code. Can you help me?

 

/* Main 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();
     /* Configure the system clock */
     SystemClock_Config();
     /* Initialize all configured peripherals */
     MX_GPIO_Init();
     MX_ADC1_Init();
     MX_CAN1_Init();
     MX_IWDG_Init();
     MX_RTC_Init();
     MX_SPI1_Init();
     MX_TIM1_Init();
     MX_TIM2_Init();
     MX_UART4_Init();
     MX_UART5_Init();
     /* USER CODE BEGIN 2 */
     /* Initialize CAN Filter */
     CAN_FilterConfTypeDef sFilterConfig;
     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_ConfigFilter(&hcan1,&sFilterConfig) != HAL_OK)
     {
       Error_Handler();
     }
     HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
     HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
     /* USER CODE END 2 */
     /* Infinite loop */
     /* USER CODE BEGIN WHILE */
     while (1)
     {
     /* USER CODE END WHILE */
     /* USER CODE BEGIN 3 */
      /* Counts time, if no msg arrives after a certain time, assume something
         is broken and set arbitrarily to 75% the duty cycle */
      HAL_CAN_Receive_IT(&hcan1,CAN_FIFO0);
      //other code..
      HAL_Delay(delay);
     }
     /* USER CODE END 3 */
   }

 

/* CAN1 init function */
static void MX_CAN1_Init(void)
{
  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;
  if (HAL_CAN_Init(&hcan1) != HAL_OK)
  {
    Error_Handler();
  }
}
And in the interrupt section
void CAN1_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN CAN1_RX0_IRQn 0 */
  /* USER CODE END CAN1_RX0_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan1);
  /* USER CODE BEGIN CAN1_RX0_IRQn 1 */
  //my own code
  HAL_Delay(delay);
  /* USER CODE END CAN1_RX0_IRQn 1 */

Outcomes