Mbed for VNG board
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/analogin_api.c@414:4ec4c5b614b0, 2014-11-25 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 25 07:15:09 2014 +0000
- Revision:
- 414:4ec4c5b614b0
- Parent:
- 402:09075a3b15e3
Synchronized with git revision fbc74e874ac089c6cb05add312fb5422be628886
Full URL: https://github.com/mbedmicro/mbed/commit/fbc74e874ac089c6cb05add312fb5422be628886/
Targets: NUCLEOs - Add PeripheralPins files for all nucleo targets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 52:a51c77007319 | 1 | /* mbed Microcontroller Library |
mbed_official | 70:c1fbde68b492 | 2 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 70:c1fbde68b492 | 3 | * All rights reserved. |
mbed_official | 52:a51c77007319 | 4 | * |
mbed_official | 70:c1fbde68b492 | 5 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 70:c1fbde68b492 | 6 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 52:a51c77007319 | 7 | * |
mbed_official | 70:c1fbde68b492 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 70:c1fbde68b492 | 9 | * this list of conditions and the following disclaimer. |
mbed_official | 70:c1fbde68b492 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 70:c1fbde68b492 | 11 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 70:c1fbde68b492 | 12 | * and/or other materials provided with the distribution. |
mbed_official | 70:c1fbde68b492 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 70:c1fbde68b492 | 14 | * may be used to endorse or promote products derived from this software |
mbed_official | 70:c1fbde68b492 | 15 | * without specific prior written permission. |
mbed_official | 52:a51c77007319 | 16 | * |
mbed_official | 70:c1fbde68b492 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 70:c1fbde68b492 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 70:c1fbde68b492 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 70:c1fbde68b492 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 70:c1fbde68b492 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 70:c1fbde68b492 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 70:c1fbde68b492 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 70:c1fbde68b492 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 70:c1fbde68b492 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 70:c1fbde68b492 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 52:a51c77007319 | 27 | */ |
mbed_official | 227:7bd0639b8911 | 28 | #include "mbed_assert.h" |
mbed_official | 52:a51c77007319 | 29 | #include "analogin_api.h" |
mbed_official | 52:a51c77007319 | 30 | |
mbed_official | 52:a51c77007319 | 31 | #if DEVICE_ANALOGIN |
mbed_official | 52:a51c77007319 | 32 | |
mbed_official | 52:a51c77007319 | 33 | #include "cmsis.h" |
mbed_official | 52:a51c77007319 | 34 | #include "pinmap.h" |
mbed_official | 174:8bb9f3a33240 | 35 | #include "wait_api.h" |
mbed_official | 414:4ec4c5b614b0 | 36 | #include "PeripheralPins.h" |
mbed_official | 52:a51c77007319 | 37 | |
mbed_official | 52:a51c77007319 | 38 | int adc_inited = 0; |
mbed_official | 52:a51c77007319 | 39 | |
mbed_official | 402:09075a3b15e3 | 40 | void analogin_init(analogin_t *obj, PinName pin) |
mbed_official | 402:09075a3b15e3 | 41 | { |
mbed_official | 174:8bb9f3a33240 | 42 | ADC_TypeDef *adc; |
mbed_official | 52:a51c77007319 | 43 | ADC_InitTypeDef ADC_InitStructure; |
mbed_official | 174:8bb9f3a33240 | 44 | |
mbed_official | 167:d5744491c362 | 45 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 52:a51c77007319 | 46 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 227:7bd0639b8911 | 47 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mbed_official | 52:a51c77007319 | 48 | |
mbed_official | 52:a51c77007319 | 49 | // Configure GPIO |
mbed_official | 52:a51c77007319 | 50 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 52:a51c77007319 | 51 | |
mbed_official | 52:a51c77007319 | 52 | // Save pin number for the read function |
mbed_official | 52:a51c77007319 | 53 | obj->pin = pin; |
mbed_official | 52:a51c77007319 | 54 | |
mbed_official | 52:a51c77007319 | 55 | // The ADC initialization is done once |
mbed_official | 52:a51c77007319 | 56 | if (adc_inited == 0) { |
mbed_official | 52:a51c77007319 | 57 | adc_inited = 1; |
mbed_official | 52:a51c77007319 | 58 | |
mbed_official | 52:a51c77007319 | 59 | // Get ADC registers structure address |
mbed_official | 52:a51c77007319 | 60 | adc = (ADC_TypeDef *)(obj->adc); |
mbed_official | 174:8bb9f3a33240 | 61 | |
mbed_official | 167:d5744491c362 | 62 | // Enable ADC clock (14 MHz maximum) |
mbed_official | 167:d5744491c362 | 63 | // PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.666 MHz |
mbed_official | 167:d5744491c362 | 64 | RCC_ADCCLKConfig(RCC_PCLK2_Div6); |
mbed_official | 52:a51c77007319 | 65 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); |
mbed_official | 174:8bb9f3a33240 | 66 | |
mbed_official | 52:a51c77007319 | 67 | // Configure ADC |
mbed_official | 167:d5744491c362 | 68 | ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; |
mbed_official | 167:d5744491c362 | 69 | ADC_InitStructure.ADC_ScanConvMode = DISABLE; |
mbed_official | 52:a51c77007319 | 70 | ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; |
mbed_official | 167:d5744491c362 | 71 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; |
mbed_official | 167:d5744491c362 | 72 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; |
mbed_official | 167:d5744491c362 | 73 | ADC_InitStructure.ADC_NbrOfChannel = 1; |
mbed_official | 52:a51c77007319 | 74 | ADC_Init(adc, &ADC_InitStructure); |
mbed_official | 52:a51c77007319 | 75 | |
mbed_official | 52:a51c77007319 | 76 | // Enable ADC |
mbed_official | 52:a51c77007319 | 77 | ADC_Cmd(adc, ENABLE); |
mbed_official | 52:a51c77007319 | 78 | |
mbed_official | 52:a51c77007319 | 79 | // Calibrate ADC |
mbed_official | 52:a51c77007319 | 80 | ADC_ResetCalibration(adc); |
mbed_official | 174:8bb9f3a33240 | 81 | while (ADC_GetResetCalibrationStatus(adc)); |
mbed_official | 52:a51c77007319 | 82 | ADC_StartCalibration(adc); |
mbed_official | 174:8bb9f3a33240 | 83 | while (ADC_GetCalibrationStatus(adc)); |
mbed_official | 52:a51c77007319 | 84 | } |
mbed_official | 52:a51c77007319 | 85 | } |
mbed_official | 52:a51c77007319 | 86 | |
mbed_official | 402:09075a3b15e3 | 87 | static inline uint16_t adc_read(analogin_t *obj) |
mbed_official | 402:09075a3b15e3 | 88 | { |
mbed_official | 174:8bb9f3a33240 | 89 | // Get ADC registers structure address |
mbed_official | 174:8bb9f3a33240 | 90 | ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc); |
mbed_official | 174:8bb9f3a33240 | 91 | int channel = 0; |
mbed_official | 52:a51c77007319 | 92 | |
mbed_official | 174:8bb9f3a33240 | 93 | // Configure ADC channel |
mbed_official | 174:8bb9f3a33240 | 94 | switch (obj->pin) { |
mbed_official | 174:8bb9f3a33240 | 95 | case PA_0: |
mbed_official | 174:8bb9f3a33240 | 96 | channel = 0; |
mbed_official | 174:8bb9f3a33240 | 97 | break; |
mbed_official | 174:8bb9f3a33240 | 98 | case PA_1: |
mbed_official | 174:8bb9f3a33240 | 99 | channel = 1; |
mbed_official | 174:8bb9f3a33240 | 100 | break; |
mbed_official | 174:8bb9f3a33240 | 101 | case PA_2: |
mbed_official | 174:8bb9f3a33240 | 102 | channel = 2; |
mbed_official | 174:8bb9f3a33240 | 103 | break; |
mbed_official | 174:8bb9f3a33240 | 104 | case PA_3: |
mbed_official | 174:8bb9f3a33240 | 105 | channel = 3; |
mbed_official | 174:8bb9f3a33240 | 106 | break; |
mbed_official | 174:8bb9f3a33240 | 107 | case PA_4: |
mbed_official | 174:8bb9f3a33240 | 108 | channel = 4; |
mbed_official | 174:8bb9f3a33240 | 109 | break; |
mbed_official | 174:8bb9f3a33240 | 110 | case PA_5: |
mbed_official | 174:8bb9f3a33240 | 111 | channel = 5; |
mbed_official | 174:8bb9f3a33240 | 112 | break; |
mbed_official | 174:8bb9f3a33240 | 113 | case PA_6: |
mbed_official | 174:8bb9f3a33240 | 114 | channel = 6; |
mbed_official | 174:8bb9f3a33240 | 115 | break; |
mbed_official | 174:8bb9f3a33240 | 116 | case PA_7: |
mbed_official | 174:8bb9f3a33240 | 117 | channel = 7; |
mbed_official | 174:8bb9f3a33240 | 118 | break; |
mbed_official | 174:8bb9f3a33240 | 119 | case PB_0: |
mbed_official | 174:8bb9f3a33240 | 120 | channel = 8; |
mbed_official | 174:8bb9f3a33240 | 121 | break; |
mbed_official | 174:8bb9f3a33240 | 122 | case PB_1: |
mbed_official | 174:8bb9f3a33240 | 123 | channel = 9; |
mbed_official | 174:8bb9f3a33240 | 124 | break; |
mbed_official | 174:8bb9f3a33240 | 125 | case PC_0: |
mbed_official | 174:8bb9f3a33240 | 126 | channel = 10; |
mbed_official | 174:8bb9f3a33240 | 127 | break; |
mbed_official | 174:8bb9f3a33240 | 128 | case PC_1: |
mbed_official | 174:8bb9f3a33240 | 129 | channel = 11; |
mbed_official | 174:8bb9f3a33240 | 130 | break; |
mbed_official | 174:8bb9f3a33240 | 131 | case PC_2: |
mbed_official | 174:8bb9f3a33240 | 132 | channel = 12; |
mbed_official | 174:8bb9f3a33240 | 133 | break; |
mbed_official | 174:8bb9f3a33240 | 134 | case PC_3: |
mbed_official | 174:8bb9f3a33240 | 135 | channel = 13; |
mbed_official | 174:8bb9f3a33240 | 136 | break; |
mbed_official | 174:8bb9f3a33240 | 137 | case PC_4: |
mbed_official | 174:8bb9f3a33240 | 138 | channel = 14; |
mbed_official | 174:8bb9f3a33240 | 139 | break; |
mbed_official | 174:8bb9f3a33240 | 140 | case PC_5: |
mbed_official | 174:8bb9f3a33240 | 141 | channel = 15; |
mbed_official | 174:8bb9f3a33240 | 142 | break; |
mbed_official | 174:8bb9f3a33240 | 143 | default: |
mbed_official | 174:8bb9f3a33240 | 144 | return 0; |
mbed_official | 174:8bb9f3a33240 | 145 | } |
mbed_official | 174:8bb9f3a33240 | 146 | |
mbed_official | 174:8bb9f3a33240 | 147 | ADC_RegularChannelConfig(adc, channel, 1, ADC_SampleTime_7Cycles5); |
mbed_official | 174:8bb9f3a33240 | 148 | |
mbed_official | 174:8bb9f3a33240 | 149 | ADC_SoftwareStartConvCmd(adc, ENABLE); // Start conversion |
mbed_official | 174:8bb9f3a33240 | 150 | |
mbed_official | 174:8bb9f3a33240 | 151 | while (ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion |
mbed_official | 174:8bb9f3a33240 | 152 | |
mbed_official | 174:8bb9f3a33240 | 153 | return (ADC_GetConversionValue(adc)); // Get conversion value |
mbed_official | 52:a51c77007319 | 154 | } |
mbed_official | 52:a51c77007319 | 155 | |
mbed_official | 402:09075a3b15e3 | 156 | uint16_t analogin_read_u16(analogin_t *obj) |
mbed_official | 402:09075a3b15e3 | 157 | { |
mbed_official | 298:7557d401dbc3 | 158 | uint16_t value = adc_read(obj); |
mbed_official | 298:7557d401dbc3 | 159 | // 12-bit to 16-bit conversion |
mbed_official | 298:7557d401dbc3 | 160 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
mbed_official | 298:7557d401dbc3 | 161 | return value; |
mbed_official | 52:a51c77007319 | 162 | } |
mbed_official | 52:a51c77007319 | 163 | |
mbed_official | 402:09075a3b15e3 | 164 | float analogin_read(analogin_t *obj) |
mbed_official | 402:09075a3b15e3 | 165 | { |
mbed_official | 174:8bb9f3a33240 | 166 | uint16_t value = adc_read(obj); |
mbed_official | 174:8bb9f3a33240 | 167 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
mbed_official | 52:a51c77007319 | 168 | } |
mbed_official | 52:a51c77007319 | 169 | |
mbed_official | 52:a51c77007319 | 170 | #endif |