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