1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "mbed_assert.h"
17 #include "pwmout_api.h"
21 #define TCR_CNT_EN 0x00000001
22 #define TCR_RESET 0x00000002
24 /* To have a PWM where we can change both the period and the duty cycle,
25 * we need an entire timer. With the following conventions:
26 * * MR3 is used for the PWM period
27 * * MR0, MR1, MR2 are used for the duty cycle
29 static const PinMap PinMap_PWM[] = {
31 {P0_8 , PWM_1, 0x02}, /* MR0 */
32 {P0_9 , PWM_2, 0x02}, /* MR1 */
35 {P1_9 , PWM_3, 0x01}, /* MR0 */
36 {P1_10, PWM_4, 0x02}, /* MR1 */
39 {P0_1 , PWM_5, 0x02}, /* MR2 */
49 static timer_mr pwm_timer_map[5] = {
50 {0, 0}, /* CT16B0, MR0 */
51 {0, 1}, /* CT16B0, MR1 */
53 {1, 0}, /* CT16B1, MR0 */
54 {1, 1}, /* CT16B1, MR1 */
56 {2, 2}, /* CT32B0, MR2 */
59 static LPC_TMR_TypeDef *Timers[3] = {
60 LPC_TMR16B0, LPC_TMR16B1,
64 void pwmout_init(pwmout_t* obj, PinName pin) {
65 // determine the channel
66 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
67 MBED_ASSERT(pwm != (uint32_t)NC);
72 timer_mr tid = pwm_timer_map[pwm];
73 LPC_TMR_TypeDef *timer = Timers[tid.timer];
78 // Power the correspondent timer
79 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
81 /* Enable PWM function */
82 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
84 /* Reset Functionality on MR3 controlling the PWM period */
87 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 */
94 // default to 20ms: standard for servos, and fine for e.g. brightness control
95 pwmout_period_ms(obj, 20);
96 pwmout_write (obj, 0);
99 pinmap_pinout(pin, PinMap_PWM);
102 void pwmout_free(pwmout_t* obj) {
106 void pwmout_write(pwmout_t* obj, float value) {
109 } else if (value > 1.0f) {
113 timer_mr tid = pwm_timer_map[obj->pwm];
114 LPC_TMR_TypeDef *timer = Timers[tid.timer];
115 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
116 // to avoid spike pulse when duty is 0%
121 timer->TCR = TCR_RESET;
122 timer->MR[tid.mr] = t_off;
123 timer->TCR = TCR_CNT_EN;
126 float pwmout_read(pwmout_t* obj) {
127 timer_mr tid = pwm_timer_map[obj->pwm];
128 LPC_TMR_TypeDef *timer = Timers[tid.timer];
130 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
131 if (timer->MR[tid.mr] > timer->MR3) {
134 return (v > 1.0f) ? (1.0f) : (v);
137 void pwmout_period(pwmout_t* obj, float seconds) {
138 pwmout_period_us(obj, seconds * 1000000.0f);
141 void pwmout_period_ms(pwmout_t* obj, int ms) {
142 pwmout_period_us(obj, ms * 1000);
145 // Set the PWM period, keeping the duty cycle the same.
146 void pwmout_period_us(pwmout_t* obj, int us) {
148 uint32_t period_ticks;
150 timer_mr tid = pwm_timer_map[obj->pwm];
151 LPC_TMR_TypeDef *timer = Timers[tid.timer];
152 uint32_t old_period_ticks = timer->MR3;
153 period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1);
155 timer->TCR = TCR_RESET;
156 timer->MR3 = period_ticks;
158 // Scale the pulse width to preserve the duty ratio
159 if (old_period_ticks > 0) {
160 for (i=0; i<3; i++) {
161 uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
162 timer->MR[i] = t_off;
165 timer->TCR = TCR_CNT_EN;
168 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
169 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
172 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
173 pwmout_pulsewidth_us(obj, ms * 1000);
176 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
177 timer_mr tid = pwm_timer_map[obj->pwm];
178 LPC_TMR_TypeDef *timer = Timers[tid.timer];
179 uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1));
181 timer->TCR = TCR_RESET;
182 if (t_on > timer->MR3) {
183 pwmout_period_us(obj, us);
185 uint32_t t_off = timer->MR3 - t_on;
186 timer->MR[tid.mr] = t_off;
187 timer->TCR = TCR_CNT_EN;