Skip to content

Commit 71a478f

Browse files
committed
Merge pull request #309 from toyowata/master
[LPC11U68] PwmOut and USBDevice support
2 parents 7fda8e7 + 71b7348 commit 71a478f

File tree

6 files changed

+213
-9
lines changed

6 files changed

+213
-9
lines changed

libraries/USBDevice/USBDevice/USBEndpoints.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef enum {
3939
/* Include configuration for specific target */
4040
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC4088)
4141
#include "USBEndpoints_LPC17_LPC23.h"
42-
#elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC1347)
42+
#elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC1347) || defined (TARGET_LPC11U6X)
4343
#include "USBEndpoints_LPC11U.h"
4444
#elif defined(TARGET_KL25Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D5M) | defined(TARGET_K64F)
4545
#include "USBEndpoints_KL25Z.h"

libraries/USBDevice/USBDevice/USBHAL_LPC11U.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
1818

19-
#if defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC1347) || defined(TARGET_LPC11U35_501)
19+
#if defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC1347) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPC11U68)
2020

2121
#if defined(TARGET_LPC1347)
2222
#define USB_IRQ USB_IRQ_IRQn
23-
#elif defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501)
23+
#elif defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPC11U68)
2424
#define USB_IRQ USB_IRQn
2525
#endif
2626

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ typedef enum {
5555
I2C_1 = (int)LPC_I2C1_BASE
5656
} I2CName;
5757

58+
typedef enum {
59+
SCT0_0 = 0,
60+
SCT0_1,
61+
SCT0_2,
62+
SCT0_3,
63+
SCT1_0,
64+
SCT1_1,
65+
SCT1_2,
66+
SCT1_3,
67+
} PWMName;
5868

5969
#ifdef __cplusplus
6070
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/analogin_api.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "pinmap.h"
2020
#include "error.h"
2121

22-
#ifdef DEVICE_ANALOGIN
22+
#if DEVICE_ANALOGIN
2323

2424
#define ANALOGIN_MEDIAN_FILTER 1
2525

@@ -55,21 +55,25 @@ void analogin_init(analogin_t *obj, PinName pin) {
5555
}
5656
pinmap_pinout(pin, PinMap_ADC);
5757

58+
__IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + (pin & 0x1FF));
59+
// set pin to ADC mode
60+
*reg &= ~(1 << 7); // set ADMODE = 0 (analog mode)
61+
5862
// ADC Powered
5963
tmp = (LPC_SYSCON->PDRUNCFG & PDRUN_VALID_BITS);
6064
tmp &= ~((1 << 4) & PDRUN_VALID_BITS);
6165
LPC_SYSCON->PDRUNCFG = (tmp | PDRUN_RESERVED_ONE);
6266

6367
// Enable clock for ADC
6468
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13);
65-
69+
6670
// Start ADC self-calibration
67-
LPC_ADC->CTRL = (1UL << 30) || (100);
71+
LPC_ADC->CTRL = (1UL << 30);
6872
do {
6973
tmp = LPC_ADC->CTRL;
7074
} while ((tmp & (1UL << 30)) != 0);
7175

72-
LPC_ADC->CTRL = 100;
76+
LPC_ADC->CTRL = 100; // 500kHz sampling
7377
}
7478

7579
static inline uint32_t adc_read(analogin_t *obj) {
@@ -79,7 +83,7 @@ static inline uint32_t adc_read(analogin_t *obj) {
7983
LPC_ADC->SEQA_CTRL |= (1UL << obj->adc);
8084

8185
// start conversion, sequence enable with async mode
82-
LPC_ADC->SEQA_CTRL |= ((1UL << 26) | (1UL << 31) | (1UL << 29) /*| (1UL << 19)*/);
86+
LPC_ADC->SEQA_CTRL |= ((1UL << 26) | (1UL << 31) | (1UL << 19));
8387

8488
// Repeatedly get the sample data until DONE bit
8589
volatile uint32_t data;

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

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

4040
#define DEVICE_ETHERNET 0
4141

42-
#define DEVICE_PWMOUT 0
42+
#define DEVICE_PWMOUT 1
4343

4444
#define DEVICE_SEMIHOST 0
4545
#define DEVICE_LOCALFILESYSTEM 0
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 "pwmout_api.h"
18+
#include "cmsis.h"
19+
#include "pinmap.h"
20+
#include "error.h"
21+
22+
#if DEVICE_PWMOUT
23+
24+
#define SCT_CHANNELS 2
25+
26+
static const PinMap PinMap_PWM[] = {
27+
{P1_19, SCT0_0, 2},
28+
{P2_2 , SCT0_1, 3},
29+
{P2_7 , SCT0_2, 2},
30+
{P1_13, SCT0_3, 2},
31+
{P2_16, SCT1_0, 1},
32+
{P2_17, SCT1_1, 1},
33+
{P2_18, SCT1_2, 1},
34+
{P2_19, SCT1_3, 1},
35+
{NC , NC ,0}
36+
};
37+
38+
39+
static LPC_SCT0_Type *SCTs[SCT_CHANNELS] = {
40+
(LPC_SCT0_Type*)LPC_SCT0,
41+
(LPC_SCT0_Type*)LPC_SCT1,
42+
43+
};
44+
45+
// bit flags for used SCTs
46+
static unsigned char sct_used = 0;
47+
48+
static int get_available_sct(void) {
49+
int i;
50+
for (i=0; i<SCT_CHANNELS; i++) {
51+
if ((sct_used & (1 << i)) == 0)
52+
return i;
53+
}
54+
return -1;
55+
}
56+
57+
void pwmout_init(pwmout_t* obj, PinName pin) {
58+
// determine the SPI to use
59+
PWMName pwm_mapped = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
60+
if (pwm_mapped == (uint32_t)NC) {
61+
error("PwmOut pin mapping failed");
62+
}
63+
int sct_n = get_available_sct();
64+
if (sct_n == -1) {
65+
error("No available SCT");
66+
}
67+
68+
sct_used |= (1 << sct_n);
69+
obj->pwm = SCTs[sct_n];
70+
obj->pwm_ch = sct_n;
71+
72+
// Enable the SCT clock
73+
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 31);
74+
75+
// Clear peripheral reset the SCT:
76+
LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
77+
pinmap_pinout(pin, PinMap_PWM);
78+
LPC_SCT0_Type* pwm = obj->pwm;
79+
80+
// Two 16-bit counters, autolimit
81+
pwm->CONFIG &= ~(0x1);
82+
pwm->CONFIG |= (1 << 17);
83+
84+
// halt and clear the counter
85+
pwm->CTRL |= (1 << 2) | (1 << 3);
86+
87+
// System Clock -> us_ticker (1)MHz
88+
pwm->CTRL &= ~(0x7F << 5);
89+
pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
90+
91+
switch(pwm_mapped) {
92+
case SCT0_0:
93+
case SCT1_0:
94+
pwm->OUT0_SET = (1 << 0); // event 0
95+
pwm->OUT0_CLR = (1 << 1); // event 1
96+
break;
97+
case SCT0_1:
98+
case SCT1_1:
99+
pwm->OUT1_SET = (1 << 0); // event 0
100+
pwm->OUT1_CLR = (1 << 1); // event 1
101+
break;
102+
case SCT0_2:
103+
case SCT1_2:
104+
pwm->OUT2_SET = (1 << 0); // event 0
105+
pwm->OUT2_CLR = (1 << 1); // event 1
106+
break;
107+
case SCT0_3:
108+
case SCT1_3:
109+
pwm->OUT3_SET = (1 << 0); // event 0
110+
pwm->OUT3_CLR = (1 << 1); // event 1
111+
break;
112+
default:
113+
break;
114+
}
115+
// Event 0 : MATCH and MATCHSEL=0
116+
pwm->EV0_CTRL = (1 << 12);
117+
pwm->EV0_STATE = 0xFFFFFFFF;
118+
// Event 1 : MATCH and MATCHSEL=1
119+
pwm->EV1_CTRL = (1 << 12) | (1 << 0);
120+
pwm->EV1_STATE = 0xFFFFFFFF;
121+
122+
// Match reload register
123+
pwm->MATCHREL0 = 20000; // 20ms
124+
pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty
125+
126+
// unhalt the counter:
127+
// - clearing bit 2 of the CTRL register
128+
pwm->CTRL &= ~(1 << 2);
129+
130+
// default to 20ms: standard for servos, and fine for e.g. brightness control
131+
pwmout_period_ms(obj, 20);
132+
pwmout_write (obj, 0);
133+
}
134+
135+
void pwmout_free(pwmout_t* obj) {
136+
sct_used &= ~(1 << obj->pwm_ch);
137+
if (sct_used == 0) {
138+
// Disable the SCT clock
139+
LPC_SYSCON->SYSAHBCLKCTRL &= ~(1UL << 31);
140+
}
141+
}
142+
143+
void pwmout_write(pwmout_t* obj, float value) {
144+
if (value < 0.0f) {
145+
value = 0.0;
146+
} else if (value > 1.0f) {
147+
value = 1.0;
148+
}
149+
uint32_t t_off = obj->pwm->MATCHREL0 - (uint32_t)((float)(obj->pwm->MATCHREL0) * value);
150+
uint32_t t_on = (uint32_t)((float)(obj->pwm->MATCHREL0) * value);
151+
obj->pwm->MATCHREL1 = t_on;
152+
}
153+
154+
float pwmout_read(pwmout_t* obj) {
155+
uint32_t t_off = obj->pwm->MATCHREL0;
156+
uint32_t t_on = obj->pwm->MATCHREL1;
157+
float v = (float)t_on/(float)t_off;
158+
return (v > 1.0f) ? (1.0f) : (v);
159+
}
160+
161+
void pwmout_period(pwmout_t* obj, float seconds) {
162+
pwmout_period_us(obj, seconds * 1000000.0f);
163+
}
164+
165+
void pwmout_period_ms(pwmout_t* obj, int ms) {
166+
pwmout_period_us(obj, ms * 1000);
167+
}
168+
169+
// Set the PWM period, keeping the duty cycle the same.
170+
void pwmout_period_us(pwmout_t* obj, int us) {
171+
uint32_t t_off = obj->pwm->MATCHREL0;
172+
uint32_t t_on = obj->pwm->MATCHREL1;
173+
float v = (float)t_on/(float)t_off;
174+
obj->pwm->MATCHREL0 = (uint64_t)us;
175+
obj->pwm->MATCHREL1 = (uint64_t)((float)us * (float)v);
176+
}
177+
178+
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
179+
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
180+
}
181+
182+
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
183+
pwmout_pulsewidth_us(obj, ms * 1000);
184+
}
185+
186+
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
187+
obj->pwm->MATCHREL1 = (uint64_t)us;
188+
}
189+
190+
#endif

0 commit comments

Comments
 (0)