补充笔记、线程示例

This commit is contained in:
james 2024-07-24 10:06:29 +08:00
parent 9d57483c76
commit a66c2910bd
19 changed files with 227 additions and 28 deletions

Binary file not shown.

BIN
Day1/en.stsw-link009.zip Normal file

Binary file not shown.

BIN
Day1/error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -1,4 +1,18 @@
# RSOC Day1 # RSOC Day1
## 运行报错
> OpenOCD: GDB Server Quit Unexpectedly. See gdb-server output in TERMINAL tab
for more details.
![error](./error.png)
大概是没能连接设备,尝试以下方法
1. 插好线,摁开关
2. 没装驱动,装驱动
[en.stsw-link007-v3-13-4.zip](./en.stsw-link007-v3-13-4.zip)
[en.stsw-link009.zip](./en.stsw-link009.zip)
## git clone
``` bash
git clone https://github.com/*.git
## git ## git
``` bash ``` bash
git init git init

45
Day2/README.md Normal file
View File

@ -0,0 +1,45 @@
# Day 2
## RTOS(实时系统)
- 确定时间完成,对外部异步事件作出正确响应
- 快、资源消耗少
- 嵌入式系统通常使用RTOS
- 多任务(线程)并发性
![裸机vsRTOS](./thread.png)
裸机并发性(多任务同时)、实时性(快)差
- 中断机制和多任务、优先级抢占和时间片轮转调度
- [RT-Thread启动流程](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/basic/basic?id=rt-thread-%e5%90%af%e5%8a%a8%e6%b5%81%e7%a8%8b)
![RT-Thread启动流程](./rtt_startup.png)
## 线程
### 线程控制块
存放线程信息的一个结构体
[参考文档链接](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/thread/thread?id=%e7%ba%bf%e7%a8%8b%e6%8e%a7%e5%88%b6%e5%9d%97)
### 线程栈
切换线程时,保存当前线程的上下文信息
存放局部变量
### 示例
代码参考[]
![运行结果](image.png)
## 经验分享
一个人忙不过来→多人协同→版本混乱、成果共享难……
### 方法
#### 软件开发管理
- 版本控制 **用git**
可本地离线使用、备份、版本与分支
- 代码review审核
- bug管理 -issue
#### CI持续集成管理
自动编译,报错,避免漏、错交
#### 常见问题
##### HardFault
> lr可能存的是死前的线程地址
- 栈内存写穿(数组越界)
- 栈溢出(分配给线程的内存不够、函数调用太深)
##### 解决方法-排除法
> 两组环境,缩小差异,最终一致,找到问题
#### 效率工具
- Ulog 彩色
- Utest
> menuconfig 输入“/”可以搜索

18
Day2/SConscript Normal file
View File

@ -0,0 +1,18 @@
from building import *
import os
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
if GetDepend(['PKG_USING_RTDUINO']) and not GetDepend(['RTDUINO_NO_SETUP_LOOP']):
src += ['arduino_main.cpp']
group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
group = group + SConscript(os.path.join(item, 'SConscript'))
Return('group')

50
Day2/day2thread.c Normal file
View File

@ -0,0 +1,50 @@
#include<rtthread.h>
#define THREAD_STACK_SIZE 512
#define THREAD_PRIORITY 25
#define THREAD_TIMESLICE 5
static rt_thread_t tid1;
static rt_thread_t tid1 =RT_NULL;
static void thread1_entry(void *parameter)
{
int count = 0;
while(1)
{
rt_kprintf("thread1 count:(%d) tick:%lu\n", count++,rt_tick_get());
rt_thread_delay(500);
}
}
static char thread2_stack[THREAD_STACK_SIZE*2];
static struct rt_thread thread2;
static void thread2_entry(void *parameter)
{
int count = 0;
while(count<10)
{
rt_kprintf("thread2 count:(%d) tick:%lu\n", count++,rt_tick_get());
rt_thread_delay(1000);
}
rt_kprintf("thread2 is exiting\n");
}
int thread(void)
{
tid1=rt_thread_create("thread1",
thread1_entry,
RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY,
THREAD_TIMESLICE);
if(tid1!=RT_NULL)
rt_thread_startup(tid1);
rt_thread_init(&thread2,"thread2",thread2_entry,RT_NULL,thread2_stack,THREAD_STACK_SIZE,THREAD_PRIORITY-1,THREAD_TIMESLICE);
rt_thread_startup(&thread2);
return 0;
}
MSH_CMD_EXPORT(tgame, test thread game);

BIN
Day2/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
Day2/rtt_startup.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

BIN
Day2/thread.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

72
Day3/README.md Normal file
View File

@ -0,0 +1,72 @@
# Day 3 IPC
![alt text](image-3.png)
![alt text](image.png)
1. √
2.
3.
## 临界区
only one can use the resource at a time
有人用了,别人就不能用
## 阻塞非阻塞
Blocking/Non-blocking
![alt text](image-1.png)
阻塞: 等待,一个线程
非阻塞: 不等待
## 挂起
<!-- ## 同步
一个线程在等待另一个线程
## 异步
一个线程在等待另一个线程,另一个线程在等待另一个线程 -->
## 死锁
两个线程互相等待
![alt text](image-2.png)
<!-- ## 互斥
两个线程不能同时使用资源 -->
## 信号量
用于线程间同步、互斥
- 二值信号量 约等于bool 获得1不得0
![alt text](image-4.png)
- 计数信号量 初始为期待的值???
- ![alt text](image-5.png)
- 裸机 根据全局变量flag 反应(错误、破坏、不能挂起一直停在这……???)
- 用系统的api不要flag
- 三种反应:一直等,等一会,不等
## api
### √ 创建信号量(动态)节省资源,动态分配,可能内存破坏
注意区别?
``` c
rt_sem_t rt_sem_create(const char* name, rt_uint32_t value, rt_uint8_t flag);
// flag:RT_IPC_FLAG_FIFO先进先出/RT_IPC_FLAG_PRIO优先级
```
### √ 删除信号量
``` c
rt_err_t rt_sem_delete(rt_sem_t sem);
```
### 初始化信号量(静态)还在内存,别人也用不了
``` c
rt_err_t rt_sem_init(rt_sem_t sem, const char* name, rt_uint32_t value, rt_uint8_t flag);
```
### 脱离信号量
从内核对象管理器中脱离
### 获取信号量
time 单位tick
![alt text](image-6.png)
### 无等待获取信号量
``` c
rt_err_t rt_sem_trytake(rt_sem_t sem);
```
### 释放信号量
``` c
rt_err_t rt_sem_release(rt_sem_t sem);
```

BIN
Day3/image-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 KiB

BIN
Day3/image-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

BIN
Day3/image-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

BIN
Day3/image-4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 KiB

BIN
Day3/image-5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

BIN
Day3/image-6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 346 KiB

BIN
Day3/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

View File

@ -9,42 +9,42 @@
* 2023-12-03 Meco Man support nano version * 2023-12-03 Meco Man support nano version
*/ */
// #include <board.h> #include <board.h>
// #include <rtthread.h>
// #include <drv_gpio.h>
// #ifndef RT_USING_NANO
// #include <rtdevice.h>
// #endif /* RT_USING_NANO */
// #define GPIO_LED_B GET_PIN(F, 11)
// #define GPIO_LED_R GET_PIN(F, 12)
// int main(void)
// {
// rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT);
// while (1)
// {
// rt_pin_write(GPIO_LED_R, PIN_HIGH);
// rt_thread_mdelay(500);
// rt_pin_write(GPIO_LED_R, PIN_LOW);
// rt_thread_mdelay(500);
// }
// }
#include <rtthread.h> #include <rtthread.h>
#include "hello.h" #include <drv_gpio.h>
#ifndef RT_USING_NANO
#include <rtdevice.h>
#endif /* RT_USING_NANO */
#define GPIO_LED_B GET_PIN(F, 11)
#define GPIO_LED_R GET_PIN(F, 12)
int main(void) int main(void)
{ {
rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT);
while (1) while (1)
{ {
Print_Hello_World(); rt_pin_write(GPIO_LED_R, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(GPIO_LED_R, PIN_LOW);
rt_thread_mdelay(500); rt_thread_mdelay(500);
} }
return 0;
} }
// #include <rtthread.h>
// #include "hello.h"
// int main(void)
// {
// while(1)
// {
// Print_Hello_World();
// rt_thread_mdelay(500);
// }
// return 0;
// }
/* /*
* /线 * /线
* *