main.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.hpp"
  14. #include "hal.h"
  15. #include "rt_test_root.h"
  16. #include "oslib_test_root.h"
  17. using namespace chibios_rt;
  18. /*
  19. * Message server thread class. It receives messages and does nothing except
  20. * reply after the specified time.
  21. */
  22. class MessageServerThread : public BaseStaticThread<256> {
  23. protected:
  24. void main(void) override {
  25. setName("server");
  26. while (true) {
  27. ThreadReference sender = waitMessage();
  28. time_msecs_t msecs = (time_msecs_t)sender.getMessage();
  29. sleep(TIME_MS2I(msecs));
  30. sender.releaseMessage(0);
  31. }
  32. }
  33. public:
  34. MessageServerThread(void) : BaseStaticThread<256>() {
  35. }
  36. };
  37. /* Reference to the server thread.*/
  38. static ThreadReference sref;
  39. /*
  40. * LED blink sequences.
  41. * NOTE: Sequences must always be terminated by a GOTO instruction.
  42. * NOTE: The sequencer language could be easily improved but this is outside
  43. * the scope of this demo.
  44. */
  45. #define SLEEP 0
  46. #define GOTO 1
  47. #define STOP 2
  48. #define BITCLEAR 3
  49. #define BITSET 4
  50. #define MESSAGE 5
  51. typedef struct {
  52. uint8_t action;
  53. union {
  54. msg_t msg;
  55. uint32_t value;
  56. ioline_t line;
  57. };
  58. } seqop_t;
  59. // Flashing sequence for LED3.
  60. static const seqop_t LED3_sequence[] =
  61. {
  62. {BITSET, LINE_LED3},
  63. {SLEEP, 800},
  64. {BITCLEAR, LINE_LED3},
  65. {SLEEP, 200},
  66. {GOTO, 0}
  67. };
  68. // Flashing sequence for LED4.
  69. static const seqop_t LED4_sequence[] =
  70. {
  71. {BITSET, LINE_LED4},
  72. {SLEEP, 600},
  73. {BITCLEAR, LINE_LED4},
  74. {SLEEP, 400},
  75. {GOTO, 0}
  76. };
  77. // Flashing sequence for LED5.
  78. static const seqop_t LED5_sequence[] =
  79. {
  80. {BITSET, LINE_LED5},
  81. {SLEEP, 400},
  82. {BITCLEAR, LINE_LED5},
  83. {SLEEP, 600},
  84. {GOTO, 0}
  85. };
  86. // Flashing sequence for LED6.
  87. static const seqop_t LED6_sequence[] =
  88. {
  89. {BITSET, LINE_LED6},
  90. {SLEEP, 200},
  91. {BITCLEAR, LINE_LED6},
  92. {SLEEP, 800},
  93. {GOTO, 0}
  94. };
  95. // Message sequence.
  96. static const seqop_t msg_sequence[] =
  97. {
  98. {MESSAGE, 50},
  99. {SLEEP, 1000},
  100. {GOTO, 0}
  101. };
  102. /*
  103. * Sequencer thread class. It can drive LEDs or other output pins.
  104. * Any sequencer is just an instance of this class, all the details are
  105. * totally encapsulated and hidden to the application level.
  106. */
  107. class SequencerThread : public BaseStaticThread<128> {
  108. private:
  109. const seqop_t *base, *curr; // Thread local variables.
  110. protected:
  111. void main(void) override {
  112. setName("sequencer");
  113. while (true) {
  114. switch(curr->action) {
  115. case SLEEP:
  116. sleep(TIME_MS2I(curr->value));
  117. break;
  118. case GOTO:
  119. curr = &base[curr->value];
  120. continue;
  121. case STOP:
  122. return;
  123. case BITCLEAR:
  124. palClearLine(curr->line);
  125. break;
  126. case BITSET:
  127. palSetLine(curr->line);
  128. break;
  129. case MESSAGE:
  130. sref.sendMessage(curr->msg);
  131. break;
  132. }
  133. curr++;
  134. }
  135. }
  136. public:
  137. SequencerThread(const seqop_t *sequence) : BaseStaticThread<128>() {
  138. base = curr = sequence;
  139. }
  140. };
  141. /*
  142. * Tester thread class. This thread executes the test suite.
  143. */
  144. class TesterThread : public BaseStaticThread<256> {
  145. protected:
  146. void main(void) override {
  147. setName("tester");
  148. test_execute((BaseSequentialStream *)&SD2, &rt_test_suite);
  149. test_execute((BaseSequentialStream *)&SD2, &oslib_test_suite);
  150. exit(test_global_fail);
  151. }
  152. public:
  153. TesterThread(void) : BaseStaticThread<256>() {
  154. }
  155. };
  156. /* Static threads instances.*/
  157. static TesterThread tester;
  158. static MessageServerThread server_thread;
  159. static SequencerThread blinker1(LED3_sequence);
  160. static SequencerThread blinker2(LED4_sequence);
  161. static SequencerThread blinker3(LED5_sequence);
  162. static SequencerThread blinker4(LED6_sequence);
  163. static SequencerThread sender1(msg_sequence);
  164. /*
  165. * Application entry point.
  166. */
  167. int main(void) {
  168. /*
  169. * System initializations.
  170. * - HAL initialization, this also initializes the configured device drivers
  171. * and performs the board-specific initializations.
  172. * - Kernel initialization, the main() function becomes a thread and the
  173. * RTOS is active.
  174. */
  175. halInit();
  176. System::init();
  177. /*
  178. * Activates the serial driver 2 using the driver default configuration.
  179. * PA2(TX) and PA3(RX) are routed to USART2.
  180. */
  181. sdStart(&SD2, NULL);
  182. palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
  183. palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
  184. /*
  185. * Starting the message server thread, storing the returned reference.
  186. */
  187. sref = server_thread.start(NORMALPRIO + 20);
  188. /*
  189. * Starts several instances of the SequencerThread class, each one operating
  190. * on a different sequence.
  191. */
  192. blinker1.start(NORMALPRIO + 10);
  193. blinker2.start(NORMALPRIO + 10);
  194. blinker3.start(NORMALPRIO + 10);
  195. blinker4.start(NORMALPRIO + 10);
  196. sender1.start(NORMALPRIO + 10);
  197. /*
  198. * Serves timer events.
  199. */
  200. while (true) {
  201. if (palReadPad(GPIOA, GPIOA_BUTTON)) {
  202. ThreadReference tref = tester.start(NORMALPRIO);
  203. tref.wait();
  204. };
  205. BaseThread::sleep(TIME_MS2I(500));
  206. }
  207. return 0;
  208. }