helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
targets/TARGET_TOSHIBA/TARGET_TMPM46B/analogin_api.c@186:9c2029bfadbe, 2018-04-20 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Apr 20 11:31:35 2018 +0100
- Revision:
- 186:9c2029bfadbe
- Parent:
- 185:08ed48f1de7f
Update to latest version of mbed lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 185:08ed48f1de7f | 1 | /* mbed Microcontroller Library |
AnnaBridge | 185:08ed48f1de7f | 2 | * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved |
AnnaBridge | 185:08ed48f1de7f | 3 | * |
AnnaBridge | 185:08ed48f1de7f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 185:08ed48f1de7f | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 185:08ed48f1de7f | 6 | * You may obtain a copy of the License at |
AnnaBridge | 185:08ed48f1de7f | 7 | * |
AnnaBridge | 185:08ed48f1de7f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 185:08ed48f1de7f | 9 | * |
AnnaBridge | 185:08ed48f1de7f | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 185:08ed48f1de7f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 185:08ed48f1de7f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 185:08ed48f1de7f | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 185:08ed48f1de7f | 14 | * limitations under the License. |
AnnaBridge | 185:08ed48f1de7f | 15 | */ |
AnnaBridge | 185:08ed48f1de7f | 16 | #include "analogin_api.h" |
AnnaBridge | 185:08ed48f1de7f | 17 | #include "PeripheralNames.h" |
AnnaBridge | 185:08ed48f1de7f | 18 | #include "pinmap.h" |
AnnaBridge | 185:08ed48f1de7f | 19 | #include "mbed_wait_api.h" |
AnnaBridge | 185:08ed48f1de7f | 20 | |
AnnaBridge | 185:08ed48f1de7f | 21 | static uint8_t adc_reset_init = 0; // Is ADC Reset happened yet? |
AnnaBridge | 185:08ed48f1de7f | 22 | #define ADC_12BIT_RANGE 0xFFF |
AnnaBridge | 185:08ed48f1de7f | 23 | |
AnnaBridge | 185:08ed48f1de7f | 24 | static const PinMap PinMap_ADC[] = { |
AnnaBridge | 185:08ed48f1de7f | 25 | {PJ0, ADC_A0, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 26 | {PJ1, ADC_A1, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 27 | {PJ2, ADC_A2, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 28 | {PJ3, ADC_A3, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 29 | {PJ4, ADC_A4, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 30 | {PJ5, ADC_A5, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 31 | {PJ6, ADC_A6, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 32 | {PJ7, ADC_A7, PIN_DATA(2, 0)}, |
AnnaBridge | 185:08ed48f1de7f | 33 | {NC, NC, 0} |
AnnaBridge | 185:08ed48f1de7f | 34 | }; |
AnnaBridge | 185:08ed48f1de7f | 35 | |
AnnaBridge | 185:08ed48f1de7f | 36 | void analogin_init(analogin_t *obj, PinName pin) |
AnnaBridge | 185:08ed48f1de7f | 37 | { |
AnnaBridge | 185:08ed48f1de7f | 38 | // Check that pin belong to ADC module |
AnnaBridge | 185:08ed48f1de7f | 39 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
AnnaBridge | 185:08ed48f1de7f | 40 | MBED_ASSERT(obj->adc != (ADCName)NC); |
AnnaBridge | 185:08ed48f1de7f | 41 | |
AnnaBridge | 185:08ed48f1de7f | 42 | // Enable clock supply |
AnnaBridge | 185:08ed48f1de7f | 43 | CG_SetADCClkSupply(ENABLE); |
AnnaBridge | 185:08ed48f1de7f | 44 | // Set pin function as ADC |
AnnaBridge | 185:08ed48f1de7f | 45 | pinmap_pinout(pin, PinMap_ADC); |
AnnaBridge | 185:08ed48f1de7f | 46 | if (!adc_reset_init) { |
AnnaBridge | 185:08ed48f1de7f | 47 | // Software reset ADC |
AnnaBridge | 185:08ed48f1de7f | 48 | ADC_SWReset(TSB_AD); |
AnnaBridge | 185:08ed48f1de7f | 49 | adc_reset_init = 1; |
AnnaBridge | 185:08ed48f1de7f | 50 | } |
AnnaBridge | 185:08ed48f1de7f | 51 | // Set sample hold time and pre-scale clock |
AnnaBridge | 185:08ed48f1de7f | 52 | ADC_SetClk(TSB_AD, ADC_CONVERSION_CLK_80, ADC_FC_DIVIDE_LEVEL_8); |
AnnaBridge | 185:08ed48f1de7f | 53 | // Set input channel |
AnnaBridge | 185:08ed48f1de7f | 54 | ADC_SetInputChannel(TSB_AD, (ADC_AINx)obj->adc); |
AnnaBridge | 185:08ed48f1de7f | 55 | // Turn VREF on |
AnnaBridge | 185:08ed48f1de7f | 56 | ADC_SetVref(TSB_AD, ENABLE); |
AnnaBridge | 185:08ed48f1de7f | 57 | // Use fixed-channel single conversion mode |
AnnaBridge | 185:08ed48f1de7f | 58 | ADC_SetRepeatMode(TSB_AD, DISABLE); |
AnnaBridge | 185:08ed48f1de7f | 59 | ADC_SetScanMode(TSB_AD, DISABLE); |
AnnaBridge | 185:08ed48f1de7f | 60 | } |
AnnaBridge | 185:08ed48f1de7f | 61 | |
AnnaBridge | 185:08ed48f1de7f | 62 | uint16_t analogin_read_u16(analogin_t *obj) |
AnnaBridge | 185:08ed48f1de7f | 63 | { |
AnnaBridge | 185:08ed48f1de7f | 64 | ADC_Result ret = {0x00}; |
AnnaBridge | 185:08ed48f1de7f | 65 | |
AnnaBridge | 185:08ed48f1de7f | 66 | // Assert that ADC channel is valid |
AnnaBridge | 185:08ed48f1de7f | 67 | MBED_ASSERT(obj->adc != (ADCName)NC); |
AnnaBridge | 185:08ed48f1de7f | 68 | wait_us(300); // Wait for register to update with convert value |
AnnaBridge | 185:08ed48f1de7f | 69 | |
AnnaBridge | 185:08ed48f1de7f | 70 | // Set input channel |
AnnaBridge | 185:08ed48f1de7f | 71 | ADC_SetInputChannel(TSB_AD, (ADC_AINx)obj->adc); |
AnnaBridge | 185:08ed48f1de7f | 72 | // Enable Vref |
AnnaBridge | 185:08ed48f1de7f | 73 | ADC_SetVref(TSB_AD, ENABLE); |
AnnaBridge | 185:08ed48f1de7f | 74 | // Wait at least 3us to ensure the voltage is stable |
AnnaBridge | 185:08ed48f1de7f | 75 | wait_us(10U); |
AnnaBridge | 185:08ed48f1de7f | 76 | // Start ADC conversion |
AnnaBridge | 185:08ed48f1de7f | 77 | ADC_Start(TSB_AD); |
AnnaBridge | 185:08ed48f1de7f | 78 | // Wait until AD conversion complete |
AnnaBridge | 185:08ed48f1de7f | 79 | while(ADC_GetConvertState(TSB_AD).Bit.NormalComplete != 1) { |
AnnaBridge | 185:08ed48f1de7f | 80 | // Do nothing |
AnnaBridge | 185:08ed48f1de7f | 81 | } |
AnnaBridge | 185:08ed48f1de7f | 82 | wait_us(30); |
AnnaBridge | 185:08ed48f1de7f | 83 | // Convert result |
AnnaBridge | 185:08ed48f1de7f | 84 | ret = ADC_GetConvertResult(TSB_AD, (ADC_REGx)obj->adc); |
AnnaBridge | 185:08ed48f1de7f | 85 | // Disable Vref to go into standby mode |
AnnaBridge | 185:08ed48f1de7f | 86 | ADC_SetVref(TSB_AD, DISABLE); |
AnnaBridge | 185:08ed48f1de7f | 87 | return (uint16_t)ret.Bit.ADResult; |
AnnaBridge | 185:08ed48f1de7f | 88 | } |
AnnaBridge | 185:08ed48f1de7f | 89 | |
AnnaBridge | 185:08ed48f1de7f | 90 | float analogin_read(analogin_t *obj) |
AnnaBridge | 185:08ed48f1de7f | 91 | { |
AnnaBridge | 185:08ed48f1de7f | 92 | uint16_t value = analogin_read_u16(obj); |
AnnaBridge | 185:08ed48f1de7f | 93 | return (float)(value * (1.0f / (float)ADC_12BIT_RANGE)); |
AnnaBridge | 185:08ed48f1de7f | 94 | } |