RT Thread之 Uart2 操作

官网连接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/uart/uart

通过前面的学习,基本上RT Thread操作步骤都是,先配置单片机底层,然后再通过应用层映射到底层,最后再根据实际情况进行操作即可;

一、配置步骤:

1、配置cubemx打开uart2、中断;

2、替换cubemx配置好的文件;

.3、修改Kconfig文件,添加uart2配置选项;

4、用env工具添加uart2配置;

5、修改mdk并添加程序;

6、验证并展示效果;

 

二、配置cubemx打开uart2、中断;

三、替换cubemx配置好的文件;

四、修改Kconfig文件,添加uart2配置选项;

五、用env工具添加uart2配置;

 

六、修改mdk芯片配置并添加程序;

/*
 * 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>


//******************************* main function *******************************************
/******************************************************************************************
*** 函数名称: main
*** 输入参数: 无
*** 返 回 值: 无
*** 调度周期:无
*** 说    明:main函数
***	链		接:无
***	编者										时间										版本
***	wagnlu									2021/04/02							V0.1
******************************************************************************************/
int main(void)
{
    int count = 1;
		
    while (count++)
    {
			rt_thread_mdelay(100);
    }

    return RT_EOK;
}



//******************************* GPIO 应用示例 *******************************************
/******************************************************************************************
*** 函数名称: functest
*** 输入参数: 无
*** 返 回 值: 无
*** 调度周期:无
*** 说    明:led测试函数
***	链		接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/pin/pin
***	编者										时间										版本
***	wagnlu									2021/04/02							V0.1
******************************************************************************************/

/* defined the LED0 pin: PC13 */
#define LED0_PIN_NUM	GET_PIN(C,  13)		


void test_led(void)
{
	uint8_t count =0;		//tset count
	
	/* set LED0 pin mode to output */
	rt_pin_mode(LED0_PIN_NUM, PIN_MODE_OUTPUT);
	
	while(count <5)
	{
		count++;
		rt_kprintf("LED计数:%d \t", count);
		rt_pin_write(LED0_PIN_NUM, PIN_HIGH);		//输出高
		rt_kprintf("引脚输出高电平 \t");
		rt_thread_mdelay(500);
		rt_pin_write(LED0_PIN_NUM, PIN_LOW);		//输出低
		rt_kprintf("引脚输出底电平\r\n");
		rt_thread_mdelay(500);
	}
}

MSH_CMD_EXPORT(test_led, function test led control 5 times );



//******************************* 串口 设备 应用示例 *******************************************
/******************************************************************************************
*** 函数名称: uart_sample
*** 输入参数: 无
*** 返 回 值: 无
*** 调度周期:无
*** 说    明:串口测试函数
***	链		接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/adc/adc
***	编者										时间										版本
***	wagnlu									2021/04/04							V0.1
******************************************************************************************/

#define SAMPLE_UART_NAME       "uart2"
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;
	while (1)
	{		 
		while (rt_device_read(serial, -1, &ch, 1) != 1) 			/* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
		{           
			rt_sem_take(&rx_sem, RT_WAITING_FOREVER);					  /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
		}
		rt_device_write(serial, 0, &ch, 1);
		rt_kprintf("uart recive:\t%c\r\n", ch);																	/* finsh 控制台查看 */
	}
}


static int uart_sample(int argc, char *argv[])
{
	rt_err_t ret = RT_EOK;
	char uart_name[RT_NAME_MAX];
	char str[] = "hello RT-Thread!\r\n";

	if(argc == 2)
	{
		rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
	}
	else
	{
		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));			/* 发送字符串 */
	
	rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);		/* 创建 serial 线程 */	
	if (thread != RT_NULL)																	/* 创建成功则启动线程 */
	{
		rt_thread_startup(thread);
	}
	else
	{
		ret = RT_ERROR;
	}

	return ret;
}

/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(uart_sample, uart device sample);


 

七、验证并展示效果;

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值