mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
182:57724642e740
Parent:
149:156823d33999
mbed-dev library. Release version 159.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 * Copyright (c) 2006-2015 ARM Limited
<> 144:ef7eb2e8f9f7 3 *
<> 144:ef7eb2e8f9f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 144:ef7eb2e8f9f7 5 * you may not use this file except in compliance with the License.
<> 144:ef7eb2e8f9f7 6 * You may obtain a copy of the License at
<> 144:ef7eb2e8f9f7 7 *
<> 144:ef7eb2e8f9f7 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 144:ef7eb2e8f9f7 9 *
<> 144:ef7eb2e8f9f7 10 * Unless required by applicable law or agreed to in writing, software
<> 144:ef7eb2e8f9f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 144:ef7eb2e8f9f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 144:ef7eb2e8f9f7 13 * See the License for the specific language governing permissions and
<> 144:ef7eb2e8f9f7 14 * limitations under the License.
<> 144:ef7eb2e8f9f7 15 */
<> 144:ef7eb2e8f9f7 16 #include "mbed_assert.h"
<> 144:ef7eb2e8f9f7 17 #include "analogin_api.h"
<> 144:ef7eb2e8f9f7 18
<> 144:ef7eb2e8f9f7 19 #include "cmsis.h"
<> 144:ef7eb2e8f9f7 20 #include "pinmap.h"
<> 144:ef7eb2e8f9f7 21 #include "clk_freqs.h"
<> 144:ef7eb2e8f9f7 22 #include "PeripheralPins.h"
<> 144:ef7eb2e8f9f7 23
<> 144:ef7eb2e8f9f7 24 #define MAX_FADC 6000000
<> 144:ef7eb2e8f9f7 25
<> 144:ef7eb2e8f9f7 26 void analogin_init(analogin_t *obj, PinName pin) {
<> 144:ef7eb2e8f9f7 27 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
<> 144:ef7eb2e8f9f7 28 MBED_ASSERT(obj->adc != (ADCName)NC);
<> 144:ef7eb2e8f9f7 29
<> 144:ef7eb2e8f9f7 30 SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
<> 144:ef7eb2e8f9f7 31
<> 144:ef7eb2e8f9f7 32 uint32_t port = (uint32_t)pin >> PORT_SHIFT;
<> 144:ef7eb2e8f9f7 33 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
<> 144:ef7eb2e8f9f7 34
<> 144:ef7eb2e8f9f7 35 // bus clk
<> 144:ef7eb2e8f9f7 36 uint32_t PCLK = bus_frequency();
<> 144:ef7eb2e8f9f7 37 uint32_t clkdiv;
<> 144:ef7eb2e8f9f7 38 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
<> 144:ef7eb2e8f9f7 39 if ((PCLK >> clkdiv) <= MAX_FADC)
<> 144:ef7eb2e8f9f7 40 break;
<> 144:ef7eb2e8f9f7 41 }
<> 144:ef7eb2e8f9f7 42 if (clkdiv == 4) //Set max div
<> 144:ef7eb2e8f9f7 43 clkdiv = 0x7;
<> 144:ef7eb2e8f9f7 44
<> 144:ef7eb2e8f9f7 45 ADC0->SC1[1] = ADC_SC1_ADCH(obj->adc);
<> 144:ef7eb2e8f9f7 46
<> 144:ef7eb2e8f9f7 47 ADC0->CFG1 = ADC_CFG1_ADLPC_MASK // Low-Power Configuration
<> 144:ef7eb2e8f9f7 48 | ADC_CFG1_ADIV(clkdiv & 0x3) // Clock Divide Select
<> 144:ef7eb2e8f9f7 49 | ADC_CFG1_ADLSMP_MASK // Long Sample Time
<> 144:ef7eb2e8f9f7 50 | ADC_CFG1_MODE(3) // (16)bits Resolution
<> 144:ef7eb2e8f9f7 51 | ADC_CFG1_ADICLK(clkdiv >> 2); // Input Clock
<> 144:ef7eb2e8f9f7 52
<> 144:ef7eb2e8f9f7 53 ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK // ADxxb or ADxxa channels
<> 144:ef7eb2e8f9f7 54 | ADC_CFG2_ADHSC_MASK // High-Speed Configuration
<> 144:ef7eb2e8f9f7 55 | ADC_CFG2_ADLSTS(0); // Long Sample Time Select
<> 144:ef7eb2e8f9f7 56
<> 144:ef7eb2e8f9f7 57 ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference
<> 144:ef7eb2e8f9f7 58
<> 144:ef7eb2e8f9f7 59 ADC0->SC3 = ADC_SC3_AVGE_MASK // Hardware Average Enable
<> 144:ef7eb2e8f9f7 60 | ADC_SC3_AVGS(0); // 4 Samples Averaged
<> 144:ef7eb2e8f9f7 61
<> 144:ef7eb2e8f9f7 62 pinmap_pinout(pin, PinMap_ADC);
<> 144:ef7eb2e8f9f7 63 }
<> 144:ef7eb2e8f9f7 64
<> 144:ef7eb2e8f9f7 65 uint16_t analogin_read_u16(analogin_t *obj) {
<> 144:ef7eb2e8f9f7 66 // start conversion
<> 144:ef7eb2e8f9f7 67 ADC0->SC1[0] = ADC_SC1_ADCH(obj->adc);
<> 144:ef7eb2e8f9f7 68
<> 144:ef7eb2e8f9f7 69 // Wait Conversion Complete
<> 144:ef7eb2e8f9f7 70 while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK);
<> 144:ef7eb2e8f9f7 71
<> 144:ef7eb2e8f9f7 72 return (uint16_t)ADC0->R[0];
<> 144:ef7eb2e8f9f7 73 }
<> 144:ef7eb2e8f9f7 74
<> 144:ef7eb2e8f9f7 75 float analogin_read(analogin_t *obj) {
<> 144:ef7eb2e8f9f7 76 uint16_t value = analogin_read_u16(obj);
<> 144:ef7eb2e8f9f7 77 return (float)value * (1.0f / (float)0xFFFF);
<> 144:ef7eb2e8f9f7 78 }