mbed-os
Fork of mbed-os by
targets/TARGET_NUVOTON/TARGET_NUC472/analogin_api.c@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2015-2016 Nuvoton |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | #include "analogin_api.h" |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #if DEVICE_ANALOGIN |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 22 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 23 | #include "PeripheralPins.h" |
elessair | 0:f269e3021894 | 24 | #include "nu_modutil.h" |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | struct nu_adc_var { |
elessair | 0:f269e3021894 | 27 | uint32_t en_msk; |
elessair | 0:f269e3021894 | 28 | }; |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | static struct nu_adc_var adc0_var = { |
elessair | 0:f269e3021894 | 31 | .en_msk = 0 |
elessair | 0:f269e3021894 | 32 | }; |
elessair | 0:f269e3021894 | 33 | static struct nu_adc_var adc1_var = { |
elessair | 0:f269e3021894 | 34 | .en_msk = 0 |
elessair | 0:f269e3021894 | 35 | }; |
elessair | 0:f269e3021894 | 36 | static struct nu_adc_var adc2_var = { |
elessair | 0:f269e3021894 | 37 | .en_msk = 0 |
elessair | 0:f269e3021894 | 38 | }; |
elessair | 0:f269e3021894 | 39 | static struct nu_adc_var adc3_var = { |
elessair | 0:f269e3021894 | 40 | .en_msk = 0 |
elessair | 0:f269e3021894 | 41 | }; |
elessair | 0:f269e3021894 | 42 | static struct nu_adc_var adc4_var = { |
elessair | 0:f269e3021894 | 43 | .en_msk = 0 |
elessair | 0:f269e3021894 | 44 | }; |
elessair | 0:f269e3021894 | 45 | static struct nu_adc_var adc5_var = { |
elessair | 0:f269e3021894 | 46 | .en_msk = 0 |
elessair | 0:f269e3021894 | 47 | }; |
elessair | 0:f269e3021894 | 48 | static struct nu_adc_var adc6_var = { |
elessair | 0:f269e3021894 | 49 | .en_msk = 0 |
elessair | 0:f269e3021894 | 50 | }; |
elessair | 0:f269e3021894 | 51 | static struct nu_adc_var adc7_var = { |
elessair | 0:f269e3021894 | 52 | .en_msk = 0 |
elessair | 0:f269e3021894 | 53 | }; |
elessair | 0:f269e3021894 | 54 | static struct nu_adc_var adc8_var = { |
elessair | 0:f269e3021894 | 55 | .en_msk = 0 |
elessair | 0:f269e3021894 | 56 | }; |
elessair | 0:f269e3021894 | 57 | static struct nu_adc_var adc9_var = { |
elessair | 0:f269e3021894 | 58 | .en_msk = 0 |
elessair | 0:f269e3021894 | 59 | }; |
elessair | 0:f269e3021894 | 60 | static struct nu_adc_var adc10_var = { |
elessair | 0:f269e3021894 | 61 | .en_msk = 0 |
elessair | 0:f269e3021894 | 62 | }; |
elessair | 0:f269e3021894 | 63 | static struct nu_adc_var adc11_var = { |
elessair | 0:f269e3021894 | 64 | .en_msk = 0 |
elessair | 0:f269e3021894 | 65 | }; |
elessair | 0:f269e3021894 | 66 | |
elessair | 0:f269e3021894 | 67 | static const struct nu_modinit_s adc_modinit_tab[] = { |
elessair | 0:f269e3021894 | 68 | {ADC_0_0, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc0_var}, |
elessair | 0:f269e3021894 | 69 | {ADC_0_1, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc1_var}, |
elessair | 0:f269e3021894 | 70 | {ADC_0_2, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc2_var}, |
elessair | 0:f269e3021894 | 71 | {ADC_0_3, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc3_var}, |
elessair | 0:f269e3021894 | 72 | {ADC_0_4, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc4_var}, |
elessair | 0:f269e3021894 | 73 | {ADC_0_5, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc5_var}, |
elessair | 0:f269e3021894 | 74 | {ADC_0_6, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc6_var}, |
elessair | 0:f269e3021894 | 75 | {ADC_0_7, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc7_var}, |
elessair | 0:f269e3021894 | 76 | {ADC_0_8, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc8_var}, |
elessair | 0:f269e3021894 | 77 | {ADC_0_9, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc9_var}, |
elessair | 0:f269e3021894 | 78 | {ADC_0_10, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc10_var}, |
elessair | 0:f269e3021894 | 79 | {ADC_0_11, ADC_MODULE, CLK_CLKSEL1_ADCSEL_HIRC, CLK_CLKDIV0_ADC(1), ADC_RST, ADC_IRQn, &adc11_var} |
elessair | 0:f269e3021894 | 80 | }; |
elessair | 0:f269e3021894 | 81 | |
elessair | 0:f269e3021894 | 82 | void analogin_init(analogin_t *obj, PinName pin) |
elessair | 0:f269e3021894 | 83 | { |
elessair | 0:f269e3021894 | 84 | obj->adc = (ADCName) pinmap_peripheral(pin, PinMap_ADC); |
elessair | 0:f269e3021894 | 85 | MBED_ASSERT(obj->adc != (ADCName) NC); |
elessair | 0:f269e3021894 | 86 | |
elessair | 0:f269e3021894 | 87 | const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab); |
elessair | 0:f269e3021894 | 88 | MBED_ASSERT(modinit != NULL); |
elessair | 0:f269e3021894 | 89 | MBED_ASSERT(modinit->modname == obj->adc); |
elessair | 0:f269e3021894 | 90 | |
elessair | 0:f269e3021894 | 91 | // NOTE: All channels (identified by ADCName) share a ADC module. This reset will also affect other channels of the same ADC module. |
elessair | 0:f269e3021894 | 92 | if (! ((struct nu_adc_var *) modinit->var)->en_msk) { |
elessair | 0:f269e3021894 | 93 | // Reset this module if no channel enabled |
elessair | 0:f269e3021894 | 94 | SYS_ResetModule(modinit->rsetidx); |
elessair | 0:f269e3021894 | 95 | |
elessair | 0:f269e3021894 | 96 | // Select clock source of paired channels |
elessair | 0:f269e3021894 | 97 | CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); |
elessair | 0:f269e3021894 | 98 | // Enable clock of paired channels |
elessair | 0:f269e3021894 | 99 | CLK_EnableModuleClock(modinit->clkidx); |
elessair | 0:f269e3021894 | 100 | |
elessair | 0:f269e3021894 | 101 | // Power on ADC |
elessair | 0:f269e3021894 | 102 | ADC_POWER_ON(ADC); |
elessair | 0:f269e3021894 | 103 | } |
elessair | 0:f269e3021894 | 104 | |
elessair | 0:f269e3021894 | 105 | ADC_T *adc_base = (ADC_T *) NU_MODBASE(obj->adc); |
elessair | 0:f269e3021894 | 106 | uint32_t chn = NU_MODSUBINDEX(obj->adc); |
elessair | 0:f269e3021894 | 107 | |
elessair | 0:f269e3021894 | 108 | // Wire pinout |
elessair | 0:f269e3021894 | 109 | pinmap_pinout(pin, PinMap_ADC); |
elessair | 0:f269e3021894 | 110 | |
elessair | 0:f269e3021894 | 111 | // Enable channel 0 |
elessair | 0:f269e3021894 | 112 | ADC_Open(adc_base, |
elessair | 0:f269e3021894 | 113 | ADC_INPUT_MODE_SINGLE_END, |
elessair | 0:f269e3021894 | 114 | ADC_OPERATION_MODE_SINGLE, |
elessair | 0:f269e3021894 | 115 | 1 << chn); // ADC_CH_0_MASK~ADC_CH_11_MASK |
elessair | 0:f269e3021894 | 116 | |
elessair | 0:f269e3021894 | 117 | ((struct nu_adc_var *) modinit->var)->en_msk |= 1 << chn; |
elessair | 0:f269e3021894 | 118 | } |
elessair | 0:f269e3021894 | 119 | |
elessair | 0:f269e3021894 | 120 | uint16_t analogin_read_u16(analogin_t *obj) |
elessair | 0:f269e3021894 | 121 | { |
elessair | 0:f269e3021894 | 122 | ADC_T *adc_base = (ADC_T *) NU_MODBASE(obj->adc); |
elessair | 0:f269e3021894 | 123 | uint32_t chn = NU_MODSUBINDEX(obj->adc); |
elessair | 0:f269e3021894 | 124 | |
elessair | 0:f269e3021894 | 125 | ADC_START_CONV(adc_base); |
elessair | 0:f269e3021894 | 126 | while (adc_base->CTL & ADC_CTL_SWTRG_Msk); |
elessair | 0:f269e3021894 | 127 | uint16_t conv_res_12 = ADC_GET_CONVERSION_DATA(adc_base, chn); |
elessair | 0:f269e3021894 | 128 | // Just 12 bits are effective. Convert to 16 bits. |
elessair | 0:f269e3021894 | 129 | // conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0 |
elessair | 0:f269e3021894 | 130 | // conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 |
elessair | 0:f269e3021894 | 131 | uint16_t conv_res_16 = (conv_res_12 << 4) | (conv_res_12 >> 8); |
elessair | 0:f269e3021894 | 132 | |
elessair | 0:f269e3021894 | 133 | return conv_res_16; |
elessair | 0:f269e3021894 | 134 | } |
elessair | 0:f269e3021894 | 135 | |
elessair | 0:f269e3021894 | 136 | float analogin_read(analogin_t *obj) |
elessair | 0:f269e3021894 | 137 | { |
elessair | 0:f269e3021894 | 138 | uint16_t value = analogin_read_u16(obj); |
elessair | 0:f269e3021894 | 139 | return (float) value * (1.0f / (float) 0xFFFF); |
elessair | 0:f269e3021894 | 140 | } |
elessair | 0:f269e3021894 | 141 | |
elessair | 0:f269e3021894 | 142 | #endif |