main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. chRegSetThreadName("counter");
  36. while (true) {
  37. chThdSleepMilliseconds(1000);
  38. seconds_counter++;
  39. }
  40. }
  41. /*
  42. * Application entry point.
  43. */
  44. int main(void) {
  45. /*
  46. * Hardware initialization, in this simple demo just the systick timer is
  47. * initialized.
  48. */
  49. SysTick->LOAD = SYSTEM_CLOCK / CH_CFG_ST_FREQUENCY - (systime_t)1;
  50. SysTick->VAL = (uint32_t)0;
  51. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk |
  52. SysTick_CTRL_TICKINT_Msk;
  53. /* IRQ enabled.*/
  54. NVIC_SetPriority(SysTick_IRQn, 1);
  55. /*
  56. * System initializations.
  57. * - Kernel initialization, the main() function becomes a thread and the
  58. * RTOS is active.
  59. */
  60. chSysInit();
  61. /*
  62. * Creates the example thread.
  63. */
  64. (void) chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  65. /*
  66. * Normal main() thread activity, in this demo it does nothing except
  67. * increasing the minutes counter.
  68. */
  69. while (true) {
  70. chThdSleepSeconds(60);
  71. minutes_counter++;
  72. }
  73. }