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 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Child:
184:8b5ae2e87659
Update mbed sources to revision 64

Who changed what in which revision?

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