mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue May 27 13:00:07 2014 +0100
Revision:
217:d0ccc61c1fd4
Parent:
85:e1a8e879a6a9
Child:
227:7bd0639b8911
Synchronized with git revision bd83615ac55fe00f83e44dafc7ba017a3850f4e5

Full URL: https://github.com/mbedmicro/mbed/commit/bd83615ac55fe00f83e44dafc7ba017a3850f4e5/

[NRF51822] analog read - set channel

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 85:e1a8e879a6a9 1 /* mbed Microcontroller Library
mbed_official 85:e1a8e879a6a9 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 85:e1a8e879a6a9 3 *
mbed_official 85:e1a8e879a6a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 85:e1a8e879a6a9 5 * you may not use this file except in compliance with the License.
mbed_official 85:e1a8e879a6a9 6 * You may obtain a copy of the License at
mbed_official 85:e1a8e879a6a9 7 *
mbed_official 85:e1a8e879a6a9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 85:e1a8e879a6a9 9 *
mbed_official 85:e1a8e879a6a9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 85:e1a8e879a6a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 85:e1a8e879a6a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 85:e1a8e879a6a9 13 * See the License for the specific language governing permissions and
mbed_official 85:e1a8e879a6a9 14 * limitations under the License.
mbed_official 85:e1a8e879a6a9 15 */
mbed_official 85:e1a8e879a6a9 16 #include "analogin_api.h"
mbed_official 85:e1a8e879a6a9 17 #include "cmsis.h"
mbed_official 85:e1a8e879a6a9 18 #include "pinmap.h"
mbed_official 85:e1a8e879a6a9 19 #include "error.h"
mbed_official 85:e1a8e879a6a9 20
mbed_official 85:e1a8e879a6a9 21 #define ANALOGIN_MEDIAN_FILTER 1
mbed_official 85:e1a8e879a6a9 22 #define ADC_10BIT_RANGE 0x3FF
mbed_official 85:e1a8e879a6a9 23 #define ADC_RANGE ADC_10BIT_RANGE
mbed_official 85:e1a8e879a6a9 24
mbed_official 85:e1a8e879a6a9 25 static const PinMap PinMap_ADC[] = {
mbed_official 85:e1a8e879a6a9 26 {p1, ADC0_0, 4},
mbed_official 85:e1a8e879a6a9 27 {p2, ADC0_0, 8},
mbed_official 85:e1a8e879a6a9 28 {p3, ADC0_0, 16},
mbed_official 85:e1a8e879a6a9 29 {p4, ADC0_0, 32},
mbed_official 85:e1a8e879a6a9 30 {p5, ADC0_0, 64},
mbed_official 85:e1a8e879a6a9 31 {p6, ADC0_0, 128},
mbed_official 85:e1a8e879a6a9 32 {NC , NC , 0}
mbed_official 85:e1a8e879a6a9 33 };
mbed_official 85:e1a8e879a6a9 34
mbed_official 85:e1a8e879a6a9 35 void analogin_init(analogin_t *obj, PinName pin) {
mbed_official 85:e1a8e879a6a9 36 int analogInputPin=0;
mbed_official 85:e1a8e879a6a9 37 const PinMap *map = PinMap_ADC;
mbed_official 85:e1a8e879a6a9 38
mbed_official 85:e1a8e879a6a9 39 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
mbed_official 85:e1a8e879a6a9 40 if (obj->adc == (ADCName)NC) {
mbed_official 85:e1a8e879a6a9 41 error("ADC pin mapping failed");
mbed_official 85:e1a8e879a6a9 42 }
mbed_official 85:e1a8e879a6a9 43
mbed_official 85:e1a8e879a6a9 44 while (map->pin != NC) {
mbed_official 85:e1a8e879a6a9 45 if (map->pin == pin){
mbed_official 85:e1a8e879a6a9 46 analogInputPin = map->function;
mbed_official 85:e1a8e879a6a9 47 break;
mbed_official 85:e1a8e879a6a9 48 }
mbed_official 85:e1a8e879a6a9 49 map++;
mbed_official 85:e1a8e879a6a9 50 }
mbed_official 217:d0ccc61c1fd4 51 obj->adc_pin = (uint8_t)analogInputPin;
mbed_official 85:e1a8e879a6a9 52
mbed_official 85:e1a8e879a6a9 53 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
mbed_official 85:e1a8e879a6a9 54 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
mbed_official 85:e1a8e879a6a9 55 (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) |
mbed_official 85:e1a8e879a6a9 56 (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
mbed_official 85:e1a8e879a6a9 57 (analogInputPin << ADC_CONFIG_PSEL_Pos) |
mbed_official 85:e1a8e879a6a9 58 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
mbed_official 85:e1a8e879a6a9 59 }
mbed_official 85:e1a8e879a6a9 60
mbed_official 85:e1a8e879a6a9 61 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 217:d0ccc61c1fd4 62 NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
mbed_official 217:d0ccc61c1fd4 63 NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
mbed_official 85:e1a8e879a6a9 64 NRF_ADC->TASKS_START = 1;
mbed_official 85:e1a8e879a6a9 65 while ( ( (NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy)
mbed_official 85:e1a8e879a6a9 66 {
mbed_official 85:e1a8e879a6a9 67 }
mbed_official 85:e1a8e879a6a9 68
mbed_official 85:e1a8e879a6a9 69 return (uint16_t)NRF_ADC->RESULT; // 10 bit
mbed_official 85:e1a8e879a6a9 70 }
mbed_official 85:e1a8e879a6a9 71
mbed_official 85:e1a8e879a6a9 72 float analogin_read(analogin_t *obj) {
mbed_official 85:e1a8e879a6a9 73 uint16_t value = analogin_read_u16(obj);
mbed_official 85:e1a8e879a6a9 74 return (float)value * (1.0f / (float)ADC_RANGE);
mbed_official 85:e1a8e879a6a9 75 }