]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c
remove experimental return, cleanup slash_question key
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11U6X / 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
17 #include "pwmout_api.h"
18 #include "cmsis.h"
19 #include "pinmap.h"
20 #include "mbed_error.h"
21
22 #if DEVICE_PWMOUT
23
24 #define SCT_CHANNELS 2
25
26 static const PinMap PinMap_PWM[] = {
27 {P1_19, SCT0_0, 2},
28 {P2_2 , SCT0_1, 3},
29 {P2_7 , SCT0_2, 2},
30 {P1_13, SCT0_3, 2},
31 {P2_16, SCT1_0, 1},
32 {P2_17, SCT1_1, 1},
33 {P2_18, SCT1_2, 1},
34 {P2_19, SCT1_3, 1},
35 {NC , NC ,0}
36 };
37
38
39 static LPC_SCT0_Type *SCTs[SCT_CHANNELS] = {
40 (LPC_SCT0_Type*)LPC_SCT0,
41 (LPC_SCT0_Type*)LPC_SCT1,
42
43 };
44
45 // bit flags for used SCTs
46 static unsigned char sct_used = 0;
47
48 static int get_available_sct(void) {
49 int i;
50 for (i=0; i<SCT_CHANNELS; i++) {
51 if ((sct_used & (1 << i)) == 0)
52 return i;
53 }
54 return -1;
55 }
56
57 void pwmout_init(pwmout_t* obj, PinName pin) {
58 // determine the SPI to use
59 PWMName pwm_mapped = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
60 if (pwm_mapped == (PWMName)NC) {
61 error("PwmOut pin mapping failed");
62 }
63 int sct_n = get_available_sct();
64 if (sct_n == -1) {
65 error("No available SCT");
66 }
67
68 sct_used |= (1 << sct_n);
69 obj->pwm = SCTs[sct_n];
70 obj->pwm_ch = sct_n;
71
72 // Enable the SCT clock
73 LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 31);
74
75 // Clear peripheral reset the SCT:
76 LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
77 pinmap_pinout(pin, PinMap_PWM);
78 LPC_SCT0_Type* pwm = obj->pwm;
79
80 // Unified 32-bit counter, autolimit
81 pwm->CONFIG |= ((0x3 << 17) | 0x01);
82
83 // halt and clear the counter
84 pwm->CTRL |= (1 << 2) | (1 << 3);
85
86 // System Clock -> us_ticker (1)MHz
87 pwm->CTRL &= ~(0x7F << 5);
88 pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
89
90 switch(pwm_mapped) {
91 case SCT0_0:
92 case SCT1_0:
93 pwm->OUT0_SET = (1 << 0); // event 0
94 pwm->OUT0_CLR = (1 << 1); // event 1
95 break;
96 case SCT0_1:
97 case SCT1_1:
98 pwm->OUT1_SET = (1 << 0); // event 0
99 pwm->OUT1_CLR = (1 << 1); // event 1
100 break;
101 case SCT0_2:
102 case SCT1_2:
103 pwm->OUT2_SET = (1 << 0); // event 0
104 pwm->OUT2_CLR = (1 << 1); // event 1
105 break;
106 case SCT0_3:
107 case SCT1_3:
108 pwm->OUT3_SET = (1 << 0); // event 0
109 pwm->OUT3_CLR = (1 << 1); // event 1
110 break;
111 default:
112 break;
113 }
114 // Event 0 : MATCH and MATCHSEL=0
115 pwm->EV0_CTRL = (1 << 12);
116 pwm->EV0_STATE = 0xFFFFFFFF;
117 // Event 1 : MATCH and MATCHSEL=1
118 pwm->EV1_CTRL = (1 << 12) | (1 << 0);
119 pwm->EV1_STATE = 0xFFFFFFFF;
120
121 // Match reload register
122 pwm->MATCHREL0 = 20000; // 20ms
123 pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty
124
125 // unhalt the counter:
126 // - clearing bit 2 of the CTRL register
127 pwm->CTRL &= ~(1 << 2);
128
129 // default to 20ms: standard for servos, and fine for e.g. brightness control
130 pwmout_period_ms(obj, 20);
131 pwmout_write (obj, 0);
132 }
133
134 void pwmout_free(pwmout_t* obj) {
135 sct_used &= ~(1 << obj->pwm_ch);
136 if (sct_used == 0) {
137 // Disable the SCT clock
138 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1UL << 31);
139 }
140 }
141
142 void pwmout_write(pwmout_t* obj, float value) {
143 if (value < 0.0f) {
144 value = 0.0;
145 } else if (value > 1.0f) {
146 value = 1.0;
147 }
148 uint32_t t_on = (uint32_t)((float)(obj->pwm->MATCHREL0) * value);
149 obj->pwm->MATCHREL1 = t_on;
150 }
151
152 float pwmout_read(pwmout_t* obj) {
153 uint32_t t_off = obj->pwm->MATCHREL0;
154 uint32_t t_on = obj->pwm->MATCHREL1;
155 float v = (float)t_on/(float)t_off;
156 return (v > 1.0f) ? (1.0f) : (v);
157 }
158
159 void pwmout_period(pwmout_t* obj, float seconds) {
160 pwmout_period_us(obj, seconds * 1000000.0f);
161 }
162
163 void pwmout_period_ms(pwmout_t* obj, int ms) {
164 pwmout_period_us(obj, ms * 1000);
165 }
166
167 // Set the PWM period, keeping the duty cycle the same.
168 void pwmout_period_us(pwmout_t* obj, int us) {
169 uint32_t t_off = obj->pwm->MATCHREL0;
170 uint32_t t_on = obj->pwm->MATCHREL1;
171 float v = (float)t_on/(float)t_off;
172 obj->pwm->MATCHREL0 = (uint32_t)us;
173 obj->pwm->MATCHREL1 = (uint32_t)((float)us * (float)v);
174 }
175
176 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
177 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
178 }
179
180 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
181 pwmout_pulsewidth_us(obj, ms * 1000);
182 }
183
184 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
185 obj->pwm->MATCHREL1 = (uint32_t)us;
186 }
187
188 #endif
Imprint / Impressum