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