mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/pwmout_api.c@236:6ac0938ee349, 2014-06-24 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jun 24 08:00:08 2014 +0100
- Revision:
- 236:6ac0938ee349
- Parent:
- 227:7bd0639b8911
Synchronized with git revision 31fd3f122572b5dd8476641f50e1b1d9b475a63b
Full URL: https://github.com/mbedmicro/mbed/commit/31fd3f122572b5dd8476641f50e1b1d9b475a63b/
[LPC1549] Enable present tests for the LPC1549
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 30:91c1d09ada54 | 1 | /* mbed Microcontroller Library |
mbed_official | 30:91c1d09ada54 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 30:91c1d09ada54 | 3 | * |
mbed_official | 30:91c1d09ada54 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 30:91c1d09ada54 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 30:91c1d09ada54 | 6 | * You may obtain a copy of the License at |
mbed_official | 30:91c1d09ada54 | 7 | * |
mbed_official | 30:91c1d09ada54 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 30:91c1d09ada54 | 9 | * |
mbed_official | 30:91c1d09ada54 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 30:91c1d09ada54 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 30:91c1d09ada54 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 30:91c1d09ada54 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 30:91c1d09ada54 | 14 | * limitations under the License. |
mbed_official | 30:91c1d09ada54 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 30:91c1d09ada54 | 17 | #include "pwmout_api.h" |
mbed_official | 30:91c1d09ada54 | 18 | #include "cmsis.h" |
mbed_official | 30:91c1d09ada54 | 19 | #include "pinmap.h" |
mbed_official | 30:91c1d09ada54 | 20 | |
mbed_official | 30:91c1d09ada54 | 21 | #define TCR_CNT_EN 0x00000001 |
mbed_official | 30:91c1d09ada54 | 22 | #define TCR_RESET 0x00000002 |
mbed_official | 30:91c1d09ada54 | 23 | |
mbed_official | 30:91c1d09ada54 | 24 | /* To have a PWM where we can change both the period and the duty cycle, |
mbed_official | 30:91c1d09ada54 | 25 | * we need an entire timer. With the following conventions: |
mbed_official | 30:91c1d09ada54 | 26 | * * MR3 is used for the PWM period |
mbed_official | 30:91c1d09ada54 | 27 | * * MR0, MR1, MR2 are used for the duty cycle |
mbed_official | 30:91c1d09ada54 | 28 | */ |
mbed_official | 30:91c1d09ada54 | 29 | static const PinMap PinMap_PWM[] = { |
mbed_official | 30:91c1d09ada54 | 30 | /* CT16B0 */ |
mbed_official | 30:91c1d09ada54 | 31 | {P0_8 , PWM_1, 0x02}, /* MR0 */ |
mbed_official | 30:91c1d09ada54 | 32 | {P0_9 , PWM_2, 0x02}, /* MR1 */ |
mbed_official | 30:91c1d09ada54 | 33 | |
mbed_official | 30:91c1d09ada54 | 34 | /* CT16B1 */ |
mbed_official | 30:91c1d09ada54 | 35 | {P1_9 , PWM_3, 0x01}, /* MR0 */ |
mbed_official | 30:91c1d09ada54 | 36 | {P1_10, PWM_4, 0x02}, /* MR1 */ |
mbed_official | 30:91c1d09ada54 | 37 | |
mbed_official | 30:91c1d09ada54 | 38 | /* CT32B0 */ |
mbed_official | 30:91c1d09ada54 | 39 | {P0_1 , PWM_5, 0x02}, /* MR2 */ |
mbed_official | 30:91c1d09ada54 | 40 | |
mbed_official | 30:91c1d09ada54 | 41 | {NC , NC ,0x00} |
mbed_official | 30:91c1d09ada54 | 42 | }; |
mbed_official | 30:91c1d09ada54 | 43 | |
mbed_official | 30:91c1d09ada54 | 44 | typedef struct { |
mbed_official | 30:91c1d09ada54 | 45 | uint8_t timer; |
mbed_official | 30:91c1d09ada54 | 46 | uint8_t mr; |
mbed_official | 30:91c1d09ada54 | 47 | } timer_mr; |
mbed_official | 30:91c1d09ada54 | 48 | |
mbed_official | 30:91c1d09ada54 | 49 | static timer_mr pwm_timer_map[5] = { |
mbed_official | 30:91c1d09ada54 | 50 | {0, 0}, /* CT16B0, MR0 */ |
mbed_official | 30:91c1d09ada54 | 51 | {0, 1}, /* CT16B0, MR1 */ |
mbed_official | 30:91c1d09ada54 | 52 | |
mbed_official | 30:91c1d09ada54 | 53 | {1, 0}, /* CT16B1, MR0 */ |
mbed_official | 30:91c1d09ada54 | 54 | {1, 1}, /* CT16B1, MR1 */ |
mbed_official | 30:91c1d09ada54 | 55 | |
mbed_official | 30:91c1d09ada54 | 56 | {2, 2}, /* CT32B0, MR2 */ |
mbed_official | 30:91c1d09ada54 | 57 | }; |
mbed_official | 30:91c1d09ada54 | 58 | |
mbed_official | 30:91c1d09ada54 | 59 | static LPC_TMR_TypeDef *Timers[3] = { |
mbed_official | 30:91c1d09ada54 | 60 | LPC_TMR16B0, LPC_TMR16B1, |
mbed_official | 30:91c1d09ada54 | 61 | LPC_TMR32B0 |
mbed_official | 30:91c1d09ada54 | 62 | }; |
mbed_official | 30:91c1d09ada54 | 63 | |
mbed_official | 30:91c1d09ada54 | 64 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 30:91c1d09ada54 | 65 | // determine the channel |
mbed_official | 30:91c1d09ada54 | 66 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 227:7bd0639b8911 | 67 | MBED_ASSERT(pwm != (uint32_t)NC); |
mbed_official | 227:7bd0639b8911 | 68 | |
mbed_official | 30:91c1d09ada54 | 69 | obj->pwm = pwm; |
mbed_official | 30:91c1d09ada54 | 70 | |
mbed_official | 30:91c1d09ada54 | 71 | // Timer registers |
mbed_official | 30:91c1d09ada54 | 72 | timer_mr tid = pwm_timer_map[pwm]; |
mbed_official | 30:91c1d09ada54 | 73 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
mbed_official | 30:91c1d09ada54 | 74 | |
mbed_official | 30:91c1d09ada54 | 75 | // Disable timer |
mbed_official | 30:91c1d09ada54 | 76 | timer->TCR = 0; |
mbed_official | 30:91c1d09ada54 | 77 | |
mbed_official | 30:91c1d09ada54 | 78 | // Power the correspondent timer |
mbed_official | 30:91c1d09ada54 | 79 | LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7); |
mbed_official | 30:91c1d09ada54 | 80 | |
mbed_official | 30:91c1d09ada54 | 81 | /* Enable PWM function */ |
mbed_official | 30:91c1d09ada54 | 82 | timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0); |
mbed_official | 30:91c1d09ada54 | 83 | |
mbed_official | 30:91c1d09ada54 | 84 | /* Reset Functionality on MR3 controlling the PWM period */ |
mbed_official | 30:91c1d09ada54 | 85 | timer->MCR = 1 << 10; |
mbed_official | 30:91c1d09ada54 | 86 | |
mbed_official | 30:91c1d09ada54 | 87 | if (timer == LPC_TMR16B0 || timer == LPC_TMR16B1) { |
mbed_official | 30:91c1d09ada54 | 88 | /* Set 16-bit timer prescaler to avoid timer expire for default 20ms */ |
mbed_official | 30:91c1d09ada54 | 89 | /* This can be also modified by user application, but the prescaler value */ |
mbed_official | 30:91c1d09ada54 | 90 | /* might be trade-off to timer accuracy */ |
mbed_official | 30:91c1d09ada54 | 91 | timer->PR = 30; |
mbed_official | 30:91c1d09ada54 | 92 | } |
mbed_official | 30:91c1d09ada54 | 93 | |
mbed_official | 30:91c1d09ada54 | 94 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
mbed_official | 30:91c1d09ada54 | 95 | pwmout_period_ms(obj, 20); |
mbed_official | 30:91c1d09ada54 | 96 | pwmout_write (obj, 0); |
mbed_official | 30:91c1d09ada54 | 97 | |
mbed_official | 30:91c1d09ada54 | 98 | // Wire pinout |
mbed_official | 30:91c1d09ada54 | 99 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 30:91c1d09ada54 | 100 | } |
mbed_official | 30:91c1d09ada54 | 101 | |
mbed_official | 30:91c1d09ada54 | 102 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 30:91c1d09ada54 | 103 | // [TODO] |
mbed_official | 30:91c1d09ada54 | 104 | } |
mbed_official | 30:91c1d09ada54 | 105 | |
mbed_official | 30:91c1d09ada54 | 106 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 30:91c1d09ada54 | 107 | if (value < 0.0f) { |
mbed_official | 30:91c1d09ada54 | 108 | value = 0.0; |
mbed_official | 30:91c1d09ada54 | 109 | } else if (value > 1.0f) { |
mbed_official | 30:91c1d09ada54 | 110 | value = 1.0; |
mbed_official | 30:91c1d09ada54 | 111 | } |
mbed_official | 236:6ac0938ee349 | 112 | |
mbed_official | 30:91c1d09ada54 | 113 | timer_mr tid = pwm_timer_map[obj->pwm]; |
mbed_official | 30:91c1d09ada54 | 114 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
mbed_official | 30:91c1d09ada54 | 115 | uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value); |
mbed_official | 236:6ac0938ee349 | 116 | // to avoid spike pulse when duty is 0% |
mbed_official | 236:6ac0938ee349 | 117 | if (value == 0) { |
mbed_official | 236:6ac0938ee349 | 118 | t_off++; |
mbed_official | 236:6ac0938ee349 | 119 | } |
mbed_official | 236:6ac0938ee349 | 120 | |
mbed_official | 30:91c1d09ada54 | 121 | timer->TCR = TCR_RESET; |
mbed_official | 30:91c1d09ada54 | 122 | timer->MR[tid.mr] = t_off; |
mbed_official | 30:91c1d09ada54 | 123 | timer->TCR = TCR_CNT_EN; |
mbed_official | 30:91c1d09ada54 | 124 | } |
mbed_official | 30:91c1d09ada54 | 125 | |
mbed_official | 30:91c1d09ada54 | 126 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 30:91c1d09ada54 | 127 | timer_mr tid = pwm_timer_map[obj->pwm]; |
mbed_official | 30:91c1d09ada54 | 128 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
mbed_official | 30:91c1d09ada54 | 129 | |
mbed_official | 30:91c1d09ada54 | 130 | float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3); |
mbed_official | 236:6ac0938ee349 | 131 | if (timer->MR[tid.mr] > timer->MR3) { |
mbed_official | 236:6ac0938ee349 | 132 | v = 0.0f; |
mbed_official | 236:6ac0938ee349 | 133 | } |
mbed_official | 30:91c1d09ada54 | 134 | return (v > 1.0f) ? (1.0f) : (v); |
mbed_official | 30:91c1d09ada54 | 135 | } |
mbed_official | 30:91c1d09ada54 | 136 | |
mbed_official | 30:91c1d09ada54 | 137 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 30:91c1d09ada54 | 138 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 30:91c1d09ada54 | 139 | } |
mbed_official | 30:91c1d09ada54 | 140 | |
mbed_official | 30:91c1d09ada54 | 141 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 30:91c1d09ada54 | 142 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 30:91c1d09ada54 | 143 | } |
mbed_official | 30:91c1d09ada54 | 144 | |
mbed_official | 30:91c1d09ada54 | 145 | // Set the PWM period, keeping the duty cycle the same. |
mbed_official | 30:91c1d09ada54 | 146 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 30:91c1d09ada54 | 147 | int i = 0; |
mbed_official | 30:91c1d09ada54 | 148 | uint32_t period_ticks; |
mbed_official | 30:91c1d09ada54 | 149 | |
mbed_official | 30:91c1d09ada54 | 150 | timer_mr tid = pwm_timer_map[obj->pwm]; |
mbed_official | 30:91c1d09ada54 | 151 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
mbed_official | 30:91c1d09ada54 | 152 | uint32_t old_period_ticks = timer->MR3; |
mbed_official | 30:91c1d09ada54 | 153 | period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1); |
mbed_official | 30:91c1d09ada54 | 154 | |
mbed_official | 30:91c1d09ada54 | 155 | timer->TCR = TCR_RESET; |
mbed_official | 30:91c1d09ada54 | 156 | timer->MR3 = period_ticks; |
mbed_official | 30:91c1d09ada54 | 157 | |
mbed_official | 30:91c1d09ada54 | 158 | // Scale the pulse width to preserve the duty ratio |
mbed_official | 30:91c1d09ada54 | 159 | if (old_period_ticks > 0) { |
mbed_official | 30:91c1d09ada54 | 160 | for (i=0; i<3; i++) { |
mbed_official | 30:91c1d09ada54 | 161 | uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks); |
mbed_official | 30:91c1d09ada54 | 162 | timer->MR[i] = t_off; |
mbed_official | 30:91c1d09ada54 | 163 | } |
mbed_official | 30:91c1d09ada54 | 164 | } |
mbed_official | 30:91c1d09ada54 | 165 | timer->TCR = TCR_CNT_EN; |
mbed_official | 30:91c1d09ada54 | 166 | } |
mbed_official | 30:91c1d09ada54 | 167 | |
mbed_official | 30:91c1d09ada54 | 168 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 30:91c1d09ada54 | 169 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 30:91c1d09ada54 | 170 | } |
mbed_official | 30:91c1d09ada54 | 171 | |
mbed_official | 30:91c1d09ada54 | 172 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 30:91c1d09ada54 | 173 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 30:91c1d09ada54 | 174 | } |
mbed_official | 30:91c1d09ada54 | 175 | |
mbed_official | 30:91c1d09ada54 | 176 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 30:91c1d09ada54 | 177 | timer_mr tid = pwm_timer_map[obj->pwm]; |
mbed_official | 30:91c1d09ada54 | 178 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
mbed_official | 30:91c1d09ada54 | 179 | uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1)); |
mbed_official | 30:91c1d09ada54 | 180 | |
mbed_official | 30:91c1d09ada54 | 181 | timer->TCR = TCR_RESET; |
mbed_official | 30:91c1d09ada54 | 182 | if (t_on > timer->MR3) { |
mbed_official | 30:91c1d09ada54 | 183 | pwmout_period_us(obj, us); |
mbed_official | 30:91c1d09ada54 | 184 | } |
mbed_official | 30:91c1d09ada54 | 185 | uint32_t t_off = timer->MR3 - t_on; |
mbed_official | 30:91c1d09ada54 | 186 | timer->MR[tid.mr] = t_off; |
mbed_official | 30:91c1d09ada54 | 187 | timer->TCR = TCR_CNT_EN; |
mbed_official | 30:91c1d09ada54 | 188 | } |