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