spidevc.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef SPIDEVC_H
  2. #define SPIDEVC_H
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <unistd.h>
  6. #define SPIMAXSZ (256*1024)
  7. /* Initialize SPI using this function */
  8. void spi_init(void);
  9. struct spi_port {
  10. /* TX-RX single frame */
  11. bool (*txrx)(struct spi_port *port);
  12. char spibuf[SPIMAXSZ], spibuf_rx[SPIMAXSZ];
  13. size_t spibufsz;
  14. struct cgpu_info *cgpu;
  15. const char *repr;
  16. int logprio;
  17. };
  18. extern struct spi_port *sys_spi;
  19. /* SPI BUFFER OPS */
  20. static inline
  21. void spi_clear_buf(struct spi_port *port)
  22. {
  23. port->spibufsz = 0;
  24. }
  25. static inline
  26. void *spi_getrxbuf(struct spi_port *port)
  27. {
  28. return port->spibuf_rx;
  29. }
  30. static inline
  31. void *spi_gettxbuf(struct spi_port *port)
  32. {
  33. return port->spibuf;
  34. }
  35. static inline
  36. size_t spi_getbufsz(struct spi_port *port)
  37. {
  38. return port->spibufsz;
  39. }
  40. extern void spi_emit_break(struct spi_port *port); /* BREAK CONNECTIONS AFTER RESET */
  41. extern void spi_emit_fsync(struct spi_port *port); /* FEED-THROUGH TO NEXT CHIP SYNCHRONOUSLY (WITH FLIP-FLOP) */
  42. extern void spi_emit_fasync(struct spi_port *port, int n); /* FEED-THROUGH TO NEXT CHIP ASYNCHRONOUSLY (WITHOUT FLIP-FLOP INTERMEDIATE) */
  43. extern void spi_emit_nop(struct spi_port *port, int n);
  44. /* TRANSMIT PROGRAMMING SEQUENCE (AND ALSO READ-BACK) */
  45. /* addr is the destination address in bits (16-bit - 0 to 0xFFFF valid ones)
  46. buf is buffer to be transmitted, it will go at position spi_getbufsz()+3
  47. len is length in _bytes_, should be 4 to 128 and be multiple of 4, as smallest
  48. transmission quantum is 32 bits */
  49. extern void spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t len);
  50. static inline
  51. bool spi_txrx(struct spi_port *port)
  52. {
  53. return port->txrx(port);
  54. }
  55. extern bool sys_spi_txrx(struct spi_port *);
  56. #endif