一个sio.c的实现及遇到的多任务执行问题
https://sourceforge.net/p/cadcdev/lwip/ci/491e00038f26dc5d775f120aa49519a066819ebf/tree/kos/sio.c
/* KallistiOS ##version## sio.c
Copyright (C)2004 Dan Potter
*/ #include <dc/scif.h>
#include <kos/thread.h>
#include <lwip/lwip.h>
#include "lwip/sio.h" /*
This implements the serial I/O interface for lwIP to use PPP for serial
connections. This hooks straight to dbgio right now but could easily
be swapped over to use the modem stuff.
*/ static volatile int sio_abort = ; sio_fd_t sio_open(u8_t foo) {
int i; sio_abort = ; // Clear out anything in the buffer already
i = ;
while (scif_read() != -)
i++;
if (i)
printf("sio: cleared %d initial chars\n", i); return NULL;
} void sio_send(u8_t ch, sio_fd_t foo) {
scif_write(ch);
scif_flush();
} u8_t sio_recv(sio_fd_t foo) {
int ch; do {
ch = scif_read();
if (ch == -)
thd_sleep();
} while (ch == - && !sio_abort); sio_abort = ;
return ch;
} // I *think* this is right, but sio_* seems to be totally
// undocumented like so many things in lwIP.
u32_t sio_read(sio_fd_t foo, u8_t *outbuf, u32_t bufmax) {
int i, ch; for (i=; i<bufmax && !sio_abort; i++) {
ch = scif_read();
if (ch == -) {
if (i == ) {
thd_sleep();
i--;
continue;
} else
break;
}
outbuf[i] = ch;
} sio_abort = ;
return i;
} // Ditto on the comment for sio_read.
u32_t sio_write(sio_fd_t foo, u8_t *buf, u32_t buflen) {
int i; for (i=; i<buflen && !sio_abort; i++)
scif_write(buf[i]);
scif_flush(); sio_abort = ;
return buflen;
} void sio_read_abort(sio_fd_t foo) {
sio_abort = ;
printf("sio_read_abort called\n");
while (sio_abort)
thd_sleep();
}
上面这个实现似乎没有实现block
下面的实现,一开始while死循环处没有加Delay(1);,结果,其它任务根本起不来,
后来,加了一个Delay(1);之后,其它任务就能起来了。
即使没有Delay(1);,任务调度不是也应该能调度其它任务吗???
/**
* Reads from the serial device.
*
* @param fd serial device handle
* @param data pointer to data buffer for receiving
* @param len maximum length (in bytes) of data to receive
* @return number of bytes actually received - may be 0 if aborted by sio_read_abort
*
* @note This function will block until data can be received. The blocking
* can be cancelled by calling sio_read_abort().
*/
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
{
if(len >= )
{
if(fd.UartNo == )
{
while(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET) {Delay();}
{
*data =(u8)USART_ReceiveData(USART1);
return ;
}
}
else if(fd.UartNo == )
{
while(USART_GetITStatus(USART3, USART_IT_RXNE) == RESET)
{
*data =(u8)USART_ReceiveData(USART3);
return ;
}
}
else if(fd.UartNo == )
{
while(USART_GetITStatus(UART5, USART_IT_RXNE) == RESET)
{
*data =(u8)USART_ReceiveData(UART5);
return ;
}
}
else if(fd.UartNo == )
{
while(USART_GetITStatus(USART6, USART_IT_RXNE) == RESET)
{
*data =(u8)USART_ReceiveData(USART6);
return ;
}
}
}
return ;
}
另外,上面没有实现多字节读取,读取len个,也没有实现abort,可以参考最开始的例子。
目前系统中所有任务的优先级分配如下,空闲任务优先级为0,最大优先级是8:
Main_task 1
ToggleLed4 2
tcpip task 6
udp task 5
eth input 7
slip input 7
slip接收任务如下:
#if SLIP_USE_RX_THREAD
/**
* The SLIP input thread.
*
* Feed the IP layer with incoming packets
*
* @param nf the lwip network interface structure for this slipif
*/
static void
slipif_loop_thread(void *nf)
{
u8_t c;
struct netif *netif = (struct netif *)nf;
struct slipif_priv *priv = (struct slipif_priv *)netif->state; while () {
if (sio_tryread(priv->sd, &c, ) > ) {
slipif_rxbyte_input(netif, c);
}
}
}
#endif /* SLIP_USE_RX_THREAD */
slip接收任务是死循环,没有自动结束,而且优先级是最高的7,因此,slip任务会一直运行,其它任务都无法打断(led、udp任务无法执行)。
在read的while中加Delay(1);可以,而且,这里的Delay(1)得是用的操作系统的delay,如下:
/**
* @brief Inserts a delay time.
* @param nCount: number of Ticks to delay.
* @retval None
*/
void Delay(uint32_t nCount)
{
vTaskDelay(nCount);
}
vTaskDelay就是FreeRTOS操作系统自带的延时函数。因为,操作系统在执行该任务的delay过程中,会去执行其它任务,因此,其它低优先级任务得以继续执行。
如果delay这里用for 100次的这种方式,仍然是不行的,操作系统仍然不能切换到其它任务。
关于vTaskDelay可以看这篇博文http://blog.csdn.net/zhzht19861011/article/details/51705148
需要对整个系统的不同任务、优先级好好了解清楚。
对于高优先级任务,需要执行完就立刻挂起或阻塞,以让其它低优先级任务得以执行。
一个sio.c的实现及遇到的多任务执行问题的更多相关文章
- 意外作出了一个javascript的服务器,可以通过js调用并执行任何java(包括 所有java 内核基本库)及C#类库,并最终由 C# 执行你提交的javascript代码! 不敢藏私,特与大家分
最近研发BDC 云开发部署平台的数据路由及服务管理器意外作出了一个javascript的服务器,可以通过js调用并执行任何java(包括 所有java 内核基本库)及C#类库,并最终由 C# 执行你提 ...
- 假设result是一个float型变量,其值为27.32,value是一个int型变量,其值为15执行以下语句后,两个便利的值分别是多少?为什么?
假设result是一个float型变量,其值为27.32,value是一个int型变量,其值为15执行以下语句后,两个便利的值分别是多少?为什么? 在执行这条语句的过程中,保存在result中的值被读 ...
- Linux是一个基于POSIX和Unix的多用户、多任务、支持多线程和多CPU的性能稳定的操作系统,可免费使用并自由传播。
Linux是一个基于POSIX和Unix的多用户.多任务.支持多线程和多CPU的性能稳定的操作系统,可免费使用并自由传播. Linux是众多操作系统之一 , 目前流行的服务器和 PC 端操作系统有 L ...
- Leaf - 一个由 Go 语言编写的开发效率和执行效率并重的开源游戏服务器框架
转自:https://toutiao.io/posts/0l7l7n/preview Leaf 游戏服务器框架简介 Leaf 是一个由 Go 语言(golang)编写的开发效率和执行效率并重的开源游戏 ...
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
这是我为项目中写的一个测试的例子, 假如,您需要这样一个xml文件, <?xml version="1.0" encoding="utf-8"?> ...
- Linq学习系列-----1.2 一个简单方法的改进思考及不同的执行形式
一.普通模式: #region 模式1 public Form1() { InitializeComponent(); GetProcessByJudge(); } public bool Memor ...
- Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- [mvc>actionResult] 封装一个操作方法的结果并用于代表该操作方法执行框架级操作
- .Net中的AOP系列之构建一个汽车租赁应用
返回<.Net中的AOP>系列学习总目录 本篇目录 开始一个新项目 没有AOP的生活 变更的代价 使用AOP重构 本系列的源码本人已托管于Coding上:点击查看. 本系列的实验环境:VS ...
随机推荐
- 根据输入参数,判定时间范围CheckTimeSpan
对于C#的开发的网页程式,一些企业或者工厂可能会运用这些程式去查询一些资料,考虑到查询的资料太多,假如一个月的资料就有上万条数据,在对于查询资料的SQL语句后时间栏位运用Between.....AND ...
- python中super()
super() : 获取当前类的父类 效果图: 代码: class Animal: def __init__(self,name): self._name = name @property def n ...
- 盘一盘Tidyverse| 筛行选列之select,玩转列操作
原文链接:https://mp.weixin.qq.com/s/ldO0rm3UM_rqlFnU3euYaA 2020年,开封 <R 数据科学>R for data science,系统学 ...
- importlib 根据字符串导入模块
应用: Django中间件,rest framework 组件的全局配置文件 import importlib path = "abc.def.foo" module_path,c ...
- kindeditor富文本编译器
一.网址 kindeditor.net/about.php 二.编辑器的使用,看官方文档 三.常用初始化参数 1.resizeType2或1或0,2时可以拖动改变宽度和高度,1时只能改变高度,0时不能 ...
- 高通量计算框架HTCondor(三)——使用命令
目录 1. 目录 2. 进程 3. 命令 3.1. condor_q 3.2. condor_status 3.3. conodr_submit 3.4. conodr_rm 4. 相关 1. 目录 ...
- [洛谷P1606] [USACO07FEB] 荷叶塘Lilypad Pond
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- 机器学习回顾篇(15):集成学习之GDBT
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- Hystrix 监控数据聚合 Turbine【Finchley 版】
原文地址:https://windmt.com/2018/04/17/spring-cloud-6-turbine/ 上一篇我们介绍了使用 Hystrix Dashboard 来展示 Hystrix ...
- ValidationAttribute特性的截图