systick.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  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/runtime.h"
  27. #include "py/mphal.h"
  28. #include "irq.h"
  29. #include "systick.h"
  30. #include "pybthread.h"
  31. extern __IO uint32_t uwTick;
  32. // We provide our own version of HAL_Delay that calls __WFI while waiting,
  33. // and works when interrupts are disabled. This function is intended to be
  34. // used only by the ST HAL functions.
  35. void HAL_Delay(uint32_t Delay) {
  36. if (query_irq() == IRQ_STATE_ENABLED) {
  37. // IRQs enabled, so can use systick counter to do the delay
  38. uint32_t start = uwTick;
  39. // Wraparound of tick is taken care of by 2's complement arithmetic.
  40. while (uwTick - start < Delay) {
  41. // Enter sleep mode, waiting for (at least) the SysTick interrupt.
  42. __WFI();
  43. }
  44. } else {
  45. // IRQs disabled, use mp_hal_delay_ms routine.
  46. mp_hal_delay_ms(Delay);
  47. }
  48. }
  49. // Core delay function that does an efficient sleep and may switch thread context.
  50. // If IRQs are enabled then we must have the GIL.
  51. void mp_hal_delay_ms(mp_uint_t Delay) {
  52. if (query_irq() == IRQ_STATE_ENABLED) {
  53. // IRQs enabled, so can use systick counter to do the delay
  54. uint32_t start = uwTick;
  55. // Wraparound of tick is taken care of by 2's complement arithmetic.
  56. while (uwTick - start < Delay) {
  57. // This macro will execute the necessary idle behaviour. It may
  58. // raise an exception, switch threads or enter sleep mode (waiting for
  59. // (at least) the SysTick interrupt).
  60. MICROPY_EVENT_POLL_HOOK
  61. }
  62. } else {
  63. // IRQs disabled, so need to use a busy loop for the delay.
  64. // To prevent possible overflow of the counter we use a double loop.
  65. const uint32_t count_1ms = HAL_RCC_GetSysClockFreq() / 4000;
  66. for (int i = 0; i < Delay; i++) {
  67. for (uint32_t count = 0; ++count <= count_1ms;) {
  68. }
  69. }
  70. }
  71. }
  72. // delay for given number of microseconds
  73. void mp_hal_delay_us(mp_uint_t usec) {
  74. if (query_irq() == IRQ_STATE_ENABLED) {
  75. // IRQs enabled, so can use systick counter to do the delay
  76. uint32_t start = mp_hal_ticks_us();
  77. while (mp_hal_ticks_us() - start < usec) {
  78. }
  79. } else {
  80. // IRQs disabled, so need to use a busy loop for the delay
  81. // sys freq is always a multiple of 2MHz, so division here won't lose precision
  82. const uint32_t ucount = HAL_RCC_GetSysClockFreq() / 2000000 * usec / 2;
  83. for (uint32_t count = 0; ++count <= ucount;) {
  84. }
  85. }
  86. }
  87. bool sys_tick_has_passed(uint32_t start_tick, uint32_t delay_ms) {
  88. return HAL_GetTick() - start_tick >= delay_ms;
  89. }
  90. // waits until at least delay_ms milliseconds have passed from the sampling of
  91. // startTick. Handles overflow properly. Assumes stc was taken from
  92. // HAL_GetTick() some time before calling this function.
  93. void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
  94. while (!sys_tick_has_passed(start_tick, delay_ms)) {
  95. __WFI(); // enter sleep mode, waiting for interrupt
  96. }
  97. }
  98. mp_uint_t mp_hal_ticks_ms(void) {
  99. return uwTick;
  100. }
  101. // The SysTick timer counts down at 168 MHz, so we can use that knowledge
  102. // to grab a microsecond counter.
  103. //
  104. // We assume that HAL_GetTickis returns milliseconds.
  105. mp_uint_t mp_hal_ticks_us(void) {
  106. mp_uint_t irq_state = disable_irq();
  107. uint32_t counter = SysTick->VAL;
  108. uint32_t milliseconds = HAL_GetTick();
  109. uint32_t status = SysTick->CTRL;
  110. enable_irq(irq_state);
  111. // It's still possible for the countflag bit to get set if the counter was
  112. // reloaded between reading VAL and reading CTRL. With interrupts disabled
  113. // it definitely takes less than 50 HCLK cycles between reading VAL and
  114. // reading CTRL, so the test (counter > 50) is to cover the case where VAL
  115. // is +ve and very close to zero, and the COUNTFLAG bit is also set.
  116. if ((status & SysTick_CTRL_COUNTFLAG_Msk) && counter > 50) {
  117. // This means that the HW reloaded VAL between the time we read VAL and the
  118. // time we read CTRL, which implies that there is an interrupt pending
  119. // to increment the tick counter.
  120. milliseconds++;
  121. }
  122. uint32_t load = SysTick->LOAD;
  123. counter = load - counter; // Convert from decrementing to incrementing
  124. // ((load + 1) / 1000) is the number of counts per microsecond.
  125. //
  126. // counter / ((load + 1) / 1000) scales from the systick clock to microseconds
  127. // and is the same thing as (counter * 1000) / (load + 1)
  128. return milliseconds * 1000 + (counter * 1000) / (load + 1);
  129. }