core_tick.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "bl_inc.h"
  22. volatile static uint64_t PowerOnTickCnt;
  23. volatile static uint64_t prvPeriod;
  24. //
  25. static void __FUNC_IN_RAM__ SystemTickIrqHandler( int32_t Line, void *pData)
  26. {
  27. volatile uint32_t clr;
  28. clr = TIMM0->TIM[CORE_TICK_TIM].EOI;
  29. PowerOnTickCnt += prvPeriod;
  30. }
  31. uint64_t __FUNC_IN_RAM__ GetSysTick(void)
  32. {
  33. volatile uint32_t clr;
  34. volatile uint64_t PowerOnTick;
  35. ISR_OnOff(CORE_TICK_IRQ, 0);
  36. PowerOnTick = PowerOnTickCnt + prvPeriod - TIMM0->TIM[CORE_TICK_TIM].CurrentValue;
  37. if (TIMM0->TIM[CORE_TICK_TIM].IntStatus)
  38. {
  39. ISR_OnOff(CORE_TICK_IRQ, 1);
  40. while(TIMM0->TIM[CORE_TICK_TIM].IntStatus){;}
  41. PowerOnTick = PowerOnTickCnt + prvPeriod - TIMM0->TIM[CORE_TICK_TIM].CurrentValue;
  42. }
  43. ISR_OnOff(CORE_TICK_IRQ, 1);
  44. return PowerOnTick;
  45. }
  46. uint64_t GetSysTickUS(void)
  47. {
  48. return GetSysTick()/CORE_TICK_1US;
  49. }
  50. uint64_t GetSysTickMS(void)
  51. {
  52. return GetSysTick()/CORE_TICK_1MS;
  53. }
  54. void SysTickDelay(uint32_t Tick)
  55. {
  56. uint64_t Tick1 = GetSysTick();
  57. uint64_t Tick2 = Tick1 + Tick;
  58. while(Tick2 > GetSysTick()){;}
  59. return;
  60. }
  61. void SysTickDelayUntil(uint64_t StartTick, uint32_t Tick)
  62. {
  63. uint64_t Tick1 = StartTick + Tick;
  64. while(Tick1 > GetSysTick()){;}
  65. return;
  66. }
  67. void CoreTick_Init(void)
  68. {
  69. prvPeriod = SystemCoreClock;
  70. TIMM0->TIM[CORE_TICK_TIM].ControlReg = 0;
  71. TIMM0->TIM[CORE_TICK_TIM].LoadCount = prvPeriod;
  72. TIMM0->TIM[CORE_TICK_TIM].ControlReg = TIMER_CONTROL_REG_TIMER_ENABLE|TIMER_CONTROL_REG_TIMER_MODE;
  73. ISR_OnOff(CORE_TICK_IRQ, 0);
  74. ISR_SetHandler(CORE_TICK_IRQ, SystemTickIrqHandler, NULL);
  75. ISR_SetPriority(CORE_TICK_IRQ, CORE_TICK_IRQ_LEVEL);
  76. ISR_OnOff(CORE_TICK_IRQ, 1);
  77. }