sys.h 478 B

1234567891011121314151617181920212223242526
  1. // (C) 2024 Victor Suarez Rovere <suarezvictor@gmail.com>
  2. // SPDX-License-Identifier: AGPL-3.0-only
  3. #ifndef __SYS_H__
  4. #define __SYS_H__
  5. #define F_XTAL 24000000
  6. static inline uint64_t cpu_count(void)
  7. {
  8. uint64_t value;
  9. __asm__ __volatile__("csrr %0, time\n" : "=r"(value) :: "memory");
  10. return value;
  11. }
  12. static inline void delay_us(unsigned long us)
  13. {
  14. uint64_t t1 = cpu_count() + us * F_XTAL/1000000;
  15. int64_t dt;
  16. do {
  17. dt = t1 - cpu_count();
  18. } while(dt > 0);
  19. }
  20. #endif