]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/pwmout_api.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11XX_11CXX / pwmout_api.c
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 #include "mbed_assert.h"
17 #include "pwmout_api.h"
18 #include "cmsis.h"
19 #include "pinmap.h"
20
21 #define TCR_CNT_EN 0x00000001
22 #define TCR_RESET 0x00000002
23
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
28 */
29 static const PinMap PinMap_PWM[] = {
30 /* CT16B0 */
31 {P0_8 , PWM_1, 0x02}, /* MR0 */
32 {P0_9 , PWM_2, 0x02}, /* MR1 */
33
34 /* CT16B1 */
35 {P1_9 , PWM_3, 0x01}, /* MR0 */
36 {P1_10, PWM_4, 0x02}, /* MR1 */
37
38 /* CT32B0 */
39 {P0_1 , PWM_5, 0x02}, /* MR2 */
40
41 {NC , NC ,0x00}
42 };
43
44 typedef struct {
45 uint8_t timer;
46 uint8_t mr;
47 } timer_mr;
48
49 static timer_mr pwm_timer_map[5] = {
50 {0, 0}, /* CT16B0, MR0 */
51 {0, 1}, /* CT16B0, MR1 */
52
53 {1, 0}, /* CT16B1, MR0 */
54 {1, 1}, /* CT16B1, MR1 */
55
56 {2, 2}, /* CT32B0, MR2 */
57 };
58
59 static LPC_TMR_TypeDef *Timers[3] = {
60 LPC_TMR16B0, LPC_TMR16B1,
61 LPC_TMR32B0
62 };
63
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);
68
69 obj->pwm = pwm;
70
71 // Timer registers
72 timer_mr tid = pwm_timer_map[pwm];
73 LPC_TMR_TypeDef *timer = Timers[tid.timer];
74
75 // Disable timer
76 timer->TCR = 0;
77
78 // Power the correspondent timer
79 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
80
81 /* Enable PWM function */
82 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
83
84 /* Reset Functionality on MR3 controlling the PWM period */
85 timer->MCR = 1 << 10;
86
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 */
91 timer->PR = 30;
92 }
93
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);
97
98 // Wire pinout
99 pinmap_pinout(pin, PinMap_PWM);
100 }
101
102 void pwmout_free(pwmout_t* obj) {
103 // [TODO]
104 }
105
106 void pwmout_write(pwmout_t* obj, float value) {
107 if (value < 0.0f) {
108 value = 0.0;
109 } else if (value > 1.0f) {
110 value = 1.0;
111 }
112
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%
117 if (value == 0) {
118 t_off++;
119 }
120
121 timer->TCR = TCR_RESET;
122 timer->MR[tid.mr] = t_off;
123 timer->TCR = TCR_CNT_EN;
124 }
125
126 float pwmout_read(pwmout_t* obj) {
127 timer_mr tid = pwm_timer_map[obj->pwm];
128 LPC_TMR_TypeDef *timer = Timers[tid.timer];
129
130 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
131 if (timer->MR[tid.mr] > timer->MR3) {
132 v = 0.0f;
133 }
134 return (v > 1.0f) ? (1.0f) : (v);
135 }
136
137 void pwmout_period(pwmout_t* obj, float seconds) {
138 pwmout_period_us(obj, seconds * 1000000.0f);
139 }
140
141 void pwmout_period_ms(pwmout_t* obj, int ms) {
142 pwmout_period_us(obj, ms * 1000);
143 }
144
145 // Set the PWM period, keeping the duty cycle the same.
146 void pwmout_period_us(pwmout_t* obj, int us) {
147 int i = 0;
148 uint32_t period_ticks;
149
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);
154
155 timer->TCR = TCR_RESET;
156 timer->MR3 = period_ticks;
157
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;
163 }
164 }
165 timer->TCR = TCR_CNT_EN;
166 }
167
168 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
169 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
170 }
171
172 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
173 pwmout_pulsewidth_us(obj, ms * 1000);
174 }
175
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));
180
181 timer->TCR = TCR_RESET;
182 if (t_on > timer->MR3) {
183 pwmout_period_us(obj, us);
184 }
185 uint32_t t_off = timer->MR3 - t_on;
186 timer->MR[tid.mr] = t_off;
187 timer->TCR = TCR_CNT_EN;
188 }
Imprint / Impressum