AnsweredAssumed Answered

CAN Transmit timeout error

Question asked by R_bisch.Dennis on Mar 27, 2017
Latest reply on May 3, 2018 by T J

Hi everyone, I´m using a STM32F103 with SW4STM32 and CubeMX and I`m struggeling with the CAN Communication. When I`ll try to transmit Data with HAL_CAN_Transmit then I get a timeout error. Like I read here in other threads it could be a bug from the HAL library but I dont`t know how to solve this problem. Or it could be a problem with my code. So I hope someone can help me.

Here is my code. If you need some other parts of it I`ll post it too.

Thanks for your support

 

int main(void)
{

 

  /* USER CODE BEGIN 1 */

 

  /* USER CODE END 1 */

 

  /* MCU Configuration----------------------------------------------------------*/

 

  /* 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_SPI1_Init();
  MX_USART1_UART_Init();
  MX_CAN_Init();

 

  /* USER CODE BEGIN 2 */
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);

 

  /* USER CODE END 2 */

 

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  HAL_GPIO_WritePin(GPIOD, USBEnable_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOD, canRS_Pin, GPIO_PIN_SET);
  HAL_UART_Receive_IT(&huart1,&Rx_data, 1);
  printf("welcome\r\n");

 

  uint16_t SOC = 5500;
  uint16_t BatVoltage = 590;
  uint16_t cellVoltageMax = 3900;
  uint16_t cellVoltageMin = 3800;
  uint8_t tempMax = 50;
  uint8_t tempMin = 47;

 

  while (1)
  {
     printf("Test\r\n");

 

     hcan.pTxMsg->Data[0] = 0b01110000;
     hcan.pTxMsg->Data[1] = (uint8_t) (SOC/1000);
     hcan.pTxMsg->Data[2] = (uint8_t) ((BatVoltage & 0b111111111111) >> 4);
     hcan.pTxMsg->Data[3] = (uint8_t) (((BatVoltage << 12) | ((cellVoltageMax & 0b0001111000000000) >> 1)) >> 8);
     hcan.pTxMsg->Data[4] = (uint8_t) (((cellVoltageMax & 0b0000000111110000) >> 1) | ((cellVoltageMin & 0b0001110000000000) >> 10));
     hcan.pTxMsg->Data[5] = (uint8_t) (((cellVoltageMin & 0b0000001111110000) >> 2) | (tempMax >> 6));
     hcan.pTxMsg->Data[6] = (uint8_t) ((tempMax << 2) | (tempMin >> 6));
     hcan.pTxMsg->Data[7] = (uint8_t) (tempMin << 2);

 

     printf("Data[0]: %d\r\n", hcan.pTxMsg->Data[0]);

 

     if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
            {
              /* Transmition Error */
              //Error_Handler();
         printf("CAN Transmit Error\r\n");
            }
     printf("%d\r\n", HAL_CAN_GetState(&hcan));
     HAL_Delay(1000);
  /* USER CODE END WHILE */

 

  /* USER CODE BEGIN 3 */

 

  }
  /* USER CODE END 3 */

 

}

 

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

 

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

 

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

 

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }

 

  HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_PLLCLK, RCC_MCODIV_1);

 

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

 

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

 

/* CAN init function */
static void MX_CAN_Init(void)
{
  CAN_FilterConfTypeDef  sFilterConfig;
  static CanTxMsgTypeDef        TxMessage;
  static CanRxMsgTypeDef        RxMessage;

 

  hcan.Instance = CAN1;
  hcan.pTxMsg = &TxMessage;
  hcan.pRxMsg = &RxMessage;

 

  hcan.Init.Prescaler = 4;
  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.SJW = CAN_SJW_2TQ;
  hcan.Init.BS1 = CAN_BS1_12TQ;
  hcan.Init.BS2 = CAN_BS2_5TQ;
  hcan.Init.TTCM = DISABLE;
  hcan.Init.ABOM = ENABLE;
  hcan.Init.AWUM = ENABLE;
  hcan.Init.NART = DISABLE;
  hcan.Init.RFLM = DISABLE;
  hcan.Init.TXFP = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    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(&hcan, &sFilterConfig) != HAL_OK)
   {
     /* Filter configuration Error */
     Error_Handler();
   }

 

   hcan.pTxMsg->StdId = 0x100;
   hcan.pTxMsg->ExtId = 0x0;
   hcan.pTxMsg->RTR = CAN_RTR_DATA;
   hcan.pTxMsg->IDE = CAN_ID_STD;
   hcan.pTxMsg->DLC = 8;

 

}

Outcomes