spidevc.h 1.9 KB

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