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.

81 lines
3.3 KiB

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