]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/i2c_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32L0 / 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 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
48 {
49     static int i2c1_inited = 0;
50     static int i2c2_inited = 0;
51 #if defined(I2C3_BASE)
52     static int i2c3_inited = 0;
53 #endif
54
55     // Determine the I2C to use
56     I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
57     I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
58
59     obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
60     MBED_ASSERT(obj->i2c != (I2CName)NC);
61
62     // Enable I2C1 clock and pinout if not done
63     if ((obj->i2c == I2C_1) && !i2c1_inited) {
64         i2c1_inited = 1;
65         __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
66         __I2C1_CLK_ENABLE();
67         // Configure I2C pins
68         pinmap_pinout(sda, PinMap_I2C_SDA);
69         pinmap_pinout(scl, PinMap_I2C_SCL);
70         pin_mode(sda, OpenDrain);
71         pin_mode(scl, OpenDrain);
72     }
73
74     // Enable I2C2 clock and pinout if not done
75     if ((obj->i2c == I2C_2) && !i2c2_inited) {
76         i2c2_inited = 1;
77         __I2C2_CLK_ENABLE();
78         // Configure I2C pins
79         pinmap_pinout(sda, PinMap_I2C_SDA);
80         pinmap_pinout(scl, PinMap_I2C_SCL);
81         pin_mode(sda, OpenDrain);
82         pin_mode(scl, OpenDrain);
83     }
84
85 #if defined(I2C3_BASE)
86     // Enable I2C3 clock and pinout if not done
87     if ((obj->i2c == I2C_3) && !i2c3_inited) {
88         i2c3_inited = 1;
89         __I2C3_CLK_ENABLE();
90         // Configure I2C pins
91         pinmap_pinout(sda, PinMap_I2C_SDA);
92         pinmap_pinout(scl, PinMap_I2C_SCL);
93         pin_mode(sda, OpenDrain);
94         pin_mode(scl, OpenDrain);
95     }
96 #endif
97
98     // Reset to clear pending flags if any
99     i2c_reset(obj);
100
101     // I2C configuration
102     i2c_frequency(obj, 100000); // 100 kHz per default
103 }
104
105 void i2c_frequency(i2c_t *obj, int hz)
106 {
107     MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
108     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
109     int timeout;
110
111     // wait before init
112     timeout = LONG_TIMEOUT;
113     while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
114
115     // Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0
116     switch (hz) {
117         case 100000:
118             I2cHandle.Init.Timing = 0x20602938; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
119             break;
120         case 400000:
121             I2cHandle.Init.Timing = 0x00B0122A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
122             break;
123         case 1000000:
124             I2cHandle.Init.Timing = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
125             break;
126         default:
127             break;
128     }
129
130     // I2C configuration
131     I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
132     I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
133     I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
134     I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
135     I2cHandle.Init.OwnAddress1      = 0;
136     I2cHandle.Init.OwnAddress2      = 0;
137     I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
138     HAL_I2C_Init(&I2cHandle);
139 }
140
141 inline int i2c_start(i2c_t *obj)
142 {
143     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
144     int timeout;
145
146     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
147
148     // Clear Acknowledge failure flag
149     __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
150
151     // Generate the START condition
152     i2c->CR2 |= I2C_CR2_START;
153
154     // Wait the START condition has been correctly sent
155     timeout = FLAG_TIMEOUT;
156     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
157         if ((timeout--) == 0) {
158             return 1;
159         }
160     }
161
162     return 0;
163 }
164
165 inline int i2c_stop(i2c_t *obj)
166 {
167     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
168
169     // Generate the STOP condition
170     i2c->CR2 |= I2C_CR2_STOP;
171
172     return 0;
173 }
174
175 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
176 {
177     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
178     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
179     int timeout;
180     int count;
181     int value;
182
183     /* update CR2 register */
184     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)))
185                | (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);
186
187     // Read all bytes
188     for (count = 0; count < length; count++) {
189         value = i2c_byte_read(obj, 0);
190         data[count] = (char)value;
191     }
192
193     // Wait transfer complete
194     timeout = FLAG_TIMEOUT;
195     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
196         timeout--;
197         if (timeout == 0) {
198             return -1;
199         }
200     }
201
202     __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
203
204     // If not repeated start, send stop.
205     if (stop) {
206         i2c_stop(obj);
207         /* Wait until STOPF flag is set */
208         timeout = FLAG_TIMEOUT;
209         while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
210             timeout--;
211             if (timeout == 0) {
212                 return -1;
213             }
214         }
215         /* Clear STOP Flag */
216         __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
217     }
218
219     return length;
220 }
221
222 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
223 {
224     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
225     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
226     int timeout;
227     int count;
228
229     /* update CR2 register */
230     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)))
231                | (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);
232
233     for (count = 0; count < length; count++) {
234         i2c_byte_write(obj, data[count]);
235     }
236
237     // Wait transfer complete
238     timeout = FLAG_TIMEOUT;
239     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
240         timeout--;
241         if (timeout == 0) {
242             return -1;
243         }
244     }
245
246     __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
247
248     // If not repeated start, send stop.
249     if (stop) {
250         i2c_stop(obj);
251         /* Wait until STOPF flag is set */
252         timeout = FLAG_TIMEOUT;
253         while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
254             timeout--;
255             if (timeout == 0) {
256                 return -1;
257             }
258         }
259         /* Clear STOP Flag */
260         __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
261     }
262
263     return count;
264 }
265
266 int i2c_byte_read(i2c_t *obj, int last)
267 {
268     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
269     int timeout;
270
271     // Wait until the byte is received
272     timeout = FLAG_TIMEOUT;
273     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
274         if ((timeout--) == 0) {
275             return -1;
276         }
277     }
278
279     return (int)i2c->RXDR;
280 }
281
282 int i2c_byte_write(i2c_t *obj, int data)
283 {
284     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
285     int timeout;
286
287     // Wait until the previous byte is transmitted
288     timeout = FLAG_TIMEOUT;
289     while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
290         if ((timeout--) == 0) {
291             return 0;
292         }
293     }
294
295     i2c->TXDR = (uint8_t)data;
296
297     return 1;
298 }
299
300 void i2c_reset(i2c_t *obj)
301 {
302     int timeout;
303
304     // wait before reset
305     timeout = LONG_TIMEOUT;
306     while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
307
308     if (obj->i2c == I2C_1) {
309         __I2C1_FORCE_RESET();
310         __I2C1_RELEASE_RESET();
311     }
312     if (obj->i2c == I2C_2) {
313         __I2C2_FORCE_RESET();
314         __I2C2_RELEASE_RESET();
315     }
316 }
317
318 #if DEVICE_I2CSLAVE
319
320 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
321 {
322     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
323     uint16_t tmpreg;
324
325     // disable
326     i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
327     // Get the old register value
328     tmpreg = i2c->OAR1;
329     // Reset address bits
330     tmpreg &= 0xFC00;
331     // Set new address
332     tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
333     // Store the new register value
334     i2c->OAR1 = tmpreg;
335     // enable
336     i2c->OAR1 |= I2C_OAR1_OA1EN;
337 }
338
339 void i2c_slave_mode(i2c_t *obj, int enable_slave)
340 {
341
342     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
343     uint16_t tmpreg;
344
345     // Get the old register value
346     tmpreg = i2c->OAR1;
347
348     // Enable / disable slave
349     if (enable_slave == 1) {
350         tmpreg |= I2C_OAR1_OA1EN;
351     } else {
352         tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
353     }
354
355     // Set new mode
356     i2c->OAR1 = tmpreg;
357
358 }
359
360 // See I2CSlave.h
361 #define NoData         0 // the slave has not been addressed
362 #define ReadAddressed  1 // the master has requested a read from this slave (slave = transmitter)
363 #define WriteGeneral   2 // the master is writing to all slave
364 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
365
366 int i2c_slave_receive(i2c_t *obj)
367 {
368     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
369     int retValue = NoData;
370
371     if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
372         if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
373             if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
374                 retValue = ReadAddressed;
375             else
376                 retValue = WriteAddressed;
377             __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
378         }
379     }
380
381     return (retValue);
382 }
383
384 int i2c_slave_read(i2c_t *obj, char *data, int length)
385 {
386     char size = 0;
387
388     while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
389
390     return size;
391 }
392
393 int i2c_slave_write(i2c_t *obj, const char *data, int length)
394 {
395     char size = 0;
396     I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
397
398     do {
399         i2c_byte_write(obj, data[size]);
400         size++;
401     } while (size < length);
402
403     return size;
404 }
405
406
407 #endif // DEVICE_I2CSLAVE
408
409 #endif // DEVICE_I2C