mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16
elessair 0:f269e3021894 17 #include "pwmout_api.h"
elessair 0:f269e3021894 18 #include "cmsis.h"
elessair 0:f269e3021894 19 #include "pinmap.h"
elessair 0:f269e3021894 20 #include "mbed_error.h"
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #if DEVICE_PWMOUT
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 #define SCT_CHANNELS 2
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 static const PinMap PinMap_PWM[] = {
elessair 0:f269e3021894 27 {P1_19, SCT0_0, 2},
elessair 0:f269e3021894 28 {P2_2 , SCT0_1, 3},
elessair 0:f269e3021894 29 {P2_7 , SCT0_2, 2},
elessair 0:f269e3021894 30 {P1_13, SCT0_3, 2},
elessair 0:f269e3021894 31 {P2_16, SCT1_0, 1},
elessair 0:f269e3021894 32 {P2_17, SCT1_1, 1},
elessair 0:f269e3021894 33 {P2_18, SCT1_2, 1},
elessair 0:f269e3021894 34 {P2_19, SCT1_3, 1},
elessair 0:f269e3021894 35 {NC , NC ,0}
elessair 0:f269e3021894 36 };
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 static LPC_SCT0_Type *SCTs[SCT_CHANNELS] = {
elessair 0:f269e3021894 40 (LPC_SCT0_Type*)LPC_SCT0,
elessair 0:f269e3021894 41 (LPC_SCT0_Type*)LPC_SCT1,
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 };
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 // bit flags for used SCTs
elessair 0:f269e3021894 46 static unsigned char sct_used = 0;
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 static int get_available_sct(void) {
elessair 0:f269e3021894 49 int i;
elessair 0:f269e3021894 50 for (i=0; i<SCT_CHANNELS; i++) {
elessair 0:f269e3021894 51 if ((sct_used & (1 << i)) == 0)
elessair 0:f269e3021894 52 return i;
elessair 0:f269e3021894 53 }
elessair 0:f269e3021894 54 return -1;
elessair 0:f269e3021894 55 }
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 void pwmout_init(pwmout_t* obj, PinName pin) {
elessair 0:f269e3021894 58 // determine the SPI to use
elessair 0:f269e3021894 59 PWMName pwm_mapped = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
elessair 0:f269e3021894 60 if (pwm_mapped == (PWMName)NC) {
elessair 0:f269e3021894 61 error("PwmOut pin mapping failed");
elessair 0:f269e3021894 62 }
elessair 0:f269e3021894 63 int sct_n = get_available_sct();
elessair 0:f269e3021894 64 if (sct_n == -1) {
elessair 0:f269e3021894 65 error("No available SCT");
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 sct_used |= (1 << sct_n);
elessair 0:f269e3021894 69 obj->pwm = SCTs[sct_n];
elessair 0:f269e3021894 70 obj->pwm_ch = sct_n;
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 // Enable the SCT clock
elessair 0:f269e3021894 73 LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 31);
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 // Clear peripheral reset the SCT:
elessair 0:f269e3021894 76 LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
elessair 0:f269e3021894 77 pinmap_pinout(pin, PinMap_PWM);
elessair 0:f269e3021894 78 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 79
elessair 0:f269e3021894 80 // Unified 32-bit counter, autolimit
elessair 0:f269e3021894 81 pwm->CONFIG |= ((0x3 << 17) | 0x01);
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 // halt and clear the counter
elessair 0:f269e3021894 84 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 switch(pwm_mapped) {
elessair 0:f269e3021894 87 case SCT0_0:
elessair 0:f269e3021894 88 case SCT1_0:
elessair 0:f269e3021894 89 pwm->OUT0_SET = (1 << 0); // event 0
elessair 0:f269e3021894 90 pwm->OUT0_CLR = (1 << 1); // event 1
elessair 0:f269e3021894 91 break;
elessair 0:f269e3021894 92 case SCT0_1:
elessair 0:f269e3021894 93 case SCT1_1:
elessair 0:f269e3021894 94 pwm->OUT1_SET = (1 << 0); // event 0
elessair 0:f269e3021894 95 pwm->OUT1_CLR = (1 << 1); // event 1
elessair 0:f269e3021894 96 break;
elessair 0:f269e3021894 97 case SCT0_2:
elessair 0:f269e3021894 98 case SCT1_2:
elessair 0:f269e3021894 99 pwm->OUT2_SET = (1 << 0); // event 0
elessair 0:f269e3021894 100 pwm->OUT2_CLR = (1 << 1); // event 1
elessair 0:f269e3021894 101 break;
elessair 0:f269e3021894 102 case SCT0_3:
elessair 0:f269e3021894 103 case SCT1_3:
elessair 0:f269e3021894 104 pwm->OUT3_SET = (1 << 0); // event 0
elessair 0:f269e3021894 105 pwm->OUT3_CLR = (1 << 1); // event 1
elessair 0:f269e3021894 106 break;
elessair 0:f269e3021894 107 default:
elessair 0:f269e3021894 108 break;
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110 // Event 0 : MATCH and MATCHSEL=0
elessair 0:f269e3021894 111 pwm->EV0_CTRL = (1 << 12);
elessair 0:f269e3021894 112 pwm->EV0_STATE = 0xFFFFFFFF;
elessair 0:f269e3021894 113 // Event 1 : MATCH and MATCHSEL=1
elessair 0:f269e3021894 114 pwm->EV1_CTRL = (1 << 12) | (1 << 0);
elessair 0:f269e3021894 115 pwm->EV1_STATE = 0xFFFFFFFF;
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 // default to 20ms: standard for servos, and fine for e.g. brightness control
elessair 0:f269e3021894 118 pwmout_period_ms(obj, 20);
elessair 0:f269e3021894 119 pwmout_write (obj, 0);
elessair 0:f269e3021894 120 }
elessair 0:f269e3021894 121
elessair 0:f269e3021894 122 void pwmout_free(pwmout_t* obj) {
elessair 0:f269e3021894 123 sct_used &= ~(1 << obj->pwm_ch);
elessair 0:f269e3021894 124 if (sct_used == 0) {
elessair 0:f269e3021894 125 // Disable the SCT clock
elessair 0:f269e3021894 126 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1UL << 31);
elessair 0:f269e3021894 127 }
elessair 0:f269e3021894 128 }
elessair 0:f269e3021894 129
elessair 0:f269e3021894 130 void pwmout_write(pwmout_t* obj, float value) {
elessair 0:f269e3021894 131 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 132 if (value < 0.0f) {
elessair 0:f269e3021894 133 value = 0.0;
elessair 0:f269e3021894 134 } else if (value > 1.0f) {
elessair 0:f269e3021894 135 value = 1.0;
elessair 0:f269e3021894 136 }
elessair 0:f269e3021894 137 uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
elessair 0:f269e3021894 138 if (t_on > 0) {
elessair 0:f269e3021894 139 pwm->MATCHREL1 = t_on - 1;
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
elessair 0:f269e3021894 142 if (pwm->CTRL & (1 << 2)) {
elessair 0:f269e3021894 143 pwm->MATCH1 = pwm->MATCHREL1;
elessair 0:f269e3021894 144 pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 145 }
elessair 0:f269e3021894 146 } else {
elessair 0:f269e3021894 147 // Halt the timer and force the output low
elessair 0:f269e3021894 148 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 149 pwm->OUTPUT = 0x00000000;
elessair 0:f269e3021894 150 }
elessair 0:f269e3021894 151 }
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 float pwmout_read(pwmout_t* obj) {
elessair 0:f269e3021894 154 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 155 uint32_t t_off = pwm->MATCHREL0 + 1;
elessair 0:f269e3021894 156 uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
elessair 0:f269e3021894 157 float v = (float)t_on/(float)t_off;
elessair 0:f269e3021894 158 return (v > 1.0f) ? (1.0f) : (v);
elessair 0:f269e3021894 159 }
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 void pwmout_period(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 162 pwmout_period_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 163 }
elessair 0:f269e3021894 164
elessair 0:f269e3021894 165 void pwmout_period_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 166 pwmout_period_us(obj, ms * 1000);
elessair 0:f269e3021894 167 }
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 // Set the PWM period, keeping the duty cycle the same.
elessair 0:f269e3021894 170 void pwmout_period_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 171 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 172 uint32_t t_off = pwm->MATCHREL0 + 1;
elessair 0:f269e3021894 173 uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
elessair 0:f269e3021894 174 float v = (float)t_on/(float)t_off;
elessair 0:f269e3021894 175 uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
elessair 0:f269e3021894 176 uint32_t pulsewidth_ticks = period_ticks * v;
elessair 0:f269e3021894 177 pwm->MATCHREL0 = period_ticks - 1;
elessair 0:f269e3021894 178 if (pulsewidth_ticks > 0) {
elessair 0:f269e3021894 179 pwm->MATCHREL1 = pulsewidth_ticks - 1;
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 // Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
elessair 0:f269e3021894 182 if (pwm->CTRL & (1 << 2)) {
elessair 0:f269e3021894 183 pwm->MATCH0 = pwm->MATCHREL0;
elessair 0:f269e3021894 184 pwm->MATCH1 = pwm->MATCHREL1;
elessair 0:f269e3021894 185 pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 186 }
elessair 0:f269e3021894 187 } else {
elessair 0:f269e3021894 188 // Halt the timer and force the output low
elessair 0:f269e3021894 189 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 190 pwm->OUTPUT = 0x00000000;
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 // Ensure the new period will take immediate effect when the timer is un-halted
elessair 0:f269e3021894 193 pwm->MATCH0 = pwm->MATCHREL0;
elessair 0:f269e3021894 194 }
elessair 0:f269e3021894 195 }
elessair 0:f269e3021894 196
elessair 0:f269e3021894 197 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 198 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 199 }
elessair 0:f269e3021894 200
elessair 0:f269e3021894 201 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 202 pwmout_pulsewidth_us(obj, ms * 1000);
elessair 0:f269e3021894 203 }
elessair 0:f269e3021894 204
elessair 0:f269e3021894 205 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 206 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 207 if (us > 0) {
elessair 0:f269e3021894 208 pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
elessair 0:f269e3021894 209
elessair 0:f269e3021894 210 // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
elessair 0:f269e3021894 211 if (pwm->CTRL & (1 << 2)) {
elessair 0:f269e3021894 212 pwm->MATCH1 = pwm->MATCHREL1;
elessair 0:f269e3021894 213 pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 214 }
elessair 0:f269e3021894 215 } else {
elessair 0:f269e3021894 216 // Halt the timer and force the output low
elessair 0:f269e3021894 217 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 218 pwm->OUTPUT = 0x00000000;
elessair 0:f269e3021894 219 }
elessair 0:f269e3021894 220 }
elessair 0:f269e3021894 221
elessair 0:f269e3021894 222 #endif