test

Committer:
elijahsj
Date:
Mon Nov 09 00:02:47 2020 -0500
Revision:
1:8a094db1347f
test

Who changed what in which revision?

UserRevisionLine numberNew 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 "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 "PeripheralPins.h"
elijahsj 1:8a094db1347f 37 #include "mbed_error.h"
elijahsj 1:8a094db1347f 38
elijahsj 1:8a094db1347f 39 void analogin_init(analogin_t *obj, PinName pin)
elijahsj 1:8a094db1347f 40 {
elijahsj 1:8a094db1347f 41 uint32_t function = (uint32_t)NC;
elijahsj 1:8a094db1347f 42
elijahsj 1:8a094db1347f 43 #if defined(ADC1)
elijahsj 1:8a094db1347f 44 static int adc1_inited = 0;
elijahsj 1:8a094db1347f 45 #endif
elijahsj 1:8a094db1347f 46 #if defined(ADC2)
elijahsj 1:8a094db1347f 47 static int adc2_inited = 0;
elijahsj 1:8a094db1347f 48 #endif
elijahsj 1:8a094db1347f 49 #if defined(ADC3)
elijahsj 1:8a094db1347f 50 static int adc3_inited = 0;
elijahsj 1:8a094db1347f 51 #endif
elijahsj 1:8a094db1347f 52 // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
elijahsj 1:8a094db1347f 53 // are described in PinNames.h and PeripheralPins.c
elijahsj 1:8a094db1347f 54 // Pin value must be between 0xF0 and 0xFF
elijahsj 1:8a094db1347f 55 if ((pin < 0xF0) || (pin >= 0x100)) {
elijahsj 1:8a094db1347f 56 // Normal channels
elijahsj 1:8a094db1347f 57 // Get the peripheral name from the pin and assign it to the object
elijahsj 1:8a094db1347f 58 obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
elijahsj 1:8a094db1347f 59 function = pinmap_function(pin, PinMap_ADC);
elijahsj 1:8a094db1347f 60 // Configure GPIO
elijahsj 1:8a094db1347f 61 pinmap_pinout(pin, PinMap_ADC);
elijahsj 1:8a094db1347f 62 } else {
elijahsj 1:8a094db1347f 63 // Internal channels
elijahsj 1:8a094db1347f 64 obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
elijahsj 1:8a094db1347f 65 function = pinmap_function(pin, PinMap_ADC_Internal);
elijahsj 1:8a094db1347f 66 // No GPIO configuration for internal channels
elijahsj 1:8a094db1347f 67 }
elijahsj 1:8a094db1347f 68 MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
elijahsj 1:8a094db1347f 69 MBED_ASSERT(function != (uint32_t)NC);
elijahsj 1:8a094db1347f 70
elijahsj 1:8a094db1347f 71 obj->channel = STM_PIN_CHANNEL(function);
elijahsj 1:8a094db1347f 72
elijahsj 1:8a094db1347f 73 // Save pin number for the read function
elijahsj 1:8a094db1347f 74 obj->pin = pin;
elijahsj 1:8a094db1347f 75
elijahsj 1:8a094db1347f 76 // Check if ADC is already initialized
elijahsj 1:8a094db1347f 77 // Enable ADC clock
elijahsj 1:8a094db1347f 78 #if defined(ADC1)
elijahsj 1:8a094db1347f 79 if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
elijahsj 1:8a094db1347f 80 if ((ADCName)obj->handle.Instance == ADC_1) {
elijahsj 1:8a094db1347f 81 __HAL_RCC_ADC1_CLK_ENABLE();
elijahsj 1:8a094db1347f 82 adc1_inited = 1;
elijahsj 1:8a094db1347f 83 }
elijahsj 1:8a094db1347f 84 #endif
elijahsj 1:8a094db1347f 85 #if defined(ADC2)
elijahsj 1:8a094db1347f 86 if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
elijahsj 1:8a094db1347f 87 if ((ADCName)obj->handle.Instance == ADC_2) {
elijahsj 1:8a094db1347f 88 __HAL_RCC_ADC2_CLK_ENABLE();
elijahsj 1:8a094db1347f 89 adc2_inited = 1;
elijahsj 1:8a094db1347f 90 }
elijahsj 1:8a094db1347f 91 #endif
elijahsj 1:8a094db1347f 92 #if defined(ADC3)
elijahsj 1:8a094db1347f 93 if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
elijahsj 1:8a094db1347f 94 if ((ADCName)obj->handle.Instance == ADC_3) {
elijahsj 1:8a094db1347f 95 __HAL_RCC_ADC3_CLK_ENABLE();
elijahsj 1:8a094db1347f 96 adc3_inited = 1;
elijahsj 1:8a094db1347f 97 }
elijahsj 1:8a094db1347f 98 #endif
elijahsj 1:8a094db1347f 99
elijahsj 1:8a094db1347f 100 // Configure ADC
elijahsj 1:8a094db1347f 101 obj->handle.State = HAL_ADC_STATE_RESET;
elijahsj 1:8a094db1347f 102 obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
elijahsj 1:8a094db1347f 103 obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
elijahsj 1:8a094db1347f 104 obj->handle.Init.ScanConvMode = DISABLE;
elijahsj 1:8a094db1347f 105 obj->handle.Init.ContinuousConvMode = DISABLE;
elijahsj 1:8a094db1347f 106 obj->handle.Init.DiscontinuousConvMode = DISABLE;
elijahsj 1:8a094db1347f 107 obj->handle.Init.NbrOfDiscConversion = 0;
elijahsj 1:8a094db1347f 108 obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
elijahsj 1:8a094db1347f 109 obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
elijahsj 1:8a094db1347f 110 obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
elijahsj 1:8a094db1347f 111 obj->handle.Init.NbrOfConversion = 1;
elijahsj 1:8a094db1347f 112 obj->handle.Init.DMAContinuousRequests = DISABLE;
elijahsj 1:8a094db1347f 113 obj->handle.Init.EOCSelection = DISABLE;
elijahsj 1:8a094db1347f 114
elijahsj 1:8a094db1347f 115 if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
elijahsj 1:8a094db1347f 116 error("Cannot initialize ADC");
elijahsj 1:8a094db1347f 117 }
elijahsj 1:8a094db1347f 118 }
elijahsj 1:8a094db1347f 119
elijahsj 1:8a094db1347f 120 static inline uint16_t adc_read(analogin_t *obj)
elijahsj 1:8a094db1347f 121 {
elijahsj 1:8a094db1347f 122 ADC_ChannelConfTypeDef sConfig = {0};
elijahsj 1:8a094db1347f 123
elijahsj 1:8a094db1347f 124 // Configure ADC channel
elijahsj 1:8a094db1347f 125 sConfig.Rank = 1;
elijahsj 1:8a094db1347f 126 sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
elijahsj 1:8a094db1347f 127 sConfig.Offset = 0;
elijahsj 1:8a094db1347f 128
elijahsj 1:8a094db1347f 129 switch (obj->channel) {
elijahsj 1:8a094db1347f 130 case 0:
elijahsj 1:8a094db1347f 131 sConfig.Channel = ADC_CHANNEL_0;
elijahsj 1:8a094db1347f 132 break;
elijahsj 1:8a094db1347f 133 case 1:
elijahsj 1:8a094db1347f 134 sConfig.Channel = ADC_CHANNEL_1;
elijahsj 1:8a094db1347f 135 break;
elijahsj 1:8a094db1347f 136 case 2:
elijahsj 1:8a094db1347f 137 sConfig.Channel = ADC_CHANNEL_2;
elijahsj 1:8a094db1347f 138 break;
elijahsj 1:8a094db1347f 139 case 3:
elijahsj 1:8a094db1347f 140 sConfig.Channel = ADC_CHANNEL_3;
elijahsj 1:8a094db1347f 141 break;
elijahsj 1:8a094db1347f 142 case 4:
elijahsj 1:8a094db1347f 143 sConfig.Channel = ADC_CHANNEL_4;
elijahsj 1:8a094db1347f 144 break;
elijahsj 1:8a094db1347f 145 case 5:
elijahsj 1:8a094db1347f 146 sConfig.Channel = ADC_CHANNEL_5;
elijahsj 1:8a094db1347f 147 break;
elijahsj 1:8a094db1347f 148 case 6:
elijahsj 1:8a094db1347f 149 sConfig.Channel = ADC_CHANNEL_6;
elijahsj 1:8a094db1347f 150 break;
elijahsj 1:8a094db1347f 151 case 7:
elijahsj 1:8a094db1347f 152 sConfig.Channel = ADC_CHANNEL_7;
elijahsj 1:8a094db1347f 153 break;
elijahsj 1:8a094db1347f 154 case 8:
elijahsj 1:8a094db1347f 155 sConfig.Channel = ADC_CHANNEL_8;
elijahsj 1:8a094db1347f 156 break;
elijahsj 1:8a094db1347f 157 case 9:
elijahsj 1:8a094db1347f 158 sConfig.Channel = ADC_CHANNEL_9;
elijahsj 1:8a094db1347f 159 break;
elijahsj 1:8a094db1347f 160 case 10:
elijahsj 1:8a094db1347f 161 sConfig.Channel = ADC_CHANNEL_10;
elijahsj 1:8a094db1347f 162 break;
elijahsj 1:8a094db1347f 163 case 11:
elijahsj 1:8a094db1347f 164 sConfig.Channel = ADC_CHANNEL_11;
elijahsj 1:8a094db1347f 165 break;
elijahsj 1:8a094db1347f 166 case 12:
elijahsj 1:8a094db1347f 167 sConfig.Channel = ADC_CHANNEL_12;
elijahsj 1:8a094db1347f 168 break;
elijahsj 1:8a094db1347f 169 case 13:
elijahsj 1:8a094db1347f 170 sConfig.Channel = ADC_CHANNEL_13;
elijahsj 1:8a094db1347f 171 break;
elijahsj 1:8a094db1347f 172 case 14:
elijahsj 1:8a094db1347f 173 sConfig.Channel = ADC_CHANNEL_14;
elijahsj 1:8a094db1347f 174 break;
elijahsj 1:8a094db1347f 175 case 15:
elijahsj 1:8a094db1347f 176 sConfig.Channel = ADC_CHANNEL_15;
elijahsj 1:8a094db1347f 177 break;
elijahsj 1:8a094db1347f 178 case 16:
elijahsj 1:8a094db1347f 179 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
elijahsj 1:8a094db1347f 180 break;
elijahsj 1:8a094db1347f 181 case 17:
elijahsj 1:8a094db1347f 182 sConfig.Channel = ADC_CHANNEL_VREFINT;
elijahsj 1:8a094db1347f 183 break;
elijahsj 1:8a094db1347f 184 case 18:
elijahsj 1:8a094db1347f 185 sConfig.Channel = ADC_CHANNEL_VBAT;
elijahsj 1:8a094db1347f 186 break;
elijahsj 1:8a094db1347f 187 default:
elijahsj 1:8a094db1347f 188 return 0;
elijahsj 1:8a094db1347f 189 }
elijahsj 1:8a094db1347f 190
elijahsj 1:8a094db1347f 191 if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
elijahsj 1:8a094db1347f 192 error("Cannot configure ADC channel");
elijahsj 1:8a094db1347f 193 }
elijahsj 1:8a094db1347f 194
elijahsj 1:8a094db1347f 195 HAL_ADC_Start(&obj->handle); // Start conversion
elijahsj 1:8a094db1347f 196
elijahsj 1:8a094db1347f 197 // Wait end of conversion and get value
elijahsj 1:8a094db1347f 198 HAL_ADC_PollForConversion(&obj->handle, 10);
elijahsj 1:8a094db1347f 199 if (HAL_ADC_GetState(&obj->handle) & HAL_ADC_STATE_EOC_REG) {
elijahsj 1:8a094db1347f 200 return (HAL_ADC_GetValue(&obj->handle));
elijahsj 1:8a094db1347f 201 } else {
elijahsj 1:8a094db1347f 202 return 0;
elijahsj 1:8a094db1347f 203 }
elijahsj 1:8a094db1347f 204 }
elijahsj 1:8a094db1347f 205
elijahsj 1:8a094db1347f 206 uint16_t analogin_read_u16(analogin_t *obj)
elijahsj 1:8a094db1347f 207 {
elijahsj 1:8a094db1347f 208 uint16_t value = adc_read(obj);
elijahsj 1:8a094db1347f 209 // 12-bit to 16-bit conversion
elijahsj 1:8a094db1347f 210 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
elijahsj 1:8a094db1347f 211 return value;
elijahsj 1:8a094db1347f 212 }
elijahsj 1:8a094db1347f 213
elijahsj 1:8a094db1347f 214 float analogin_read(analogin_t *obj)
elijahsj 1:8a094db1347f 215 {
elijahsj 1:8a094db1347f 216 uint16_t value = adc_read(obj);
elijahsj 1:8a094db1347f 217 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
elijahsj 1:8a094db1347f 218 }
elijahsj 1:8a094db1347f 219
elijahsj 1:8a094db1347f 220 #endif