core_tick.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 SysTickAddSleepTime(uint64_t Tick)
  55. {
  56. PowerOnTickCnt += Tick;
  57. }
  58. void SysTickDelay(uint32_t Tick)
  59. {
  60. uint64_t Tick1 = GetSysTick();
  61. uint64_t Tick2 = Tick1 + Tick;
  62. while(Tick2 > GetSysTick()){;}
  63. return;
  64. }
  65. void SysTickDelayUntil(uint64_t StartTick, uint32_t Tick)
  66. {
  67. uint64_t Tick1 = StartTick + Tick;
  68. while(Tick1 > GetSysTick()){;}
  69. return;
  70. }
  71. void CoreTick_Init(void)
  72. {
  73. prvPeriod = SystemCoreClock;
  74. TIMM0->TIM[CORE_TICK_TIM].ControlReg = 0;
  75. TIMM0->TIM[CORE_TICK_TIM].LoadCount = prvPeriod - 1;
  76. TIMM0->TIM[CORE_TICK_TIM].ControlReg = TIMER_CONTROL_REG_TIMER_ENABLE|TIMER_CONTROL_REG_TIMER_MODE;
  77. ISR_OnOff(CORE_TICK_IRQ, 0);
  78. ISR_SetHandler(CORE_TICK_IRQ, SystemTickIrqHandler, NULL);
  79. ISR_SetPriority(CORE_TICK_IRQ, CORE_TICK_IRQ_LEVEL);
  80. ISR_OnOff(CORE_TICK_IRQ, 1);
  81. }