bc0eaea17b4bb9f05af377d7f56a9cf6f0564bc3
[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 /*
58  *
59  */
60 #if 1
61         /* I2C 0 */
62         #define I2C_PCONP                    PCONP_PCI2C0
63         #define I2C_CONSET                   I20CONSET
64         #define I2C_CONCLR                   I20CONCLR
65         #define I2C_SCLH                     I20SCLH
66         #define I2C_SCLL                     I20SCLL
67         #define I2C_STAT                     I20STAT
68         #define I2C_DAT                      I20DAT
69         #define I2C_PINSEL_PORT              PINSEL1
70         #define I2C_PINSEL                   I2C0_PINSEL
71         #define I2C_PINSEL_MASK              I2C0_PINSEL_MASK
72         #define I2C_PCLKSEL                  PCLKSEL0
73         #define I2C_PCLK_MASK                I2C0_PCLK_MASK
74         #define I2C_PCLK_DIV8                I2C0_PCLK_DIV8
75 #else
76         /* I2C 1 */
77         #error
78 #endif
79
80 #define GET_STATUS()   ((uint8_t)I2C_STAT)
81 /*
82  * Wait that SI bit is set.
83  *
84  * Note: this bit is set when the I2C state changes. However, entering
85  * state F8 does not set SI since there is nothing for an interrupt service
86  * routine to do in that case.
87  */
88 #define WAIT_SI()           while( !(I2C_CONSET & BV(I2CON_SI)) )
89
90 static uint8_t i2c_builtin_start(void)
91 {
92         // Clear all pending flags.
93         I2C_CONCLR = BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
94
95         // Set start and ack bit.
96         I2C_CONSET = BV(I2CON_STA) | BV(I2CON_AA);
97
98         WAIT_SI();
99
100         return true;
101 }
102
103
104 /**
105  * Send START condition and select slave for write.
106  * \c id is the device id comprehensive of address left shifted by 1.
107  * The LSB of \c id is ignored and reset to 0 for write operation.
108  *
109  * \return true on success, false otherwise.
110  */
111 bool i2c_builtin_start_w(uint8_t id)
112 {
113         ticks_t start = timer_clock();
114         while (i2c_builtin_start())
115         {
116                 uint8_t status = GET_STATUS();
117
118                 /* Start status ok, set addres and the R/W bit */
119                 if ((status == I2C_STAT_SEND) || (status == I2C_STAT_RESEND))
120                         I2C_DAT = id & ~I2C_READBIT;
121
122                 /* Clear the start bit and clear the SI bit */
123                 I2C_CONCLR = BV(I2CON_SIC) | BV(I2CON_STAC);
124
125                 if (status == I2C_STAT_SLAW_ACK)
126                         return true;
127                 else if (status == I2C_STAT_ARB_LOST)
128                 {
129                         LOG_ERR("Arbitration lost\n");
130                         break;
131                 }
132                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
133                 {
134                         LOG_ERR("Timeout on I2C START\n");
135                         break;
136                 }
137         }
138         return false;
139 }
140
141
142 /**
143  * Send START condition and select slave for read.
144  * \c id is the device id comprehensive of address left shifted by 1.
145  * The LSB of \c id is ignored and set to 1 for read operation.
146  *
147  * \return true on success, false otherwise.
148  */
149 bool i2c_builtin_start_r(uint8_t id)
150 {
151         if (i2c_builtin_start())
152         {
153                 uint8_t status = GET_STATUS();
154
155                 /* Start status ok, set addres and the R/W bit */
156                 if ((status == I2C_STAT_SEND) || (status == I2C_STAT_RESEND))
157                         I2C_DAT = id | I2C_READBIT;
158
159                 /* Clear the start bit and clear the SI bit */
160                 I2C_CONCLR = BV(I2CON_SIC) | BV(I2CON_STAC);
161
162                 WAIT_SI();
163
164                 status = GET_STATUS();
165
166                 if (status == I2C_STAT_SLAR_ACK)
167                         return true;
168
169                 else if (status == I2C_STAT_SLAR_ACK)
170                 {
171                         LOG_ERR("SLAR NACK:%02x\n", status);
172                 }
173                 else if (status == I2C_STAT_ARB_LOST)
174                 {
175                         LOG_ERR("ARB Lost:%02x\n", status);
176                 }
177
178         }
179         return false;
180 }
181
182
183 void i2c_builtin_stop(void)
184 {
185         /* Set the stop bit */
186         I2C_CONSET = BV(I2CON_STO);
187         /* Clear pending flags */
188         I2C_CONCLR = BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
189 }
190
191
192 bool i2c_builtin_put(const uint8_t data)
193 {
194         I2C_DAT = data;
195         I2C_CONCLR = BV(I2CON_SIC);
196
197         WAIT_SI();
198
199         uint32_t status = GET_STATUS();
200
201         if (status == I2C_STAT_DATA_ACK)
202                 return true;
203         else if (status == I2C_STAT_DATA_NACK)
204         {
205                 LOG_ERR("Data NACK\n");
206                 return false;
207         }
208         else if (status == I2C_STAT_ERROR)
209         {
210                 LOG_ERR("I2C error.\n");
211                 return false;
212         }
213         else if (status == I2C_STAT_UNKNOW)
214         {
215                 LOG_ERR("I2C unable to read status.\n");
216                 return false;
217         }
218
219         return false;
220 }
221
222
223 int i2c_builtin_get(bool ack)
224 {
225
226         /*
227          * Set ack bit if we want read more byte, otherwise
228          * we disable it
229          */
230         if (ack)
231                 I2C_CONSET = BV(I2CON_AA);
232         else
233                 I2C_CONCLR = BV(I2CON_AAC);
234
235         I2C_CONCLR = BV(I2CON_SIC);
236
237         WAIT_SI();
238
239         uint32_t status = GET_STATUS();
240
241         if (status == I2C_STAT_RDATA_ACK)
242                 return (uint8_t)I2C_DAT;
243         else if (status == I2C_STAT_RDATA_NACK)
244                 return true;
245         else if (status == I2C_STAT_ERROR)
246         {
247                 LOG_ERR("I2C error.\n");
248                 return EOF;
249         }
250         else if (status == I2C_STAT_UNKNOW)
251         {
252                 LOG_ERR("I2C unable to read status.\n");
253                 return EOF;
254         }
255
256         return EOF;
257 }
258
259 /*
260  * With this function is allowed only the atomic write.
261  */
262 static bool i2c_send1(const void *_buf, size_t count)
263 {
264         const uint8_t *buf = (const uint8_t *)_buf;
265         uint8_t status = 0;
266
267         while (count)
268         {
269                 I2C_DAT = *buf++;
270                 I2C_CONCLR = BV(I2CON_SIC);
271                 count--;
272
273                 WAIT_SI();
274
275                 status = GET_STATUS();
276
277                 if (status == I2C_STAT_DATA_ACK)
278                         continue;
279                 else if (status == I2C_STAT_DATA_NACK)
280                 {
281                         LOG_ERR("Data NACK\n");
282                         return false;
283                 }
284                 else if (status == I2C_STAT_ERROR)
285                 {
286                         LOG_ERR("I2C error.\n");
287                         return false;
288                 }
289                 else if (status == I2C_STAT_UNKNOW)
290                 {
291                         LOG_ERR("I2C unable to read status.\n");
292                         return false;
293                 }
294
295         }
296
297         return true;
298 }
299
300 /**
301  * In order to read bytes from the i2c we should make some tricks.
302  */
303 static bool i2c_recv1(void *_buf, size_t count)
304 {
305         uint8_t *buf = (uint8_t *)_buf;
306         uint8_t status = GET_STATUS();
307
308         /* Ready for read */
309         I2C_CONSET = BV(I2CON_AA);
310         I2C_CONCLR = BV(I2CON_SIC);
311
312         WAIT_SI();
313
314         while (count)
315         {
316                 *buf++ = I2C_DAT;
317                 /*
318                  * Set ack bit if we want read more byte, otherwise
319                  * we disable it
320                  */
321                 if (count > 1)
322                         I2C_CONSET = BV(I2CON_AA);
323                 else
324                         I2C_CONCLR = BV(I2CON_AAC);
325
326                 I2C_CONCLR = BV(I2CON_SIC);
327                 count--;
328
329                 WAIT_SI();
330
331                 status = GET_STATUS();
332
333                 if (status == I2C_STAT_RDATA_ACK)
334                         continue;
335                 else if (status == I2C_STAT_RDATA_NACK)
336                         return true;
337                 else if (status == I2C_STAT_ERROR)
338                 {
339                         LOG_ERR("I2C error.\n");
340                         return false;
341                 }
342                 else if (status == I2C_STAT_UNKNOW)
343                 {
344                         LOG_ERR("I2C unable to read status.\n");
345                         return false;
346                 }
347         }
348
349         return true;
350 }
351
352 MOD_DEFINE(i2c);
353
354 /**
355  * Initialize I2C module.
356  */
357 void i2c_builtin_init(void)
358 {
359         /* Enable I2C clock */
360         PCONP |= BV(I2C_PCONP);
361
362         #if (CONFIG_I2C_FREQ > 400000)
363                 #error i2c frequency is to hight.
364         #endif
365
366         I2C_CONCLR = BV(I2CON_I2ENC) | BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
367
368         /*
369          * Bit Frequency = Fplk / (I2C_I2SCLH + I2C_I2SCLL)
370          * value of I2SCLH and I2SCLL must be different
371          */
372         I2C_PCLKSEL &= ~I2C_PCLK_MASK;
373         I2C_PCLKSEL |= I2C_PCLK_DIV8;
374
375         I2C_SCLH = (((CPU_FREQ / 8) / CONFIG_I2C_FREQ) / 2) + 1;
376         I2C_SCLL = (((CPU_FREQ / 8) / CONFIG_I2C_FREQ) / 2);
377
378         ASSERT(I2C_SCLH > 4 || I2C_SCLL > 4);
379
380         /* Assign pins to SCL and SDA (P0_27, P0_28) */
381         I2C_PINSEL_PORT &= ~I2C_PINSEL_MASK;
382         I2C_PINSEL_PORT |= I2C_PINSEL;
383
384         // Enable I2C
385         I2C_CONSET = BV(I2CON_I2EN);
386
387         MOD_INIT(i2c);
388 }