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