mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Mar 07 16:00:07 2014 +0000
Revision:
112:a7168c414ef2
Child:
117:e0a7df0a9a56
Synchronized with git revision 50b949b631f36d800e0008780cf33062071b4fa3

Full URL: https://github.com/mbedmicro/mbed/commit/50b949b631f36d800e0008780cf33062071b4fa3/

Protection against concurrent access to USBHost

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 112:a7168c414ef2 1 /* mbed Microcontroller Library
mbed_official 112:a7168c414ef2 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 112:a7168c414ef2 3 *
mbed_official 112:a7168c414ef2 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 112:a7168c414ef2 5 * you may not use this file except in compliance with the License.
mbed_official 112:a7168c414ef2 6 * You may obtain a copy of the License at
mbed_official 112:a7168c414ef2 7 *
mbed_official 112:a7168c414ef2 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 112:a7168c414ef2 9 *
mbed_official 112:a7168c414ef2 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 112:a7168c414ef2 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 112:a7168c414ef2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 112:a7168c414ef2 13 * See the License for the specific language governing permissions and
mbed_official 112:a7168c414ef2 14 * limitations under the License.
mbed_official 112:a7168c414ef2 15 */
mbed_official 112:a7168c414ef2 16
mbed_official 112:a7168c414ef2 17 #include "pwmout_api.h"
mbed_official 112:a7168c414ef2 18 #include "cmsis.h"
mbed_official 112:a7168c414ef2 19 #include "pinmap.h"
mbed_official 112:a7168c414ef2 20 #include "error.h"
mbed_official 112:a7168c414ef2 21
mbed_official 112:a7168c414ef2 22 static LPC_SCT0_Type *SCTs[4] = {
mbed_official 112:a7168c414ef2 23 (LPC_SCT0_Type*)LPC_SCT0,
mbed_official 112:a7168c414ef2 24 (LPC_SCT0_Type*)LPC_SCT1,
mbed_official 112:a7168c414ef2 25 (LPC_SCT0_Type*)LPC_SCT2,
mbed_official 112:a7168c414ef2 26 (LPC_SCT0_Type*)LPC_SCT3,
mbed_official 112:a7168c414ef2 27 };
mbed_official 112:a7168c414ef2 28
mbed_official 112:a7168c414ef2 29 // bit flags for used SCTs
mbed_official 112:a7168c414ef2 30 static unsigned char sct_used = 0;
mbed_official 112:a7168c414ef2 31 static int get_available_sct(void) {
mbed_official 112:a7168c414ef2 32 int i;
mbed_official 112:a7168c414ef2 33 // start from 1, since 0 is used by ticker at the moment
mbed_official 112:a7168c414ef2 34 for (i=1; i<4; i++) {
mbed_official 112:a7168c414ef2 35 if ((sct_used & (1 << i)) == 0)
mbed_official 112:a7168c414ef2 36 return i;
mbed_official 112:a7168c414ef2 37 }
mbed_official 112:a7168c414ef2 38 return -1;
mbed_official 112:a7168c414ef2 39 }
mbed_official 112:a7168c414ef2 40
mbed_official 112:a7168c414ef2 41 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 112:a7168c414ef2 42 if (pin == (uint32_t)NC)
mbed_official 112:a7168c414ef2 43 error("PwmOut pin mapping failed");
mbed_official 112:a7168c414ef2 44
mbed_official 112:a7168c414ef2 45 int sct_n = get_available_sct();
mbed_official 112:a7168c414ef2 46 if (sct_n == -1) {
mbed_official 112:a7168c414ef2 47 error("No available SCT");
mbed_official 112:a7168c414ef2 48 }
mbed_official 112:a7168c414ef2 49
mbed_official 112:a7168c414ef2 50 sct_used |= (1 << sct_n);
mbed_official 112:a7168c414ef2 51 obj->pwm = SCTs[sct_n];
mbed_official 112:a7168c414ef2 52 obj->pwm_ch = sct_n;
mbed_official 112:a7168c414ef2 53
mbed_official 112:a7168c414ef2 54 LPC_SCT0_Type* pwm = obj->pwm;
mbed_official 112:a7168c414ef2 55
mbed_official 112:a7168c414ef2 56 // Enable the SCT clock
mbed_official 112:a7168c414ef2 57 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << (obj->pwm_ch + 2));
mbed_official 112:a7168c414ef2 58
mbed_official 112:a7168c414ef2 59 // Clear peripheral reset the SCT:
mbed_official 112:a7168c414ef2 60 LPC_SYSCON->PRESETCTRL1 |= (1 << (obj->pwm_ch + 2));
mbed_official 112:a7168c414ef2 61 LPC_SYSCON->PRESETCTRL1 &= ~(1 << (obj->pwm_ch + 2));
mbed_official 112:a7168c414ef2 62
mbed_official 112:a7168c414ef2 63 switch(obj->pwm_ch) {
mbed_official 112:a7168c414ef2 64 case 1:
mbed_official 112:a7168c414ef2 65 // SCT1_OUT0
mbed_official 112:a7168c414ef2 66 LPC_SWM->PINASSIGN[8] &= ~0x000000FF;
mbed_official 112:a7168c414ef2 67 LPC_SWM->PINASSIGN[8] |= (pin);
mbed_official 112:a7168c414ef2 68 break;
mbed_official 112:a7168c414ef2 69 case 2:
mbed_official 112:a7168c414ef2 70 // SCT2_OUT0
mbed_official 112:a7168c414ef2 71 LPC_SWM->PINASSIGN[8] &= ~0xFF000000;
mbed_official 112:a7168c414ef2 72 LPC_SWM->PINASSIGN[8] |= (pin << 24);
mbed_official 112:a7168c414ef2 73 break;
mbed_official 112:a7168c414ef2 74 case 3:
mbed_official 112:a7168c414ef2 75 // SCT3_OUT0
mbed_official 112:a7168c414ef2 76 LPC_SWM->PINASSIGN[9] &= ~0x00FF0000;
mbed_official 112:a7168c414ef2 77 LPC_SWM->PINASSIGN[9] |= (pin << 16);
mbed_official 112:a7168c414ef2 78 break;
mbed_official 112:a7168c414ef2 79 default:
mbed_official 112:a7168c414ef2 80 break;
mbed_official 112:a7168c414ef2 81 }
mbed_official 112:a7168c414ef2 82
mbed_official 112:a7168c414ef2 83 // Two 16-bit counters, autolimit
mbed_official 112:a7168c414ef2 84 pwm->CONFIG &= ~(0x1);
mbed_official 112:a7168c414ef2 85 pwm->CONFIG |= (1 << 17);
mbed_official 112:a7168c414ef2 86
mbed_official 112:a7168c414ef2 87 // halt and clear the counter
mbed_official 112:a7168c414ef2 88 pwm->CTRL |= (1 << 2) | (1 << 3);
mbed_official 112:a7168c414ef2 89
mbed_official 112:a7168c414ef2 90 // System Clock -> us_ticker (1)MHz
mbed_official 112:a7168c414ef2 91 pwm->CTRL &= ~(0x7F << 5);
mbed_official 112:a7168c414ef2 92 pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
mbed_official 112:a7168c414ef2 93
mbed_official 112:a7168c414ef2 94 // Match reload register
mbed_official 112:a7168c414ef2 95 pwm->MATCHREL0 = 20000; // 20ms
mbed_official 112:a7168c414ef2 96 pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty
mbed_official 112:a7168c414ef2 97
mbed_official 112:a7168c414ef2 98 pwm->OUT0_SET = (1 << 0); // event 0
mbed_official 112:a7168c414ef2 99 pwm->OUT0_CLR = (1 << 1); // event 1
mbed_official 112:a7168c414ef2 100
mbed_official 112:a7168c414ef2 101 pwm->EV0_CTRL = (1 << 12);
mbed_official 112:a7168c414ef2 102 pwm->EV0_STATE = 0xFFFFFFFF;
mbed_official 112:a7168c414ef2 103 pwm->EV1_CTRL = (1 << 12) | (1 << 0);
mbed_official 112:a7168c414ef2 104 pwm->EV1_STATE = 0xFFFFFFFF;
mbed_official 112:a7168c414ef2 105
mbed_official 112:a7168c414ef2 106 // unhalt the counter:
mbed_official 112:a7168c414ef2 107 // - clearing bit 2 of the CTRL register
mbed_official 112:a7168c414ef2 108 pwm->CTRL &= ~(1 << 2);
mbed_official 112:a7168c414ef2 109
mbed_official 112:a7168c414ef2 110 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 112:a7168c414ef2 111 pwmout_period_ms(obj, 20);
mbed_official 112:a7168c414ef2 112 pwmout_write (obj, 0);
mbed_official 112:a7168c414ef2 113 }
mbed_official 112:a7168c414ef2 114
mbed_official 112:a7168c414ef2 115 void pwmout_free(pwmout_t* obj) {
mbed_official 112:a7168c414ef2 116 // Disable the SCT clock
mbed_official 112:a7168c414ef2 117 LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1 << (obj->pwm_ch + 2));
mbed_official 112:a7168c414ef2 118 sct_used &= ~(1 << obj->pwm_ch);
mbed_official 112:a7168c414ef2 119 }
mbed_official 112:a7168c414ef2 120
mbed_official 112:a7168c414ef2 121 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 112:a7168c414ef2 122 LPC_SCT0_Type* pwm = obj->pwm;
mbed_official 112:a7168c414ef2 123 if (value < 0.0f) {
mbed_official 112:a7168c414ef2 124 value = 0.0;
mbed_official 112:a7168c414ef2 125 } else if (value > 1.0f) {
mbed_official 112:a7168c414ef2 126 value = 1.0;
mbed_official 112:a7168c414ef2 127 }
mbed_official 112:a7168c414ef2 128 uint32_t t_off = pwm->MATCHREL0 - (uint32_t)((float)(pwm->MATCHREL0) * value);
mbed_official 112:a7168c414ef2 129 uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0) * value);
mbed_official 112:a7168c414ef2 130 pwm->MATCHREL1 = t_on;
mbed_official 112:a7168c414ef2 131 }
mbed_official 112:a7168c414ef2 132
mbed_official 112:a7168c414ef2 133 float pwmout_read(pwmout_t* obj) {
mbed_official 112:a7168c414ef2 134 uint32_t t_off = obj->pwm->MATCHREL0;
mbed_official 112:a7168c414ef2 135 uint32_t t_on = obj->pwm->MATCHREL1;
mbed_official 112:a7168c414ef2 136 float v = (float)t_on/(float)t_off;
mbed_official 112:a7168c414ef2 137 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 112:a7168c414ef2 138 }
mbed_official 112:a7168c414ef2 139
mbed_official 112:a7168c414ef2 140 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 112:a7168c414ef2 141 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 112:a7168c414ef2 142 }
mbed_official 112:a7168c414ef2 143
mbed_official 112:a7168c414ef2 144 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 112:a7168c414ef2 145 pwmout_period_us(obj, ms * 1000);
mbed_official 112:a7168c414ef2 146 }
mbed_official 112:a7168c414ef2 147
mbed_official 112:a7168c414ef2 148 // Set the PWM period, keeping the duty cycle the same.
mbed_official 112:a7168c414ef2 149 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 112:a7168c414ef2 150 LPC_SCT0_Type* pwm = obj->pwm;
mbed_official 112:a7168c414ef2 151 uint32_t t_off = pwm->MATCHREL0;
mbed_official 112:a7168c414ef2 152 uint32_t t_on = pwm->MATCHREL1;
mbed_official 112:a7168c414ef2 153 float v = (float)t_on/(float)t_off;
mbed_official 112:a7168c414ef2 154 pwm->MATCHREL0 = (uint64_t)us;
mbed_official 112:a7168c414ef2 155 pwm->MATCHREL1 = (uint64_t)((float)us * (float)v);
mbed_official 112:a7168c414ef2 156 }
mbed_official 112:a7168c414ef2 157
mbed_official 112:a7168c414ef2 158 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 112:a7168c414ef2 159 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 112:a7168c414ef2 160 }
mbed_official 112:a7168c414ef2 161
mbed_official 112:a7168c414ef2 162 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 112:a7168c414ef2 163 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 112:a7168c414ef2 164 }
mbed_official 112:a7168c414ef2 165
mbed_official 112:a7168c414ef2 166 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 112:a7168c414ef2 167 obj->pwm->MATCHREL1 = (uint64_t)us;
mbed_official 112:a7168c414ef2 168 }
mbed_official 112:a7168c414ef2 169