lowl-spi.h 2.5 KB

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