[新版中科蓝讯AB32VG1 RISC-V开发板] - 4:线程版的Blink
本帖最后由 MianQi 于 2021-8-12 13:37 编辑<p>整个程序从功能上,按照逻辑顺序,分成这样几部分:</p>
<p> </p>
<p>1、管脚相关类的声明、定义与实例化</p>
<p>2、定义管脚</p>
<p>3、定义管脚的驱动程序</p>
<p>4、定义线程的主体(入口)函数</p>
<p>5、定义线程的初始化(启动)函数</p>
<p>6、调用启动线程的函数</p>
<p>7、修改项目的入口函数(main.c)</p>
<p> </p>
<p>首先要指出(<span style="font-size:12px;"><span style="font-family: Microsoft YaHei, 微软雅黑, Roboto, Helvetica Neue, Helvetica, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, WenQuanYi Micro Hei, sans-serif; font-weight: 700; font-style: normal; color: rgb(51, 51, 51); background: transparent none repeat scroll 0% 0%; letter-spacing: 0pt; vertical-align: baseline;">二、中科蓝讯AB32VG1上的GPIO实践 - </span></span>https://docs.qq.com/doc/DTVVWWXpLRVl6cER2?_t=1626260921919)跟实测结果不一致的地方:</p>
<p>pin2 ➡ 3.3v</p>
<p>pin1 ➡ R20(470Ω)➡ PA1</p>
<p>pin3 ➡ R17(1kΩ)➡ PE4</p>
<p>pin4 ➡ R18(470Ω)➡ PE1</p>
<p> </p>
<p> </p>
<p> </p>
本帖最后由 MianQi 于 2021-8-12 13:49 编辑
<p>整个项目在创建的基础上做过改动的只有三个文件:</p>
<p>1、在“applications”目录下添加了“LED.c”和“LED.h”两个文件。</p>
<p>2、对“applications”目录下的“main.c”做了改动。</p>
<p> </p>
<p>其中,“LED.h”实际上有效内容为空:</p>
<pre>
<code class="language-cpp">/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-11 MQ the first version
*/
</code></pre>
<p> </p>
<p>“main.c”的内容接近为空:</p>
<pre>
<code class="language-cpp">/*
* Copyright (c) 2020-2021, Bluetrum Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020/12/10 greedyhao The first version
*/
/**
* Notice!
* All functions or data that are called during an interrupt need to be in RAM.
* You can do it the way exception_isr() does.
*/
#include <rtthread.h>
#include "board.h"
int main(void)
{
/*
uint8_t pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_kprintf("Hello, world\n");
*/
while (1)
{
/*
rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(500);
*/
}
}</code></pre>
<p> </p>
<p>几乎全部的有效内容都在“LED.c”中:</p>
<pre>
<code class="language-cpp">#include "LED.h"
#include <rtthread.h>
#include "board.h"
//定义一个结构体类型
struct LED_RGB
{
uint8_t LED_R;
uint8_t LED_G;
uint8_t LED_B;
};
//实例化一个结构体对象
struct LED_RGB led_rgb;
//结构体对象的初始化
void LED_RGB_Init(void){
//将结构体对象的成员对应到管脚
led_rgb.LED_R = rt_pin_get("PE.1");
led_rgb.LED_G = rt_pin_get("PE.4");
led_rgb.LED_B = rt_pin_get("PA.1");
//设置结构体对象的成员管脚的输出方式
rt_pin_mode(led_rgb.LED_R, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgb.LED_G, PIN_MODE_OUTPUT);
rt_pin_mode(led_rgb.LED_B, PIN_MODE_OUTPUT);
}
//RGB三色LED各自的驱动程序
//if(toggle==1) LED亮
//if(toggle==0) LED灭
void RGB_Red(rt_bool_t toggle){
rt_pin_write(led_rgb.LED_G, PIN_HIGH);
rt_pin_write(led_rgb.LED_B, PIN_HIGH);
if(toggle){
rt_pin_write(led_rgb.LED_R, PIN_LOW);
} else{
rt_pin_write(led_rgb.LED_R, PIN_HIGH);
}
}
void RGB_Green(rt_bool_t toggle){
rt_pin_write(led_rgb.LED_R, PIN_HIGH);
rt_pin_write(led_rgb.LED_B, PIN_HIGH);
if(toggle){
rt_pin_write(led_rgb.LED_G, PIN_LOW);
} else{
rt_pin_write(led_rgb.LED_G, PIN_HIGH);
}
}
void RGB_Blue(rt_bool_t toggle){
rt_pin_write(led_rgb.LED_R, PIN_HIGH);
rt_pin_write(led_rgb.LED_G, PIN_HIGH);
if(toggle){
rt_pin_write(led_rgb.LED_B, PIN_LOW);
} else{
rt_pin_write(led_rgb.LED_B, PIN_HIGH);
}
}
//编写线程入口函数(主程序文件)
static void led_rgb_thread_entry(void* p){
LED_RGB_Init();
while(1){
RGB_Red(1);
rt_thread_mdelay(1000);
RGB_Red(0);
rt_thread_mdelay(1000);
RGB_Red(1);
rt_thread_mdelay(1000);
RGB_Red(0);
rt_thread_mdelay(1000);
RGB_Green(1);
rt_thread_mdelay(1000);
RGB_Green(0);
rt_thread_mdelay(1000);
RGB_Green(1);
rt_thread_mdelay(1000);
RGB_Green(0);
rt_thread_mdelay(1000);
RGB_Blue(1);
rt_thread_mdelay(1000);
RGB_Blue(0);
rt_thread_mdelay(1000);
RGB_Blue(1);
rt_thread_mdelay(1000);
RGB_Blue(0);
rt_thread_mdelay(1000);
}
}
//创建线程启动函数,用于启动此前创建的线程主体
static int Thread_led_rgb(void){
rt_thread_t thread = RT_NULL;
thread = rt_thread_create("led_rgb", led_rgb_thread_entry, RT_NULL, 512, 10, 10);
if(thread == RT_NULL){
rt_kprintf("Thread_led_rgb initiate ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
//将线程的初始化加入系统初始化,启动线程
INIT_APP_EXPORT(Thread_led_rgb);
</code></pre>
<p> </p>
<p>演示效果:</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=18L411J7MS&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p>条理清晰,谢谢分享。</p>
<p>while(1)里面可以优化一下呀,这个看着好难受。。。</p>
freebsder 发表于 2021-8-17 20:37
while(1)里面可以优化一下呀,这个看着好难受。。。
<p>想延续一下汇编风格——主线程留空,任务都在分块中完成。</p>
MianQi 发表于 2021-8-18 10:31
想延续一下汇编风格——主线程留空,任务都在分块中完成。
<p>哈哈,汇编,,,牛。</p>
<p>之前想在沁恒的板子上尝试一下汇编,资料不全。这次,看到蓝讯的这个板子依然没有提供指令集和寄存器地址。</p>
页:
[1]