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