mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC11UXX/pwmout_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_NXP/TARGET_LPC11UXX/pwmout_api.c@144:ef7eb2e8f9f7
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 17 | #include "pwmout_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform |
<> | 144:ef7eb2e8f9f7 | 21 | |
<> | 144:ef7eb2e8f9f7 | 22 | #define TCR_CNT_EN 0x00000001 |
<> | 144:ef7eb2e8f9f7 | 23 | #define TCR_RESET 0x00000002 |
<> | 144:ef7eb2e8f9f7 | 24 | |
<> | 144:ef7eb2e8f9f7 | 25 | typedef struct { |
<> | 144:ef7eb2e8f9f7 | 26 | uint8_t timer; |
<> | 144:ef7eb2e8f9f7 | 27 | uint8_t mr; |
<> | 144:ef7eb2e8f9f7 | 28 | } timer_mr; |
<> | 144:ef7eb2e8f9f7 | 29 | |
<> | 144:ef7eb2e8f9f7 | 30 | static timer_mr pwm_timer_map[11] = { |
<> | 144:ef7eb2e8f9f7 | 31 | {0, 0}, {0, 1}, {0, 2}, |
<> | 144:ef7eb2e8f9f7 | 32 | {1, 0}, {1, 1}, |
<> | 144:ef7eb2e8f9f7 | 33 | {2, 0}, {2, 1}, {2, 2}, |
<> | 144:ef7eb2e8f9f7 | 34 | {3, 0}, {3, 1}, {3, 2}, |
<> | 144:ef7eb2e8f9f7 | 35 | }; |
<> | 144:ef7eb2e8f9f7 | 36 | |
<> | 144:ef7eb2e8f9f7 | 37 | static LPC_CTxxBx_Type *Timers[4] = { |
<> | 144:ef7eb2e8f9f7 | 38 | LPC_CT16B0, LPC_CT16B1, |
<> | 144:ef7eb2e8f9f7 | 39 | LPC_CT32B0, LPC_CT32B1 |
<> | 144:ef7eb2e8f9f7 | 40 | }; |
<> | 144:ef7eb2e8f9f7 | 41 | |
<> | 144:ef7eb2e8f9f7 | 42 | void pwmout_init(pwmout_t* obj, PinName pin) { |
<> | 144:ef7eb2e8f9f7 | 43 | // determine the channel |
<> | 144:ef7eb2e8f9f7 | 44 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 45 | MBED_ASSERT(pwm != (PWMName)NC); |
<> | 144:ef7eb2e8f9f7 | 46 | |
<> | 144:ef7eb2e8f9f7 | 47 | obj->pwm = pwm; |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | // Timer registers |
<> | 144:ef7eb2e8f9f7 | 50 | timer_mr tid = pwm_timer_map[pwm]; |
<> | 144:ef7eb2e8f9f7 | 51 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | // Disable timer |
<> | 144:ef7eb2e8f9f7 | 54 | timer->TCR = 0; |
<> | 144:ef7eb2e8f9f7 | 55 | |
<> | 144:ef7eb2e8f9f7 | 56 | // Power the correspondent timer |
<> | 144:ef7eb2e8f9f7 | 57 | LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7); |
<> | 144:ef7eb2e8f9f7 | 58 | |
<> | 144:ef7eb2e8f9f7 | 59 | /* Enable PWM function */ |
<> | 144:ef7eb2e8f9f7 | 60 | timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0); |
<> | 144:ef7eb2e8f9f7 | 61 | |
<> | 144:ef7eb2e8f9f7 | 62 | /* Reset Functionality on MR3 controlling the PWM period */ |
<> | 144:ef7eb2e8f9f7 | 63 | timer->MCR = 1 << 10; |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
<> | 144:ef7eb2e8f9f7 | 66 | pwmout_period_ms(obj, 20); |
<> | 144:ef7eb2e8f9f7 | 67 | pwmout_write (obj, 0); |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | // Wire pinout |
<> | 144:ef7eb2e8f9f7 | 70 | pinmap_pinout(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 71 | } |
<> | 144:ef7eb2e8f9f7 | 72 | |
<> | 144:ef7eb2e8f9f7 | 73 | void pwmout_free(pwmout_t* obj) { |
<> | 144:ef7eb2e8f9f7 | 74 | // [TODO] |
<> | 144:ef7eb2e8f9f7 | 75 | } |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | void pwmout_write(pwmout_t* obj, float value) { |
<> | 144:ef7eb2e8f9f7 | 78 | if (value < 0.0f) { |
<> | 144:ef7eb2e8f9f7 | 79 | value = 0.0; |
<> | 144:ef7eb2e8f9f7 | 80 | } else if (value > 1.0f) { |
<> | 144:ef7eb2e8f9f7 | 81 | value = 1.0; |
<> | 144:ef7eb2e8f9f7 | 82 | } |
<> | 144:ef7eb2e8f9f7 | 83 | |
<> | 144:ef7eb2e8f9f7 | 84 | timer_mr tid = pwm_timer_map[obj->pwm]; |
<> | 144:ef7eb2e8f9f7 | 85 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
<> | 144:ef7eb2e8f9f7 | 86 | uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value); |
<> | 144:ef7eb2e8f9f7 | 87 | |
<> | 144:ef7eb2e8f9f7 | 88 | timer->MR[tid.mr] = t_off; |
<> | 144:ef7eb2e8f9f7 | 89 | } |
<> | 144:ef7eb2e8f9f7 | 90 | |
<> | 144:ef7eb2e8f9f7 | 91 | float pwmout_read(pwmout_t* obj) { |
<> | 144:ef7eb2e8f9f7 | 92 | timer_mr tid = pwm_timer_map[obj->pwm]; |
<> | 144:ef7eb2e8f9f7 | 93 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
<> | 144:ef7eb2e8f9f7 | 94 | |
<> | 144:ef7eb2e8f9f7 | 95 | float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3); |
<> | 144:ef7eb2e8f9f7 | 96 | return (v > 1.0f) ? (1.0f) : (v); |
<> | 144:ef7eb2e8f9f7 | 97 | } |
<> | 144:ef7eb2e8f9f7 | 98 | |
<> | 144:ef7eb2e8f9f7 | 99 | void pwmout_period(pwmout_t* obj, float seconds) { |
<> | 144:ef7eb2e8f9f7 | 100 | pwmout_period_us(obj, seconds * 1000000.0f); |
<> | 144:ef7eb2e8f9f7 | 101 | } |
<> | 144:ef7eb2e8f9f7 | 102 | |
<> | 144:ef7eb2e8f9f7 | 103 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
<> | 144:ef7eb2e8f9f7 | 104 | pwmout_period_us(obj, ms * 1000); |
<> | 144:ef7eb2e8f9f7 | 105 | } |
<> | 144:ef7eb2e8f9f7 | 106 | |
<> | 144:ef7eb2e8f9f7 | 107 | // Set the PWM period, keeping the duty cycle the same. |
<> | 144:ef7eb2e8f9f7 | 108 | void pwmout_period_us(pwmout_t* obj, int us) { |
<> | 144:ef7eb2e8f9f7 | 109 | int i = 0; |
<> | 144:ef7eb2e8f9f7 | 110 | uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000); |
<> | 144:ef7eb2e8f9f7 | 111 | |
<> | 144:ef7eb2e8f9f7 | 112 | timer_mr tid = pwm_timer_map[obj->pwm]; |
<> | 144:ef7eb2e8f9f7 | 113 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
<> | 144:ef7eb2e8f9f7 | 114 | uint32_t old_period_ticks = timer->MR3; |
<> | 144:ef7eb2e8f9f7 | 115 | |
<> | 144:ef7eb2e8f9f7 | 116 | // for 16bit timer, set prescaler to avoid overflow |
<> | 144:ef7eb2e8f9f7 | 117 | if (timer == LPC_CT16B0 || timer == LPC_CT16B1) { |
<> | 144:ef7eb2e8f9f7 | 118 | uint16_t high_period_ticks = period_ticks >> 16; |
<> | 144:ef7eb2e8f9f7 | 119 | timer->PR = high_period_ticks; |
<> | 144:ef7eb2e8f9f7 | 120 | period_ticks /= (high_period_ticks + 1); |
<> | 144:ef7eb2e8f9f7 | 121 | } |
<> | 144:ef7eb2e8f9f7 | 122 | |
<> | 144:ef7eb2e8f9f7 | 123 | timer->TCR = TCR_RESET; |
<> | 144:ef7eb2e8f9f7 | 124 | timer->MR3 = period_ticks; |
<> | 144:ef7eb2e8f9f7 | 125 | |
<> | 144:ef7eb2e8f9f7 | 126 | // Scale the pulse width to preserve the duty ratio |
<> | 144:ef7eb2e8f9f7 | 127 | if (old_period_ticks > 0) { |
<> | 144:ef7eb2e8f9f7 | 128 | for (i=0; i<3; i++) { |
<> | 144:ef7eb2e8f9f7 | 129 | uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks); |
<> | 144:ef7eb2e8f9f7 | 130 | timer->MR[i] = t_off; |
<> | 144:ef7eb2e8f9f7 | 131 | } |
<> | 144:ef7eb2e8f9f7 | 132 | } |
<> | 144:ef7eb2e8f9f7 | 133 | timer->TCR = TCR_CNT_EN; |
<> | 144:ef7eb2e8f9f7 | 134 | } |
<> | 144:ef7eb2e8f9f7 | 135 | |
<> | 144:ef7eb2e8f9f7 | 136 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
<> | 144:ef7eb2e8f9f7 | 137 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
<> | 144:ef7eb2e8f9f7 | 138 | } |
<> | 144:ef7eb2e8f9f7 | 139 | |
<> | 144:ef7eb2e8f9f7 | 140 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
<> | 144:ef7eb2e8f9f7 | 141 | pwmout_pulsewidth_us(obj, ms * 1000); |
<> | 144:ef7eb2e8f9f7 | 142 | } |
<> | 144:ef7eb2e8f9f7 | 143 | |
<> | 144:ef7eb2e8f9f7 | 144 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
<> | 144:ef7eb2e8f9f7 | 145 | timer_mr tid = pwm_timer_map[obj->pwm]; |
<> | 144:ef7eb2e8f9f7 | 146 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
<> | 144:ef7eb2e8f9f7 | 147 | uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1)); |
<> | 144:ef7eb2e8f9f7 | 148 | |
<> | 144:ef7eb2e8f9f7 | 149 | timer->TCR = TCR_RESET; |
<> | 144:ef7eb2e8f9f7 | 150 | if (t_on > timer->MR3) { |
<> | 144:ef7eb2e8f9f7 | 151 | pwmout_period_us(obj, us); |
<> | 144:ef7eb2e8f9f7 | 152 | t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1)); |
<> | 144:ef7eb2e8f9f7 | 153 | } |
<> | 144:ef7eb2e8f9f7 | 154 | uint32_t t_off = timer->MR3 - t_on; |
<> | 144:ef7eb2e8f9f7 | 155 | timer->MR[tid.mr] = t_off; |
<> | 144:ef7eb2e8f9f7 | 156 | timer->TCR = TCR_CNT_EN; |
<> | 144:ef7eb2e8f9f7 | 157 | } |