spi-context.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef SPI_CONTEXT_H
  2. #define SPI_CONTEXT_H
  3. #include <linux/types.h>
  4. #include <linux/spi/spidev.h>
  5. #include <stdbool.h>
  6. #include <stdint.h>
  7. #define SPI_DEVICE_TEMPLATE "/dev/spidev%d.%d"
  8. #define DEFAULT_SPI_BUS 0
  9. #define DEFAULT_SPI_CS_LINE 0
  10. #define DEFAULT_SPI_MODE SPI_MODE_0
  11. #define DEFAULT_SPI_BITS_PER_WORD 8
  12. #define DEFAULT_SPI_SPEED 1500000
  13. #define DEFAULT_SPI_DELAY_USECS 0
  14. struct spi_config {
  15. int bus;
  16. int cs_line;
  17. uint8_t mode;
  18. uint32_t speed;
  19. uint8_t bits;
  20. uint16_t delay;
  21. };
  22. static const struct spi_config default_spi_config = {
  23. .bus = DEFAULT_SPI_BUS,
  24. .cs_line = DEFAULT_SPI_CS_LINE,
  25. .mode = DEFAULT_SPI_MODE,
  26. .speed = DEFAULT_SPI_SPEED,
  27. .bits = DEFAULT_SPI_BITS_PER_WORD,
  28. .delay = DEFAULT_SPI_DELAY_USECS,
  29. };
  30. struct spi_ctx {
  31. int fd;
  32. struct spi_config config;
  33. };
  34. /* create SPI context with given configuration, returns NULL on failure */
  35. extern struct spi_ctx *spi_init(struct spi_config *config);
  36. /* close descriptor and free resources */
  37. extern void spi_exit(struct spi_ctx *ctx);
  38. /* process RX/TX transfer, ensure buffers are long enough */
  39. extern bool spi_transfer(struct spi_ctx *ctx, uint8_t *txbuf,
  40. uint8_t *rxbuf, int len);
  41. #endif /* SPI_CONTEXT_H */