mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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