os_systick.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**************************************************************************//**
  2. * @file os_systick.c
  3. * @brief CMSIS OS Tick SysTick implementation
  4. * @version V1.0.1
  5. * @date 29. 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 <rtthread.h>
  25. #include "cmsis_rtthread.h"
  26. #include "os_tick.h"
  27. #include "board.h"
  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. RT_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. NVIC_SetPriority(SysTick_IRQn, SYSTICK_IRQ_PRIORITY);
  47. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk;
  48. SysTick->LOAD = load;
  49. SysTick->VAL = 0U;
  50. PendST = 0U;
  51. return (0);
  52. }
  53. /// Enable OS Tick.
  54. RT_WEAK void OS_Tick_Enable (void) {
  55. if (PendST != 0U) {
  56. PendST = 0U;
  57. SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
  58. }
  59. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  60. }
  61. /// Disable OS Tick.
  62. RT_WEAK void OS_Tick_Disable (void) {
  63. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  64. if ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U) {
  65. SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
  66. PendST = 1U;
  67. }
  68. }
  69. // Acknowledge OS Tick IRQ.
  70. RT_WEAK void OS_Tick_AcknowledgeIRQ (void) {
  71. (void)SysTick->CTRL;
  72. }
  73. // Get OS Tick IRQ number.
  74. RT_WEAK int32_t OS_Tick_GetIRQn (void) {
  75. return ((int32_t)SysTick_IRQn);
  76. }
  77. // Get OS Tick clock.
  78. RT_WEAK uint32_t OS_Tick_GetClock (void) {
  79. return (SystemCoreClock);
  80. }
  81. // Get OS Tick interval.
  82. RT_WEAK uint32_t OS_Tick_GetInterval (void) {
  83. return (SysTick->LOAD + 1U);
  84. }
  85. // Get OS Tick count value.
  86. RT_WEAK uint32_t OS_Tick_GetCount (void) {
  87. uint32_t load = SysTick->LOAD;
  88. return (load - SysTick->VAL);
  89. }
  90. // Get OS Tick overflow status.
  91. RT_WEAK uint32_t OS_Tick_GetOverflow (void) {
  92. return ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) >> SCB_ICSR_PENDSTSET_Pos);
  93. }
  94. #endif // SysTick