AnsweredAssumed Answered

the HAL has BUG of CAN drive on STM32F1?

Question asked by Wei RP on Jun 14, 2018
Latest reply on Jun 15, 2018 by Wei RP

Stm32F103 HAL_CAN_Transmit(&hcan,10) not working, always return HAL_TIMEOUT and HAL_ERROR, but when i use STD_Lib everything is good. So i think if the HAL has BUG of CAN on STM32F1?

Hello,

 

I'm trying to set up CAN on my STM32F103  to send a CAN message but I keep getting HAL_TIMEOUT error from the HAL_CAN_Transmit() function :

/* Check End of transmission flag */
while(!(__HAL_CAN_TRANSMIT_STATUS(hcan, transmitmailbox)))
{
/* Check for the Timeout */
   if(Timeout != HAL_MAX_DELAY)
   {
      if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))                           ///////here!!!!!!!!!!!!!
      {
         hcan->State = HAL_CAN_STATE_TIMEOUT;

         /* Cancel transmission */
         __HAL_CAN_CANCEL_TRANSMIT(hcan, transmitmailbox);

         /* Process unlocked */
         __HAL_UNLOCK(hcan);
         return HAL_TIMEOUT;
      }
   }
}

 

 

Here's my code:

 

/* Includes ------------------------------------------------------------------*/
#include "can.h"

#include "gpio.h"

/* USER CODE BEGIN 0 */
CanTxMsgTypeDef TxMessage;
CanRxMsgTypeDef RxMessage;
/* USER CODE END 0 */

CAN_HandleTypeDef hcan;

/* CAN init function */
void MX_CAN_Init(void)
{

hcan.Instance = CAN1;
hcan.pTxMsg = &TxMessage; //开启收发器
hcan.pRxMsg = &RxMessage;
hcan.Init.Prescaler = 6;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_6TQ;
hcan.Init.BS2 = CAN_BS2_5TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = ENABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

CAN_FilterConfTypeDef sFilterConfig;
//
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();
}
}

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
{

GPIO_InitTypeDef GPIO_InitStruct;
if(canHandle->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */

/* USER CODE END CAN1_MspInit 0 */
/* CAN1 clock enable */
__HAL_RCC_CAN1_CLK_ENABLE();

/**CAN GPIO Configuration
PB8 ------> CAN_RX
PB9 ------> CAN_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

__HAL_AFIO_REMAP_CAN1_2();

/* CAN1 interrupt Init */
HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
/* USER CODE BEGIN CAN1_MspInit 1 */

/* USER CODE END CAN1_MspInit 1 */
}
}

void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
{

if(canHandle->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspDeInit 0 */

/* USER CODE END CAN1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_CAN1_CLK_DISABLE();

/**CAN GPIO Configuration
PB8 ------> CAN_RX
PB9 ------> CAN_TX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);

/* CAN1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
/* USER CODE BEGIN CAN1_MspDeInit 1 */

/* USER CODE END CAN1_MspDeInit 1 */
}
}

/* USER CODE BEGIN 1 */
u8 Can_Send_Msg(u8 i,u8* msg,u8 len)
{
u8 j=0;
switch(i)
{
case 0:TxMessage.StdId = 0x200;break;
case 1:TxMessage.StdId = 0x1FF;break;
}
TxMessage.IDE=CAN_ID_STD; // 标准帧
TxMessage.RTR=CAN_RTR_DATA; // 数据帧
TxMessage.DLC=len; // 要发送的数据长度
for(j=0;j<len;j++)
TxMessage.Data[j]=msg[j];
if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
{
/* Transmition Error */
Error_Handler();
}
return 1;
}

 

u8 Can_Receive_Msg(u8 *buf)
{
u32 i;

if((RxMessage.StdId|0x0F)==0x20F)
{
for(i=0;i<RxMessage.DLC;i++) buf[i] = RxMessage.Data[i];
buf[7] = RxMessage.StdId - 0x201;
return RxMessage.DLC;
}
else return 0;
}

/***************RX中断调回************/
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
{

if (HAL_CAN_Receive_IT(hcan, CAN_FIFO0) != HAL_OK)
{
/* Reception Error */
Error_Handler();
}
Can_Receive_Msg(RxMessage.Data);
}


/* USER CODE END 1 */

Outcomes