Skip to content

Commit fabd906

Browse files
committed
pwmout - LPC11U6X - add read methods for period and pulsewidth
1 parent a6f9c5b commit fabd906

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

targets/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,39 @@
2525

2626
static const PinMap PinMap_PWM[] = {
2727
{P1_19, SCT0_0, 2},
28-
{P2_2 , SCT0_1, 3},
29-
{P2_7 , SCT0_2, 2},
28+
{P2_2, SCT0_1, 3},
29+
{P2_7, SCT0_2, 2},
3030
{P1_13, SCT0_3, 2},
3131
{P2_16, SCT1_0, 1},
3232
{P2_17, SCT1_1, 1},
3333
{P2_18, SCT1_2, 1},
3434
{P2_19, SCT1_3, 1},
35-
{NC , NC ,0}
35+
{NC, NC, 0}
3636
};
3737

3838

3939
static LPC_SCT0_Type *SCTs[SCT_CHANNELS] = {
40-
(LPC_SCT0_Type*)LPC_SCT0,
41-
(LPC_SCT0_Type*)LPC_SCT1,
40+
(LPC_SCT0_Type *)LPC_SCT0,
41+
(LPC_SCT0_Type *)LPC_SCT1,
4242

4343
};
4444

4545
// bit flags for used SCTs
4646
static unsigned char sct_used = 0;
4747

48-
static int get_available_sct(void) {
48+
static int get_available_sct(void)
49+
{
4950
int i;
50-
for (i=0; i<SCT_CHANNELS; i++) {
51-
if ((sct_used & (1 << i)) == 0)
51+
for (i = 0; i < SCT_CHANNELS; i++) {
52+
if ((sct_used & (1 << i)) == 0) {
5253
return i;
54+
}
5355
}
5456
return -1;
5557
}
5658

57-
void pwmout_init(pwmout_t* obj, PinName pin) {
59+
void pwmout_init(pwmout_t *obj, PinName pin)
60+
{
5861
// determine the SPI to use
5962
PWMName pwm_mapped = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
6063
if (pwm_mapped == (PWMName)NC) {
@@ -73,17 +76,17 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
7376
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 31);
7477

7578
// Clear peripheral reset the SCT:
76-
LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
79+
LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
7780
pinmap_pinout(pin, PinMap_PWM);
78-
LPC_SCT0_Type* pwm = obj->pwm;
79-
81+
LPC_SCT0_Type *pwm = obj->pwm;
82+
8083
// Unified 32-bit counter, autolimit
8184
pwm->CONFIG |= ((0x3 << 17) | 0x01);
82-
85+
8386
// halt and clear the counter
8487
pwm->CTRL |= (1 << 2) | (1 << 3);
85-
86-
switch(pwm_mapped) {
88+
89+
switch (pwm_mapped) {
8790
case SCT0_0:
8891
case SCT1_0:
8992
pwm->OUT0_SET = (1 << 0); // event 0
@@ -116,19 +119,21 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
116119

117120
// default to 20ms: standard for servos, and fine for e.g. brightness control
118121
pwmout_period_ms(obj, 20);
119-
pwmout_write (obj, 0);
122+
pwmout_write(obj, 0);
120123
}
121124

122-
void pwmout_free(pwmout_t* obj) {
125+
void pwmout_free(pwmout_t *obj)
126+
{
123127
sct_used &= ~(1 << obj->pwm_ch);
124128
if (sct_used == 0) {
125129
// Disable the SCT clock
126130
LPC_SYSCON->SYSAHBCLKCTRL &= ~(1UL << 31);
127131
}
128132
}
129133

130-
void pwmout_write(pwmout_t* obj, float value) {
131-
LPC_SCT0_Type* pwm = obj->pwm;
134+
void pwmout_write(pwmout_t *obj, float value)
135+
{
136+
LPC_SCT0_Type *pwm = obj->pwm;
132137
if (value < 0.0f) {
133138
value = 0.0;
134139
} else if (value > 1.0f) {
@@ -137,7 +142,7 @@ void pwmout_write(pwmout_t* obj, float value) {
137142
uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
138143
if (t_on > 0) {
139144
pwm->MATCHREL1 = t_on - 1;
140-
145+
141146
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
142147
if (pwm->CTRL & (1 << 2)) {
143148
pwm->MATCH1 = pwm->MATCHREL1;
@@ -150,34 +155,38 @@ void pwmout_write(pwmout_t* obj, float value) {
150155
}
151156
}
152157

153-
float pwmout_read(pwmout_t* obj) {
154-
LPC_SCT0_Type* pwm = obj->pwm;
158+
float pwmout_read(pwmout_t *obj)
159+
{
160+
LPC_SCT0_Type *pwm = obj->pwm;
155161
uint32_t t_off = pwm->MATCHREL0 + 1;
156162
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
157-
float v = (float)t_on/(float)t_off;
163+
float v = (float)t_on / (float)t_off;
158164
return (v > 1.0f) ? (1.0f) : (v);
159165
}
160166

161-
void pwmout_period(pwmout_t* obj, float seconds) {
167+
void pwmout_period(pwmout_t *obj, float seconds)
168+
{
162169
pwmout_period_us(obj, seconds * 1000000.0f);
163170
}
164171

165-
void pwmout_period_ms(pwmout_t* obj, int ms) {
172+
void pwmout_period_ms(pwmout_t *obj, int ms)
173+
{
166174
pwmout_period_us(obj, ms * 1000);
167175
}
168176

169177
// Set the PWM period, keeping the duty cycle the same.
170-
void pwmout_period_us(pwmout_t* obj, int us) {
171-
LPC_SCT0_Type* pwm = obj->pwm;
178+
void pwmout_period_us(pwmout_t *obj, int us)
179+
{
180+
LPC_SCT0_Type *pwm = obj->pwm;
172181
uint32_t t_off = pwm->MATCHREL0 + 1;
173182
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
174-
float v = (float)t_on/(float)t_off;
183+
float v = (float)t_on / (float)t_off;
175184
uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
176185
uint32_t pulsewidth_ticks = period_ticks * v;
177186
pwm->MATCHREL0 = period_ticks - 1;
178187
if (pulsewidth_ticks > 0) {
179188
pwm->MATCHREL1 = pulsewidth_ticks - 1;
180-
189+
181190
// Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
182191
if (pwm->CTRL & (1 << 2)) {
183192
pwm->MATCH0 = pwm->MATCHREL0;
@@ -188,25 +197,33 @@ void pwmout_period_us(pwmout_t* obj, int us) {
188197
// Halt the timer and force the output low
189198
pwm->CTRL |= (1 << 2) | (1 << 3);
190199
pwm->OUTPUT = 0x00000000;
191-
200+
192201
// Ensure the new period will take immediate effect when the timer is un-halted
193202
pwm->MATCH0 = pwm->MATCHREL0;
194203
}
195204
}
196205

197-
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
206+
int pwmout_read_period_us(pwmout_t *obj)
207+
{
208+
return pwm->MATCHREL0 + 1;
209+
}
210+
211+
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
212+
{
198213
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
199214
}
200215

201-
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
216+
void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
217+
{
202218
pwmout_pulsewidth_us(obj, ms * 1000);
203219
}
204220

205-
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
206-
LPC_SCT0_Type* pwm = obj->pwm;
221+
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
222+
{
223+
LPC_SCT0_Type *pwm = obj->pwm;
207224
if (us > 0) {
208225
pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
209-
226+
210227
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
211228
if (pwm->CTRL & (1 << 2)) {
212229
pwm->MATCH1 = pwm->MATCHREL1;
@@ -219,6 +236,10 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
219236
}
220237
}
221238

239+
int pwmout_read_pulsewidth_us(pwmout_t *obj {
240+
return (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
241+
}
242+
222243
const PinMap *pwmout_pinmap()
223244
{
224245
return PinMap_PWM;

0 commit comments

Comments
 (0)