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