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.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
targets/TARGET_STM/TARGET_STM32L0/analogout_api.c@0:098463de4c5d, 2017-01-25 (annotated)
- Committer:
- group-onsemi
- Date:
- Wed Jan 25 20:34:15 2017 +0000
- Revision:
- 0:098463de4c5d
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| group-onsemi | 0:098463de4c5d | 1 | /* mbed Microcontroller Library | 
| group-onsemi | 0:098463de4c5d | 2 | * Copyright (c) 2015, STMicroelectronics | 
| group-onsemi | 0:098463de4c5d | 3 | * All rights reserved. | 
| group-onsemi | 0:098463de4c5d | 4 | * | 
| group-onsemi | 0:098463de4c5d | 5 | * Redistribution and use in source and binary forms, with or without | 
| group-onsemi | 0:098463de4c5d | 6 | * modification, are permitted provided that the following conditions are met: | 
| group-onsemi | 0:098463de4c5d | 7 | * | 
| group-onsemi | 0:098463de4c5d | 8 | * 1. Redistributions of source code must retain the above copyright notice, | 
| group-onsemi | 0:098463de4c5d | 9 | * this list of conditions and the following disclaimer. | 
| group-onsemi | 0:098463de4c5d | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, | 
| group-onsemi | 0:098463de4c5d | 11 | * this list of conditions and the following disclaimer in the documentation | 
| group-onsemi | 0:098463de4c5d | 12 | * and/or other materials provided with the distribution. | 
| group-onsemi | 0:098463de4c5d | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors | 
| group-onsemi | 0:098463de4c5d | 14 | * may be used to endorse or promote products derived from this software | 
| group-onsemi | 0:098463de4c5d | 15 | * without specific prior written permission. | 
| group-onsemi | 0:098463de4c5d | 16 | * | 
| group-onsemi | 0:098463de4c5d | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
| group-onsemi | 0:098463de4c5d | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
| group-onsemi | 0:098463de4c5d | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| group-onsemi | 0:098463de4c5d | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
| group-onsemi | 0:098463de4c5d | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
| group-onsemi | 0:098463de4c5d | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
| group-onsemi | 0:098463de4c5d | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
| group-onsemi | 0:098463de4c5d | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
| group-onsemi | 0:098463de4c5d | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| group-onsemi | 0:098463de4c5d | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| group-onsemi | 0:098463de4c5d | 27 | */ | 
| group-onsemi | 0:098463de4c5d | 28 | #include "mbed_assert.h" | 
| group-onsemi | 0:098463de4c5d | 29 | #include "analogout_api.h" | 
| group-onsemi | 0:098463de4c5d | 30 | |
| group-onsemi | 0:098463de4c5d | 31 | #if DEVICE_ANALOGOUT | 
| group-onsemi | 0:098463de4c5d | 32 | |
| group-onsemi | 0:098463de4c5d | 33 | #include "cmsis.h" | 
| group-onsemi | 0:098463de4c5d | 34 | #include "pinmap.h" | 
| group-onsemi | 0:098463de4c5d | 35 | #include "mbed_error.h" | 
| group-onsemi | 0:098463de4c5d | 36 | #include "PeripheralPins.h" | 
| group-onsemi | 0:098463de4c5d | 37 | |
| group-onsemi | 0:098463de4c5d | 38 | #define DAC_RANGE (0xFFF) // 12 bits | 
| group-onsemi | 0:098463de4c5d | 39 | #define DAC_NB_BITS (12) | 
| group-onsemi | 0:098463de4c5d | 40 | |
| group-onsemi | 0:098463de4c5d | 41 | static DAC_HandleTypeDef DacHandle; | 
| group-onsemi | 0:098463de4c5d | 42 | |
| group-onsemi | 0:098463de4c5d | 43 | // These variables are used for the "free" function | 
| group-onsemi | 0:098463de4c5d | 44 | static int channel1_used = 0; | 
| group-onsemi | 0:098463de4c5d | 45 | static int channel2_used = 0; | 
| group-onsemi | 0:098463de4c5d | 46 | |
| group-onsemi | 0:098463de4c5d | 47 | void analogout_init(dac_t *obj, PinName pin) { | 
| group-onsemi | 0:098463de4c5d | 48 | DAC_ChannelConfTypeDef sConfig; | 
| group-onsemi | 0:098463de4c5d | 49 | |
| group-onsemi | 0:098463de4c5d | 50 | // Get the peripheral name from the pin and assign it to the object | 
| group-onsemi | 0:098463de4c5d | 51 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); | 
| group-onsemi | 0:098463de4c5d | 52 | MBED_ASSERT(obj->dac != (DACName)NC); | 
| group-onsemi | 0:098463de4c5d | 53 | |
| group-onsemi | 0:098463de4c5d | 54 | // Get the pin function and assign the used channel to the object | 
| group-onsemi | 0:098463de4c5d | 55 | uint32_t function = pinmap_function(pin, PinMap_DAC); | 
| group-onsemi | 0:098463de4c5d | 56 | MBED_ASSERT(function != (uint32_t)NC); | 
| group-onsemi | 0:098463de4c5d | 57 | obj->channel = STM_PIN_CHANNEL(function); | 
| group-onsemi | 0:098463de4c5d | 58 | |
| group-onsemi | 0:098463de4c5d | 59 | // Configure GPIO | 
| group-onsemi | 0:098463de4c5d | 60 | pinmap_pinout(pin, PinMap_DAC); | 
| group-onsemi | 0:098463de4c5d | 61 | |
| group-onsemi | 0:098463de4c5d | 62 | // Save the pin for future use | 
| group-onsemi | 0:098463de4c5d | 63 | obj->pin = pin; | 
| group-onsemi | 0:098463de4c5d | 64 | |
| group-onsemi | 0:098463de4c5d | 65 | // Enable DAC clock | 
| group-onsemi | 0:098463de4c5d | 66 | __DAC_CLK_ENABLE(); | 
| group-onsemi | 0:098463de4c5d | 67 | |
| group-onsemi | 0:098463de4c5d | 68 | // Configure DAC | 
| group-onsemi | 0:098463de4c5d | 69 | DacHandle.Instance = DAC; | 
| group-onsemi | 0:098463de4c5d | 70 | |
| group-onsemi | 0:098463de4c5d | 71 | sConfig.DAC_Trigger = DAC_TRIGGER_NONE; | 
| group-onsemi | 0:098463de4c5d | 72 | sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; | 
| group-onsemi | 0:098463de4c5d | 73 | |
| group-onsemi | 0:098463de4c5d | 74 | #if defined(DAC_CHANNEL_2) | 
| group-onsemi | 0:098463de4c5d | 75 | if (obj->channel == 2) { | 
| group-onsemi | 0:098463de4c5d | 76 | if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2) != HAL_OK) { | 
| group-onsemi | 0:098463de4c5d | 77 | error("Cannot configure DAC channel 2"); | 
| group-onsemi | 0:098463de4c5d | 78 | } | 
| group-onsemi | 0:098463de4c5d | 79 | channel2_used = 1; | 
| group-onsemi | 0:098463de4c5d | 80 | } else | 
| group-onsemi | 0:098463de4c5d | 81 | #endif | 
| group-onsemi | 0:098463de4c5d | 82 | { | 
| group-onsemi | 0:098463de4c5d | 83 | // channel 1 per default | 
| group-onsemi | 0:098463de4c5d | 84 | if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK) { | 
| group-onsemi | 0:098463de4c5d | 85 | error("Cannot configure DAC channel 1"); | 
| group-onsemi | 0:098463de4c5d | 86 | } | 
| group-onsemi | 0:098463de4c5d | 87 | obj->channel = 1; | 
| group-onsemi | 0:098463de4c5d | 88 | channel1_used = 1; | 
| group-onsemi | 0:098463de4c5d | 89 | } | 
| group-onsemi | 0:098463de4c5d | 90 | |
| group-onsemi | 0:098463de4c5d | 91 | analogout_write_u16(obj, 0); | 
| group-onsemi | 0:098463de4c5d | 92 | } | 
| group-onsemi | 0:098463de4c5d | 93 | |
| group-onsemi | 0:098463de4c5d | 94 | void analogout_free(dac_t *obj) { | 
| group-onsemi | 0:098463de4c5d | 95 | // Reset DAC and disable clock | 
| group-onsemi | 0:098463de4c5d | 96 | if (obj->channel == 1) channel1_used = 0; | 
| group-onsemi | 0:098463de4c5d | 97 | if (obj->channel == 2) channel2_used = 0; | 
| group-onsemi | 0:098463de4c5d | 98 | |
| group-onsemi | 0:098463de4c5d | 99 | if ((channel1_used == 0) && (channel2_used == 0)) { | 
| group-onsemi | 0:098463de4c5d | 100 | __DAC_FORCE_RESET(); | 
| group-onsemi | 0:098463de4c5d | 101 | __DAC_RELEASE_RESET(); | 
| group-onsemi | 0:098463de4c5d | 102 | __DAC_CLK_DISABLE(); | 
| group-onsemi | 0:098463de4c5d | 103 | } | 
| group-onsemi | 0:098463de4c5d | 104 | |
| group-onsemi | 0:098463de4c5d | 105 | // Configure GPIO | 
| group-onsemi | 0:098463de4c5d | 106 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); | 
| group-onsemi | 0:098463de4c5d | 107 | } | 
| group-onsemi | 0:098463de4c5d | 108 | |
| group-onsemi | 0:098463de4c5d | 109 | static inline void dac_write(dac_t *obj, int value) { | 
| group-onsemi | 0:098463de4c5d | 110 | if (obj->channel == 1) { | 
| group-onsemi | 0:098463de4c5d | 111 | HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); | 
| group-onsemi | 0:098463de4c5d | 112 | HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); | 
| group-onsemi | 0:098463de4c5d | 113 | } | 
| group-onsemi | 0:098463de4c5d | 114 | #if defined(DAC_CHANNEL_2) | 
| group-onsemi | 0:098463de4c5d | 115 | if (obj->channel == 2) { | 
| group-onsemi | 0:098463de4c5d | 116 | HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); | 
| group-onsemi | 0:098463de4c5d | 117 | HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); | 
| group-onsemi | 0:098463de4c5d | 118 | } | 
| group-onsemi | 0:098463de4c5d | 119 | #endif | 
| group-onsemi | 0:098463de4c5d | 120 | } | 
| group-onsemi | 0:098463de4c5d | 121 | |
| group-onsemi | 0:098463de4c5d | 122 | static inline int dac_read(dac_t *obj) { | 
| group-onsemi | 0:098463de4c5d | 123 | if (obj->channel == 1) { | 
| group-onsemi | 0:098463de4c5d | 124 | return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); | 
| group-onsemi | 0:098463de4c5d | 125 | } | 
| group-onsemi | 0:098463de4c5d | 126 | #if defined(DAC_CHANNEL_2) | 
| group-onsemi | 0:098463de4c5d | 127 | if (obj->channel == 2) { | 
| group-onsemi | 0:098463de4c5d | 128 | return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); | 
| group-onsemi | 0:098463de4c5d | 129 | } | 
| group-onsemi | 0:098463de4c5d | 130 | #endif | 
| group-onsemi | 0:098463de4c5d | 131 | return 0; | 
| group-onsemi | 0:098463de4c5d | 132 | } | 
| group-onsemi | 0:098463de4c5d | 133 | |
| group-onsemi | 0:098463de4c5d | 134 | void analogout_write(dac_t *obj, float value) { | 
| group-onsemi | 0:098463de4c5d | 135 | if (value < 0.0f) { | 
| group-onsemi | 0:098463de4c5d | 136 | dac_write(obj, 0); // Min value | 
| group-onsemi | 0:098463de4c5d | 137 | } else if (value > 1.0f) { | 
| group-onsemi | 0:098463de4c5d | 138 | dac_write(obj, (int)DAC_RANGE); // Max value | 
| group-onsemi | 0:098463de4c5d | 139 | } else { | 
| group-onsemi | 0:098463de4c5d | 140 | dac_write(obj, (int)(value * (float)DAC_RANGE)); | 
| group-onsemi | 0:098463de4c5d | 141 | } | 
| group-onsemi | 0:098463de4c5d | 142 | } | 
| group-onsemi | 0:098463de4c5d | 143 | |
| group-onsemi | 0:098463de4c5d | 144 | void analogout_write_u16(dac_t *obj, uint16_t value) { | 
| group-onsemi | 0:098463de4c5d | 145 | dac_write(obj, value >> (16 - DAC_NB_BITS)); | 
| group-onsemi | 0:098463de4c5d | 146 | } | 
| group-onsemi | 0:098463de4c5d | 147 | |
| group-onsemi | 0:098463de4c5d | 148 | float analogout_read(dac_t *obj) { | 
| group-onsemi | 0:098463de4c5d | 149 | uint32_t value = dac_read(obj); | 
| group-onsemi | 0:098463de4c5d | 150 | return (float)value * (1.0f / (float)DAC_RANGE); | 
| group-onsemi | 0:098463de4c5d | 151 | } | 
| group-onsemi | 0:098463de4c5d | 152 | |
| group-onsemi | 0:098463de4c5d | 153 | uint16_t analogout_read_u16(dac_t *obj) { | 
| group-onsemi | 0:098463de4c5d | 154 | uint32_t value = dac_read(obj); | 
| group-onsemi | 0:098463de4c5d | 155 | return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits | 
| group-onsemi | 0:098463de4c5d | 156 | } | 
| group-onsemi | 0:098463de4c5d | 157 | |
| group-onsemi | 0:098463de4c5d | 158 | #endif // DEVICE_ANALOGOUT |