sam3 i2c: change stop function name, and read before set is not needed.
[bertos.git] / bertos / cpu / cortex-m3 / drv / i2c_sam3.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 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief TWI driver for SAM3 (implementation)
34  *
35  * Only master mode is supported.
36  *
37  * \author Stefano Fedrigo <aleph@develer.com>
38  */
39
40
41 #include "cfg/cfg_i2c.h"
42
43 #define LOG_LEVEL  I2C_LOG_LEVEL
44 #define LOG_FORMAT I2C_LOG_FORMAT
45
46 #include <cfg/log.h>
47
48 #include <hw/hw_cpufreq.h>  // CPU_FREQ
49 #include <cfg/debug.h>
50 #include <cfg/macros.h> // BV()
51 #include <cfg/module.h>
52 #include <cpu/detect.h>
53 #include <cpu/irq.h>
54 #include <cpu/power.h>
55 #include <drv/timer.h>
56 #include <drv/i2c.h>
57 #include <io/sam3.h>
58
59
60 struct I2cHardware
61 {
62         uint32_t base;
63         bool     first_xtranf;
64 };
65
66
67 INLINE bool waitTxRdy(I2c *i2c, time_t ms_timeout)
68 {
69         ticks_t start = timer_clock();
70
71         while (!(HWREG(i2c->hw->base + TWI_SR_OFF) & TWI_SR_TXRDY))
72         {
73                 if (timer_clock() - start > ms_to_ticks(ms_timeout))
74                         return false;
75                 cpu_relax();
76         }
77
78         return true;
79 }
80
81 INLINE bool waitRxRdy(I2c *i2c, time_t ms_timeout)
82 {
83         ticks_t start = timer_clock();
84
85         while (!(HWREG(i2c->hw->base + TWI_SR_OFF) & TWI_SR_RXRDY))
86         {
87                 if (timer_clock() - start > ms_to_ticks(ms_timeout))
88                         return false;
89                 cpu_relax();
90         }
91
92         return true;
93 }
94
95 INLINE void waitXferComplete(I2c *i2c)
96 {
97         while (!(HWREG(i2c->hw->base + TWI_SR_OFF) & TWI_SR_TXCOMP))
98                 cpu_relax();
99 }
100
101 /*
102  * Set STOP condition bit, to send stop after next sent byte.
103  */
104 INLINE void setStop(I2c *i2c)
105 {
106         HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_STOP;
107 }
108
109 /*
110  * The start is not performed when we call the start function
111  * because the hardware should know the first data byte to send.
112  * Generally to perform a byte send we should write the slave address
113  * in slave address register and the first byte to send in data registry.
114  * After then we can perform the start write procedure, and send really
115  * the our data. To use common bertos i2c api the really start will be
116  * performed when the user "put" or "send" its data. These tricks are hide
117  * from the driver implementation.
118  */
119 static void i2c_sam3_start(struct I2c *i2c, uint16_t slave_addr)
120 {
121         i2c->hw->first_xtranf = true;
122
123         if (I2C_TEST_START(i2c->flags) == I2C_START_R)
124                 HWREG(i2c->hw->base + TWI_MMR_OFF) = TWI_MMR_DADR(slave_addr >> 1) | TWI_MMR_MREAD;
125         else
126                 HWREG(i2c->hw->base + TWI_MMR_OFF) = TWI_MMR_DADR(slave_addr >> 1);
127 }
128
129 static void i2c_sam3_putc(I2c *i2c, const uint8_t data)
130 {
131         if (!waitTxRdy(i2c, CONFIG_I2C_START_TIMEOUT))
132         {
133                 LOG_ERR("i2c: txready timeout\n");
134                 i2c->errors |= I2C_START_TIMEOUT;
135                 return;
136         }
137
138         HWREG(i2c->hw->base + TWI_THR_OFF) = data;
139
140         // On first byte sent wait for start timeout
141         if (i2c->hw->first_xtranf && !waitTxRdy(i2c, CONFIG_I2C_START_TIMEOUT))
142         {
143                 LOG_ERR("i2c: write start timeout\n");
144                 i2c->errors |= I2C_START_TIMEOUT;
145                 setStop(i2c);
146                 waitXferComplete(i2c);
147                 return;
148         }
149         i2c->hw->first_xtranf = false;
150
151         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
152         {
153                 setStop(i2c);
154                 waitXferComplete(i2c);
155         }
156 }
157
158 static uint8_t i2c_sam3_getc(I2c *i2c)
159 {
160         uint8_t data;
161
162         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
163                 setStop(i2c);
164
165         if (i2c->hw->first_xtranf)
166         {
167                 HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_START;
168                 i2c->hw->first_xtranf = false;
169         }
170
171         if (!waitRxRdy(i2c, CONFIG_I2C_START_TIMEOUT))
172         {
173                 LOG_ERR("i2c: read start timeout\n");
174                 i2c->errors |= I2C_START_TIMEOUT;
175                 return 0xFF;
176         }
177
178         data = HWREG(i2c->hw->base + TWI_RHR_OFF);
179
180         if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
181                 waitXferComplete(i2c);
182
183         return data;
184 }
185
186 static void i2c_setClock(I2c *i2c, int clock)
187 {
188         uint32_t ck_div = 0;
189         uint32_t cl_div;
190
191         for (;;)
192         {
193                 cl_div = ((CPU_FREQ / (2 * clock)) - 4) / (1 << ck_div);
194
195                 if (cl_div <= 255)
196                         break;
197
198                 ck_div++;
199         }
200
201         ASSERT(ck_div < 8);
202         LOG_INFO("i2c: using CKDIV = %lu and CLDIV/CHDIV = %lu\n\n", ck_div, cl_div);
203
204         HWREG(i2c->hw->base + TWI_CWGR_OFF) = 0;
205         HWREG(i2c->hw->base + TWI_CWGR_OFF) = (ck_div << 16) | (cl_div << 8) | cl_div;
206 }
207
208
209 static const I2cVT i2c_sam3_vt =
210 {
211         .start = i2c_sam3_start,
212         .getc = i2c_sam3_getc,
213         .putc = i2c_sam3_putc,
214         .write = i2c_genericWrite,
215         .read = i2c_genericRead,
216 };
217
218 struct I2cHardware i2c_sam3_hw[I2C_CNT];
219
220
221 /**
222  * Initialize I2C module.
223  */
224 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
225 {
226         uint8_t dummy;
227
228         ASSERT(dev < I2C_CNT);
229
230         i2c->hw = &i2c_sam3_hw[dev];
231         i2c->vt = &i2c_sam3_vt;
232
233         // Configure I/O pins
234         pmc_periphEnable(PIOA_ID);
235
236         switch (dev)
237         {
238                 case I2C0:
239                         i2c->hw->base = TWI0_BASE;
240                         PIO_PERIPH_SEL(TWI0_PORT, BV(TWI0_TWD) | BV(TWI0_TWCK), TWI0_PERIPH);
241                         HWREG(TWI0_PORT + PIO_PDR_OFF) = BV(TWI0_TWD) | BV(TWI0_TWCK);
242                         pmc_periphEnable(TWI0_ID);
243                         break;
244                 case I2C1:
245                         i2c->hw->base = TWI1_BASE;
246                         PIO_PERIPH_SEL(TWI1_PORT, BV(TWI1_TWD) | BV(TWI1_TWCK), TWI1_PERIPH);
247                         HWREG(TWI1_PORT + PIO_PDR_OFF) = BV(TWI1_TWD) | BV(TWI1_TWCK);
248                         pmc_periphEnable(TWI1_ID);
249                         break;
250                 default:
251                         ASSERT(!"i2c: invalid dev number");
252                         return;
253         }
254
255         /*
256          * Reset sequence: enable slave mode, reset, read RHR,
257          * disable slave and master modes.
258          */
259         HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_SVEN;
260         HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_SWRST;
261         dummy = HWREG(i2c->hw->base + TWI_RHR_OFF);
262         HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_SVDIS;
263         HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_MSDIS;
264
265         // Set master mode
266         HWREG(i2c->hw->base + TWI_CR_OFF) = TWI_CR_MSEN;
267
268         i2c_setClock(i2c, clock);
269 }