你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【NUCLEO-WB09KE评测】+实现串口printf打印和shell移植

[复制链接]
STMWoodData 发布时间:2024-10-20 11:28

基于STM32CubeMX新建了基础模板工程,实现了点灯。本次继续实现串口发送接收功能。方便打印输出和shell命令调试。

首先实现串口中断发送接收功能。通过FIFO缓存来控制发送接收。

主要代码如下:

static uint8_t  uart_recv_buff[4];
static uint8_t  uart_send_buff[128];


shell_uart_buffer_t  g_shell_uart=
{
    .tx.read_i = 0,
    .tx.write_i = 0,

    .rx.read_i = 0,
    .rx.write_i = 0,

    .tx_cpl = 0,
};

//
static void uart_get_data_send(void)
{
    uint32_t    i;
    //
    for(i=0;i<128;i++)
    {
        if(g_shell_uart.tx.read_i != g_shell_uart.tx.write_i)
        {
            uart_send_buff[i] = g_shell_uart.tx.buff[g_shell_uart.tx.read_i++];
            g_shell_uart.tx.read_i &= 0x1ff; //256Byte
        }else break;
    }
    if(i)
    {
        g_shell_uart.tx_cpl = 1;
        HAL_UART_Transmit_IT(&UART_SHELL_PORT,uart_send_buff,i);
    }
}


static void uart_send_char(uint8_t ch)
{
    while(((g_shell_uart.tx.write_i+1)&0x1ff) == g_shell_uart.tx.read_i)
    {
        //fifo buffer full
        if((g_shell_uart.tx_cpl == 0))
        {
            uart_get_data_send();
        }
    }

    g_shell_uart.tx.buff[g_shell_uart.tx.write_i++] = ch;
    g_shell_uart.tx.write_i &= 0x1ff;//256Byte

    if((g_shell_uart.tx_cpl == 0))
    {
        uart_get_data_send();
    }
}

//////////////////////////////////////////////////////////////////////////////
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == UART_PORT)   //shell
    {
        //
        if(((g_shell_uart.rx.write_i+1)&0x1ff) != g_shell_uart.rx.read_i)
        {
            g_shell_uart.rx.buff[g_shell_uart.rx.write_i++] = uart_recv_buff[0] & 0xff;
            g_shell_uart.rx.write_i &= 0x1ff;//256Byte
        }
        HAL_UART_Receive_IT(huart,uart_recv_buff,1);//
    }
}

//
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == UART_PORT)   //shell
    {
        g_shell_uart.tx_cpl = 0;
        uart_get_data_send();
    }
}

//
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == UART_PORT)   //shell
    {
        HAL_UART_Receive_IT(huart,uart_recv_buff,1);//
    }
}

uint8_t USART_GetChar(void)
{
    uint8_t ch;
    while(g_shell_uart.rx.read_i == g_shell_uart.rx.write_i);
    ch = g_shell_uart.rx.buff[g_shell_uart.rx.read_i++];
    g_shell_uart.rx.read_i &= 0x1ff; //256Byte
    return ch;
}

void USART_PutChar(uint8_t ch)
{
    uart_send_char(ch);
}

下面对接printf输出。选择pack里面的软件包。

image.png

然后实现下面几个函数:


void _ttywrch (int ch) {
}

void _sys_exit (void) {
}

int stdin_getchar (void)
{
    return USART_GetChar();
}

int stdout_putchar (int ch)
{
    USART_PutChar(ch & 0xff);
    return ch;
}

初始化启动串口中断接收后。就可以准备printf打印了。

image.png

编译下载效果:

image.png

下面移植一下nr_micro_shell功能。首先添加nr_micro_shell的代码,然后配置串口的输入和输出。

主要配置如下:

image.png

添加头文件路径:

image.png

添加初始化和主循环处理串口数据。

image.png

image.png

然后编译下载,就可以实现shell调试命令了。

image.png

收藏 评论0 发布时间:2024-10-20 11:28

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版