mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c@234:37acebda271b, 2014-06-18 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 18 09:00:08 2014 +0100
- Revision:
- 234:37acebda271b
- Parent:
- 227:7bd0639b8911
- Child:
- 250:a49055e7a707
Synchronized with git revision 2a4bbdfe4f273f3b7371abaf0280a109cb27fb1f
Full URL: https://github.com/mbedmicro/mbed/commit/2a4bbdfe4f273f3b7371abaf0280a109cb27fb1f/
[LPC1549] USBDevice support
Who changed what in which revision?
User | Revision | Line number | New 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 | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
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 | 117:e0a7df0a9a56 | 33 | for (i=0; i<4; i++) { |
mbed_official | 112:a7168c414ef2 | 34 | if ((sct_used & (1 << i)) == 0) |
mbed_official | 112:a7168c414ef2 | 35 | return i; |
mbed_official | 112:a7168c414ef2 | 36 | } |
mbed_official | 112:a7168c414ef2 | 37 | return -1; |
mbed_official | 112:a7168c414ef2 | 38 | } |
mbed_official | 112:a7168c414ef2 | 39 | |
mbed_official | 112:a7168c414ef2 | 40 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 227:7bd0639b8911 | 41 | MBED_ASSERT(pin != (uint32_t)NC); |
mbed_official | 227:7bd0639b8911 | 42 | |
mbed_official | 112:a7168c414ef2 | 43 | int sct_n = get_available_sct(); |
mbed_official | 112:a7168c414ef2 | 44 | if (sct_n == -1) { |
mbed_official | 112:a7168c414ef2 | 45 | error("No available SCT"); |
mbed_official | 112:a7168c414ef2 | 46 | } |
mbed_official | 112:a7168c414ef2 | 47 | |
mbed_official | 112:a7168c414ef2 | 48 | sct_used |= (1 << sct_n); |
mbed_official | 112:a7168c414ef2 | 49 | obj->pwm = SCTs[sct_n]; |
mbed_official | 112:a7168c414ef2 | 50 | obj->pwm_ch = sct_n; |
mbed_official | 112:a7168c414ef2 | 51 | |
mbed_official | 112:a7168c414ef2 | 52 | LPC_SCT0_Type* pwm = obj->pwm; |
mbed_official | 112:a7168c414ef2 | 53 | |
mbed_official | 112:a7168c414ef2 | 54 | // Enable the SCT clock |
mbed_official | 112:a7168c414ef2 | 55 | LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << (obj->pwm_ch + 2)); |
mbed_official | 112:a7168c414ef2 | 56 | |
mbed_official | 112:a7168c414ef2 | 57 | // Clear peripheral reset the SCT: |
mbed_official | 112:a7168c414ef2 | 58 | LPC_SYSCON->PRESETCTRL1 |= (1 << (obj->pwm_ch + 2)); |
mbed_official | 112:a7168c414ef2 | 59 | LPC_SYSCON->PRESETCTRL1 &= ~(1 << (obj->pwm_ch + 2)); |
mbed_official | 112:a7168c414ef2 | 60 | |
mbed_official | 112:a7168c414ef2 | 61 | switch(obj->pwm_ch) { |
mbed_official | 117:e0a7df0a9a56 | 62 | case 0: |
mbed_official | 117:e0a7df0a9a56 | 63 | // SCT0_OUT0 |
mbed_official | 117:e0a7df0a9a56 | 64 | LPC_SWM->PINASSIGN[7] &= ~0x0000FF00; |
mbed_official | 117:e0a7df0a9a56 | 65 | LPC_SWM->PINASSIGN[7] |= (pin << 8); |
mbed_official | 117:e0a7df0a9a56 | 66 | break; |
mbed_official | 112:a7168c414ef2 | 67 | case 1: |
mbed_official | 112:a7168c414ef2 | 68 | // SCT1_OUT0 |
mbed_official | 112:a7168c414ef2 | 69 | LPC_SWM->PINASSIGN[8] &= ~0x000000FF; |
mbed_official | 112:a7168c414ef2 | 70 | LPC_SWM->PINASSIGN[8] |= (pin); |
mbed_official | 112:a7168c414ef2 | 71 | break; |
mbed_official | 112:a7168c414ef2 | 72 | case 2: |
mbed_official | 112:a7168c414ef2 | 73 | // SCT2_OUT0 |
mbed_official | 112:a7168c414ef2 | 74 | LPC_SWM->PINASSIGN[8] &= ~0xFF000000; |
mbed_official | 112:a7168c414ef2 | 75 | LPC_SWM->PINASSIGN[8] |= (pin << 24); |
mbed_official | 112:a7168c414ef2 | 76 | break; |
mbed_official | 112:a7168c414ef2 | 77 | case 3: |
mbed_official | 112:a7168c414ef2 | 78 | // SCT3_OUT0 |
mbed_official | 112:a7168c414ef2 | 79 | LPC_SWM->PINASSIGN[9] &= ~0x00FF0000; |
mbed_official | 112:a7168c414ef2 | 80 | LPC_SWM->PINASSIGN[9] |= (pin << 16); |
mbed_official | 112:a7168c414ef2 | 81 | break; |
mbed_official | 112:a7168c414ef2 | 82 | default: |
mbed_official | 112:a7168c414ef2 | 83 | break; |
mbed_official | 112:a7168c414ef2 | 84 | } |
mbed_official | 112:a7168c414ef2 | 85 | |
mbed_official | 112:a7168c414ef2 | 86 | // Two 16-bit counters, autolimit |
mbed_official | 112:a7168c414ef2 | 87 | pwm->CONFIG &= ~(0x1); |
mbed_official | 112:a7168c414ef2 | 88 | pwm->CONFIG |= (1 << 17); |
mbed_official | 112:a7168c414ef2 | 89 | |
mbed_official | 112:a7168c414ef2 | 90 | // halt and clear the counter |
mbed_official | 112:a7168c414ef2 | 91 | pwm->CTRL |= (1 << 2) | (1 << 3); |
mbed_official | 112:a7168c414ef2 | 92 | |
mbed_official | 112:a7168c414ef2 | 93 | // System Clock -> us_ticker (1)MHz |
mbed_official | 112:a7168c414ef2 | 94 | pwm->CTRL &= ~(0x7F << 5); |
mbed_official | 112:a7168c414ef2 | 95 | pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5); |
mbed_official | 112:a7168c414ef2 | 96 | |
mbed_official | 112:a7168c414ef2 | 97 | // Match reload register |
mbed_official | 112:a7168c414ef2 | 98 | pwm->MATCHREL0 = 20000; // 20ms |
mbed_official | 112:a7168c414ef2 | 99 | pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty |
mbed_official | 112:a7168c414ef2 | 100 | |
mbed_official | 112:a7168c414ef2 | 101 | pwm->OUT0_SET = (1 << 0); // event 0 |
mbed_official | 112:a7168c414ef2 | 102 | pwm->OUT0_CLR = (1 << 1); // event 1 |
mbed_official | 112:a7168c414ef2 | 103 | |
mbed_official | 112:a7168c414ef2 | 104 | pwm->EV0_CTRL = (1 << 12); |
mbed_official | 112:a7168c414ef2 | 105 | pwm->EV0_STATE = 0xFFFFFFFF; |
mbed_official | 112:a7168c414ef2 | 106 | pwm->EV1_CTRL = (1 << 12) | (1 << 0); |
mbed_official | 112:a7168c414ef2 | 107 | pwm->EV1_STATE = 0xFFFFFFFF; |
mbed_official | 112:a7168c414ef2 | 108 | |
mbed_official | 112:a7168c414ef2 | 109 | // unhalt the counter: |
mbed_official | 112:a7168c414ef2 | 110 | // - clearing bit 2 of the CTRL register |
mbed_official | 112:a7168c414ef2 | 111 | pwm->CTRL &= ~(1 << 2); |
mbed_official | 112:a7168c414ef2 | 112 | |
mbed_official | 112:a7168c414ef2 | 113 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
mbed_official | 112:a7168c414ef2 | 114 | pwmout_period_ms(obj, 20); |
mbed_official | 112:a7168c414ef2 | 115 | pwmout_write (obj, 0); |
mbed_official | 112:a7168c414ef2 | 116 | } |
mbed_official | 112:a7168c414ef2 | 117 | |
mbed_official | 112:a7168c414ef2 | 118 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 112:a7168c414ef2 | 119 | // Disable the SCT clock |
mbed_official | 112:a7168c414ef2 | 120 | LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1 << (obj->pwm_ch + 2)); |
mbed_official | 112:a7168c414ef2 | 121 | sct_used &= ~(1 << obj->pwm_ch); |
mbed_official | 112:a7168c414ef2 | 122 | } |
mbed_official | 112:a7168c414ef2 | 123 | |
mbed_official | 112:a7168c414ef2 | 124 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 112:a7168c414ef2 | 125 | LPC_SCT0_Type* pwm = obj->pwm; |
mbed_official | 112:a7168c414ef2 | 126 | if (value < 0.0f) { |
mbed_official | 112:a7168c414ef2 | 127 | value = 0.0; |
mbed_official | 112:a7168c414ef2 | 128 | } else if (value > 1.0f) { |
mbed_official | 112:a7168c414ef2 | 129 | value = 1.0; |
mbed_official | 112:a7168c414ef2 | 130 | } |
mbed_official | 112:a7168c414ef2 | 131 | uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0) * value); |
mbed_official | 112:a7168c414ef2 | 132 | pwm->MATCHREL1 = t_on; |
mbed_official | 112:a7168c414ef2 | 133 | } |
mbed_official | 112:a7168c414ef2 | 134 | |
mbed_official | 112:a7168c414ef2 | 135 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 112:a7168c414ef2 | 136 | uint32_t t_off = obj->pwm->MATCHREL0; |
mbed_official | 112:a7168c414ef2 | 137 | uint32_t t_on = obj->pwm->MATCHREL1; |
mbed_official | 112:a7168c414ef2 | 138 | float v = (float)t_on/(float)t_off; |
mbed_official | 112:a7168c414ef2 | 139 | return (v > 1.0f) ? (1.0f) : (v); |
mbed_official | 112:a7168c414ef2 | 140 | } |
mbed_official | 112:a7168c414ef2 | 141 | |
mbed_official | 112:a7168c414ef2 | 142 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 112:a7168c414ef2 | 143 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 112:a7168c414ef2 | 144 | } |
mbed_official | 112:a7168c414ef2 | 145 | |
mbed_official | 112:a7168c414ef2 | 146 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 112:a7168c414ef2 | 147 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 112:a7168c414ef2 | 148 | } |
mbed_official | 112:a7168c414ef2 | 149 | |
mbed_official | 112:a7168c414ef2 | 150 | // Set the PWM period, keeping the duty cycle the same. |
mbed_official | 112:a7168c414ef2 | 151 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 112:a7168c414ef2 | 152 | LPC_SCT0_Type* pwm = obj->pwm; |
mbed_official | 112:a7168c414ef2 | 153 | uint32_t t_off = pwm->MATCHREL0; |
mbed_official | 112:a7168c414ef2 | 154 | uint32_t t_on = pwm->MATCHREL1; |
mbed_official | 112:a7168c414ef2 | 155 | float v = (float)t_on/(float)t_off; |
mbed_official | 112:a7168c414ef2 | 156 | pwm->MATCHREL0 = (uint64_t)us; |
mbed_official | 112:a7168c414ef2 | 157 | pwm->MATCHREL1 = (uint64_t)((float)us * (float)v); |
mbed_official | 112:a7168c414ef2 | 158 | } |
mbed_official | 112:a7168c414ef2 | 159 | |
mbed_official | 112:a7168c414ef2 | 160 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 112:a7168c414ef2 | 161 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 112:a7168c414ef2 | 162 | } |
mbed_official | 112:a7168c414ef2 | 163 | |
mbed_official | 112:a7168c414ef2 | 164 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 112:a7168c414ef2 | 165 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 112:a7168c414ef2 | 166 | } |
mbed_official | 112:a7168c414ef2 | 167 | |
mbed_official | 112:a7168c414ef2 | 168 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 112:a7168c414ef2 | 169 | obj->pwm->MATCHREL1 = (uint64_t)us; |
mbed_official | 112:a7168c414ef2 | 170 | } |
mbed_official | 112:a7168c414ef2 | 171 |