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