]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC408X / TARGET_LPC4088 / 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 //  PORT ID, PWM ID, Pin function
25 static const PinMap PinMap_PWM[] = {
26     {P1_2,  PWM0_1, 3},
27     {P1_3,  PWM0_2, 3},
28     {P1_5,  PWM0_3, 3},
29     {P1_6,  PWM0_4, 3},
30     {P1_7,  PWM0_5, 3},
31     {P1_11, PWM0_6, 3},
32     {P1_18, PWM1_1, 2},
33     {P1_20, PWM1_2, 2},
34     {P1_21, PWM1_3, 2},
35     {P1_23, PWM1_4, 2},
36     {P1_24, PWM1_5, 2},
37     {P1_26, PWM1_6, 2},
38     {P2_0,  PWM1_1, 1},
39     {P2_1,  PWM1_2, 1},
40     {P2_2,  PWM1_3, 1},
41     {P2_3,  PWM1_4, 1},
42     {P2_4,  PWM1_5, 1},
43     {P2_5,  PWM1_6, 1},
44     {P3_16, PWM0_1, 2},
45     {P3_17, PWM0_2, 2},
46     {P3_18, PWM0_3, 2},
47     {P3_19, PWM0_4, 2},
48     {P3_20, PWM0_5, 2},
49     {P3_21, PWM0_6, 2},
50     {P3_24, PWM1_1, 2},
51     {P3_25, PWM1_2, 2},
52     {P3_26, PWM1_3, 2},
53     {P3_27, PWM1_4, 2},
54     {P3_28, PWM1_5, 2},
55     {P3_29, PWM1_6, 2},
56     {NC, NC, 0}
57 };
58
59 static const uint32_t PWM_mr_offset[7] = {
60     0x18, 0x1C, 0x20, 0x24, 0x40, 0x44, 0x48
61 };
62
63 #define TCR_PWM_EN       0x00000008
64 static unsigned int pwm_clock_mhz;
65
66 void pwmout_init(pwmout_t* obj, PinName pin) {
67     // determine the channel
68     PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
69     MBED_ASSERT(pwm != (PWMName)NC);
70
71     obj->channel = pwm;
72     obj->pwm = LPC_PWM0;
73     
74     if (obj->channel > 6) { // PWM1 is used if pwm > 6
75       obj->channel -= 6;
76       obj->pwm = LPC_PWM1;
77     }
78     
79     obj->MR = (__IO uint32_t *)((uint32_t)obj->pwm + PWM_mr_offset[obj->channel]);
80     
81     // ensure the power is on
82     if (obj->pwm == LPC_PWM0) {
83         LPC_SC->PCONP |= 1 << 5;
84     } else {
85         LPC_SC->PCONP |= 1 << 6;
86     }
87     
88     obj->pwm->PR = 0;                     // no pre-scale
89     
90     // ensure single PWM mode
91     obj->pwm->MCR = 1 << 1; // reset TC on match 0
92     
93     // enable the specific PWM output
94     obj->pwm->PCR |= 1 << (8 + obj->channel);
95     
96     pwm_clock_mhz = PeripheralClock / 1000000;
97     
98     // default to 20ms: standard for servos, and fine for e.g. brightness control
99     pwmout_period_ms(obj, 20);
100     pwmout_write    (obj, 0);
101     
102     // Wire pinout
103     pinmap_pinout(pin, PinMap_PWM);
104 }
105
106 void pwmout_free(pwmout_t* obj) {
107     // [TODO]
108 }
109
110 void pwmout_write(pwmout_t* obj, float value) {
111     if (value < 0.0f) {
112         value = 0.0;
113     } else if (value > 1.0f) {
114         value = 1.0;
115     }
116     
117     // set channel match to percentage
118     uint32_t v = (uint32_t)((float)(obj->pwm->MR0) * value);
119     
120     // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
121     if (v == obj->pwm->MR0) {
122         v++;
123     }
124     
125     *obj->MR = v;
126     
127     // accept on next period start
128     obj->pwm->LER |= 1 << obj->channel;
129 }
130
131 float pwmout_read(pwmout_t* obj) {
132     float v = (float)(*obj->MR) / (float)(obj->pwm->MR0);
133     return (v > 1.0f) ? (1.0f) : (v);
134 }
135
136 void pwmout_period(pwmout_t* obj, float seconds) {
137     pwmout_period_us(obj, seconds * 1000000.0f);
138 }
139
140 void pwmout_period_ms(pwmout_t* obj, int ms) {
141     pwmout_period_us(obj, ms * 1000);
142 }
143
144 // Set the PWM period, keeping the duty cycle the same.
145 void pwmout_period_us(pwmout_t* obj, int us) {
146     // calculate number of ticks
147     uint32_t ticks = pwm_clock_mhz * us;
148     
149     // set reset
150     obj->pwm->TCR = TCR_RESET;
151     
152     // set the global match register
153     obj->pwm->MR0 = ticks;
154     
155     // Scale the pulse width to preserve the duty ratio
156     if (obj->pwm->MR0 > 0) {
157         *obj->MR = (*obj->MR * ticks) / obj->pwm->MR0;
158     }
159     
160     // set the channel latch to update value at next period start
161     obj->pwm->LER |= 1 << 0;
162     
163     // enable counter and pwm, clear reset
164     obj->pwm->TCR = TCR_CNT_EN | TCR_PWM_EN;
165 }
166
167 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
168     pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
169 }
170
171 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
172     pwmout_pulsewidth_us(obj, ms * 1000);
173 }
174
175 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
176     // calculate number of ticks
177     uint32_t v = pwm_clock_mhz * us;
178     
179     // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
180     if (v == obj->pwm->MR0) {
181         v++;
182     }
183     
184     // set the match register value
185     *obj->MR = v;
186     
187     // set the channel latch to update value at next period start
188     obj->pwm->LER |= 1 << obj->channel;
189 }