cancel
Showing results for 
Search instead for 
Did you mean: 

STMF303 CAN Normal Mode

akshayamk
Associate II
Posted on December 22, 2015 at 10:47

Hello!

I have already coded for CAN Loopback mode and it works.

However, when I changed the mode to CAN Normal Mode, I am unable to hit Receive Handler, but I can hit transmit Handler.

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_CAN_Init();

  CanTxMsgTypeDef TxMsg = {

  .StdId = 0x321,

.ExtId = 0x01,  

    .IDE = CAN_ID_STD,

    .RTR = CAN_RTR_DATA,

    .DLC = 1,

  };

  CanRxMsgTypeDef RxMsg;

  hcan.pTxMsg = &TxMsg;

  hcan.pRxMsg = &RxMsg;

 

   CAN_FilterConfTypeDef CAN_FilterConfig = {

    .BankNumber = 0,

    .FilterActivation = ENABLE,

    .FilterScale = CAN_FILTERSCALE_16BIT,

    .FilterMode = CAN_FILTERMODE_IDMASK,

    .FilterNumber = 0,

    .FilterFIFOAssignment = 0,

    .FilterMaskIdLow = (0x7FF << 5),

    .FilterMaskIdHigh = (0x400 << 5),

    .FilterIdLow = (CAN_ID << 5),

    .FilterIdHigh = (2047 << 5),

  };

    HAL_CAN_ConfigFilter(&hcan, &CAN_FilterConfig);

  HAL_CAN_Receive_IT(&hcan, CAN_FIFO0);

 

 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); //CAN initialisation over

 

  hcan.pTxMsg->Data[0]++;

HAL_CAN_Transmit_IT(&hcan); //transmit interrupt hits after this

 HAL_Delay(10);

 

  uint32_t i = 0;

  while((i < 0xFFF))

  {

    i++;

  }

  while (1)

  {

 

  }

}

void USB_LP_CAN_RX0_IRQHandler(void)

{

 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);

  HAL_CAN_IRQHandler(&hcan);

}

void USB_HP_CAN_TX_IRQHandler(void)

{

 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);

  HAL_CAN_IRQHandler(&hcan);

}

void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan)

{

   uint32_t timeout = 500;

  HAL_StatusTypeDef status;

  status = HAL_CAN_Receive_IT(hcan, CAN_FIFO0);

  if(HAL_OK != status){

    while(1);

  }

}

I have also put the following in HAL_CAN_MspInit function:

    HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn);

  HAL_NVIC_SetPriority(USB_HP_CAN_TX_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(USB_HP_CAN_TX_IRQn);

Is it a sequencing issue? I am not sure why receive handler is not getting it (i.e. USB_LP_CAN_RX0_IRQHandler cannot be hit). Please help! Any help is appreciated! Thank you!

#can #stmf303 #can-normal-mode
1 REPLY 1
jpeacock
Associate III
Posted on December 22, 2015 at 19:03

Check the CAN error status register and if no errors then your RX filters to determine why you cannot receive.  Are both nodes showing the same problem (you need a minimum of two CAN nodes to send and receive)?

  Jack Peacock