cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F469 CAN Rx Issues

jameslenvo
Associate II

Hello, I am attempting to transmit a simple CAN message between two STM32F469I discovery boards.

I am using the official STM32F4 github as reference found here:

https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM324x9I_EVAL/Examples/CAN/CAN_Networking

I can get Tx working on both boards, but the receiving of messages is not working. Am I handling the receive FIFO incorrectly? I've tried different filter settings and the loopback examples works fine.

Below is my code on both boards. Appreciate any help.

  HAL_Init();
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_CAN1_Init();
 
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOK, GPIO_PIN_3, GPIO_PIN_SET);
 
  CAN1_filterConfig();
  CAN1_start();
 
  // Activate Notifcation
  // CAN1_activateNotificaiton();
 
  // Set Transmit 
   myTxMessage.StdId = SendingId;
   myTxMessage.ExtId = ReceivingId;
   myTxMessage.RTR = CAN_RTR_DATA;
   myTxMessage.IDE = CAN_ID_STD;
   myTxMessage.DLC = 2;
   myTxMessage.TransmitGlobalTime = DISABLE;
   TxData[0] = 0xAD;
   TxData[1] = 0xAD;
 
 
 ThisId = SendingId;
  //  ThisId = ReceivingId;
 
  while (1) {
	  if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET){
      if(ThisId==SendingId){
// Lines for sending:
        if (HAL_CAN_GetTxMailbox0FreeLevel(&hcan1) != 0) {
        // Updated LED
        if(payload == 2) payload=0;
        LED_Switch(++payload);
        sendMessageStatus = HAL_CAN_AddTxMessage(&hcan1, &myTxMessage, TxData, mailbox);
          if(sendMessageStatus!=HAL_OK) {
            Error_Handler_Custom(sendMessageStatus);
          }
        } else {
        LED_Switch(3);
        }
      } else {
  /*##-5- Start the Reception process ########################################*/
          // Following lines are for receiving
        if(payload == 2) payload=0;
        LED_Switch(++payload);
        checkFillLevelStatus = HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0);
        if(checkFillLevelStatus == 0) {
          /* Reception Missing */
          Error_Handler_Custom(HAL_TIMEOUT);
        } else {
          getMessageStatus = HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &myRxMessage, RxData);
          if(getMessageStatus != HAL_OK) {
            /* Reception Error */
            Error_Handler_Custom(getMessageStatus);
          } else if((myRxMessage.StdId != SendingId)                    ||
                    (myRxMessage.RTR != CAN_RTR_DATA)               ||
                    (myRxMessage.IDE != CAN_ID_STD)                 ||
                    (myRxMessage.DLC != 2)                          ||
                    ((RxData[0]<<8 | RxData[1]) != 0xADAD)) {
                    /* Rx message Error */
                    Error_Handler_Custom(HAL_BUSY);
        } else {
                    LED_Switch(3);
                  }
        }
      }
		 HAL_Delay(200);
	  }
  }
}
 
static void MX_CAN1_Init(void)
{
 
  hcan1.Instance = CAN1;
  hcan1.Init.Prescaler = 2;
  hcan1.Init.Mode = CAN_MODE_NORMAL;
  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
  hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
  hcan1.Init.TimeTriggeredMode = DISABLE;
  hcan1.Init.AutoBusOff = DISABLE;
  hcan1.Init.AutoWakeUp = DISABLE;
  hcan1.Init.AutoRetransmission = DISABLE;
  hcan1.Init.ReceiveFifoLocked = DISABLE;
  hcan1.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan1) != HAL_OK)
  {
    Error_Handler();
  }
 
}
 
 
void CAN1_filterConfig(void)
{
	CAN_FilterTypeDef filterConfig;
	filterConfig.FilterBank = 0;
  filterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  filterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  filterConfig.FilterIdHigh = 0x1fff;
	filterConfig.FilterIdLow = 0xffff;
  filterConfig.FilterMaskIdHigh = 0x0000;
	filterConfig.FilterMaskIdLow = 0x0000;
  filterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
	filterConfig.FilterActivation = ENABLE;
  filterConfig.SlaveStartFilterBank = 14;
 
  can1filterConfigStatus = HAL_CAN_ConfigFilter(&hcan1, &filterConfig);
  if(can1filterConfigStatus != HAL_OK)
  {
    Error_Handler_Custom(can1filterConfigStatus);
  }
}
 
uint32_t HAL_CAN_GetTxMailbox0FreeLevel(CAN_HandleTypeDef *hcan)
{
  uint32_t freelevel = 0U;
  HAL_CAN_StateTypeDef state = hcan->State;
 
  if ((state == HAL_CAN_STATE_READY) ||
      (state == HAL_CAN_STATE_LISTENING))
  {
    /* Check Tx Mailbox 0 status */
    if ((hcan->Instance->TSR & CAN_TSR_TME0) != 0U)
    {
      freelevel++;
    }
  }
 
  /* Return Tx Mailboxes free level */
  return freelevel;
}

0 REPLIES 0