1 /* mbed Microcontroller Library
2 * Copyright (c) 2014, STMicroelectronics
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "mbed_assert.h"
29 #include "analogout_api.h"
36 #define RANGE_12BIT (0xFFF)
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
43 void analogout_init(dac_t *obj, PinName pin) {
45 DAC_InitTypeDef DAC_InitStructure;
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);
51 dac = (DAC_TypeDef *)(obj->dac);
54 pinmap_pinout(pin, PinMap_DAC);
56 // Save the channel for the write and read functions
60 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
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);
67 analogout_write_u16(obj, 0);
70 void analogout_free(dac_t *obj) {
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);
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);
83 void analogout_write(dac_t *obj, float value) {
85 dac_write(obj, 0); // Min value
86 } else if (value > 1.0f) {
87 dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
89 dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
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
97 dac_write(obj, value);
101 float analogout_read(dac_t *obj) {
102 uint32_t value = dac_read(obj);
103 return (float)value * (1.0f / (float)RANGE_12BIT);
106 uint16_t analogout_read_u16(dac_t *obj) {
107 return (uint16_t)dac_read(obj);
110 #endif // DEVICE_ANALOGOUT