fix for mbed lib issue 3 (i2c problem) see also https://mbed.org/users/mbed_official/code/mbed/issues/3 affected implementations: LPC812, LPC11U24, LPC1768, LPC2368, LPC4088

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers analogin_api.c Source File

analogin_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "analogin_api.h"
00017 
00018 #include "cmsis.h"
00019 #include "pinmap.h"
00020 #include "error.h"
00021 
00022 static const PinMap PinMap_ADC[] = {
00023     {PTE20, ADC0_SE0,  0},
00024     {PTE22, ADC0_SE3,  0},
00025     {PTE29, ADC0_SE4b, 0},
00026     {PTE30, ADC0_SE23, 0},
00027     {PTB0,  ADC0_SE8,  0},
00028     {PTB1,  ADC0_SE9,  0},
00029     {PTB2,  ADC0_SE12, 0},
00030     {PTB3,  ADC0_SE13, 0},
00031     {PTC0,  ADC0_SE14, 0},
00032     {PTC1,  ADC0_SE15, 0},
00033     {PTC2,  ADC0_SE11, 0},
00034     {PTD1,  ADC0_SE5b, 0},
00035     {PTD5,  ADC0_SE6b, 0},
00036     {PTD6,  ADC0_SE7b, 0},
00037     {NC,    NC,        0}
00038 };
00039 
00040 void analogin_init(analogin_t *obj, PinName pin) {
00041     obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
00042     if (obj->adc == (uint32_t)NC) {
00043         error("ADC pin mapping failed");
00044     }
00045 
00046     SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
00047 
00048     uint32_t port = (uint32_t)pin >> PORT_SHIFT;
00049     SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
00050 
00051     ADC0->SC1[1] = ADC_SC1_ADCH(obj->adc);
00052 
00053     ADC0->CFG1 = ADC_CFG1_ADLPC_MASK    // Low-Power Configuration
00054                | ADC_CFG1_ADIV(3)       // Clock Divide Select: (Input Clock)/8
00055                | ADC_CFG1_ADLSMP_MASK   // Long Sample Time
00056                | ADC_CFG1_MODE(3)       // (16)bits Resolution
00057                | ADC_CFG1_ADICLK(1);    // Input Clock: (Bus Clock)/2
00058 
00059     ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK   // ADxxb channels are selected
00060                | ADC_CFG2_ADACKEN_MASK  // Asynchronous Clock Output Enable
00061                | ADC_CFG2_ADHSC_MASK    // High-Speed Configuration
00062                | ADC_CFG2_ADLSTS(0);    // Long Sample Time Select
00063 
00064     ADC0->SC2 = ADC_SC2_REFSEL(0);      // Default Voltage Reference
00065 
00066     ADC0->SC3 = ADC_SC3_AVGE_MASK       // Hardware Average Enable
00067               | ADC_SC3_AVGS(0);        // 4 Samples Averaged
00068 
00069     pinmap_pinout(pin, PinMap_ADC);
00070 }
00071 
00072 uint16_t analogin_read_u16(analogin_t *obj) {
00073     // start conversion
00074     ADC0->SC1[0] = ADC_SC1_ADCH(obj->adc);
00075 
00076     // Wait Conversion Complete
00077     while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK);
00078 
00079     // Return value
00080     return (uint16_t)ADC0->R[0];
00081 }
00082 
00083 float analogin_read(analogin_t *obj) {
00084     uint16_t value = analogin_read_u16(obj);
00085     return (float)value * (1.0f / (float)0xFFFF);
00086 }
00087