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