Skip to content

Commit 7270f29

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

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

targets/TARGET_NXP/TARGET_LPC11XX_11CXX/pwmout_api.c

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
*/
2929
static const PinMap PinMap_PWM[] = {
3030
/* CT16B0 */
31-
{P0_8 , PWM_1, 0x02}, /* MR0 */
32-
{P0_9 , PWM_2, 0x02}, /* MR1 */
31+
{P0_8, PWM_1, 0x02}, /* MR0 */
32+
{P0_9, PWM_2, 0x02}, /* MR1 */
3333

3434
/* CT16B1 */
35-
{P1_9 , PWM_3, 0x01}, /* MR0 */
35+
{P1_9, PWM_3, 0x01}, /* MR0 */
3636
{P1_10, PWM_4, 0x02}, /* MR1 */
3737

3838
/* CT32B0 */
39-
{P0_1 , PWM_5, 0x02}, /* MR2 */
39+
{P0_1, PWM_5, 0x02}, /* MR2 */
4040

41-
{NC , NC ,0x00}
41+
{NC, NC, 0x00}
4242
};
4343

4444
typedef struct {
@@ -61,49 +61,52 @@ static LPC_TMR_TypeDef *Timers[3] = {
6161
LPC_TMR32B0
6262
};
6363

64-
void pwmout_init(pwmout_t* obj, PinName pin) {
64+
void pwmout_init(pwmout_t *obj, PinName pin)
65+
{
6566
// determine the channel
6667
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
6768
MBED_ASSERT(pwm != (PWMName)NC);
6869

6970
obj->pwm = pwm;
70-
71+
7172
// Timer registers
7273
timer_mr tid = pwm_timer_map[pwm];
7374
LPC_TMR_TypeDef *timer = Timers[tid.timer];
74-
75+
7576
// Disable timer
7677
timer->TCR = 0;
77-
78+
7879
// Power the correspondent timer
7980
LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
80-
81+
8182
/* Enable PWM function */
82-
timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
83-
83+
timer->PWMC = (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
84+
8485
/* Reset Functionality on MR3 controlling the PWM period */
8586
timer->MCR = 1 << 10;
86-
87+
8788
if (timer == LPC_TMR16B0 || timer == LPC_TMR16B1) {
88-
/* Set 16-bit timer prescaler to avoid timer expire for default 20ms */
89-
/* This can be also modified by user application, but the prescaler value */
90-
/* might be trade-off to timer accuracy */
89+
/* Set 16-bit timer prescaler to avoid timer expire for default 20ms */
90+
/* This can be also modified by user application, but the prescaler value */
91+
/* might be trade-off to timer accuracy */
9192
timer->PR = 30;
9293
}
9394

9495
// default to 20ms: standard for servos, and fine for e.g. brightness control
9596
pwmout_period_ms(obj, 20);
96-
pwmout_write (obj, 0);
97-
97+
pwmout_write(obj, 0);
98+
9899
// Wire pinout
99100
pinmap_pinout(pin, PinMap_PWM);
100101
}
101102

102-
void pwmout_free(pwmout_t* obj) {
103+
void pwmout_free(pwmout_t *obj)
104+
{
103105
// [TODO]
104106
}
105107

106-
void pwmout_write(pwmout_t* obj, float value) {
108+
void pwmout_write(pwmout_t *obj, float value)
109+
{
107110
if (value < 0.0f) {
108111
value = 0.0;
109112
} else if (value > 1.0f) {
@@ -123,61 +126,73 @@ void pwmout_write(pwmout_t* obj, float value) {
123126
timer->TCR = TCR_CNT_EN;
124127
}
125128

126-
float pwmout_read(pwmout_t* obj) {
129+
float pwmout_read(pwmout_t *obj)
130+
{
127131
timer_mr tid = pwm_timer_map[obj->pwm];
128132
LPC_TMR_TypeDef *timer = Timers[tid.timer];
129-
133+
130134
float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
131135
if (timer->MR[tid.mr] > timer->MR3) {
132136
v = 0.0f;
133137
}
134138
return (v > 1.0f) ? (1.0f) : (v);
135139
}
136140

137-
void pwmout_period(pwmout_t* obj, float seconds) {
141+
void pwmout_period(pwmout_t *obj, float seconds)
142+
{
138143
pwmout_period_us(obj, seconds * 1000000.0f);
139144
}
140145

141-
void pwmout_period_ms(pwmout_t* obj, int ms) {
146+
void pwmout_period_ms(pwmout_t *obj, int ms)
147+
{
142148
pwmout_period_us(obj, ms * 1000);
143149
}
144150

145151
// Set the PWM period, keeping the duty cycle the same.
146-
void pwmout_period_us(pwmout_t* obj, int us) {
152+
void pwmout_period_us(pwmout_t *obj, int us)
153+
{
147154
int i = 0;
148155
uint32_t period_ticks;
149-
156+
150157
timer_mr tid = pwm_timer_map[obj->pwm];
151158
LPC_TMR_TypeDef *timer = Timers[tid.timer];
152159
uint32_t old_period_ticks = timer->MR3;
153160
period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1);
154161

155162
timer->TCR = TCR_RESET;
156163
timer->MR3 = period_ticks;
157-
164+
158165
// Scale the pulse width to preserve the duty ratio
159166
if (old_period_ticks > 0) {
160-
for (i=0; i<3; i++) {
167+
for (i = 0; i < 3; i++) {
161168
uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
162169
timer->MR[i] = t_off;
163170
}
164171
}
165172
timer->TCR = TCR_CNT_EN;
166173
}
167174

168-
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
175+
int pwmout_read_period_us(pwmout_t *obj)
176+
{
177+
return (timer->MR3);
178+
}
179+
180+
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
181+
{
169182
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
170183
}
171184

172-
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
185+
void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
186+
{
173187
pwmout_pulsewidth_us(obj, ms * 1000);
174188
}
175189

176-
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
190+
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
191+
{
177192
timer_mr tid = pwm_timer_map[obj->pwm];
178193
LPC_TMR_TypeDef *timer = Timers[tid.timer];
179194
uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1));
180-
195+
181196
timer->TCR = TCR_RESET;
182197
if (t_on > timer->MR3) {
183198
pwmout_period_us(obj, us);
@@ -187,6 +202,10 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
187202
timer->TCR = TCR_CNT_EN;
188203
}
189204

205+
int pwmout_read_pulsewidth_us(pwmout_t *obj {
206+
return (timer->MR3 - timer->MR[tid.mr]);
207+
}
208+
190209
const PinMap *pwmout_pinmap()
191210
{
192211
return PinMap_PWM;

0 commit comments

Comments
 (0)