mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
targets/TARGET_NUVOTON/TARGET_M451/analogin_api.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
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 | static struct nu_adc_var adc12_var = { |
elessair | 0:f269e3021894 | 67 | .en_msk = 0 |
elessair | 0:f269e3021894 | 68 | }; |
elessair | 0:f269e3021894 | 69 | static struct nu_adc_var adc13_var = { |
elessair | 0:f269e3021894 | 70 | .en_msk = 0 |
elessair | 0:f269e3021894 | 71 | }; |
elessair | 0:f269e3021894 | 72 | static struct nu_adc_var adc14_var = { |
elessair | 0:f269e3021894 | 73 | .en_msk = 0 |
elessair | 0:f269e3021894 | 74 | }; |
elessair | 0:f269e3021894 | 75 | static struct nu_adc_var adc15_var = { |
elessair | 0:f269e3021894 | 76 | .en_msk = 0 |
elessair | 0:f269e3021894 | 77 | }; |
elessair | 0:f269e3021894 | 78 | |
elessair | 0:f269e3021894 | 79 | static const struct nu_modinit_s adc_modinit_tab[] = { |
elessair | 0:f269e3021894 | 80 | {ADC_0_0, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc0_var}, |
elessair | 0:f269e3021894 | 81 | {ADC_0_1, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc1_var}, |
elessair | 0:f269e3021894 | 82 | {ADC_0_2, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc2_var}, |
elessair | 0:f269e3021894 | 83 | {ADC_0_3, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc3_var}, |
elessair | 0:f269e3021894 | 84 | {ADC_0_4, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc4_var}, |
elessair | 0:f269e3021894 | 85 | {ADC_0_5, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc5_var}, |
elessair | 0:f269e3021894 | 86 | {ADC_0_6, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc6_var}, |
elessair | 0:f269e3021894 | 87 | {ADC_0_7, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc7_var}, |
elessair | 0:f269e3021894 | 88 | {ADC_0_8, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc8_var}, |
elessair | 0:f269e3021894 | 89 | {ADC_0_9, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc9_var}, |
elessair | 0:f269e3021894 | 90 | {ADC_0_10, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc10_var}, |
elessair | 0:f269e3021894 | 91 | {ADC_0_11, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc11_var}, |
elessair | 0:f269e3021894 | 92 | {ADC_0_12, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc12_var}, |
elessair | 0:f269e3021894 | 93 | {ADC_0_13, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc13_var}, |
elessair | 0:f269e3021894 | 94 | {ADC_0_14, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc14_var}, |
elessair | 0:f269e3021894 | 95 | {ADC_0_15, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc15_var}, |
elessair | 0:f269e3021894 | 96 | }; |
elessair | 0:f269e3021894 | 97 | |
elessair | 0:f269e3021894 | 98 | void analogin_init(analogin_t *obj, PinName pin) |
elessair | 0:f269e3021894 | 99 | { |
elessair | 0:f269e3021894 | 100 | obj->adc = (ADCName) pinmap_peripheral(pin, PinMap_ADC); |
elessair | 0:f269e3021894 | 101 | MBED_ASSERT(obj->adc != (ADCName) NC); |
elessair | 0:f269e3021894 | 102 | |
elessair | 0:f269e3021894 | 103 | const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab); |
elessair | 0:f269e3021894 | 104 | MBED_ASSERT(modinit != NULL); |
elessair | 0:f269e3021894 | 105 | MBED_ASSERT(modinit->modname == obj->adc); |
elessair | 0:f269e3021894 | 106 | |
elessair | 0:f269e3021894 | 107 | EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc); |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | // 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 | 110 | if (! ((struct nu_adc_var *) modinit->var)->en_msk) { |
elessair | 0:f269e3021894 | 111 | // Reset this module if no channel enabled |
elessair | 0:f269e3021894 | 112 | SYS_ResetModule(modinit->rsetidx); |
elessair | 0:f269e3021894 | 113 | |
elessair | 0:f269e3021894 | 114 | // Select clock source of paired channels |
elessair | 0:f269e3021894 | 115 | CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); |
elessair | 0:f269e3021894 | 116 | // Enable clock of paired channels |
elessair | 0:f269e3021894 | 117 | CLK_EnableModuleClock(modinit->clkidx); |
elessair | 0:f269e3021894 | 118 | |
elessair | 0:f269e3021894 | 119 | // Power on ADC |
elessair | 0:f269e3021894 | 120 | //ADC_POWER_ON(ADC); |
elessair | 0:f269e3021894 | 121 | |
elessair | 0:f269e3021894 | 122 | // Set the ADC internal sampling time, input mode as single-end and enable the A/D converter |
elessair | 0:f269e3021894 | 123 | EADC_Open(eadc_base, EADC_CTL_DIFFEN_SINGLE_END); |
elessair | 0:f269e3021894 | 124 | EADC_SetInternalSampleTime(eadc_base, 6); |
elessair | 0:f269e3021894 | 125 | } |
elessair | 0:f269e3021894 | 126 | |
elessair | 0:f269e3021894 | 127 | uint32_t chn = NU_MODSUBINDEX(obj->adc); |
elessair | 0:f269e3021894 | 128 | |
elessair | 0:f269e3021894 | 129 | // Wire pinout |
elessair | 0:f269e3021894 | 130 | pinmap_pinout(pin, PinMap_ADC); |
elessair | 0:f269e3021894 | 131 | |
elessair | 0:f269e3021894 | 132 | // Configure the sample module Nmod for analog input channel Nch and software trigger source |
elessair | 0:f269e3021894 | 133 | EADC_ConfigSampleModule(EADC, chn, EADC_SOFTWARE_TRIGGER, chn); |
elessair | 0:f269e3021894 | 134 | |
elessair | 0:f269e3021894 | 135 | ((struct nu_adc_var *) modinit->var)->en_msk |= 1 << chn; |
elessair | 0:f269e3021894 | 136 | } |
elessair | 0:f269e3021894 | 137 | |
elessair | 0:f269e3021894 | 138 | uint16_t analogin_read_u16(analogin_t *obj) |
elessair | 0:f269e3021894 | 139 | { |
elessair | 0:f269e3021894 | 140 | EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc); |
elessair | 0:f269e3021894 | 141 | uint32_t chn = NU_MODSUBINDEX(obj->adc); |
elessair | 0:f269e3021894 | 142 | |
elessair | 0:f269e3021894 | 143 | EADC_START_CONV(eadc_base, 1 << chn); |
elessair | 0:f269e3021894 | 144 | while (EADC_GET_PENDING_CONV(eadc_base) & (1 << chn)); |
elessair | 0:f269e3021894 | 145 | uint16_t conv_res_12 = EADC_GET_CONV_DATA(eadc_base, chn); |
elessair | 0:f269e3021894 | 146 | // Just 12 bits are effective. Convert to 16 bits. |
elessair | 0:f269e3021894 | 147 | // conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0 |
elessair | 0:f269e3021894 | 148 | // conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 |
elessair | 0:f269e3021894 | 149 | uint16_t conv_res_16 = (conv_res_12 << 4) | (conv_res_12 >> 8); |
elessair | 0:f269e3021894 | 150 | |
elessair | 0:f269e3021894 | 151 | return conv_res_16; |
elessair | 0:f269e3021894 | 152 | } |
elessair | 0:f269e3021894 | 153 | |
elessair | 0:f269e3021894 | 154 | float analogin_read(analogin_t *obj) |
elessair | 0:f269e3021894 | 155 | { |
elessair | 0:f269e3021894 | 156 | uint16_t value = analogin_read_u16(obj); |
elessair | 0:f269e3021894 | 157 | return (float) value * (1.0f / (float) 0xFFFF); |
elessair | 0:f269e3021894 | 158 | } |
elessair | 0:f269e3021894 | 159 | |
elessair | 0:f269e3021894 | 160 | #endif |