mbed library sources for GR-PEACH rev.B.

Fork of mbed-src by mbed official

Committer:
RyoheiHagimoto
Date:
Wed Apr 15 01:34:29 2015 +0000
Revision:
514:cf59050bad8e
Parent:
227:7bd0639b8911
mbed library sources for GR-PEACH rev.B.

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