cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042G6Ux CAN issue

Tonave
Associate II

Hello All !

 

I have an issue with the CAN bus on STM32F042G6. I am using PA11 and PA12 Pins. Here is the configuration part with a 48MHz internal clock :

 

 

static void MX_CAN_Init(void)
{

  /* USER CODE BEGIN CAN_Init 0 */

  /* USER CODE END CAN_Init 0 */

  /* USER CODE BEGIN CAN_Init 1 */

  /* USER CODE END CAN_Init 1 */
  hcan.Instance = CAN;
  hcan.Init.Prescaler = 23;
  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_5TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_6TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = DISABLE;
  hcan.Init.AutoWakeUp = DISABLE;
  hcan.Init.AutoRetransmission = DISABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN_Init 2 */

  /* USER CODE END CAN_Init 2 */

}

 

 

Here is the remap done by STCube :

 

 

void HAL_MspInit(void)
{
  /* USER CODE BEGIN MspInit 0 */

  /* USER CODE END MspInit 0 */

  __HAL_RCC_SYSCFG_CLK_ENABLE();
  __HAL_RCC_PWR_CLK_ENABLE();

  /* System interrupt init*/

  __HAL_REMAP_PIN_ENABLE(HAL_REMAP_PA11_PA12);

  /* USER CODE BEGIN MspInit 1 */

  /* USER CODE END MspInit 1 */
}

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hcan->Instance==CAN)
  {
  /* USER CODE BEGIN CAN_MspInit 0 */

  /* USER CODE END CAN_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_CAN1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**CAN GPIO Configuration
    PA11     ------> CAN_RX
    PA12     ------> CAN_TX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN CAN_MspInit 1 */

  /* USER CODE END CAN_MspInit 1 */
  }
}

 

 

I can write with the CAN bus it works (verified with oscillo), here is the code for the writing part :

 

 

int main(void)
{
  /* 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();
  MX_CRC_Init();
  /* USER CODE BEGIN 2 */
  if(HAL_CAN_Start(&hcan) != HAL_OK)
    Error_Handler();
  
  TxHeader.StdId = 0x446; 
  TxHeader.ExtId = 0x01; 
  TxHeader.RTR = CAN_RTR_DATA; 
  TxHeader.IDE = CAN_ID_STD; 
  TxHeader.DLC = 2;
  
  CAN_FilterTypeDef canfilterconfig;

  canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
  canfilterconfig.FilterBank = 18;  // which filter bank to use from the assigned ones
  canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
  canfilterconfig.FilterIdHigh = 0x446<<5;
  canfilterconfig.FilterIdLow = 0;
  canfilterconfig.FilterMaskIdHigh = 0x446<<5;
  canfilterconfig.FilterMaskIdLow = 0x0000;
  canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
  canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
  canfilterconfig.SlaveStartFilterBank = 20;  

  HAL_CAN_ConfigFilter(&hcan, &canfilterconfig);

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    HAL_Delay(500);
    HAL_CAN_AddTxMessage(&hcan, &TxHeader, txData, &mail_box);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

 

This part is ok !

So now I tried to read the frame sent with this code :

 

 

int main(void)
{
  /* 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();
  MX_CRC_Init();
  /* USER CODE BEGIN 2 */
  if(HAL_CAN_Start(&hcan) != HAL_OK)
    Error_Handler();
  
  TxHeader.StdId = 0x446; 
  TxHeader.ExtId = 0x01; 
  TxHeader.RTR = CAN_RTR_DATA; 
  TxHeader.IDE = CAN_ID_STD; 
  TxHeader.DLC = 2;
  
  CAN_FilterTypeDef canfilterconfig;

  canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;
  canfilterconfig.FilterBank = 18;  // which filter bank to use from the assigned ones
  canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
  canfilterconfig.FilterIdHigh = 0x446<<5;
  canfilterconfig.FilterIdLow = 0;
  canfilterconfig.FilterMaskIdHigh = 0x446<<5;
  canfilterconfig.FilterMaskIdLow = 0x0000;
  canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;
  canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;
  canfilterconfig.SlaveStartFilterBank = 20;  // how many filters to assign to the CAN1 (master can)

 HAL_CAN_ConfigFilter(&hcan, &canfilterconfig);
 //activate Int
 if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING ) != HAL_OK)
 {
   Error_Handler();
 }
 while(1)
 {
 }
}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
 
{
  HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, Rx); //Receive CAN bus message to canRX buffer 
}

 

 

 I don't understand why but it is impossible for me to go in the HAL_CAN_RxFifo0MsgPendingCallback function when a message is received. I checkes the RX Pin with an oscilloscope and the CAN frame is correct on this Pin...

 

I hope it is clear enough... Do you have any advise to help me ?

I looked everywhere in the datasheet and on forums but impossible to make it works...??

Thank you in advance :).

Tonave

 

11 REPLIES 11
Tonave
Associate II

It is working with both configuration.

Many thanks for your help !!!!!

Tonave

So to conclude there were two issues: filter config + GPIO config (pull-up not configured for Rx pin in loopback mode).

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.