mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

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:
bogdanm
Date:
Mon Aug 19 18:17:02 2013 +0300
Revision:
19:398f4c622e1b
Parent:
13:0645d8841f51
Child:
63:a46ad637dc84
Sync with official mbed library release 66

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 */
emilmont 10:3bc89ef62ce7 16 #include "pwmout_api.h"
emilmont 10:3bc89ef62ce7 17 #include "cmsis.h"
emilmont 10:3bc89ef62ce7 18 #include "pinmap.h"
emilmont 10:3bc89ef62ce7 19 #include "error.h"
emilmont 10:3bc89ef62ce7 20
emilmont 10:3bc89ef62ce7 21 #define TCR_CNT_EN 0x00000001
emilmont 10:3bc89ef62ce7 22 #define TCR_RESET 0x00000002
emilmont 10:3bc89ef62ce7 23
emilmont 10:3bc89ef62ce7 24 /* To have a PWM where we can change both the period and the duty cycle,
emilmont 10:3bc89ef62ce7 25 * we need an entire timer. With the following conventions:
emilmont 10:3bc89ef62ce7 26 * * MR3 is used for the PWM period
emilmont 10:3bc89ef62ce7 27 * * MR0, MR1, MR2 are used for the duty cycle
emilmont 10:3bc89ef62ce7 28 */
emilmont 10:3bc89ef62ce7 29 static const PinMap PinMap_PWM[] = {
emilmont 10:3bc89ef62ce7 30 /* CT16B0 */
emilmont 10:3bc89ef62ce7 31 {P0_8 , PWM_1, 2}, {P1_13, PWM_1, 2}, /* MR0 */
emilmont 10:3bc89ef62ce7 32 {P0_9 , PWM_2, 2}, {P1_14, PWM_2, 2}, /* MR1 */
emilmont 10:3bc89ef62ce7 33 {P0_10, PWM_3, 3}, {P1_15, PWM_3, 2}, /* MR2 */
emilmont 10:3bc89ef62ce7 34
emilmont 10:3bc89ef62ce7 35 /* CT16B1 */
emilmont 10:3bc89ef62ce7 36 {P0_21, PWM_4, 1}, /* MR0 */
emilmont 10:3bc89ef62ce7 37 {P0_22, PWM_5, 2}, {P1_23, PWM_5, 1}, /* MR1 */
emilmont 10:3bc89ef62ce7 38
emilmont 10:3bc89ef62ce7 39 /* CT32B0 */
emilmont 10:3bc89ef62ce7 40 {P0_18, PWM_6, 2}, {P1_24, PWM_6, 1}, /* MR0 */
emilmont 10:3bc89ef62ce7 41 {P0_19, PWM_7, 2}, {P1_25, PWM_7, 1}, /* MR1 */
emilmont 10:3bc89ef62ce7 42 {P0_1 , PWM_8, 2}, {P1_26, PWM_8, 1}, /* MR2 */
emilmont 10:3bc89ef62ce7 43
emilmont 10:3bc89ef62ce7 44 /* CT32B1 */
emilmont 10:3bc89ef62ce7 45 {P0_13, PWM_9 , 3}, {P1_0, PWM_9 , 1}, /* MR0 */
emilmont 10:3bc89ef62ce7 46 {P0_14, PWM_10, 3}, {P1_1, PWM_10, 1}, /* MR1 */
emilmont 10:3bc89ef62ce7 47 {P0_15, PWM_11, 3}, {P1_2, PWM_11, 1}, /* MR2 */
emilmont 10:3bc89ef62ce7 48
emilmont 10:3bc89ef62ce7 49 {NC, NC, 0}
emilmont 10:3bc89ef62ce7 50 };
emilmont 10:3bc89ef62ce7 51
emilmont 10:3bc89ef62ce7 52 typedef struct {
emilmont 10:3bc89ef62ce7 53 uint8_t timer;
emilmont 10:3bc89ef62ce7 54 uint8_t mr;
emilmont 10:3bc89ef62ce7 55 } timer_mr;
emilmont 10:3bc89ef62ce7 56
emilmont 10:3bc89ef62ce7 57 static timer_mr pwm_timer_map[11] = {
emilmont 10:3bc89ef62ce7 58 {0, 0}, {0, 1}, {0, 2},
emilmont 10:3bc89ef62ce7 59 {1, 0}, {1, 1},
emilmont 10:3bc89ef62ce7 60 {2, 0}, {2, 1}, {2, 2},
emilmont 10:3bc89ef62ce7 61 {3, 0}, {3, 1}, {3, 2},
emilmont 10:3bc89ef62ce7 62 };
emilmont 10:3bc89ef62ce7 63
emilmont 10:3bc89ef62ce7 64 static LPC_CTxxBx_Type *Timers[4] = {
emilmont 10:3bc89ef62ce7 65 LPC_CT16B0, LPC_CT16B1,
emilmont 10:3bc89ef62ce7 66 LPC_CT32B0, LPC_CT32B1
emilmont 10:3bc89ef62ce7 67 };
emilmont 10:3bc89ef62ce7 68
emilmont 10:3bc89ef62ce7 69 static unsigned int pwm_clock_mhz;
emilmont 10:3bc89ef62ce7 70
emilmont 10:3bc89ef62ce7 71 void pwmout_init(pwmout_t* obj, PinName pin) {
emilmont 10:3bc89ef62ce7 72 // determine the channel
emilmont 10:3bc89ef62ce7 73 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
bogdanm 19:398f4c622e1b 74 if (pwm == (PWMName)NC)
emilmont 10:3bc89ef62ce7 75 error("PwmOut pin mapping failed");
emilmont 10:3bc89ef62ce7 76
emilmont 10:3bc89ef62ce7 77 obj->pwm = pwm;
emilmont 10:3bc89ef62ce7 78
emilmont 10:3bc89ef62ce7 79 // Timer registers
emilmont 10:3bc89ef62ce7 80 timer_mr tid = pwm_timer_map[pwm];
emilmont 10:3bc89ef62ce7 81 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 82
emilmont 10:3bc89ef62ce7 83 // Disable timer
emilmont 10:3bc89ef62ce7 84 timer->TCR = 0;
emilmont 10:3bc89ef62ce7 85
emilmont 10:3bc89ef62ce7 86 // Power the correspondent timer
emilmont 10:3bc89ef62ce7 87 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
emilmont 10:3bc89ef62ce7 88
emilmont 10:3bc89ef62ce7 89 /* Enable PWM function */
emilmont 10:3bc89ef62ce7 90 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
emilmont 10:3bc89ef62ce7 91
emilmont 10:3bc89ef62ce7 92 /* Reset Functionality on MR3 controlling the PWM period */
emilmont 10:3bc89ef62ce7 93 timer->MCR = 1 << 10;
emilmont 10:3bc89ef62ce7 94
emilmont 10:3bc89ef62ce7 95 pwm_clock_mhz = SystemCoreClock / 1000000;
emilmont 10:3bc89ef62ce7 96
emilmont 10:3bc89ef62ce7 97 // default to 20ms: standard for servos, and fine for e.g. brightness control
emilmont 10:3bc89ef62ce7 98 pwmout_period_ms(obj, 20);
emilmont 10:3bc89ef62ce7 99 pwmout_write (obj, 0);
emilmont 10:3bc89ef62ce7 100
emilmont 10:3bc89ef62ce7 101 // Wire pinout
emilmont 10:3bc89ef62ce7 102 pinmap_pinout(pin, PinMap_PWM);
emilmont 10:3bc89ef62ce7 103 }
emilmont 10:3bc89ef62ce7 104
emilmont 10:3bc89ef62ce7 105 void pwmout_free(pwmout_t* obj) {
emilmont 10:3bc89ef62ce7 106 // [TODO]
emilmont 10:3bc89ef62ce7 107 }
emilmont 10:3bc89ef62ce7 108
emilmont 10:3bc89ef62ce7 109 void pwmout_write(pwmout_t* obj, float value) {
emilmont 10:3bc89ef62ce7 110 if (value < 0.0f) {
emilmont 10:3bc89ef62ce7 111 value = 0.0;
emilmont 10:3bc89ef62ce7 112 } else if (value > 1.0f) {
emilmont 10:3bc89ef62ce7 113 value = 1.0;
emilmont 10:3bc89ef62ce7 114 }
emilmont 10:3bc89ef62ce7 115
emilmont 10:3bc89ef62ce7 116 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 117 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 118 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
emilmont 10:3bc89ef62ce7 119
emilmont 10:3bc89ef62ce7 120 timer->TCR = TCR_RESET;
emilmont 10:3bc89ef62ce7 121 timer->MR[tid.mr] = t_off;
emilmont 10:3bc89ef62ce7 122 timer->TCR = TCR_CNT_EN;
emilmont 10:3bc89ef62ce7 123 }
emilmont 10:3bc89ef62ce7 124
emilmont 10:3bc89ef62ce7 125 float pwmout_read(pwmout_t* obj) {
emilmont 10:3bc89ef62ce7 126 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 127 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 128
emilmont 10:3bc89ef62ce7 129 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
emilmont 10:3bc89ef62ce7 130 return (v > 1.0f) ? (1.0f) : (v);
emilmont 10:3bc89ef62ce7 131 }
emilmont 10:3bc89ef62ce7 132
emilmont 10:3bc89ef62ce7 133 void pwmout_period(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 134 pwmout_period_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 135 }
emilmont 10:3bc89ef62ce7 136
emilmont 10:3bc89ef62ce7 137 void pwmout_period_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 138 pwmout_period_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 139 }
emilmont 10:3bc89ef62ce7 140
emilmont 10:3bc89ef62ce7 141 // Set the PWM period, keeping the duty cycle the same.
emilmont 10:3bc89ef62ce7 142 void pwmout_period_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 143 int i = 0;
emilmont 10:3bc89ef62ce7 144 uint32_t period_ticks = pwm_clock_mhz * us;
emilmont 10:3bc89ef62ce7 145
emilmont 10:3bc89ef62ce7 146 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 147 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 148 uint32_t old_period_ticks = timer->MR3;
emilmont 10:3bc89ef62ce7 149
emilmont 10:3bc89ef62ce7 150 timer->TCR = TCR_RESET;
emilmont 10:3bc89ef62ce7 151 timer->MR3 = period_ticks;
emilmont 10:3bc89ef62ce7 152
emilmont 10:3bc89ef62ce7 153 // Scale the pulse width to preserve the duty ratio
emilmont 10:3bc89ef62ce7 154 if (old_period_ticks > 0) {
emilmont 10:3bc89ef62ce7 155 for (i=0; i<3; i++) {
emilmont 10:3bc89ef62ce7 156 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 157 timer->MR[i] = t_off;
emilmont 10:3bc89ef62ce7 158 }
emilmont 10:3bc89ef62ce7 159 }
emilmont 10:3bc89ef62ce7 160 timer->TCR = TCR_CNT_EN;
emilmont 10:3bc89ef62ce7 161 }
emilmont 10:3bc89ef62ce7 162
emilmont 10:3bc89ef62ce7 163 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 164 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 165 }
emilmont 10:3bc89ef62ce7 166
emilmont 10:3bc89ef62ce7 167 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 168 pwmout_pulsewidth_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 169 }
emilmont 10:3bc89ef62ce7 170
emilmont 10:3bc89ef62ce7 171 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 172 uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
emilmont 10:3bc89ef62ce7 173 timer_mr tid = pwm_timer_map[obj->pwm];
emilmont 10:3bc89ef62ce7 174 LPC_CTxxBx_Type *timer = Timers[tid.timer];
emilmont 10:3bc89ef62ce7 175
emilmont 10:3bc89ef62ce7 176 timer->TCR = TCR_RESET;
emilmont 10:3bc89ef62ce7 177 if (t_on > timer->MR3) {
emilmont 10:3bc89ef62ce7 178 pwmout_period_us(obj, us);
emilmont 10:3bc89ef62ce7 179 }
emilmont 10:3bc89ef62ce7 180 uint32_t t_off = timer->MR3 - t_on;
emilmont 10:3bc89ef62ce7 181 timer->MR[tid.mr] = t_off;
emilmont 10:3bc89ef62ce7 182 timer->TCR = TCR_CNT_EN;
emilmont 10:3bc89ef62ce7 183 }