Implement read functions.
[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
53 #include <io/stm32.h>
54
55 struct stm32_i2c *i2c = (struct stm32_i2c *)I2C1_BASE;
56
57 INLINE uint32_t get_status(struct stm32_i2c *base)
58 {
59         return ((base->SR1 | (base->SR2 << 16)) & 0x00FFFFFF);
60 }
61
62 /**
63  * Send START condition on the bus.
64  *
65  * \return true on success, false otherwise.
66  */
67 static bool i2c_builtin_start(void)
68 {
69         i2c->CR1 |= CR1_ACK_SET;
70         i2c->CR1 |= CR1_PE_SET;
71         i2c->CR1 |= CR1_START_SET;
72         
73         while (get_status(i2c) != I2C_EVENT_MASTER_MODE_SELECT);
74
75         return true;
76 }
77
78
79 /**
80  * Send START condition and select slave for write.
81  * \c id is the device id comprehensive of address left shifted by 1.
82  * The LSB of \c id is ignored and reset to 0 for write operation.
83  *
84  * \return true on success, false otherwise.
85  */
86 bool i2c_builtin_start_w(uint8_t id)
87 {
88         id &= OAR1_ADD0_RESET;
89
90         i2c_builtin_start();
91
92         i2c->DR = id;
93         while (get_status(i2c) != I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);
94         
95         return true;
96 }
97
98
99 /**
100  * Send START condition and select slave for read.
101  * \c id is the device id comprehensive of address left shifted by 1.
102  * The LSB of \c id is ignored and set to 1 for read operation.
103  *
104  * \return true on success, false otherwise.
105  */
106 bool i2c_builtin_start_r(uint8_t id)
107 {
108         id |=  OAR1_ADD0_SET;
109
110         i2c_builtin_start();
111
112         i2c->DR = id;
113         while (get_status(i2c) != I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED);
114
115         return true;
116 }
117
118
119 /**
120  * Send STOP condition.
121  */
122 void i2c_builtin_stop(void)
123 {
124         i2c->CR1 |= CR1_STOP_SET;
125         i2c->CR1 &= CR1_PE_RESET;
126 }
127
128
129 /**
130  * Put a single byte in master transmitter mode
131  * to the selected slave device through the TWI bus.
132  *
133  * \return true on success, false on error.
134  */
135 bool i2c_builtin_put(const uint8_t data)
136 {
137         i2c->DR = data;
138         while (get_status(i2c) != I2C_EVENT_MASTER_BYTE_TRANSMITTED);
139
140         return true;
141 }
142
143 /**
144  * Get 1 byte from slave in master transmitter mode
145  * to the selected slave device through the TWI bus.
146  * If \a ack is true issue a ACK after getting the byte,
147  * otherwise a NACK is issued.
148  *
149  * \return the byte read if ok, EOF on errors.
150  */
151 int i2c_builtin_get(bool ack)
152 {
153         while (get_status(i2c) != I2C_EVENT_MASTER_BYTE_RECEIVED);
154
155         return i2c->DR;
156 }
157
158
159 MOD_DEFINE(i2c);
160
161 /**
162  * Initialize I2C module.
163  */
164 void i2c_builtin_init(void)
165 {
166         MOD_INIT(i2c);
167
168         RCC->APB2ENR |= RCC_APB2_GPIOB;
169         RCC->APB1ENR |= RCC_APB1_I2C1;
170
171         stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_I2C1_SCL_PIN,
172                                 GPIO_MODE_AF_OD, GPIO_SPEED_50MHZ);
173
174         stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_I2C1_SDA_PIN,
175                                 GPIO_MODE_AF_OD, GPIO_SPEED_50MHZ);
176
177         i2c->CR1 = 0;
178         i2c->CR2 = 0;
179         i2c->CCR = 0;
180         i2c->TRISE = 0;
181         i2c->OAR1 = 0;
182
183         i2c->CR2 |= CR2_FREQ_36MHZ;
184
185         /* Configure spi in standard mode */
186         #if CONFIG_I2C_FREQ <= 100000
187                 i2c->CCR |= (uint16_t)((CR2_FREQ_36MHZ * 1000000) / (CONFIG_I2C_FREQ << 1));
188                 i2c->TRISE |= (CR2_FREQ_36MHZ + 1);
189         #else
190                 #error fast mode not supported
191         #endif
192
193 }