Add first implemtation of i2c for lpc23xx.
[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  */
61 #if 0
62         /* I2C 0 */
63         #define I2C                        I2C0_MASTER_BASE
64         #define SYSCTL_RCGC1_I2C           SYSCTL_RCGC1_I2C0
65         #define SYSCTL_RCGC2_GPIO          SYSCTL_RCGC2_GPIOB
66         #define GPIO_I2C_SCL_PIN           GPIO_I2C0_SCL_PIN
67         #define GPIO_I2C_SDA_PIN           GPIO_I2C0_SDA_PIN
68         #define GPIO_PORT_BASE             GPIO_PORTB_BASE
69 #else
70         /* I2C 1 */
71         #define I2C                        I2C1_MASTER_BASE
72         #define SYSCTL_RCGC1_I2C           SYSCTL_RCGC1_I2C1
73         #define SYSCTL_RCGC2_GPIO          SYSCTL_RCGC2_GPIOA
74         #define GPIO_I2C_SCL_PIN           GPIO_I2C1_SCL_PIN
75         #define GPIO_I2C_SDA_PIN           GPIO_I2C1_SDA_PIN
76         #define GPIO_PORT_BASE             GPIO_PORTA_BASE
77 #endif
78
79
80 /**
81  * Send START condition and select slave for write.
82  * \c id is the device id comprehensive of address left shifted by 1.
83  * The LSB of \c id is ignored and reset to 0 for write operation.
84  *
85  * \return true on success, false otherwise.
86  */
87 bool i2c_builtin_start_w(uint8_t id)
88 {
89
90         return true;
91 }
92
93
94 /**
95  * Send START condition and select slave for read.
96  * \c id is the device id comprehensive of address left shifted by 1.
97  * The LSB of \c id is ignored and set to 1 for read operation.
98  *
99  * \return true on success, false otherwise.
100  */
101 bool i2c_builtin_start_r(uint8_t id)
102 {
103
104         return true;
105 }
106
107
108 void i2c_builtin_stop(void)
109 {
110 }
111
112
113 bool i2c_builtin_put(const uint8_t data)
114 {
115         (void)data;
116         return true;
117 }
118
119
120 int i2c_builtin_get(bool ack)
121 {
122         (void)ack;
123         return 0;
124 }
125
126 /*
127  * With this function is allowed only the atomic write.
128  */
129 bool i2c_send(const void *_buf, size_t count)
130 {
131         const uint8_t *buf = (const uint8_t *)_buf;
132
133
134         return true;
135 }
136
137 /**
138  * In order to read bytes from the i2c we should make some tricks.
139  */
140 bool i2c_recv(void *_buf, size_t count)
141 {
142
143         return true;
144 }
145
146 MOD_DEFINE(i2c);
147
148 #define I2C_PCONP             PCONP_PCI2C0
149 #define I2C_CONSET            I20CONSET
150 #define I2C_CONCLR            I20CONCLR
151 #define I2C_SCLH              I20SCLH
152 #define I2C_SCLL              I20SCLL
153
154
155 /**
156  * Initialize I2C module.
157  */
158 void i2c_builtin_init(void)
159 {
160         PCONP |= BV(I2C_PCONP); // enable I2C0 clock
161
162         #if (CONFIG_I2C_FREQ > 400000)
163                 #error i2c frequency is to hight.
164         #endif
165
166         I2C_CONCLR = BV(I2CON_I2ENC);
167
168         // Bit Frequency = Fplk / (I2C_I2SCLH + I2C_I2SCLL)
169         // value of I2SCLH and I2SCLL must be different
170
171         PCLKSEL0 |= I2C0_PCLK_DIV8;
172         I2C_SCLH = (((CPU_FREQ / 8) / CONFIG_I2C_FREQ) / 2) + 1;
173         I2C_SCLL = (((CPU_FREQ / 8) / CONFIG_I2C_FREQ) / 2);
174
175         ASSERT(I2C_SCLH > 4 || I2C_SCLL > 4);
176
177         //I2CState = I2C_IDLE;
178
179         // Assign pins to SCL and SDA (P0_27, P0_28)
180         PINSEL0 |= BV(27) | BV(28);
181
182         // Enable I2C
183         I2C_CONSET = BV(I2CON_I2EN);
184         I2C_CONCLR |= BV(I2CON_STAC) | BV(I2CON_SIC) | BV(I2CON_AAC);
185
186 kprintf("h%ld l%ld\n", I2C_SCLH, I2C_SCLL);
187
188         MOD_INIT(i2c);
189 }