Use different api name for new i2c interface. Comply all driver.
[bertos.git] / bertos / cpu / avr / drv / i2c_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Driver for the AVR ATMega TWI (implementation)
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #include <hw/hw_cpufreq.h>  /* CPU_FREQ */
40
41 #include "cfg/cfg_i2c.h"
42
43 #define LOG_LEVEL  I2C_LOG_LEVEL
44 #define LOG_FORMAT I2C_LOG_FORMAT
45
46 #include <cfg/log.h>
47
48 #include <cfg/debug.h>
49 #include <cfg/macros.h> // BV()
50 #include <cfg/module.h>
51
52 #include <cpu/detect.h>
53 #include <cpu/irq.h>
54 #include <drv/timer.h>
55 #include <drv/i2c.h>
56
57 #include <cpu/power.h>
58
59 #include <compat/twi.h>
60
61
62 /* Wait for TWINT flag set: bus is ready */
63 #define WAIT_TWI_READY  do {} while (!(TWCR & BV(TWINT)))
64
65 /**
66  * Send START condition on the bus.
67  *
68  * \return true on success, false otherwise.
69  */
70 static bool i2c_builtin_start(void)
71 {
72         TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
73         WAIT_TWI_READY;
74
75         if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
76                 return true;
77
78         LOG_ERR("!TW_(REP)START: %x\n", TWSR);
79         return false;
80 }
81
82
83 /**
84  * Send START condition and select slave for write.
85  * \c id is the device id comprehensive of address left shifted by 1.
86  * The LSB of \c id is ignored and reset to 0 for write operation.
87  *
88  * \return true on success, false otherwise.
89  */
90 bool i2c_builtin_start_w(uint8_t id)
91 {
92         /*
93          * Loop on the select write sequence: when the eeprom is busy
94          * writing previously sent data it will reply to the SLA_W
95          * control byte with a NACK.  In this case, we must
96          * keep trying until the eeprom responds with an ACK.
97          */
98         ticks_t start = timer_clock();
99         while (i2c_builtin_start())
100         {
101                 TWDR = id & ~I2C_READBIT;
102                 TWCR = BV(TWINT) | BV(TWEN);
103                 WAIT_TWI_READY;
104
105                 if (TW_STATUS == TW_MT_SLA_ACK)
106                         return true;
107                 else if (TW_STATUS != TW_MT_SLA_NACK)
108                 {
109                         LOG_ERR("!TW_MT_SLA_(N)ACK: %x\n", TWSR);
110                         break;
111                 }
112                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
113                 {
114                         LOG_ERR("Timeout on TWI_MT_START\n");
115                         break;
116                 }
117         }
118
119         return false;
120 }
121
122
123 /**
124  * Send START condition and select slave for read.
125  * \c id is the device id comprehensive of address left shifted by 1.
126  * The LSB of \c id is ignored and set to 1 for read operation.
127  *
128  * \return true on success, false otherwise.
129  */
130 bool i2c_builtin_start_r(uint8_t id)
131 {
132         if (i2c_builtin_start())
133         {
134                 TWDR = id | I2C_READBIT;
135                 TWCR = BV(TWINT) | BV(TWEN);
136                 WAIT_TWI_READY;
137
138                 if (TW_STATUS == TW_MR_SLA_ACK)
139                         return true;
140
141                 LOG_ERR("!TW_MR_SLA_ACK: %x\n", TWSR);
142         }
143
144         return false;
145 }
146
147
148 /**
149  * Send STOP condition.
150  */
151 void i2c_builtin_stop(void)
152 {
153         TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
154 }
155
156
157 /**
158  * Put a single byte in master transmitter mode
159  * to the selected slave device through the TWI bus.
160  *
161  * \return true on success, false on error.
162  */
163 bool i2c_builtin_put(const uint8_t data)
164 {
165         TWDR = data;
166         TWCR = BV(TWINT) | BV(TWEN);
167         WAIT_TWI_READY;
168         if (TW_STATUS != TW_MT_DATA_ACK)
169         {
170                 LOG_ERR("!TW_MT_DATA_ACK: %x\n", TWSR);
171                 return false;
172         }
173         return true;
174 }
175
176 /**
177  * Get 1 byte from slave in master transmitter mode
178  * to the selected slave device through the TWI bus.
179  * If \a ack is true issue a ACK after getting the byte,
180  * otherwise a NACK is issued.
181  *
182  * \return the byte read if ok, EOF on errors.
183  */
184 int i2c_builtin_get(bool ack)
185 {
186         TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0);
187         WAIT_TWI_READY;
188
189         if (ack)
190         {
191                 if (TW_STATUS != TW_MR_DATA_ACK)
192                 {
193                         LOG_ERR("!TW_MR_DATA_ACK: %x\n", TWSR);
194                         return EOF;
195                 }
196         }
197         else
198         {
199                 if (TW_STATUS != TW_MR_DATA_NACK)
200                 {
201                         LOG_ERR("!TW_MR_DATA_NACK: %x\n", TWSR);
202                         return EOF;
203                 }
204         }
205
206         /* avoid sign extension */
207         return (int)(uint8_t)TWDR;
208 }
209
210
211 MOD_DEFINE(i2c);
212
213 /**
214  * Initialize TWI module.
215  */
216 void i2c_builtin_init(void)
217 {
218         ATOMIC(
219                 /*
220                  * This is pretty useless according to AVR's datasheet,
221                  * but it helps us driving the TWI data lines on boards
222                  * where the bus pull-up resistors are missing.  This is
223                  * probably due to some unwanted interaction between the
224                  * port pin and the TWI lines.
225                  */
226 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
227                 PORTD |= BV(PD0) | BV(PD1);
228                 DDRD  |= BV(PD0) | BV(PD1);
229 #elif CPU_AVR_ATMEGA8
230                 PORTC |= BV(PC4) | BV(PC5);
231                 DDRC  |= BV(PC4) | BV(PC5);
232 #elif CPU_AVR_ATMEGA32
233                 PORTC |= BV(PC1) | BV(PC0);
234                 DDRC  |= BV(PC1) | BV(PC0);
235 #else
236                 #error Unsupported architecture
237 #endif
238
239                 /*
240                  * Set speed:
241                  * F = CPU_FREQ / (16 + 2*TWBR * 4^TWPS)
242                  */
243                 #ifndef CONFIG_I2C_FREQ
244                         #warning Using default value of 300000L for CONFIG_I2C_FREQ
245                         #define CONFIG_I2C_FREQ  300000L /* ~300 kHz */
246                 #endif
247                 #define TWI_PRESC 1       /* 4 ^ TWPS */
248
249                 TWBR = (CPU_FREQ / (2 * CONFIG_I2C_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
250                 TWSR = 0;
251                 TWCR = BV(TWEN);
252         );
253         MOD_INIT(i2c);
254 }
255
256 /*
257  * New Api
258  */
259
260
261 struct I2cHardware
262 {
263 };
264
265
266 /* Wait for TWINT flag set: bus is ready */
267 #define WAIT_READY() \
268         do { \
269                 while (!(TWCR & BV(TWINT))) \
270                         cpu_relax(); \
271         } while (0)
272
273 /**
274  * Send START condition on the bus.
275  */
276 INLINE bool i2c_hw_start(void)
277 {
278         TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
279         WAIT_READY();
280
281         if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
282                 return true;
283
284         return false;
285 }
286
287 /**
288  * Send STOP condition.
289  */
290 INLINE void i2c_hw_stop(void)
291 {
292         TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
293 }
294
295 static void i2c_avr_start(I2c *i2c, uint16_t slave_addr)
296 {
297         /*
298          * Loop on the select write sequence: when the eeprom is busy
299          * writing previously sent data it will reply to the SLA_W
300          * control byte with a NACK.  In this case, we must
301          * keep trying until the slave responds with an ACK.
302          */
303         ticks_t start = timer_clock();
304         while (i2c_hw_start())
305         {
306                 uint8_t sla_ack = 0;
307                 uint8_t sla_nack = 0;
308                 if (I2C_TEST_START(i2c->flags) == I2C_START_W)
309                 {
310                         TWDR = slave_addr & ~I2C_READBIT;
311                         sla_ack = TW_MT_SLA_ACK;
312                         sla_nack = TW_MT_SLA_NACK;
313                 }
314                 else
315                 {
316                         TWDR = slave_addr | I2C_READBIT;
317                         sla_ack = TW_MR_SLA_ACK;
318                         sla_nack = TW_MR_SLA_NACK;
319                 }
320
321                 TWCR = BV(TWINT) | BV(TWEN);
322                 WAIT_READY();
323
324                 if (TW_STATUS == sla_ack)
325                         return;
326                 else if (TW_STATUS != sla_nack)
327                 {
328                         LOG_ERR("Start addr NACK[%x]\n", TWSR);
329                         i2c->errors |= I2C_NO_ACK;
330                         i2c_hw_stop();
331                         break;
332                 }
333                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
334                 {
335                         LOG_ERR("Start timeout\n");
336                         i2c->errors |= I2C_START_TIMEOUT;
337                         i2c_hw_stop();
338                         break;
339                 }
340         }
341
342         LOG_ERR("I2c error\n");
343         i2c->errors |= I2C_ERR;
344         i2c_hw_stop();
345 }
346
347 static void i2c_avr_putc(I2c *i2c, const uint8_t data)
348 {
349
350         TWDR = data;
351         TWCR = BV(TWINT) | BV(TWEN);
352         WAIT_READY();
353
354         if (TW_STATUS != TW_MT_DATA_ACK)
355         {
356                 LOG_ERR("Data nack[%x]\n", TWSR);
357                 i2c->errors |= I2C_DATA_NACK;
358                 i2c_hw_stop();
359         }
360
361         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
362                 i2c_hw_stop();
363 }
364
365 static uint8_t i2c_avr_getc(I2c *i2c)
366 {
367         uint8_t data_flag = 0;
368         if (i2c->xfer_size == 1)
369         {
370                 TWCR = BV(TWINT) | BV(TWEN);
371                 data_flag = TW_MR_DATA_NACK;
372         }
373         else
374         {
375                 TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA);
376                 data_flag = TW_MR_DATA_ACK;
377         }
378
379         WAIT_READY();
380
381         if (TW_STATUS != TW_MR_DATA_ACK)
382         {
383                 LOG_ERR("Data nack[%x]\n", TWSR);
384                 i2c->errors |= I2C_DATA_NACK;
385                 i2c_hw_stop();
386
387                 return 0xFF;
388         }
389
390         uint8_t data = TWDR;
391
392         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
393                 i2c_hw_stop();
394
395         return data;
396 }
397
398
399 static const I2cVT i2c_avr_vt =
400 {
401         .start = i2c_avr_start,
402         .getc = i2c_avr_getc,
403         .putc = i2c_avr_putc,
404         .write = i2c_genericWrite,
405         .read = i2c_genericRead,
406 };
407
408 struct I2cHardware i2c_avr_hw[] =
409 {
410         { /* I2C0 */
411         },
412 };
413
414 /**
415  * Initialize I2C module.
416  */
417 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
418 {
419         i2c->hw = &i2c_avr_hw[dev];
420         i2c->vt = &i2c_avr_vt;
421
422         ATOMIC(
423                 /*
424                  * This is pretty useless according to AVR's datasheet,
425                  * but it helps us driving the TWI data lines on boards
426                  * where the bus pull-up resistors are missing.  This is
427                  * probably due to some unwanted interaction between the
428                  * port pin and the TWI lines.
429                  */
430         #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
431                 PORTD |= BV(PD0) | BV(PD1);
432                 DDRD  |= BV(PD0) | BV(PD1);
433         #elif CPU_AVR_ATMEGA8
434                 PORTC |= BV(PC4) | BV(PC5);
435                 DDRC  |= BV(PC4) | BV(PC5);
436         #elif CPU_AVR_ATMEGA32
437                 PORTC |= BV(PC1) | BV(PC0);
438                 DDRC  |= BV(PC1) | BV(PC0);
439         #else
440                 #error Unsupported architecture
441         #endif
442
443                 /*
444                  * Set speed:
445                  * F = CPU_FREQ / (16 + 2*TWBR * 4^TWPS)
446                  */
447                 ASSERT(clock);
448                 #define TWI_PRESC    1       /* 4 ^ TWPS */
449
450                 TWBR = (CPU_FREQ / (2 * clock * TWI_PRESC)) - (8 / TWI_PRESC);
451                 TWSR = 0;
452                 TWCR = BV(TWEN);
453         );
454
455         MOD_INIT(i2c);
456 }