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