test
targets/TARGET_STM/TARGET_STM32F3/analogout_device.c@2:4364577b5ad8, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:33:19 2020 -0500
- Revision:
- 2:4364577b5ad8
- Parent:
- 1:8a094db1347f
copied mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 1:8a094db1347f | 1 | /* mbed Microcontroller Library |
elijahsj | 1:8a094db1347f | 2 | * Copyright (c) 2015, STMicroelectronics |
elijahsj | 1:8a094db1347f | 3 | * All rights reserved. |
elijahsj | 1:8a094db1347f | 4 | * |
elijahsj | 1:8a094db1347f | 5 | * Redistribution and use in source and binary forms, with or without |
elijahsj | 1:8a094db1347f | 6 | * modification, are permitted provided that the following conditions are met: |
elijahsj | 1:8a094db1347f | 7 | * |
elijahsj | 1:8a094db1347f | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
elijahsj | 1:8a094db1347f | 9 | * this list of conditions and the following disclaimer. |
elijahsj | 1:8a094db1347f | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elijahsj | 1:8a094db1347f | 11 | * this list of conditions and the following disclaimer in the documentation |
elijahsj | 1:8a094db1347f | 12 | * and/or other materials provided with the distribution. |
elijahsj | 1:8a094db1347f | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elijahsj | 1:8a094db1347f | 14 | * may be used to endorse or promote products derived from this software |
elijahsj | 1:8a094db1347f | 15 | * without specific prior written permission. |
elijahsj | 1:8a094db1347f | 16 | * |
elijahsj | 1:8a094db1347f | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elijahsj | 1:8a094db1347f | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elijahsj | 1:8a094db1347f | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elijahsj | 1:8a094db1347f | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elijahsj | 1:8a094db1347f | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elijahsj | 1:8a094db1347f | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elijahsj | 1:8a094db1347f | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elijahsj | 1:8a094db1347f | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elijahsj | 1:8a094db1347f | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elijahsj | 1:8a094db1347f | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elijahsj | 1:8a094db1347f | 27 | */ |
elijahsj | 1:8a094db1347f | 28 | #include "mbed_assert.h" |
elijahsj | 1:8a094db1347f | 29 | #include "analogout_api.h" |
elijahsj | 1:8a094db1347f | 30 | |
elijahsj | 1:8a094db1347f | 31 | #if DEVICE_ANALOGOUT |
elijahsj | 1:8a094db1347f | 32 | |
elijahsj | 1:8a094db1347f | 33 | #include "cmsis.h" |
elijahsj | 1:8a094db1347f | 34 | #include "pinmap.h" |
elijahsj | 1:8a094db1347f | 35 | #include "mbed_error.h" |
elijahsj | 1:8a094db1347f | 36 | #include "PeripheralPins.h" |
elijahsj | 1:8a094db1347f | 37 | |
elijahsj | 1:8a094db1347f | 38 | // These variables are used for the "free" function |
elijahsj | 1:8a094db1347f | 39 | static int pa4_used = 0; |
elijahsj | 1:8a094db1347f | 40 | static int pa5_used = 0; |
elijahsj | 1:8a094db1347f | 41 | |
elijahsj | 1:8a094db1347f | 42 | void analogout_init(dac_t *obj, PinName pin) { |
elijahsj | 1:8a094db1347f | 43 | DAC_ChannelConfTypeDef sConfig; |
elijahsj | 1:8a094db1347f | 44 | |
elijahsj | 1:8a094db1347f | 45 | // Get the peripheral name from the pin and assign it to the object |
elijahsj | 1:8a094db1347f | 46 | obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
elijahsj | 1:8a094db1347f | 47 | MBED_ASSERT(obj->dac != (DACName)NC); |
elijahsj | 1:8a094db1347f | 48 | |
elijahsj | 1:8a094db1347f | 49 | // Get the pin function and assign the used channel to the object |
elijahsj | 1:8a094db1347f | 50 | uint32_t function = pinmap_function(pin, PinMap_DAC); |
elijahsj | 1:8a094db1347f | 51 | MBED_ASSERT(function != (uint32_t)NC); |
elijahsj | 1:8a094db1347f | 52 | |
elijahsj | 1:8a094db1347f | 53 | // Save the channel for the write and read functions |
elijahsj | 1:8a094db1347f | 54 | switch (STM_PIN_CHANNEL(function)) { |
elijahsj | 1:8a094db1347f | 55 | case 1: |
elijahsj | 1:8a094db1347f | 56 | obj->channel = DAC_CHANNEL_1; |
elijahsj | 1:8a094db1347f | 57 | break; |
elijahsj | 1:8a094db1347f | 58 | #if defined(DAC_CHANNEL_2) |
elijahsj | 1:8a094db1347f | 59 | case 2: |
elijahsj | 1:8a094db1347f | 60 | obj->channel = DAC_CHANNEL_2; |
elijahsj | 1:8a094db1347f | 61 | break; |
elijahsj | 1:8a094db1347f | 62 | #endif |
elijahsj | 1:8a094db1347f | 63 | default: |
elijahsj | 1:8a094db1347f | 64 | error("Unknown DAC channel"); |
elijahsj | 1:8a094db1347f | 65 | break; |
elijahsj | 1:8a094db1347f | 66 | } |
elijahsj | 1:8a094db1347f | 67 | |
elijahsj | 1:8a094db1347f | 68 | // Configure GPIO |
elijahsj | 1:8a094db1347f | 69 | pinmap_pinout(pin, PinMap_DAC); |
elijahsj | 1:8a094db1347f | 70 | |
elijahsj | 1:8a094db1347f | 71 | // Save the pin for future use |
elijahsj | 1:8a094db1347f | 72 | obj->pin = pin; |
elijahsj | 1:8a094db1347f | 73 | |
elijahsj | 1:8a094db1347f | 74 | // Enable DAC clock |
elijahsj | 1:8a094db1347f | 75 | if (obj->dac == DAC_1) { |
elijahsj | 1:8a094db1347f | 76 | __DAC1_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 77 | } |
elijahsj | 1:8a094db1347f | 78 | #if defined(DAC2) |
elijahsj | 1:8a094db1347f | 79 | if (obj->dac == DAC_2) { |
elijahsj | 1:8a094db1347f | 80 | __DAC2_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 81 | } |
elijahsj | 1:8a094db1347f | 82 | #endif |
elijahsj | 1:8a094db1347f | 83 | |
elijahsj | 1:8a094db1347f | 84 | // Configure DAC |
elijahsj | 1:8a094db1347f | 85 | obj->handle.Instance = (DAC_TypeDef *)(obj->dac); |
elijahsj | 1:8a094db1347f | 86 | if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { |
elijahsj | 1:8a094db1347f | 87 | error("HAL_DAC_Init failed"); |
elijahsj | 1:8a094db1347f | 88 | } |
elijahsj | 1:8a094db1347f | 89 | |
elijahsj | 1:8a094db1347f | 90 | /* Enable both Buffer and Switch in the configuration, |
elijahsj | 1:8a094db1347f | 91 | * letting HAL layer in charge of selecting either one |
elijahsj | 1:8a094db1347f | 92 | * or the other depending on the actual DAC instance and |
elijahsj | 1:8a094db1347f | 93 | * channel being configured. |
elijahsj | 1:8a094db1347f | 94 | */ |
elijahsj | 1:8a094db1347f | 95 | sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
elijahsj | 1:8a094db1347f | 96 | sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
elijahsj | 1:8a094db1347f | 97 | #if defined(DAC_OUTPUTSWITCH_ENABLE) |
elijahsj | 1:8a094db1347f | 98 | sConfig.DAC_OutputSwitch = DAC_OUTPUTSWITCH_ENABLE; |
elijahsj | 1:8a094db1347f | 99 | #endif |
elijahsj | 1:8a094db1347f | 100 | |
elijahsj | 1:8a094db1347f | 101 | if (pin == PA_4) { |
elijahsj | 1:8a094db1347f | 102 | pa4_used = 1; |
elijahsj | 1:8a094db1347f | 103 | } |
elijahsj | 1:8a094db1347f | 104 | |
elijahsj | 1:8a094db1347f | 105 | if (pin == PA_5) { |
elijahsj | 1:8a094db1347f | 106 | pa5_used = 1; |
elijahsj | 1:8a094db1347f | 107 | } |
elijahsj | 1:8a094db1347f | 108 | |
elijahsj | 1:8a094db1347f | 109 | if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { |
elijahsj | 1:8a094db1347f | 110 | error("HAL_DAC_ConfigChannel failed"); |
elijahsj | 1:8a094db1347f | 111 | } |
elijahsj | 1:8a094db1347f | 112 | |
elijahsj | 1:8a094db1347f | 113 | analogout_write_u16(obj, 0); |
elijahsj | 1:8a094db1347f | 114 | } |
elijahsj | 1:8a094db1347f | 115 | |
elijahsj | 1:8a094db1347f | 116 | void analogout_free(dac_t *obj) { |
elijahsj | 1:8a094db1347f | 117 | // Reset DAC and disable clock |
elijahsj | 1:8a094db1347f | 118 | if (obj->pin == PA_4) pa4_used = 0; |
elijahsj | 1:8a094db1347f | 119 | if (obj->pin == PA_5) pa5_used = 0; |
elijahsj | 1:8a094db1347f | 120 | |
elijahsj | 1:8a094db1347f | 121 | if ((pa4_used == 0) && (pa5_used == 0)) { |
elijahsj | 1:8a094db1347f | 122 | __DAC1_FORCE_RESET(); |
elijahsj | 1:8a094db1347f | 123 | __DAC1_RELEASE_RESET(); |
elijahsj | 1:8a094db1347f | 124 | __DAC1_CLK_DISABLE(); |
elijahsj | 1:8a094db1347f | 125 | } |
elijahsj | 1:8a094db1347f | 126 | |
elijahsj | 1:8a094db1347f | 127 | #if defined(DAC2) |
elijahsj | 1:8a094db1347f | 128 | if (obj->pin == PA_6) { |
elijahsj | 1:8a094db1347f | 129 | __DAC2_FORCE_RESET(); |
elijahsj | 1:8a094db1347f | 130 | __DAC2_RELEASE_RESET(); |
elijahsj | 1:8a094db1347f | 131 | __DAC2_CLK_DISABLE(); |
elijahsj | 1:8a094db1347f | 132 | } |
elijahsj | 1:8a094db1347f | 133 | #endif |
elijahsj | 1:8a094db1347f | 134 | |
elijahsj | 1:8a094db1347f | 135 | // Configure GPIO |
elijahsj | 1:8a094db1347f | 136 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
elijahsj | 1:8a094db1347f | 137 | } |
elijahsj | 1:8a094db1347f | 138 | |
elijahsj | 1:8a094db1347f | 139 | #endif // DEVICE_ANALOGOUT |