GD32HAL库开发问题
- 1can初始化进入error handle
- 2发送邮箱不能按照填写顺序发送
- 3 GD32修改代码被stm32cudemx覆盖问题
1can初始化进入error handle
HAL库的HAL_CAN_Init中,hcan->Instance->MSR寄存器无法清零,STM32先清零,再退出睡眠模式,而GD32需要先退出才能清零
while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U) {if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE) {/* Update error code */hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;/* Change CAN state */hcan->State = HAL_CAN_STATE_ERROR;return HAL_ERROR;}
再其前面加上退出睡眠语句即可
#if defined(GD32F407xx)CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);#endif/* Wait initialisation acknowledge */while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U) {if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE) {/* Update error code */hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;/* Change CAN state */hcan->State = HAL_CAN_STATE_ERROR;return HAL_ERROR;}}
2发送邮箱不能按照填写顺序发送
配置了发送邮箱按照填写邮箱的先后顺序发送,ST可以按照顺序发送,而GD不行
查看HAL_CAN_AddTxMessage里面,发现transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos获取的GD32空闲的邮箱不对
/* Select an empty transmit mailbox */transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;/* Check transmit mailbox value */if (transmitmailbox > 2U) {/* Update error code */hcan->ErrorCode |= HAL_CAN_ERROR_INTERNAL;return HAL_ERROR;}
对比文档发现,CAN_TSR_CODE的寄存器ST和GD定义不一样,ST表示的是空闲邮箱,而GD表示下一个发送邮箱
通过TMEx获取空闲邮箱后正常
if (((tsr & CAN_TSR_TME0) != 0U) || ((tsr & CAN_TSR_TME1) != 0U) || ((tsr & CAN_TSR_TME2) != 0U)) {/* Select an empty transmit mailbox */transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;#if defined(GD32F407xx)if (CAN_TSR_TME0 == (tsr & CAN_TSR_TME0)) {transmitmailbox = 0;} else if (CAN_TSR_TME1 == (tsr & CAN_TSR_TME1)) {transmitmailbox = 1;} else if (CAN_TSR_TME2 == (tsr & CAN_TSR_TME2)) {transmitmailbox = 2;} else {transmitmailbox = 3;}#endif/* Check transmit mailbox value */if (transmitmailbox > 2U) {/* Update error code */hcan->ErrorCode |= HAL_CAN_ERROR_INTERNAL;return HAL_ERROR;}
3 GD32修改代码被stm32cudemx覆盖问题
在上面中,对GD32不一样的地方加入了GD32F407xx预处理命令,在KEIL或stm32cubeid中定义该宏就可以编译可以加入GD32代码,但是每次重新生成代码都会被覆盖。在库文件里面修改改文件后,cubemx生成文件时从库复制过来就是修改后的文件。
对应的库文件路径在C:\Users\用户名\STM32Cube\Repository\STM32Cube_FW_F4_V1.27.1\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_can.c