os_systick.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************//**
  2. * @file os_systick.c
  3. * @brief CMSIS OS Tick SysTick implementation
  4. * @version V1.0.4
  5. * @date 20. January 2023
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2017-2023 ARM Limited. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #include "os_tick.h"
  25. //lint -emacro((923,9078),SCB,SysTick) "cast from unsigned long to pointer"
  26. #include "RTE_Components.h"
  27. #include CMSIS_device_header
  28. #ifdef SysTick
  29. #ifndef SYSTICK_IRQ_PRIORITY
  30. #define SYSTICK_IRQ_PRIORITY 0xFFU
  31. #endif
  32. static uint8_t PendST __attribute__((section(".bss.os")));
  33. // Setup OS Tick.
  34. __WEAK int32_t OS_Tick_Setup (uint32_t freq, IRQHandler_t handler) {
  35. uint32_t load;
  36. (void)handler;
  37. if (freq == 0U) {
  38. //lint -e{904} "Return statement before end of function"
  39. return (-1);
  40. }
  41. load = (SystemCoreClock / freq) - 1U;
  42. if (load > 0x00FFFFFFU) {
  43. //lint -e{904} "Return statement before end of function"
  44. return (-1);
  45. }
  46. // Set SysTick Interrupt Priority
  47. #if ((defined(__ARM_ARCH_8M_MAIN__) && (__ARM_ARCH_8M_MAIN__ != 0)) || \
  48. (defined(__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ != 0)) || \
  49. (defined(__CORTEX_M) && (__CORTEX_M == 7U)))
  50. SCB->SHPR[11] = SYSTICK_IRQ_PRIORITY;
  51. #elif (defined(__ARM_ARCH_8M_BASE__) && (__ARM_ARCH_8M_BASE__ != 0))
  52. SCB->SHPR[1] |= ((uint32_t)SYSTICK_IRQ_PRIORITY << 24);
  53. #elif ((defined(__ARM_ARCH_7M__) && (__ARM_ARCH_7M__ != 0)) || \
  54. (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ != 0)))
  55. SCB->SHP[11] = SYSTICK_IRQ_PRIORITY;
  56. #elif (defined(__ARM_ARCH_6M__) && (__ARM_ARCH_6M__ != 0))
  57. SCB->SHP[1] |= ((uint32_t)SYSTICK_IRQ_PRIORITY << 24);
  58. #else
  59. #error "Unknown ARM Core!"
  60. #endif
  61. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk;
  62. SysTick->LOAD = load;
  63. SysTick->VAL = 0U;
  64. PendST = 0U;
  65. return (0);
  66. }
  67. /// Enable OS Tick.
  68. __WEAK void OS_Tick_Enable (void) {
  69. if (PendST != 0U) {
  70. PendST = 0U;
  71. SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
  72. }
  73. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  74. }
  75. /// Disable OS Tick.
  76. __WEAK void OS_Tick_Disable (void) {
  77. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  78. if ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U) {
  79. SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
  80. PendST = 1U;
  81. }
  82. }
  83. // Acknowledge OS Tick IRQ.
  84. __WEAK void OS_Tick_AcknowledgeIRQ (void) {
  85. (void)SysTick->CTRL;
  86. }
  87. // Get OS Tick IRQ number.
  88. __WEAK int32_t OS_Tick_GetIRQn (void) {
  89. return ((int32_t)SysTick_IRQn);
  90. }
  91. // Get OS Tick clock.
  92. __WEAK uint32_t OS_Tick_GetClock (void) {
  93. return (SystemCoreClock);
  94. }
  95. // Get OS Tick interval.
  96. __WEAK uint32_t OS_Tick_GetInterval (void) {
  97. return (SysTick->LOAD + 1U);
  98. }
  99. // Get OS Tick count value.
  100. __WEAK uint32_t OS_Tick_GetCount (void) {
  101. uint32_t val;
  102. uint32_t count;
  103. val = SysTick->VAL;
  104. if (val != 0U) {
  105. count = (SysTick->LOAD - val) + 1U;
  106. } else {
  107. count = 0U;
  108. }
  109. return (count);
  110. }
  111. // Get OS Tick overflow status.
  112. __WEAK uint32_t OS_Tick_GetOverflow (void) {
  113. return ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) >> SCB_ICSR_PENDSTSET_Pos);
  114. }
  115. #endif // SysTick