]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC13XX / 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, 2}, {P1_13, PWM_1, 2}, /* MR0 */
32 {P0_9 , PWM_2, 2}, {P1_14, PWM_2, 2}, /* MR1 */
33 {P0_10, PWM_3, 3}, {P1_15, PWM_3, 2}, /* MR2 */
34
35 /* CT16B1 */
36 {P0_21, PWM_4, 1}, /* MR0 */
37 {P0_22, PWM_5, 2}, {P1_23, PWM_5, 1}, /* MR1 */
38
39 /* CT32B0 */
40 {P0_18, PWM_6, 2}, {P1_24, PWM_6, 1}, /* MR0 */
41 {P0_19, PWM_7, 2}, {P1_25, PWM_7, 1}, /* MR1 */
42 {P0_1 , PWM_8, 2}, {P1_26, PWM_8, 1}, /* MR2 */
43
44 /* CT32B1 */
45 {P0_13, PWM_9 , 3}, //{P1_0, PWM_9 , 1}, /* MR0 */
46 {P0_14, PWM_10, 3}, //{P1_1, PWM_10, 1}, /* MR1 */
47 {P0_15, PWM_11, 3}, //{P1_2, PWM_11, 1}, /* MR2 */
48
49 {NC, NC, 0}
50 };
51
52 typedef struct {
53 uint8_t timer;
54 uint8_t mr;
55 } timer_mr;
56
57 static timer_mr pwm_timer_map[11] = {
58 {0, 0}, {0, 1}, {0, 2},
59 {1, 0}, {1, 1},
60 {2, 0}, {2, 1}, {2, 2},
61 {3, 0}, {3, 1}, {3, 2},
62 };
63
64 static LPC_CTxxBx_Type *Timers[4] = {
65 LPC_CT16B0, LPC_CT16B1,
66 LPC_CT32B0, LPC_CT32B1
67 };
68
69 static unsigned int pwm_clock_mhz;
70
71 void pwmout_init(pwmout_t* obj, PinName pin) {
72 // determine the channel
73 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
74 MBED_ASSERT(pwm != (uint32_t)NC);
75
76 obj->pwm = pwm;
77
78 // Timer registers
79 timer_mr tid = pwm_timer_map[pwm];
80 LPC_CTxxBx_Type *timer = Timers[tid.timer];
81
82 // Disable timer
83 timer->TCR = 0;
84
85 // Power the correspondent timer
86 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
87
88 /* Enable PWM function */
89 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
90
91 /* Reset Functionality on MR3 controlling the PWM period */
92 timer->MCR = 1 << 10;
93
94 pwm_clock_mhz = SystemCoreClock / 1000000;
95
96 // default to 20ms: standard for servos, and fine for e.g. brightness control
97 pwmout_period_ms(obj, 20);
98 pwmout_write (obj, 0);
99
100 // Wire pinout
101 pinmap_pinout(pin, PinMap_PWM);
102 }
103
104 void pwmout_free(pwmout_t* obj) {
105 // [TODO]
106 }
107
108 void pwmout_write(pwmout_t* obj, float value) {
109 if (value < 0.0f) {
110 value = 0.0;
111 } else if (value > 1.0f) {
112 value = 1.0;
113 }
114
115 timer_mr tid = pwm_timer_map[obj->pwm];
116 LPC_CTxxBx_Type *timer = Timers[tid.timer];
117 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
118
119 timer->MR[tid.mr] = t_off;
120 }
121
122 float pwmout_read(pwmout_t* obj) {
123 timer_mr tid = pwm_timer_map[obj->pwm];
124 LPC_CTxxBx_Type *timer = Timers[tid.timer];
125
126 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
127 return (v > 1.0f) ? (1.0f) : (v);
128 }
129
130 void pwmout_period(pwmout_t* obj, float seconds) {
131 pwmout_period_us(obj, seconds * 1000000.0f);
132 }
133
134 void pwmout_period_ms(pwmout_t* obj, int ms) {
135 pwmout_period_us(obj, ms * 1000);
136 }
137
138 // Set the PWM period, keeping the duty cycle the same.
139 void pwmout_period_us(pwmout_t* obj, int us) {
140 int i = 0;
141 uint32_t period_ticks = pwm_clock_mhz * us;
142
143 timer_mr tid = pwm_timer_map[obj->pwm];
144 LPC_CTxxBx_Type *timer = Timers[tid.timer];
145 uint32_t old_period_ticks = timer->MR3;
146
147 timer->TCR = TCR_RESET;
148 timer->MR3 = period_ticks;
149
150 // Scale the pulse width to preserve the duty ratio
151 if (old_period_ticks > 0) {
152 for (i=0; i<3; i++) {
153 uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
154 timer->MR[i] = t_off;
155 }
156 }
157 timer->TCR = TCR_CNT_EN;
158 }
159
160 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
161 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
162 }
163
164 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
165 pwmout_pulsewidth_us(obj, ms * 1000);
166 }
167
168 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
169 uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
170 timer_mr tid = pwm_timer_map[obj->pwm];
171 LPC_CTxxBx_Type *timer = Timers[tid.timer];
172
173 timer->TCR = TCR_RESET;
174 if (t_on > timer->MR3) {
175 pwmout_period_us(obj, us);
176 }
177 uint32_t t_off = timer->MR3 - t_on;
178 timer->MR[tid.mr] = t_off;
179 timer->TCR = TCR_CNT_EN;
180 }
Imprint / Impressum