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