Fix the i2c implemtation for lm3s. Add some defines to use also the second i2c device.
[bertos.git] / bertos / cpu / cortex-m3 / drv / i2c_lm3s.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 LM3S 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 <io/cm3_types.h>
52 #include <io/lm3s.h>
53
54 #include <drv/timer.h>
55 #include <drv/i2c.h>
56 #include <drv/gpio_lm3s.h>
57 #include <drv/clock_lm3s.h>
58
59 /*
60  *
61  */
62 #if 0
63         /* I2C 0 */
64         #define I2C                        I2C0_MASTER_BASE
65         #define SYSCTL_RCGC1_I2C           SYSCTL_RCGC1_I2C0
66         #define SYSCTL_RCGC2_GPIO          SYSCTL_RCGC2_GPIOB
67         #define GPIO_I2C_SCL_PIN           GPIO_I2C0_SCL_PIN
68         #define GPIO_I2C_SDA_PIN           GPIO_I2C0_SDA_PIN
69         #define GPIO_PORT_BASE             GPIO_PORTB_BASE
70 #else
71         /* I2C 1 */
72         #define I2C                        I2C1_MASTER_BASE
73         #define SYSCTL_RCGC1_I2C           SYSCTL_RCGC1_I2C1
74         #define SYSCTL_RCGC2_GPIO          SYSCTL_RCGC2_GPIOA
75         #define GPIO_I2C_SCL_PIN           GPIO_I2C1_SCL_PIN
76         #define GPIO_I2C_SDA_PIN           GPIO_I2C1_SDA_PIN
77         #define GPIO_PORT_BASE             GPIO_PORTA_BASE
78 #endif
79
80
81 /**
82  * Send START condition and select slave for write.
83  * \c id is the device id comprehensive of address left shifted by 1.
84  * The LSB of \c id is ignored and reset to 0 for write operation.
85  *
86  * \return true on success, false otherwise.
87  */
88 bool i2c_builtin_start_w(uint8_t id)
89 {
90         HWREG(I2C + I2C_O_MSA) = id & ~BV(0);
91         return true;
92 }
93
94
95 /**
96  * Send START condition and select slave for read.
97  * \c id is the device id comprehensive of address left shifted by 1.
98  * The LSB of \c id is ignored and set to 1 for read operation.
99  *
100  * \return true on success, false otherwise.
101  */
102 bool i2c_builtin_start_r(uint8_t id)
103 {
104         HWREG(I2C + I2C_O_MSA) = id | 1;
105         return true;
106 }
107
108
109 void i2c_builtin_stop(void)
110 {
111 }
112
113
114 bool i2c_builtin_put(const uint8_t data)
115 {
116         (void)data;
117         return true;
118 }
119
120
121 int i2c_builtin_get(bool ack)
122 {
123         (void)ack;
124         return 0;
125 }
126
127 INLINE bool check_ack(uint32_t mode_mask)
128 {
129         ticks_t start = timer_clock();
130         while ( HWREG(I2C + I2C_O_MCS) &  I2C_MCS_ADRACK )
131         {
132                 if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
133                 {
134                         LOG_ERR("Timeout on I2C_START\n");
135                         return false;
136                 }
137
138                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_ERROR_STOP;
139
140                 HWREG(I2C + I2C_O_MCS) = mode_mask;
141                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
142
143         }
144
145         return true;
146 }
147
148 /*
149  * With this function is allowed only the atomic write.
150  */
151 bool i2c_send(const void *_buf, size_t count)
152 {
153         const uint8_t *buf = (const uint8_t *)_buf;
154
155         if (count == 1)
156         {
157                 HWREGB(I2C + I2C_O_MDR) = *buf;
158
159                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_SINGLE_SEND;
160
161                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
162
163                 if ( !check_ack(I2C_MASTER_CMD_SINGLE_SEND) )
164                         return false;
165
166                 count = 0;
167                 buf++;
168         }
169
170         if (count > 1)
171         {
172                 HWREGB(I2C + I2C_O_MDR) = *buf++;
173                 count--;
174
175                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_START;
176
177                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
178
179                 if ( !check_ack(I2C_MASTER_CMD_BURST_SEND_START) )
180                         return false;
181
182                 while(count - 1)
183                 {
184                         HWREGB(I2C + I2C_O_MDR) = *buf++;
185                         count--;
186                         HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_CONT;
187                         while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
188
189                 }
190
191                 HWREGB(I2C + I2C_O_MDR) = *buf++;
192
193                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_FINISH;
194                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
195         }
196
197         return true;
198 }
199
200 /**
201  * In order to read bytes from the i2c we should make some tricks.
202  */
203 bool i2c_recv(void *_buf, size_t count)
204 {
205         uint8_t *buf = (uint8_t *)_buf;
206
207         if (count == 1)
208         {
209                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_SINGLE_RECEIVE;
210                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
211
212                 if ( !check_ack(I2C_MASTER_CMD_SINGLE_RECEIVE) )
213                         return false;
214
215                 *buf++ = HWREG(I2C + I2C_O_MDR);
216                 count = 0;
217         }
218
219         if (count > 1)
220         {
221
222                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_START;
223                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
224
225                 if ( !check_ack(I2C_MASTER_CMD_BURST_RECEIVE_START) )
226                         return false;
227
228                 *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
229                 count--;
230
231                 while(count - 1)
232                 {
233
234                         HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_CONT;
235                         while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
236                         *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
237                         count--;
238                 }
239
240                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;
241                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
242
243                 *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
244                 count--;
245         }
246
247         return true;
248 }
249
250 MOD_DEFINE(i2c);
251
252 /**
253  * Initialize I2C module.
254  */
255 void i2c_builtin_init(void)
256 {
257
258         /* Enable the peripheral clock */
259         SYSCTL_RCGC1_R |= SYSCTL_RCGC1_I2C;
260         SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIO;
261
262         /* Configure GPIO pins to work as I2C pins */
263         lm3s_gpioPinConfig(GPIO_PORT_BASE, GPIO_I2C_SCL_PIN,
264                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
265
266         lm3s_gpioPinConfig(GPIO_PORT_BASE, GPIO_I2C_SDA_PIN,
267                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
268
269     /*
270          * Compute the clock divider that achieves the fastest speed less than or
271      * equal to the desired speed.  The numerator is biased to favor a larger
272      * clock divider so that the resulting clock is always less than or equal
273      * to the desired clock, never greater.
274          */
275     HWREG(I2C + I2C_O_MTPR) = ((CPU_FREQ + (2 * 10 * CONFIG_I2C_FREQ) - 1) / (2 * 10 * CONFIG_I2C_FREQ)) - 1;
276
277
278         //Enable I2C in master mode
279         HWREG(I2C + I2C_O_MCR) |= I2C_MCR_MFE;
280
281         MOD_INIT(i2c);
282 }