AnsweredAssumed Answered

STM32 EVAL board CAN2 receive interrupt not working, but CAN2 transmit working

Question asked by Preman.Praseen on Oct 31, 2016
Latest reply on Nov 2, 2016 by ST Renegade
Hardware: STM3240G-EVAL (STM32 20-21-45-46 G-EVAL) using STM32F407IGH6UHPAHP microcontroller

IDE: Eclipse IDE for C/C++ Luna Service Release 2 (4.4.2) Build: 20150219-0600

Toolchain: Ac6 STM32 MCU GCC



Using the above hardware and software I am unable to get CAN2 receive interrupt on the STM32 evaluation module mentioned above. However I am able 
to transmit without interrupt using CAN2. The CAN1 peripheral is working as expected and I am able to successfully transmit and get receive interrupts. 

Below is code I have written for configuring CAN2 for receive interrupt. The CAN2 transmit is working without interrupt using this configuration.

stm32f4xx_it.h - In this file added the lines

void CAN2_RX0_IRQHandler(void);
void CAN2_RX1_IRQHandler(void);

Can.cpp - In this file added the lines

extern "C" void CAN2_RX0_IRQHandler(void)
{
   canRxIsr(&canHandle);
}

__inline void canRxIsr(CAN_HandleTypeDef* canrxhandle)
{
   HAL_CAN_IRQHandler(canrxhandle);
   HAL_CAN_Receive_IT(canrxhandle, CAN_FIFO0);

   //changed for integration
   // Transmit below done to validate the receive that has happened
   canHandle.pTxMsg->StdId = 821;
   canHandle.pTxMsg->DLC = 8;
   canHandle.pTxMsg->Data[0] = 0xAA;
   canHandle.pTxMsg->Data[1] = 0xBB;
   canHandle.pTxMsg->Data[2] = 0xCC;
   canHandle.pTxMsg->Data[3] = 0xDD;


   canHandle.pTxMsg->Data[4] = 0xEE;
   canHandle.pTxMsg->Data[5] = 0xFF;
   canHandle.pTxMsg->Data[6] = 0xAA;
   canHandle.pTxMsg->Data[7] = 0xBB;


   HAL_CAN_Transmit(&canHandle, 10);
   //changed for integration
}

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
   //changed for CAN2 configuration
   GPIO_InitTypeDef   GPIO_InitStruct;

  /*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* CAN2 Periph clock enable */
  CANx1_CLK_ENABLE(); //Both clocks are configured as per data sheet for CAN2 to work
  CANx_CLK_ENABLE();

  /* Enable GPIO clock ****************************************/
  CANx_GPIO_CLK_ENABLE();

  /*##-2- Configure peripheral GPIO ##########################################*/
  /* CAN2 TX GPIO pin configuration */
  GPIO_InitStruct.Pin = CANx_TX_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Alternate =  CANx_TX_AF;

  HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct);

  /* CAN2 RX GPIO pin configuration */
  GPIO_InitStruct.Pin = CANx_RX_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Alternate =  CANx_RX_AF;

  HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct);

  /*##-3- Configure the NVIC #################################################*/
  /* NVIC configuration for CAN2 Reception complete interrupt */
  HAL_NVIC_SetPriority(CANx_RX_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(CANx_RX_IRQn);
}

/**
  * @brief CAN MSP De-Initialization
  *        This function frees the hardware resources used in this example:
  *          - Disable the Peripheral's clock
  *          - Revert GPIO to their default state
  * @param hcan: CAN handle pointer
  * @retval None
  */
void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
{
  /*##-1- Reset peripherals ##################################################*/
  CANx_FORCE_RESET();
  CANx_RELEASE_RESET();

  /*##-2- Disable peripherals and GPIO Clocks ################################*/
  /* De-initialize the CAN2 TX GPIO pin */
  HAL_GPIO_DeInit(CANx_TX_GPIO_PORT, CANx_TX_PIN);
  /* De-initialize the CAN2 RX GPIO pin */
  HAL_GPIO_DeInit(CANx_RX_GPIO_PORT, CANx_RX_PIN);

  /*##-4- Disable the NVIC for CAN reception #################################*/
  HAL_NVIC_DisableIRQ(CANx_RX_IRQn);
}

Since the EVK is being used the PORTB PIN 13 is used for CAN2 Tx and PORTB PIN 8 is used for CAN2 Rx

The values of the config macros used above is as below

 #define CANx                            CAN2
 #define CANx_CLK_ENABLE()               __HAL_RCC_CAN2_CLK_ENABLE()
 #define CANx1_CLK_ENABLE()              __HAL_RCC_CAN1_CLK_ENABLE()
 #define CANx_GPIO_CLK_ENABLE()          __HAL_RCC_GPIOB_CLK_ENABLE()

 #define CANx_FORCE_RESET()              __HAL_RCC_CAN2_FORCE_RESET()
 #define CANx_RELEASE_RESET()            __HAL_RCC_CAN2_RELEASE_RESET()

 /* Definition for CANx Pins */
 #define CANx_TX_PIN                    GPIO_PIN_13
 #define CANx_TX_GPIO_PORT              GPIOB
 #define CANx_TX_AF                     GPIO_AF9_CAN2
 #define CANx_RX_PIN                    GPIO_PIN_5
 #define CANx_RX_GPIO_PORT              GPIOB
 #define CANx_RX_AF                     GPIO_AF9_CAN2

 /* Definition for CANx's NVIC */
 #define CANx_RX_IRQn                   CAN2_RX0_IRQn
 #define CANx_RX_IRQHandler             CAN2_RX0_IRQHandler
 
 I have also made the jumper changes to the JP3, JP10, JP7 and JP9 on the evaluation module for enabling CAN2 in high speed.
 Please have a look and let me know what I missed in the configuration, to get CAN2 receive interrupt working.

Outcomes