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