[LOOK] LOOK菜鸟mutex应用例程

[复制链接]
 楼主| hotpower 发表于 2011-5-20 18:49 | 显示全部楼层 |阅读模式
本帖最后由 hotpower 于 2011-5-23 19:03 编辑

点击下载:Look_Mutex


1.look_mutex.h

  1. #include "look_config.h"
  2. #include <look.h>
  3. #include <instantiate>
  4. // 任务类 task_Look_Mutex_Led1_t 的定义
  5. class task_Look_Mutex_Led1_t : public task_t {
  6. public:
  7. task_Look_Mutex_Led1_t() __OPT_ATTR__; // 构造函数

  8. protected:
  9. void routine();  // 任务例程
  10. };
  11. // 任务类 task_Look_Mutex_Led1_t 的构造函数
  12. __OPT_INLINE__ task_Look_Mutex_Led1_t::task_Look_Mutex_Led1_t()
  13. {
  14. // TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
  15. }
  16. // 任务类 task_Look_Mutex_Led2_t 的定义
  17. class task_Look_Mutex_Led2_t : public task_t {
  18. public:
  19. task_Look_Mutex_Led2_t() __OPT_ATTR__; // 构造函数

  20. protected:
  21. void routine();  // 任务例程
  22. };

  23. // 任务类 task_Look_Mutex_Beep_t 的定义
  24. class task_Look_Mutex_Beep_t : public task_t {
  25. public:
  26. task_Look_Mutex_Beep_t() __OPT_ATTR__; // 构造函数

  27. protected:
  28. void routine();  // 任务例程
  29. };
  30. // 任务类 task_Look_Mutex_Led1_t 的构造函数
  31. __OPT_INLINE__ task_Look_Mutex_Beep_t::task_Look_Mutex_Beep_t()
  32. {
  33. // TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
  34. }
  35. extern instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1;
  36. extern instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2;
  37. extern instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep;
2.look_mutex.cpp

  1. #include "Look_Mutex.h"
  2. #include "NUC1xx.h"
  3. #include "NUC1xxM051Seriescfg.h"
  4. mutex_t mutex;
  5. mbox_t<int> mbox(0);
  6. class Keyboard_t : public interrupt_t {
  7. public:
  8. Keyboard_t() __OPT_ATTR__;
  9. protected:
  10. bool isr(int vector);
  11. void dsr(int vector, uintptr_t count);
  12. };         
  13. // Keyboard_t 构造函数
  14. inline Keyboard_t::Keyboard_t()
  15. {
  16. attach(EINT0_IRQn);
  17. attach(EINT1_IRQn);
  18. GPIOBs.IEN.Regs = (1 << Pin15) | (1 << Pin14);   // 开启中断
  19. vector_t::enable(EINT0_IRQn);
  20. vector_t::enable(EINT1_IRQn);
  21. }
  22. // Keyboard_t 中断服务例程
  23. bool Keyboard_t::isr(int vector)
  24. {
  25. GPIOBs.ISRC.Regs = GPIOBs.ISRC.Regs;   // 清中断 Mutex
  26. return true;
  27. }
  28. // Keyboard_t 中断滞后服务例程
  29. void Keyboard_t::dsr(int vector, uintptr_t count)
  30. {
  31. if (vector == EINT0_IRQn)//Key2
  32. {
  33.   mbox.do_tryput(6);
  34. }
  35. else if (vector == EINT1_IRQn)//Key1
  36. {
  37.   mbox.do_tryput(2);
  38. }
  39. }
  40. Keyboard_t Key;         // 创建Key对象

  41. // 任务类 task_Look_Mutex_Led1_t 的例程
  42. void task_Look_Mutex_Led1_t::routine()
  43. {
  44. // TODO: 在此编写 task_Look_Mutex_Led1_t 例程的内容
  45. while (true) {
  46.   // TODO: 在此编写 task_Look_Mutex_Led1_t 例程的内容
  47.   int Mutex = mutex.lock();
  48.   if (Mutex)
  49.   {
  50.    delay(LOOK_TICKS_PER_SEC / 10);
  51.       uint32_t data = ~0b100;
  52.    GPIOAs.DMASK.Regs = ~0b111100;
  53.    for (uint32_t i = 0; i < 18; i ++){
  54.     data &= 0b111100;
  55.       GPIOAs.DOUT.Regs = data;
  56.       data <<= 1;
  57.       data += data >> 4;
  58.             delay(LOOK_TICKS_PER_SEC / 10);
  59.    }
  60.    GPIOAs.DOUT.Regs = 0b111100;
  61.    mutex.unlock();
  62.   }
  63. //  delay(LOOK_TICKS_PER_SEC / 100);
  64. }
  65. }

  66. // 任务类 task_Look_Mutex_Led2_t 的例程
  67. void task_Look_Mutex_Led2_t::routine()
  68. {      
  69. // TODO: 在此编写 task_Look_Mutex_Led2_t 例程的内容
  70. while (true) {
  71.   // TODO: 在此编写 task_Look_Mutex_Led2_t 例程的内容
  72.   int Mutex = mutex.lock();
  73.   if (Mutex)
  74.   {
  75.    delay(LOOK_TICKS_PER_SEC / 10);
  76.       uint32_t data = ~0b100000;
  77.    GPIOAs.DMASK.Regs = ~0b111100;
  78.    for (uint32_t i = 0; i < 18; i ++){
  79.     data &= 0b111100;
  80.       GPIOAs.DOUT.Regs = data;
  81.       data >>= 1;
  82.       data += data << 4;
  83.             delay(LOOK_TICKS_PER_SEC / 10);
  84.    }
  85.    GPIOAs.DOUT.Regs = 0b111100;
  86.    mutex.unlock();
  87.   }
  88. //  delay(LOOK_TICKS_PER_SEC / 100);
  89. }
  90. }

  91. // 任务类 task_Look_Mutex_Beep_t 的例程
  92. void task_Look_Mutex_Beep_t::routine()
  93. {
  94. // TODO: 在此编写 task_Look_Mutex_Beep_t 例程的内容
  95. while (true) {
  96.   // TODO: 在此编写 task_Look_Mutex_Beep_t 例程的内容
  97.   uint32_t msg = mbox.get();//取出响铃次数
  98.   if (msg)
  99.   {
  100.    GPIOBs.DMASK.Bits.Pin10 = 0;
  101.    for (uint32_t i = 0; i < msg; i ++){//蜂鸣器响X次
  102.      GPIOBs.DOUT.Bits.Pin10 ^= 1;//蜂鸣器响PB10=1,不响PB10=0
  103.            delay(LOOK_TICKS_PER_SEC / 20);//断续间隔
  104.    }
  105.    GPIOBs.DOUT.Bits.Pin10 = 0;//蜂鸣器不响
  106.   }
  107. }
  108. }
  109. #ifdef LOOK_SCHEDULING_PRIORITY
  110. instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1(0);
  111. instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2(0);
  112. instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep(0);
  113. #else
  114. instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1;
  115. instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2;
  116. instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep;
  117. #endif

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
murex 发表于 2011-5-20 18:56 | 显示全部楼层
沙发
sunshitao 发表于 2011-5-20 18:58 | 显示全部楼层
板凳
wang4101 发表于 2011-5-20 19:19 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

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