io.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. static inline void pause(void)
  8. {
  9. /* Placeholder for anything that might want to be done while waiting */
  10. }
  11. static inline void set_led(uint8_t leds)
  12. {
  13. SYS_LED = leds;
  14. }
  15. static inline __attribute__((noreturn)) void reset(void)
  16. {
  17. while (1)
  18. SYS_RESET = 1;
  19. }
  20. extern const uint32_t time_zero;
  21. static inline uint32_t rdtime(void)
  22. {
  23. uint32_t t;
  24. asm volatile("rdtime %0" : "=r" (t));
  25. return t;
  26. }
  27. static inline uint64_t rdtimeq(void)
  28. {
  29. uint32_t l, h1, h0;
  30. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  31. : "=r" (h1), "=r" (l), "=r" (h0));
  32. return ((uint64_t)(((int32_t)l < 0) ? h1 : h0) << 32) + l;
  33. }
  34. static inline void udelay(uint32_t us)
  35. {
  36. uint32_t cycles = us * (CPU_HZ / 1000000);
  37. uint32_t start = rdtime();
  38. while (rdtime() - start < cycles)
  39. pause();
  40. }
  41. static inline void i2c_set_speed(unsigned int khz)
  42. {
  43. I2C_DIVISOR = ((CPU_HZ/4000)-1)/khz;
  44. }
  45. #endif /* IO_H */