systick.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "ARMCM0.h"
  17. #include "systick.h"
  18. #include "../misc.h"
  19. // 0x20000324
  20. static uint32_t gTickMultiplier;
  21. void SYSTICK_Init(void)
  22. {
  23. SysTick_Config(480000);
  24. gTickMultiplier = 48;
  25. }
  26. void SYSTICK_DelayUs(uint32_t Delay)
  27. {
  28. const uint32_t ticks = Delay * gTickMultiplier;
  29. uint32_t elapsed_ticks = 0;
  30. uint32_t Start = SysTick->LOAD;
  31. uint32_t Previous = SysTick->VAL;
  32. do {
  33. uint32_t Current;
  34. do {
  35. Current = SysTick->VAL;
  36. } while (Current == Previous);
  37. uint32_t Delta = ((Current < Previous) ? - Current : Start - Current);
  38. elapsed_ticks += Delta + Previous;
  39. Previous = Current;
  40. } while (elapsed_ticks < ticks);
  41. }