Add configurable option to choose at compile time which i2c backend to use.
[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  * \version $Id$
36  *
37  * \author Stefano Fedrigo <aleph@develer.com>
38  * \author Bernie Innocenti <bernie@codewiz.org>
39  */
40
41 #include "hw/hw_cpu.h"  /* CLOCK_FREQ */
42
43 #include "cfg/cfg_i2c.h"
44
45 #define LOG_LEVEL  I2C_LOG_LEVEL
46 #define LOG_FORMAT I2C_LOG_FORMAT
47
48 #include <cfg/log.h>
49
50 #include <cfg/debug.h>
51 #include <cfg/macros.h> // BV()
52 #include <cfg/module.h>
53
54 #include <cpu/detect.h>
55 #include <cpu/irq.h>
56 #include <drv/timer.h>
57 #include <drv/i2c.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 MOD_DEFINE(i2c);
211
212 /**
213  * Initialize TWI module.
214  */
215 void i2c_builtin_init(void)
216 {
217         ATOMIC(
218                 /*
219                  * This is pretty useless according to AVR's datasheet,
220                  * but it helps us driving the TWI data lines on boards
221                  * where the bus pull-up resistors are missing.  This is
222                  * probably due to some unwanted interaction between the
223                  * port pin and the TWI lines.
224                  */
225 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
226                 PORTD |= BV(PD0) | BV(PD1);
227                 DDRD  |= BV(PD0) | BV(PD1);
228 #elif CPU_AVR_ATMEGA8
229                 PORTC |= BV(PC4) | BV(PC5);
230                 DDRC  |= BV(PC4) | BV(PC5);
231 #elif CPU_AVR_ATMEGA32
232                 PORTC |= BV(PC1) | BV(PC0);
233                 DDRC  |= BV(PC1) | BV(PC0);
234 #else
235                 #error Unsupported architecture
236 #endif
237
238                 /*
239                  * Set speed:
240                  * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
241                  */
242                 #ifndef CONFIG_I2C_FREQ
243                         #warning Using default value of 300000L for CONFIG_I2C_FREQ
244                         #define CONFIG_I2C_FREQ  300000L /* ~300 kHz */
245                 #endif
246                 #define TWI_PRESC 1       /* 4 ^ TWPS */
247
248                 TWBR = (CLOCK_FREQ / (2 * CONFIG_I2C_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
249                 TWSR = 0;
250                 TWCR = BV(TWEN);
251         );
252         MOD_INIT(i2c);
253 }