搜索
bottom↓
回复: 26

Protothreads第一个程序(IAR,GCC共用一份代码),两个任务,控制LED闪烁。

[复制链接]

出0入0汤圆

发表于 2007-12-25 18:18:29 | 显示全部楼层 |阅读模式
Protothreads第一个程序(IAR,GCC共用一份代码),两个任务,控制LED闪烁。

编译输出目录:
IAR是debug,
gcc+avr_studio是default,
gcc+makefile是output。

点击此处下载ourdev_193336.rar(文件大小:92K)

实际上Protothreads只是几个宏文件,就能实现线程切换,一个函数都没有。
我移植AVR上加上一个pt_ticks.c文件。整个系统不到200字节。
usmartx可以不用了,以后就用Protothreads了。


Protothreads - Lightweight, Stackless Threads in C   
http://www.sics.se/~adam/pt/  

Protothreads are extremely lightweight stackless threads designed for
severely memory constrained systems such as small embedded systems or
sensor network nodes. Protothreads can be used with or without an
underlying operating system.

Protothreads provides a blocking context on top of an event-driven
system, without the overhead of per-thread stacks. The purpose of
protothreads is to implement sequential flow of control without
complex state machines or full multi-threading.

Main features:

    * No machine specific code - the protothreads library is pure C
    * Does not use error-prone functions such as longjmp()
    * Very small RAM overhead - only two bytes per protothread
    * Can be used with or without an OS
    * Provides blocking wait without full multi-threading or
      stack-switching
    * Freely available under a BSD-like open source license     

Example applications:

    * Memory constrained systems
    * Event-driven protocol stacks
    * Small embedded systems
    * Sensor network nodes

The protothreads library is released under an open source BSD-style
license that allows for both non-commercial and commercial usage. The
only requirement is that credit is given.

The protothreads library was written by Adam Dunkels <adam@sics.se>
with support from Oliver Schmidt <ol.sc@web.de>.

More information and new versions can be found at the protothreads
homepage:
                     http://www.sics.se/~adam/pt/

Documentation can be found in the doc/ subdirectory.

Two example programs illustrating the use of protothreads can be found
in this directory:

   example-small.c         A small example showing how to use protothreads
   example-buffer.c        The bounded buffer problem with protothreads
   example-codelock.c           A code lock with simulated key input

To compile the examples, simply run "make".


Adam Dunkels, 3 June 2006

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2007-12-25 20:03:40 | 显示全部楼层
IAR是最大优化等级?586字节?

出0入0汤圆

 楼主| 发表于 2007-12-25 20:14:55 | 显示全部楼层
IAR最高代码优化,
GCC OS优化。

代码比较短,IAR和GCC优化能力差不多。


出0入0汤圆

发表于 2007-12-25 22:06:11 | 显示全部楼层
iar data要324字节?这么大?
另外,我在WinAVR-20070525下编译出来是602字节的code。winavr版本不同么?

出0入0汤圆

 楼主| 发表于 2007-12-25 22:17:33 | 显示全部楼层
to:楼上,你从新下载编译。
GCC的CFLAGS和LDFLAGS我改动了一下,编译出来的代码更小些。

IAR我设置堆栈是300字节,除去堆栈大小,和GCC使用RAM一致。

IAR和GCC堆栈策略不同。
IAR必须先一定大小分配堆栈,GCC不必先分配堆栈大小。

出0入0汤圆

发表于 2007-12-26 03:28:07 | 显示全部楼层
不错

出0入0汤圆

发表于 2007-12-26 14:58:31 | 显示全部楼层
谢谢了!研究一下!楼主,能简单的注释一下程序吗?看不懂哦!

出0入0汤圆

 楼主| 发表于 2007-12-26 16:18:33 | 显示全部楼层
#include "config.h"

PT_TMR_Create(tmr1);   //创建定时器tmr1
PT_TMR_Create(tmr2);   //创建定时器tmr2

PT_TSK_Create(protothread1);  //创建任务protothread1
PT_TSK_Create(protothread2);  //创建任务protothread2

unsigned char protothread1(struct pt *pt)           //任务protothread1
{
  PT_BEGIN(pt);       //任务开始
  while(1)
  {   
      PT_TimerSet(&tmr1, OS_TICKS_PER_SEC/2);          //设置定时器定时1/2秒
      PT_WAIT_UNTIL(pt, PT_TimerExpired(&tmr1));       //等待1/2秒
      PORTD^=0X80;                                     //LED1 闪烁
  }
  PT_END(pt);      //任务结束
}

unsigned char protothread2(struct pt *pt)            //任务protothread2
{
  PT_BEGIN(pt);      //任务开始
  while(1)
  {
      PT_TimerSet(&tmr2, OS_TICKS_PER_SEC/4);     //设置定时器定时1/4秒
      PT_WAIT_UNTIL(pt, PT_TimerExpired(&tmr2));  //等待1/4秒
      PORTD^=0X40;                                //LED2 闪烁
  }
  PT_END(pt);      //任务结束
}



int main(void)
{
  DDRD=_BV(7)|_BV(6);
  TCNT0 = TCNT0_INIT;
  TCCR0 =  T0_CLK_DIV256;       //定时器0 256分频
  TIMSK|=_BV(0);                //打开定时器0溢出中断

  PT_INIT(&PT_TCB(protothread1));   //初始化任务protothread1
  PT_INIT(&PT_TCB(protothread2));   //初始化任务protothread2
  
  sei();                           //开中断
  
  while(1)   //循环调度任务
{
    protothread1(&PT_TCB(protothread1));
    protothread2(&PT_TCB(protothread2));
  }
}

出0入0汤圆

发表于 2007-12-26 17:07:13 | 显示全部楼层
ATmega32 cortex-m3你好

下载直接用GCC编译报错:

出0入0汤圆

 楼主| 发表于 2007-12-26 17:11:39 | 显示全部楼层
我用的是
AVR_STUDIO4.13SP2+WinAVR-20071221rc1
不要解压到中文目录,包括桌面。

里面还有一个makefile,也可以编译这个工程。

出0入0汤圆

发表于 2007-12-26 17:18:10 | 显示全部楼层
直接用命令行的make,就无所谓是否是中文目录了。
另外,代码做些小改动,gcc下可以优化到 520 Bytes

出0入0汤圆

发表于 2007-12-26 23:14:57 | 显示全部楼层
请教一下10楼,如何优化?是代码优化还是编译器选项呢?

出0入0汤圆

发表于 2007-12-27 00:41:19 | 显示全部楼层
哈哈,看楼主介绍这个DD正合我意,学习学习

出0入50汤圆

发表于 2007-12-27 09:18:10 | 显示全部楼层
楼主的钻研精神自叹不如,佩服ing................

楼主现在开始做流明诺瑞的拥泵了,ZLG的fans,^_^

出0入12汤圆

发表于 2007-12-27 10:43:54 | 显示全部楼层
顶,楼主带给我们这样一个好系统,终于可以不裸----穿上裤衩了。

出0入0汤圆

 楼主| 发表于 2007-12-27 15:57:18 | 显示全部楼层
STM32比LM3S要复杂很多,还是先从简单的学起。

还要买个ZLG的FTDI JTAG。

Protothreads真的不错,从usmartx全面转向Protothreads。

出0入12汤圆

发表于 2007-12-28 11:47:22 | 显示全部楼层
把pt最简单的例子,用在m8上了,用winavr070525通过,用仿真软件看到了结果。
点击此处下载ourdev_194672.zip(文件大小:17K)

出0入0汤圆

发表于 2007-12-28 18:39:49 | 显示全部楼层
ATmega32 cortex-m3,你用的IAR是什么版本的??哪里有下载哈??是完整版还是EV版?

出0入0汤圆

 楼主| 发表于 2007-12-28 20:50:15 | 显示全部楼层
IAR EWAVR 4.30A
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=848850&bbs_page_no=1&search_mode=1&search_text=I*A*R&bbs_id=1000

出0入0汤圆

发表于 2008-3-12 10:27:52 | 显示全部楼层
留名。打算仔细看看。

出0入0汤圆

 楼主| 发表于 2008-3-12 13:49:47 | 显示全部楼层
更新一下,改成C++,到比较完善的时候再改写成C。
打造自己的Protothreads(2008.03.7)  (AVR在10楼)
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=935503&bbs_page_no=1&bbs_id=1032

出0入0汤圆

发表于 2008-3-26 17:44:19 | 显示全部楼层
看着不错,标记

出0入0汤圆

发表于 2008-3-31 17:18:11 | 显示全部楼层
顶起

出0入0汤圆

 楼主| 发表于 2008-3-31 17:58:17 | 显示全部楼层
更新一下2008.03.29
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=960766&bbs_page_no=1&search_mode=3&search_text=ATmega32&bbs_id=9999

出0入0汤圆

发表于 2008-4-1 09:13:59 | 显示全部楼层
有sem的例子吗?谢谢!

出0入0汤圆

发表于 2009-2-25 15:00:04 | 显示全部楼层
刚看觉得很吃惊,体积太小了。
下载代码看了看,眼前一亮,结构不错。
仔细想想,代码分文件写,可能就会有大麻烦了。

出0入0汤圆

发表于 2009-3-22 19:01:34 | 显示全部楼层
以前的老帖子。

Protothreads的弊病还是比较明显。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-20 19:46

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表