afc6a60b2947ee4ad8f74e73fd5c279b0dd16cd8
[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 #include <cpu/types.h>
51 #include <cpu/power.h>
52
53 #include <io/lm3s.h>
54
55 #include <drv/timer.h>
56 #include <drv/i2c.h>
57 #include <drv/gpio_lm3s.h>
58 #include <drv/clock_lm3s.h>
59
60
61 struct I2cHardware
62 {
63         uint32_t base;
64         uint32_t sys_cntl;
65         uint32_t sys_gpio;
66         uint32_t pin_mask;
67         uint32_t gpio_base;
68         bool first_xtranf;
69 };
70
71 #define WAIT_BUSY(base) \
72         do { \
73                 while (HWREG(base + I2C_O_MCS) & I2C_MCS_BUSY ) \
74                         cpu_relax(); \
75         } while (0);
76
77
78 /*
79  * The start is not performed when we call the start function
80  * because the hardware should know the first data byte to send.
81  * Generally to perform a byte send we should write the slave address
82  * in slave address register and the first byte to send in data registry.
83  * After then we can perform the start write procedure, and send really
84  * the our data. To use common bertos i2c api the really start will be
85  * performed when the user "put" or "send" its data. These tricks are hide
86  * from the driver implementation.
87  */
88 static void i2c_lm3s_start(struct I2c *i2c, uint16_t slave_addr)
89 {
90         i2c->hw->first_xtranf = true;
91
92         if (I2C_TEST_START(i2c->flags) == I2C_START_W)
93                 HWREG(i2c->hw->base + I2C_O_MSA) = slave_addr & ~BV(0);
94         else /* (I2C_TEST_START(i2c->flags) == I2C_START_R) */
95                 HWREG(i2c->hw->base + I2C_O_MSA) = slave_addr | BV(0);
96 }
97
98 INLINE bool wait_addrAck(I2c *i2c, uint32_t mode_mask)
99 {
100         ticks_t start = timer_clock();
101         while (1)
102         {
103                 uint32_t status = HWREG(i2c->hw->base + I2C_O_MCS);
104
105                 if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
106                         return false;
107
108                 if(status & I2C_MCS_ADRACK)
109                 {
110                         HWREG(i2c->hw->base + I2C_O_MCS) = mode_mask;
111                         WAIT_BUSY(i2c->hw->base);
112                 }
113                 else
114                         break;
115
116                 cpu_relax();
117         }
118         return true;
119 }
120
121 static void i2c_lm3s_put(I2c *i2c, const uint8_t data)
122 {
123         HWREG(i2c->hw->base + I2C_O_MDR) = data;
124
125         if (i2c->hw->first_xtranf)
126         {
127                 HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_RUN | I2C_MCS_START;
128                 while( HWREG(i2c->hw->base + I2C_O_MCS) & I2C_MCS_BUSY );
129
130                 if (!wait_addrAck(i2c, I2C_MCS_RUN | I2C_MCS_START))
131                 {
132                         LOG_ERR("Start timeout\n");
133                         i2c->errors |= I2C_START_TIMEOUT;
134                         HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_STOP;
135                         WAIT_BUSY(i2c->hw->base);
136                         return;
137                 }
138
139                 i2c->hw->first_xtranf = false;
140         }
141         else
142         {
143                 HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_RUN;
144                 WAIT_BUSY(i2c->hw->base);
145         }
146
147         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
148         {
149                 HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_STOP;
150                 WAIT_BUSY(i2c->hw->base);
151         }
152 }
153
154 static uint8_t i2c_lm3s_get(I2c *i2c)
155 {
156         uint8_t data;
157         if (i2c->hw->first_xtranf)
158         {
159                 uint32_t start_mode;
160                 if (i2c->xfer_size == 1)
161                         start_mode = I2C_MCS_RUN | I2C_MCS_START;
162                 else
163                         start_mode = I2C_MCS_ACK | I2C_MCS_RUN | I2C_MCS_START;
164
165                 HWREG(i2c->hw->base + I2C_O_MCS) = start_mode;
166                 WAIT_BUSY(i2c->hw->base);
167                 if (!wait_addrAck(i2c, start_mode))
168                 {
169                         LOG_ERR("Start timeout\n");
170                         i2c->errors |= I2C_START_TIMEOUT;
171                         HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_STOP;
172                         WAIT_BUSY(i2c->hw->base);
173                         return 0xFF;
174                 }
175
176                 data = HWREG(i2c->hw->base + I2C_O_MDR);
177                 i2c->hw->first_xtranf = false;
178         }
179         else
180         {
181                 if (i2c->xfer_size > 1)
182                         HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_ACK | I2C_MCS_RUN;
183                 else
184                         HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_RUN;
185
186                 WAIT_BUSY(i2c->hw->base);
187                 data = HWREG(i2c->hw->base + I2C_O_MDR);
188         }
189
190         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
191         {
192                 HWREG(i2c->hw->base + I2C_O_MCS) = I2C_MCS_STOP;
193                 WAIT_BUSY(i2c->hw->base);
194         }
195         return data;
196 }
197
198 MOD_DEFINE(i2c);
199
200 static const I2cVT i2c_lm3s_vt =
201 {
202         .start = i2c_lm3s_start,
203         .get = i2c_lm3s_get,
204         .put = i2c_lm3s_put,
205         .send = i2c_swSend,
206         .recv = i2c_swRecv,
207 };
208
209 struct I2cHardware i2c_lm3s_hw[] =
210 {
211         { /* I2C0 */
212                 .base = I2C0_MASTER_BASE,
213                 .sys_cntl = SYSCTL_RCGC1_I2C0,
214                 .sys_gpio = SYSCTL_RCGC2_GPIOB,
215                 .pin_mask = (GPIO_I2C0_SCL_PIN | GPIO_I2C0_SDA_PIN),
216                 .gpio_base = GPIO_PORTB_BASE,
217         },
218         { /* I2C1 */
219                 .base = I2C1_MASTER_BASE,
220                 .sys_cntl = SYSCTL_RCGC1_I2C1,
221                 .sys_gpio = SYSCTL_RCGC2_GPIOA,
222                 .pin_mask = (GPIO_I2C1_SCL_PIN | GPIO_I2C1_SDA_PIN),
223                 .gpio_base = GPIO_PORTA_BASE,
224         },
225 };
226
227 /**
228  * Initialize I2C module.
229  */
230 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
231 {
232         i2c->hw = &i2c_lm3s_hw[dev];
233         i2c->vt = &i2c_lm3s_vt;
234
235         /* Enable the peripheral clock */
236         SYSCTL_RCGC1_R |= i2c->hw->sys_cntl;
237         SYSCTL_RCGC2_R |= i2c->hw->sys_gpio;
238
239         /* Configure GPIO pins to work as I2C pins */
240         lm3s_gpioPinConfig(i2c->hw->gpio_base, i2c->hw->pin_mask,
241                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD_WPU);
242         /*
243          * Note: to set correctly the i2c speed we shold before enable the i2c
244          * device and then set in master time period the correct value
245          */
246
247         /* Enable i2c device */
248         HWREG(i2c->hw->base + I2C_O_MCR) |= I2C_MCR_MFE;
249
250     /*
251          * Compute the clock divider that achieves the fastest speed less than or
252      * equal to the desired speed.  The numerator is biased to favor a larger
253      * clock divider so that the resulting clock is always less than or equal
254      * to the desired clock, never greater.
255          */
256     HWREG(i2c->hw->base + I2C_O_MTPR) = ((CPU_FREQ + (2 * 10 * clock) - 1) / (2 * 10 * clock)) - 1;
257
258         MOD_INIT(i2c);
259 }