]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/avr/i2c_master.c
a04e6570d7645c23aa07a7ac333ceb1477bb1021
[qmk_firmware.git] / drivers / avr / i2c_master.c
1 /* Library made by: g4lvanix
2  * Github repository: https://github.com/g4lvanix/I2C-master-lib
3  */
4
5 #include <avr/io.h>
6 #include <util/twi.h>
7
8 #include "i2c_master.h"
9 #include "timer.h"
10
11 #ifndef F_SCL
12 #define F_SCL 400000UL // SCL frequency
13 #endif
14 #define Prescaler 1
15 #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
16
17 void i2c_init(void)
18 {
19   TWSR = 0;     /* no prescaler */
20   TWBR = (uint8_t)TWBR_val;
21 }
22
23 i2c_status_t i2c_start(uint8_t address, uint16_t timeout)
24 {
25   // reset TWI control register
26   TWCR = 0;
27   // transmit START condition
28   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
29
30   uint16_t timeout_timer = timer_read();
31   while( !(TWCR & (1<<TWINT)) ) {
32     if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
33       return I2C_STATUS_TIMEOUT;
34     }
35   }
36
37   // check if the start condition was successfully transmitted
38   if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return I2C_STATUS_ERROR; }
39
40   // load slave address into data register
41   TWDR = address;
42   // start transmission of address
43   TWCR = (1<<TWINT) | (1<<TWEN);
44
45   timeout_timer = timer_read();
46   while( !(TWCR & (1<<TWINT)) ) {
47     if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
48       return I2C_STATUS_TIMEOUT;
49     }
50   }
51
52   // check if the device has acknowledged the READ / WRITE mode
53   uint8_t twst = TW_STATUS & 0xF8;
54   if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return I2C_STATUS_ERROR;
55
56   return I2C_STATUS_SUCCESS;
57 }
58
59 i2c_status_t i2c_write(uint8_t data, uint16_t timeout)
60 {
61   // load data into data register
62   TWDR = data;
63   // start transmission of data
64   TWCR = (1<<TWINT) | (1<<TWEN);
65
66   uint16_t timeout_timer = timer_read();
67   while( !(TWCR & (1<<TWINT)) ) {
68     if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
69       return I2C_STATUS_TIMEOUT;
70     }
71   }
72
73   if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return I2C_STATUS_ERROR; }
74
75   return I2C_STATUS_SUCCESS;
76 }
77
78 int16_t i2c_read_ack(uint16_t timeout)
79 {
80
81   // start TWI module and acknowledge data after reception
82   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
83
84   uint16_t timeout_timer = timer_read();
85   while( !(TWCR & (1<<TWINT)) ) {
86     if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
87       return I2C_STATUS_TIMEOUT;
88     }
89   }
90
91   // return received data from TWDR
92   return TWDR;
93 }
94
95 int16_t i2c_read_nack(uint16_t timeout)
96 {
97
98   // start receiving without acknowledging reception
99   TWCR = (1<<TWINT) | (1<<TWEN);
100
101   uint16_t timeout_timer = timer_read();
102   while( !(TWCR & (1<<TWINT)) ) {
103     if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
104       return I2C_STATUS_TIMEOUT;
105     }
106   }
107
108   // return received data from TWDR
109   return TWDR;
110 }
111
112 i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
113 {
114   i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
115   if (status) return status;
116
117   for (uint16_t i = 0; i < length; i++) {
118     status = i2c_write(data[i], timeout);
119     if (status) return status;
120   }
121
122   status = i2c_stop(timeout);
123   if (status) return status;
124
125   return I2C_STATUS_SUCCESS;
126 }
127
128 i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
129 {
130   i2c_status_t status = i2c_start(address | I2C_READ, timeout);
131   if (status) return status;
132
133   for (uint16_t i = 0; i < (length-1); i++) {
134     status = i2c_read_ack(timeout);
135     if (status >= 0) {
136       data[i] = status;
137     } else {
138       return status;
139     }
140   }
141
142   status = i2c_read_nack(timeout);
143   if (status >= 0 ) {
144     data[(length-1)] = status;
145   } else {
146     return status;
147   }
148
149   status = i2c_stop(timeout);
150   if (status) return status;
151
152   return I2C_STATUS_SUCCESS;
153 }
154
155 i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
156 {
157   i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
158   if (status) return status;
159
160   status = i2c_write(regaddr, timeout);
161   if (status) return status;
162
163   for (uint16_t i = 0; i < length; i++) {
164     status = i2c_write(data[i], timeout);
165     if (status) return status;
166   }
167
168   status = i2c_stop(timeout);
169   if (status) return status;
170
171   return I2C_STATUS_SUCCESS;
172 }
173
174 i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
175 {
176   i2c_status_t status = i2c_start(devaddr, timeout);
177   if (status) return status;
178
179   status = i2c_write(regaddr, timeout);
180   if (status) return status;
181
182   status = i2c_start(devaddr | 0x01, timeout);
183   if (status) return status;
184
185   for (uint16_t i = 0; i < (length-1); i++) {
186     status = i2c_read_ack(timeout);
187     if (status >= 0) {
188       data[i] = status;
189     } else {
190       return status;
191     }
192   }
193
194   status = i2c_read_nack(timeout);
195   if (status >= 0 ) {
196     data[(length-1)] = status;
197   } else {
198     return status;
199   }
200
201   status = i2c_stop(timeout);
202   if (status) return status;
203
204   return I2C_STATUS_SUCCESS;
205 }
206
207 i2c_status_t i2c_stop(uint16_t timeout)
208 {
209   // transmit STOP condition
210   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
211
212   uint16_t timeout_timer = timer_read();
213   while(TWCR & (1<<TWSTO)) {
214     if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
215       return I2C_STATUS_TIMEOUT;
216     }
217   }
218
219   return I2C_STATUS_SUCCESS;
220 }