Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F334R8/analogout_api.c@405:cbc7dfcb0ce9, 2014-11-14 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Nov 14 10:15:07 2014 +0000
- Revision:
- 405:cbc7dfcb0ce9
- Parent:
- 285:31249416b6f9
- Child:
- 415:4ec4c5b614b0
Synchronized with git revision c4901dbb33541e1dcda3fe69a1ec2a11caf496bf
Full URL: https://github.com/mbedmicro/mbed/commit/c4901dbb33541e1dcda3fe69a1ec2a11caf496bf/
Targets: NUCLEOs - Align hal files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 237:f3da66175598 | 1 | /* mbed Microcontroller Library |
mbed_official | 237:f3da66175598 | 2 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 237:f3da66175598 | 3 | * All rights reserved. |
mbed_official | 237:f3da66175598 | 4 | * |
mbed_official | 237:f3da66175598 | 5 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 237:f3da66175598 | 6 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 237:f3da66175598 | 7 | * |
mbed_official | 237:f3da66175598 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 237:f3da66175598 | 9 | * this list of conditions and the following disclaimer. |
mbed_official | 237:f3da66175598 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 237:f3da66175598 | 11 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 237:f3da66175598 | 12 | * and/or other materials provided with the distribution. |
mbed_official | 237:f3da66175598 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 237:f3da66175598 | 14 | * may be used to endorse or promote products derived from this software |
mbed_official | 237:f3da66175598 | 15 | * without specific prior written permission. |
mbed_official | 237:f3da66175598 | 16 | * |
mbed_official | 237:f3da66175598 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 237:f3da66175598 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 237:f3da66175598 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 237:f3da66175598 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 237:f3da66175598 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 237:f3da66175598 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 237:f3da66175598 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 237:f3da66175598 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 237:f3da66175598 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 237:f3da66175598 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 237:f3da66175598 | 27 | */ |
mbed_official | 237:f3da66175598 | 28 | #include "mbed_assert.h" |
mbed_official | 237:f3da66175598 | 29 | #include "analogout_api.h" |
mbed_official | 237:f3da66175598 | 30 | |
mbed_official | 237:f3da66175598 | 31 | #if DEVICE_ANALOGOUT |
mbed_official | 237:f3da66175598 | 32 | |
mbed_official | 237:f3da66175598 | 33 | #include "cmsis.h" |
mbed_official | 237:f3da66175598 | 34 | #include "pinmap.h" |
mbed_official | 285:31249416b6f9 | 35 | #include "mbed_error.h" |
mbed_official | 237:f3da66175598 | 36 | |
mbed_official | 237:f3da66175598 | 37 | #define DAC_RANGE (0xFFF) // 12 bits |
mbed_official | 237:f3da66175598 | 38 | |
mbed_official | 237:f3da66175598 | 39 | static const PinMap PinMap_DAC[] = { |
mbed_official | 237:f3da66175598 | 40 | {PA_4, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC1_OUT1 |
mbed_official | 237:f3da66175598 | 41 | {PA_5, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC1_OUT2 (Warning: LED1 is also on this pin) |
mbed_official | 237:f3da66175598 | 42 | {PA_6, DAC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC2_OUT1 |
mbed_official | 237:f3da66175598 | 43 | {NC, NC, 0} |
mbed_official | 237:f3da66175598 | 44 | }; |
mbed_official | 237:f3da66175598 | 45 | |
mbed_official | 237:f3da66175598 | 46 | static DAC_HandleTypeDef DacHandle; |
mbed_official | 237:f3da66175598 | 47 | |
mbed_official | 237:f3da66175598 | 48 | // These variables are used for the "free" function |
mbed_official | 237:f3da66175598 | 49 | static int pa4_used = 0; |
mbed_official | 237:f3da66175598 | 50 | static int pa5_used = 0; |
mbed_official | 237:f3da66175598 | 51 | |
mbed_official | 237:f3da66175598 | 52 | void analogout_init(dac_t *obj, PinName pin) |
mbed_official | 237:f3da66175598 | 53 | { |
mbed_official | 237:f3da66175598 | 54 | DAC_ChannelConfTypeDef sConfig; |
mbed_official | 237:f3da66175598 | 55 | |
mbed_official | 237:f3da66175598 | 56 | // Get the peripheral name (DAC_1, DAC_2...) from the pin and assign it to the object |
mbed_official | 237:f3da66175598 | 57 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
mbed_official | 237:f3da66175598 | 58 | MBED_ASSERT(obj->dac != (DACName)NC); |
mbed_official | 237:f3da66175598 | 59 | |
mbed_official | 237:f3da66175598 | 60 | // Configure GPIO |
mbed_official | 237:f3da66175598 | 61 | pinmap_pinout(pin, PinMap_DAC); |
mbed_official | 237:f3da66175598 | 62 | |
mbed_official | 237:f3da66175598 | 63 | // Save the pin for future use |
mbed_official | 237:f3da66175598 | 64 | obj->pin = pin; |
mbed_official | 237:f3da66175598 | 65 | |
mbed_official | 237:f3da66175598 | 66 | // Enable DAC clock |
mbed_official | 237:f3da66175598 | 67 | if (obj->dac == DAC_1) { |
mbed_official | 237:f3da66175598 | 68 | __DAC1_CLK_ENABLE(); |
mbed_official | 237:f3da66175598 | 69 | } |
mbed_official | 405:cbc7dfcb0ce9 | 70 | |
mbed_official | 237:f3da66175598 | 71 | if (obj->dac == DAC_2) { |
mbed_official | 237:f3da66175598 | 72 | __DAC2_CLK_ENABLE(); |
mbed_official | 237:f3da66175598 | 73 | } |
mbed_official | 237:f3da66175598 | 74 | |
mbed_official | 237:f3da66175598 | 75 | // Configure DAC |
mbed_official | 237:f3da66175598 | 76 | DacHandle.Instance = (DAC_TypeDef *)(obj->dac); |
mbed_official | 237:f3da66175598 | 77 | |
mbed_official | 237:f3da66175598 | 78 | sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
mbed_official | 237:f3da66175598 | 79 | sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; |
mbed_official | 237:f3da66175598 | 80 | |
mbed_official | 237:f3da66175598 | 81 | if (pin == PA_4) { |
mbed_official | 237:f3da66175598 | 82 | HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); |
mbed_official | 237:f3da66175598 | 83 | pa4_used = 1; |
mbed_official | 237:f3da66175598 | 84 | } |
mbed_official | 237:f3da66175598 | 85 | |
mbed_official | 237:f3da66175598 | 86 | if (pin == PA_5) { |
mbed_official | 237:f3da66175598 | 87 | HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2); |
mbed_official | 237:f3da66175598 | 88 | pa5_used = 1; |
mbed_official | 237:f3da66175598 | 89 | } |
mbed_official | 237:f3da66175598 | 90 | |
mbed_official | 237:f3da66175598 | 91 | if (pin == PA_6) { |
mbed_official | 237:f3da66175598 | 92 | HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); |
mbed_official | 237:f3da66175598 | 93 | } |
mbed_official | 237:f3da66175598 | 94 | |
mbed_official | 237:f3da66175598 | 95 | analogout_write_u16(obj, 0); |
mbed_official | 237:f3da66175598 | 96 | } |
mbed_official | 237:f3da66175598 | 97 | |
mbed_official | 237:f3da66175598 | 98 | void analogout_free(dac_t *obj) |
mbed_official | 237:f3da66175598 | 99 | { |
mbed_official | 237:f3da66175598 | 100 | // Reset DAC and disable clock |
mbed_official | 237:f3da66175598 | 101 | if (obj->pin == PA_4) pa4_used = 0; |
mbed_official | 237:f3da66175598 | 102 | if (obj->pin == PA_5) pa5_used = 0; |
mbed_official | 237:f3da66175598 | 103 | |
mbed_official | 237:f3da66175598 | 104 | if ((pa4_used == 0) && (pa5_used == 0)) { |
mbed_official | 237:f3da66175598 | 105 | __DAC1_FORCE_RESET(); |
mbed_official | 237:f3da66175598 | 106 | __DAC1_RELEASE_RESET(); |
mbed_official | 237:f3da66175598 | 107 | __DAC1_CLK_DISABLE(); |
mbed_official | 237:f3da66175598 | 108 | } |
mbed_official | 237:f3da66175598 | 109 | |
mbed_official | 237:f3da66175598 | 110 | if (obj->pin == PA_6) { |
mbed_official | 237:f3da66175598 | 111 | __DAC2_FORCE_RESET(); |
mbed_official | 237:f3da66175598 | 112 | __DAC2_RELEASE_RESET(); |
mbed_official | 237:f3da66175598 | 113 | __DAC2_CLK_DISABLE(); |
mbed_official | 237:f3da66175598 | 114 | } |
mbed_official | 237:f3da66175598 | 115 | |
mbed_official | 237:f3da66175598 | 116 | // Configure GPIO |
mbed_official | 237:f3da66175598 | 117 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 237:f3da66175598 | 118 | } |
mbed_official | 237:f3da66175598 | 119 | |
mbed_official | 237:f3da66175598 | 120 | static inline void dac_write(dac_t *obj, uint16_t value) |
mbed_official | 237:f3da66175598 | 121 | { |
mbed_official | 237:f3da66175598 | 122 | if ((obj->pin == PA_4) || (obj->pin == PA_6)) { |
mbed_official | 237:f3da66175598 | 123 | HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); |
mbed_official | 237:f3da66175598 | 124 | HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); |
mbed_official | 237:f3da66175598 | 125 | } |
mbed_official | 237:f3da66175598 | 126 | |
mbed_official | 237:f3da66175598 | 127 | if (obj->pin == PA_5) { |
mbed_official | 237:f3da66175598 | 128 | HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); |
mbed_official | 237:f3da66175598 | 129 | HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); |
mbed_official | 237:f3da66175598 | 130 | } |
mbed_official | 237:f3da66175598 | 131 | } |
mbed_official | 237:f3da66175598 | 132 | |
mbed_official | 237:f3da66175598 | 133 | static inline int dac_read(dac_t *obj) |
mbed_official | 237:f3da66175598 | 134 | { |
mbed_official | 237:f3da66175598 | 135 | if ((obj->pin == PA_4) || (obj->pin == PA_6)) { |
mbed_official | 237:f3da66175598 | 136 | return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); |
mbed_official | 237:f3da66175598 | 137 | } else if (obj->pin == PA_5) { |
mbed_official | 237:f3da66175598 | 138 | return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); |
mbed_official | 237:f3da66175598 | 139 | } else { |
mbed_official | 237:f3da66175598 | 140 | return 0; |
mbed_official | 237:f3da66175598 | 141 | } |
mbed_official | 237:f3da66175598 | 142 | } |
mbed_official | 237:f3da66175598 | 143 | |
mbed_official | 237:f3da66175598 | 144 | void analogout_write(dac_t *obj, float value) |
mbed_official | 237:f3da66175598 | 145 | { |
mbed_official | 237:f3da66175598 | 146 | if (value < 0.0f) { |
mbed_official | 237:f3da66175598 | 147 | dac_write(obj, 0); // Min value |
mbed_official | 237:f3da66175598 | 148 | } else if (value > 1.0f) { |
mbed_official | 237:f3da66175598 | 149 | dac_write(obj, (uint16_t)DAC_RANGE); // Max value |
mbed_official | 237:f3da66175598 | 150 | } else { |
mbed_official | 237:f3da66175598 | 151 | dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); |
mbed_official | 237:f3da66175598 | 152 | } |
mbed_official | 237:f3da66175598 | 153 | } |
mbed_official | 237:f3da66175598 | 154 | |
mbed_official | 237:f3da66175598 | 155 | void analogout_write_u16(dac_t *obj, uint16_t value) |
mbed_official | 237:f3da66175598 | 156 | { |
mbed_official | 237:f3da66175598 | 157 | if (value > (uint16_t)DAC_RANGE) { |
mbed_official | 237:f3da66175598 | 158 | dac_write(obj, (uint16_t)DAC_RANGE); // Max value |
mbed_official | 237:f3da66175598 | 159 | } else { |
mbed_official | 237:f3da66175598 | 160 | dac_write(obj, value); |
mbed_official | 237:f3da66175598 | 161 | } |
mbed_official | 237:f3da66175598 | 162 | } |
mbed_official | 237:f3da66175598 | 163 | |
mbed_official | 237:f3da66175598 | 164 | float analogout_read(dac_t *obj) |
mbed_official | 237:f3da66175598 | 165 | { |
mbed_official | 237:f3da66175598 | 166 | uint32_t value = dac_read(obj); |
mbed_official | 237:f3da66175598 | 167 | return (float)((float)value * (1.0f / (float)DAC_RANGE)); |
mbed_official | 237:f3da66175598 | 168 | } |
mbed_official | 237:f3da66175598 | 169 | |
mbed_official | 237:f3da66175598 | 170 | uint16_t analogout_read_u16(dac_t *obj) |
mbed_official | 237:f3da66175598 | 171 | { |
mbed_official | 237:f3da66175598 | 172 | return (uint16_t)dac_read(obj); |
mbed_official | 237:f3da66175598 | 173 | } |
mbed_official | 237:f3da66175598 | 174 | |
mbed_official | 237:f3da66175598 | 175 | #endif // DEVICE_ANALOGOUT |