[抢楼250] 菜地公告:即日起创建《菜农Cortex-M0助学园地》(盖楼入口)

[复制链接]
998019|18727
lixiaoxu2meng 发表于 2011-12-2 10:23 | 显示全部楼层
再补一个:lol
 楼主| hotpower 发表于 2011-12-2 16:04 | 显示全部楼层
李老师授课
用户数据长度不够4字节的,可以扩展到4字节,就可以使用邮箱传递了,但要考虑效率问题,4字节的数据,效率是100%,2字节的数据,效率只有50%,单字节的数据,效率只有25%。
大于4字节的数据,一般不采用直接传递数据本身,而是把数据的地址(指针)作为消息来传递。
也可以采用拆分数据的方法,把大数据分成小块从邮箱传递,但这种方法需要考虑多方面的问题,如抢断、邮箱满等情况。
对于消息数据的类型来说,由于传统的邮箱发送和接收函数只能接受固定的数据类型(一般是void*),所以用户的数据类型,需要在本身的类型和固定类型之间强制转换(发送时强制为void*,接收时强制回用户类型),这样做,除了麻烦外,也是一种隐患。
而look由于是基于c++的RTOS,比较容易地解决了这个问题,mbox_t设计为模板类,用户的消息类型需要在邮箱创建时,以模板参数的形式给出,并且消息的发送和接收函数,也会只接受这种用户类型的消息,这就避免了传统的强制的方法,不仅优雅,而且是类型安全的。
下面具体说下mbox_t的使用方法:
首先要创建邮箱实例,上面说了,需要给出用户消息类型和邮箱容量作为创建时的模板参数,语法形式为:
mbox_t<用户消息类型,邮箱容量> 邮箱实例名;
使用邮箱发送和接收消息,有一个限制:不允许在调度器尚未运转之前,这些地方位于:
1、构造函数,当创建全局对象时。
2、LOOK_HWINIT()函数。
发送消息使用mbox_t::put()函数,put()函数的原型为:
bool put(T msg, uintptr_t timeout = 0);
T是用户消息类型,msg是消息实体,timeout是超时限制。
put()函数还有其他几种形式:
bool tryput(T msg);
非阻塞地发送消息。
bool tryput_front(T msg);
非阻塞地以lifo方式发送消息。
bool put_front(T msg, uintptr_t timeout = 0);
以lifo方式发送消息。
bool do_tryput(T msg);
在调度器已锁定的情况下,发送消息,没有阻塞。
bool do_tryput_front(T msg);
在调度器已锁定的情况下,以lifo方式发送消息,没有阻塞。

 楼主| hotpower 发表于 2011-12-2 17:41 | 显示全部楼层
下午测试了15个look的最基本应用的例程。
5251 发表于 2011-12-2 18:56 | 显示全部楼层
murex 发表于 2011-12-2 21:08 | 显示全部楼层
:lol晚上有空了来盖下楼
老鱼探戈 发表于 2011-12-2 22:45 | 显示全部楼层
盖楼。。。。
 楼主| hotpower 发表于 2011-12-3 00:48 | 显示全部楼层
加紧盖楼,LOOK革命教育即将开始
 楼主| hotpower 发表于 2011-12-3 00:53 | 显示全部楼层
目前楼差为1681
 楼主| hotpower 发表于 2011-12-3 01:21 | 显示全部楼层
 楼主| hotpower 发表于 2011-12-3 01:24 | 显示全部楼层
忘了,不知LOOK的缔造者李老师是否也是俺色诱所为???

菜地公告:TinyThread的缔造者将被菜农色诱入群!
 楼主| hotpower 发表于 2011-12-3 01:29 | 显示全部楼层
delay.cpp
  1. #include "Look_delay.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. instance_task1_Look_delay_t task1_Look_delay(1);        // 任务实例

  5. // 任务类 task1_Look_delay_t 的例程
  6. __DEBUG//debug调试不优化便于调试
  7. void task1_Look_delay_t::routine()
  8. {
  9.         // TODO: 在此编写例程的内容
  10.         using namespace sfr::gpio;
  11.         while (true) {
  12.                 // TODO: 在此编写例程的内容
  13.                 GPIOA.DOUT().DOUT3(1)//LED2灭
  14.                             .DOUT2(0);//LED1亮
  15.                 delay(LOOK_TICKS_PER_SEC / 2);//暂时放弃控制权给Task2
  16.                 GPIOA.DOUT().DOUT2 = 1;//LED1灭
  17.                 delay(LOOK_TICKS_PER_SEC / 2);//暂时放弃控制权给Task2
  18.         }
  19. }

  20. instance_task2_Look_delay_t task2_Look_delay(2);        // 任务实例

  21. // 任务类 task2_Look_delay_t 的例程
  22. __DEBUG//debug调试不优化便于调试
  23. void task2_Look_delay_t::routine()
  24. {
  25.         // TODO: 在此编写例程的内容
  26.         using namespace sfr::gpio;
  27.         while (true) {
  28.                 // TODO: 在此编写例程的内容
  29.                 GPIOA.DOUT().DOUT3 = 0;//LED2亮
  30. //由于Task1任务级别高,故Task2不可能独占CPU,将会被Task1抢占
  31.                 for(int i = 0; i < 0x250000; i++) asm volatile("nop");//独占CPU
  32.         }
  33. }
 楼主| hotpower 发表于 2011-12-3 01:30 | 显示全部楼层
yield.cpp
  1. #include "Look_yield.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. instance_task1_Look_yield_t task1_Look_yield;        // 任务实例

  5. // 任务类 task1_Look_yield_t 的例程
  6. __DEBUG//debug调试不优化便于调试
  7. void task1_Look_yield_t::routine()
  8. {
  9.         // TODO: 在此编写例程的内容
  10.         using namespace sfr::gpio;
  11.         while (true) {
  12.                 // TODO: 在此编写例程的内容
  13.                 GPIOA.DOUT().DOUT3(1)//LED2灭
  14.                             .DOUT2(0);//LED1亮
  15.                 for(int i = 0; i < 0x25000; i++) asm volatile("nop");//独占CPU
  16.                 scheduler.yield();
  17.         }
  18. }

  19. instance_task2_Look_yield_t task2_Look_yield;        // 任务实例

  20. // 任务类 task2_Look_yield_t 的例程
  21. __DEBUG//debug调试不优化便于调试
  22. void task2_Look_yield_t::routine()
  23. {
  24.         // TODO: 在此编写例程的内容
  25.         using namespace sfr::gpio;
  26.         while (true) {
  27.                 // TODO: 在此编写例程的内容
  28.                 GPIOA.DOUT().DOUT2(1)//LED1灭
  29.                             .DOUT3(0);//LED2亮
  30.                 for(int i = 0; i < 0x25000; i++) asm volatile("nop");//独占CPU
  31.                 scheduler.yield();
  32.         }
  33. }
 楼主| hotpower 发表于 2011-12-3 01:31 | 显示全部楼层
timer.cpp
  1. #include "Look_timer.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. instance_task_Look_timer_t task_Look_timer;        // 任务实例

  5. // 任务类 task_Look_timer_t 的例程
  6. void task_Look_timer_t::routine()
  7. {
  8.         // TODO: 在此编写例程的内容
  9.         using namespace sfr::gpio;
  10.         timer1.start(interval1);//启动定时器1
  11.         timer2.start(interval2);//启动定时器1
  12.         while (true) {
  13.                 // TODO: 在此编写例程的内容
  14.                 GPIOA.DOUT().DOUT2(t1)//LED1
  15.                             .DOUT3(t2);//LED2
  16.                 delay(LOOK_TICKS_PER_SEC / 1000);
  17.         }
  18. }

  19. void task_Look_timer_t::on_signal(void* addr, uintptr_t signal)
  20. {
  21.         using namespace sfr::gpio;
  22.         if (addr == &timer1) {
  23.                 //处理定时器事务
  24.                 t1 = !t1;
  25.                 timer1.do_start(interval1);//重置定时器
  26.         }
  27.         else if (addr == &timer2) {
  28.                 //处理定时器事务
  29.                 timer2.do_start(interval2);//重置定时器
  30.                 t2 = !t2;
  31.         }
  32.         else {
  33.                 task_t::on_signal(addr, signal);
  34.         }

  35. }
 楼主| hotpower 发表于 2011-12-3 01:32 | 显示全部楼层
idle.cpp
  1. #include "Look_idle.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. flag_t Flag(0);//初始化

  5. // eint_t 类提供了 INT0/INT1 的接口
  6. // 当 INT0/INT1 发生时,对象将发送相应的 int 消息到 mbox。
  7. class eint_t : public interrupt_t {
  8. public:
  9.         __INLINE eint_t();

  10. protected:
  11.         bool isr(int vector);
  12.         void dsr(int vector, uintptr_t count);
  13. };

  14. // eint 构造函数
  15. __DEBUG//debug调试不优化便于调试
  16. __INLINE eint_t::eint_t()
  17. {
  18.         using namespace sfr::gpio;
  19.         attach(EINT0_IRQn);//绑定外部中断0
  20.         attach(EINT1_IRQn);//绑定外部中断1
  21.         GPIOB.IEN(0).IF_EN15(1).IF_EN14(1);//开启Key1,Key2中断
  22.         vector_t::enable(EINT0_IRQn);//使能外部中断0即Key1中断
  23.         vector_t::enable(EINT1_IRQn);//使能外部中断1即Key2中断
  24. }

  25. // eint 中断服务例程
  26. __DEBUG//debug调试不优化便于调试
  27. bool eint_t::isr(int vector)
  28. {
  29.         using namespace sfr::gpio;
  30.         GPIOB.ISRC = GPIOB.ISRC;                        // 清中断 flag
  31.         return true;
  32. }

  33. // eint 中断滞后服务例程
  34. __DEBUG//debug调试不优化便于调试
  35. void eint_t::dsr(int vector, uintptr_t count)
  36. {
  37.         using namespace sfr::gpio;
  38.         if (vector == EINT0_IRQn)//Key2中断
  39.         {
  40.                 Flag.do_set_bits(2);//在中断中唤醒任务2
  41.         }
  42.         else if (vector == EINT1_IRQn)//Key1中断
  43.         {
  44.                 Flag.do_set_bits(1);//在中断中唤醒任务1
  45.         }
  46. }

  47. eint_t eint;                                                                        // 创建 eint 对象

  48. instance_task1_Look_idle_t task1_Look_idle(1);        // 任务实例

  49. // 任务类 task1_Look_idle_t 的例程
  50. void task1_Look_idle_t::routine()
  51. {
  52.         // TODO: 在此编写 task1_Look_idle_t 例程的内容
  53.         using namespace sfr::gpio;
  54.         while (true) {
  55.                 // TODO: 在此编写 task1_Look_idle_t 例程的内容
  56.                 int flag = Flag.wait(1, flag_t::ANY_CONSUME);//阻塞等待Key1中断
  57.                 if (flag)
  58.                 {
  59.                         GPIOA.DOUT().DOUT2 = 0;//LED1亮
  60.                         delay(LOOK_TICKS_PER_SEC / 2);
  61.                         GPIOA.DOUT().DOUT2 = 1;//LED1灭
  62.                 }
  63.         }
  64. }

  65. instance_task2_Look_idle_t task2_Look_idle(2);        // 任务实例

  66. // 任务类 task2_Look_idle_t 的例程
  67. void task2_Look_idle_t::routine()
  68. {
  69.         // TODO: 在此编写 task2_Look_idle_t 例程的内容
  70.         using namespace sfr::gpio;
  71.         while (true) {
  72.                 // TODO: 在此编写 task2_Look_idle_t 例程的内容
  73.                 int flag = Flag.wait(2, flag_t::ANY_CONSUME);//阻塞等待Key2中断
  74.                 if (flag)
  75.                 {
  76.                         GPIOA.DOUT().DOUT3 = 0;//LED2亮
  77.                         delay(LOOK_TICKS_PER_SEC / 2);
  78.                         GPIOA.DOUT().DOUT3 = 1;//LED2灭
  79.                 }
  80.         }
  81. }
 楼主| hotpower 发表于 2011-12-3 01:33 | 显示全部楼层
flag.cpp
  1. #include "Look_flag.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. flag_t Flag(0);//初始化

  5. // eint_t 类提供了 INT0/INT1 的接口
  6. // 当 INT0/INT1 发生时,对象将发送相应的 int 消息到 mbox。
  7. class eint_t : public interrupt_t {
  8. public:
  9.         __INLINE eint_t();

  10. protected:
  11.         bool isr(int vector);
  12.         void dsr(int vector, uintptr_t count);
  13. };

  14. // eint 构造函数
  15. __DEBUG//debug调试不优化便于调试
  16. __INLINE eint_t::eint_t()
  17. {
  18.         using namespace sfr::gpio;
  19.         attach(EINT0_IRQn);//绑定外部中断0
  20.         attach(EINT1_IRQn);//绑定外部中断1
  21.         GPIOB.IEN(0).IF_EN15(1).IF_EN14(1);//开启Key1,Key2中断
  22.         vector_t::enable(EINT0_IRQn);//使能外部中断0即Key1中断
  23.         vector_t::enable(EINT1_IRQn);//使能外部中断1即Key2中断
  24. }

  25. // eint 中断服务例程
  26. __DEBUG//debug调试不优化便于调试
  27. bool eint_t::isr(int vector)
  28. {
  29.         using namespace sfr::gpio;
  30.         GPIOB.ISRC = GPIOB.ISRC;                        // 清中断 flag
  31.         return true;
  32. }

  33. // eint 中断滞后服务例程
  34. __DEBUG//debug调试不优化便于调试
  35. void eint_t::dsr(int vector, uintptr_t count)
  36. {
  37.         using namespace sfr::gpio;
  38.         if (vector == EINT0_IRQn)//Key2中断
  39.         {
  40.                 Flag.do_set_bits(2);//在中断中唤醒任务2
  41.         }
  42.         else if (vector == EINT1_IRQn)//Key1中断
  43.         {
  44.                 Flag.do_set_bits(1);//在中断中唤醒任务1
  45.         }
  46. }

  47. eint_t eint;                                                                        // 创建 eint 对象

  48. instance_task1_Look_flag_t task1_Look_flag(1);        // 任务实例,优化级别1

  49. // 任务类 task1_Look_flag_t 的例程
  50. __DEBUG
  51. void task1_Look_flag_t::routine()
  52. {
  53.         // TODO: 在此编写 task1_Look_flag_t 例程的内容
  54.         using namespace sfr::gpio;
  55.         while (true) {
  56.                 // TODO: 在此编写 task1_Look_flag_t 例程的内容
  57.                 int flag = Flag.wait(1, flag_t::ANY_CONSUME);//阻塞等待Key1中断
  58.                 if (flag)
  59.                 {
  60.                         GPIOA.DOUT().DOUT2 ^= 1;//LED1闪烁
  61.                         Flag.set_bits(4);//在任务中唤醒任务3
  62.                 }
  63.         }
  64. }

  65. instance_task2_Look_flag_t task2_Look_flag(2);        // 任务实例,优化级别2

  66. // 任务类 task2_Look_flag_t 的例程
  67. __DEBUG//debug调试不优化便于调试
  68. void task2_Look_flag_t::routine()
  69. {
  70.         // TODO: 在此编写 task2_Look_flag_t 例程的内容
  71.         using namespace sfr::gpio;
  72.         while (true) {
  73.                 // TODO: 在此编写 task2_Look_flag_t 例程的内容
  74.                 int flag = Flag.wait(2, flag_t::ANY_CONSUME);//阻塞等待Key2中断
  75.                 if (flag)
  76.                 {
  77.                         GPIOA.DOUT().DOUT3 ^= 1;//LED2闪烁
  78.                         Flag.set_bits(4);//在任务中唤醒任务3
  79.                 }
  80.         }
  81. }

  82. instance_task3_Look_flag_t task3_Look_flag(0);        // 任务实例,优化级别0

  83. // 任务类 task2_Look_flag_t 的例程
  84. __DEBUG//debug调试不优化便于调试
  85. void task3_Look_flag_t::routine()
  86. {
  87.         // TODO: 在此编写 task2_Look_flag_t 例程的内容
  88.         using namespace sfr::gpio;
  89.         while (true) {
  90.                 // TODO: 在此编写 task3_Look_flag_t 例程的内容
  91.                 int flag = Flag.wait(4, flag_t::ANY_CONSUME);
  92.                 if (flag)
  93.                 {
  94.                         GPIOA.DOUT().DOUT4(0).DOUT5(0);//LED3,LED4亮
  95.                         delay(LOOK_TICKS_PER_SEC / 2);//延时亮0.5S
  96.                         GPIOA.DOUT().DOUT4(1).DOUT5(1);//LED3,LED4灭
  97.                 }
  98.         }
  99. }
 楼主| hotpower 发表于 2011-12-3 01:34 | 显示全部楼层
sem.cpp
  1. #include "Look_sem.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. sem_t Sem = 1;//Sem > 0 Sem.wait()任务可以直接获取信号量

  5. instance_task1_Look_sem_t task1_Look_sem(1);        // 任务实例

  6. // 任务类 task1_Look_sem_t 的例程
  7. void task1_Look_sem_t::routine()
  8. {
  9.         // TODO: 在此编写 task1_Look_sem_t 例程的内容
  10.         using namespace sfr::gpio;
  11.         while (true) {
  12.                 // TODO: 在此编写 task1_Look_sem_t 例程的内容
  13.                 if (Sem.wait())//等待获取信号量,Sem > 0直接获取,Sem = 0阻塞等待
  14.                 {//任务1独占信号量2S,此时Sem-- = Sem = 0,阻止任务2获取信号量
  15.                         GPIOA.DOUT().DOUT2(0).DOUT3(0);//LED1,LED2亮
  16.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  17.                         GPIOA.DOUT().DOUT2(1).DOUT3(1);//LED1,LED2灭
  18.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  19.                         Sem.post();//独占2S后释放信号量,任务2可以获取信号量,Sem++ = Sem = 1
  20.                 }
  21.         }
  22. }

  23. instance_task2_Look_sem_t task2_Look_sem(2);        // 任务实例

  24. // 任务类 task2_Look_sem_t 的例程
  25. void task2_Look_sem_t::routine()
  26. {
  27.         // TODO: 在此编写 task2_Look_sem_t 例程的内容
  28.         using namespace sfr::gpio;
  29.         while (true) {
  30.                 // TODO: 在此编写 task2_Look_sem_t 例程的内容
  31.                 if (Sem.wait())//等待获取信号量,Sem > 0直接获取,Sem = 0阻塞等待
  32.                 {//任务1独占信号量2S,此时Sem-- = Sem = 0,阻止任务2获取信号量
  33.                         GPIOA.DOUT().DOUT4(0).DOUT5(0);//LED3,LED4亮
  34.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  35.                         GPIOA.DOUT().DOUT4(1).DOUT5(1);//LED3,LED4灭
  36.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  37.                         Sem.post();//独占2S后释放信号量,任务1可以获取信号量,Sem++ = Sem = 1
  38.                 }
  39.         }
  40. }
 楼主| hotpower 发表于 2011-12-3 01:35 | 显示全部楼层
sem_key.cpp
  1. #include "Look_sem_key.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"
  4. sem_t Sem1 = 0;//Sem1.wait()任务1直接阻塞
  5. sem_t Sem2 = 0;//Sem2.wait()任务2直接阻塞

  6. // eint_t 类提供了 INT0/INT1 的接口
  7. // 当 INT0/INT1 发生时,对象将发送相应的 int 消息到 mbox。
  8. class eint_t : public interrupt_t {
  9. public:
  10.         __INLINE eint_t();

  11. protected:
  12.         bool isr(int vector);
  13.         void dsr(int vector, uintptr_t count);
  14. };

  15. // eint 构造函数
  16. __DEBUG//debug调试不优化便于调试
  17. __INLINE eint_t::eint_t()
  18. {
  19.         using namespace sfr::gpio;
  20.         attach(EINT0_IRQn);//绑定外部中断0
  21.         attach(EINT1_IRQn);//绑定外部中断1
  22.         GPIOB.IEN(0).IF_EN15(1).IF_EN14(1);//开启Key1,Key2中断
  23.         vector_t::enable(EINT0_IRQn);//使能外部中断0即Key1中断
  24.         vector_t::enable(EINT1_IRQn);//使能外部中断1即Key2中断
  25. }

  26. // eint 中断服务例程
  27. __DEBUG//debug调试不优化便于调试
  28. bool eint_t::isr(int vector)
  29. {
  30.         using namespace sfr::gpio;
  31.         GPIOB.ISRC = GPIOB.ISRC;//清中断flag
  32.         return true;
  33. }

  34. // eint 中断滞后服务例程
  35. __DEBUG//debug调试不优化便于调试
  36. void eint_t::dsr(int vector, uintptr_t count)
  37. {
  38.         using namespace sfr::gpio;
  39.         if (vector == EINT0_IRQn)//Key2中断
  40.         {//Key2按几次,LED3,LED4闪烁几次
  41.                 Sem2.do_post();//在中断中唤醒任务2,Sem2++>0
  42.         }
  43.         else if (vector == EINT1_IRQn)//Key1中断
  44.         {//Key1按几次,LED1,LED2闪烁几次
  45.                 Sem1.do_post();//在中断中唤醒任务1,Sem1++>0
  46.         }
  47. }

  48. eint_t eint;                                                                        // 创建 eint 对象

  49. instance_task1_Look_sem_key_t task1_Look_sem_key(1);        // 任务实例

  50. // 任务类 task1_Look_sem_key_t 的例程
  51. void task1_Look_sem_key_t::routine()
  52. {
  53.         // TODO: 在此编写 task1_Look_sem_key_t 例程的内容
  54.         using namespace sfr::gpio;
  55.         while (true) {
  56.                 // TODO: 在此编写 task1_Look_sem_key_t 例程的内容
  57.                 if (Sem1.wait())//Sem1=0阻塞等待获取信号量
  58.                 {//注意此时Key1释放信号量Sem1++>0
  59.                         GPIOA.DOUT().DOUT2(0).DOUT3(0);//LED1,LED2亮
  60.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  61.                         GPIOA.DOUT().DOUT2(1).DOUT3(1);//LED1,LED2灭
  62.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  63.                 }
  64.         }
  65. }


  66. instance_task2_Look_sem_key_t task2_Look_sem_key(2);        // 任务实例

  67. // 任务类 task2_Look_sem_key_t 的例程
  68. void task2_Look_sem_key_t::routine()
  69. {
  70.         // TODO: 在此编写 task2_Look_sem_key_t 例程的内容
  71.         using namespace sfr::gpio;
  72.         while (true) {
  73.                 // TODO: 在此编写 task2_Look_sem_key_t 例程的内容
  74.                 if (Sem2.wait())//Sem2=0阻塞等待获取信号量
  75.                 {//注意此时Key2释放信号量Sem2++>0
  76.                         GPIOA.DOUT().DOUT4(0).DOUT5(0);//LED3,LED4亮
  77.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  78.                         GPIOA.DOUT().DOUT4(1).DOUT5(1);//LED3,LED4灭
  79.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  80.                 }
  81.         }
  82. }
 楼主| hotpower 发表于 2011-12-3 01:36 | 显示全部楼层
mutex.cpp
  1. #include "Look_mutex.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. mutex_t Mutex;

  5. instance_task1_Look_mutex_t task1_Look_mutex(1);        // 任务实例

  6. // 任务类 task1_Look_mutex_t 的例程
  7. void task1_Look_mutex_t::routine()
  8. {
  9.         // TODO: 在此编写 task1_Look_mutex_t 例程的内容
  10.         using namespace sfr::gpio;
  11.         while (true) {
  12.                 // TODO: 在此编写 task1_Look_mutex_t 例程的内容
  13.                 if (Mutex.lock())
  14.                 {
  15.                         GPIOA.DOUT().DOUT2(0).DOUT3(0);//LED1,LED2亮
  16.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  17.                         GPIOA.DOUT().DOUT2(1).DOUT3(1);//LED1,LED2灭
  18.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  19.                         Mutex.unlock();
  20.                 }
  21.         }
  22. }

  23. instance_task2_Look_mutex_t task2_Look_mutex(2);        // 任务实例

  24. // 任务类 task1_Look_mutex_t 的例程
  25. void task2_Look_mutex_t::routine()
  26. {
  27.         // TODO: 在此编写 task2_Look_mutex_t 例程的内容
  28.         using namespace sfr::gpio;
  29.         while (true) {
  30.                 // TODO: 在此编写 task2_Look_mutex_t 例程的内容
  31.                 if (Mutex.lock())
  32.                 {
  33.                         GPIOA.DOUT().DOUT4(0).DOUT5(0);//LED3,LED4亮
  34.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  35.                         GPIOA.DOUT().DOUT4(1).DOUT5(1);//LED3,LED4灭
  36.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  37.                         Mutex.unlock();
  38.                 }
  39.         }
  40. }
 楼主| hotpower 发表于 2011-12-3 01:36 | 显示全部楼层
mutex.cpp
  1. #include "Look_mutex.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. mutex_t Mutex;

  5. instance_task1_Look_mutex_t task1_Look_mutex(1);        // 任务实例

  6. // 任务类 task1_Look_mutex_t 的例程
  7. void task1_Look_mutex_t::routine()
  8. {
  9.         // TODO: 在此编写 task1_Look_mutex_t 例程的内容
  10.         using namespace sfr::gpio;
  11.         while (true) {
  12.                 // TODO: 在此编写 task1_Look_mutex_t 例程的内容
  13.                 if (Mutex.lock())
  14.                 {
  15.                         GPIOA.DOUT().DOUT2(0).DOUT3(0);//LED1,LED2亮
  16.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  17.                         GPIOA.DOUT().DOUT2(1).DOUT3(1);//LED1,LED2灭
  18.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  19.                         Mutex.unlock();
  20.                 }
  21.         }
  22. }

  23. instance_task2_Look_mutex_t task2_Look_mutex(2);        // 任务实例

  24. // 任务类 task1_Look_mutex_t 的例程
  25. void task2_Look_mutex_t::routine()
  26. {
  27.         // TODO: 在此编写 task2_Look_mutex_t 例程的内容
  28.         using namespace sfr::gpio;
  29.         while (true) {
  30.                 // TODO: 在此编写 task2_Look_mutex_t 例程的内容
  31.                 if (Mutex.lock())
  32.                 {
  33.                         GPIOA.DOUT().DOUT4(0).DOUT5(0);//LED3,LED4亮
  34.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  35.                         GPIOA.DOUT().DOUT4(1).DOUT5(1);//LED3,LED4灭
  36.                         delay(LOOK_TICKS_PER_SEC);//延时亮1S
  37.                         Mutex.unlock();
  38.                 }
  39.         }
  40. }
 楼主| hotpower 发表于 2011-12-3 01:37 | 显示全部楼层
mbox.cpp
  1. #include "Look_mbox.h"
  2. #include "utils/debug.h"
  3. #include "numicro/sfr/gpio"

  4. mbox_t<int, 1> Mbox1;// 创建 int 型邮箱
  5. mbox_t<int, 1> Mbox2;// 创建 int 型邮箱
  6. mbox_t<int, 1> Mbox3;// 创建 int 型邮箱

  7. // eint_t 类提供了 INT0/INT1 的接口
  8. // 当 INT0/INT1 发生时,对象将发送相应的 int 消息到 mbox。
  9. class eint_t : public interrupt_t {
  10. public:
  11.         __INLINE eint_t();

  12. protected:
  13.         bool isr(int vector);
  14.         void dsr(int vector, uintptr_t count);
  15. };

  16. // eint 构造函数
  17. __DEBUG//debug调试不优化便于调试
  18. __INLINE eint_t::eint_t()
  19. {
  20.         using namespace sfr::gpio;
  21.         attach(EINT0_IRQn);//绑定外部中断0
  22.         attach(EINT1_IRQn);//绑定外部中断1
  23.         GPIOB.IEN(0).IF_EN15(1).IF_EN14(1);//开启Key1,Key2中断
  24.         vector_t::enable(EINT0_IRQn);//使能外部中断0即Key1中断
  25.         vector_t::enable(EINT1_IRQn);//使能外部中断1即Key2中断
  26. }

  27. // eint 中断服务例程
  28. __DEBUG//debug调试不优化便于调试
  29. bool eint_t::isr(int vector)
  30. {
  31.         using namespace sfr::gpio;
  32.         GPIOB.ISRC = GPIOB.ISRC;                        // 清中断 flag
  33.         return true;
  34. }

  35. // eint 中断滞后服务例程
  36. __DEBUG//debug调试不优化便于调试
  37. void eint_t::dsr(int vector, uintptr_t count)
  38. {
  39.         using namespace sfr::gpio;
  40.         if (vector == EINT0_IRQn)//Key2中断
  41.         {
  42.                 Mbox2.do_tryput(1);//在中断中唤醒任务2
  43.         }
  44.         else if (vector == EINT1_IRQn)//Key1中断
  45.         {
  46.                 Mbox1.do_tryput(0);//在中断中唤醒任务1
  47.         }
  48. }

  49. eint_t eint;                                                                        // 创建 eint 对象

  50. instance_task1_Look_mbox_t task1_Look_mbox(1);        // 任务实例

  51. // 任务类 task1_Look_mbox_t 的例程
  52. __DEBUG//debug调试不优化便于调试
  53. void task1_Look_mbox_t::routine()
  54. {
  55.         // TODO: 在此编写 task1_Look_mbox_t 例程的内容
  56.         using namespace sfr::gpio;
  57.         while (true) {
  58.                 // TODO: 在此编写 task1_Look_mbox_t 例程的内容
  59.                 int mbox;
  60.                 if (Mbox1.get(mbox))//阻塞等待Key1中断发送消息
  61.                 {
  62.                         if (mbox == 0)
  63.                         {
  64.                                 GPIOA.DOUT().DOUT2 ^= 1;//LED1闪烁
  65.                                 Mbox3.tryput(10);//在任务1中唤醒任务3,LED3闪烁
  66.                         }
  67.                 }
  68.         }
  69. }


  70. instance_task2_Look_mbox_t task2_Look_mbox(2);        // 任务实例

  71. // 任务类 task2_Look_mbox_t 的例程
  72. __DEBUG//debug调试不优化便于调试
  73. void task2_Look_mbox_t::routine()
  74. {
  75.         // TODO: 在此编写 task2_Look_mbox_t 例程的内容
  76.         using namespace sfr::gpio;
  77.         while (true) {
  78.                 // TODO: 在此编写 task2_Look_mbox_t 例程的内容
  79.                 int msg;
  80.                 if (Mbox2.get(msg))//阻塞等待Key1中断发送消息
  81.                 {
  82.                         if (msg == 1)
  83.                         {
  84.                                 GPIOA.DOUT().DOUT3 ^= 1;//LED2闪烁
  85.                                 Mbox3.tryput(11);//在任务2中唤醒任务3,LED4闪烁
  86.                         }
  87.                 }
  88.         }
  89. }

  90. instance_task3_Look_mbox_t task3_Look_mbox(0);        // 任务实例

  91. // 任务类 task2_Look_mbox_t 的例程
  92. __DEBUG//debug调试不优化便于调试
  93. void task3_Look_mbox_t::routine()
  94. {
  95.         // TODO: 在此编写 task3_Look_mbox_t 例程的内容
  96.         using namespace sfr::gpio;
  97.         while (true) {
  98.                 // TODO: 在此编写 task3_Look_mbox_t 例程的内容
  99.                 int msg;
  100.                 if (Mbox3.get(msg))//阻塞等待任务1或任务2发送消息
  101.                 {
  102.                         if (msg == 10)//Task1任务发送消息
  103.                         {
  104.                                 GPIOA.DOUT().DOUT4(0);//LED3亮
  105.                                 delay(LOOK_TICKS_PER_SEC / 2);//延时亮0.5S
  106.                                 GPIOA.DOUT().DOUT4(1);//LED3灭
  107.                         }
  108.                         else if (msg == 11)//Task2任务发送消息
  109.                         {
  110.                                 GPIOA.DOUT().DOUT5(0);//LED4亮
  111.                                 delay(LOOK_TICKS_PER_SEC / 2);//延时亮0.5S
  112.                                 GPIOA.DOUT().DOUT5(1);//LED4灭
  113.                         }
  114.                 }
  115.         }
  116. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 在线客服 返回列表 返回顶部
0