cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F767NI CAN RECEIVER PROBLEM

Suat Berkant Gulen
Associate II
Posted on October 09, 2017 at 13:40

I generate Can code from Cube MX. But i can't receive any data.

 i just want to receive data on CAN Receiver of STM32F767NI.

I try to communicate via PCAN View. When i send data from PCAN-View the program said 'Bus-Off'. And receive interrupt never triggered.

APB1 periph clocks : 54Mhz

My configurations is here:

# Main Function

int main(void)

{

   HAL_Init();

   SystemClock_Config();

   MX_GPIO_Init();

   MX_ADC2_Init();

   MX_ADC3_Init();

   MX_CAN1_Init();

   MX_CRC_Init();

   MX_I2C2_Init();

   MX_SDMMC1_SD_Init();

   MX_SPI1_Init();

   MX_TIM5_Init();

   MX_TIM12_Init();

   MX_UART4_Init();

   MX_USART1_UART_Init();

   MX_USART2_UART_Init();

   MX_USART3_UART_Init();

   MX_USART6_UART_Init();

   MX_FATFS_Init();

   MX_LWIP_Init();

   MX_USB_DEVICE_Init();

   if (HAL_CAN_Receive_IT(&hcan1, CAN_FIFO0) != HAL_OK)

   {

      /* Reception Error */

      Error_Handler();

   }

  while (1);

}

# CAN Init Function

static void MX_CAN1_Init(void)

{

   static CanTxMsgTypeDef TxMessage;

   static CanRxMsgTypeDef RxMessage;

   hcan1.Instance = CAN1;

   hcan1.pTxMsg = &TxMessage;

   hcan1.pRxMsg = &RxMessage;

   hcan1.Init.TTCM = DISABLE;

   hcan1.Init.ABOM = ENABLE;

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

   }

}

# MSP Init Functions

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)

{

   GPIO_InitTypeDef GPIO_InitStruct;

   f(hcan->Instance==CAN1)

   {

      __HAL_RCC_CAN1_CLK_ENABLE();

   GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_11;

   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

   GPIO_InitStruct.Pull = GPIO_NOPULL;

   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

   GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;

   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

   HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);

   HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

   HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 0, 0);

   HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);

   }

}

void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)

{

if(hcan->Instance==CAN1)

{

   __HAL_RCC_CAN1_CLK_DISABLE();

   HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12|GPIO_PIN_11);

   

   HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);

   HAL_NVIC_DisableIRQ(CAN1_RX1_IRQn);   

   }

}

4 REPLIES 4
Tiago Malheiro
Associate II
Posted on October 09, 2017 at 14:28

I don't know about the PCAN View, and whether it shows that a node acknowledged a message.

You might need to recheck the CAN timing parameters for your application.

But, I see 2 things in your code:

1) you must configure the filters to enable reception. Otherwise no message will go into the FIFOs. (below config will catch any message and pipe it into FIFO0)

 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 = 14;
 if (HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)
 {
 /* Filter configuration Error */
 Error_Handler();
 }�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

2) Since you are doing it interrupt based, you need to define the callback for CAN messages.

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *CanHandle)
{
}�?�?�?

Posted on October 09, 2017 at 15:45

Thank you for reply,

i add filter and Callback function. The code with last changes:

static void MX_CAN1_Init(void)

{

CAN_FilterConfTypeDef sFilterConfig;

static CanTxMsgTypeDef TxMessage;

static CanRxMsgTypeDef RxMessage;

hcan1.Instance = CAN1;

hcan1.pTxMsg = &TxMessage;

hcan1.pRxMsg = &RxMessage;

hcan1.Init.TTCM = DISABLE;

hcan1.Init.ABOM = ENABLE;

hcan1.Init.AWUM = ENABLE;

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

}

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 = 14;

if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)

{

/* Filter configuration Error */

Error_Handler();

}

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 callback function:

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *hcan1)

{

   

if ((hcan1->pRxMsg->StdId == 0x321) && (hcan1->pRxMsg->IDE == CAN_ID_STD) && (hcan1->pRxMsg->DLC == 2))

   {

   }

}

Posted on October 09, 2017 at 15:48

Hello Suat Berkant Gulen,

Let me also provide you some feedback.

First of all thanks Tiago for you answer. Definitely I agree that CAN filtering has to be configured and enabled. By default it is not done and I confirm that in such conditions it is not possible for STM32 to receive any CAN messages. By filling in all filter parameters with zeros and by callingHAL_CAN_ConfigFilter function you will enable CAN filtering in a way, that all CAN messages will be accepted.

Some time ago I created a presentation, which shows how to develop very simple CAN application for STM32F7 step by step. Please find attached both presentation and application. I hope it will be helpful to you.

Best regards

Szymon

________________

Attachments :

STM32 F7 CAN.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyOD&d=%2Fa%2F0X0000000b6w%2F4gZLcGNtakiDMEcocCkZOXSAeS8TWG56mRn.1xng7Uo&asPdf=false

CAN_example.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyO3&d=%2Fa%2F0X0000000b6u%2FSwLz8hWcxLp7bITHwWFRqqkXPFrRbObEM0EB2pcxV2w&asPdf=false
Posted on October 09, 2017 at 17:40

Thank you again and again.. It works. And thanks for help Tiago..