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