mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32F4XX/analogin_api.c@285:31249416b6f9, 2014-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 15 16:30:08 2014 +0100
- Revision:
- 285:31249416b6f9
- Parent:
- 251:de9a1e4ffd79
Synchronized with git revision 601712595f49bbd2a2771c52cf3e84c9c7a591af
Full URL: https://github.com/mbedmicro/mbed/commit/601712595f49bbd2a2771c52cf3e84c9c7a591af/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 20:4263a77256ae | 1 | /* mbed Microcontroller Library |
bogdanm | 20:4263a77256ae | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 20:4263a77256ae | 3 | * |
bogdanm | 20:4263a77256ae | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 20:4263a77256ae | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 20:4263a77256ae | 6 | * You may obtain a copy of the License at |
bogdanm | 20:4263a77256ae | 7 | * |
bogdanm | 20:4263a77256ae | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 20:4263a77256ae | 9 | * |
bogdanm | 20:4263a77256ae | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 20:4263a77256ae | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 20:4263a77256ae | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 20:4263a77256ae | 13 | * See the License for the specific language governing permissions and |
bogdanm | 20:4263a77256ae | 14 | * limitations under the License. |
bogdanm | 20:4263a77256ae | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
bogdanm | 20:4263a77256ae | 17 | #include "analogin_api.h" |
bogdanm | 20:4263a77256ae | 18 | |
bogdanm | 20:4263a77256ae | 19 | #if DEVICE_ANALOGIN |
bogdanm | 20:4263a77256ae | 20 | |
bogdanm | 20:4263a77256ae | 21 | #include "cmsis.h" |
bogdanm | 20:4263a77256ae | 22 | #include "pinmap.h" |
mbed_official | 285:31249416b6f9 | 23 | #include "mbed_error.h" |
bogdanm | 20:4263a77256ae | 24 | |
bogdanm | 20:4263a77256ae | 25 | #define ADC_10BIT_RANGE 0x3FF |
bogdanm | 20:4263a77256ae | 26 | #define ADC_12BIT_RANGE 0xFFF |
bogdanm | 20:4263a77256ae | 27 | |
bogdanm | 20:4263a77256ae | 28 | static const PinMap PinMap_ADC[] = { |
bogdanm | 20:4263a77256ae | 29 | {PA_0, ADC0_0, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 30 | {PA_1, ADC0_1, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 31 | {PA_2, ADC0_2, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 32 | {PA_3, ADC0_3, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 33 | {PA_4, ADC0_4, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 34 | {PA_5, ADC0_5, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 35 | {PA_6, ADC0_6, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 36 | {PA_7, ADC0_7, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 37 | {PB_0, ADC0_8, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 38 | {PB_1, ADC0_9, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 39 | {PC_0, ADC0_10, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 40 | {PC_1, ADC0_11, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 41 | {PC_2, ADC0_12, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 42 | {PC_3, ADC0_13, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 43 | {PC_4, ADC0_14, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 44 | {PC_5, ADC0_15, STM_PIN_DATA(3, 0)}, |
bogdanm | 20:4263a77256ae | 45 | {NC, NC, 0} |
bogdanm | 20:4263a77256ae | 46 | }; |
bogdanm | 20:4263a77256ae | 47 | |
bogdanm | 20:4263a77256ae | 48 | # define ADC_RANGE ADC_12BIT_RANGE |
bogdanm | 20:4263a77256ae | 49 | |
bogdanm | 20:4263a77256ae | 50 | void analogin_init(analogin_t *obj, PinName pin) { |
bogdanm | 20:4263a77256ae | 51 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 227:7bd0639b8911 | 52 | MBED_ASSERT(obj->adc != (uint32_t)NC); |
bogdanm | 20:4263a77256ae | 53 | |
bogdanm | 20:4263a77256ae | 54 | // ensure power is turned on |
bogdanm | 20:4263a77256ae | 55 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | |
bogdanm | 20:4263a77256ae | 56 | RCC_AHB1ENR_GPIOCEN; |
bogdanm | 20:4263a77256ae | 57 | RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; |
bogdanm | 20:4263a77256ae | 58 | |
bogdanm | 20:4263a77256ae | 59 | // Enable the ADC |
bogdanm | 20:4263a77256ae | 60 | ADC1->CR2 |= ADC_CR2_ADON; |
bogdanm | 20:4263a77256ae | 61 | |
bogdanm | 20:4263a77256ae | 62 | pinmap_pinout(pin, PinMap_ADC); |
bogdanm | 20:4263a77256ae | 63 | } |
bogdanm | 20:4263a77256ae | 64 | |
bogdanm | 20:4263a77256ae | 65 | static inline uint32_t adc_read(analogin_t *obj) { |
bogdanm | 20:4263a77256ae | 66 | // Select the appropriate channel |
bogdanm | 20:4263a77256ae | 67 | ADC1->SQR3 = (int) obj->adc; |
bogdanm | 20:4263a77256ae | 68 | |
bogdanm | 20:4263a77256ae | 69 | // Start conversion |
bogdanm | 20:4263a77256ae | 70 | ADC1->CR2 |= ADC_CR2_SWSTART; |
bogdanm | 20:4263a77256ae | 71 | |
bogdanm | 20:4263a77256ae | 72 | // Wait for conversion to finish |
bogdanm | 20:4263a77256ae | 73 | while (!(ADC1->SR & ADC_SR_EOC)); |
bogdanm | 20:4263a77256ae | 74 | |
bogdanm | 20:4263a77256ae | 75 | uint32_t data = ADC1->DR; |
bogdanm | 20:4263a77256ae | 76 | return data; // 12 bit |
bogdanm | 20:4263a77256ae | 77 | } |
bogdanm | 20:4263a77256ae | 78 | |
bogdanm | 20:4263a77256ae | 79 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
bogdanm | 20:4263a77256ae | 80 | uint32_t value; |
bogdanm | 20:4263a77256ae | 81 | value = adc_read(obj); |
bogdanm | 20:4263a77256ae | 82 | return value; |
bogdanm | 20:4263a77256ae | 83 | } |
bogdanm | 20:4263a77256ae | 84 | |
bogdanm | 20:4263a77256ae | 85 | uint16_t analogin_read_u16(analogin_t *obj) { |
bogdanm | 20:4263a77256ae | 86 | uint32_t value = adc_read_u32(obj); |
bogdanm | 20:4263a77256ae | 87 | |
bogdanm | 20:4263a77256ae | 88 | return (value << 4) | ((value >> 8) & 0x000F); // 12 bit |
bogdanm | 20:4263a77256ae | 89 | } |
bogdanm | 20:4263a77256ae | 90 | |
bogdanm | 20:4263a77256ae | 91 | float analogin_read(analogin_t *obj) { |
bogdanm | 20:4263a77256ae | 92 | uint32_t value = adc_read_u32(obj); |
bogdanm | 20:4263a77256ae | 93 | return (float)value * (1.0f / (float)ADC_RANGE); |
bogdanm | 20:4263a77256ae | 94 | } |
bogdanm | 20:4263a77256ae | 95 | |
bogdanm | 20:4263a77256ae | 96 | #endif |