mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC408X/pwmout_api.c@259:da299e454ce7, 2014-07-18 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Jul 18 14:15:09 2014 +0100
- Revision:
- 259:da299e454ce7
- Parent:
- 227:7bd0639b8911
Synchronized with git revision df84f2b3e967624878b8b54e093a8a3a675f3ca7
Full URL: https://github.com/mbedmicro/mbed/commit/df84f2b3e967624878b8b54e093a8a3a675f3ca7/
New ARCH_GPRS target
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 15:4892fe388435 | 1 | /* mbed Microcontroller Library |
bogdanm | 15:4892fe388435 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 15:4892fe388435 | 3 | * |
bogdanm | 15:4892fe388435 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 15:4892fe388435 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 15:4892fe388435 | 6 | * You may obtain a copy of the License at |
bogdanm | 15:4892fe388435 | 7 | * |
bogdanm | 15:4892fe388435 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 15:4892fe388435 | 9 | * |
bogdanm | 15:4892fe388435 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 15:4892fe388435 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 15:4892fe388435 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 15:4892fe388435 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 15:4892fe388435 | 14 | * limitations under the License. |
bogdanm | 15:4892fe388435 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
bogdanm | 15:4892fe388435 | 17 | #include "pwmout_api.h" |
bogdanm | 15:4892fe388435 | 18 | #include "cmsis.h" |
bogdanm | 15:4892fe388435 | 19 | #include "pinmap.h" |
bogdanm | 15:4892fe388435 | 20 | |
bogdanm | 15:4892fe388435 | 21 | #define TCR_CNT_EN 0x00000001 |
bogdanm | 15:4892fe388435 | 22 | #define TCR_RESET 0x00000002 |
bogdanm | 15:4892fe388435 | 23 | |
bogdanm | 15:4892fe388435 | 24 | // PORT ID, PWM ID, Pin function |
bogdanm | 15:4892fe388435 | 25 | static const PinMap PinMap_PWM[] = { |
bogdanm | 15:4892fe388435 | 26 | {P1_2, PWM0_1, 3}, |
bogdanm | 15:4892fe388435 | 27 | {P1_3, PWM0_2, 3}, |
bogdanm | 15:4892fe388435 | 28 | {P1_5, PWM0_3, 3}, |
bogdanm | 15:4892fe388435 | 29 | {P1_6, PWM0_4, 3}, |
bogdanm | 15:4892fe388435 | 30 | {P1_7, PWM0_5, 3}, |
bogdanm | 15:4892fe388435 | 31 | {P1_11, PWM0_6, 3}, |
bogdanm | 15:4892fe388435 | 32 | {P1_18, PWM1_1, 2}, |
bogdanm | 15:4892fe388435 | 33 | {P1_20, PWM1_2, 2}, |
bogdanm | 15:4892fe388435 | 34 | {P1_21, PWM1_3, 2}, |
bogdanm | 15:4892fe388435 | 35 | {P1_23, PWM1_4, 2}, |
bogdanm | 15:4892fe388435 | 36 | {P1_24, PWM1_5, 2}, |
bogdanm | 15:4892fe388435 | 37 | {P1_26, PWM1_6, 2}, |
bogdanm | 15:4892fe388435 | 38 | {P2_0, PWM1_1, 1}, |
bogdanm | 15:4892fe388435 | 39 | {P2_1, PWM1_2, 1}, |
bogdanm | 15:4892fe388435 | 40 | {P2_2, PWM1_3, 1}, |
bogdanm | 15:4892fe388435 | 41 | {P2_3, PWM1_4, 1}, |
bogdanm | 15:4892fe388435 | 42 | {P2_4, PWM1_5, 1}, |
bogdanm | 15:4892fe388435 | 43 | {P2_5, PWM1_6, 1}, |
bogdanm | 15:4892fe388435 | 44 | {P3_16, PWM0_1, 2}, |
bogdanm | 15:4892fe388435 | 45 | {P3_17, PWM0_2, 2}, |
bogdanm | 15:4892fe388435 | 46 | {P3_18, PWM0_3, 2}, |
bogdanm | 15:4892fe388435 | 47 | {P3_19, PWM0_4, 2}, |
bogdanm | 15:4892fe388435 | 48 | {P3_20, PWM0_5, 2}, |
bogdanm | 15:4892fe388435 | 49 | {P3_21, PWM0_6, 2}, |
bogdanm | 15:4892fe388435 | 50 | {P3_24, PWM1_1, 2}, |
bogdanm | 15:4892fe388435 | 51 | {P3_25, PWM1_2, 2}, |
bogdanm | 15:4892fe388435 | 52 | {P3_26, PWM1_3, 2}, |
bogdanm | 15:4892fe388435 | 53 | {P3_27, PWM1_4, 2}, |
bogdanm | 15:4892fe388435 | 54 | {P3_28, PWM1_5, 2}, |
bogdanm | 15:4892fe388435 | 55 | {P3_29, PWM1_6, 2}, |
bogdanm | 15:4892fe388435 | 56 | {NC, NC, 0} |
bogdanm | 15:4892fe388435 | 57 | }; |
bogdanm | 15:4892fe388435 | 58 | |
bogdanm | 15:4892fe388435 | 59 | static const uint32_t PWM_mr_offset[7] = { |
bogdanm | 15:4892fe388435 | 60 | 0x18, 0x1C, 0x20, 0x24, 0x40, 0x44, 0x48 |
bogdanm | 15:4892fe388435 | 61 | }; |
bogdanm | 15:4892fe388435 | 62 | |
bogdanm | 15:4892fe388435 | 63 | #define TCR_PWM_EN 0x00000008 |
bogdanm | 15:4892fe388435 | 64 | static unsigned int pwm_clock_mhz; |
bogdanm | 15:4892fe388435 | 65 | |
bogdanm | 15:4892fe388435 | 66 | void pwmout_init(pwmout_t* obj, PinName pin) { |
bogdanm | 15:4892fe388435 | 67 | // determine the channel |
bogdanm | 15:4892fe388435 | 68 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 227:7bd0639b8911 | 69 | MBED_ASSERT(pwm != (PWMName)NC); |
mbed_official | 227:7bd0639b8911 | 70 | |
bogdanm | 15:4892fe388435 | 71 | obj->channel = pwm; |
bogdanm | 15:4892fe388435 | 72 | obj->pwm = LPC_PWM0; |
bogdanm | 15:4892fe388435 | 73 | |
bogdanm | 15:4892fe388435 | 74 | if (obj->channel > 6) { // PWM1 is used if pwm > 6 |
bogdanm | 15:4892fe388435 | 75 | obj->channel -= 6; |
bogdanm | 15:4892fe388435 | 76 | obj->pwm = LPC_PWM1; |
bogdanm | 15:4892fe388435 | 77 | } |
bogdanm | 15:4892fe388435 | 78 | |
bogdanm | 15:4892fe388435 | 79 | obj->MR = (__IO uint32_t *)((uint32_t)obj->pwm + PWM_mr_offset[obj->channel]); |
bogdanm | 15:4892fe388435 | 80 | |
bogdanm | 15:4892fe388435 | 81 | // ensure the power is on |
bogdanm | 15:4892fe388435 | 82 | if (obj->pwm == LPC_PWM0) { |
bogdanm | 15:4892fe388435 | 83 | LPC_SC->PCONP |= 1 << 5; |
bogdanm | 15:4892fe388435 | 84 | } else { |
bogdanm | 15:4892fe388435 | 85 | LPC_SC->PCONP |= 1 << 6; |
bogdanm | 15:4892fe388435 | 86 | } |
bogdanm | 15:4892fe388435 | 87 | |
bogdanm | 15:4892fe388435 | 88 | obj->pwm->PR = 0; // no pre-scale |
bogdanm | 15:4892fe388435 | 89 | |
bogdanm | 15:4892fe388435 | 90 | // ensure single PWM mode |
bogdanm | 15:4892fe388435 | 91 | obj->pwm->MCR = 1 << 1; // reset TC on match 0 |
bogdanm | 15:4892fe388435 | 92 | |
bogdanm | 15:4892fe388435 | 93 | // enable the specific PWM output |
bogdanm | 15:4892fe388435 | 94 | obj->pwm->PCR |= 1 << (8 + obj->channel); |
bogdanm | 15:4892fe388435 | 95 | |
bogdanm | 15:4892fe388435 | 96 | pwm_clock_mhz = PeripheralClock / 1000000; |
bogdanm | 15:4892fe388435 | 97 | |
bogdanm | 15:4892fe388435 | 98 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
bogdanm | 15:4892fe388435 | 99 | pwmout_period_ms(obj, 20); |
bogdanm | 15:4892fe388435 | 100 | pwmout_write (obj, 0); |
bogdanm | 15:4892fe388435 | 101 | |
bogdanm | 15:4892fe388435 | 102 | // Wire pinout |
bogdanm | 15:4892fe388435 | 103 | pinmap_pinout(pin, PinMap_PWM); |
bogdanm | 15:4892fe388435 | 104 | } |
bogdanm | 15:4892fe388435 | 105 | |
bogdanm | 15:4892fe388435 | 106 | void pwmout_free(pwmout_t* obj) { |
bogdanm | 15:4892fe388435 | 107 | // [TODO] |
bogdanm | 15:4892fe388435 | 108 | } |
bogdanm | 15:4892fe388435 | 109 | |
bogdanm | 15:4892fe388435 | 110 | void pwmout_write(pwmout_t* obj, float value) { |
bogdanm | 15:4892fe388435 | 111 | if (value < 0.0f) { |
bogdanm | 15:4892fe388435 | 112 | value = 0.0; |
bogdanm | 15:4892fe388435 | 113 | } else if (value > 1.0f) { |
bogdanm | 15:4892fe388435 | 114 | value = 1.0; |
bogdanm | 15:4892fe388435 | 115 | } |
bogdanm | 15:4892fe388435 | 116 | |
bogdanm | 15:4892fe388435 | 117 | // set channel match to percentage |
bogdanm | 15:4892fe388435 | 118 | uint32_t v = (uint32_t)((float)(obj->pwm->MR0) * value); |
bogdanm | 15:4892fe388435 | 119 | |
bogdanm | 15:4892fe388435 | 120 | // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout |
bogdanm | 15:4892fe388435 | 121 | if (v == obj->pwm->MR0) { |
bogdanm | 15:4892fe388435 | 122 | v++; |
bogdanm | 15:4892fe388435 | 123 | } |
bogdanm | 15:4892fe388435 | 124 | |
bogdanm | 15:4892fe388435 | 125 | *obj->MR = v; |
bogdanm | 15:4892fe388435 | 126 | |
bogdanm | 15:4892fe388435 | 127 | // accept on next period start |
bogdanm | 15:4892fe388435 | 128 | obj->pwm->LER |= 1 << obj->channel; |
bogdanm | 15:4892fe388435 | 129 | } |
bogdanm | 15:4892fe388435 | 130 | |
bogdanm | 15:4892fe388435 | 131 | float pwmout_read(pwmout_t* obj) { |
bogdanm | 15:4892fe388435 | 132 | float v = (float)(*obj->MR) / (float)(obj->pwm->MR0); |
bogdanm | 15:4892fe388435 | 133 | return (v > 1.0f) ? (1.0f) : (v); |
bogdanm | 15:4892fe388435 | 134 | } |
bogdanm | 15:4892fe388435 | 135 | |
bogdanm | 15:4892fe388435 | 136 | void pwmout_period(pwmout_t* obj, float seconds) { |
bogdanm | 15:4892fe388435 | 137 | pwmout_period_us(obj, seconds * 1000000.0f); |
bogdanm | 15:4892fe388435 | 138 | } |
bogdanm | 15:4892fe388435 | 139 | |
bogdanm | 15:4892fe388435 | 140 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
bogdanm | 15:4892fe388435 | 141 | pwmout_period_us(obj, ms * 1000); |
bogdanm | 15:4892fe388435 | 142 | } |
bogdanm | 15:4892fe388435 | 143 | |
bogdanm | 15:4892fe388435 | 144 | // Set the PWM period, keeping the duty cycle the same. |
bogdanm | 15:4892fe388435 | 145 | void pwmout_period_us(pwmout_t* obj, int us) { |
bogdanm | 15:4892fe388435 | 146 | // calculate number of ticks |
bogdanm | 15:4892fe388435 | 147 | uint32_t ticks = pwm_clock_mhz * us; |
bogdanm | 15:4892fe388435 | 148 | |
bogdanm | 15:4892fe388435 | 149 | // set reset |
bogdanm | 15:4892fe388435 | 150 | obj->pwm->TCR = TCR_RESET; |
bogdanm | 15:4892fe388435 | 151 | |
bogdanm | 15:4892fe388435 | 152 | // set the global match register |
bogdanm | 15:4892fe388435 | 153 | obj->pwm->MR0 = ticks; |
bogdanm | 15:4892fe388435 | 154 | |
bogdanm | 15:4892fe388435 | 155 | // Scale the pulse width to preserve the duty ratio |
bogdanm | 15:4892fe388435 | 156 | if (obj->pwm->MR0 > 0) { |
bogdanm | 15:4892fe388435 | 157 | *obj->MR = (*obj->MR * ticks) / obj->pwm->MR0; |
bogdanm | 15:4892fe388435 | 158 | } |
bogdanm | 15:4892fe388435 | 159 | |
bogdanm | 15:4892fe388435 | 160 | // set the channel latch to update value at next period start |
bogdanm | 15:4892fe388435 | 161 | obj->pwm->LER |= 1 << 0; |
bogdanm | 15:4892fe388435 | 162 | |
bogdanm | 15:4892fe388435 | 163 | // enable counter and pwm, clear reset |
bogdanm | 15:4892fe388435 | 164 | obj->pwm->TCR = TCR_CNT_EN | TCR_PWM_EN; |
bogdanm | 15:4892fe388435 | 165 | } |
bogdanm | 15:4892fe388435 | 166 | |
bogdanm | 15:4892fe388435 | 167 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
bogdanm | 15:4892fe388435 | 168 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
bogdanm | 15:4892fe388435 | 169 | } |
bogdanm | 15:4892fe388435 | 170 | |
bogdanm | 15:4892fe388435 | 171 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
bogdanm | 15:4892fe388435 | 172 | pwmout_pulsewidth_us(obj, ms * 1000); |
bogdanm | 15:4892fe388435 | 173 | } |
bogdanm | 15:4892fe388435 | 174 | |
bogdanm | 15:4892fe388435 | 175 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
bogdanm | 15:4892fe388435 | 176 | // calculate number of ticks |
bogdanm | 15:4892fe388435 | 177 | uint32_t v = pwm_clock_mhz * us; |
bogdanm | 15:4892fe388435 | 178 | |
bogdanm | 15:4892fe388435 | 179 | // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout |
bogdanm | 15:4892fe388435 | 180 | if (v == obj->pwm->MR0) { |
bogdanm | 15:4892fe388435 | 181 | v++; |
bogdanm | 15:4892fe388435 | 182 | } |
bogdanm | 15:4892fe388435 | 183 | |
bogdanm | 15:4892fe388435 | 184 | // set the match register value |
bogdanm | 15:4892fe388435 | 185 | *obj->MR = v; |
bogdanm | 15:4892fe388435 | 186 | |
bogdanm | 15:4892fe388435 | 187 | // set the channel latch to update value at next period start |
bogdanm | 15:4892fe388435 | 188 | obj->pwm->LER |= 1 << obj->channel; |
bogdanm | 15:4892fe388435 | 189 | } |