2
0

io.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef IO_H
  2. #define IO_H
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <stdbool.h>
  6. #include "ioregs.h"
  7. #include "picorv32.h"
  8. static inline void pause(void)
  9. {
  10. /* Placeholder for anything that might want to be done while waiting */
  11. }
  12. static inline void set_led(uint8_t leds)
  13. {
  14. SYS_LED = leds;
  15. }
  16. static inline void __attribute__((noreturn)) reset(void)
  17. {
  18. p_maskirq(~0, 0); /* Block all interrupts */
  19. for (;;)
  20. asm volatile("ebreak"); /* Trigger system hang */
  21. }
  22. extern const uint32_t time_zero;
  23. static inline uint32_t rdtime(void)
  24. {
  25. uint32_t t;
  26. asm volatile("rdtime %0" : "=r" (t));
  27. return t;
  28. }
  29. static inline uint64_t rdtimeq(void)
  30. {
  31. uint32_t l, h1, h0;
  32. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  33. : "=r" (h1), "=r" (l), "=r" (h0));
  34. return ((uint64_t)(((int32_t)l < 0) ? h1 : h0) << 32) + l;
  35. }
  36. static inline void udelay(uint32_t us)
  37. {
  38. uint32_t cycles = us * (CPU_HZ / 1000000);
  39. uint32_t start = rdtime();
  40. while (rdtime() - start < cycles)
  41. pause();
  42. }
  43. static inline void i2c_set_speed(unsigned int khz)
  44. {
  45. I2C_DIVISOR = ((CPU_HZ/4000)-1)/khz;
  46. }
  47. #endif /* IO_H */