]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC81X/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC81X / 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 #include "mbed_error.h"
21
22 // Ported from LPC824 and adapted.
23
24 #if DEVICE_PWMOUT
25
26 #define PWM_IRQn SCT_IRQn
27
28 // Bit flags for used SCT Outputs
29 static unsigned char sct_used = 0;
30 static int sct_inited = 0;
31
32 // Find available output channel
33 // Max number of PWM outputs is 4 on LPC812
34 static int get_available_sct() {
35 int i;
36
37 // Find available output channel 0..3
38 // Also need one Match register per channel
39 for (i = 0; i < CONFIG_SCT_nOU; i++) {
40 // for (i = 0; i < 4; i++) {
41 if ((sct_used & (1 << i)) == 0)
42 return i;
43 }
44 return -1;
45 }
46
47 // Any Port pin may be used for PWM.
48 // Max number of PWM outputs is 4
49 void pwmout_init(pwmout_t* obj, PinName pin) {
50 MBED_ASSERT(pin != (uint32_t)NC);
51
52 int sct_n = get_available_sct();
53 if (sct_n == -1) {
54 error("No available SCT Output");
55 }
56
57 sct_used |= (1 << sct_n);
58
59 obj->pwm = (LPC_SCT_TypeDef*)LPC_SCT;
60 obj->pwm_ch = sct_n;
61
62 LPC_SCT_TypeDef* pwm = obj->pwm;
63
64 // Init SCT on first use
65 if (! sct_inited) {
66 sct_inited = 1;
67
68 // Enable the SCT clock
69 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);
70
71 // Clear peripheral reset the SCT:
72 LPC_SYSCON->PRESETCTRL |= (1 << 8);
73
74 // Two 16-bit counters, autolimit (ie reset on Match_0)
75 //pwm->CONFIG &= ~(0x1);
76 //pwm->CONFIG |= (1 << 17);
77 pwm->CONFIG |= ((0x3 << 17) | 0x01);
78
79 // halt and clear the counter
80 pwm->CTRL_U |= (1 << 2) | (1 << 3);
81
82 // System Clock (30 Mhz) -> Prescaler -> us_ticker (1 MHz)
83 pwm->CTRL_U &= ~(0x7F << 5);
84 pwm->CTRL_U |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
85
86 pwm->EVENT[0].CTRL = (1 << 12) | 0; // Event_0 on Match_0
87 pwm->EVENT[0].STATE = 0xFFFFFFFF; // All states
88
89 // unhalt the counter:
90 // - clearing bit 2 of the CTRL register
91 pwm->CTRL_U &= ~(1 << 2);
92
93 // Not using IRQs
94 //NVIC_SetVector(PWM_IRQn, (uint32_t)pwm_irq_handler);
95 //NVIC_EnableIRQ(PWM_IRQn);
96 }
97
98 // LPC81x has only one SCT and 4 Outputs
99 // LPC82x has only one SCT and 6 Outputs
100 // LPC1549 has 4 SCTs and 16 Outputs
101 switch(sct_n) {
102 case 0:
103 // SCTx_OUT0
104 LPC_SWM->PINASSIGN[6] &= ~0xFF000000;
105 LPC_SWM->PINASSIGN[6] |= (pin << 24);
106 break;
107 case 1:
108 // SCTx_OUT1
109 LPC_SWM->PINASSIGN[7] &= ~0x000000FF;
110 LPC_SWM->PINASSIGN[7] |= (pin);
111 break;
112 case 2:
113 // SCTx_OUT2
114 LPC_SWM->PINASSIGN[7] &= ~0x0000FF00;
115 LPC_SWM->PINASSIGN[7] |= (pin << 8);
116 break;
117 case 3:
118 // SCTx_OUT3
119 LPC_SWM->PINASSIGN[7] &= ~0x00FF0000;
120 LPC_SWM->PINASSIGN[7] |= (pin << 16);
121 break;
122 default:
123 break;
124 }
125
126 pwm->EVENT[sct_n + 1].CTRL = (1 << 12) | (sct_n + 1); // Event_n on Match_n
127 pwm->EVENT[sct_n + 1].STATE = 0xFFFFFFFF; // All states
128
129 pwm->OUT[sct_n].SET = (1 << 0); // All PWM channels are SET on Event_0
130 pwm->OUT[sct_n].CLR = (1 << (sct_n + 1)); // PWM ch is CLRed on Event_(ch+1)
131
132 // default to 20ms: standard for servos, and fine for e.g. brightness control
133 pwmout_period_ms(obj, 20); // 20ms period
134 pwmout_write (obj, 0.0); // 0ms pulsewidth, dutycycle 0
135 }
136
137 void pwmout_free(pwmout_t* obj) {
138 // PWM channel is now free
139 sct_used &= ~(1 << obj->pwm_ch);
140
141 // Disable the SCT clock when all channels free
142 if (sct_used == 0) {
143 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 8);
144 sct_inited = 0;
145 };
146 }
147
148 // Set new dutycycle (0.0 .. 1.0)
149 void pwmout_write(pwmout_t* obj, float value) {
150 //value is new dutycycle
151 if (value < 0.0f) {
152 value = 0.0;
153 } else if (value > 1.0f) {
154 value = 1.0;
155 }
156
157 // Match_0 is PWM period. Compute new endtime of pulse for current channel
158 uint32_t t_off = (uint32_t)((float)(obj->pwm->MATCHREL[0].U) * value);
159 obj->pwm->MATCHREL[(obj->pwm_ch) + 1].U = t_off; // New endtime
160 }
161
162 // Get dutycycle (0.0 .. 1.0)
163 float pwmout_read(pwmout_t* obj) {
164 uint32_t t_period = obj->pwm->MATCHREL[0].U;
165
166 //Sanity check
167 if (t_period == 0) {
168 return 0.0;
169 };
170
171 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch) + 1].U;
172 float v = (float)t_off/(float)t_period;
173 //Sanity check
174 return (v > 1.0f) ? (1.0f) : (v);
175 }
176
177 // Set the PWM period, keeping the duty cycle the same (for this channel only!).
178 void pwmout_period(pwmout_t* obj, float seconds){
179 pwmout_period_us(obj, seconds * 1000000.0f);
180 }
181
182 // Set the PWM period, keeping the duty cycle the same (for this channel only!).
183 void pwmout_period_ms(pwmout_t* obj, int ms) {
184 pwmout_period_us(obj, ms * 1000);
185 }
186
187 // Set the PWM period, keeping the duty cycle the same (for this channel only!).
188 void pwmout_period_us(pwmout_t* obj, int us) {
189
190 uint32_t t_period = obj->pwm->MATCHREL[0].U; // Current PWM period
191 obj->pwm->MATCHREL[0].U = (uint32_t)us; // New PWM period
192
193 //Keep the dutycycle for the new PWM period
194 //Should really do this for all active channels!!
195 //This problem exists in all mbed libs.
196
197 //Sanity check
198 if (t_period == 0) {
199 return;
200 // obj->pwm->MATCHREL[(obj->pwm_ch) + 1].L = 0; // New endtime for this channel
201 }
202 else {
203 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch) + 1].U;
204 float v = (float)t_off/(float)t_period;
205 obj->pwm->MATCHREL[(obj->pwm_ch) + 1].U = (uint32_t)((float)us * (float)v); // New endtime for this channel
206 }
207 }
208
209
210 //Set pulsewidth
211 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
212 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
213 }
214
215 //Set pulsewidth
216 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms){
217 pwmout_pulsewidth_us(obj, ms * 1000);
218 }
219
220 //Set pulsewidth
221 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
222
223 //Should add Sanity check to make sure pulsewidth < period!
224 obj->pwm->MATCHREL[(obj->pwm_ch) + 1].U = (uint32_t)us; // New endtime for this channel
225 }
226
227 #endif
Imprint / Impressum