spidevc.h 2.0 KB

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