23d60a4245f0405d7f12d45c2e324c4b52a18f81
[bertos.git] / bertos / cpu / cortex-m3 / drv / i2c_stm32.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 STM32F103xx I2C driver.
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  */
37
38 #include "cfg/cfg_i2c.h"
39
40 #define LOG_LEVEL  I2C_LOG_LEVEL
41 #define LOG_FORMAT I2C_LOG_FORMAT
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 <drv/gpio_stm32.h>
49 #include <drv/irq_cm3.h>
50 #include <drv/clock_stm32.h>
51 #include <drv/i2c.h>
52 #include <drv/timer.h>
53
54 #include <io/stm32.h>
55
56 struct stm32_i2c *i2c = (struct stm32_i2c *)I2C1_BASE;
57
58
59 #define WAIT_BTF(base)        while( !(base->SR1 & BV(SR1_BTF)) )
60 #define WAIT_RXE(base)        while( !(base->SR1 & BV(SR1_RXE)) )
61
62 INLINE uint32_t get_status(struct stm32_i2c *base)
63 {
64         return ((base->SR1 | (base->SR2 << 16)) & 0x00FFFFFF);
65 }
66
67
68 INLINE bool check_i2cStatus(uint32_t event)
69 {
70         while (true)
71         {
72                 uint32_t stat = get_status(i2c);
73
74                 if (stat == event)
75                         break;
76
77                 if (stat & SR1_ERR_MASK)
78                 {
79                         LOG_ERR("[%08lx]\n", stat & SR1_ERR_MASK);
80                         i2c->SR1 &= ~SR1_ERR_MASK;
81
82                         i2c->CR1 |= CR1_START_SET;
83                         return false;
84                 }
85
86         }
87
88         return true;
89 }
90
91 /**
92  * Send START condition on the bus.
93  *
94  * \return true on success, false otherwise.
95  */
96 static bool i2c_builtin_start(void)
97 {
98
99         i2c->CR1 |= CR1_ACK_SET | CR1_PE_SET | CR1_START_SET;
100
101         if(check_i2cStatus(I2C_EVENT_MASTER_MODE_SELECT))
102                 return true;
103
104         return false;
105 }
106
107
108 /**
109  * Send START condition and select slave for write.
110  * \c id is the device id comprehensive of address left shifted by 1.
111  * The LSB of \c id is ignored and reset to 0 for write operation.
112  *
113  * \return true on success, false otherwise.
114  */
115 bool i2c_builtin_start_w(uint8_t id)
116 {
117
118         /*
119          * Loop on the select write sequence: when the eeprom is busy
120          * writing previously sent data it will reply to the SLA_W
121          * control byte with a NACK.  In this case, we must
122          * keep trying until the eeprom responds with an ACK.
123          */
124         ticks_t start = timer_clock();
125         while (i2c_builtin_start())
126         {
127                 i2c->DR = id & OAR1_ADD0_RESET;
128
129                 if(check_i2cStatus(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
130                         return true;
131
132                 if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
133                 {
134                         LOG_ERR("Timeout on I2C_START\n");
135                         break;
136                 }
137         }
138
139         return false;
140 }
141
142
143 /**
144  * Send START condition and select slave for read.
145  * \c id is the device id comprehensive of address left shifted by 1.
146  * The LSB of \c id is ignored and set to 1 for read operation.
147  *
148  * \return true on success, false otherwise.
149  */
150 bool i2c_builtin_start_r(uint8_t id)
151 {
152         i2c_builtin_start();
153
154         i2c->DR = (id | OAR1_ADD0_SET);
155
156         if(check_i2cStatus(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
157                 return true;
158
159         return false;
160 }
161
162
163 /**
164  * Send STOP condition.
165  */
166 void i2c_builtin_stop(void)
167 {
168         i2c->CR1 |= CR1_STOP_SET;
169         i2c->CR1 &= CR1_PE_RESET;
170 }
171
172
173
174 bool i2c_builtin_put(const uint8_t data)
175 {
176         i2c->DR = data;
177
178         WAIT_BTF(i2c);
179
180         if(check_i2cStatus(I2C_EVENT_MASTER_BYTE_TRANSMITTED))
181                 return true;
182
183         return false;
184 }
185
186 int i2c_builtin_get(bool ack)
187 {
188         (void)ack;
189         return EOF;
190 }
191
192 /**
193  * In order to read bytes from the i2c we should make some tricks.
194  * This because the silicon manage automatically the NACK on last byte, so to read
195  * one, two or three byte we should manage separately these cases.
196  */
197 bool i2c_recv(void *_buf, size_t count)
198 {
199         uint8_t *buf = (uint8_t *)_buf;
200
201         while (count)
202         {
203                 if (count == 1)
204                 {
205                         if(!check_i2cStatus(I2C_EVENT_MASTER_BYTE_RECEIVED))
206                                 return false;
207
208                         i2c->CR1 &= CR1_ACK_RESET;
209
210                         *buf++ = i2c->DR;
211                         count = 0;
212                 }
213                 else if (count == 2)
214                 {
215                         i2c->CR1 &= CR1_ACK_RESET;
216
217                         WAIT_BTF(i2c);
218
219                         i2c->CR1 |= CR1_STOP_SET;
220
221                         *buf++ = i2c->DR;
222                         *buf++ = i2c->DR;
223
224                         count = 0;
225                 }
226                 else
227                 {
228                         WAIT_BTF(i2c);
229                         *buf++ = i2c->DR;
230                         count--;
231                 }
232         }
233
234         return true;
235 }
236
237 MOD_DEFINE(i2c);
238
239 /**
240  * Initialize I2C module.
241  */
242 void i2c_builtin_init(void)
243 {
244         MOD_INIT(i2c);
245
246         RCC->APB2ENR |= RCC_APB2_GPIOB;
247         RCC->APB1ENR |= RCC_APB1_I2C1;
248
249         /* Set gpio to use I2C driver */
250         stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_I2C1_SCL_PIN,
251                                 GPIO_MODE_AF_OD, GPIO_SPEED_50MHZ);
252
253         stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_I2C1_SDA_PIN,
254                                 GPIO_MODE_AF_OD, GPIO_SPEED_50MHZ);
255
256
257         /* Clear all needed registers */
258         i2c->CR1 = 0;
259         i2c->CR2 = 0;
260         i2c->CCR = 0;
261         i2c->TRISE = 0;
262         i2c->OAR1 = 0;
263
264         /* Set PCLK1 frequency accornding to the master clock settings. See stm32_clock.c */
265         i2c->CR2 |= CR2_FREQ_36MHZ;
266
267         /* Configure spi in standard mode */
268         #if CONFIG_I2C_FREQ <= 100000
269                 i2c->CCR |= (uint16_t)((CR2_FREQ_36MHZ * 1000000) / (CONFIG_I2C_FREQ << 1));
270                 i2c->TRISE |= (CR2_FREQ_36MHZ + 1);
271         #else
272                 #error fast mode not supported
273         #endif
274
275 }