笔者最近在使用WHEELTEC 惯导模块时,需要将它给的F103代码移植到F407上
在此期间,遇到了许多问题,特此记录
首先,我们需要在上位机上将所有口都打开,调为NAV模式
接着,代码使用的是标准库,笔者用CUBEMX初始化两个串口,同时打开串口中断后就可以开始移植了
第一个需要改动的函数,用chatgpt将标准库改为HAL库:
void USART3_IRQHandler(void)
{
//__HAL_UART_DISABLE_IT(&huart3, UART_IT_RXNE);
static u8 Count=0;
static u8 rs_count=0;
static u8 last_rsnum=0;
volatile u8 Usart_Receive;
static u8 rsimu_flag=0;
static u8 rsacc_flag=0;
// RS485_RX_RE=0;
// RS485_RX_DE=0;
ttl_receive=1;
if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE) != RESET) // 检查是否接收到数据
{
Usart_Receive = huart3.Instance->DR; // 读取数据
Fd_data[Count] = Usart_Receive; // 串口数据填入数组
if (((last_rsnum == FRAME_END) && (Usart_Receive == FRAME_HEAD)) || Count > 0)
{
rs_count = 1;
Count++;
if ((Fd_data[1] == TYPE_IMU) && (Fd_data[2] == IMU_LEN))
rsimu_flag = 1;
if ((Fd_data[1] == TYPE_AHRS) && (Fd_data[2] == AHRS_LEN))
rsacc_flag = 1;
}
else
Count = 0;
last_rsnum = Usart_Receive;
if (rsimu_flag == 1 && Count == IMU_RS) // 将本帧数据保存至Fd_rsimu数组中
{
Count = 0;
rsimu_flag = 0;
rs_imutype = 1;
if (Fd_data[IMU_RS - 1] == FRAME_END) // 帧尾校验
memcpy(Fd_rsimu, Fd_data, sizeof(Fd_data));
}
if (rsacc_flag == 1 && Count == AHRS_RS)
{
Count = 0;
rsacc_flag = 0;
rs_ahrstype = 1;
if (Fd_data[AHRS_RS - 1] == FRAME_END)
memcpy(Fd_rsahrs, Fd_data, sizeof(Fd_data));
for (int i = 0; i < sizeof(Fd_data); i++)
Fd_data[i] = 0;
}
}
//__HAL_UART_CLEAR_FLAG(&huart3, UART_FLAG_RXNE);
//__HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
}
接着需要加入这个函数,重定义printf函数到串口x输出,因为示例代码使用了printf:
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
huart1.Instance->DR = (uint8_t) ch;
/* Loop until the end of transmission */
while(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) == RESET){}
return ch;
}
最后,最重要的是在while(1)之前,必须加上下面这行代码,打开中断,笔者在这边卡了很久
__HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);