mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_WIZNET/TARGET_W7500x/analogin_api.c@165:2dd56e6daeec, 2017-05-23 (annotated)
- Committer:
- ranaumarnaeem
- Date:
- Tue May 23 12:54:50 2017 +0000
- Revision:
- 165:2dd56e6daeec
- Parent:
- 160:d5399cc887bb
jhjg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 3 | * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved. |
<> | 144:ef7eb2e8f9f7 | 4 | * All rights reserved. |
<> | 144:ef7eb2e8f9f7 | 5 | * |
<> | 144:ef7eb2e8f9f7 | 6 | * Redistribution and use in source and binary forms, with or without |
<> | 144:ef7eb2e8f9f7 | 7 | * modification, are permitted provided that the following conditions are met: |
<> | 144:ef7eb2e8f9f7 | 8 | * |
<> | 144:ef7eb2e8f9f7 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
<> | 144:ef7eb2e8f9f7 | 10 | * this list of conditions and the following disclaimer. |
<> | 144:ef7eb2e8f9f7 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
<> | 144:ef7eb2e8f9f7 | 12 | * this list of conditions and the following disclaimer in the documentation |
<> | 144:ef7eb2e8f9f7 | 13 | * and/or other materials provided with the distribution. |
<> | 144:ef7eb2e8f9f7 | 14 | * 3. Neither the name of ARM Limited nor the names of its contributors |
<> | 144:ef7eb2e8f9f7 | 15 | * may be used to endorse or promote products derived from this software |
<> | 144:ef7eb2e8f9f7 | 16 | * without specific prior written permission. |
<> | 144:ef7eb2e8f9f7 | 17 | * |
<> | 144:ef7eb2e8f9f7 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
<> | 144:ef7eb2e8f9f7 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
<> | 144:ef7eb2e8f9f7 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
<> | 144:ef7eb2e8f9f7 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
<> | 144:ef7eb2e8f9f7 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
<> | 144:ef7eb2e8f9f7 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
<> | 144:ef7eb2e8f9f7 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
<> | 144:ef7eb2e8f9f7 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
<> | 144:ef7eb2e8f9f7 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
<> | 144:ef7eb2e8f9f7 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
<> | 144:ef7eb2e8f9f7 | 28 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 29 | */ |
<> | 144:ef7eb2e8f9f7 | 30 | |
<> | 144:ef7eb2e8f9f7 | 31 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 32 | #include "analogin_api.h" |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | #if DEVICE_ANALOGIN |
<> | 144:ef7eb2e8f9f7 | 35 | |
<> | 160:d5399cc887bb | 36 | #include "mbed_wait_api.h" |
<> | 144:ef7eb2e8f9f7 | 37 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 38 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 39 | #include "W7500x_adc.h" |
<> | 144:ef7eb2e8f9f7 | 40 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 41 | |
<> | 144:ef7eb2e8f9f7 | 42 | |
<> | 144:ef7eb2e8f9f7 | 43 | ADC_TypeDef * AdcHandle; |
<> | 144:ef7eb2e8f9f7 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | static int adc_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 46 | |
<> | 144:ef7eb2e8f9f7 | 47 | void analogin_init(analogin_t *obj, PinName pin) |
<> | 144:ef7eb2e8f9f7 | 48 | { |
<> | 144:ef7eb2e8f9f7 | 49 | // Get the peripheral name from the pin and assign it to the object |
<> | 144:ef7eb2e8f9f7 | 50 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 51 | |
<> | 144:ef7eb2e8f9f7 | 52 | MBED_ASSERT(obj->adc != (ADCName)NC); |
<> | 144:ef7eb2e8f9f7 | 53 | |
<> | 144:ef7eb2e8f9f7 | 54 | // Configure GPIO |
<> | 144:ef7eb2e8f9f7 | 55 | pinmap_pinout(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 56 | |
<> | 144:ef7eb2e8f9f7 | 57 | // Save pin number for the read function |
<> | 144:ef7eb2e8f9f7 | 58 | obj->pin = pin; |
<> | 144:ef7eb2e8f9f7 | 59 | |
<> | 144:ef7eb2e8f9f7 | 60 | // The ADC initialization is done once |
<> | 144:ef7eb2e8f9f7 | 61 | if (adc_inited == 0) { |
<> | 144:ef7eb2e8f9f7 | 62 | adc_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 63 | |
<> | 144:ef7eb2e8f9f7 | 64 | ADC_Init(); |
<> | 144:ef7eb2e8f9f7 | 65 | } |
<> | 144:ef7eb2e8f9f7 | 66 | } |
<> | 144:ef7eb2e8f9f7 | 67 | |
<> | 144:ef7eb2e8f9f7 | 68 | static inline uint16_t adc_read(analogin_t *obj) |
<> | 144:ef7eb2e8f9f7 | 69 | { |
<> | 144:ef7eb2e8f9f7 | 70 | ADC_CH adc_ch; |
<> | 144:ef7eb2e8f9f7 | 71 | |
<> | 144:ef7eb2e8f9f7 | 72 | switch (obj->pin) { |
<> | 144:ef7eb2e8f9f7 | 73 | case PC_15: |
<> | 144:ef7eb2e8f9f7 | 74 | adc_ch = ADC_CH0; |
<> | 144:ef7eb2e8f9f7 | 75 | break; |
<> | 144:ef7eb2e8f9f7 | 76 | case PC_14: |
<> | 144:ef7eb2e8f9f7 | 77 | adc_ch = ADC_CH1; |
<> | 144:ef7eb2e8f9f7 | 78 | break; |
<> | 144:ef7eb2e8f9f7 | 79 | case PC_13: |
<> | 144:ef7eb2e8f9f7 | 80 | adc_ch = ADC_CH2; |
<> | 144:ef7eb2e8f9f7 | 81 | break; |
<> | 144:ef7eb2e8f9f7 | 82 | case PC_12: |
<> | 144:ef7eb2e8f9f7 | 83 | adc_ch = ADC_CH3; |
<> | 144:ef7eb2e8f9f7 | 84 | break; |
<> | 144:ef7eb2e8f9f7 | 85 | case PC_11: |
<> | 144:ef7eb2e8f9f7 | 86 | adc_ch = ADC_CH4; |
<> | 144:ef7eb2e8f9f7 | 87 | break; |
<> | 144:ef7eb2e8f9f7 | 88 | case PC_10: |
<> | 144:ef7eb2e8f9f7 | 89 | adc_ch = ADC_CH5; |
<> | 144:ef7eb2e8f9f7 | 90 | break; |
<> | 144:ef7eb2e8f9f7 | 91 | default: |
<> | 144:ef7eb2e8f9f7 | 92 | return 0; |
<> | 144:ef7eb2e8f9f7 | 93 | } |
<> | 144:ef7eb2e8f9f7 | 94 | |
<> | 144:ef7eb2e8f9f7 | 95 | ADC_ChannelSelect(adc_ch); |
<> | 144:ef7eb2e8f9f7 | 96 | ADC_Start(); |
<> | 144:ef7eb2e8f9f7 | 97 | |
<> | 144:ef7eb2e8f9f7 | 98 | // Wait end of conversion and get value |
<> | 144:ef7eb2e8f9f7 | 99 | while(ADC_IsInterrupt()); |
<> | 144:ef7eb2e8f9f7 | 100 | return ADC_ReadData(); |
<> | 144:ef7eb2e8f9f7 | 101 | } |
<> | 144:ef7eb2e8f9f7 | 102 | |
<> | 144:ef7eb2e8f9f7 | 103 | uint16_t analogin_read_u16(analogin_t *obj) |
<> | 144:ef7eb2e8f9f7 | 104 | { |
<> | 144:ef7eb2e8f9f7 | 105 | uint16_t value = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 106 | // 12-bit to 16-bit conversion |
<> | 144:ef7eb2e8f9f7 | 107 | return value; |
<> | 144:ef7eb2e8f9f7 | 108 | } |
<> | 144:ef7eb2e8f9f7 | 109 | |
<> | 144:ef7eb2e8f9f7 | 110 | float analogin_read(analogin_t *obj) |
<> | 144:ef7eb2e8f9f7 | 111 | { |
<> | 144:ef7eb2e8f9f7 | 112 | uint16_t value = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 113 | |
<> | 144:ef7eb2e8f9f7 | 114 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
<> | 144:ef7eb2e8f9f7 | 115 | } |
<> | 144:ef7eb2e8f9f7 | 116 | |
<> | 144:ef7eb2e8f9f7 | 117 | #endif |