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