spidevc.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /* TRANSMIT PROGRAMMING SEQUENCE (AND ALSO READ-BACK) */
  41. /* addr is the destination address in bits (16-bit - 0 to 0xFFFF valid ones)
  42. buf is buffer to be transmitted, it will go at position spi_getbufsz()+3
  43. len is length in _bytes_, should be 4 to 128 and be multiple of 4, as smallest
  44. transmission quantum is 32 bits */
  45. extern void spi_emit_data(struct spi_port *port, uint16_t addr, void *buf, size_t len);
  46. static inline
  47. bool spi_txrx(struct spi_port *port)
  48. {
  49. return port->txrx(port);
  50. }
  51. #endif