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