Fix/projectkb/alice/right spacebar layout size from 2.25 to 2.7… (#6984)
[jackhill/qmk/firmware.git] / drivers / avr / i2c_master.c
1 /* Copyright (C) 2019 Elia Ritterbusch
2 +
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16 /* Library made by: g4lvanix
17 * Github repository: https://github.com/g4lvanix/I2C-master-lib
18 */
19
20 #include <avr/io.h>
21 #include <util/twi.h>
22
23 #include "i2c_master.h"
24 #include "timer.h"
25 #include "wait.h"
26
27 #ifndef F_SCL
28 # define F_SCL 400000UL // SCL frequency
29 #endif
30
31 #define TWBR_val (((F_CPU / F_SCL) - 16) / 2)
32
33 void i2c_init(void) {
34 TWSR = 0; /* no prescaler */
35 TWBR = (uint8_t)TWBR_val;
36
37 #ifdef __AVR_ATmega32A__
38 // set pull-up resistors on I2C bus pins
39 PORTC |= 0b11;
40
41 // enable TWI (two-wire interface)
42 TWCR |= (1 << TWEN);
43
44 // enable TWI interrupt and slave address ACK
45 TWCR |= (1 << TWIE);
46 TWCR |= (1 << TWEA);
47 #endif
48 }
49
50 i2c_status_t i2c_start(uint8_t address, uint16_t timeout) {
51 // reset TWI control register
52 TWCR = 0;
53 // transmit START condition
54 TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
55
56 uint16_t timeout_timer = timer_read();
57 while (!(TWCR & (1 << TWINT))) {
58 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
59 return I2C_STATUS_TIMEOUT;
60 }
61 }
62
63 // check if the start condition was successfully transmitted
64 if (((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)) {
65 return I2C_STATUS_ERROR;
66 }
67
68 // load slave address into data register
69 TWDR = address;
70 // start transmission of address
71 TWCR = (1 << TWINT) | (1 << TWEN);
72
73 timeout_timer = timer_read();
74 while (!(TWCR & (1 << TWINT))) {
75 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
76 return I2C_STATUS_TIMEOUT;
77 }
78 }
79
80 // check if the device has acknowledged the READ / WRITE mode
81 uint8_t twst = TW_STATUS & 0xF8;
82 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
83 return I2C_STATUS_ERROR;
84 }
85
86 return I2C_STATUS_SUCCESS;
87 }
88
89 i2c_status_t i2c_write(uint8_t data, uint16_t timeout) {
90 // load data into data register
91 TWDR = data;
92 // start transmission of data
93 TWCR = (1 << TWINT) | (1 << TWEN);
94
95 uint16_t timeout_timer = timer_read();
96 while (!(TWCR & (1 << TWINT))) {
97 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
98 return I2C_STATUS_TIMEOUT;
99 }
100 }
101
102 if ((TW_STATUS & 0xF8) != TW_MT_DATA_ACK) {
103 return I2C_STATUS_ERROR;
104 }
105
106 return I2C_STATUS_SUCCESS;
107 }
108
109 int16_t i2c_read_ack(uint16_t timeout) {
110 // start TWI module and acknowledge data after reception
111 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
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 int16_t i2c_read_nack(uint16_t timeout) {
125 // start receiving without acknowledging reception
126 TWCR = (1 << TWINT) | (1 << TWEN);
127
128 uint16_t timeout_timer = timer_read();
129 while (!(TWCR & (1 << TWINT))) {
130 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
131 return I2C_STATUS_TIMEOUT;
132 }
133 }
134
135 // return received data from TWDR
136 return TWDR;
137 }
138
139 i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
140 i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
141
142 for (uint16_t i = 0; i < length && status >= 0; i++) {
143 status = i2c_write(data[i], timeout);
144 }
145
146 i2c_stop();
147
148 return status;
149 }
150
151 i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
152 i2c_status_t status = i2c_start(address | I2C_READ, timeout);
153
154 for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
155 status = i2c_read_ack(timeout);
156 if (status >= 0) {
157 data[i] = status;
158 }
159 }
160
161 if (status >= 0) {
162 status = i2c_read_nack(timeout);
163 if (status >= 0) {
164 data[(length - 1)] = status;
165 }
166 }
167
168 i2c_stop();
169
170 return (status < 0) ? status : I2C_STATUS_SUCCESS;
171 }
172
173 i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
174 i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
175 if (status >= 0) {
176 status = i2c_write(regaddr, timeout);
177
178 for (uint16_t i = 0; i < length && status >= 0; i++) {
179 status = i2c_write(data[i], timeout);
180 }
181 }
182
183 i2c_stop();
184
185 return status;
186 }
187
188 i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
189 i2c_status_t status = i2c_start(devaddr, timeout);
190 if (status < 0) {
191 goto error;
192 }
193
194 status = i2c_write(regaddr, timeout);
195 if (status < 0) {
196 goto error;
197 }
198
199 status = i2c_start(devaddr | 0x01, timeout);
200
201 for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
202 status = i2c_read_ack(timeout);
203 if (status >= 0) {
204 data[i] = status;
205 }
206 }
207
208 if (status >= 0) {
209 status = i2c_read_nack(timeout);
210 if (status >= 0) {
211 data[(length - 1)] = status;
212 }
213 }
214
215 error:
216 i2c_stop();
217
218 return (status < 0) ? status : I2C_STATUS_SUCCESS;
219 }
220
221 void i2c_stop(void) {
222 // transmit STOP condition
223 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
224 }