spidevc.h 1.6 KB

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