Skip to content

Commit 6b2f312

Browse files
committed
Merge pull request #207 from toyowata/master
[LPC1549] Added AnalogOut API and PWM improvement
2 parents fb3bf48 + 84a654d commit 6b2f312

File tree

7 files changed

+146
-121
lines changed

7 files changed

+146
-121
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ typedef enum {
4949
ADC1_11,
5050
} ADCName;
5151

52+
typedef enum {
53+
DAC0_0 = 0,
54+
} DACName;
5255

5356
#ifdef __cplusplus
5457
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
#include "analogout_api.h"
17+
#include "cmsis.h"
18+
#include "pinmap.h"
19+
#include "error.h"
20+
21+
void analogout_init(dac_t *obj, PinName pin) {
22+
if (pin != P0_12) {
23+
error("DAC pin mapping failed");
24+
}
25+
26+
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29);
27+
LPC_SYSCON->PDRUNCFG &= ~(1 << 12);
28+
LPC_IOCON->PIO0_12 = 0;
29+
LPC_SWM->PINENABLE0 &= ~(1 << 24);
30+
LPC_DAC->CTRL = 0;
31+
32+
analogout_write_u16(obj, 0);
33+
}
34+
35+
void analogout_free(dac_t *obj)
36+
{
37+
LPC_SYSCON->SYSAHBCLKCTRL0 &= ~(1 << 29);
38+
LPC_SWM->PINENABLE0 |= (1 << 24);
39+
}
40+
41+
static inline void dac_write(int value) {
42+
value &= 0xFFF; // 12-bit
43+
44+
// Set the DAC output
45+
LPC_DAC->VAL = (value << 4);
46+
}
47+
48+
static inline int dac_read() {
49+
return ((LPC_DAC->VAL >> 4) & 0xFFF);
50+
}
51+
52+
void analogout_write(dac_t *obj, float value) {
53+
if (value < 0.0f) {
54+
dac_write(0);
55+
} else if (value > 1.0f) {
56+
dac_write(0xFFF);
57+
} else {
58+
dac_write((uint32_t)(value * (float)0xFFF));
59+
}
60+
}
61+
62+
void analogout_write_u16(dac_t *obj, uint16_t value) {
63+
dac_write(value);
64+
}
65+
66+
float analogout_read(dac_t *obj) {
67+
uint32_t value = dac_read();
68+
return (float)value * (1.0f / (float)0xFFF);
69+
}
70+
71+
uint16_t analogout_read_u16(dac_t *obj) {
72+
return (uint16_t)dac_read();
73+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define DEVICE_INTERRUPTIN 1
2424

2525
#define DEVICE_ANALOGIN 1
26-
#define DEVICE_ANALOGOUT 0
26+
#define DEVICE_ANALOGOUT 1
2727

2828
#define DEVICE_SERIAL 1
2929
#define DEVICE_SERIAL_FC 1

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static inline int i2c_status(i2c_t *obj) {
2929

3030
// Wait until the Serial Interrupt (SI) is set
3131
static int i2c_wait_SI(i2c_t *obj) {
32-
int timeout = 0;
32+
volatile int timeout = 0;
3333
while (!(LPC_I2C0->STAT & (1 << 0))) {
3434
timeout++;
3535
if (timeout > 100000) return -1;
@@ -41,25 +41,21 @@ static inline void i2c_interface_enable(i2c_t *obj) {
4141
LPC_I2C0->CFG |= (1 << 0);
4242
}
4343

44-
static inline void i2c_power_enable(i2c_t *obj) {
44+
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
45+
if ((sda != P0_23) | (scl != P0_22)) {
46+
error("I2C pin mapping failed");
47+
}
48+
4549
// Enables clock for I2C0
46-
LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<13);
47-
// LPC_SYSCON->PRESETCTRL1 &= ~(0x1<<13);
48-
LPC_SYSCON->PRESETCTRL1 |= (0x1<<13);
49-
LPC_SYSCON->PRESETCTRL1 &= ~(0x1 << 13);
50+
LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 13);
5051

51-
}
52+
LPC_SYSCON->PRESETCTRL1 |= (1 << 13);
53+
LPC_SYSCON->PRESETCTRL1 &= ~(1 << 13);
5254

53-
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
54-
55-
// ピン定義の確認どうしよう…
56-
57-
58-
// enable power
59-
i2c_power_enable(obj);
6055
// pin enable
6156
LPC_SWM->PINENABLE1 &= ~(0x3 << 3);
62-
// set default frequency at 100k
57+
58+
// set default frequency at 100kHz
6359
i2c_frequency(obj, 100000);
6460
i2c_interface_enable(obj);
6561
}
@@ -76,7 +72,7 @@ inline int i2c_start(i2c_t *obj) {
7672
}
7773

7874
inline int i2c_stop(i2c_t *obj) {
79-
int timeout = 0;
75+
volatile int timeout = 0;
8076

8177
LPC_I2C0->MSTCTL = (1 << 2) | (1 << 0);
8278
while ((LPC_I2C0->STAT & ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1))) {
@@ -107,14 +103,12 @@ static inline int i2c_do_read(i2c_t *obj, int last) {
107103
LPC_I2C0->MSTCTL = (1 << 0);
108104

109105
// return the data
110-
//return (I2C_DAT(obj) & 0xFF);
111106
return (LPC_I2C0->MSTDAT & 0xFF);
112107
}
113108

114109
void i2c_frequency(i2c_t *obj, int hz) {
115110
// No peripheral clock divider on the M0
116111
uint32_t PCLK = SystemCoreClock;
117-
118112
uint32_t clkdiv = PCLK / (hz * 4) - 1;
119113

120114
LPC_I2C0->DIV = clkdiv;
@@ -123,59 +117,43 @@ void i2c_frequency(i2c_t *obj, int hz) {
123117

124118
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
125119
int count, status;
126-
int timeout = 0;
127120

128121
i2c_start(obj);
129122

130-
//status = i2c_do_write(obj, (address | 0x01), 1);
131123
LPC_I2C0->MSTDAT = (address | 0x01);
132124
LPC_I2C0->MSTCTL |= 0x20;
133-
while (!(LPC_I2C0->STAT & (1 << 0))) {
134-
timeout++;
135-
if (timeout > 100000) return -1;
136-
}
125+
if (i2c_wait_SI(obj) == -1)
126+
return -1;
127+
137128
status = ((LPC_I2C0->STAT >> 1) & (0x07));
138-
139129
if (status != 0x01) {
140130
i2c_stop(obj);
141131
return I2C_ERROR_NO_SLAVE;
142132
}
143133

144134
// Read in all except last byte
145135
for (count = 0; count < (length - 1); count++) {
146-
//int value = i2c_do_read(obj, 0);
147-
while (!(LPC_I2C0->STAT & (1 << 0))) {
148-
timeout++;
149-
if (timeout > 100000) return -1;
150-
}
151-
if (!0)
152-
LPC_I2C0->MSTCTL = (1 << 0);
153-
data[count] = (LPC_I2C0->MSTDAT & 0xFF);
154-
//
155-
status = ((LPC_I2C0->STAT >> 1) & (0x07));
136+
if (i2c_wait_SI(obj) == -1)
137+
return -1;
138+
LPC_I2C0->MSTCTL = (1 << 0);
139+
data[count] = (LPC_I2C0->MSTDAT & 0xFF);
140+
status = ((LPC_I2C0->STAT >> 1) & (0x07));
156141
if (status != 0x00) {
157142
i2c_stop(obj);
158143
return count;
159144
}
160-
//data[count] = (char) value;
161145
}
162146

163147
// read in last byte
164-
//int value = i2c_do_read(obj, 1);
165-
while (!(LPC_I2C0->STAT & (1 << 0))) {
166-
timeout++;
167-
if (timeout > 100000) return -1;
168-
}
148+
if (i2c_wait_SI(obj) == -1)
149+
return -1;
150+
169151
data[count] = (LPC_I2C0->MSTDAT & 0xFF);
170-
//
171152
status = i2c_status(obj);
172153
if (status != 0x01) {
173154
i2c_stop(obj);
174155
return length - 1;
175156
}
176-
177-
//data[count] = (char) value;
178-
179157
// If not repeated start, send stop.
180158
if (stop) {
181159
i2c_stop(obj);
@@ -188,35 +166,27 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
188166

189167
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
190168
int i, status;
191-
int timeout = 0;
192169

193170
i2c_start(obj);
194171

195-
//status = i2c_do_write(obj, (address & 0xFE), 1);
196172
LPC_I2C0->MSTDAT = (address & 0xFE);
197173
LPC_I2C0->MSTCTL |= 0x20;
198-
// wait and return status
199-
while (!(LPC_I2C0->STAT & (1 << 0))) {
200-
timeout++;
201-
if (timeout > 100000) return -1;
202-
}
174+
if (i2c_wait_SI(obj) == -1)
175+
return -1;
176+
203177
status = ((LPC_I2C0->STAT >> 1) & (0x07));
204-
205178
if (status != 0x02) {
206179
i2c_stop(obj);
207180
return I2C_ERROR_NO_SLAVE;
208181
}
209182

210183
for (i=0; i<length; i++) {
211-
//status = i2c_do_write(obj, data[i], 0);
212184
LPC_I2C0->MSTDAT = data[i];
213185
LPC_I2C0->MSTCTL = (1 << 0);
214-
// wait and return status
215-
while (!(LPC_I2C0->STAT & (1 << 0))) {
216-
timeout++;
217-
if (timeout > 100000) return -1;
218-
}
219-
status = ((LPC_I2C0->STAT >> 1) & (0x07));
186+
if (i2c_wait_SI(obj) == -1)
187+
return -1;
188+
189+
status = ((LPC_I2C0->STAT >> 1) & (0x07));
220190
if (status != 0x02) {
221191
i2c_stop(obj);
222192
return i;
@@ -242,17 +212,9 @@ int i2c_byte_read(i2c_t *obj, int last) {
242212
}
243213

244214
int i2c_byte_write(i2c_t *obj, int data) {
245-
int ack;
246-
int status = i2c_do_write(obj, (data & 0xFF), 0);
247-
248-
switch(status) {
249-
case 2:
250-
ack = 1;
251-
break;
252-
default:
253-
ack = 0;
254-
break;
215+
if (i2c_do_write(obj, (data & 0xFF), 0) == 2) {
216+
return 1;
217+
} else {
218+
return 0;
255219
}
256-
257-
return ack;
258220
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ struct analogin_s {
4343
ADCName adc;
4444
};
4545

46+
struct dac_s {
47+
DACName dac;
48+
};
49+
4650
struct i2c_s {
4751
LPC_I2C0_Type *i2c;
4852
};

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ static LPC_SCT0_Type *SCTs[4] = {
3030
static unsigned char sct_used = 0;
3131
static int get_available_sct(void) {
3232
int i;
33-
// start from 1, since 0 is used by ticker at the moment
34-
for (i=1; i<4; i++) {
33+
for (i=0; i<4; i++) {
3534
if ((sct_used & (1 << i)) == 0)
3635
return i;
3736
}
@@ -61,6 +60,11 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
6160
LPC_SYSCON->PRESETCTRL1 &= ~(1 << (obj->pwm_ch + 2));
6261

6362
switch(obj->pwm_ch) {
63+
case 0:
64+
// SCT0_OUT0
65+
LPC_SWM->PINASSIGN[7] &= ~0x0000FF00;
66+
LPC_SWM->PINASSIGN[7] |= (pin << 8);
67+
break;
6468
case 1:
6569
// SCT1_OUT0
6670
LPC_SWM->PINASSIGN[8] &= ~0x000000FF;

0 commit comments

Comments
 (0)