]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC15XX / 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 static LPC_SCT0_Type *SCTs[4] = {
23     (LPC_SCT0_Type*)LPC_SCT0,
24     (LPC_SCT0_Type*)LPC_SCT1,
25     (LPC_SCT0_Type*)LPC_SCT2,
26     (LPC_SCT0_Type*)LPC_SCT3,
27 };
28
29 // bit flags for used SCTs
30 static unsigned char sct_used = 0;
31 static int get_available_sct(void) {
32     int i;
33     for (i=0; i<4; i++) {
34         if ((sct_used & (1 << i)) == 0)
35             return i;
36     }
37     return -1;
38 }
39
40 void pwmout_init(pwmout_t* obj, PinName pin) {
41     MBED_ASSERT(pin != (uint32_t)NC);
42
43     int sct_n = get_available_sct();
44     if (sct_n == -1) {
45         error("No available SCT");
46     }
47     
48     sct_used |= (1 << sct_n);
49     obj->pwm =  SCTs[sct_n];
50     obj->pwm_ch = sct_n;
51
52     LPC_SCT0_Type* pwm = obj->pwm;
53
54     // Enable the SCT clock
55     LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << (obj->pwm_ch + 2));
56
57     // Clear peripheral reset the SCT:
58     LPC_SYSCON->PRESETCTRL1 |=  (1 << (obj->pwm_ch + 2));
59     LPC_SYSCON->PRESETCTRL1 &= ~(1 << (obj->pwm_ch + 2));
60     
61     switch(obj->pwm_ch) {
62         case 0:
63             // SCT0_OUT0
64             LPC_SWM->PINASSIGN[7] &= ~0x0000FF00;
65             LPC_SWM->PINASSIGN[7] |= (pin << 8);
66             break;
67         case 1:
68             // SCT1_OUT0
69             LPC_SWM->PINASSIGN[8] &= ~0x000000FF;
70             LPC_SWM->PINASSIGN[8] |= (pin);
71             break;
72         case 2:
73             // SCT2_OUT0
74             LPC_SWM->PINASSIGN[8] &= ~0xFF000000;
75             LPC_SWM->PINASSIGN[8] |= (pin << 24);
76             break;
77         case 3:
78             // SCT3_OUT0
79             LPC_SWM->PINASSIGN[9] &= ~0x00FF0000;
80             LPC_SWM->PINASSIGN[9] |= (pin << 16);
81             break;
82         default:
83             break;
84     }
85     
86     // Unified 32-bit counter, autolimit
87     pwm->CONFIG |= ((0x3 << 17) | 0x01);
88     
89     // halt and clear the counter
90     pwm->CTRL |= (1 << 2) | (1 << 3);
91     
92     // System Clock -> us_ticker (1)MHz
93     pwm->CTRL &= ~(0x7F << 5);
94     pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
95     
96     // Match reload register
97     pwm->MATCHREL0 = 20000; // 20ms
98     pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty
99     
100     pwm->OUT0_SET = (1 << 0); // event 0
101     pwm->OUT0_CLR = (1 << 1); // event 1
102
103     pwm->EV0_CTRL  = (1 << 12);
104     pwm->EV0_STATE = 0xFFFFFFFF;
105     pwm->EV1_CTRL  = (1 << 12) | (1 << 0);
106     pwm->EV1_STATE = 0xFFFFFFFF;
107
108     // unhalt the counter:
109     //    - clearing bit 2 of the CTRL register
110     pwm->CTRL &= ~(1 << 2);
111
112     // default to 20ms: standard for servos, and fine for e.g. brightness control
113     pwmout_period_ms(obj, 20);
114     pwmout_write    (obj, 0);
115 }
116
117 void pwmout_free(pwmout_t* obj) {
118     // Disable the SCT clock
119     LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1 << (obj->pwm_ch + 2));
120     sct_used &= ~(1 << obj->pwm_ch);
121 }
122
123 void pwmout_write(pwmout_t* obj, float value) {
124     LPC_SCT0_Type* pwm = obj->pwm;
125     if (value < 0.0f) {
126         value = 0.0;
127     } else if (value > 1.0f) {
128         value = 1.0;
129     }
130     uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0) * value);
131     pwm->MATCHREL1 = t_on;
132 }
133
134 float pwmout_read(pwmout_t* obj) {
135     uint32_t t_off = obj->pwm->MATCHREL0;
136     uint32_t t_on  = obj->pwm->MATCHREL1;
137     float v = (float)t_on/(float)t_off;
138     return (v > 1.0f) ? (1.0f) : (v);
139 }
140
141 void pwmout_period(pwmout_t* obj, float seconds) {
142     pwmout_period_us(obj, seconds * 1000000.0f);
143 }
144
145 void pwmout_period_ms(pwmout_t* obj, int ms) {
146     pwmout_period_us(obj, ms * 1000);
147 }
148
149 // Set the PWM period, keeping the duty cycle the same.
150 void pwmout_period_us(pwmout_t* obj, int us) {
151     LPC_SCT0_Type* pwm = obj->pwm;
152     uint32_t t_off = pwm->MATCHREL0;
153     uint32_t t_on  = pwm->MATCHREL1;
154     float v = (float)t_on/(float)t_off;
155     pwm->MATCHREL0 = (uint32_t)us;
156     pwm->MATCHREL1 = (uint32_t)((float)us * (float)v);
157 }
158
159 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
160     pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
161 }
162
163 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
164     pwmout_pulsewidth_us(obj, ms * 1000);
165 }
166
167 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
168     obj->pwm->MATCHREL1 = (uint32_t)us;
169 }
170