]>
git.gir.st - tmk_keyboard.git/blob - 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
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
, 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 */
36 {P0_21
, PWM_4
, 1}, /* MR0 */
37 {P0_22
, PWM_5
, 2}, {P1_23
, PWM_5
, 1}, /* MR1 */
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 */
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 */
57 static timer_mr pwm_timer_map
[11] = {
58 {0, 0}, {0, 1}, {0, 2},
60 {2, 0}, {2, 1}, {2, 2},
61 {3, 0}, {3, 1}, {3, 2},
64 static LPC_CTxxBx_Type
*Timers
[4] = {
65 LPC_CT16B0
, LPC_CT16B1
,
66 LPC_CT32B0
, LPC_CT32B1
69 static unsigned int pwm_clock_mhz
;
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
);
79 timer_mr tid
= pwm_timer_map
[pwm
];
80 LPC_CTxxBx_Type
*timer
= Timers
[tid
.timer
];
85 // Power the correspondent timer
86 LPC_SYSCON
->SYSAHBCLKCTRL
|= 1 << (tid
.timer
+ 7);
88 /* Enable PWM function */
89 timer
->PWMC
= (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
91 /* Reset Functionality on MR3 controlling the PWM period */
94 pwm_clock_mhz
= SystemCoreClock
/ 1000000;
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);
101 pinmap_pinout(pin
, PinMap_PWM
);
104 void pwmout_free(pwmout_t
* obj
) {
108 void pwmout_write(pwmout_t
* obj
, float value
) {
111 } else if (value
> 1.0f
) {
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
);
119 timer
->MR
[tid
.mr
] = t_off
;
122 float pwmout_read(pwmout_t
* obj
) {
123 timer_mr tid
= pwm_timer_map
[obj
->pwm
];
124 LPC_CTxxBx_Type
*timer
= Timers
[tid
.timer
];
126 float v
= (float)(timer
->MR3
- timer
->MR
[tid
.mr
]) / (float)(timer
->MR3
);
127 return (v
> 1.0f
) ? (1.0f
) : (v
);
130 void pwmout_period(pwmout_t
* obj
, float seconds
) {
131 pwmout_period_us(obj
, seconds
* 1000000.0f
);
134 void pwmout_period_ms(pwmout_t
* obj
, int ms
) {
135 pwmout_period_us(obj
, ms
* 1000);
138 // Set the PWM period, keeping the duty cycle the same.
139 void pwmout_period_us(pwmout_t
* obj
, int us
) {
141 uint32_t period_ticks
= pwm_clock_mhz
* us
;
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
;
147 timer
->TCR
= TCR_RESET
;
148 timer
->MR3
= period_ticks
;
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
;
157 timer
->TCR
= TCR_CNT_EN
;
160 void pwmout_pulsewidth(pwmout_t
* obj
, float seconds
) {
161 pwmout_pulsewidth_us(obj
, seconds
* 1000000.0f
);
164 void pwmout_pulsewidth_ms(pwmout_t
* obj
, int ms
) {
165 pwmout_pulsewidth_us(obj
, ms
* 1000);
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
];
173 timer
->TCR
= TCR_RESET
;
174 if (t_on
> timer
->MR3
) {
175 pwmout_period_us(obj
, us
);
177 uint32_t t_off
= timer
->MR3
- t_on
;
178 timer
->MR
[tid
.mr
] = t_off
;
179 timer
->TCR
= TCR_CNT_EN
;