spidevc.h 2.1 KB

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