![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_NUVOTON/TARGET_NANO100/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-2017 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 "mbed_wait_api.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "cmsis.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "pinmap.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "PeripheralPins.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | #include "nu_modutil.h" |
kadonotakashi | 0:8fdf9a60065b | 26 | |
kadonotakashi | 0:8fdf9a60065b | 27 | static uint32_t adc_modinit_mask = 0; |
kadonotakashi | 0:8fdf9a60065b | 28 | volatile int adc_busy_flag = 0; |
kadonotakashi | 0:8fdf9a60065b | 29 | |
kadonotakashi | 0:8fdf9a60065b | 30 | static const struct nu_modinit_s adc_modinit_tab[] = { |
kadonotakashi | 0:8fdf9a60065b | 31 | {ADC_0_0, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 32 | {ADC_0_1, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 33 | {ADC_0_2, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 34 | {ADC_0_3, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 35 | {ADC_0_4, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 36 | {ADC_0_5, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 37 | {ADC_0_6, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 38 | {ADC_0_7, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 39 | {ADC_0_8, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 40 | {ADC_0_9, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 41 | {ADC_0_10, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 42 | {ADC_0_11, ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_ADC_CLK_DIVIDER(1), ADC_RST, ADC_IRQn, NULL}, |
kadonotakashi | 0:8fdf9a60065b | 43 | }; |
kadonotakashi | 0:8fdf9a60065b | 44 | |
kadonotakashi | 0:8fdf9a60065b | 45 | void analogin_init(analogin_t *obj, PinName pin) |
kadonotakashi | 0:8fdf9a60065b | 46 | { |
kadonotakashi | 0:8fdf9a60065b | 47 | obj->adc = (ADCName) pinmap_peripheral(pin, PinMap_ADC); |
kadonotakashi | 0:8fdf9a60065b | 48 | MBED_ASSERT(obj->adc != (ADCName) NC); |
kadonotakashi | 0:8fdf9a60065b | 49 | |
kadonotakashi | 0:8fdf9a60065b | 50 | const struct nu_modinit_s *modinit = get_modinit(obj->adc, adc_modinit_tab); |
kadonotakashi | 0:8fdf9a60065b | 51 | MBED_ASSERT(modinit != NULL); |
kadonotakashi | 0:8fdf9a60065b | 52 | MBED_ASSERT((ADCName) modinit->modname == obj->adc); |
kadonotakashi | 0:8fdf9a60065b | 53 | |
kadonotakashi | 0:8fdf9a60065b | 54 | ADC_T *adc_base = (ADC_T *) NU_MODBASE(obj->adc); |
kadonotakashi | 0:8fdf9a60065b | 55 | uint32_t chn = NU_MODSUBINDEX(obj->adc); |
kadonotakashi | 0:8fdf9a60065b | 56 | |
kadonotakashi | 0:8fdf9a60065b | 57 | // Wait for ADC is not busy, due to all ADC channels share the same module |
kadonotakashi | 0:8fdf9a60065b | 58 | while (adc_busy_flag != 0) { |
kadonotakashi | 0:8fdf9a60065b | 59 | wait_us(100); |
kadonotakashi | 0:8fdf9a60065b | 60 | } |
kadonotakashi | 0:8fdf9a60065b | 61 | adc_busy_flag = 1; |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | // 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 | 64 | if (! adc_modinit_mask) { |
kadonotakashi | 0:8fdf9a60065b | 65 | // Reset this module if no channel enabled |
kadonotakashi | 0:8fdf9a60065b | 66 | SYS_ResetModule(modinit->rsetidx); |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | // Select clock source of paired channels |
kadonotakashi | 0:8fdf9a60065b | 69 | CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); |
kadonotakashi | 0:8fdf9a60065b | 70 | // Enable clock of paired channels |
kadonotakashi | 0:8fdf9a60065b | 71 | CLK_EnableModuleClock(modinit->clkidx); |
kadonotakashi | 0:8fdf9a60065b | 72 | |
kadonotakashi | 0:8fdf9a60065b | 73 | // Set operation mode and enable channel N |
kadonotakashi | 0:8fdf9a60065b | 74 | ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_SINGLE_CYCLE, 1 << chn); |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | // Set reference voltage to AVDD |
kadonotakashi | 0:8fdf9a60065b | 77 | ADC_SET_REF_VOLTAGE(ADC, ADC_REFSEL_POWER); |
kadonotakashi | 0:8fdf9a60065b | 78 | |
kadonotakashi | 0:8fdf9a60065b | 79 | // Power on ADC |
kadonotakashi | 0:8fdf9a60065b | 80 | ADC_POWER_ON(ADC); |
kadonotakashi | 0:8fdf9a60065b | 81 | } else { |
kadonotakashi | 0:8fdf9a60065b | 82 | // Just enable channel N |
kadonotakashi | 0:8fdf9a60065b | 83 | adc_base->CHEN |= 1 << chn; |
kadonotakashi | 0:8fdf9a60065b | 84 | } |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | adc_modinit_mask |= 1 << chn; |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | adc_busy_flag = 0; |
kadonotakashi | 0:8fdf9a60065b | 89 | |
kadonotakashi | 0:8fdf9a60065b | 90 | // Wire pinout |
kadonotakashi | 0:8fdf9a60065b | 91 | pinmap_pinout(pin, PinMap_ADC); |
kadonotakashi | 0:8fdf9a60065b | 92 | } |
kadonotakashi | 0:8fdf9a60065b | 93 | |
kadonotakashi | 0:8fdf9a60065b | 94 | void analogin_deinit(PinName pin) |
kadonotakashi | 0:8fdf9a60065b | 95 | { |
kadonotakashi | 0:8fdf9a60065b | 96 | analogin_t obj; |
kadonotakashi | 0:8fdf9a60065b | 97 | obj.adc = (ADCName) pinmap_peripheral(pin, PinMap_ADC); |
kadonotakashi | 0:8fdf9a60065b | 98 | MBED_ASSERT(obj.adc != (ADCName) NC); |
kadonotakashi | 0:8fdf9a60065b | 99 | |
kadonotakashi | 0:8fdf9a60065b | 100 | const struct nu_modinit_s *modinit = get_modinit(obj.adc, adc_modinit_tab); |
kadonotakashi | 0:8fdf9a60065b | 101 | MBED_ASSERT(modinit != NULL); |
kadonotakashi | 0:8fdf9a60065b | 102 | MBED_ASSERT((ADCName) modinit->modname == obj.adc); |
kadonotakashi | 0:8fdf9a60065b | 103 | |
kadonotakashi | 0:8fdf9a60065b | 104 | ADC_T *adc_base = (ADC_T *) NU_MODBASE(obj.adc); |
kadonotakashi | 0:8fdf9a60065b | 105 | uint32_t chn = NU_MODSUBINDEX(obj.adc); |
kadonotakashi | 0:8fdf9a60065b | 106 | |
kadonotakashi | 0:8fdf9a60065b | 107 | // Wait for ADC is not busy, due to all ADC channels share the same module |
kadonotakashi | 0:8fdf9a60065b | 108 | while (adc_busy_flag != 0) { |
kadonotakashi | 0:8fdf9a60065b | 109 | wait_us(100); |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | adc_busy_flag = 1; |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | // Disable channel N |
kadonotakashi | 0:8fdf9a60065b | 114 | adc_base->CHEN &= ~(1 << chn); |
kadonotakashi | 0:8fdf9a60065b | 115 | adc_modinit_mask &= ~(1 << chn); |
kadonotakashi | 0:8fdf9a60065b | 116 | |
kadonotakashi | 0:8fdf9a60065b | 117 | adc_busy_flag = 0; |
kadonotakashi | 0:8fdf9a60065b | 118 | } |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | uint16_t analogin_read_u16(analogin_t *obj) |
kadonotakashi | 0:8fdf9a60065b | 121 | { |
kadonotakashi | 0:8fdf9a60065b | 122 | ADC_T *adc_base = (ADC_T *) NU_MODBASE(obj->adc); |
kadonotakashi | 0:8fdf9a60065b | 123 | uint32_t chn = NU_MODSUBINDEX(obj->adc); |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | // Wait for ADC is not busy, due to all ADC channels share the same module |
kadonotakashi | 0:8fdf9a60065b | 126 | while (adc_busy_flag != 0) { |
kadonotakashi | 0:8fdf9a60065b | 127 | wait_us(100); |
kadonotakashi | 0:8fdf9a60065b | 128 | } |
kadonotakashi | 0:8fdf9a60065b | 129 | adc_busy_flag = 1; |
kadonotakashi | 0:8fdf9a60065b | 130 | |
kadonotakashi | 0:8fdf9a60065b | 131 | // Start the A/D conversion |
kadonotakashi | 0:8fdf9a60065b | 132 | adc_base->CR |= ADC_CR_ADST_Msk; |
kadonotakashi | 0:8fdf9a60065b | 133 | // Wait for conversion finish |
kadonotakashi | 0:8fdf9a60065b | 134 | while (! ADC_GET_INT_FLAG(adc_base, ADC_ADF_INT) & ADC_ADF_INT) ; |
kadonotakashi | 0:8fdf9a60065b | 135 | ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); |
kadonotakashi | 0:8fdf9a60065b | 136 | uint16_t conv_res_12 = ADC_GET_CONVERSION_DATA(adc_base, chn); |
kadonotakashi | 0:8fdf9a60065b | 137 | |
kadonotakashi | 0:8fdf9a60065b | 138 | adc_busy_flag = 0; |
kadonotakashi | 0:8fdf9a60065b | 139 | |
kadonotakashi | 0:8fdf9a60065b | 140 | // Just 12 bits are effective. Convert to 16 bits. |
kadonotakashi | 0:8fdf9a60065b | 141 | // conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0 |
kadonotakashi | 0:8fdf9a60065b | 142 | // conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 |
kadonotakashi | 0:8fdf9a60065b | 143 | uint16_t conv_res_16 = (conv_res_12 << 4) | (conv_res_12 >> 8); |
kadonotakashi | 0:8fdf9a60065b | 144 | |
kadonotakashi | 0:8fdf9a60065b | 145 | return conv_res_16; |
kadonotakashi | 0:8fdf9a60065b | 146 | } |
kadonotakashi | 0:8fdf9a60065b | 147 | |
kadonotakashi | 0:8fdf9a60065b | 148 | float analogin_read(analogin_t *obj) |
kadonotakashi | 0:8fdf9a60065b | 149 | { |
kadonotakashi | 0:8fdf9a60065b | 150 | uint16_t value = analogin_read_u16(obj); |
kadonotakashi | 0:8fdf9a60065b | 151 | return (float) value * (1.0f / (float) 0xFFFF); |
kadonotakashi | 0:8fdf9a60065b | 152 | } |
kadonotakashi | 0:8fdf9a60065b | 153 | |
kadonotakashi | 0:8fdf9a60065b | 154 | #endif |