io.h 831 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef IO_H
  2. #define IO_H
  3. #include "iodev.h"
  4. static inline void con_write(uint8_t c)
  5. {
  6. CONSOLE = c;
  7. }
  8. static inline void con_set_baudrate(uint32_t b)
  9. {
  10. uint32_t bauddiv = (CON_BAUD_BASE + (b >> 1))/b;
  11. CON_BAUDDIV = bauddiv ? bauddiv - 1 : 0;
  12. }
  13. static inline void set_led(uint8_t leds)
  14. {
  15. LED = leds;
  16. }
  17. static inline uint32_t rdtime(void)
  18. {
  19. uint32_t t;
  20. asm volatile("rdtime %0" : "=r" (t));
  21. return t;
  22. }
  23. static inline uint64_t rdtimeq(void)
  24. {
  25. uint32_t l, h1, h0;
  26. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  27. : "=r" (h1), "=r" (l), "=r" (h0));
  28. return ((int32_t)l < 0) ? h1 : h0;
  29. }
  30. static inline void udelay(uint32_t us)
  31. {
  32. uint32_t cycles = us * (CPU_CLK_HZ / 1000000);
  33. uint32_t start = rdtime();
  34. while (rdtime() - start < cycles)
  35. /* wait */;
  36. }
  37. #endif /* IO_H */