Skip to content

Commit 95f4057

Browse files
committed
[LPC1549] Added AnalogIn support
1 parent e2a47d9 commit 95f4057

File tree

5 files changed

+188
-3
lines changed

5 files changed

+188
-3
lines changed

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/PeripheralNames.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@
2222
extern "C" {
2323
#endif
2424

25+
typedef enum {
26+
ADC0_0 = 0,
27+
ADC0_1,
28+
ADC0_2,
29+
ADC0_3,
30+
ADC0_4,
31+
ADC0_5,
32+
ADC0_6,
33+
ADC0_7,
34+
ADC0_8,
35+
ADC0_9,
36+
ADC0_10,
37+
ADC0_11,
38+
ADC1_0,
39+
ADC1_1,
40+
ADC1_2,
41+
ADC1_3,
42+
ADC1_4,
43+
ADC1_5,
44+
ADC1_6,
45+
ADC1_7,
46+
ADC1_8,
47+
ADC1_9,
48+
ADC1_10,
49+
ADC1_11,
50+
} ADCName;
51+
2552

2653
#ifdef __cplusplus
2754
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/PinNames.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ typedef enum {
6767
A1 = P0_7,
6868
A2 = P0_6,
6969
A3 = P0_5,
70-
A4 = P0_23,
71-
A5 = P0_22,
70+
A4 = P0_23, // same port as SDA
71+
A5 = P0_22, // same port as SCL
72+
SDA= P0_23, // same port as A4
73+
SCL= P0_22, // same port as A5
7274

7375
// Not connected
7476
NC = (int)0xFFFFFFFF,
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "analogin_api.h"
18+
#include "cmsis.h"
19+
#include "pinmap.h"
20+
#include "error.h"
21+
22+
#define ANALOGIN_MEDIAN_FILTER 1
23+
24+
#define ADC_10BIT_RANGE 0x3FF
25+
#define ADC_12BIT_RANGE 0xFFF
26+
27+
#define ADC_RANGE ADC_12BIT_RANGE
28+
29+
static const PinMap PinMap_ADC[] = {
30+
{P0_8 , ADC0_0, 0},
31+
{P0_7 , ADC0_1, 0},
32+
{P0_6 , ADC0_2, 0},
33+
{P0_5 , ADC0_3, 0},
34+
{P0_4 , ADC0_4, 0},
35+
{P0_3 , ADC0_5, 0},
36+
{P0_2 , ADC0_6, 0},
37+
{P0_1 , ADC0_7, 0},
38+
{P1_0 , ADC0_8, 0},
39+
{P0_31, ADC0_9, 0},
40+
{P0_0 , ADC0_10,0},
41+
{P0_30, ADC0_11,0},
42+
{P1_1 , ADC1_0, 0},
43+
{P0_9 , ADC1_1, 0},
44+
{P0_10, ADC1_2, 0},
45+
{P0_11, ADC1_3, 0},
46+
{P1_2 , ADC1_4, 0},
47+
{P1_3 , ADC1_5, 0},
48+
{P0_13, ADC1_6, 0},
49+
{P0_14, ADC1_7, 0},
50+
{P0_15, ADC1_8, 0},
51+
{P0_16, ADC1_9, 0},
52+
{P1_4 , ADC1_10,0},
53+
{P1_5 , ADC1_11,0},
54+
};
55+
56+
void analogin_init(analogin_t *obj, PinName pin) {
57+
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
58+
if (obj->adc == (uint32_t)NC) {
59+
error("ADC pin mapping failed");
60+
}
61+
uint32_t port = (pin >> 5);
62+
// enable clock for GPIOx
63+
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1UL << (14 + port));
64+
// pin enable
65+
LPC_SWM->PINENABLE0 &= ~(1UL << obj->adc);
66+
// configure GPIO as input
67+
LPC_GPIO_PORT->DIR[port] &= ~(1UL << (pin & 0x1F));
68+
69+
// power up ADC
70+
if (obj->adc < ADC1_0)
71+
{
72+
// ADC0
73+
LPC_SYSCON->PDRUNCFG &= ~(1 << 10);
74+
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 27);
75+
}
76+
else {
77+
// ADC1
78+
LPC_SYSCON->PDRUNCFG &= ~(1 << 11);
79+
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 28);
80+
}
81+
82+
// select IRC as async. clock, divided by 1
83+
LPC_SYSCON->ADCASYNCCLKSEL = 0;
84+
LPC_SYSCON->ADCASYNCCLKDIV = 1;
85+
86+
__IO LPC_ADC0_Type *adc_reg = (obj->adc < ADC1_0) ? (__IO LPC_ADC0_Type*)(LPC_ADC0) : (__IO LPC_ADC0_Type*)(LPC_ADC1);
87+
88+
// start calibration
89+
adc_reg->CTRL |= (1UL << 30);
90+
__NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
91+
92+
// asynchronous mode
93+
adc_reg->CTRL = (1UL << 8);
94+
95+
}
96+
97+
static inline uint32_t adc_read(analogin_t *obj) {
98+
99+
__IO LPC_ADC0_Type *adc_reg = (obj->adc < ADC1_0) ? (__IO LPC_ADC0_Type*)(LPC_ADC0) : (__IO LPC_ADC0_Type*)(LPC_ADC1);
100+
101+
// select channel
102+
adc_reg->SEQA_CTRL &= ~(0xFFF);
103+
adc_reg->SEQA_CTRL |= (1UL << (obj->adc & 0x1F));
104+
105+
// start conversion and sequence enable
106+
adc_reg->SEQA_CTRL |= ((1UL << 26) | (1UL << 31));
107+
108+
// Repeatedly get the sample data until DONE bit
109+
volatile uint32_t data;
110+
do {
111+
data = adc_reg->SEQA_GDAT;
112+
} while ((data & (1UL << 31)) == 0);
113+
114+
// Stop conversion
115+
adc_reg->SEQA_CTRL &= ~(1UL << 31);
116+
117+
return ((data >> 4) & ADC_RANGE);
118+
}
119+
120+
static inline void order(uint32_t *a, uint32_t *b) {
121+
if (*a > *b) {
122+
uint32_t t = *a;
123+
*a = *b;
124+
*b = t;
125+
}
126+
}
127+
128+
static inline uint32_t adc_read_u32(analogin_t *obj) {
129+
uint32_t value;
130+
#if ANALOGIN_MEDIAN_FILTER
131+
uint32_t v1 = adc_read(obj);
132+
uint32_t v2 = adc_read(obj);
133+
uint32_t v3 = adc_read(obj);
134+
order(&v1, &v2);
135+
order(&v2, &v3);
136+
order(&v1, &v2);
137+
value = v2;
138+
#else
139+
value = adc_read(obj);
140+
#endif
141+
return value;
142+
}
143+
144+
uint16_t analogin_read_u16(analogin_t *obj) {
145+
uint32_t value = adc_read_u32(obj);
146+
return (value << 4) | ((value >> 8) & 0x000F); // 12 bit
147+
}
148+
149+
float analogin_read(analogin_t *obj) {
150+
uint32_t value = adc_read_u32(obj);
151+
return (float)value * (1.0f / (float)ADC_RANGE);
152+
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#define DEVICE_INTERRUPTIN 0
2424

25-
#define DEVICE_ANALOGIN 0
25+
#define DEVICE_ANALOGIN 1
2626
#define DEVICE_ANALOGOUT 0
2727

2828
#define DEVICE_SERIAL 1

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ struct serial_s {
3434
unsigned char index;
3535
};
3636

37+
struct analogin_s {
38+
ADCName adc;
39+
};
40+
3741
struct i2c_s {
3842
LPC_I2C0_Type *i2c;
3943
};

0 commit comments

Comments
 (0)