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