os_tick_ptim.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************//**
  2. * @file os_tick_ptim.c
  3. * @brief CMSIS OS Tick implementation for Private Timer
  4. * @version V1.0.2
  5. * @date 02. March 2018
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2017-2018 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 "RTE_Components.h"
  25. #include CMSIS_device_header
  26. #if defined(PTIM)
  27. #include "os_tick.h"
  28. #include "irq_ctrl.h"
  29. #ifndef PTIM_IRQ_PRIORITY
  30. #define PTIM_IRQ_PRIORITY 0xFFU
  31. #endif
  32. static uint8_t PTIM_PendIRQ; // Timer interrupt pending flag
  33. // Setup OS Tick.
  34. int32_t OS_Tick_Setup (uint32_t freq, IRQHandler_t handler) {
  35. uint32_t load;
  36. uint32_t prio;
  37. uint32_t bits;
  38. if (freq == 0U) {
  39. return (-1);
  40. }
  41. PTIM_PendIRQ = 0U;
  42. // Private Timer runs with the system frequency
  43. load = (SystemCoreClock / freq) - 1U;
  44. // Disable Private Timer and set load value
  45. PTIM_SetControl (0U);
  46. PTIM_SetLoadValue (load);
  47. // Disable corresponding IRQ
  48. IRQ_Disable (PrivTimer_IRQn);
  49. IRQ_ClearPending(PrivTimer_IRQn);
  50. // Determine number of implemented priority bits
  51. IRQ_SetPriority (PrivTimer_IRQn, 0xFFU);
  52. prio = IRQ_GetPriority (PrivTimer_IRQn);
  53. // At least bits [7:4] must be implemented
  54. if ((prio & 0xF0U) == 0U) {
  55. return (-1);
  56. }
  57. for (bits = 0; bits < 4; bits++) {
  58. if ((prio & 0x01) != 0) {
  59. break;
  60. }
  61. prio >>= 1;
  62. }
  63. // Adjust configured priority to the number of implemented priority bits
  64. prio = (PTIM_IRQ_PRIORITY << bits) & 0xFFUL;
  65. // Set Private Timer interrupt priority
  66. IRQ_SetPriority(PrivTimer_IRQn, prio-1U);
  67. // Set edge-triggered IRQ
  68. IRQ_SetMode(PrivTimer_IRQn, IRQ_MODE_TRIG_EDGE);
  69. // Register tick interrupt handler function
  70. IRQ_SetHandler(PrivTimer_IRQn, handler);
  71. // Enable corresponding interrupt
  72. IRQ_Enable (PrivTimer_IRQn);
  73. // Set bits: IRQ enable and Auto reload
  74. PTIM_SetControl (0x06U);
  75. return (0);
  76. }
  77. /// Enable OS Tick.
  78. void OS_Tick_Enable (void) {
  79. uint32_t ctrl;
  80. // Set pending interrupt if flag set
  81. if (PTIM_PendIRQ != 0U) {
  82. PTIM_PendIRQ = 0U;
  83. IRQ_SetPending (PrivTimer_IRQn);
  84. }
  85. // Start the Private Timer
  86. ctrl = PTIM_GetControl();
  87. // Set bit: Timer enable
  88. ctrl |= 1U;
  89. PTIM_SetControl (ctrl);
  90. }
  91. /// Disable OS Tick.
  92. void OS_Tick_Disable (void) {
  93. uint32_t ctrl;
  94. // Stop the Private Timer
  95. ctrl = PTIM_GetControl();
  96. // Clear bit: Timer enable
  97. ctrl &= ~1U;
  98. PTIM_SetControl (ctrl);
  99. // Remember pending interrupt flag
  100. if (IRQ_GetPending(PrivTimer_IRQn) != 0) {
  101. IRQ_ClearPending (PrivTimer_IRQn);
  102. PTIM_PendIRQ = 1U;
  103. }
  104. }
  105. // Acknowledge OS Tick IRQ.
  106. void OS_Tick_AcknowledgeIRQ (void) {
  107. PTIM_ClearEventFlag();
  108. }
  109. // Get OS Tick IRQ number.
  110. int32_t OS_Tick_GetIRQn (void) {
  111. return (PrivTimer_IRQn);
  112. }
  113. // Get OS Tick clock.
  114. uint32_t OS_Tick_GetClock (void) {
  115. return (SystemCoreClock);
  116. }
  117. // Get OS Tick interval.
  118. uint32_t OS_Tick_GetInterval (void) {
  119. return (PTIM_GetLoadValue() + 1U);
  120. }
  121. // Get OS Tick count value.
  122. uint32_t OS_Tick_GetCount (void) {
  123. uint32_t load = PTIM_GetLoadValue();
  124. return (load - PTIM_GetCurrentValue());
  125. }
  126. // Get OS Tick overflow status.
  127. uint32_t OS_Tick_GetOverflow (void) {
  128. return (PTIM->ISR & 1);
  129. }
  130. #endif // PTIM