helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
172:7d866c31b3c5
This updates the lib to the mbed lib v 150

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 172:7d866c31b3c5 1 /* mbed Microcontroller Library
AnnaBridge 172:7d866c31b3c5 2 * Copyright (c) 2015-2016 Nuvoton
AnnaBridge 172:7d866c31b3c5 3 *
AnnaBridge 172:7d866c31b3c5 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 172:7d866c31b3c5 5 * you may not use this file except in compliance with the License.
AnnaBridge 172:7d866c31b3c5 6 * You may obtain a copy of the License at
AnnaBridge 172:7d866c31b3c5 7 *
AnnaBridge 172:7d866c31b3c5 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 172:7d866c31b3c5 9 *
AnnaBridge 172:7d866c31b3c5 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 172:7d866c31b3c5 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 172:7d866c31b3c5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 172:7d866c31b3c5 13 * See the License for the specific language governing permissions and
AnnaBridge 172:7d866c31b3c5 14 * limitations under the License.
AnnaBridge 172:7d866c31b3c5 15 */
AnnaBridge 172:7d866c31b3c5 16
AnnaBridge 172:7d866c31b3c5 17 #include "analogin_api.h"
AnnaBridge 172:7d866c31b3c5 18
AnnaBridge 172:7d866c31b3c5 19 #if DEVICE_ANALOGIN
AnnaBridge 172:7d866c31b3c5 20
AnnaBridge 172:7d866c31b3c5 21 #include "cmsis.h"
AnnaBridge 172:7d866c31b3c5 22 #include "pinmap.h"
AnnaBridge 172:7d866c31b3c5 23 #include "PeripheralPins.h"
AnnaBridge 172:7d866c31b3c5 24 #include "nu_modutil.h"
AnnaBridge 172:7d866c31b3c5 25
AnnaBridge 172:7d866c31b3c5 26 static uint32_t eadc_modinit_mask = 0;
AnnaBridge 172:7d866c31b3c5 27
AnnaBridge 172:7d866c31b3c5 28 static const struct nu_modinit_s adc_modinit_tab[] = {
AnnaBridge 172:7d866c31b3c5 29 {ADC_0_0, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 30 {ADC_0_1, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 31 {ADC_0_2, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 32 {ADC_0_3, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 33 {ADC_0_4, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 34 {ADC_0_5, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 35 {ADC_0_6, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 36 {ADC_0_7, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 37 {ADC_0_8, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 38 {ADC_0_9, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 39 {ADC_0_10, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 40 {ADC_0_11, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 41 {ADC_0_12, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 42 {ADC_0_13, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 43 {ADC_0_14, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 44 {ADC_0_15, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC0_IRQn, NULL},
AnnaBridge 172:7d866c31b3c5 45 };
AnnaBridge 172:7d866c31b3c5 46
AnnaBridge 172:7d866c31b3c5 47 void analogin_init(analogin_t *obj, PinName pin)
AnnaBridge 172:7d866c31b3c5 48 {
AnnaBridge 172:7d866c31b3c5 49 obj->adc = (ADCName) pinmap_peripheral(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 50 MBED_ASSERT(obj->adc != (ADCName) NC);
AnnaBridge 172:7d866c31b3c5 51
AnnaBridge 172:7d866c31b3c5 52 const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
AnnaBridge 172:7d866c31b3c5 53 MBED_ASSERT(modinit != NULL);
AnnaBridge 172:7d866c31b3c5 54 MBED_ASSERT(modinit->modname == (int) obj->adc);
AnnaBridge 172:7d866c31b3c5 55
AnnaBridge 172:7d866c31b3c5 56 EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
AnnaBridge 172:7d866c31b3c5 57
AnnaBridge 172:7d866c31b3c5 58 // NOTE: All channels (identified by ADCName) share a ADC module. This reset will also affect other channels of the same ADC module.
AnnaBridge 172:7d866c31b3c5 59 if (! eadc_modinit_mask) {
AnnaBridge 172:7d866c31b3c5 60 // Reset this module if no channel enabled
AnnaBridge 172:7d866c31b3c5 61 SYS_ResetModule(modinit->rsetidx);
AnnaBridge 172:7d866c31b3c5 62
AnnaBridge 172:7d866c31b3c5 63 // Select clock source of paired channels
AnnaBridge 172:7d866c31b3c5 64 CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
AnnaBridge 172:7d866c31b3c5 65 // Enable clock of paired channels
AnnaBridge 172:7d866c31b3c5 66 CLK_EnableModuleClock(modinit->clkidx);
AnnaBridge 172:7d866c31b3c5 67
AnnaBridge 172:7d866c31b3c5 68 // Set the ADC internal sampling time, input mode as single-end and enable the A/D converter
AnnaBridge 172:7d866c31b3c5 69 EADC_Open(eadc_base, EADC_CTL_DIFFEN_SINGLE_END);
AnnaBridge 172:7d866c31b3c5 70 }
AnnaBridge 172:7d866c31b3c5 71
AnnaBridge 172:7d866c31b3c5 72 uint32_t chn = NU_MODSUBINDEX(obj->adc);
AnnaBridge 172:7d866c31b3c5 73
AnnaBridge 172:7d866c31b3c5 74 // Wire pinout
AnnaBridge 172:7d866c31b3c5 75 pinmap_pinout(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 76
AnnaBridge 172:7d866c31b3c5 77 // Configure the sample module Nmod for analog input channel Nch and software trigger source
AnnaBridge 172:7d866c31b3c5 78 EADC_ConfigSampleModule(eadc_base, chn, EADC_SOFTWARE_TRIGGER, chn);
AnnaBridge 172:7d866c31b3c5 79
AnnaBridge 172:7d866c31b3c5 80 eadc_modinit_mask |= 1 << chn;
AnnaBridge 172:7d866c31b3c5 81 }
AnnaBridge 172:7d866c31b3c5 82
AnnaBridge 172:7d866c31b3c5 83 uint16_t analogin_read_u16(analogin_t *obj)
AnnaBridge 172:7d866c31b3c5 84 {
AnnaBridge 172:7d866c31b3c5 85 EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
AnnaBridge 172:7d866c31b3c5 86 uint32_t chn = NU_MODSUBINDEX(obj->adc);
AnnaBridge 172:7d866c31b3c5 87
AnnaBridge 172:7d866c31b3c5 88 EADC_START_CONV(eadc_base, 1 << chn);
AnnaBridge 172:7d866c31b3c5 89 while (EADC_GET_DATA_VALID_FLAG(eadc_base, 1 << chn) != ((uint32_t) (1 << chn)));
AnnaBridge 172:7d866c31b3c5 90 uint16_t conv_res_12 = EADC_GET_CONV_DATA(eadc_base, chn);
AnnaBridge 172:7d866c31b3c5 91 // Just 12 bits are effective. Convert to 16 bits.
AnnaBridge 172:7d866c31b3c5 92 // conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0
AnnaBridge 172:7d866c31b3c5 93 // conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8
AnnaBridge 172:7d866c31b3c5 94 uint16_t conv_res_16 = (conv_res_12 << 4) | (conv_res_12 >> 8);
AnnaBridge 172:7d866c31b3c5 95
AnnaBridge 172:7d866c31b3c5 96 return conv_res_16;
AnnaBridge 172:7d866c31b3c5 97 }
AnnaBridge 172:7d866c31b3c5 98
AnnaBridge 172:7d866c31b3c5 99 float analogin_read(analogin_t *obj)
AnnaBridge 172:7d866c31b3c5 100 {
AnnaBridge 172:7d866c31b3c5 101 uint16_t value = analogin_read_u16(obj);
AnnaBridge 172:7d866c31b3c5 102 return (float) value * (1.0f / (float) 0xFFFF);
AnnaBridge 172:7d866c31b3c5 103 }
AnnaBridge 172:7d866c31b3c5 104
AnnaBridge 172:7d866c31b3c5 105 #endif