mxc_delay.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* *****************************************************************************
  2. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Maxim Integrated
  23. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  24. * Products, Inc. Branding Policy.
  25. *
  26. * The mere transfer of this software does not imply any licenses
  27. * of trade secrets, proprietary technology, copyrights, patents,
  28. * trademarks, maskwork rights, or any other form of intellectual
  29. * property whatsoever. Maxim Integrated Products, Inc. retains all
  30. * ownership rights.
  31. *
  32. * $Id: mxc_delay.c 36202 2018-07-16 21:06:02Z michael.bayern $
  33. *
  34. *************************************************************************** */
  35. /* **** Includes **** */
  36. #include <stdint.h>
  37. #include "mxc_config.h"
  38. #include "mxc_delay.h"
  39. /* **** File Scope Variables **** */
  40. static uint32_t ctrl_save;
  41. static volatile uint64_t compare_value = 0;
  42. static volatile uint64_t curr_value;
  43. static volatile uint32_t reload;
  44. static void mxc_delay_init(unsigned long us);
  45. extern void SysTick_Handler(void);
  46. /* ************************************************************************** */
  47. __weak void SysTick_Handler(void)
  48. {
  49. mxc_delay_handler();
  50. }
  51. /* ************************************************************************** */
  52. void mxc_delay_handler(void)
  53. {
  54. // Check and clear overflow flag
  55. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
  56. // Is a delay in progress?
  57. if(compare_value != 0) {
  58. curr_value += reload;
  59. if(curr_value >= compare_value) {
  60. mxc_delay_stop();
  61. }
  62. }
  63. }
  64. }
  65. /* ************************************************************************** */
  66. static void mxc_delay_init(unsigned long us)
  67. {
  68. uint32_t starttick, ticks;
  69. // Record the current tick value and clear the overflow flag
  70. starttick = SysTick->VAL;
  71. // Save the state of control register (and clear the overflow flag)
  72. ctrl_save = SysTick->CTRL & ~SysTick_CTRL_COUNTFLAG_Msk;
  73. // If the SysTick is not running, configure and start it
  74. if (!(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk)) {
  75. SysTick->LOAD = SysTick_LOAD_RELOAD_Msk;
  76. SysTick->VAL = SysTick_VAL_CURRENT_Msk;
  77. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;
  78. starttick = SysTick_VAL_CURRENT_Msk;
  79. reload = SysTick_LOAD_RELOAD_Msk + 1;
  80. } else {
  81. reload = SysTick->LOAD + 1; // get the current reload value
  82. }
  83. // Calculate the total number of ticks to delay
  84. ticks = (uint32_t)(((uint64_t)us * (uint64_t)SystemCoreClock) / 1000000);
  85. compare_value = ticks + (reload - starttick);
  86. curr_value = 0;
  87. if (!(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk)) {
  88. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  89. }
  90. }
  91. /* ************************************************************************** */
  92. int mxc_delay_start(unsigned long us)
  93. {
  94. // Check if timeout currently ongoing
  95. if (compare_value != 0) {
  96. return E_BUSY;
  97. }
  98. // Check if there is nothing to do
  99. if (us == 0) {
  100. return E_NO_ERROR;
  101. }
  102. // Calculate the necessary delay and start the timer
  103. mxc_delay_init(us);
  104. // Enable SysTick interrupt if necessary
  105. if (compare_value != 0) {
  106. SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
  107. }
  108. return E_NO_ERROR;
  109. }
  110. /* ************************************************************************** */
  111. int mxc_delay_check(void)
  112. {
  113. // Check if timeout currently ongoing
  114. if (compare_value == 0) {
  115. return E_NO_ERROR;
  116. }
  117. if((curr_value + (reload - SysTick->VAL)) >= compare_value) {
  118. mxc_delay_stop();
  119. return E_NO_ERROR;
  120. }
  121. return E_BUSY;
  122. }
  123. /* ************************************************************************** */
  124. void mxc_delay_stop(void)
  125. {
  126. SysTick->CTRL = ctrl_save;
  127. compare_value = 0;
  128. }
  129. /* ************************************************************************** */
  130. int mxc_delay(unsigned long us)
  131. {
  132. // Check if timeout currently ongoing
  133. if (compare_value != 0) {
  134. return E_BUSY;
  135. }
  136. // Check if there is nothing to do
  137. if (us == 0) {
  138. return E_NO_ERROR;
  139. }
  140. // Calculate the necessary delay and start the timer
  141. mxc_delay_init(us);
  142. // Wait until the total number of ticks exceeds the compare value.
  143. while ((curr_value + (reload - SysTick->VAL)) < compare_value) {
  144. // If SysTick interrupts are enabled, COUNTFLAG will never be set here and
  145. // curr_value will be incremented in the ISR. If SysTick interrupts are
  146. // disabled, curr_value is incremented here.
  147. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
  148. curr_value += reload;
  149. }
  150. }
  151. mxc_delay_stop();
  152. return E_NO_ERROR;
  153. }