Debian Codegraph @

kpy/mh-i2c.c

raw symbols SCIP
1 /*
2 * mh-i2c.c
3 *
4 * Created: 09-Jun-19 11:20:17 AM
5 * Author: TEP SOVICHEA
6 MIT License
7
8 Copyright (c) 2019 Sovichea Tep
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27
28 */
29
30 #include <avr/io.h>
31 #include <util/twi.h>
32 #include <stdbool.h>
33
34
35 #define DEBUG_LOG 0
36 #define SUCCESS 0
37
38 // For KuttypyPlus
39 #define TW_SCL_PIN PORTC0
40 #define TW_SDA_PIN PORTC1
41
42 #define TW_SLA_W(ADDR) ((ADDR << 1) | TW_WRITE)
43 #define TW_SLA_R(ADDR) ((ADDR << 1) | TW_READ)
44 #define TW_READ_ACK 1
45 #define TW_READ_NACK 0
46
47 typedef enum {
48 TW_FREQ_100K,
49 TW_FREQ_250K,
50 TW_FREQ_400K
51 } twi_freq_mode_t;
52
53 static uint16_t tw_start(void)
54 {
55 /* Send START condition */
56 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA);
57
58 /* Wait for TWINT flag to set */
59 while (!(TWCR & (1 << TWINT)));
60
61 /* Check error */
62 if (TW_STATUS != TW_START && TW_STATUS != TW_REP_START)
63 {
64 return TW_STATUS;
65 }
66
67
68 return SUCCESS;
69 }
70
71
72 static void tw_stop(void)
73 {
74 /* Send STOP condition */
75
76 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
77 }
78
79
80 static uint16_t tw_write_sla(uint8_t sla)
81 {
82 /* Transmit slave address with read/write flag */
83
84 TWDR = sla;
85 TWCR = (1 << TWINT) | (1 << TWEN);
86
87 /* Wait for TWINT flag to set */
88 while (!(TWCR & (1 << TWINT)));
89 if (TW_STATUS != TW_MT_SLA_ACK && TW_STATUS != TW_MR_SLA_ACK)
90 {
91
92 return TW_STATUS;
93 }
94
95 return SUCCESS;
96 }
97
98
99 static uint16_t tw_write(uint8_t data)
100 {
101 /* Transmit 1 byte*/
102
103 TWDR = data;
104 TWCR = (1 << TWINT) | (1 << TWEN);
105
106 /* Wait for TWINT flag to set */
107 while (!(TWCR & (1 << TWINT)));
108 if (TW_STATUS != TW_MT_DATA_ACK)
109 {
110
111 return TW_STATUS;
112 }
113
114
115 return SUCCESS;
116 }
117
118
119 static uint8_t tw_read(bool read_ack)
120 {
121 if (read_ack)
122 {
123 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
124 while (!(TWCR & (1 << TWINT)));
125 if (TW_STATUS != TW_MR_DATA_ACK)
126 {
127 return TW_STATUS;
128 }
129 }
130 else
131 {
132 TWCR = (1 << TWINT) | (1 << TWEN);
133 while (!(TWCR & (1 << TWINT)));
134 if (TW_STATUS != TW_MR_DATA_NACK)
135 {
136 return TW_STATUS;
137 }
138 }
139 uint8_t data = TWDR;
140
141 return data;
142 }
143
144
145 void tw_init(twi_freq_mode_t twi_freq_mode, bool pullup_en)
146 {
147 DDRC |= (1 << TW_SDA_PIN) | (1 << TW_SCL_PIN);
148 if (pullup_en)
149 {
150
151 PORTC |= (1 << TW_SDA_PIN) | (1 << TW_SCL_PIN);
152 }
153 else
154 {
155 PORTC &= ~((1 << TW_SDA_PIN) | (1 << TW_SCL_PIN));
156 }
157 DDRC &= ~((1 << TW_SDA_PIN) | (1 << TW_SCL_PIN));
158
159 switch (twi_freq_mode)
160 {
161 case TW_FREQ_100K:
162 /* Set bit rate register 72 and prescaler to 1 resulting in
163 SCL_freq = 16MHz/(16 + 2*72*1) = 100KHz */
164 TWBR = 72;
165 break;
166
167 case TW_FREQ_250K:
168 /* Set bit rate register 24 and prescaler to 1 resulting in
169 SCL_freq = 16MHz/(16 + 2*24*1) = 250KHz */
170 TWBR = 24;
171 break;
172
173 case TW_FREQ_400K:
174 /* Set bit rate register 12 and prescaler to 1 resulting in
175 SCL_freq = 16MHz/(16 + 2*12*1) = 400KHz */
176 TWBR = 12;
177 break;
178
179 default: break;
180 }
181 }
182
183
184 uint16_t tw_master_transmit(uint8_t slave_addr, uint8_t* p_data, uint8_t len, bool repeat_start)
185 {
186 uint16_t error_code;
187
188 /* Send START condition */
189 error_code = tw_start();
190 if (error_code != SUCCESS)
191 {
192 return error_code;
193 }
194
195 /* Send slave address with WRITE flag */
196 error_code = tw_write_sla(TW_SLA_W(slave_addr));
197 if (error_code != SUCCESS)
198 {
199 return error_code;
200 }
201
202 /* Send data byte in single or burst mode */
203 for (int i = 0; i < len; ++i)
204 {
205 error_code = tw_write(p_data[i]);
206 if (error_code != SUCCESS)
207 {
208 return error_code;
209 }
210 }
211
212 if (!repeat_start)
213 {
214 /* Send STOP condition */
215 tw_stop();
216 }
217
218 return SUCCESS;
219 }
220
221
222 uint16_t tw_master_receive(uint8_t slave_addr, uint8_t* p_data, uint8_t len)
223 {
224 uint16_t error_code;
225
226 /* Send START condition */
227 error_code = tw_start();
228 if (error_code != SUCCESS)
229 {
230 return error_code;
231 }
232
233 /* Write slave address with READ flag */
234 error_code = tw_write_sla(TW_SLA_R(slave_addr));
235 if (error_code != SUCCESS)
236 {
237 return error_code;
238 }
239
240 /* Read single or multiple data byte and send ack */
241 for (int i = 0; i < len-1; ++i)
242 {
243 p_data[i] = tw_read(TW_READ_ACK);
244 }
245 p_data[len-1] = tw_read(TW_READ_NACK);
246
247 /* Send STOP condition */
248 tw_stop();
249
250 return SUCCESS;
251 }
252