mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
ua1arn
Date:
Mon Jul 30 12:31:10 2018 +0000
Revision:
188:3f10722804f9
Parent:
184:08ed48f1de7f
before add multi-configuration USB descriptors

Who changed what in which revision?

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