io.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "irq.h"
  9. #define barrier() do { asm volatile("" : : : "memory"); } while (0)
  10. static __always_inline void relax(void)
  11. {
  12. /* Placeholder for anything that might want to be done while waiting */
  13. }
  14. static __always_inline void set_leds(unsigned int leds)
  15. {
  16. SYS_LED = leds;
  17. }
  18. static __always_inline void set_led(unsigned int led, bool on)
  19. {
  20. unsigned int leds = SYS_LED;
  21. unsigned int mask = 1 << led;
  22. leds &= ~mask;
  23. leds |= on ? mask : 0;
  24. SYS_LED = leds;
  25. }
  26. enum leds {
  27. LED_SDCARD,
  28. LED_DISKIO,
  29. LED_ABCBUS
  30. };
  31. static __always_inline no_return reset(unsigned int type)
  32. {
  33. disable_irqs();
  34. for (;;)
  35. SYS_RESET = type;
  36. }
  37. extern const uint32_t time_zero;
  38. static __always_inline uint32_t rdtime(void)
  39. {
  40. uint32_t t;
  41. asm volatile("rdtime %0" : "=r" (t));
  42. return t;
  43. }
  44. static __always_inline uint64_t rdtimeq(void)
  45. {
  46. uint32_t l, h1, h0;
  47. asm volatile("rdtimeh %0; rdtime %1; %rdtimeh %2"
  48. : "=r" (h1), "=r" (l), "=r" (h0));
  49. return ((uint64_t)(((int32_t)l < 0) ? h1 : h0) << 32) + l;
  50. }
  51. static __always_inline void cdelay(uint32_t cycles)
  52. {
  53. if (!__builtin_constant_p(cycles) || cycles >= 4) {
  54. uint32_t start = rdtime();
  55. while (rdtime() - start < cycles)
  56. relax();
  57. }
  58. }
  59. static __always_inline void udelay(uint32_t us)
  60. {
  61. cdelay(us * (CPU_HZ / 1000000));
  62. }
  63. static __always_inline void mdelay(uint32_t ms)
  64. {
  65. cdelay(ms * (CPU_HZ / 1000));
  66. }
  67. static inline void i2c_set_speed(unsigned int khz)
  68. {
  69. I2C_DIVISOR = ((CPU_HZ/4000)-1)/khz;
  70. }
  71. static __always_inline void wait_romcopy_done(void)
  72. {
  73. while (~irqmask() & (1 << ROMCOPY_IRQ))
  74. relax();
  75. barrier();
  76. }
  77. /* Read a random 32-bit number */
  78. static inline uint32_t rdrand(void)
  79. {
  80. waitfor(RANDOM_IRQ);
  81. return RANDOM_DATA;
  82. }
  83. #endif /* IO_H */