twi_master.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef TWI_MASTER_MAC_H_
  2. #define TWI_MASTER_MAC_H_
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include "util.h"
  6. struct twi_transfer;
  7. enum twi_status {
  8. TWI_STAT_IDLE = 0,
  9. TWI_STAT_INPROGRESS,
  10. TWI_STAT_FINISHED,
  11. TWI_STAT_BUSERROR,
  12. TWI_STAT_TIMEOUT,
  13. TWI_STAT_CANCELLED,
  14. };
  15. #if defined(TWI_SIZE_16BIT) && (TWI_SIZE_16BIT != 0)
  16. typedef uint16_t twi_size_t;
  17. #else
  18. typedef uint8_t twi_size_t;
  19. #endif
  20. typedef void (*twi_callback_t)(struct twi_transfer *, enum twi_status);
  21. struct twi_transfer {
  22. void *buffer;
  23. twi_size_t write_size;
  24. twi_size_t read_size;
  25. uint8_t address;
  26. twi_callback_t callback;
  27. /* Internal fields follow. */
  28. uint8_t status; /* enum twi_transfer_status_flags */
  29. twi_size_t offset; /* The current byte offset. */
  30. struct twi_transfer *next; /* Linked list of transfer objects. */
  31. };
  32. void twi_init(void);
  33. static inline void twi_transfer_init(struct twi_transfer *xfer)
  34. {
  35. memset(xfer, 0, sizeof(*xfer));
  36. }
  37. void twi_transfer(struct twi_transfer *xfer);
  38. enum twi_status twi_transfer_get_status(const struct twi_transfer *xfer);
  39. enum twi_status twi_transfer_wait(struct twi_transfer *xfer,
  40. uint16_t timeout_ms);
  41. void twi_transfer_cancel(struct twi_transfer *xfer);
  42. #endif /* TWI_MASTER_MAC_H_ */