platform_wch_riscv.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifdef ZIVUOS_PLATFORM_WCH_RISCV
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. #include <ch32fun.h>
  6. #include <zivuos.h>
  7. #ifndef FUNCONF_SYSTEM_CORE_CLOCK
  8. #error "MCU core clock (FUNCONF_SYSTEM_CORE_CLOCK) not defined."
  9. #endif
  10. #define ZV_TICKS_PER_US ((uint32_t) FUNCONF_SYSTEM_CORE_CLOCK / 1000000)
  11. #define ZV_TICKS_PER_MS ((uint32_t) FUNCONF_SYSTEM_CORE_CLOCK / 1000)
  12. volatile zv_time_t __zv_current_time_miliseconds;
  13. void zv_platform_init(void) {
  14. __zv_current_time_miliseconds = 0;
  15. SysTick->CTLR = (
  16. SYSTICK_CTLR_STE |
  17. SYSTICK_CTLR_STCLK |
  18. SYSTICK_CTLR_STIE
  19. );
  20. NVIC_EnableIRQ(SysTick_IRQn);
  21. SysTick->CNT = 0x00;
  22. SysTick->CMP = ZV_TICKS_PER_MS;
  23. }
  24. void SysTick_Handler(void) __attribute__((interrupt));
  25. void SysTick_Handler(void) {
  26. SysTick->CMP += ZV_TICKS_PER_MS;
  27. SysTick->SR = 0x00;
  28. ++__zv_current_time_miliseconds;
  29. }
  30. zv_time_t __zv_platform_get_time_ms(void) {
  31. return __zv_current_time_miliseconds;
  32. }
  33. zv_ticks_t __zv_platform_get_time_us(void) {
  34. return SysTick->CNT / ZV_TICKS_PER_US;
  35. }
  36. #endif