title: STM32F1 CANbus - receive not working
body: <p>I am dealing with a CAN RX problem. I am using STM32F103C8T6 and CubeMX code generator. Transmitting is OK without a problem but I am not able to receive any message, if I using polling MCU every time goes into HardFault and if I use interrupt mode it does nothing. My goal is to receive message and send some message like confirmation of receive. Here is the code:</p>
  
  <pre><code>   /**
    ******************************************************************************
    * File Name          : main.c
    * Description        : Main program body
    ******************************************************************************
    *
    * COPYRIGHT(c) 2017 STMicroelectronics
    *
    * Redistribution and use in source and binary forms, with or without modification,
    * are permitted provided that the following conditions are met:
    *   1. Redistributions of source code must retain the above copyright notice,
    *      this list of conditions and the following disclaimer.
    *   2. Redistributions in binary form must reproduce the above copyright notice,
    *      this list of conditions and the following disclaimer in the documentation
    *      and/or other materials provided with the distribution.
    *   3. Neither the name of STMicroelectronics nor the names of its contributors
    *      may be used to endorse or promote products derived from this software
    *      without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    *
    ******************************************************************************
    */
  
  #include "main.h"
  #include "stm32f1xx_hal.h"
  
  CAN_HandleTypeDef hcan;
  GPIO_InitTypeDef GPIO_InitStruct;
  
  void SystemClock_Config(void);
  void Error_Handler(void);
  static void MX_GPIO_Init(void);
  static void MX_CAN_Init(void);
  
  int main(void)
  {
    /* 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_CAN_Init();
  
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    CanTxMsgTypeDef msg;
  
    msg.StdId = 0x666;
    msg.IDE = CAN_ID_STD;
    msg.RTR = CAN_RTR_DATA;
    msg.DLC = 2;
    msg.Data[0] = 0xAA;
    msg.Data[1] = 0xAA;
    hcan.pTxMsg = &amp;msg;
  
    while (1)
    {
        HAL_CAN_Receive_IT(&amp;hcan,CAN_FILTER_FIFO0);
  
        msg.IDE = hcan.pRxMsg-&gt;IDE;
        msg.DLC = hcan.pRxMsg-&gt;DLC;
        msg.Data[0] = hcan.pRxMsg-&gt;Data[0];
        msg.Data[1] = hcan.pRxMsg-&gt;Data[1];
        msg.Data[2] = hcan.pRxMsg-&gt;Data[2];
        msg.Data[3] = hcan.pRxMsg-&gt;Data[3];
        msg.Data[4] = hcan.pRxMsg-&gt;Data[4];
        msg.Data[5] = hcan.pRxMsg-&gt;Data[5];
        msg.Data[6] = hcan.pRxMsg-&gt;Data[6];
        msg.Data[7] = hcan.pRxMsg-&gt;Data[7];
        msg.StdId = hcan.pRxMsg-&gt;StdId;
        msg.RTR = hcan.pRxMsg-&gt;RTR;
  
        HAL_CAN_Transmit(&amp;hcan, 10);
    }
  }
  
  /** System Clock Configuration
  */
  void SystemClock_Config(void)
  {
  
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
  
      /**Initializes the CPU, AHB and APB busses clocks 
      */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = 16;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    if (HAL_RCC_OscConfig(&amp;RCC_OscInitStruct) != HAL_OK)
    {
      Error_Handler();
    }
  
      /**Initializes the CPU, AHB and APB busses clocks 
      */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  
    if (HAL_RCC_ClockConfig(&amp;RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    {
      Error_Handler();
    }
  
      /**Configure the Systick interrupt time 
      */
    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  
      /**Configure the Systick 
      */
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  
    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  }
  
  /* CAN init function */
  static void MX_CAN_Init(void)
  {
  
      CAN_FilterConfTypeDef  sFilterConfig;
  
    hcan.Instance = CAN1;
    hcan.Init.Prescaler = 2;
    hcan.Init.Mode = CAN_MODE_NORMAL;
    hcan.Init.SJW = CAN_SJW_1TQ;
    hcan.Init.BS1 = CAN_BS1_5TQ;
    hcan.Init.BS2 = CAN_BS2_2TQ;
    hcan.Init.TTCM = DISABLE;
    hcan.Init.ABOM = DISABLE;
    hcan.Init.AWUM = DISABLE;
    hcan.Init.NART = DISABLE;
    hcan.Init.RFLM = DISABLE;
    hcan.Init.TXFP = DISABLE;
    if (HAL_CAN_Init(&amp;hcan) != HAL_OK)
    {
      Error_Handler();
    }
  
     sFilterConfig.FilterNumber = 0;
     sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
     sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
     sFilterConfig.FilterIdHigh = 0x0000;
     sFilterConfig.FilterIdLow = 0x0000;
     sFilterConfig.FilterMaskIdHigh = 0x0000;
     sFilterConfig.FilterMaskIdLow = 0x0000;
     sFilterConfig.FilterFIFOAssignment = 0;
     sFilterConfig.FilterActivation = ENABLE;
     sFilterConfig.BankNumber = 0;
  
     if(HAL_CAN_ConfigFilter(&amp;hcan, &amp;sFilterConfig) != HAL_OK)
     {
       /* Filter configuration Error */
       Error_Handler();
     }
  
     HAL_NVIC_SetPriority(CAN1_RX1_IRQn,0,0);
       HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
  
  }
  
  /** Configure pins as 
          * Analog 
          * Input 
          * Output
          * EVENT_OUT
          * EXTI
  */
  static void MX_GPIO_Init(void)
  {
  
    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_CAN1_CLK_ENABLE();
  
        /**CAN GPIO Configuration
        PA11     ------&gt; CAN_RX
        PA12     ------&gt; CAN_TX
        */
        GPIO_InitStruct.Pin = GPIO_PIN_11;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &amp;GPIO_InitStruct);
  
        GPIO_InitStruct.Pin = GPIO_PIN_12;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(GPIOA, &amp;GPIO_InitStruct);
  }
  
  /**
    * @brief  This function is executed in case of error occurrence.
    * @param  None
    * @retval None
    */
  void Error_Handler(void)
  {
    /* USER CODE BEGIN Error_Handler */
    /* User can add his own implementation to report the HAL error return state */
    while(1) 
    {
    }
    /* USER CODE END Error_Handler */ 
  }
  
  #ifdef USE_FULL_ASSERT
  
  /**
     * @brief Reports the name of the source file and the source line number
     * where the assert_param error has occurred.
     * @param file: pointer to the source file name
     * @param line: assert_param error line source number
     * @retval None
     */
  void assert_failed(uint8_t* file, uint32_t line)
  {
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
  
  }
  
  #endif
  
  /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Now i am able to use interrupts, but still i have problem - from interrupt handle there is the call to HAL_CAN_Receive_IT() and here it jumps to Hard fault from this line:</p>
  
  <pre><code>  if (hcan-&gt;pRxMsg-&gt;IDE == CAN_ID_STD)
  </code></pre>
  
  <p>EDIT2:</p>
  
  <p>Here is the HAL_CAN_Receive_IT() function:</p>
  
  <pre><code>static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber)
  {
    /* Get the Id */
    hcan-&gt;pRxMsg-&gt;IDE = (uint8_t)0x04 &amp; hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RIR;
    if (hcan-&gt;pRxMsg-&gt;IDE == CAN_ID_STD)
    {
      hcan-&gt;pRxMsg-&gt;StdId = (uint32_t)0x000007FF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RIR &gt;&gt; 21);
    }
    else
    {
      hcan-&gt;pRxMsg-&gt;ExtId = (uint32_t)0x1FFFFFFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RIR &gt;&gt; 3);
    }
  
    hcan-&gt;pRxMsg-&gt;RTR = (uint8_t)0x02 &amp; hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RIR;
    /* Get the DLC */
    hcan-&gt;pRxMsg-&gt;DLC = (uint8_t)0x0F &amp; hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDTR;
    /* Get the FMI */
    hcan-&gt;pRxMsg-&gt;FMI = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDTR &gt;&gt; 8);
    /* Get the data field */
    hcan-&gt;pRxMsg-&gt;Data[0] = (uint8_t)0xFF &amp; hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDLR;
    hcan-&gt;pRxMsg-&gt;Data[1] = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDLR &gt;&gt; 8);
    hcan-&gt;pRxMsg-&gt;Data[2] = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDLR &gt;&gt; 16);
    hcan-&gt;pRxMsg-&gt;Data[3] = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDLR &gt;&gt; 24);
    hcan-&gt;pRxMsg-&gt;Data[4] = (uint8_t)0xFF &amp; hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDHR;
    hcan-&gt;pRxMsg-&gt;Data[5] = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDHR &gt;&gt; 8);
    hcan-&gt;pRxMsg-&gt;Data[6] = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDHR &gt;&gt; 16);
    hcan-&gt;pRxMsg-&gt;Data[7] = (uint8_t)0xFF &amp; (hcan-&gt;Instance-&gt;sFIFOMailBox[FIFONumber].RDHR &gt;&gt; 24);
    /* Release the FIFO */
    /* Release FIFO0 */
    if (FIFONumber == CAN_FIFO0)
    {
      __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0);
  
      /* Disable FIFO 0 message pending Interrupt */
      __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FMP0);
    }
    /* Release FIFO1 */
    else /* FIFONumber == CAN_FIFO1 */
    {
      __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1);
  
      /* Disable FIFO 1 message pending Interrupt */
      __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FMP1);
    }
  
    if(hcan-&gt;State == HAL_CAN_STATE_BUSY_RX)
    {
      /* Disable interrupts: */
      /*  - Disable Error warning Interrupt */
      /*  - Disable Error passive Interrupt */
      /*  - Disable Bus-off Interrupt */
      /*  - Disable Last error code Interrupt */
      /*  - Disable Error Interrupt */
      __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
                                 CAN_IT_EPV |
                                 CAN_IT_BOF |
                                 CAN_IT_LEC |
                                 CAN_IT_ERR  );
    }
  
    if(hcan-&gt;State == HAL_CAN_STATE_BUSY_TX_RX) 
    {
      /* Disable CAN state */
      hcan-&gt;State = HAL_CAN_STATE_BUSY_TX;
    }
    else
    {
      /* Change CAN state */
      hcan-&gt;State = HAL_CAN_STATE_READY;
    }
  
    /* Receive complete callback */ 
    HAL_CAN_RxCpltCallback(hcan);
  
    /* Return function status */
    return HAL_OK;
  }
  </code></pre>