Refactor the driver for new api.
[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         int dev;
63 };
64
65 /*
66  *
67  */
68 #if 1
69         /* I2C 0 */
70         #define I2C_PCONP                    PCONP_PCI2C0
71         #define I2C_CONSET                   I20CONSET
72         #define I2C_CONCLR                   I20CONCLR
73         #define I2C_SCLH                     I20SCLH
74         #define I2C_SCLL                     I20SCLL
75         #define I2C_STAT                     I20STAT
76         #define I2C_DAT                      I20DAT
77         #define I2C_PINSEL_PORT              PINSEL1
78         #define I2C_PINSEL                   I2C0_PINSEL
79         #define I2C_PINSEL_MASK              I2C0_PINSEL_MASK
80         #define I2C_PCLKSEL                  PCLKSEL0
81         #define I2C_PCLK_MASK                I2C0_PCLK_MASK
82         #define I2C_PCLK_DIV8                I2C0_PCLK_DIV8
83 #else
84         /* I2C 1 */
85         #error
86 #endif
87
88 #define GET_STATUS()   ((uint8_t)I2C_STAT)
89 /*
90  * Wait that SI bit is set.
91  *
92  * Note: this bit is set when the I2C state changes. However, entering
93  * state F8 does not set SI since there is nothing for an interrupt service
94  * routine to do in that case.
95  */
96 #define WAIT_SI() \
97         do { \
98                 ticks_t start = timer_clock(); \
99                 while( !(I2C_CONSET & BV(I2CON_SI)) ) \
100                 { \
101                         if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT)) \
102                         { \
103                                 LOG_ERR("Timeout SI assert\n"); \
104                                 break; \
105                         } \
106                 } \
107         } while (0)
108
109 static void i2c_hw_restart(void)
110 {
111         // Clear all pending flags.
112         I2C_CONCLR = BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
113
114         // Set start and ack bit.
115         I2C_CONSET = BV(I2CON_STA);
116
117         WAIT_SI();
118 }
119
120
121 static void i2c_hw_stop(void)
122 {
123         /* Set the stop bit */
124         I2C_CONSET = BV(I2CON_STO);
125         /* Clear pending flags */
126         I2C_CONCLR = BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
127 }
128
129 static void i2c_lpc2_put(I2c *i2c, uint8_t data)
130 {
131         I2C_DAT = data;
132
133 //      kprintf("w %02x\n", data);
134
135         I2C_CONCLR = BV(I2CON_SIC);
136
137         WAIT_SI();
138
139         uint32_t status = GET_STATUS();
140         //kprintf("w %08lx\n", status);
141
142         /* if (status == I2C_STAT_DATA_ACK) */
143
144         /* Generate the stop if we finish to send all programmed bytes */
145         if (i2c->xfer_size == 1)
146         {
147                 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
148                         i2c_hw_stop();
149         }
150
151         if (status == I2C_STAT_DATA_NACK)
152         {
153                 LOG_ERR("Data NACK\n");
154                 i2c->errors |= I2C_NO_ACK;
155                 i2c_hw_stop();
156         }
157         else if ((status == I2C_STAT_ERROR) || (status == I2C_STAT_UNKNOW))
158         {
159                 LOG_ERR("I2C error.\n");
160                 i2c->errors |= I2C_ERR;
161                 i2c_hw_stop();
162         }
163 }
164
165 static uint8_t i2c_lpc2_get(I2c *i2c)
166 {
167         /*
168          * Set ack bit if we want read more byte, otherwise
169          * we disable it
170          */
171         if (i2c->xfer_size > 1)
172                 I2C_CONSET = BV(I2CON_AA);
173         else
174                 I2C_CONCLR = BV(I2CON_AAC);
175
176         I2C_CONCLR = BV(I2CON_SIC);
177
178         WAIT_SI();
179
180         uint32_t status = GET_STATUS();
181         uint8_t data = (uint8_t)(I2C_DAT & 0xFF);
182
183 //      kprintf("r %02x\n", data);
184 //      kprintf("r %08lx\n", status);
185
186         if (status == I2C_STAT_RDATA_ACK)
187         {
188                 return data;
189         }
190         else if (status == I2C_STAT_RDATA_NACK)
191         {
192                 /*
193                  * last byte to read generate the stop if
194                  * required
195                  */
196                 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
197                         i2c_hw_stop();
198
199                 return data;
200         }
201         else if ((status == I2C_STAT_ERROR) || (status == I2C_STAT_UNKNOW))
202         {
203                 LOG_ERR("I2C error.\n");
204                 i2c->errors |= I2C_ERR;
205                 i2c_hw_stop();
206         }
207
208         return 0xFF;
209 }
210
211 MOD_DEFINE(i2c);
212
213 static void i2c_lpc2_start(struct I2c *i2c, uint16_t slave_addr)
214 {
215         if (I2C_TEST_START(i2c->flags) == I2C_START_W)
216         {
217                 ticks_t start = timer_clock();
218                 while (true)
219                 {
220                         i2c_hw_restart();
221
222                         uint8_t status = GET_STATUS();
223
224                         /* Start status ok, set addres and the R/W bit */
225                         if ((status == I2C_STAT_SEND) || (status == I2C_STAT_RESEND))
226                                 I2C_DAT = slave_addr & ~I2C_READBIT;
227
228                         /* Clear the start bit and clear the SI bit */
229                         I2C_CONCLR = BV(I2CON_SIC) | BV(I2CON_STAC);
230
231                         if (status == I2C_STAT_SLAW_ACK)
232                                 break;
233                         else if (status == I2C_STAT_ARB_LOST)
234                         {
235                                 LOG_ERR("Arbitration lost\n");
236                                 i2c->errors |= I2C_ARB_LOST;
237                                 i2c_hw_stop();
238                                 break;
239                         }
240
241                         if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
242                         {
243                                 LOG_ERR("Timeout on I2C START\n");
244                                 i2c->errors |= I2C_NO_ACK;
245                                 i2c_hw_stop();
246                                 break;
247                         }
248                 }
249         }
250         else if (I2C_TEST_START(i2c->flags) == I2C_START_R)
251         {
252                 i2c_hw_restart();
253
254                 uint8_t status = GET_STATUS();
255
256                 /* Start status ok, set addres and the R/W bit */
257                 if ((status == I2C_STAT_SEND) || (status == I2C_STAT_RESEND))
258                         I2C_DAT = slave_addr | I2C_READBIT;
259
260                 /* Clear the start bit and clear the SI bit */
261                 I2C_CONCLR = BV(I2CON_SIC) | BV(I2CON_STAC);
262
263                 WAIT_SI();
264
265                 status = GET_STATUS();
266 /*
267                 if (status == I2C_STAT_SLAR_ACK)
268                         break;
269 */
270                 if (status == I2C_STAT_SLAR_NACK)
271                 {
272                         LOG_ERR("SLAR NACK:%02x\n", status);
273                         i2c->errors |= I2C_NO_ACK;
274                         i2c_hw_stop();
275                 }
276                 else if (status == I2C_STAT_ARB_LOST)
277                 {
278                         LOG_ERR("Arbitration lost\n");
279                         i2c->errors |= I2C_ARB_LOST;
280                         i2c_hw_stop();
281                 }
282         }
283         else
284         {
285                 ASSERT(0);
286         }
287
288 }
289
290 static const I2cVT i2c_lpc_vt =
291 {
292         .start = i2c_lpc2_start,
293         .get = i2c_lpc2_get,
294         .put = i2c_lpc2_put,
295         .send = i2c_swSend,
296         .recv = i2c_swRecv,
297 };
298
299 struct I2cHardware i2c_lpc2_hw =
300 {
301         .dev = 0,
302 };
303
304
305 /**
306  * Initialize I2C module.
307  */
308 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
309 {
310         i2c->hw = &i2c_lpc2_hw;
311         i2c->vt = &i2c_lpc_vt;
312
313         /* Enable I2C clock */
314         PCONP |= BV(I2C_PCONP);
315
316         ASSERT(clock <= 400000);
317
318         I2C_CONCLR = BV(I2CON_I2ENC) | BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
319
320         /*
321          * Bit Frequency = Fplk / (I2C_I2SCLH + I2C_I2SCLL)
322          * value of I2SCLH and I2SCLL must be different
323          */
324         I2C_PCLKSEL &= ~I2C_PCLK_MASK;
325         I2C_PCLKSEL |= I2C_PCLK_DIV8;
326
327         I2C_SCLH = (((CPU_FREQ / 8) / clock) / 2) + 1;
328         I2C_SCLL = (((CPU_FREQ / 8) / clock) / 2);
329
330         ASSERT(I2C_SCLH > 4 || I2C_SCLL > 4);
331
332         /* Assign pins to SCL and SDA (P0_27, P0_28) */
333         I2C_PINSEL_PORT &= ~I2C_PINSEL_MASK;
334         I2C_PINSEL_PORT |= I2C_PINSEL;
335
336         // Enable I2C
337         I2C_CONSET = BV(I2CON_I2EN);
338
339         MOD_INIT(i2c);
340 }