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