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