mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC11U6X/pwmout_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 285:31249416b6f9
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 193:cff126de7aaf | 1 | /* mbed Microcontroller Library |
mbed_official | 193:cff126de7aaf | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 193:cff126de7aaf | 3 | * |
mbed_official | 193:cff126de7aaf | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 193:cff126de7aaf | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 193:cff126de7aaf | 6 | * You may obtain a copy of the License at |
mbed_official | 193:cff126de7aaf | 7 | * |
mbed_official | 193:cff126de7aaf | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 193:cff126de7aaf | 9 | * |
mbed_official | 193:cff126de7aaf | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 193:cff126de7aaf | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 193:cff126de7aaf | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 193:cff126de7aaf | 13 | * See the License for the specific language governing permissions and |
mbed_official | 193:cff126de7aaf | 14 | * limitations under the License. |
mbed_official | 193:cff126de7aaf | 15 | */ |
mbed_official | 193:cff126de7aaf | 16 | |
mbed_official | 193:cff126de7aaf | 17 | #include "pwmout_api.h" |
mbed_official | 193:cff126de7aaf | 18 | #include "cmsis.h" |
mbed_official | 193:cff126de7aaf | 19 | #include "pinmap.h" |
mbed_official | 285:31249416b6f9 | 20 | #include "mbed_error.h" |
mbed_official | 193:cff126de7aaf | 21 | |
mbed_official | 193:cff126de7aaf | 22 | #if DEVICE_PWMOUT |
mbed_official | 193:cff126de7aaf | 23 | |
mbed_official | 193:cff126de7aaf | 24 | #define SCT_CHANNELS 2 |
mbed_official | 193:cff126de7aaf | 25 | |
mbed_official | 193:cff126de7aaf | 26 | static const PinMap PinMap_PWM[] = { |
mbed_official | 193:cff126de7aaf | 27 | {P1_19, SCT0_0, 2}, |
mbed_official | 193:cff126de7aaf | 28 | {P2_2 , SCT0_1, 3}, |
mbed_official | 193:cff126de7aaf | 29 | {P2_7 , SCT0_2, 2}, |
mbed_official | 193:cff126de7aaf | 30 | {P1_13, SCT0_3, 2}, |
mbed_official | 193:cff126de7aaf | 31 | {P2_16, SCT1_0, 1}, |
mbed_official | 193:cff126de7aaf | 32 | {P2_17, SCT1_1, 1}, |
mbed_official | 193:cff126de7aaf | 33 | {P2_18, SCT1_2, 1}, |
mbed_official | 193:cff126de7aaf | 34 | {P2_19, SCT1_3, 1}, |
mbed_official | 193:cff126de7aaf | 35 | {NC , NC ,0} |
mbed_official | 193:cff126de7aaf | 36 | }; |
mbed_official | 193:cff126de7aaf | 37 | |
mbed_official | 193:cff126de7aaf | 38 | |
mbed_official | 193:cff126de7aaf | 39 | static LPC_SCT0_Type *SCTs[SCT_CHANNELS] = { |
mbed_official | 193:cff126de7aaf | 40 | (LPC_SCT0_Type*)LPC_SCT0, |
mbed_official | 193:cff126de7aaf | 41 | (LPC_SCT0_Type*)LPC_SCT1, |
mbed_official | 193:cff126de7aaf | 42 | |
mbed_official | 193:cff126de7aaf | 43 | }; |
mbed_official | 193:cff126de7aaf | 44 | |
mbed_official | 193:cff126de7aaf | 45 | // bit flags for used SCTs |
mbed_official | 193:cff126de7aaf | 46 | static unsigned char sct_used = 0; |
mbed_official | 193:cff126de7aaf | 47 | |
mbed_official | 193:cff126de7aaf | 48 | static int get_available_sct(void) { |
mbed_official | 193:cff126de7aaf | 49 | int i; |
mbed_official | 193:cff126de7aaf | 50 | for (i=0; i<SCT_CHANNELS; i++) { |
mbed_official | 193:cff126de7aaf | 51 | if ((sct_used & (1 << i)) == 0) |
mbed_official | 193:cff126de7aaf | 52 | return i; |
mbed_official | 193:cff126de7aaf | 53 | } |
mbed_official | 193:cff126de7aaf | 54 | return -1; |
mbed_official | 193:cff126de7aaf | 55 | } |
mbed_official | 193:cff126de7aaf | 56 | |
mbed_official | 193:cff126de7aaf | 57 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 193:cff126de7aaf | 58 | // determine the SPI to use |
mbed_official | 193:cff126de7aaf | 59 | PWMName pwm_mapped = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 234:37acebda271b | 60 | if (pwm_mapped == (PWMName)NC) { |
mbed_official | 193:cff126de7aaf | 61 | error("PwmOut pin mapping failed"); |
mbed_official | 193:cff126de7aaf | 62 | } |
mbed_official | 193:cff126de7aaf | 63 | int sct_n = get_available_sct(); |
mbed_official | 193:cff126de7aaf | 64 | if (sct_n == -1) { |
mbed_official | 193:cff126de7aaf | 65 | error("No available SCT"); |
mbed_official | 193:cff126de7aaf | 66 | } |
mbed_official | 193:cff126de7aaf | 67 | |
mbed_official | 193:cff126de7aaf | 68 | sct_used |= (1 << sct_n); |
mbed_official | 193:cff126de7aaf | 69 | obj->pwm = SCTs[sct_n]; |
mbed_official | 193:cff126de7aaf | 70 | obj->pwm_ch = sct_n; |
mbed_official | 193:cff126de7aaf | 71 | |
mbed_official | 193:cff126de7aaf | 72 | // Enable the SCT clock |
mbed_official | 193:cff126de7aaf | 73 | LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 31); |
mbed_official | 193:cff126de7aaf | 74 | |
mbed_official | 193:cff126de7aaf | 75 | // Clear peripheral reset the SCT: |
mbed_official | 193:cff126de7aaf | 76 | LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9)); |
mbed_official | 193:cff126de7aaf | 77 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 193:cff126de7aaf | 78 | LPC_SCT0_Type* pwm = obj->pwm; |
mbed_official | 193:cff126de7aaf | 79 | |
mbed_official | 193:cff126de7aaf | 80 | // Two 16-bit counters, autolimit |
mbed_official | 193:cff126de7aaf | 81 | pwm->CONFIG &= ~(0x1); |
mbed_official | 193:cff126de7aaf | 82 | pwm->CONFIG |= (1 << 17); |
mbed_official | 193:cff126de7aaf | 83 | |
mbed_official | 193:cff126de7aaf | 84 | // halt and clear the counter |
mbed_official | 193:cff126de7aaf | 85 | pwm->CTRL |= (1 << 2) | (1 << 3); |
mbed_official | 193:cff126de7aaf | 86 | |
mbed_official | 193:cff126de7aaf | 87 | // System Clock -> us_ticker (1)MHz |
mbed_official | 193:cff126de7aaf | 88 | pwm->CTRL &= ~(0x7F << 5); |
mbed_official | 193:cff126de7aaf | 89 | pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5); |
mbed_official | 193:cff126de7aaf | 90 | |
mbed_official | 193:cff126de7aaf | 91 | switch(pwm_mapped) { |
mbed_official | 193:cff126de7aaf | 92 | case SCT0_0: |
mbed_official | 193:cff126de7aaf | 93 | case SCT1_0: |
mbed_official | 193:cff126de7aaf | 94 | pwm->OUT0_SET = (1 << 0); // event 0 |
mbed_official | 193:cff126de7aaf | 95 | pwm->OUT0_CLR = (1 << 1); // event 1 |
mbed_official | 193:cff126de7aaf | 96 | break; |
mbed_official | 193:cff126de7aaf | 97 | case SCT0_1: |
mbed_official | 193:cff126de7aaf | 98 | case SCT1_1: |
mbed_official | 193:cff126de7aaf | 99 | pwm->OUT1_SET = (1 << 0); // event 0 |
mbed_official | 193:cff126de7aaf | 100 | pwm->OUT1_CLR = (1 << 1); // event 1 |
mbed_official | 193:cff126de7aaf | 101 | break; |
mbed_official | 193:cff126de7aaf | 102 | case SCT0_2: |
mbed_official | 193:cff126de7aaf | 103 | case SCT1_2: |
mbed_official | 193:cff126de7aaf | 104 | pwm->OUT2_SET = (1 << 0); // event 0 |
mbed_official | 193:cff126de7aaf | 105 | pwm->OUT2_CLR = (1 << 1); // event 1 |
mbed_official | 193:cff126de7aaf | 106 | break; |
mbed_official | 193:cff126de7aaf | 107 | case SCT0_3: |
mbed_official | 193:cff126de7aaf | 108 | case SCT1_3: |
mbed_official | 193:cff126de7aaf | 109 | pwm->OUT3_SET = (1 << 0); // event 0 |
mbed_official | 193:cff126de7aaf | 110 | pwm->OUT3_CLR = (1 << 1); // event 1 |
mbed_official | 193:cff126de7aaf | 111 | break; |
mbed_official | 193:cff126de7aaf | 112 | default: |
mbed_official | 193:cff126de7aaf | 113 | break; |
mbed_official | 193:cff126de7aaf | 114 | } |
mbed_official | 193:cff126de7aaf | 115 | // Event 0 : MATCH and MATCHSEL=0 |
mbed_official | 193:cff126de7aaf | 116 | pwm->EV0_CTRL = (1 << 12); |
mbed_official | 193:cff126de7aaf | 117 | pwm->EV0_STATE = 0xFFFFFFFF; |
mbed_official | 193:cff126de7aaf | 118 | // Event 1 : MATCH and MATCHSEL=1 |
mbed_official | 193:cff126de7aaf | 119 | pwm->EV1_CTRL = (1 << 12) | (1 << 0); |
mbed_official | 193:cff126de7aaf | 120 | pwm->EV1_STATE = 0xFFFFFFFF; |
mbed_official | 193:cff126de7aaf | 121 | |
mbed_official | 193:cff126de7aaf | 122 | // Match reload register |
mbed_official | 193:cff126de7aaf | 123 | pwm->MATCHREL0 = 20000; // 20ms |
mbed_official | 193:cff126de7aaf | 124 | pwm->MATCHREL1 = (pwm->MATCHREL0 / 4); // 50% duty |
mbed_official | 193:cff126de7aaf | 125 | |
mbed_official | 193:cff126de7aaf | 126 | // unhalt the counter: |
mbed_official | 193:cff126de7aaf | 127 | // - clearing bit 2 of the CTRL register |
mbed_official | 193:cff126de7aaf | 128 | pwm->CTRL &= ~(1 << 2); |
mbed_official | 193:cff126de7aaf | 129 | |
mbed_official | 193:cff126de7aaf | 130 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
mbed_official | 193:cff126de7aaf | 131 | pwmout_period_ms(obj, 20); |
mbed_official | 193:cff126de7aaf | 132 | pwmout_write (obj, 0); |
mbed_official | 193:cff126de7aaf | 133 | } |
mbed_official | 193:cff126de7aaf | 134 | |
mbed_official | 193:cff126de7aaf | 135 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 193:cff126de7aaf | 136 | sct_used &= ~(1 << obj->pwm_ch); |
mbed_official | 193:cff126de7aaf | 137 | if (sct_used == 0) { |
mbed_official | 193:cff126de7aaf | 138 | // Disable the SCT clock |
mbed_official | 193:cff126de7aaf | 139 | LPC_SYSCON->SYSAHBCLKCTRL &= ~(1UL << 31); |
mbed_official | 193:cff126de7aaf | 140 | } |
mbed_official | 193:cff126de7aaf | 141 | } |
mbed_official | 193:cff126de7aaf | 142 | |
mbed_official | 193:cff126de7aaf | 143 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 193:cff126de7aaf | 144 | if (value < 0.0f) { |
mbed_official | 193:cff126de7aaf | 145 | value = 0.0; |
mbed_official | 193:cff126de7aaf | 146 | } else if (value > 1.0f) { |
mbed_official | 193:cff126de7aaf | 147 | value = 1.0; |
mbed_official | 193:cff126de7aaf | 148 | } |
mbed_official | 193:cff126de7aaf | 149 | uint32_t t_on = (uint32_t)((float)(obj->pwm->MATCHREL0) * value); |
mbed_official | 193:cff126de7aaf | 150 | obj->pwm->MATCHREL1 = t_on; |
mbed_official | 193:cff126de7aaf | 151 | } |
mbed_official | 193:cff126de7aaf | 152 | |
mbed_official | 193:cff126de7aaf | 153 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 193:cff126de7aaf | 154 | uint32_t t_off = obj->pwm->MATCHREL0; |
mbed_official | 193:cff126de7aaf | 155 | uint32_t t_on = obj->pwm->MATCHREL1; |
mbed_official | 193:cff126de7aaf | 156 | float v = (float)t_on/(float)t_off; |
mbed_official | 193:cff126de7aaf | 157 | return (v > 1.0f) ? (1.0f) : (v); |
mbed_official | 193:cff126de7aaf | 158 | } |
mbed_official | 193:cff126de7aaf | 159 | |
mbed_official | 193:cff126de7aaf | 160 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 193:cff126de7aaf | 161 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 193:cff126de7aaf | 162 | } |
mbed_official | 193:cff126de7aaf | 163 | |
mbed_official | 193:cff126de7aaf | 164 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 193:cff126de7aaf | 165 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 193:cff126de7aaf | 166 | } |
mbed_official | 193:cff126de7aaf | 167 | |
mbed_official | 193:cff126de7aaf | 168 | // Set the PWM period, keeping the duty cycle the same. |
mbed_official | 193:cff126de7aaf | 169 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 193:cff126de7aaf | 170 | uint32_t t_off = obj->pwm->MATCHREL0; |
mbed_official | 193:cff126de7aaf | 171 | uint32_t t_on = obj->pwm->MATCHREL1; |
mbed_official | 193:cff126de7aaf | 172 | float v = (float)t_on/(float)t_off; |
mbed_official | 193:cff126de7aaf | 173 | obj->pwm->MATCHREL0 = (uint64_t)us; |
mbed_official | 193:cff126de7aaf | 174 | obj->pwm->MATCHREL1 = (uint64_t)((float)us * (float)v); |
mbed_official | 193:cff126de7aaf | 175 | } |
mbed_official | 193:cff126de7aaf | 176 | |
mbed_official | 193:cff126de7aaf | 177 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 193:cff126de7aaf | 178 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 193:cff126de7aaf | 179 | } |
mbed_official | 193:cff126de7aaf | 180 | |
mbed_official | 193:cff126de7aaf | 181 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 193:cff126de7aaf | 182 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 193:cff126de7aaf | 183 | } |
mbed_official | 193:cff126de7aaf | 184 | |
mbed_official | 193:cff126de7aaf | 185 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 193:cff126de7aaf | 186 | obj->pwm->MATCHREL1 = (uint64_t)us; |
mbed_official | 193:cff126de7aaf | 187 | } |
mbed_official | 193:cff126de7aaf | 188 | |
mbed_official | 193:cff126de7aaf | 189 | #endif |