4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief STM32F103xx I2C driver.
35 * \author Daniele Basile <asterix@develer.com>
38 #include "cfg/cfg_i2c.h"
40 #define LOG_LEVEL I2C_LOG_LEVEL
41 #define LOG_FORMAT I2C_LOG_FORMAT
44 #include <cfg/debug.h>
45 #include <cfg/macros.h> // BV()
46 #include <cfg/module.h>
48 #include <cpu/power.h>
49 #include <drv/gpio_stm32.h>
50 #include <drv/irq_cm3.h>
51 #include <drv/clock_stm32.h>
53 #include <drv/timer.h>
60 struct stm32_i2c *base;
67 #define WAIT_BTF(base) \
69 while (!(base->SR1 & BV(SR1_BTF))) \
73 #define WAIT_RXNE(base) \
75 while (!(base->SR1 & BV(SR1_RXNE))) \
79 INLINE uint32_t get_status(struct stm32_i2c *base)
81 return ((base->SR1 | (base->SR2 << 16)) & 0x00FFFFFF);
85 INLINE bool wait_event(I2c *i2c, uint32_t event)
89 uint32_t stat = get_status(i2c->hw->base);
94 if (stat & SR1_ERR_MASK)
96 i2c->hw->base->SR1 &= ~SR1_ERR_MASK;
104 static void i2c_stm32_start(struct I2c *i2c, uint16_t slave_addr)
106 i2c->hw->cached = false;
108 if (I2C_TEST_START(i2c->flags) == I2C_START_W)
111 * Loop on the select write sequence: when the eeprom is busy
112 * writing previously sent data it will reply to the SLA_W
113 * control byte with a NACK. In this case, we must
114 * keep trying until the eeprom responds with an ACK.
116 ticks_t start = timer_clock();
119 i2c->hw->base->CR1 |= CR1_ACK_SET | CR1_START_SET;
121 if(!wait_event(i2c, I2C_EVENT_MASTER_MODE_SELECT))
123 LOG_ERR("ARBIT lost\n");
124 i2c->errors |= I2C_ARB_LOST;
128 i2c->hw->base->DR = slave_addr & OAR1_ADD0_RESET;
130 if(wait_event(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
133 if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
135 LOG_ERR("Timeout on I2C START\n");
136 i2c->errors |= I2C_START_TIMEOUT;
137 i2c->hw->base->CR1 |= CR1_STOP_SET;
143 else /* (I2C_TEST_START(i2c->flags) == I2C_START_R) */
145 i2c->hw->base->CR1 |= CR1_START_SET;
147 if(!wait_event(i2c, I2C_EVENT_MASTER_MODE_SELECT))
149 LOG_ERR("ARBIT lost\n");
150 i2c->errors |= I2C_ARB_LOST;
151 i2c->hw->base->CR1 |= CR1_STOP_SET;
155 i2c->hw->base->DR = (slave_addr | OAR1_ADD0_SET);
157 if (i2c->xfer_size == 2)
158 i2c->hw->base->CR1 |= CR1_ACK_SET | CR1_POS_SET;
160 if(!wait_event(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
162 LOG_ERR("SLAR NACK:%08lx\n", get_status(i2c->hw->base));
163 i2c->errors |= I2C_NO_ACK;
164 i2c->hw->base->CR1 |= CR1_STOP_SET;
168 if (i2c->xfer_size == 1)
170 i2c->hw->base->CR1 &= CR1_ACK_RESET;
173 IRQ_SAVE_DISABLE(irq);
175 (void)i2c->hw->base->SR2;
177 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
178 i2c->hw->base->CR1 |= CR1_STOP_SET;
182 WAIT_RXNE(i2c->hw->base);
184 i2c->hw->cache[0] = i2c->hw->base->DR;
185 i2c->hw->cached = true;
187 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
188 while (i2c->hw->base->CR1 & CR1_STOP_SET);
190 i2c->hw->base->CR1 |= CR1_ACK_SET;
193 else if (i2c->xfer_size == 2)
196 IRQ_SAVE_DISABLE(irq);
198 (void)i2c->hw->base->SR2;
200 i2c->hw->base->CR1 &= CR1_ACK_RESET;
204 WAIT_BTF(i2c->hw->base);
206 IRQ_SAVE_DISABLE(irq);
208 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
209 i2c->hw->base->CR1 |= CR1_STOP_SET;
212 * We store read bytes like a fifo..
214 i2c->hw->cache[1] = i2c->hw->base->DR;
215 i2c->hw->cache[0] = i2c->hw->base->DR;
216 i2c->hw->cached = true;
220 i2c->hw->base->CR1 &= CR1_POS_RESET;
221 i2c->hw->base->CR1 |= CR1_ACK_SET;
226 static void i2c_stm32_put(I2c *i2c, const uint8_t data)
228 i2c->hw->base->DR = data;
230 WAIT_BTF(i2c->hw->base);
232 /* Generate the stop if we finish to send all programmed bytes */
233 if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
235 wait_event(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED);
236 i2c->hw->base->CR1 |= CR1_STOP_SET;
240 static uint8_t i2c_stm32_get(I2c *i2c)
244 ASSERT(i2c->xfer_size <= 2);
245 return i2c->hw->cache[i2c->xfer_size - 1];
249 WAIT_BTF(i2c->hw->base);
251 if (i2c->xfer_size == 3)
253 i2c->hw->base->CR1 &= CR1_ACK_RESET;
256 IRQ_SAVE_DISABLE(irq);
258 uint8_t data = i2c->hw->base->DR;
260 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
261 i2c->hw->base->CR1 |= CR1_STOP_SET;
263 i2c->hw->cache[1] = i2c->hw->base->DR;
267 WAIT_RXNE(i2c->hw->base);
269 i2c->hw->cache[0] = i2c->hw->base->DR;
270 i2c->hw->cached = true;
272 if (I2C_TEST_STOP(i2c->flags) == I2C_STOP)
273 while (i2c->hw->base->CR1 & CR1_STOP_SET);
278 return i2c->hw->base->DR;
283 static const I2cVT i2c_stm32_vt =
285 .start = i2c_stm32_start,
286 .get = i2c_stm32_get,
287 .put = i2c_stm32_put,
292 struct I2cHardware i2c_stm32_hw[] =
295 .base = (struct stm32_i2c *)I2C1_BASE,
296 .clk_i2c_en = RCC_APB1_I2C1,
297 .pin_mask = (GPIO_I2C1_SCL_PIN | GPIO_I2C1_SDA_PIN),
300 .base = (struct stm32_i2c *)I2C2_BASE,
301 .clk_i2c_en = RCC_APB1_I2C2,
302 .pin_mask = (GPIO_I2C2_SCL_PIN | GPIO_I2C2_SDA_PIN),
309 * Initialize I2C module.
311 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
314 i2c->hw = &i2c_stm32_hw[dev];
315 i2c->vt = &i2c_stm32_vt;
317 RCC->APB2ENR |= RCC_APB2_GPIOB;
318 RCC->APB1ENR |= i2c->hw->clk_i2c_en;
320 /* Set gpio to use I2C driver */
321 stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, i2c->hw->pin_mask,
322 GPIO_MODE_AF_OD, GPIO_SPEED_50MHZ);
324 /* Clear all needed registers */
325 i2c->hw->base->CR1 = 0;
326 i2c->hw->base->CR2 = 0;
327 i2c->hw->base->CCR = 0;
328 i2c->hw->base->TRISE = 0;
329 i2c->hw->base->OAR1 = 0;
331 /* Set PCLK1 frequency accornding to the master clock settings. See stm32_clock.c */
332 i2c->hw->base->CR2 |= CR2_FREQ_36MHZ;
334 /* Configure spi in standard mode */
335 ASSERT2(clock >= 100000, "fast mode not supported");
337 i2c->hw->base->CCR |= (uint16_t)((CR2_FREQ_36MHZ * 1000000) / (clock << 1));
338 i2c->hw->base->TRISE |= (CR2_FREQ_36MHZ + 1);
340 i2c->hw->base->CR1 |= CR1_PE_SET;