mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Aug 06 08:15:07 2014 +0100
Revision:
274:6937b19af361
Parent:
227:7bd0639b8911
Synchronized with git revision 5b145e4f6c509376173c3ea2aa35a6da879a2124

Full URL: https://github.com/mbedmicro/mbed/commit/5b145e4f6c509376173c3ea2aa35a6da879a2124/

[TARGET_LPC11UXX] PeripheralNames.h and PinMap definitions separation for LPC11UXX platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /* mbed Microcontroller Library
emilmont 10:3bc89ef62ce7 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 10:3bc89ef62ce7 3 *
emilmont 10:3bc89ef62ce7 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 10:3bc89ef62ce7 5 * you may not use this file except in compliance with the License.
emilmont 10:3bc89ef62ce7 6 * You may obtain a copy of the License at
emilmont 10:3bc89ef62ce7 7 *
emilmont 10:3bc89ef62ce7 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 10:3bc89ef62ce7 9 *
emilmont 10:3bc89ef62ce7 10 * Unless required by applicable law or agreed to in writing, software
emilmont 10:3bc89ef62ce7 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 10:3bc89ef62ce7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 10:3bc89ef62ce7 13 * See the License for the specific language governing permissions and
emilmont 10:3bc89ef62ce7 14 * limitations under the License.
emilmont 10:3bc89ef62ce7 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
emilmont 10:3bc89ef62ce7 17 #include "pwmout_api.h"
emilmont 10:3bc89ef62ce7 18 #include "cmsis.h"
emilmont 10:3bc89ef62ce7 19 #include "pinmap.h"
mbed_official 274:6937b19af361 20 #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform
emilmont 10:3bc89ef62ce7 21
emilmont 10:3bc89ef62ce7 22 #define TCR_CNT_EN 0x00000001
emilmont 10:3bc89ef62ce7 23 #define TCR_RESET 0x00000002
emilmont 10:3bc89ef62ce7 24
emilmont 10:3bc89ef62ce7 25 typedef struct {
emilmont 10:3bc89ef62ce7 26 uint8_t timer;
emilmont 10:3bc89ef62ce7 27 uint8_t mr;
emilmont 10:3bc89ef62ce7 28 } timer_mr;
emilmont 10:3bc89ef62ce7 29
emilmont 10:3bc89ef62ce7 30 static timer_mr pwm_timer_map[11] = {
emilmont 10:3bc89ef62ce7 31 {0, 0}, {0, 1}, {0, 2},
emilmont 10:3bc89ef62ce7 32 {1, 0}, {1, 1},
emilmont 10:3bc89ef62ce7 33 {2, 0}, {2, 1}, {2, 2},
emilmont 10:3bc89ef62ce7 34 {3, 0}, {3, 1}, {3, 2},
emilmont 10:3bc89ef62ce7 35 };
emilmont 10:3bc89ef62ce7 36
emilmont 10:3bc89ef62ce7 37 static LPC_CTxxBx_Type *Timers[4] = {
emilmont 10:3bc89ef62ce7 38 LPC_CT16B0, LPC_CT16B1,
emilmont 10:3bc89ef62ce7 39 LPC_CT32B0, LPC_CT32B1
emilmont 10:3bc89ef62ce7 40 };
emilmont 10:3bc89ef62ce7 41
emilmont 10:3bc89ef62ce7 42 void pwmout_init(pwmout_t* obj, PinName pin) {
emilmont 10:3bc89ef62ce7 43 // determine the channel
emilmont 10:3bc89ef62ce7 44 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 227:7bd0639b8911 45 MBED_ASSERT(pwm != (PWMName)NC);
mbed_official 227:7bd0639b8911 46
emilmont 10:3bc89ef62ce7 47 obj->pwm = pwm;
emilmont 10:3bc89ef62ce7 48
emilmont 10:3bc89ef62ce7 49 // Timer registers
emilmont 10:3bc89ef62ce7 50 timer_mr tid = pwm_timer_map[pwm];
emilmont 10:3bc89ef62ce7 51 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 52
emilmont 10:3bc89ef62ce7 53 // Disable timer
emilmont 10:3bc89ef62ce7 54 timer->TCR = 0;
emilmont 10:3bc89ef62ce7 55
emilmont 10:3bc89ef62ce7 56 // Power the correspondent timer
emilmont 10:3bc89ef62ce7 57 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
emilmont 10:3bc89ef62ce7 58
emilmont 10:3bc89ef62ce7 59 /* Enable PWM function */
emilmont 10:3bc89ef62ce7 60 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
emilmont 10:3bc89ef62ce7 61
emilmont 10:3bc89ef62ce7 62 /* Reset Functionality on MR3 controlling the PWM period */
emilmont 10:3bc89ef62ce7 63 timer->MCR = 1 << 10;
emilmont 10:3bc89ef62ce7 64
emilmont 10:3bc89ef62ce7 65 // default to 20ms: standard for servos, and fine for e.g. brightness control
emilmont 10:3bc89ef62ce7 66 pwmout_period_ms(obj, 20);
emilmont 10:3bc89ef62ce7 67 pwmout_write (obj, 0);
emilmont 10:3bc89ef62ce7 68
emilmont 10:3bc89ef62ce7 69 // Wire pinout
emilmont 10:3bc89ef62ce7 70 pinmap_pinout(pin, PinMap_PWM);
emilmont 10:3bc89ef62ce7 71 }
emilmont 10:3bc89ef62ce7 72
emilmont 10:3bc89ef62ce7 73 void pwmout_free(pwmout_t* obj) {
emilmont 10:3bc89ef62ce7 74 // [TODO]
emilmont 10:3bc89ef62ce7 75 }
emilmont 10:3bc89ef62ce7 76
emilmont 10:3bc89ef62ce7 77 void pwmout_write(pwmout_t* obj, float value) {
emilmont 10:3bc89ef62ce7 78 if (value < 0.0f) {
emilmont 10:3bc89ef62ce7 79 value = 0.0;
emilmont 10:3bc89ef62ce7 80 } else if (value > 1.0f) {
emilmont 10:3bc89ef62ce7 81 value = 1.0;
emilmont 10:3bc89ef62ce7 82 }
emilmont 10:3bc89ef62ce7 83
emilmont 10:3bc89ef62ce7 84 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 85 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 86 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
emilmont 10:3bc89ef62ce7 87
emilmont 10:3bc89ef62ce7 88 timer->MR[tid.mr] = t_off;
emilmont 10:3bc89ef62ce7 89 }
emilmont 10:3bc89ef62ce7 90
emilmont 10:3bc89ef62ce7 91 float pwmout_read(pwmout_t* obj) {
emilmont 10:3bc89ef62ce7 92 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 93 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 94
emilmont 10:3bc89ef62ce7 95 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
emilmont 10:3bc89ef62ce7 96 return (v > 1.0f) ? (1.0f) : (v);
emilmont 10:3bc89ef62ce7 97 }
emilmont 10:3bc89ef62ce7 98
emilmont 10:3bc89ef62ce7 99 void pwmout_period(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 100 pwmout_period_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 101 }
emilmont 10:3bc89ef62ce7 102
emilmont 10:3bc89ef62ce7 103 void pwmout_period_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 104 pwmout_period_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 105 }
emilmont 10:3bc89ef62ce7 106
emilmont 10:3bc89ef62ce7 107 // Set the PWM period, keeping the duty cycle the same.
emilmont 10:3bc89ef62ce7 108 void pwmout_period_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 109 int i = 0;
mbed_official 63:a46ad637dc84 110 uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
emilmont 10:3bc89ef62ce7 111
emilmont 10:3bc89ef62ce7 112 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 113 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 114 uint32_t old_period_ticks = timer->MR3;
mbed_official 63:a46ad637dc84 115
mbed_official 63:a46ad637dc84 116 // for 16bit timer, set prescaler to avoid overflow
mbed_official 175:906e2386ace8 117 if (timer == LPC_CT16B0 || timer == LPC_CT16B1) {
mbed_official 227:7bd0639b8911 118 uint16_t high_period_ticks = period_ticks >> 16;
mbed_official 63:a46ad637dc84 119 timer->PR = high_period_ticks;
mbed_official 63:a46ad637dc84 120 period_ticks /= (high_period_ticks + 1);
mbed_official 63:a46ad637dc84 121 }
emilmont 10:3bc89ef62ce7 122
emilmont 10:3bc89ef62ce7 123 timer->TCR = TCR_RESET;
emilmont 10:3bc89ef62ce7 124 timer->MR3 = period_ticks;
emilmont 10:3bc89ef62ce7 125
emilmont 10:3bc89ef62ce7 126 // Scale the pulse width to preserve the duty ratio
emilmont 10:3bc89ef62ce7 127 if (old_period_ticks > 0) {
emilmont 10:3bc89ef62ce7 128 for (i=0; i<3; i++) {
emilmont 10:3bc89ef62ce7 129 uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
emilmont 10:3bc89ef62ce7 130 timer->MR[i] = t_off;
emilmont 10:3bc89ef62ce7 131 }
emilmont 10:3bc89ef62ce7 132 }
emilmont 10:3bc89ef62ce7 133 timer->TCR = TCR_CNT_EN;
emilmont 10:3bc89ef62ce7 134 }
emilmont 10:3bc89ef62ce7 135
emilmont 10:3bc89ef62ce7 136 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 137 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 138 }
emilmont 10:3bc89ef62ce7 139
emilmont 10:3bc89ef62ce7 140 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 141 pwmout_pulsewidth_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 142 }
emilmont 10:3bc89ef62ce7 143
emilmont 10:3bc89ef62ce7 144 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 145 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 146 LPC_CTxxBx_Type *timer = Timers[tid.timer];
mbed_official 63:a46ad637dc84 147 uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1));
emilmont 10:3bc89ef62ce7 148
emilmont 10:3bc89ef62ce7 149 timer->TCR = TCR_RESET;
emilmont 10:3bc89ef62ce7 150 if (t_on > timer->MR3) {
emilmont 10:3bc89ef62ce7 151 pwmout_period_us(obj, us);
mbed_official 63:a46ad637dc84 152 t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1));
emilmont 10:3bc89ef62ce7 153 }
emilmont 10:3bc89ef62ce7 154 uint32_t t_off = timer->MR3 - t_on;
emilmont 10:3bc89ef62ce7 155 timer->MR[tid.mr] = t_off;
emilmont 10:3bc89ef62ce7 156 timer->TCR = TCR_CNT_EN;
emilmont 10:3bc89ef62ce7 157 }