ticker.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Mark Shannon
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mphal.h"
  27. #if MICROPY_PY_MACHINE_SOFT_PWM
  28. #include "ticker.h"
  29. #include "nrfx_glue.h"
  30. #define FastTicker NRF_TIMER1
  31. #define FastTicker_IRQn TIMER1_IRQn
  32. #define FastTicker_IRQHandler TIMER1_IRQHandler
  33. #define SlowTicker_IRQn SWI0_IRQn
  34. #define SlowTicker_IRQHandler SWI0_IRQHandler
  35. // Ticker callback function called every MACRO_TICK
  36. static volatile uint8_t m_num_of_slow_tickers = 0;
  37. static volatile callback_ptr m_slow_tickers[2] = {NULL, NULL};
  38. void ticker_init0(void) {
  39. NRF_TIMER_Type *ticker = FastTicker;
  40. #ifdef NRF51
  41. ticker->POWER = 1;
  42. #endif
  43. __NOP();
  44. ticker_stop();
  45. ticker->TASKS_CLEAR = 1;
  46. ticker->CC[3] = MICROSECONDS_PER_MACRO_TICK;
  47. ticker->MODE = TIMER_MODE_MODE_Timer;
  48. ticker->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
  49. ticker->PRESCALER = 4; // 1 tick == 1 microsecond
  50. ticker->INTENSET = TIMER_INTENSET_COMPARE3_Msk;
  51. ticker->SHORTS = 0;
  52. #ifdef NRF51
  53. NRFX_IRQ_PRIORITY_SET(FastTicker_IRQn, 1);
  54. #else
  55. NRFX_IRQ_PRIORITY_SET(FastTicker_IRQn, 2);
  56. #endif
  57. NRFX_IRQ_PRIORITY_SET(SlowTicker_IRQn, 3);
  58. NRFX_IRQ_PRIORITY_SET(SlowTicker_IRQn, 3);
  59. NRFX_IRQ_ENABLE(SlowTicker_IRQn);
  60. }
  61. void ticker_register_low_pri_callback(callback_ptr slow_ticker_callback) {
  62. m_slow_tickers[m_num_of_slow_tickers++] = slow_ticker_callback;
  63. }
  64. /* Start and stop timer 1 including workarounds for Anomaly 73 for Timer
  65. * http://www.nordicsemi.com/eng/content/download/29490/494569/file/nRF51822-PAN%20v3.0.pdf
  66. */
  67. void ticker_start(void) {
  68. NRFX_IRQ_ENABLE(FastTicker_IRQn);
  69. #ifdef NRF51
  70. *(uint32_t *)0x40009C0C = 1; // for Timer 1
  71. #endif
  72. FastTicker->TASKS_START = 1;
  73. }
  74. void ticker_stop(void) {
  75. NRFX_IRQ_DISABLE(FastTicker_IRQn);
  76. FastTicker->TASKS_STOP = 1;
  77. #ifdef NRF51
  78. *(uint32_t *)0x40009C0C = 0; // for Timer 1
  79. #endif
  80. }
  81. int32_t noop(void) {
  82. return -1;
  83. }
  84. volatile uint32_t ticks;
  85. static ticker_callback_ptr callbacks[3] = { noop, noop, noop };
  86. void FastTicker_IRQHandler(void) {
  87. NRF_TIMER_Type *ticker = FastTicker;
  88. ticker_callback_ptr *call = callbacks;
  89. if (ticker->EVENTS_COMPARE[0]) {
  90. ticker->EVENTS_COMPARE[0] = 0;
  91. ticker->CC[0] += call[0]()*MICROSECONDS_PER_TICK;
  92. }
  93. if (ticker->EVENTS_COMPARE[1]) {
  94. ticker->EVENTS_COMPARE[1] = 0;
  95. ticker->CC[1] += call[1]()*MICROSECONDS_PER_TICK;
  96. }
  97. if (ticker->EVENTS_COMPARE[2]) {
  98. ticker->EVENTS_COMPARE[2] = 0;
  99. ticker->CC[2] += call[2]()*MICROSECONDS_PER_TICK;
  100. }
  101. if (ticker->EVENTS_COMPARE[3]) {
  102. ticker->EVENTS_COMPARE[3] = 0;
  103. ticker->CC[3] += MICROSECONDS_PER_MACRO_TICK;
  104. ticks += MILLISECONDS_PER_MACRO_TICK;
  105. NRFX_IRQ_PENDING_SET(SlowTicker_IRQn);
  106. }
  107. }
  108. static const uint32_t masks[3] = {
  109. TIMER_INTENCLR_COMPARE0_Msk,
  110. TIMER_INTENCLR_COMPARE1_Msk,
  111. TIMER_INTENCLR_COMPARE2_Msk,
  112. };
  113. int set_ticker_callback(uint32_t index, ticker_callback_ptr func, int32_t initial_delay_us) {
  114. if (index > 3)
  115. return -1;
  116. NRF_TIMER_Type *ticker = FastTicker;
  117. callbacks[index] = noop;
  118. ticker->INTENCLR = masks[index];
  119. ticker->TASKS_CAPTURE[index] = 1;
  120. uint32_t t = FastTicker->CC[index];
  121. // Need to make sure that set tick is aligned to lastest tick
  122. // Use CC[3] as a reference, as that is always up-to-date.
  123. int32_t cc3 = FastTicker->CC[3];
  124. int32_t delta = t+initial_delay_us-cc3;
  125. delta = (delta/MICROSECONDS_PER_TICK+1)*MICROSECONDS_PER_TICK;
  126. callbacks[index] = func;
  127. ticker->INTENSET = masks[index];
  128. FastTicker->CC[index] = cc3 + delta;
  129. return 0;
  130. }
  131. int clear_ticker_callback(uint32_t index) {
  132. if (index > 3)
  133. return -1;
  134. FastTicker->INTENCLR = masks[index];
  135. callbacks[index] = noop;
  136. return 0;
  137. }
  138. void SlowTicker_IRQHandler(void)
  139. {
  140. for (int i = 0; i < m_num_of_slow_tickers; i++) {
  141. if (m_slow_tickers[i] != NULL) {
  142. m_slow_tickers[i]();
  143. }
  144. }
  145. }
  146. #endif // MICROPY_PY_MACHINE_SOFT_PWM