2
0

io.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_leds(unsigned int leds)
  13. {
  14. SYS_LED = leds;
  15. }
  16. static inline void set_led(unsigned int led, bool on)
  17. {
  18. unsigned int leds = SYS_LED;
  19. unsigned int mask = 1 << led;
  20. leds &= ~mask;
  21. leds |= on ? mask : 0;
  22. SYS_LED = leds;
  23. }
  24. enum leds {
  25. LED_SDCARD,
  26. LED_DISKIO,
  27. LED_ABCBUS
  28. };
  29. static inline void __attribute__((noreturn)) reset(void)
  30. {
  31. p_maskirq(~0, 0); /* Block all interrupts */
  32. for (;;)
  33. asm volatile("ebreak"); /* Trigger system hang */
  34. }
  35. extern const uint32_t time_zero;
  36. static inline uint32_t rdtime(void)
  37. {
  38. uint32_t t;
  39. asm volatile("rdtime %0" : "=r" (t));
  40. return t;
  41. }
  42. static inline uint64_t rdtimeq(void)
  43. {
  44. uint32_t l, h1, h0;
  45. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  46. : "=r" (h1), "=r" (l), "=r" (h0));
  47. return ((uint64_t)(((int32_t)l < 0) ? h1 : h0) << 32) + l;
  48. }
  49. static inline void udelay(uint32_t us)
  50. {
  51. uint32_t cycles = us * (CPU_HZ / 1000000);
  52. uint32_t start = rdtime();
  53. while (rdtime() - start < cycles)
  54. pause();
  55. }
  56. static inline void i2c_set_speed(unsigned int khz)
  57. {
  58. I2C_DIVISOR = ((CPU_HZ/4000)-1)/khz;
  59. }
  60. #endif /* IO_H */