]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/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 // PORT ID, PWM ID, Pin function
25 static const PinMap PinMap_PWM
[] = {
43 __IO
uint32_t *PWM_MATCH
[] = {
53 #define TCR_PWM_EN 0x00000008
55 static unsigned int pwm_clock_mhz
;
57 void pwmout_init(pwmout_t
* obj
, PinName pin
) {
58 // determine the channel
59 PWMName pwm
= (PWMName
)pinmap_peripheral(pin
, PinMap_PWM
);
60 MBED_ASSERT(pwm
!= (PWMName
)NC
);
63 obj
->MR
= PWM_MATCH
[pwm
];
65 // ensure the power is on
66 LPC_SC
->PCONP
|= 1 << 6;
69 LPC_SC
->PCLKSEL0
&= ~(0x3 << 12); // pclk = /4
70 LPC_PWM1
->PR
= 0; // no pre-scale
72 // ensure single PWM mode
73 LPC_PWM1
->MCR
= 1 << 1; // reset TC on match 0
75 // enable the specific PWM output
76 LPC_PWM1
->PCR
|= 1 << (8 + pwm
);
78 pwm_clock_mhz
= SystemCoreClock
/ 4000000;
80 // default to 20ms: standard for servos, and fine for e.g. brightness control
81 pwmout_period_ms(obj
, 20);
82 pwmout_write (obj
, 0);
85 pinmap_pinout(pin
, PinMap_PWM
);
88 void pwmout_free(pwmout_t
* obj
) {
92 void pwmout_write(pwmout_t
* obj
, float value
) {
95 } else if (value
> 1.0f
) {
99 // set channel match to percentage
100 uint32_t v
= (uint32_t)((float)(LPC_PWM1
->MR0
) * value
);
102 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
103 if (v
== LPC_PWM1
->MR0
) {
109 // accept on next period start
110 LPC_PWM1
->LER
|= 1 << obj
->pwm
;
113 float pwmout_read(pwmout_t
* obj
) {
114 float v
= (float)(*obj
->MR
) / (float)(LPC_PWM1
->MR0
);
115 return (v
> 1.0f
) ? (1.0f
) : (v
);
118 void pwmout_period(pwmout_t
* obj
, float seconds
) {
119 pwmout_period_us(obj
, seconds
* 1000000.0f
);
122 void pwmout_period_ms(pwmout_t
* obj
, int ms
) {
123 pwmout_period_us(obj
, ms
* 1000);
126 // Set the PWM period, keeping the duty cycle the same.
127 void pwmout_period_us(pwmout_t
* obj
, int us
) {
128 // calculate number of ticks
129 uint32_t ticks
= pwm_clock_mhz
* us
;
132 LPC_PWM1
->TCR
= TCR_RESET
;
134 // set the global match register
135 LPC_PWM1
->MR0
= ticks
;
137 // Scale the pulse width to preserve the duty ratio
138 if (LPC_PWM1
->MR0
> 0) {
139 *obj
->MR
= (*obj
->MR
* ticks
) / LPC_PWM1
->MR0
;
142 // set the channel latch to update value at next period start
143 LPC_PWM1
->LER
|= 1 << 0;
145 // enable counter and pwm, clear reset
146 LPC_PWM1
->TCR
= TCR_CNT_EN
| TCR_PWM_EN
;
149 void pwmout_pulsewidth(pwmout_t
* obj
, float seconds
) {
150 pwmout_pulsewidth_us(obj
, seconds
* 1000000.0f
);
153 void pwmout_pulsewidth_ms(pwmout_t
* obj
, int ms
) {
154 pwmout_pulsewidth_us(obj
, ms
* 1000);
157 void pwmout_pulsewidth_us(pwmout_t
* obj
, int us
) {
158 // calculate number of ticks
159 uint32_t v
= pwm_clock_mhz
* us
;
161 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
162 if (v
== LPC_PWM1
->MR0
) {
166 // set the match register value
169 // set the channel latch to update value at next period start
170 LPC_PWM1
->LER
|= 1 << obj
->pwm
;