[LOOK] LOOK_Mutex半主机例程及源码

[复制链接]
2847|4
 楼主| hotpower 发表于 2011-8-6 21:59 | 显示全部楼层 |阅读模式
1.LOOK_Mutex.H
  1. #include "look_config.h"
  2. #include <look.h>

  3. // 任务类 task_Look_Mutex_Led1_t 的定义
  4. class task_Look_Mutex_Led1_t : public task_t {
  5. public:
  6. __INLINE__ task_Look_Mutex_Led1_t(uintptr_t prio); // 构造函数

  7. protected:
  8. void routine(); // 任务例程
  9. };

  10. // 任务类 task_Look_Mutex_Led1_t 的构造函数
  11. __INLINE__ task_Look_Mutex_Led1_t::task_Look_Mutex_Led1_t(uintptr_t prio)
  12. :task_t(prio)
  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. __INLINE__ task_Look_Mutex_Led2_t(uintptr_t prio); // 构造函数

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

  23. // 任务类 task_Look_Mutex_Led1_t 的构造函数
  24. __INLINE__ task_Look_Mutex_Led2_t::task_Look_Mutex_Led2_t(uintptr_t prio)
  25. :task_t(prio)
  26. {
  27. // TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
  28. }

  29. // 任务类 task_Look_Mutex_Beep_t 的定义
  30. class task_Look_Mutex_Beep_t : public task_t {
  31. public:
  32. __INLINE__ task_Look_Mutex_Beep_t(uintptr_t prio); // 构造函数

  33. protected:
  34. void routine(); // 任务例程
  35. };

  36. // 任务类 task_Look_Mutex_Led1_t 的构造函数
  37. __INLINE__ task_Look_Mutex_Beep_t::task_Look_Mutex_Beep_t(uintptr_t prio)
  38. :task_t(prio)
  39. {
  40. // TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
  41. }

  42. extern instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1;
  43. extern instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2;
  44. 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. #define LOOK_H 1
  3. #define LOOK_SEMIHOST

  4. #if LOOK_H == 0
  5. #include "NUC1xx.h"
  6. #include "NUC1xxM051Seriescfg.h"
  7. #else
  8. #include <nuc120re3an.h>
  9. using namespace nuvoton;
  10. #endif

  11. #ifdef LOOK_SEMIHOST
  12. #include <ostream>
  13. #include <semihosting>
  14. using namespace redapricot;
  15. ostream_t<semihosting::console_t, 128> cout;
  16. #endif

  17. mutex_t mutex;
  18. mbox_t<int> mbox(0);

  19. class Keyboard_t : public interrupt_t {
  20. public:
  21. __INLINE__ Keyboard_t();

  22. protected:
  23. bool isr(int vector);
  24. void dsr(int vector, uintptr_t count);
  25. };

  26. // Keyboard_t 构造函数
  27. __INLINE__ Keyboard_t::Keyboard_t()
  28. {
  29. #ifdef LOOK_SEMIHOST
  30. cout << "Keyboard_t构造函数开始" << endl;
  31. #endif
  32. attach(EINT0_IRQn);
  33. attach(EINT1_IRQn);
  34. #if LOOK_H == 0
  35. GPIOBs.IEN.Regs = (1 << Pin15) | (1 << Pin14); // 开启中断
  36. #else
  37. GPIOB.IEN(0)
  38. .IF_EN15(1)
  39. .IF_EN14(1); // 开启中断
  40. #endif
  41. vector_t::enable(EINT0_IRQn);
  42. vector_t::enable(EINT1_IRQn);
  43. #ifdef LOOK_SEMIHOST
  44. cout << "Keyboard_t构造函数结束" << endl;
  45. #endif
  46. }

  47. // Keyboard_t 中断服务例程
  48. bool Keyboard_t::isr(int vector)
  49. {
  50. #if LOOK_H == 0
  51. GPIOBs.ISRC.Regs = GPIOBs.ISRC.Regs; // 清中断 flag
  52. #else
  53. //注意:下句被优化了
  54. // GPIOB.ISRC = GPIOB.ISRC; // 清中断 flag

  55. //注意:下句不能被优化,特别注意()是建立缓存
  56. GPIOB.ISRC = GPIOB.ISRC(); // 清中断 flag
  57. #endif
  58. #ifdef LOOK_SEMIHOST
  59. cout << "中断Keyboard_t清除中断标志" << endl;
  60. #endif
  61. return true;
  62. }

  63. // Keyboard_t 中断滞后服务例程
  64. void Keyboard_t::dsr(int vector, uintptr_t count)
  65. {
  66. uint32_t key;
  67. #if LOOK_H == 0
  68. key = GPIOBs.PIN.Regs;
  69. #else
  70. key = GPIOB.PIN;
  71. #endif
  72. if ((vector == EINT0_IRQn) && ((key & (1 << 14)) == 0))//Key2
  73. {
  74. #ifdef LOOK_SEMIHOST
  75. cout << "中断Keyboard_t按下Key2键" << endl;
  76. #endif
  77. mbox.do_tryput(6);
  78. }
  79. else if ((vector == EINT1_IRQn) && ((key & (1 << 15)) == 0))//Key1
  80. {
  81. #ifdef LOOK_SEMIHOST
  82. cout << "中断Keyboard_t按下Key1键" << endl;
  83. #endif
  84. mbox.do_tryput(2);
  85. }
  86. else
  87. {
  88. #ifdef LOOK_SEMIHOST
  89. cout << "中断Keyboard_t键盘发生抖动!!!" << endl;
  90. #endif
  91. }
  92. }

  93. Keyboard_t Key; // 创建Key对象


  94. // 任务类 task_Look_Mutex_Led1_t 的例程
  95. void task_Look_Mutex_Led1_t::routine()
  96. {
  97. #ifdef LOOK_SEMIHOST
  98. cout << "任务task_Look_Mutex_Led1_t开始:" << endl;
  99. #endif
  100. // TODO: 在此编写 task_Look_Mutex_Led1_t 例程的内容
  101. while (true) {
  102. // TODO: 在此编写 task_Look_Mutex_Led1_t 例程的内容
  103. int Mutex = mutex.lock();
  104. if (Mutex)
  105. {
  106. #ifdef LOOK_SEMIHOST
  107. cout << "任务task_Look_Mutex_Led1_t保护开始" << endl;
  108. #endif
  109. delay(LOOK_TICKS_PER_SEC / 10);
  110. uint32_t data = ~0b100;
  111. #if LOOK_H == 0
  112. GPIOAs.DMASK.Regs = ~0b111100;
  113. #else
  114. GPIOA.DMASK(-1)
  115. .DMASK5(0)
  116. .DMASK4(0)
  117. .DMASK3(0)
  118. .DMASK2(0);
  119. #endif
  120. for (uint32_t i = 0; i < 18; i ++){
  121. data &= 0b111100;
  122. #if LOOK_H == 0
  123. GPIOAs.DOUT.Regs = data;
  124. #else
  125. GPIOA.DOUT = data;
  126. #endif
  127. data <<= 1;
  128. data += data >> 4;
  129. delay(LOOK_TICKS_PER_SEC / 10);
  130. }
  131. #if LOOK_H == 0
  132. GPIOAs.DOUT.Regs = 0b111100;
  133. #else
  134. GPIOA.DOUT(0)
  135. .DOUT5(1)
  136. .DOUT4(1)
  137. .DOUT3(1)
  138. .DOUT2(1);
  139. #endif
  140. mutex.unlock();
  141. #ifdef LOOK_SEMIHOST
  142. cout << "任务task_Look_Mutex_Led1_t保护结束" << endl;
  143. #endif
  144. }
  145. // delay(LOOK_TICKS_PER_SEC / 100);
  146. }
  147. }


  148. // 任务类 task_Look_Mutex_Led2_t 的例程
  149. void task_Look_Mutex_Led2_t::routine()
  150. {
  151. #ifdef LOOK_SEMIHOST
  152. cout << "任务task_Look_Mutex_Led2_t开始:" << endl;
  153. #endif
  154. // TODO: 在此编写 task_Look_Mutex_Led2_t 例程的内容
  155. while (true) {
  156. // TODO: 在此编写 task_Look_Mutex_Led2_t 例程的内容
  157. int Mutex = mutex.lock();
  158. if (Mutex)
  159. {

  160. #ifdef LOOK_SEMIHOST
  161. cout << "任务task_Look_Mutex_Led2_t保护开始" << endl;
  162. #endif
  163. delay(LOOK_TICKS_PER_SEC / 10);
  164. uint32_t data = ~0b100000;
  165. #if LOOK_H == 0
  166. GPIOAs.DMASK.Regs = ~0b111100;
  167. #else
  168. GPIOA.DMASK(-1)
  169. .DMASK5(0)
  170. .DMASK4(0)
  171. .DMASK3(0)
  172. .DMASK2(0);
  173. #endif
  174. for (uint32_t i = 0; i < 18; i ++){
  175. data &= 0b111100;
  176. #if LOOK_H == 0
  177. GPIOAs.DOUT.Regs = data;
  178. #else
  179. GPIOA.DOUT = data;
  180. #endif
  181. data >>= 1;
  182. data += data << 4;
  183. delay(LOOK_TICKS_PER_SEC / 10);
  184. }
  185. #if LOOK_H == 0
  186. GPIOAs.DOUT.Regs = 0b111100;
  187. #else
  188. GPIOA.DOUT(0)
  189. .DOUT5(1)
  190. .DOUT4(1)
  191. .DOUT3(1)
  192. .DOUT2(1);
  193. #endif
  194. mutex.unlock();
  195. #ifdef LOOK_SEMIHOST
  196. cout << "任务task_Look_Mutex_Led2_t保护结束" << endl;
  197. #endif
  198. }
  199. // delay(LOOK_TICKS_PER_SEC / 100);
  200. }
  201. }


  202. // 任务类 task_Look_Mutex_Beep_t 的例程
  203. void task_Look_Mutex_Beep_t::routine()
  204. {
  205. #ifdef LOOK_SEMIHOST
  206. cout << "任务task_Look_Mutex_Beep_t开始:" << endl;
  207. #endif
  208. // TODO: 在此编写 task_Look_Mutex_Beep_t 例程的内容
  209. while (true) {
  210. // TODO: 在此编写 task_Look_Mutex_Beep_t 例程的内容
  211. uint32_t msg = mbox.get();//取出响铃次数
  212. if (msg)
  213. {
  214. /*
  215. #ifdef LOOK_SEMIHOST
  216. cout << "任务task_Look_Mutex_Beep_t mbox=" << msg << endl;
  217. #endif
  218. */
  219. #ifdef LOOK_SEMIHOST
  220. if (msg == 2)
  221. {
  222. cout << "执行Key1键盘命令" << endl;
  223. }
  224. else if (msg == 2)
  225. {
  226. cout << "执行Key2键盘命令" << endl;
  227. }
  228. #endif
  229. #if LOOK_H == 0
  230. GPIOBs.DMASK.Bits.Pin10 = 0;
  231. #else
  232. GPIOB.DMASK().DMASK10(0);
  233. #endif
  234. for (uint32_t i = 0; i < msg; i ++){//蜂鸣器响X次
  235. #if LOOK_H == 0
  236. GPIOBs.DOUT.Bits.Pin10 ^= 1;//蜂鸣器响PB10=1,不响PB10=0
  237. #else
  238. GPIOB.DOUT().DOUT10 ^= 1;
  239. #endif
  240. delay(LOOK_TICKS_PER_SEC / 20);//断续间隔
  241. }
  242. #if LOOK_H == 0
  243. GPIOBs.DOUT.Bits.Pin10 = 0;
  244. #else
  245. GPIOB.DOUT().DOUT10(0);
  246. #endif
  247. delay(LOOK_TICKS_PER_SEC / 50);
  248. }
  249. }
  250. }

  251. #ifdef LOOK_SCHEDULING_PRIORITY
  252. instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1(0);
  253. instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2(0);
  254. instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep(0);
  255. #else
  256. instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1;
  257. instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2;
  258. instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep;
  259. #endif

3.贴图



4.LOOK_Mutex.RAR

本帖子中包含更多资源

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

×
lwslws201 发表于 2011-8-6 22:05 | 显示全部楼层
我先顶,日后学习。
老鱼探戈 发表于 2011-8-6 22:13 | 显示全部楼层
顶! 板凳坐着也很舒服滴~~  :D
wang0225 发表于 2011-8-7 12:09 | 显示全部楼层
给力啊,大叔!!!
plc_avr 发表于 2011-8-7 12:39 | 显示全部楼层
顶!有空时慢慢看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1460

主题

21617

帖子

508

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