]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/avr/i2c_master.c
start updating i2c for timeouts
[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, uint8_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 && (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 1; }
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 && (timer_read() - timeout_timer) > I2C_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 1;
54
55         return 0;
56 }
57
58 i2c_status_t i2c_write(uint8_t data, uint8_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 && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
68       return I2C_STATUS_TIMEOUT;
69     }
70   }
71
72         if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
73
74         return 0;
75 }
76
77 i2c_status_t i2c_read_ack(uint8_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 && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
86       return I2C_STATUS_TIMEOUT;
87     }
88   }
89
90         // return received data from TWDR
91         return TWDR;
92 }
93
94 i2c_status_t i2c_read_nack(uint8_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 && (timer_read() - timeout_timer) > I2C_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)
112 {
113         if (i2c_start(address | I2C_WRITE)) return 1;
114
115         for (uint16_t i = 0; i < length; i++)
116         {
117                 if (i2c_write(data[i])) return 1;
118         }
119
120         i2c_stop();
121
122         return 0;
123 }
124
125 uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
126 {
127         if (i2c_start(address | I2C_READ)) return 1;
128
129         for (uint16_t i = 0; i < (length-1); i++)
130         {
131                 data[i] = i2c_read_ack();
132         }
133         data[(length-1)] = i2c_read_nack();
134
135         i2c_stop();
136
137         return 0;
138 }
139
140 uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
141 {
142         if (i2c_start(devaddr | 0x00)) return 1;
143
144         i2c_write(regaddr);
145
146         for (uint16_t i = 0; i < length; i++)
147         {
148                 if (i2c_write(data[i])) return 1;
149         }
150
151         i2c_stop();
152
153         return 0;
154 }
155
156 uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
157 {
158         if (i2c_start(devaddr)) return 1;
159
160         i2c_write(regaddr);
161
162         if (i2c_start(devaddr | 0x01)) return 1;
163
164         for (uint16_t i = 0; i < (length-1); i++)
165         {
166                 data[i] = i2c_read_ack();
167         }
168         data[(length-1)] = i2c_read_nack();
169
170         i2c_stop();
171
172         return 0;
173 }
174
175 i2c_status_t i2c_stop(uint8_t timeout)
176 {
177         // transmit STOP condition
178         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
179
180   uint16_t timeout_timer = timer_read();
181   while(TWCR & (1<<TWSTO)) {
182       if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
183       return I2C_STATUS_TIMEOUT;
184     }
185   }
186
187   return 0;
188 }