main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.h"
  14. #include "hal.h"
  15. #include "portab.h"
  16. /*===========================================================================*/
  17. /* Generic code. */
  18. /*===========================================================================*/
  19. #if defined(PORTAB_LINE_LED2)
  20. /*
  21. * LED blinker thread, times are in milliseconds.
  22. */
  23. static THD_WORKING_AREA(waThread1, 128);
  24. static THD_FUNCTION(Thread1, arg) {
  25. (void)arg;
  26. chRegSetThreadName("blinker");
  27. while (true) {
  28. systime_t time = palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED ? 250 : 500;
  29. palToggleLine(PORTAB_LINE_LED2);
  30. chThdSleepMilliseconds(time);
  31. }
  32. }
  33. #endif
  34. #if PAL_USE_WAIT || defined(__DOXYGEN__)
  35. /*
  36. * Application entry point.
  37. */
  38. int main(void) {
  39. /*
  40. * System initializations.
  41. * - HAL initialization, this also initializes the configured device drivers
  42. * and performs the board-specific initializations.
  43. * - Kernel initialization, the main() function becomes a thread and the
  44. * RTOS is active.
  45. */
  46. halInit();
  47. chSysInit();
  48. #if defined(PORTAB_LINE_LED2)
  49. /*
  50. * Creates the blinker thread.
  51. */
  52. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  53. #endif
  54. /* Enabling events on both edges of the button line.*/
  55. palEnableLineEvent(PORTAB_LINE_BUTTON, PAL_EVENT_MODE_BOTH_EDGES);
  56. /*
  57. * Normal main() thread activity.
  58. */
  59. while (true) {
  60. /* Waiting for an edge on the button.*/
  61. palWaitLineTimeout(PORTAB_LINE_BUTTON, TIME_INFINITE);
  62. /* Action depending on button state.*/
  63. if (palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED) {
  64. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_ON);
  65. }
  66. else {
  67. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_OFF);
  68. }
  69. }
  70. }
  71. #endif /* PAL_USE_WAIT */
  72. #if !PAL_USE_WAIT && PAL_USE_CALLBACKS
  73. static event_source_t button_pressed_event;
  74. static event_source_t button_released_event;
  75. static void button_cb(void *arg) {
  76. (void)arg;
  77. chSysLockFromISR();
  78. if (palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED) {
  79. chEvtBroadcastI(&button_pressed_event);
  80. }
  81. else {
  82. chEvtBroadcastI(&button_released_event);
  83. }
  84. chSysUnlockFromISR();
  85. }
  86. /*
  87. * Application entry point.
  88. */
  89. int main(void) {
  90. event_listener_t el0, el1;
  91. /*
  92. * System initializations.
  93. * - HAL initialization, this also initializes the configured device drivers
  94. * and performs the board-specific initializations.
  95. * - Kernel initialization, the main() function becomes a thread and the
  96. * RTOS is active.
  97. */
  98. halInit();
  99. chSysInit();
  100. /* Events initialization and registration.*/
  101. chEvtObjectInit(&button_pressed_event);
  102. chEvtObjectInit(&button_released_event);
  103. chEvtRegister(&button_pressed_event, &el0, 0);
  104. chEvtRegister(&button_released_event, &el1, 1);
  105. #if defined(PORTAB_LINE_LED2)
  106. /*
  107. * Creates the blinker thread.
  108. */
  109. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  110. #endif
  111. /* Enabling events on both edges of the button line.*/
  112. palEnableLineEvent(PORTAB_LINE_BUTTON, PAL_EVENT_MODE_BOTH_EDGES);
  113. palSetLineCallback(PORTAB_LINE_BUTTON, button_cb, NULL);
  114. /*
  115. * Normal main() thread activity.
  116. */
  117. while (true) {
  118. eventmask_t events;
  119. events = chEvtWaitOne(EVENT_MASK(0) | EVENT_MASK(1));
  120. if (events & EVENT_MASK(0)) {
  121. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_ON);
  122. }
  123. if (events & EVENT_MASK(1)) {
  124. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_OFF);
  125. }
  126. }
  127. }
  128. #endif /* !PAL_USE_WAIT && PAL_USE_CALLBACKS */
  129. #if !PAL_USE_WAIT && !PAL_USE_CALLBACKS
  130. /*
  131. * Application entry point.
  132. */
  133. int main(void) {
  134. /*
  135. * System initializations.
  136. * - HAL initialization, this also initializes the configured device drivers
  137. * and performs the board-specific initializations.
  138. * - Kernel initialization, the main() function becomes a thread and the
  139. * RTOS is active.
  140. */
  141. halInit();
  142. chSysInit();
  143. #if defined(PORTAB_LINE_LED2)
  144. /*
  145. * Creates the blinker thread.
  146. */
  147. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  148. #endif
  149. /*
  150. * Normal main() thread activity.
  151. */
  152. while (true) {
  153. palToggleLine(PORTAB_LINE_LED1);
  154. chThdSleepMilliseconds(500);
  155. }
  156. }
  157. #endif /* !PAL_USE_WAIT && !PAL_USE_CALLBACKS */