test
targets/TARGET_STM/TARGET_STM32L4/analogin_api.c@1:8a094db1347f, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:02:47 2020 -0500
- Revision:
- 1:8a094db1347f
test
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) 2016, 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 "analogin_api.h" |
elijahsj | 1:8a094db1347f | 30 | |
elijahsj | 1:8a094db1347f | 31 | #if DEVICE_ANALOGIN |
elijahsj | 1:8a094db1347f | 32 | |
elijahsj | 1:8a094db1347f | 33 | #include "mbed_wait_api.h" |
elijahsj | 1:8a094db1347f | 34 | #include "cmsis.h" |
elijahsj | 1:8a094db1347f | 35 | #include "pinmap.h" |
elijahsj | 1:8a094db1347f | 36 | #include "mbed_error.h" |
elijahsj | 1:8a094db1347f | 37 | #include "PeripheralPins.h" |
elijahsj | 1:8a094db1347f | 38 | |
elijahsj | 1:8a094db1347f | 39 | int adc_inited = 0; |
elijahsj | 1:8a094db1347f | 40 | |
elijahsj | 1:8a094db1347f | 41 | void analogin_init(analogin_t *obj, PinName pin) |
elijahsj | 1:8a094db1347f | 42 | { |
elijahsj | 1:8a094db1347f | 43 | uint32_t function = (uint32_t)NC; |
elijahsj | 1:8a094db1347f | 44 | |
elijahsj | 1:8a094db1347f | 45 | // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) |
elijahsj | 1:8a094db1347f | 46 | // are described in PinNames.h and PeripheralPins.c |
elijahsj | 1:8a094db1347f | 47 | // Pin value must be between 0xF0 and 0xFF |
elijahsj | 1:8a094db1347f | 48 | if ((pin < 0xF0) || (pin >= 0x100)) { |
elijahsj | 1:8a094db1347f | 49 | // Normal channels |
elijahsj | 1:8a094db1347f | 50 | // Get the peripheral name from the pin and assign it to the object |
elijahsj | 1:8a094db1347f | 51 | obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); |
elijahsj | 1:8a094db1347f | 52 | // Get the functions (adc channel) from the pin and assign it to the object |
elijahsj | 1:8a094db1347f | 53 | function = pinmap_function(pin, PinMap_ADC); |
elijahsj | 1:8a094db1347f | 54 | // Configure GPIO |
elijahsj | 1:8a094db1347f | 55 | pinmap_pinout(pin, PinMap_ADC); |
elijahsj | 1:8a094db1347f | 56 | } else { |
elijahsj | 1:8a094db1347f | 57 | // Internal channels |
elijahsj | 1:8a094db1347f | 58 | obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal); |
elijahsj | 1:8a094db1347f | 59 | function = pinmap_function(pin, PinMap_ADC_Internal); |
elijahsj | 1:8a094db1347f | 60 | // No GPIO configuration for internal channels |
elijahsj | 1:8a094db1347f | 61 | } |
elijahsj | 1:8a094db1347f | 62 | MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); |
elijahsj | 1:8a094db1347f | 63 | MBED_ASSERT(function != (uint32_t)NC); |
elijahsj | 1:8a094db1347f | 64 | |
elijahsj | 1:8a094db1347f | 65 | obj->channel = STM_PIN_CHANNEL(function); |
elijahsj | 1:8a094db1347f | 66 | |
elijahsj | 1:8a094db1347f | 67 | // Save pin number for the read function |
elijahsj | 1:8a094db1347f | 68 | obj->pin = pin; |
elijahsj | 1:8a094db1347f | 69 | |
elijahsj | 1:8a094db1347f | 70 | // The ADC initialization is done once |
elijahsj | 1:8a094db1347f | 71 | if (adc_inited == 0) { |
elijahsj | 1:8a094db1347f | 72 | adc_inited = 1; |
elijahsj | 1:8a094db1347f | 73 | |
elijahsj | 1:8a094db1347f | 74 | // Enable ADC clock |
elijahsj | 1:8a094db1347f | 75 | __HAL_RCC_ADC_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 76 | __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); |
elijahsj | 1:8a094db1347f | 77 | |
elijahsj | 1:8a094db1347f | 78 | obj->handle.State = HAL_ADC_STATE_RESET; |
elijahsj | 1:8a094db1347f | 79 | // Configure ADC |
elijahsj | 1:8a094db1347f | 80 | obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock |
elijahsj | 1:8a094db1347f | 81 | obj->handle.Init.Resolution = ADC_RESOLUTION_12B; |
elijahsj | 1:8a094db1347f | 82 | obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
elijahsj | 1:8a094db1347f | 83 | obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) |
elijahsj | 1:8a094db1347f | 84 | obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example). |
elijahsj | 1:8a094db1347f | 85 | obj->handle.Init.LowPowerAutoWait = DISABLE; |
elijahsj | 1:8a094db1347f | 86 | obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig |
elijahsj | 1:8a094db1347f | 87 | obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled |
elijahsj | 1:8a094db1347f | 88 | obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled |
elijahsj | 1:8a094db1347f | 89 | obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled |
elijahsj | 1:8a094db1347f | 90 | obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event |
elijahsj | 1:8a094db1347f | 91 | obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
elijahsj | 1:8a094db1347f | 92 | obj->handle.Init.DMAContinuousRequests = DISABLE; |
elijahsj | 1:8a094db1347f | 93 | obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun |
elijahsj | 1:8a094db1347f | 94 | obj->handle.Init.OversamplingMode = DISABLE; // No oversampling |
elijahsj | 1:8a094db1347f | 95 | |
elijahsj | 1:8a094db1347f | 96 | if (HAL_ADC_Init(&obj->handle) != HAL_OK) { |
elijahsj | 1:8a094db1347f | 97 | error("Cannot initialize ADC\n"); |
elijahsj | 1:8a094db1347f | 98 | } |
elijahsj | 1:8a094db1347f | 99 | } |
elijahsj | 1:8a094db1347f | 100 | } |
elijahsj | 1:8a094db1347f | 101 | |
elijahsj | 1:8a094db1347f | 102 | static inline uint16_t adc_read(analogin_t *obj) |
elijahsj | 1:8a094db1347f | 103 | { |
elijahsj | 1:8a094db1347f | 104 | ADC_ChannelConfTypeDef sConfig = {0}; |
elijahsj | 1:8a094db1347f | 105 | |
elijahsj | 1:8a094db1347f | 106 | // Configure ADC channel |
elijahsj | 1:8a094db1347f | 107 | switch (obj->channel) { |
elijahsj | 1:8a094db1347f | 108 | case 0: |
elijahsj | 1:8a094db1347f | 109 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
elijahsj | 1:8a094db1347f | 110 | break; |
elijahsj | 1:8a094db1347f | 111 | case 1: |
elijahsj | 1:8a094db1347f | 112 | sConfig.Channel = ADC_CHANNEL_1; |
elijahsj | 1:8a094db1347f | 113 | break; |
elijahsj | 1:8a094db1347f | 114 | case 2: |
elijahsj | 1:8a094db1347f | 115 | sConfig.Channel = ADC_CHANNEL_2; |
elijahsj | 1:8a094db1347f | 116 | break; |
elijahsj | 1:8a094db1347f | 117 | case 3: |
elijahsj | 1:8a094db1347f | 118 | sConfig.Channel = ADC_CHANNEL_3; |
elijahsj | 1:8a094db1347f | 119 | break; |
elijahsj | 1:8a094db1347f | 120 | case 4: |
elijahsj | 1:8a094db1347f | 121 | sConfig.Channel = ADC_CHANNEL_4; |
elijahsj | 1:8a094db1347f | 122 | break; |
elijahsj | 1:8a094db1347f | 123 | case 5: |
elijahsj | 1:8a094db1347f | 124 | sConfig.Channel = ADC_CHANNEL_5; |
elijahsj | 1:8a094db1347f | 125 | break; |
elijahsj | 1:8a094db1347f | 126 | case 6: |
elijahsj | 1:8a094db1347f | 127 | sConfig.Channel = ADC_CHANNEL_6; |
elijahsj | 1:8a094db1347f | 128 | break; |
elijahsj | 1:8a094db1347f | 129 | case 7: |
elijahsj | 1:8a094db1347f | 130 | sConfig.Channel = ADC_CHANNEL_7; |
elijahsj | 1:8a094db1347f | 131 | break; |
elijahsj | 1:8a094db1347f | 132 | case 8: |
elijahsj | 1:8a094db1347f | 133 | sConfig.Channel = ADC_CHANNEL_8; |
elijahsj | 1:8a094db1347f | 134 | break; |
elijahsj | 1:8a094db1347f | 135 | case 9: |
elijahsj | 1:8a094db1347f | 136 | sConfig.Channel = ADC_CHANNEL_9; |
elijahsj | 1:8a094db1347f | 137 | break; |
elijahsj | 1:8a094db1347f | 138 | case 10: |
elijahsj | 1:8a094db1347f | 139 | sConfig.Channel = ADC_CHANNEL_10; |
elijahsj | 1:8a094db1347f | 140 | break; |
elijahsj | 1:8a094db1347f | 141 | case 11: |
elijahsj | 1:8a094db1347f | 142 | sConfig.Channel = ADC_CHANNEL_11; |
elijahsj | 1:8a094db1347f | 143 | break; |
elijahsj | 1:8a094db1347f | 144 | case 12: |
elijahsj | 1:8a094db1347f | 145 | sConfig.Channel = ADC_CHANNEL_12; |
elijahsj | 1:8a094db1347f | 146 | break; |
elijahsj | 1:8a094db1347f | 147 | case 13: |
elijahsj | 1:8a094db1347f | 148 | sConfig.Channel = ADC_CHANNEL_13; |
elijahsj | 1:8a094db1347f | 149 | break; |
elijahsj | 1:8a094db1347f | 150 | case 14: |
elijahsj | 1:8a094db1347f | 151 | sConfig.Channel = ADC_CHANNEL_14; |
elijahsj | 1:8a094db1347f | 152 | break; |
elijahsj | 1:8a094db1347f | 153 | case 15: |
elijahsj | 1:8a094db1347f | 154 | sConfig.Channel = ADC_CHANNEL_15; |
elijahsj | 1:8a094db1347f | 155 | break; |
elijahsj | 1:8a094db1347f | 156 | case 16: |
elijahsj | 1:8a094db1347f | 157 | sConfig.Channel = ADC_CHANNEL_16; |
elijahsj | 1:8a094db1347f | 158 | break; |
elijahsj | 1:8a094db1347f | 159 | case 17: |
elijahsj | 1:8a094db1347f | 160 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
elijahsj | 1:8a094db1347f | 161 | break; |
elijahsj | 1:8a094db1347f | 162 | case 18: |
elijahsj | 1:8a094db1347f | 163 | sConfig.Channel = ADC_CHANNEL_VBAT; |
elijahsj | 1:8a094db1347f | 164 | break; |
elijahsj | 1:8a094db1347f | 165 | default: |
elijahsj | 1:8a094db1347f | 166 | return 0; |
elijahsj | 1:8a094db1347f | 167 | } |
elijahsj | 1:8a094db1347f | 168 | |
elijahsj | 1:8a094db1347f | 169 | sConfig.Rank = ADC_REGULAR_RANK_1; |
elijahsj | 1:8a094db1347f | 170 | sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5; |
elijahsj | 1:8a094db1347f | 171 | sConfig.SingleDiff = ADC_SINGLE_ENDED; |
elijahsj | 1:8a094db1347f | 172 | sConfig.OffsetNumber = ADC_OFFSET_NONE; |
elijahsj | 1:8a094db1347f | 173 | sConfig.Offset = 0; |
elijahsj | 1:8a094db1347f | 174 | |
elijahsj | 1:8a094db1347f | 175 | HAL_ADC_ConfigChannel(&obj->handle, &sConfig); |
elijahsj | 1:8a094db1347f | 176 | |
elijahsj | 1:8a094db1347f | 177 | HAL_ADC_Start(&obj->handle); // Start conversion |
elijahsj | 1:8a094db1347f | 178 | |
elijahsj | 1:8a094db1347f | 179 | // Wait end of conversion and get value |
elijahsj | 1:8a094db1347f | 180 | if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { |
elijahsj | 1:8a094db1347f | 181 | return (HAL_ADC_GetValue(&obj->handle)); |
elijahsj | 1:8a094db1347f | 182 | } else { |
elijahsj | 1:8a094db1347f | 183 | return 0; |
elijahsj | 1:8a094db1347f | 184 | } |
elijahsj | 1:8a094db1347f | 185 | } |
elijahsj | 1:8a094db1347f | 186 | |
elijahsj | 1:8a094db1347f | 187 | uint16_t analogin_read_u16(analogin_t *obj) |
elijahsj | 1:8a094db1347f | 188 | { |
elijahsj | 1:8a094db1347f | 189 | uint16_t value = adc_read(obj); |
elijahsj | 1:8a094db1347f | 190 | // 12-bit to 16-bit conversion |
elijahsj | 1:8a094db1347f | 191 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
elijahsj | 1:8a094db1347f | 192 | return value; |
elijahsj | 1:8a094db1347f | 193 | } |
elijahsj | 1:8a094db1347f | 194 | |
elijahsj | 1:8a094db1347f | 195 | float analogin_read(analogin_t *obj) |
elijahsj | 1:8a094db1347f | 196 | { |
elijahsj | 1:8a094db1347f | 197 | uint16_t value = adc_read(obj); |
elijahsj | 1:8a094db1347f | 198 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
elijahsj | 1:8a094db1347f | 199 | } |
elijahsj | 1:8a094db1347f | 200 | |
elijahsj | 1:8a094db1347f | 201 | #endif |