RTT 使用menuconfig 和cubeMux 在移植過來的模板工程上增加一個串口2

1、在board目錄下Kconfig文件中增加如下內容

打開CubeMX_Config下的CubeMX_Config.ioc的cubeMx工程

a;使能串口1;

b;使能外部時鐘,設置時鐘樹;

 

1工程名字必須是CubeMX_Config

下圖是新增串口的配置

 

代碼生成後會出現編譯不過的情況

解決辦法是添加一個 stm32f1xx_hal_conf.h 

參看博客 https://blog.csdn.net/u010261063/article/details/103988752

測試代碼:

/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* defined the LED0 pin: PB1 */
#define LED0_PIN    GET_PIN(B, 5)



#define SAMPLE_UART_NAME "uart3"
/* ? ? ? ? ? ? ? ? ? ? */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
/* ? ? ? ? ? ? ? ? */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* ? ? ? ? ? ? ? ? ? ? ? ?, ? ? ? ? ? ? ?, ? ? ? ? ? ? ? ? ? */
    rt_sem_release(&rx_sem);
    return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
    char ch='a';
    while (1)
    {
    /* ? ? ? ? ? ? ? ? ? ? ? ?, ? ? ? ? ? ? ? ? ? ? ? ? ? */
        while (rt_device_read(serial, -1, &ch, 1) != 1)
        {
        /* ? ? ? ? ? ? ? ? ?, ? ? ? ? ? ? ? ? ? ? ? ? */
        rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
        /* ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
       // ch = ch + 1;
        rt_device_write(serial, 0, &ch, 1);
       // rt_thread_sleep(100);
    }
}
static int uart_sample()
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX] ;
    char str[] = "hello RT-Thread!\r\n";

    rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);

    /* ? ? ? ? ? ? ? ? ? ? */
    serial = rt_device_find(uart_name);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }
    /* ? ? ? ? ? ? */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    /* ? ? ? ? ? ? ? ? */
    rt_device_set_rx_indicate(serial, uart_input);
    /* ? ? ? ? ? */
    rt_device_write(serial, 0, str, (sizeof(str) - 1));
    /* ? ? serial ? ? */
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL,
    1024, 25, 10);
    /* ? ? ? ? ? ? ? ? ? */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }
    return ret;
}


int main(void)
{
    int count = 1;
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
		uart_sample();
    while (count++)
    {
        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }

    return RT_EOK;
}



後記:

移植模板工程教程  https://github.com/RT-Thread/rt-thread/blob/master/bsp/stm32/docs/STM32%E7%B3%BB%E5%88%97BSP%E5%88%B6%E4%BD%9C%E6%95%99%E7%A8%8B.md

更改cubeMx後導致stm32f1xx_hal_conf.h文件中的宏定義發生變化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章