os_systick.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************//**
  2. * @file os_systick.c
  3. * @brief CMSIS OS Tick SysTick implementation
  4. * @version V1.0.1
  5. * @date 24. November 2017
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2017-2017 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;
  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(__CORTEX_M) && (__CORTEX_M == 7U)))
  49. SCB->SHPR[11] = SYSTICK_IRQ_PRIORITY;
  50. #elif (defined(__ARM_ARCH_8M_BASE__) && (__ARM_ARCH_8M_BASE__ != 0))
  51. SCB->SHPR[1] |= ((uint32_t)SYSTICK_IRQ_PRIORITY << 24);
  52. #elif ((defined(__ARM_ARCH_7M__) && (__ARM_ARCH_7M__ != 0)) || \
  53. (defined(__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ != 0)))
  54. SCB->SHP[11] = SYSTICK_IRQ_PRIORITY;
  55. #elif (defined(__ARM_ARCH_6M__) && (__ARM_ARCH_6M__ != 0))
  56. SCB->SHP[1] |= ((uint32_t)SYSTICK_IRQ_PRIORITY << 24);
  57. #else
  58. #error "Unknown ARM Core!"
  59. #endif
  60. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk;
  61. SysTick->LOAD = load;
  62. SysTick->VAL = 0U;
  63. PendST = 0U;
  64. return (0);
  65. }
  66. /// Enable OS Tick.
  67. __WEAK void OS_Tick_Enable (void) {
  68. if (PendST != 0U) {
  69. PendST = 0U;
  70. SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
  71. }
  72. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  73. }
  74. /// Disable OS Tick.
  75. __WEAK void OS_Tick_Disable (void) {
  76. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  77. if ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U) {
  78. SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
  79. PendST = 1U;
  80. }
  81. }
  82. // Acknowledge OS Tick IRQ.
  83. __WEAK void OS_Tick_AcknowledgeIRQ (void) {
  84. (void)SysTick->CTRL;
  85. }
  86. // Get OS Tick IRQ number.
  87. __WEAK int32_t OS_Tick_GetIRQn (void) {
  88. return ((int32_t)SysTick_IRQn);
  89. }
  90. // Get OS Tick clock.
  91. __WEAK uint32_t OS_Tick_GetClock (void) {
  92. return (SystemCoreClock);
  93. }
  94. // Get OS Tick interval.
  95. __WEAK uint32_t OS_Tick_GetInterval (void) {
  96. return (SysTick->LOAD + 1U);
  97. }
  98. // Get OS Tick count value.
  99. __WEAK uint32_t OS_Tick_GetCount (void) {
  100. uint32_t load = SysTick->LOAD;
  101. return (load - SysTick->VAL);
  102. }
  103. // Get OS Tick overflow status.
  104. __WEAK uint32_t OS_Tick_GetOverflow (void) {
  105. return ((SysTick->CTRL >> 16) & 1U);
  106. }
  107. #endif // SysTick