timer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*- Includes ----------------------------------------------------------------*/
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <stdbool.h>
  32. #include "same70.h"
  33. #include "timer.h"
  34. /*- Variables ---------------------------------------------------------------*/
  35. static Timer *g_timer_list[TIMER_COUNT];
  36. /*- Implementations ---------------------------------------------------------*/
  37. //-----------------------------------------------------------------------------
  38. static inline void systick_start(void)
  39. {
  40. SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk;
  41. }
  42. //-----------------------------------------------------------------------------
  43. static inline void systick_stop(void)
  44. {
  45. SysTick->CTRL = 0;
  46. }
  47. //-----------------------------------------------------------------------------
  48. void timer_init(void)
  49. {
  50. SysTick->VAL = 0;
  51. SysTick->LOAD = (F_CPU / 1000) * TIMER_TICK;
  52. for (int i = 0; i < TIMER_COUNT; i++)
  53. g_timer_list[i] = NULL;
  54. systick_start();
  55. }
  56. //-----------------------------------------------------------------------------
  57. void timer_start(Timer *timer)
  58. {
  59. timer->expired = false;
  60. timer->interval = (timer->interval < TIMER_TICK) ? TIMER_TICK : timer->interval;
  61. timer->timeout = timer->interval / TIMER_TICK;
  62. systick_stop();
  63. for (int i = 0; i < TIMER_COUNT; i++)
  64. {
  65. if (g_timer_list[i] == NULL)
  66. {
  67. g_timer_list[i] = timer;
  68. break;
  69. }
  70. }
  71. systick_start();
  72. }
  73. //-----------------------------------------------------------------------------
  74. void timer_stop(Timer *timer)
  75. {
  76. systick_stop();
  77. for (int i = 0; i < TIMER_COUNT; i++)
  78. {
  79. if (g_timer_list[i] == timer)
  80. {
  81. g_timer_list[i] = NULL;
  82. break;
  83. }
  84. }
  85. systick_start();
  86. }
  87. //-----------------------------------------------------------------------------
  88. void irq_handler_sys_tick(void)
  89. {
  90. for (int i = 0; i < TIMER_COUNT; i++)
  91. {
  92. Timer *timer = g_timer_list[i];
  93. if (timer == NULL)
  94. continue;
  95. if (--timer->timeout > 0)
  96. continue;
  97. timer->expired = true;
  98. if (timer->repeat)
  99. timer->timeout = timer->interval / TIMER_TICK;
  100. else
  101. g_timer_list[i] = NULL;
  102. }
  103. }