Add defines for all i2c devices.
[bertos.git] / bertos / cpu / arm / drv / i2c_lpc2.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Driver for the LPC23xx I2C (implementation)
34  *
35  */
36
37 #include "cfg/cfg_i2c.h"
38
39 #define LOG_LEVEL  I2C_LOG_LEVEL
40 #define LOG_FORMAT I2C_LOG_FORMAT
41
42 #include <cfg/log.h>
43
44 #include <cfg/debug.h>
45 #include <cfg/macros.h> // BV()
46 #include <cfg/module.h>
47
48 #include <cpu/detect.h>
49 #include <cpu/irq.h>
50
51 #include <drv/timer.h>
52 #include <drv/i2c.h>
53 #include <drv/vic_lpc2.h> /* vic_handler_t */
54
55 #include <io/lpc23xx.h>
56
57 #include <stdarg.h>
58
59
60 struct I2cHardware
61 {
62         uint32_t base;
63         uint32_t pconp;
64         uint32_t pinsel_port;
65         uint32_t pinsel;
66         uint32_t pinsel_mask;
67         uint32_t pclksel;
68         uint32_t pclk_mask;
69         uint32_t pclk_div;
70 };
71
72 /*
73  * Wait that SI bit is set.
74  *
75  * Note: this bit is set when the I2C state changes. However, entering
76  * state F8 does not set SI since there is nothing for an interrupt service
77  * routine to do in that case.
78  */
79 #define WAIT_SI(i2c) \
80         do { \
81                 ticks_t start = timer_clock(); \
82                 while( !(HWREG(i2c->hw->base + I2C_CONSET_OFF) & BV(I2CON_SI)) ) \
83                 { \
84                         if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT)) \
85                         { \
86                                 LOG_ERR("Timeout SI assert\n"); \
87                                 LOG_ERR("[%08lx]\n", HWREG(i2c->hw->base + I2C_STAT_OFF)); \
88                                 break; \
89                         } \
90                 } \
91         } while (0)
92
93 static void i2c_hw_restart(I2c *i2c)
94 {
95         // Clear all pending flags.
96         HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
97
98         // Set start and ack bit.
99         HWREG(i2c->hw->base + I2C_CONSET_OFF) = BV(I2CON_STA);
100
101         WAIT_SI(i2c);
102 }
103
104
105 static void i2c_hw_stop(I2c *i2c)
106 {
107         /* Set the stop bit */
108         HWREG(i2c->hw->base + I2C_CONSET_OFF) = BV(I2CON_STO);
109         /* Clear pending flags */
110         HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
111 }
112
113 static void i2c_lpc2_put(I2c *i2c, uint8_t data)
114 {
115         HWREG(i2c->hw->base + I2C_DAT_OFF) = data;
116         HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_SIC);
117
118         WAIT_SI(i2c);
119
120         uint32_t status = HWREG(i2c->hw->base + I2C_STAT_OFF);
121
122
123         /* Generate the stop if we finish to send all programmed bytes */
124         if (i2c->xfer_size == 1)
125         {
126                 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
127                         i2c_hw_stop(i2c);
128         }
129
130         if (status == I2C_STAT_DATA_NACK)
131         {
132                 LOG_ERR("Data NACK\n");
133                 i2c->errors |= I2C_NO_ACK;
134                 i2c_hw_stop(i2c);
135         }
136         else if ((status == I2C_STAT_ERROR) || (status == I2C_STAT_UNKNOW))
137         {
138                 LOG_ERR("I2C error.\n");
139                 i2c->errors |= I2C_ERR;
140                 i2c_hw_stop(i2c);
141         }
142 }
143
144 static uint8_t i2c_lpc2_get(I2c *i2c)
145 {
146         /*
147          * Set ack bit if we want read more byte, otherwise
148          * we disable it
149          */
150         if (i2c->xfer_size > 1)
151                 HWREG(i2c->hw->base + I2C_CONSET_OFF) = BV(I2CON_AA);
152         else
153                 HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_AAC);
154
155         HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_SIC);
156
157         WAIT_SI(i2c);
158
159         uint32_t status = HWREG(i2c->hw->base + I2C_STAT_OFF);
160         uint8_t data = (uint8_t)HWREG(i2c->hw->base + I2C_DAT_OFF);
161
162         if (status == I2C_STAT_RDATA_ACK)
163         {
164                 return data;
165         }
166         else if (status == I2C_STAT_RDATA_NACK)
167         {
168                 /*
169                  * last byte to read generate the stop if
170                  * required
171                  */
172                 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
173                         i2c_hw_stop(i2c);
174
175                 return data;
176         }
177         else if ((status == I2C_STAT_ERROR) || (status == I2C_STAT_UNKNOW))
178         {
179                 LOG_ERR("I2C error.\n");
180                 i2c->errors |= I2C_ERR;
181                 i2c_hw_stop(i2c);
182         }
183
184         return 0xFF;
185 }
186
187 MOD_DEFINE(i2c);
188
189 static void i2c_lpc2_start(struct I2c *i2c, uint16_t slave_addr)
190 {
191         if (I2C_TEST_START(i2c->flags) == I2C_START_W)
192         {
193                 ticks_t start = timer_clock();
194                 while (true)
195                 {
196                         i2c_hw_restart(i2c);
197
198                         uint8_t status = HWREG(i2c->hw->base + I2C_STAT_OFF);
199
200                         /* Start status ok, set addres and the R/W bit */
201                         if ((status == I2C_STAT_SEND) || (status == I2C_STAT_RESEND))
202                                 HWREG(i2c->hw->base + I2C_DAT_OFF) = slave_addr & ~I2C_READBIT;
203
204                         /* Clear the start bit and clear the SI bit */
205                         HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_SIC) | BV(I2CON_STAC);
206
207                         if (status == I2C_STAT_SLAW_ACK)
208                                 break;
209
210                         if (status == I2C_STAT_ARB_LOST)
211                         {
212                                 LOG_ERR("Arbitration lost\n");
213                                 i2c->errors |= I2C_ARB_LOST;
214                                 i2c_hw_stop(i2c);
215                         }
216
217                         if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
218                         {
219                                 LOG_ERR("Timeout on I2C START\n");
220                                 i2c->errors |= I2C_NO_ACK;
221                                 i2c_hw_stop(i2c);
222                                 break;
223                         }
224                 }
225         }
226         else if (I2C_TEST_START(i2c->flags) == I2C_START_R)
227         {
228                 i2c_hw_restart(i2c);
229
230                 uint8_t status = HWREG(i2c->hw->base + I2C_STAT_OFF);
231
232                 /* Start status ok, set addres and the R/W bit */
233                 if ((status == I2C_STAT_SEND) || (status == I2C_STAT_RESEND))
234                         HWREG(i2c->hw->base + I2C_DAT_OFF) = slave_addr | I2C_READBIT;
235
236                 /* Clear the start bit and clear the SI bit */
237                 HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_SIC) | BV(I2CON_STAC);
238
239                 WAIT_SI(i2c);
240
241                 status = HWREG(i2c->hw->base + I2C_STAT_OFF);
242
243                 if (status == I2C_STAT_SLAR_NACK)
244                 {
245                         LOG_ERR("SLAR NACK:%02x\n", status);
246                         i2c->errors |= I2C_NO_ACK;
247                         i2c_hw_stop(i2c);
248                 }
249
250                 if (status == I2C_STAT_ARB_LOST)
251                 {
252                         LOG_ERR("Arbitration lost\n");
253                         i2c->errors |= I2C_ARB_LOST;
254                         i2c_hw_stop(i2c);
255                 }
256         }
257         else
258         {
259                 ASSERT(0);
260         }
261 }
262
263 static const I2cVT i2c_lpc_vt =
264 {
265         .start = i2c_lpc2_start,
266         .get = i2c_lpc2_get,
267         .put = i2c_lpc2_put,
268         .send = i2c_swSend,
269         .recv = i2c_swRecv,
270 };
271
272 struct I2cHardware i2c_lpc2_hw[] =
273 {
274         { /* I2C0 */
275                 .base = I2C0_BASE_ADDR,
276                 .pconp = BV(PCONP_PCI2C0),
277                 .pinsel_port = PINSEL1_OFF,
278                 .pinsel = I2C0_PINSEL,
279                 .pinsel_mask = I2C0_PINSEL_MASK,
280                 .pclksel = PCLKSEL0_OFF,
281                 .pclk_mask = I2C0_PCLK_MASK,
282                 .pclk_div = I2C0_PCLK_DIV8,
283         },
284         { /* I2C1 */
285                 .base = I2C1_BASE_ADDR,
286                 .pconp = BV(PCONP_PCI2C1),
287                 .pinsel_port = PINSEL0_OFF,
288                 .pinsel = I2C1_PINSEL,
289                 .pinsel_mask = I2C1_PINSEL_MASK,
290                 .pclksel = PCLKSEL1_OFF,
291                 .pclk_mask = I2C1_PCLK_MASK,
292                 .pclk_div = I2C1_PCLK_DIV8,
293         },
294         { /* I2C2 */
295                 .base = I2C2_BASE_ADDR,
296                 .pconp = BV(PCONP_PCI2C2),
297                 .pinsel_port = PINSEL0_OFF,
298                 .pinsel = I2C2_PINSEL,
299                 .pinsel_mask = I2C2_PINSEL_MASK,
300                 .pclksel = PCLKSEL1_OFF,
301                 .pclk_mask = I2C2_PCLK_MASK,
302                 .pclk_div = I2C2_PCLK_DIV8,
303         },
304 };
305
306 /**
307  * Initialize I2C module.
308  */
309 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
310 {
311         i2c->hw = &i2c_lpc2_hw[dev];
312         i2c->vt = &i2c_lpc_vt;
313
314         /* Enable I2C clock */
315         PCONP |= i2c->hw->pconp;
316
317         ASSERT(clock <= 400000);
318
319         HWREG(i2c->hw->base + I2C_CONCLR_OFF) = BV(I2CON_I2ENC) | BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
320
321         /*
322          * Bit Frequency = Fplk / (I2C_I2SCLH + I2C_I2SCLL)
323          * value of I2SCLH and I2SCLL must be different
324          */
325         HWREG(SCB_BASE_ADDR + i2c->hw->pclksel) &= ~i2c->hw->pclk_mask;
326         HWREG(SCB_BASE_ADDR + i2c->hw->pclksel) |= i2c->hw->pclk_div;
327
328         HWREG(i2c->hw->base + I2C_SCLH_OFF) = (((CPU_FREQ / 8) / clock) / 2) + 1;
329         HWREG(i2c->hw->base + I2C_SCLL_OFF) = (((CPU_FREQ / 8) / clock) / 2);
330
331         ASSERT(HWREG(i2c->hw->base + I2C_SCLH_OFF) > 4);
332         ASSERT(HWREG(i2c->hw->base + I2C_SCLL_OFF) > 4);
333
334         /* Assign pins to SCL and SDA */
335         HWREG(PINSEL_BASE_ADDR + i2c->hw->pinsel_port) &= ~i2c->hw->pinsel_mask;
336         HWREG(PINSEL_BASE_ADDR + i2c->hw->pinsel_port) |= i2c->hw->pinsel;
337
338         // Enable I2C
339         HWREG(i2c->hw->base + I2C_CONSET_OFF) = BV(I2CON_I2EN);
340
341         MOD_INIT(i2c);
342 }