spidevc.h 1.9 KB

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