main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #if !defined(SYSTEM_CLOCK)
  15. #define SYSTEM_CLOCK 8000000U
  16. #endif
  17. /*
  18. * @brief System Timer handler.
  19. */
  20. CH_IRQ_HANDLER(SysTick_Handler) {
  21. CH_IRQ_PROLOGUE();
  22. chSysLockFromISR();
  23. chSysTimerHandlerI();
  24. chSysUnlockFromISR();
  25. CH_IRQ_EPILOGUE();
  26. }
  27. static uint32_t seconds_counter;
  28. static uint32_t minutes_counter;
  29. /*
  30. * Seconds counter thread.
  31. */
  32. static THD_WORKING_AREA(waThread1, 128);
  33. static THD_FUNCTION(Thread1, arg) {
  34. (void)arg;
  35. while (true) {
  36. chThdSleepMilliseconds(1000);
  37. seconds_counter++;
  38. }
  39. }
  40. /*
  41. * Minutes counter thread.
  42. */
  43. static THD_WORKING_AREA(waThread2, 128);
  44. static THD_FUNCTION(Thread2, arg) {
  45. (void)arg;
  46. while (true) {
  47. chThdSleepSeconds(60);
  48. minutes_counter++;
  49. }
  50. }
  51. /*
  52. * Threads static table, one entry per thread. The number of entries must
  53. * match NIL_CFG_NUM_THREADS.
  54. */
  55. THD_TABLE_BEGIN
  56. THD_TABLE_ENTRY(waThread1, "counter1", Thread1, NULL)
  57. THD_TABLE_ENTRY(waThread2, "counter2", Thread2, NULL)
  58. THD_TABLE_END
  59. /*
  60. * Application entry point.
  61. */
  62. int main(void) {
  63. /*
  64. * Hardware initialization, in this simple demo just the systick timer is
  65. * initialized.
  66. */
  67. SysTick->LOAD = SYSTEM_CLOCK / CH_CFG_ST_FREQUENCY - (systime_t)1;
  68. SysTick->VAL = (uint32_t)0;
  69. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk |
  70. SysTick_CTRL_TICKINT_Msk;
  71. /*
  72. * System initializations.
  73. * - Kernel initialization, the main() function becomes a thread and the
  74. * RTOS is active.
  75. */
  76. chSysInit();
  77. /* This is now the idle thread loop, you may perform here a low priority
  78. task but you must never try to sleep or wait in this loop. Note that
  79. this tasks runs at the lowest priority level so any instruction added
  80. here will be executed after all other tasks have been started.*/
  81. while (true) {
  82. }
  83. }