You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.8 KiB

  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. typedef enum {
  18. Ready,
  19. Initializing,
  20. RepeatedStartSent,
  21. MasterTransmitter,
  22. MasterReceiver,
  23. SlaceTransmitter,
  24. SlaveReciever
  25. } TWIMode;
  26. typedef struct TWIInfoStruct{
  27. TWIMode mode;
  28. uint8_t errorCode;
  29. uint8_t repStart;
  30. }TWIInfoStruct;
  31. extern TWIInfoStruct TWIInfo;
  32. // TWI Status Codes
  33. #define TWI_START_SENT 0x08 // Start sent
  34. #define TWI_REP_START_SENT 0x10 // Repeated Start sent
  35. // Master Transmitter Mode
  36. #define TWI_MT_SLAW_ACK 0x18 // SLA+W sent and ACK received
  37. #define TWI_MT_SLAW_NACK 0x20 // SLA+W sent and NACK received
  38. #define TWI_MT_DATA_ACK 0x28 // DATA sent and ACK received
  39. #define TWI_MT_DATA_NACK 0x30 // DATA sent and NACK received
  40. // Master Receiver Mode
  41. #define TWI_MR_SLAR_ACK 0x40 // SLA+R sent, ACK received
  42. #define TWI_MR_SLAR_NACK 0x48 // SLA+R sent, NACK received
  43. #define TWI_MR_DATA_ACK 0x50 // Data received, ACK returned
  44. #define TWI_MR_DATA_NACK 0x58 // Data received, NACK returned
  45. // Miscellaneous States
  46. #define TWI_LOST_ARBIT 0x38 // Arbitration has been lost
  47. #define TWI_NO_RELEVANT_INFO 0xF8 // No relevant information available
  48. #define TWI_ILLEGAL_START_STOP 0x00 // Illegal START or STOP condition has been detected
  49. #define TWI_SUCCESS 0xFF // Successful transfer, this state is impossible from TWSR as bit2 is 0 and read only
  50. #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.
  51. #define TWISendStop() (TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN)|(1<<TWIE)) // Send the STOP signal, enable interrupts and TWI, clear TWINT flag.
  52. #define TWISendTransmit() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // Used to resume a transfer, clear TWINT and ensure that TWI and interrupts are enabled.
  53. #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.
  54. #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.
  55. // Function declarations
  56. void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking);
  57. void TWIInit(void);
  58. uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart);
  59. uint8_t isTWIReady(void);
  60. #endif // TWICOMMS_H_