AnsweredAssumed Answered

How can fix CAN Receive problem in Normal Mode

Question asked by Suat Berkant Gulen on Oct 2, 2017
Latest reply on Oct 6, 2017 by Clive One

Hi,

I try to communicate via CAN. I can transmit data to PCAN but i can't receive in Normal Mode. When i use Loopback, i can receive data. Please Help me.. My configuration is here

/* CAN1 init function */
static void MX_CAN1_Init(void)
{

CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage;
static CanRxMsgTypeDef RxMessage;

/*##-1- Configure the CAN peripheral #######################################*/
hcan1.Instance = CANx;
hcan1.pTxMsg = &TxMessage;
hcan1.pRxMsg = &RxMessage;

hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = DISABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = DISABLE;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = CAN_SJW_1TQ;
hcan1.Init.BS1 = CAN_BS1_6TQ;
hcan1.Init.BS2 = CAN_BS2_7TQ;
hcan1.Init.Prescaler = 4;

if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}

/*##-2- Configure the CAN Filter ###########################################*/
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 = CAN_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 0;

if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}

/*##-3- Configure Transmission process #####################################*/
hcan1.pTxMsg->StdId = 0x321;
hcan1.pTxMsg->ExtId = 0x01;
hcan1.pTxMsg->RTR = CAN_RTR_DATA;
hcan1.pTxMsg->IDE = CAN_ID_STD;
hcan1.pTxMsg->DLC = 2;

}

And i use these functions to enable them:

if (HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0) != HAL_OK)
{
/* Reception Error */
Error_Handler();
}

//////////////////////////////////////////////////////////////////////////////////////////////////

if (HAL_CAN_Transmit(&hcan1, 10) != HAL_OK)
{
/* Transmission Error */
Error_Handler();
}

My MspDeInit functions :

 

void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
{
   GPIO_InitTypeDef GPIO_InitStruct;

   *##-1- Enable peripherals and GPIO Clocks #################################*/
   /* CAN1 Periph clock enable */
   CANx_CLK_ENABLE();
   /   * Enable GPIO clock ****************************************/
   CANx_GPIO_CLK_ENABLE();

 

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

   HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct);

   /* CAN1 RX GPIO pin configuration */


   GPIO_InitStruct.Pin = CANx_RX_PIN;
   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   GPIO_InitStruct.Alternate = CANx_RX_AF;

   HAL_GPIO_Init(CANx_RX_GPIO_PORT, &GPIO_InitStruct);

 

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


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 CAN1 TX GPIO pin */
   HAL_GPIO_DeInit(CANx_TX_GPIO_PORT, CANx_TX_PIN);
   /* De-initialize the CAN1 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);
}

Outcomes