helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Dec 20 17:27:56 2016 +0000
Revision:
153:fa9ff456f731
Parent:
149:156823d33999
This updates the lib to the mbed lib v132

Who changed what in which revision?

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