mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Sep 30 20:45:04 2013 +0100
Revision:
32:4742f6c694e8
Parent:
targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11XX/pwmout_api.c@30:91c1d09ada54
Synchronized with git revision e8afb9cd1deecf3a0ef2c140d38e3f2d68a2eb50

Who changed what in which revision?

UserRevisionLine numberNew 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 30:91c1d09ada54 16 #include "pwmout_api.h"
mbed_official 30:91c1d09ada54 17 #include "cmsis.h"
mbed_official 30:91c1d09ada54 18 #include "pinmap.h"
mbed_official 30:91c1d09ada54 19 #include "error.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 30:91c1d09ada54 67 if (pwm == (uint32_t)NC)
mbed_official 30:91c1d09ada54 68 error("PwmOut pin mapping failed");
mbed_official 30:91c1d09ada54 69
mbed_official 30:91c1d09ada54 70 obj->pwm = pwm;
mbed_official 30:91c1d09ada54 71
mbed_official 30:91c1d09ada54 72 // Timer registers
mbed_official 30:91c1d09ada54 73 timer_mr tid = pwm_timer_map[pwm];
mbed_official 30:91c1d09ada54 74 LPC_TMR_TypeDef *timer = Timers[tid.timer];
mbed_official 30:91c1d09ada54 75
mbed_official 30:91c1d09ada54 76 // Disable timer
mbed_official 30:91c1d09ada54 77 timer->TCR = 0;
mbed_official 30:91c1d09ada54 78
mbed_official 30:91c1d09ada54 79 // Power the correspondent timer
mbed_official 30:91c1d09ada54 80 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
mbed_official 30:91c1d09ada54 81
mbed_official 30:91c1d09ada54 82 /* Enable PWM function */
mbed_official 30:91c1d09ada54 83 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
mbed_official 30:91c1d09ada54 84
mbed_official 30:91c1d09ada54 85 /* Reset Functionality on MR3 controlling the PWM period */
mbed_official 30:91c1d09ada54 86 timer->MCR = 1 << 10;
mbed_official 30:91c1d09ada54 87
mbed_official 30:91c1d09ada54 88 if (timer == LPC_TMR16B0 || timer == LPC_TMR16B1) {
mbed_official 30:91c1d09ada54 89 /* Set 16-bit timer prescaler to avoid timer expire for default 20ms */
mbed_official 30:91c1d09ada54 90 /* This can be also modified by user application, but the prescaler value */
mbed_official 30:91c1d09ada54 91 /* might be trade-off to timer accuracy */
mbed_official 30:91c1d09ada54 92 timer->PR = 30;
mbed_official 30:91c1d09ada54 93 }
mbed_official 30:91c1d09ada54 94
mbed_official 30:91c1d09ada54 95 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 30:91c1d09ada54 96 pwmout_period_ms(obj, 20);
mbed_official 30:91c1d09ada54 97 pwmout_write (obj, 0);
mbed_official 30:91c1d09ada54 98
mbed_official 30:91c1d09ada54 99 // Wire pinout
mbed_official 30:91c1d09ada54 100 pinmap_pinout(pin, PinMap_PWM);
mbed_official 30:91c1d09ada54 101 }
mbed_official 30:91c1d09ada54 102
mbed_official 30:91c1d09ada54 103 void pwmout_free(pwmout_t* obj) {
mbed_official 30:91c1d09ada54 104 // [TODO]
mbed_official 30:91c1d09ada54 105 }
mbed_official 30:91c1d09ada54 106
mbed_official 30:91c1d09ada54 107 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 30:91c1d09ada54 108 if (value < 0.0f) {
mbed_official 30:91c1d09ada54 109 value = 0.0;
mbed_official 30:91c1d09ada54 110 } else if (value > 1.0f) {
mbed_official 30:91c1d09ada54 111 value = 1.0;
mbed_official 30:91c1d09ada54 112 }
mbed_official 30:91c1d09ada54 113
mbed_official 30:91c1d09ada54 114 timer_mr tid = pwm_timer_map[obj->pwm];
mbed_official 30:91c1d09ada54 115 LPC_TMR_TypeDef *timer = Timers[tid.timer];
mbed_official 30:91c1d09ada54 116 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
mbed_official 30:91c1d09ada54 117
mbed_official 30:91c1d09ada54 118 timer->TCR = TCR_RESET;
mbed_official 30:91c1d09ada54 119 timer->MR[tid.mr] = t_off;
mbed_official 30:91c1d09ada54 120 timer->TCR = TCR_CNT_EN;
mbed_official 30:91c1d09ada54 121 }
mbed_official 30:91c1d09ada54 122
mbed_official 30:91c1d09ada54 123 float pwmout_read(pwmout_t* obj) {
mbed_official 30:91c1d09ada54 124 timer_mr tid = pwm_timer_map[obj->pwm];
mbed_official 30:91c1d09ada54 125 LPC_TMR_TypeDef *timer = Timers[tid.timer];
mbed_official 30:91c1d09ada54 126
mbed_official 30:91c1d09ada54 127 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
mbed_official 30:91c1d09ada54 128 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 30:91c1d09ada54 129 }
mbed_official 30:91c1d09ada54 130
mbed_official 30:91c1d09ada54 131 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 30:91c1d09ada54 132 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 30:91c1d09ada54 133 }
mbed_official 30:91c1d09ada54 134
mbed_official 30:91c1d09ada54 135 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 30:91c1d09ada54 136 pwmout_period_us(obj, ms * 1000);
mbed_official 30:91c1d09ada54 137 }
mbed_official 30:91c1d09ada54 138
mbed_official 30:91c1d09ada54 139 // Set the PWM period, keeping the duty cycle the same.
mbed_official 30:91c1d09ada54 140 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 30:91c1d09ada54 141 int i = 0;
mbed_official 30:91c1d09ada54 142 uint32_t period_ticks;
mbed_official 30:91c1d09ada54 143
mbed_official 30:91c1d09ada54 144 timer_mr tid = pwm_timer_map[obj->pwm];
mbed_official 30:91c1d09ada54 145 LPC_TMR_TypeDef *timer = Timers[tid.timer];
mbed_official 30:91c1d09ada54 146 uint32_t old_period_ticks = timer->MR3;
mbed_official 30:91c1d09ada54 147 period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1);
mbed_official 30:91c1d09ada54 148
mbed_official 30:91c1d09ada54 149 timer->TCR = TCR_RESET;
mbed_official 30:91c1d09ada54 150 timer->MR3 = period_ticks;
mbed_official 30:91c1d09ada54 151
mbed_official 30:91c1d09ada54 152 // Scale the pulse width to preserve the duty ratio
mbed_official 30:91c1d09ada54 153 if (old_period_ticks > 0) {
mbed_official 30:91c1d09ada54 154 for (i=0; i<3; i++) {
mbed_official 30:91c1d09ada54 155 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 156 timer->MR[i] = t_off;
mbed_official 30:91c1d09ada54 157 }
mbed_official 30:91c1d09ada54 158 }
mbed_official 30:91c1d09ada54 159 timer->TCR = TCR_CNT_EN;
mbed_official 30:91c1d09ada54 160 }
mbed_official 30:91c1d09ada54 161
mbed_official 30:91c1d09ada54 162 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 30:91c1d09ada54 163 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 30:91c1d09ada54 164 }
mbed_official 30:91c1d09ada54 165
mbed_official 30:91c1d09ada54 166 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 30:91c1d09ada54 167 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 30:91c1d09ada54 168 }
mbed_official 30:91c1d09ada54 169
mbed_official 30:91c1d09ada54 170 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 30:91c1d09ada54 171 timer_mr tid = pwm_timer_map[obj->pwm];
mbed_official 30:91c1d09ada54 172 LPC_TMR_TypeDef *timer = Timers[tid.timer];
mbed_official 30:91c1d09ada54 173 uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1));
mbed_official 30:91c1d09ada54 174
mbed_official 30:91c1d09ada54 175 timer->TCR = TCR_RESET;
mbed_official 30:91c1d09ada54 176 if (t_on > timer->MR3) {
mbed_official 30:91c1d09ada54 177 pwmout_period_us(obj, us);
mbed_official 30:91c1d09ada54 178 }
mbed_official 30:91c1d09ada54 179 uint32_t t_off = timer->MR3 - t_on;
mbed_official 30:91c1d09ada54 180 timer->MR[tid.mr] = t_off;
mbed_official 30:91c1d09ada54 181 timer->TCR = TCR_CNT_EN;
mbed_official 30:91c1d09ada54 182 }