AnsweredAssumed Answered

STM32F4 problem with CAN bus

Question asked by ragazzi.valerio on Nov 19, 2015
Latest reply on Nov 19, 2015 by Clive One
Hi, I useanSTM32F407,I'm tryingcommunicationwith CAN bus,for now onlytransmission, using Hal library.

1) I have setted PA11 (CAN1_RX) and PA12 (CAN1_TX):

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
  GPIO_InitTypeDef   GPIO_InitStruct;

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

  /* Configure peripheral GPIO */
  /* CAN1 TX GPIO pin configuration (PA12)*/
  GPIO_InitStruct.Pin = CANx_TX_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  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 (PA11)*/
  GPIO_InitStruct.Pin = CANx_RX_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Alternate =  CANx_RX_AF;

  HAL_GPIO_Init(CANx_RX_GPIO_PORT, &GPIO_InitStruct);

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

It's correct?

2) I have a clock of 42MHz (APB1), I want a sample point to 75% and baudrate of 250 Kbit/s, my settings are:

    CanHandle.Init.TTCM = DISABLE;
    CanHandle.Init.ABOM = DISABLE;
    CanHandle.Init.AWUM = DISABLE;
    CanHandle.Init.NART = DISABLE;
    CanHandle.Init.RFLM = DISABLE;
    CanHandle.Init.TXFP = DISABLE;
    CanHandle.Init.Mode = CAN_MODE_NORMAL;
    CanHandle.Init.SJW = CAN_SJW_1TQ;
    CanHandle.Init.BS1 = CAN_BS1_8TQ;
    CanHandle.Init.BS2 = CAN_BS2_3TQ;
    CanHandle.Init.Prescaler = 11;

It's correct?

3) I have a problem with the function "HAL_CAN_Transmit()", when i call it in the main.c it returns timeout, waiting for TransmitMailbox to be sent  (Following codes are from : stm32f4xx_hal_can.

/* Check End of transmission flag */
   while(!(__HAL_CAN_TRANSMIT_STATUS(hcan, transmitmailbox)))
   {
     /* Check for the Timeout */
     if(Timeout != HAL_MAX_DELAY)
     {
      if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
      {
        hcan->State = HAL_CAN_STATE_TIMEOUT;
        /* Process unlocked */
        __HAL_UNLOCK(hcan);
        return HAL_TIMEOUT;
       }
     }
   }

Why?
Also the function "HAL_CAN_Transmit()" does not send anything.

Thank you in advance



Outcomes