Skip to content

Commit ea6e806

Browse files
committed
pwmout - LPC176X - add read methods for period and pulsewidth
1 parent 7270f29 commit ea6e806

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

targets/TARGET_NXP/TARGET_LPC176X/pwmout_api.c

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ static const PinMap PinMap_PWM[] = {
2929
{P1_23, PWM_4, 2},
3030
{P1_24, PWM_5, 2},
3131
{P1_26, PWM_6, 2},
32-
{P2_0 , PWM_1, 1},
33-
{P2_1 , PWM_2, 1},
34-
{P2_2 , PWM_3, 1},
35-
{P2_3 , PWM_4, 1},
36-
{P2_4 , PWM_5, 1},
37-
{P2_5 , PWM_6, 1},
32+
{P2_0, PWM_1, 1},
33+
{P2_1, PWM_2, 1},
34+
{P2_2, PWM_3, 1},
35+
{P2_3, PWM_4, 1},
36+
{P2_4, PWM_5, 1},
37+
{P2_5, PWM_6, 1},
3838
{P3_25, PWM_2, 3},
3939
{P3_26, PWM_3, 3},
4040
{NC, NC, 0}
@@ -54,77 +54,84 @@ __IO uint32_t *PWM_MATCH[] = {
5454

5555
static unsigned int pwm_clock_mhz;
5656

57-
void pwmout_init(pwmout_t* obj, PinName pin) {
57+
void pwmout_init(pwmout_t *obj, PinName pin)
58+
{
5859
// determine the channel
5960
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
6061
MBED_ASSERT(pwm != (PWMName)NC);
6162

6263
obj->pwm = pwm;
6364
obj->MR = PWM_MATCH[pwm];
64-
65+
6566
// ensure the power is on
6667
LPC_SC->PCONP |= 1 << 6;
67-
68+
6869
// ensure clock to /4
6970
LPC_SC->PCLKSEL0 &= ~(0x3 << 12); // pclk = /4
7071
LPC_PWM1->PR = 0; // no pre-scale
71-
72+
7273
// ensure single PWM mode
7374
LPC_PWM1->MCR = 1 << 1; // reset TC on match 0
74-
75+
7576
// enable the specific PWM output
7677
LPC_PWM1->PCR |= 1 << (8 + pwm);
77-
78+
7879
pwm_clock_mhz = SystemCoreClock / 4000000;
79-
80+
8081
// default to 20ms: standard for servos, and fine for e.g. brightness control
8182
pwmout_period_ms(obj, 20);
82-
pwmout_write (obj, 0);
83-
83+
pwmout_write(obj, 0);
84+
8485
// Wire pinout
8586
pinmap_pinout(pin, PinMap_PWM);
8687
}
8788

88-
void pwmout_free(pwmout_t* obj) {
89+
void pwmout_free(pwmout_t *obj)
90+
{
8991
// [TODO]
9092
}
9193

92-
void pwmout_write(pwmout_t* obj, float value) {
94+
void pwmout_write(pwmout_t *obj, float value)
95+
{
9396
if (value < 0.0f) {
9497
value = 0.0;
9598
} else if (value > 1.0f) {
9699
value = 1.0;
97100
}
98-
101+
99102
// set channel match to percentage
100103
uint32_t v = (uint32_t)((float)(LPC_PWM1->MR0) * value);
101-
104+
102105
// workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
103106
if (v == LPC_PWM1->MR0) {
104107
v++;
105108
}
106-
109+
107110
*obj->MR = v;
108-
111+
109112
// accept on next period start
110113
LPC_PWM1->LER |= 1 << obj->pwm;
111114
}
112115

113-
float pwmout_read(pwmout_t* obj) {
116+
float pwmout_read(pwmout_t *obj)
117+
{
114118
float v = (float)(*obj->MR) / (float)(LPC_PWM1->MR0);
115119
return (v > 1.0f) ? (1.0f) : (v);
116120
}
117121

118-
void pwmout_period(pwmout_t* obj, float seconds) {
122+
void pwmout_period(pwmout_t *obj, float seconds)
123+
{
119124
pwmout_period_us(obj, seconds * 1000000.0f);
120125
}
121126

122-
void pwmout_period_ms(pwmout_t* obj, int ms) {
127+
void pwmout_period_ms(pwmout_t *obj, int ms)
128+
{
123129
pwmout_period_us(obj, ms * 1000);
124130
}
125131

126132
// Set the PWM period, keeping the duty cycle the same.
127-
void pwmout_period_us(pwmout_t* obj, int us) {
133+
void pwmout_period_us(pwmout_t *obj, int us)
134+
{
128135
// calculate number of ticks
129136
uint32_t ticks = pwm_clock_mhz * us;
130137

@@ -146,30 +153,42 @@ void pwmout_period_us(pwmout_t* obj, int us) {
146153
LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN;
147154
}
148155

149-
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
156+
int pwmout_read_period_us(pwmout_t *obj)
157+
{
158+
return (float)(LPC_PWM1->MR0);
159+
}
160+
161+
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
162+
{
150163
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
151164
}
152165

153-
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
166+
void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
167+
{
154168
pwmout_pulsewidth_us(obj, ms * 1000);
155169
}
156170

157-
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
171+
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
172+
{
158173
// calculate number of ticks
159174
uint32_t v = pwm_clock_mhz * us;
160-
175+
161176
// workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
162177
if (v == LPC_PWM1->MR0) {
163178
v++;
164179
}
165-
180+
166181
// set the match register value
167182
*obj->MR = v;
168-
183+
169184
// set the channel latch to update value at next period start
170185
LPC_PWM1->LER |= 1 << obj->pwm;
171186
}
172187

188+
int pwmout_read_pulsewidth_us(pwmout_t *obj {
189+
return (timer->MR3 - timer->MR[tid.mr]);
190+
}
191+
173192
const PinMap *pwmout_pinmap()
174193
{
175194
return PinMap_PWM;

0 commit comments

Comments
 (0)