TWIlib.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * TWIlib.h
  3. *
  4. * Created: 6/01/2014 10:38:42 PM
  5. * Author: Chris Herring
  6. */
  7. #ifndef TWILIB_H_
  8. #define TWILIB_H_
  9. // TWI bit rate
  10. #define TWI_FREQ 400000
  11. // Get TWI status
  12. #define TWI_STATUS (TWSR & 0xF8)
  13. // Transmit buffer length
  14. #define TXMAXBUFLEN 20
  15. // Receive buffer length
  16. #define RXMAXBUFLEN 20
  17. // Global transmit buffer
  18. volatile uint8_t *TWITransmitBuffer;
  19. // Global receive buffer
  20. volatile uint8_t TWIReceiveBuffer[RXMAXBUFLEN];
  21. // Buffer indexes
  22. volatile int TXBuffIndex; // Index of the transmit buffer. Is volatile, can change at any time.
  23. int RXBuffIndex; // Current index in the receive buffer
  24. // Buffer lengths
  25. int TXBuffLen; // The total length of the transmit buffer
  26. int RXBuffLen; // The total number of bytes to read (should be less than RXMAXBUFFLEN)
  27. typedef enum {
  28. Ready,
  29. Initializing,
  30. RepeatedStartSent,
  31. MasterTransmitter,
  32. MasterReceiver,
  33. SlaceTransmitter,
  34. SlaveReciever
  35. } TWIMode;
  36. typedef struct TWIInfoStruct{
  37. TWIMode mode;
  38. uint8_t errorCode;
  39. uint8_t repStart;
  40. }TWIInfoStruct;
  41. TWIInfoStruct TWIInfo;
  42. // TWI Status Codes
  43. #define TWI_START_SENT 0x08 // Start sent
  44. #define TWI_REP_START_SENT 0x10 // Repeated Start sent
  45. // Master Transmitter Mode
  46. #define TWI_MT_SLAW_ACK 0x18 // SLA+W sent and ACK received
  47. #define TWI_MT_SLAW_NACK 0x20 // SLA+W sent and NACK received
  48. #define TWI_MT_DATA_ACK 0x28 // DATA sent and ACK received
  49. #define TWI_MT_DATA_NACK 0x30 // DATA sent and NACK received
  50. // Master Receiver Mode
  51. #define TWI_MR_SLAR_ACK 0x40 // SLA+R sent, ACK received
  52. #define TWI_MR_SLAR_NACK 0x48 // SLA+R sent, NACK received
  53. #define TWI_MR_DATA_ACK 0x50 // Data received, ACK returned
  54. #define TWI_MR_DATA_NACK 0x58 // Data received, NACK returned
  55. // Miscellaneous States
  56. #define TWI_LOST_ARBIT 0x38 // Arbitration has been lost
  57. #define TWI_NO_RELEVANT_INFO 0xF8 // No relevant information available
  58. #define TWI_ILLEGAL_START_STOP 0x00 // Illegal START or STOP condition has been detected
  59. #define TWI_SUCCESS 0xFF // Successful transfer, this state is impossible from TWSR as bit2 is 0 and read only
  60. #define TWISendStart() (TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWIE)) // Send the START signal, enable interrupts and TWI, clear TWINT flag to resume transfer.
  61. #define TWISendStop() (TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN)|(1<<TWIE)) // Send the STOP signal, enable interrupts and TWI, clear TWINT flag.
  62. #define TWISendTransmit() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // Used to resume a transfer, clear TWINT and ensure that TWI and interrupts are enabled.
  63. #define TWISendACK() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)|(1<<TWEA)) // FOR MR mode. Resume a transfer, ensure that TWI and interrupts are enabled and respond with an ACK if the device is addressed as a slave or after it receives a byte.
  64. #define TWISendNACK() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // FOR MR mode. Resume a transfer, ensure that TWI and interrupts are enabled but DO NOT respond with an ACK if the device is addressed as a slave or after it receives a byte.
  65. // Function declarations
  66. void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking);
  67. void TWIInit(void);
  68. uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart);
  69. uint8_t isTWIReady(void);
  70. #endif // TWICOMMS_H_