mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

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