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