]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3XX/analogout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F3XX / analogout_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2014, STMicroelectronics
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "mbed_assert.h"
29 #include "analogout_api.h"
30
31 #if DEVICE_ANALOGOUT
32
33 #include "cmsis.h"
34 #include "pinmap.h"
35
36 #define RANGE_12BIT (0xFFF)
37
38 static const PinMap PinMap_DAC[] = {
39 {PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1
40 {NC, NC, 0}
41 };
42
43 void analogout_init(dac_t *obj, PinName pin) {
44 DAC_TypeDef *dac;
45 DAC_InitTypeDef DAC_InitStructure;
46
47 // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
48 obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
49 MBED_ASSERT(obj->dac == (DACName)NC);
50
51 dac = (DAC_TypeDef *)(obj->dac);
52
53 // Configure GPIO
54 pinmap_pinout(pin, PinMap_DAC);
55
56 // Save the channel for the write and read functions
57 obj->channel = pin;
58
59 // Enable DAC clock
60 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
61
62 // Configure and enable DAC channel
63 DAC_StructInit(&DAC_InitStructure);
64 DAC_Init(dac, DAC_Channel_1, &DAC_InitStructure);
65 DAC_Cmd(dac, DAC_Channel_1, ENABLE);
66
67 analogout_write_u16(obj, 0);
68 }
69
70 void analogout_free(dac_t *obj) {
71 }
72
73 static inline void dac_write(dac_t *obj, uint16_t value) {
74 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
75 DAC_SetChannel1Data(dac, DAC_Align_12b_R, value);
76 }
77
78 static inline int dac_read(dac_t *obj) {
79 DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
80 return (int)DAC_GetDataOutputValue(dac, DAC_Channel_1);
81 }
82
83 void analogout_write(dac_t *obj, float value) {
84 if (value < 0.0f) {
85 dac_write(obj, 0); // Min value
86 } else if (value > 1.0f) {
87 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
88 } else {
89 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
90 }
91 }
92
93 void analogout_write_u16(dac_t *obj, uint16_t value) {
94 if (value > (uint16_t)RANGE_12BIT) {
95 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
96 } else {
97 dac_write(obj, value);
98 }
99 }
100
101 float analogout_read(dac_t *obj) {
102 uint32_t value = dac_read(obj);
103 return (float)value * (1.0f / (float)RANGE_12BIT);
104 }
105
106 uint16_t analogout_read_u16(dac_t *obj) {
107 return (uint16_t)dac_read(obj);
108 }
109
110 #endif // DEVICE_ANALOGOUT
Imprint / Impressum