spidevc.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. int speed;
  18. };
  19. extern struct spi_port *sys_spi;
  20. /* SPI BUFFER OPS */
  21. static inline
  22. void spi_clear_buf(struct spi_port *port)
  23. {
  24. port->spibufsz = 0;
  25. }
  26. static inline
  27. void *spi_getrxbuf(struct spi_port *port)
  28. {
  29. return port->spibuf_rx;
  30. }
  31. static inline
  32. void *spi_gettxbuf(struct spi_port *port)
  33. {
  34. return port->spibuf;
  35. }
  36. static inline
  37. size_t spi_getbufsz(struct spi_port *port)
  38. {
  39. return port->spibufsz;
  40. }
  41. extern void spi_emit_break(struct spi_port *port); /* BREAK CONNECTIONS AFTER RESET */
  42. extern void spi_emit_fsync(struct spi_port *port); /* FEED-THROUGH TO NEXT CHIP SYNCHRONOUSLY (WITH FLIP-FLOP) */
  43. extern void spi_emit_fasync(struct spi_port *port, int n); /* FEED-THROUGH TO NEXT CHIP ASYNCHRONOUSLY (WITHOUT FLIP-FLOP INTERMEDIATE) */
  44. extern void spi_emit_nop(struct spi_port *port, int n);
  45. /* TRANSMIT PROGRAMMING SEQUENCE (AND ALSO READ-BACK) */
  46. /* addr is the destination address in bits (16-bit - 0 to 0xFFFF valid ones)
  47. buf is buffer to be transmitted, it will go at position spi_getbufsz()+3
  48. len is length in _bytes_, should be 4 to 128 and be multiple of 4, as smallest
  49. transmission quantum is 32 bits */
  50. extern void *spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t len);
  51. static inline
  52. bool spi_txrx(struct spi_port *port)
  53. {
  54. return port->txrx(port);
  55. }
  56. extern bool sys_spi_txrx(struct spi_port *);
  57. void spi_bfsb_select_bank(int bank);
  58. #endif