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"
20 #include "mbed_error.h"
22 // Ported from LPC824 and adapted.
26 #define PWM_IRQn SCT_IRQn
28 // Bit flags for used SCT Outputs
29 static unsigned char sct_used = 0;
30 static int sct_inited = 0;
32 // Find available output channel
33 // Max number of PWM outputs is 4 on LPC812
34 static int get_available_sct() {
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)
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);
52 int sct_n = get_available_sct();
54 error("No available SCT Output");
57 sct_used |= (1 << sct_n);
59 obj->pwm = (LPC_SCT_TypeDef*)LPC_SCT;
62 LPC_SCT_TypeDef* pwm = obj->pwm;
64 // Init SCT on first use
68 // Enable the SCT clock
69 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);
71 // Clear peripheral reset the SCT:
72 LPC_SYSCON->PRESETCTRL |= (1 << 8);
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);
79 // halt and clear the counter
80 pwm->CTRL_U |= (1 << 2) | (1 << 3);
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);
86 pwm->EVENT[0].CTRL = (1 << 12) | 0; // Event_0 on Match_0
87 pwm->EVENT[0].STATE = 0xFFFFFFFF; // All states
89 // unhalt the counter:
90 // - clearing bit 2 of the CTRL register
91 pwm->CTRL_U &= ~(1 << 2);
94 //NVIC_SetVector(PWM_IRQn, (uint32_t)pwm_irq_handler);
95 //NVIC_EnableIRQ(PWM_IRQn);
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
104 LPC_SWM->PINASSIGN[6] &= ~0xFF000000;
105 LPC_SWM->PINASSIGN[6] |= (pin << 24);
109 LPC_SWM->PINASSIGN[7] &= ~0x000000FF;
110 LPC_SWM->PINASSIGN[7] |= (pin);
114 LPC_SWM->PINASSIGN[7] &= ~0x0000FF00;
115 LPC_SWM->PINASSIGN[7] |= (pin << 8);
119 LPC_SWM->PINASSIGN[7] &= ~0x00FF0000;
120 LPC_SWM->PINASSIGN[7] |= (pin << 16);
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
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)
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
137 void pwmout_free(pwmout_t* obj) {
138 // PWM channel is now free
139 sct_used &= ~(1 << obj->pwm_ch);
141 // Disable the SCT clock when all channels free
143 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 8);
148 // Set new dutycycle (0.0 .. 1.0)
149 void pwmout_write(pwmout_t* obj, float value) {
150 //value is new dutycycle
153 } else if (value > 1.0f) {
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
162 // Get dutycycle (0.0 .. 1.0)
163 float pwmout_read(pwmout_t* obj) {
164 uint32_t t_period = obj->pwm->MATCHREL[0].U;
171 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch) + 1].U;
172 float v = (float)t_off/(float)t_period;
174 return (v > 1.0f) ? (1.0f) : (v);
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);
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);
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) {
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
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.
200 // obj->pwm->MATCHREL[(obj->pwm_ch) + 1].L = 0; // New endtime for this channel
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
211 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
212 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
216 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms){
217 pwmout_pulsewidth_us(obj, ms * 1000);
221 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
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