10 同样的FINSH驱动代码,在STM32F1上正常工作,在STM32F4上就出错

发布于 2021-04-15 14:10:44    浏览:219

正点原子战舰开发板上这段代码运行正常,探索者开发板上就出现问题,不加控制台的话是在正常运行的,这是为什么?有点想不明白,初始化都是通过CubeMX配置好移植的,硬件直接运行官方例程控制台都没问题
finsh问题.jpg

#include "board.h"
#include "drv_usart.h"
#include "string.h"

UART_HandleTypeDef huart1;

rt_err_t rt_uart_init(rt_device_t dev)
{
    huart1.Instance                 = USART1;
  huart1.Init.BaudRate                 = 115200;
  huart1.Init.WordLength             = UART_WORDLENGTH_8B;
  huart1.Init.StopBits                 = UART_STOPBITS_1;
  huart1.Init.Parity                 = UART_PARITY_NONE;
  huart1.Init.Mode                     = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl             = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling             = UART_OVERSAMPLING_16;
    
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    return RT_ERROR;
  }
    
    return RT_EOK;
}

rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
{
    return rt_uart_init(dev);
}

static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    rt_size_t i = 0;
    char a = '\r';
    const char *val = 0;
    
    val = (const char *)buffer;
    
    __HAL_UNLOCK(&huart1);
    
    for(i = 0; i < size; i++)
    {
        if(*(val + i) == '\n')
        {
            HAL_UART_Transmit(&huart1, (uint8_t *)&a, 1, 0xff);
        }
        HAL_UART_Transmit(&huart1, (uint8_t *)(val + i), 1, 0xff);
    }
    
    return 1;
}

static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{    
    while(1)
    {
        if(HAL_UART_Receive(&huart1, buffer, size, 0xff) == HAL_OK)
        {
            return size;
        }
        
        rt_thread_delay(1);
    }
}

struct rt_device my_serial;

void rt_hw_usart_init(void)
{
        my_serial.type = RT_Device_Class_Char;
    my_serial.init = rt_uart_init;
    my_serial.open = rt_uart_open;
    my_serial.read = rt_uart_read;
    my_serial.write = rt_uart_write;
    
    rt_device_register(&my_serial, "uart1",
                                         RT_DEVICE_FLAG_RDWR);
}
INIT_BOARD_EXPORT(rt_hw_usart_init);

查看更多

mii
mii 2021-04-15
This guy hasn't written anything yet

都是通过cube生成的话,基本可以明确初始化是完成的。但要考虑到硬件会不会问题,先裸机测试一下。如果硬件无故障,单步调试是不是卡在图一位置?

3 个回答
flashman2002
flashman2002 2021-04-15
This guy hasn't written anything yet

看来应该是不同硬件的初始化不同造成的。因为嵌入式软件开发与硬件是紧密关联的。

steins
steins 2021-04-15
This guy hasn't written anything yet

问题解决,因为是复制以前做过的项目的rtconfig, console device name当时是"uart1", 注册成了"usart1", 没想到是自己坑自己...

撰写答案

请登录后再发布答案,点击登录
关注者
0
被浏览
219

发布
问题

分享
好友

手机
浏览

扫码手机浏览