platform_wch_riscv.c.bak 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/zv_types.h"
  7. #include "zivuos/zv_time.h"
  8. #ifndef FUNCONF_SYSTEM_CORE_CLOCK
  9. #error "MCU core clock (FUNCONF_SYSTEM_CORE_CLOCK) not defined."
  10. #endif
  11. #define ZV_TICKS_PER_US ((uint32_t) FUNCONF_SYSTEM_CORE_CLOCK / 1000000)
  12. #define ZV_TICKS_PER_MS ((uint32_t) FUNCONF_SYSTEM_CORE_CLOCK / 1000)
  13. volatile uint32_t __zv_current_time_miliseconds;
  14. void zv_platform_init(void) {
  15. __zv_current_time_miliseconds = 0;
  16. SysTick->CTLR = (
  17. SYSTICK_CTLR_STE |
  18. SYSTICK_CTLR_STCLK |
  19. SYSTICK_CTLR_STIE
  20. );
  21. NVIC_EnableIRQ(SysTick_IRQn);
  22. SysTick->CNT = 0x00;
  23. SysTick->CMP = ZV_TICKS_PER_MS;
  24. }
  25. void SysTick_Handler(void) __attribute__((interrupt));
  26. void SysTick_Handler(void) {
  27. SysTick->CMP += ZV_TICKS_PER_MS;
  28. SysTick->SR = 0x00;
  29. ++__zv_current_time_miliseconds;
  30. }
  31. zv_time_t zv_platform_get_time(void) {
  32. return __zv_current_time_miliseconds;
  33. }
  34. zv_ticks_t zv_platform_get_ticks(void) {
  35. return SysTick->CNT;
  36. }
  37. zv_ticks_t zv_platform_get_time_us(void) {
  38. return SysTick->CNT / ZV_TICKS_PER_US;
  39. }
  40. #endif