7d699ad5c345070e4a6a0343692673dbe6eaeb36
[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 | BV(0);
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         return true;
145 }
146
147 /*
148  * With this function is allowed only the atomic write.
149  */
150 bool i2c_send(const void *_buf, size_t count)
151 {
152         const uint8_t *buf = (const uint8_t *)_buf;
153
154         HWREGB(I2C + I2C_O_MDR) = *buf++;
155
156         if (count == 1)
157         {
158                 count = 0;
159
160                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_SINGLE_SEND;
161
162                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
163
164                 if ( !check_ack(I2C_MASTER_CMD_SINGLE_SEND) )
165                 {
166                         LOG_ERR("No single send start");
167                         return false;
168                 }
169         }
170         else
171         {
172                 count--;
173                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_START;
174
175                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
176
177                 if ( !check_ack(I2C_MASTER_CMD_BURST_SEND_START) )
178                 {
179                         LOG_ERR("No burst send start");
180                         return false;
181                 }
182
183                 while(count - 1)
184                 {
185                         HWREGB(I2C + I2C_O_MDR) = *buf++;
186                         count--;
187                         HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_CONT;
188                         while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
189
190                 }
191                 HWREGB(I2C + I2C_O_MDR) = *buf++;
192                 count--;
193
194                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_SEND_FINISH;
195                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
196         }
197
198         return true;
199 }
200
201 /**
202  * In order to read bytes from the i2c we should make some tricks.
203  */
204 bool i2c_recv(void *_buf, size_t count)
205 {
206         uint8_t *buf = (uint8_t *)_buf;
207
208         if (count == 1)
209         {
210                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_SINGLE_RECEIVE;
211                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
212
213                 if ( !check_ack(I2C_MASTER_CMD_SINGLE_RECEIVE) )
214                         return false;
215
216                 *buf++ = HWREG(I2C + I2C_O_MDR);
217                 count = 0;
218         }
219         else
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
237                         *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
238                         count--;
239                 }
240
241                 HWREG(I2C + I2C_O_MCS) = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;
242                 while( HWREG(I2C + I2C_O_MCS) & I2C_MCS_BUSY );
243
244                 *buf++ = (uint8_t)HWREG(I2C + I2C_O_MDR);
245                 count--;
246         }
247
248         return true;
249 }
250
251 MOD_DEFINE(i2c);
252
253 /**
254  * Initialize I2C module.
255  */
256 void i2c_builtin_init(void)
257 {
258
259         /* Enable the peripheral clock */
260         SYSCTL_RCGC1_R |= SYSCTL_RCGC1_I2C;
261         SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIO;
262
263         /* Configure GPIO pins to work as I2C pins */
264         lm3s_gpioPinConfig(GPIO_PORT_BASE, GPIO_I2C_SCL_PIN,
265                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
266
267         lm3s_gpioPinConfig(GPIO_PORT_BASE, GPIO_I2C_SDA_PIN,
268                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
269
270
271         /*
272          * Note: to set correctly the i2c speed we shold before enable the i2c
273          * device and then set in master time period the correct value
274          */
275
276         /* Enable i2c device */
277         HWREG(I2C + I2C_O_MCR) |= I2C_MCR_MFE;
278
279     /*
280          * Compute the clock divider that achieves the fastest speed less than or
281      * equal to the desired speed.  The numerator is biased to favor a larger
282      * clock divider so that the resulting clock is always less than or equal
283      * to the desired clock, never greater.
284          */
285     HWREG(I2C + I2C_O_MTPR) = ((CPU_FREQ + (2 * 10 * CONFIG_I2C_FREQ) - 1) / (2 * 10 * CONFIG_I2C_FREQ)) - 1;
286
287
288
289         MOD_INIT(i2c);
290 }