]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/i2c_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F0 / i2c_api.c
1 /* mbed Microcontroller Library
2  *******************************************************************************
3  * Copyright (c) 2014, STMicroelectronics
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  * 3. Neither the name of STMicroelectronics nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *******************************************************************************
29  */
30 #include "mbed_assert.h"
31 #include "i2c_api.h"
32
33 #if DEVICE_I2C
34
35 #include "cmsis.h"
36 #include "pinmap.h"
37 #include "PeripheralPins.h"
38
39 /* Timeout values for flags and events waiting loops. These timeouts are
40    not based on accurate values, they just guarantee that the application will
41    not remain stuck if the I2C communication is corrupted. */
42 #define FLAG_TIMEOUT ((int)0x1000)
43 #define LONG_TIMEOUT ((int)0x8000)
44
45 I2C_HandleTypeDef I2cHandle;
46
47 int i2c1_inited = 0;
48 int i2c2_inited = 0;
49
50 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
51 {
52     // Determine the I2C to use
53     I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
54     I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
55
56     obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
57     MBED_ASSERT(obj->i2c != (I2CName)NC);
58
59     // Enable I2C1 clock and pinout if not done
60     if ((obj->i2c == I2C_1) && !i2c1_inited) {
61         i2c1_inited = 1;
62         __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
63         __I2C1_CLK_ENABLE();
64         // Configure I2C pins
65         pinmap_pinout(sda, PinMap_I2C_SDA);
66         pinmap_pinout(scl, PinMap_I2C_SCL);
67         pin_mode(sda, OpenDrain);
68         pin_mode(scl, OpenDrain);
69     }
70
71     // Enable I2C2 clock and pinout if not done
72     if ((obj->i2c == I2C_2) && !i2c2_inited) {
73         i2c2_inited = 1;
74         __I2C2_CLK_ENABLE();
75         // Configure I2C pins
76         pinmap_pinout(sda, PinMap_I2C_SDA);
77         pinmap_pinout(scl, PinMap_I2C_SCL);
78         pin_mode(sda, OpenDrain);
79         pin_mode(scl, OpenDrain);
80     }
81
82     // Reset to clear pending flags if any
83     i2c_reset(obj);
84
85     // I2C configuration
86     i2c_frequency(obj, 100000); // 100 kHz per default
87 }
88
89 void i2c_frequency(i2c_t *obj, int hz)
90 {
91     MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
92     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
93     int timeout;
94
95     // wait before init
96     timeout = LONG_TIMEOUT;
97     while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
98
99     // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
100     switch (hz) {
101         case 100000:
102             I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
103             break;
104         case 400000:
105             I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
106             break;
107         case 1000000:
108             I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
109             break;
110         default:
111             break;
112     }
113
114     // I2C configuration
115     I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
116     I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
117     I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
118     I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
119     I2cHandle.Init.OwnAddress1      = 0;
120     I2cHandle.Init.OwnAddress2      = 0;
121     I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
122     HAL_I2C_Init(&I2cHandle);
123 }
124
125 inline int i2c_start(i2c_t *obj)
126 {
127     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
128     int timeout;
129
130     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
131
132     // Clear Acknowledge failure flag
133     __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
134
135     // Generate the START condition
136     i2c->CR2 |= I2C_CR2_START;
137
138     // Wait the START condition has been correctly sent
139     timeout = FLAG_TIMEOUT;
140     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
141         if ((timeout--) == 0) {
142             return 1;
143         }
144     }
145
146     return 0;
147 }
148
149 inline int i2c_stop(i2c_t *obj)
150 {
151     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
152
153     // Generate the STOP condition
154     i2c->CR2 |= I2C_CR2_STOP;
155
156     return 0;
157 }
158
159 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
160 {
161     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
162     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
163     int timeout;
164     int count;
165     int value;
166
167     // Update CR2 register
168     i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
169                | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
170
171     // Read all bytes
172     for (count = 0; count < length; count++) {
173         value = i2c_byte_read(obj, 0);
174         data[count] = (char)value;
175     }
176
177     // Wait transfer complete
178     timeout = FLAG_TIMEOUT;
179     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
180         timeout--;
181         if (timeout == 0) {
182             return -1;
183         }
184     }
185     __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
186
187     // If not repeated start, send stop.
188     if (stop) {
189         i2c_stop(obj);
190         // Wait until STOPF flag is set
191         timeout = FLAG_TIMEOUT;
192         while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
193             timeout--;
194             if (timeout == 0) {
195                 return -1;
196             }
197         }
198         // Clear STOP Flag
199         __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
200     }
201
202     return length;
203 }
204
205 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
206 {
207     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
208     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
209     int timeout;
210     int count;
211
212     // Update CR2 register
213     i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
214                | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
215
216     for (count = 0; count < length; count++) {
217         i2c_byte_write(obj, data[count]);
218     }
219
220     // Wait transfer complete
221     timeout = FLAG_TIMEOUT;
222     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
223         timeout--;
224         if (timeout == 0) {
225             return -1;
226         }
227     }
228     __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
229
230     // If not repeated start, send stop
231     if (stop) {
232         i2c_stop(obj);
233         // Wait until STOPF flag is set
234         timeout = FLAG_TIMEOUT;
235         while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
236             timeout--;
237             if (timeout == 0) {
238                 return -1;
239             }
240         }
241         // Clear STOP Flag
242         __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
243     }
244
245     return count;
246 }
247
248 int i2c_byte_read(i2c_t *obj, int last)
249 {
250     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
251     int timeout;
252
253     // Wait until the byte is received
254     timeout = FLAG_TIMEOUT;
255     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
256         if ((timeout--) == 0) {
257             return -1;
258         }
259     }
260
261     return (int)i2c->RXDR;
262 }
263
264 int i2c_byte_write(i2c_t *obj, int data)
265 {
266     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
267     int timeout;
268
269     // Wait until the previous byte is transmitted
270     timeout = FLAG_TIMEOUT;
271     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
272         if ((timeout--) == 0) {
273             return 0;
274         }
275     }
276
277     i2c->TXDR = (uint8_t)data;
278
279     return 1;
280 }
281
282 void i2c_reset(i2c_t *obj)
283 {
284     int timeout;
285
286     // Wait before reset
287     timeout = LONG_TIMEOUT;
288     while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
289
290     if (obj->i2c == I2C_1) {
291         __I2C1_FORCE_RESET();
292         __I2C1_RELEASE_RESET();
293     }
294     if (obj->i2c == I2C_2) {
295         __I2C2_FORCE_RESET();
296         __I2C2_RELEASE_RESET();
297     }
298 }
299
300 #if DEVICE_I2CSLAVE
301
302 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
303 {
304     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
305     uint16_t tmpreg = 0;
306
307     // disable
308     i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
309     // Get the old register value
310     tmpreg = i2c->OAR1;
311     // Reset address bits
312     tmpreg &= 0xFC00;
313     // Set new address
314     tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
315     // Store the new register value
316     i2c->OAR1 = tmpreg;
317     // enable
318     i2c->OAR1 |= I2C_OAR1_OA1EN;
319 }
320
321 void i2c_slave_mode(i2c_t *obj, int enable_slave)
322 {
323     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
324     uint16_t tmpreg;
325
326     // Get the old register value
327     tmpreg = i2c->OAR1;
328
329     // Enable / disable slave
330     if (enable_slave == 1) {
331         tmpreg |= I2C_OAR1_OA1EN;
332     } else {
333         tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
334     }
335
336     // Set new mode
337     i2c->OAR1 = tmpreg;
338 }
339
340 // See I2CSlave.h
341 #define NoData         0 // the slave has not been addressed
342 #define ReadAddressed  1 // the master has requested a read from this slave (slave = transmitter)
343 #define WriteGeneral   2 // the master is writing to all slave
344 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
345
346 int i2c_slave_receive(i2c_t *obj)
347 {
348     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
349     int retValue = NoData;
350
351     if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
352         if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
353             if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
354                 retValue = ReadAddressed;
355             else
356                 retValue = WriteAddressed;
357
358             __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
359         }
360     }
361
362     return (retValue);
363 }
364
365 int i2c_slave_read(i2c_t *obj, char *data, int length)
366 {
367     char size = 0;
368
369     while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
370
371     return size;
372 }
373
374 int i2c_slave_write(i2c_t *obj, const char *data, int length)
375 {
376     char size = 0;
377     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
378
379     do {
380         i2c_byte_write(obj, data[size]);
381         size++;
382     } while (size < length);
383
384     return size;
385 }
386
387
388 #endif // DEVICE_I2CSLAVE
389
390 #endif // DEVICE_I2C