mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

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
<> 149:156823d33999 26 struct nu_adc_var {
<> 149:156823d33999 27 uint32_t en_msk;
<> 149:156823d33999 28 };
<> 149:156823d33999 29
<> 149:156823d33999 30 static struct nu_adc_var adc0_var = {
<> 149:156823d33999 31 .en_msk = 0
<> 149:156823d33999 32 };
<> 149:156823d33999 33 static struct nu_adc_var adc1_var = {
<> 149:156823d33999 34 .en_msk = 0
<> 149:156823d33999 35 };
<> 149:156823d33999 36 static struct nu_adc_var adc2_var = {
<> 149:156823d33999 37 .en_msk = 0
<> 149:156823d33999 38 };
<> 149:156823d33999 39 static struct nu_adc_var adc3_var = {
<> 149:156823d33999 40 .en_msk = 0
<> 149:156823d33999 41 };
<> 149:156823d33999 42 static struct nu_adc_var adc4_var = {
<> 149:156823d33999 43 .en_msk = 0
<> 149:156823d33999 44 };
<> 149:156823d33999 45 static struct nu_adc_var adc5_var = {
<> 149:156823d33999 46 .en_msk = 0
<> 149:156823d33999 47 };
<> 149:156823d33999 48 static struct nu_adc_var adc6_var = {
<> 149:156823d33999 49 .en_msk = 0
<> 149:156823d33999 50 };
<> 149:156823d33999 51 static struct nu_adc_var adc7_var = {
<> 149:156823d33999 52 .en_msk = 0
<> 149:156823d33999 53 };
<> 149:156823d33999 54 static struct nu_adc_var adc8_var = {
<> 149:156823d33999 55 .en_msk = 0
<> 149:156823d33999 56 };
<> 149:156823d33999 57 static struct nu_adc_var adc9_var = {
<> 149:156823d33999 58 .en_msk = 0
<> 149:156823d33999 59 };
<> 149:156823d33999 60 static struct nu_adc_var adc10_var = {
<> 149:156823d33999 61 .en_msk = 0
<> 149:156823d33999 62 };
<> 149:156823d33999 63 static struct nu_adc_var adc11_var = {
<> 149:156823d33999 64 .en_msk = 0
<> 149:156823d33999 65 };
<> 149:156823d33999 66 static struct nu_adc_var adc12_var = {
<> 149:156823d33999 67 .en_msk = 0
<> 149:156823d33999 68 };
<> 149:156823d33999 69 static struct nu_adc_var adc13_var = {
<> 149:156823d33999 70 .en_msk = 0
<> 149:156823d33999 71 };
<> 149:156823d33999 72 static struct nu_adc_var adc14_var = {
<> 149:156823d33999 73 .en_msk = 0
<> 149:156823d33999 74 };
<> 149:156823d33999 75 static struct nu_adc_var adc15_var = {
<> 149:156823d33999 76 .en_msk = 0
<> 149:156823d33999 77 };
<> 149:156823d33999 78
<> 149:156823d33999 79 static const struct nu_modinit_s adc_modinit_tab[] = {
<> 149:156823d33999 80 {ADC_0_0, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc0_var},
<> 149:156823d33999 81 {ADC_0_1, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc1_var},
<> 149:156823d33999 82 {ADC_0_2, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc2_var},
<> 149:156823d33999 83 {ADC_0_3, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc3_var},
<> 149:156823d33999 84 {ADC_0_4, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc4_var},
<> 149:156823d33999 85 {ADC_0_5, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc5_var},
<> 149:156823d33999 86 {ADC_0_6, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc6_var},
<> 149:156823d33999 87 {ADC_0_7, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc7_var},
<> 149:156823d33999 88 {ADC_0_8, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc8_var},
<> 149:156823d33999 89 {ADC_0_9, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc9_var},
<> 149:156823d33999 90 {ADC_0_10, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc10_var},
<> 149:156823d33999 91 {ADC_0_11, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc11_var},
<> 149:156823d33999 92 {ADC_0_12, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc12_var},
<> 149:156823d33999 93 {ADC_0_13, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc13_var},
<> 149:156823d33999 94 {ADC_0_14, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc14_var},
<> 149:156823d33999 95 {ADC_0_15, EADC_MODULE, 0, CLK_CLKDIV0_EADC(8), EADC_RST, ADC00_IRQn, &adc15_var},
<> 149:156823d33999 96 };
<> 149:156823d33999 97
<> 149:156823d33999 98 void analogin_init(analogin_t *obj, PinName pin)
<> 149:156823d33999 99 {
<> 149:156823d33999 100 obj->adc = (ADCName) pinmap_peripheral(pin, PinMap_ADC);
<> 149:156823d33999 101 MBED_ASSERT(obj->adc != (ADCName) NC);
<> 149:156823d33999 102
<> 149:156823d33999 103 const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab);
<> 149:156823d33999 104 MBED_ASSERT(modinit != NULL);
<> 149:156823d33999 105 MBED_ASSERT(modinit->modname == obj->adc);
<> 149:156823d33999 106
<> 149:156823d33999 107 EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
<> 149:156823d33999 108
<> 149:156823d33999 109 // NOTE: All channels (identified by ADCName) share a ADC module. This reset will also affect other channels of the same ADC module.
<> 149:156823d33999 110 if (! ((struct nu_adc_var *) modinit->var)->en_msk) {
<> 149:156823d33999 111 // Reset this module if no channel enabled
<> 149:156823d33999 112 SYS_ResetModule(modinit->rsetidx);
<> 149:156823d33999 113
<> 149:156823d33999 114 // Select clock source of paired channels
<> 149:156823d33999 115 CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
<> 149:156823d33999 116 // Enable clock of paired channels
<> 149:156823d33999 117 CLK_EnableModuleClock(modinit->clkidx);
<> 149:156823d33999 118
<> 149:156823d33999 119 // Power on ADC
<> 149:156823d33999 120 //ADC_POWER_ON(ADC);
<> 149:156823d33999 121
<> 149:156823d33999 122 // Set the ADC internal sampling time, input mode as single-end and enable the A/D converter
<> 149:156823d33999 123 EADC_Open(eadc_base, EADC_CTL_DIFFEN_SINGLE_END);
<> 149:156823d33999 124 EADC_SetInternalSampleTime(eadc_base, 6);
<> 149:156823d33999 125 }
<> 149:156823d33999 126
<> 149:156823d33999 127 uint32_t chn = NU_MODSUBINDEX(obj->adc);
<> 149:156823d33999 128
<> 149:156823d33999 129 // Wire pinout
<> 149:156823d33999 130 pinmap_pinout(pin, PinMap_ADC);
<> 149:156823d33999 131
<> 149:156823d33999 132 // Configure the sample module Nmod for analog input channel Nch and software trigger source
<> 149:156823d33999 133 EADC_ConfigSampleModule(EADC, chn, EADC_SOFTWARE_TRIGGER, chn);
<> 149:156823d33999 134
<> 149:156823d33999 135 ((struct nu_adc_var *) modinit->var)->en_msk |= 1 << chn;
<> 149:156823d33999 136 }
<> 149:156823d33999 137
<> 149:156823d33999 138 uint16_t analogin_read_u16(analogin_t *obj)
<> 149:156823d33999 139 {
<> 149:156823d33999 140 EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
<> 149:156823d33999 141 uint32_t chn = NU_MODSUBINDEX(obj->adc);
<> 149:156823d33999 142
<> 149:156823d33999 143 EADC_START_CONV(eadc_base, 1 << chn);
<> 149:156823d33999 144 while (EADC_GET_PENDING_CONV(eadc_base) & (1 << chn));
<> 149:156823d33999 145 uint16_t conv_res_12 = EADC_GET_CONV_DATA(eadc_base, chn);
<> 149:156823d33999 146 // Just 12 bits are effective. Convert to 16 bits.
<> 149:156823d33999 147 // conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0
<> 149:156823d33999 148 // conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8
<> 149:156823d33999 149 uint16_t conv_res_16 = (conv_res_12 << 4) | (conv_res_12 >> 8);
<> 149:156823d33999 150
<> 149:156823d33999 151 return conv_res_16;
<> 149:156823d33999 152 }
<> 149:156823d33999 153
<> 149:156823d33999 154 float analogin_read(analogin_t *obj)
<> 149:156823d33999 155 {
<> 149:156823d33999 156 uint16_t value = analogin_read_u16(obj);
<> 149:156823d33999 157 return (float) value * (1.0f / (float) 0xFFFF);
<> 149:156823d33999 158 }
<> 149:156823d33999 159
<> 149:156823d33999 160 #endif