mbed library sources

Dependents:   Nucleo_blink_led

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Feb 03 13:15:07 2015 +0000
Revision:
462:e03396e14338
Synchronized with git revision ae7e2e76ed57b9ca11dc05f51f097df1de144fe2

Full URL: https://github.com/mbedmicro/mbed/commit/ae7e2e76ed57b9ca11dc05f51f097df1de144fe2/

Add support for EA LPC4088_DM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 462:e03396e14338 1 /* mbed Microcontroller Library
mbed_official 462:e03396e14338 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 462:e03396e14338 3 *
mbed_official 462:e03396e14338 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 462:e03396e14338 5 * you may not use this file except in compliance with the License.
mbed_official 462:e03396e14338 6 * You may obtain a copy of the License at
mbed_official 462:e03396e14338 7 *
mbed_official 462:e03396e14338 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 462:e03396e14338 9 *
mbed_official 462:e03396e14338 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 462:e03396e14338 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 462:e03396e14338 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 462:e03396e14338 13 * See the License for the specific language governing permissions and
mbed_official 462:e03396e14338 14 * limitations under the License.
mbed_official 462:e03396e14338 15 */
mbed_official 462:e03396e14338 16 #include "mbed_assert.h"
mbed_official 462:e03396e14338 17 #include "pwmout_api.h"
mbed_official 462:e03396e14338 18 #include "cmsis.h"
mbed_official 462:e03396e14338 19 #include "pinmap.h"
mbed_official 462:e03396e14338 20
mbed_official 462:e03396e14338 21 #define TCR_CNT_EN 0x00000001
mbed_official 462:e03396e14338 22 #define TCR_RESET 0x00000002
mbed_official 462:e03396e14338 23
mbed_official 462:e03396e14338 24 // PORT ID, PWM ID, Pin function
mbed_official 462:e03396e14338 25 static const PinMap PinMap_PWM[] = {
mbed_official 462:e03396e14338 26 {P1_5, PWM0_3, 3},
mbed_official 462:e03396e14338 27 {P1_20, PWM1_2, 2},
mbed_official 462:e03396e14338 28 {P1_23, PWM1_4, 2},
mbed_official 462:e03396e14338 29 {P1_24, PWM1_5, 2},
mbed_official 462:e03396e14338 30 {NC, NC, 0}
mbed_official 462:e03396e14338 31 };
mbed_official 462:e03396e14338 32
mbed_official 462:e03396e14338 33 static const uint32_t PWM_mr_offset[7] = {
mbed_official 462:e03396e14338 34 0x18, 0x1C, 0x20, 0x24, 0x40, 0x44, 0x48
mbed_official 462:e03396e14338 35 };
mbed_official 462:e03396e14338 36
mbed_official 462:e03396e14338 37 #define TCR_PWM_EN 0x00000008
mbed_official 462:e03396e14338 38 static unsigned int pwm_clock_mhz;
mbed_official 462:e03396e14338 39
mbed_official 462:e03396e14338 40 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 462:e03396e14338 41 // determine the channel
mbed_official 462:e03396e14338 42 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 462:e03396e14338 43 MBED_ASSERT(pwm != (PWMName)NC);
mbed_official 462:e03396e14338 44
mbed_official 462:e03396e14338 45 obj->channel = pwm;
mbed_official 462:e03396e14338 46 obj->pwm = LPC_PWM0;
mbed_official 462:e03396e14338 47
mbed_official 462:e03396e14338 48 if (obj->channel > 6) { // PWM1 is used if pwm > 6
mbed_official 462:e03396e14338 49 obj->channel -= 6;
mbed_official 462:e03396e14338 50 obj->pwm = LPC_PWM1;
mbed_official 462:e03396e14338 51 }
mbed_official 462:e03396e14338 52
mbed_official 462:e03396e14338 53 obj->MR = (__IO uint32_t *)((uint32_t)obj->pwm + PWM_mr_offset[obj->channel]);
mbed_official 462:e03396e14338 54
mbed_official 462:e03396e14338 55 // ensure the power is on
mbed_official 462:e03396e14338 56 if (obj->pwm == LPC_PWM0) {
mbed_official 462:e03396e14338 57 LPC_SC->PCONP |= 1 << 5;
mbed_official 462:e03396e14338 58 } else {
mbed_official 462:e03396e14338 59 LPC_SC->PCONP |= 1 << 6;
mbed_official 462:e03396e14338 60 }
mbed_official 462:e03396e14338 61
mbed_official 462:e03396e14338 62 obj->pwm->PR = 0; // no pre-scale
mbed_official 462:e03396e14338 63
mbed_official 462:e03396e14338 64 // ensure single PWM mode
mbed_official 462:e03396e14338 65 obj->pwm->MCR = 1 << 1; // reset TC on match 0
mbed_official 462:e03396e14338 66
mbed_official 462:e03396e14338 67 // enable the specific PWM output
mbed_official 462:e03396e14338 68 obj->pwm->PCR |= 1 << (8 + obj->channel);
mbed_official 462:e03396e14338 69
mbed_official 462:e03396e14338 70 pwm_clock_mhz = PeripheralClock / 1000000;
mbed_official 462:e03396e14338 71
mbed_official 462:e03396e14338 72 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 462:e03396e14338 73 pwmout_period_ms(obj, 20);
mbed_official 462:e03396e14338 74 pwmout_write (obj, 0);
mbed_official 462:e03396e14338 75
mbed_official 462:e03396e14338 76 // Wire pinout
mbed_official 462:e03396e14338 77 pinmap_pinout(pin, PinMap_PWM);
mbed_official 462:e03396e14338 78 }
mbed_official 462:e03396e14338 79
mbed_official 462:e03396e14338 80 void pwmout_free(pwmout_t* obj) {
mbed_official 462:e03396e14338 81 // [TODO]
mbed_official 462:e03396e14338 82 }
mbed_official 462:e03396e14338 83
mbed_official 462:e03396e14338 84 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 462:e03396e14338 85 if (value < 0.0f) {
mbed_official 462:e03396e14338 86 value = 0.0;
mbed_official 462:e03396e14338 87 } else if (value > 1.0f) {
mbed_official 462:e03396e14338 88 value = 1.0;
mbed_official 462:e03396e14338 89 }
mbed_official 462:e03396e14338 90
mbed_official 462:e03396e14338 91 // set channel match to percentage
mbed_official 462:e03396e14338 92 uint32_t v = (uint32_t)((float)(obj->pwm->MR0) * value);
mbed_official 462:e03396e14338 93
mbed_official 462:e03396e14338 94 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
mbed_official 462:e03396e14338 95 if (v == obj->pwm->MR0) {
mbed_official 462:e03396e14338 96 v++;
mbed_official 462:e03396e14338 97 }
mbed_official 462:e03396e14338 98
mbed_official 462:e03396e14338 99 *obj->MR = v;
mbed_official 462:e03396e14338 100
mbed_official 462:e03396e14338 101 // accept on next period start
mbed_official 462:e03396e14338 102 obj->pwm->LER |= 1 << obj->channel;
mbed_official 462:e03396e14338 103 }
mbed_official 462:e03396e14338 104
mbed_official 462:e03396e14338 105 float pwmout_read(pwmout_t* obj) {
mbed_official 462:e03396e14338 106 float v = (float)(*obj->MR) / (float)(obj->pwm->MR0);
mbed_official 462:e03396e14338 107 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 462:e03396e14338 108 }
mbed_official 462:e03396e14338 109
mbed_official 462:e03396e14338 110 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 462:e03396e14338 111 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 462:e03396e14338 112 }
mbed_official 462:e03396e14338 113
mbed_official 462:e03396e14338 114 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 462:e03396e14338 115 pwmout_period_us(obj, ms * 1000);
mbed_official 462:e03396e14338 116 }
mbed_official 462:e03396e14338 117
mbed_official 462:e03396e14338 118 // Set the PWM period, keeping the duty cycle the same.
mbed_official 462:e03396e14338 119 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 462:e03396e14338 120 // calculate number of ticks
mbed_official 462:e03396e14338 121 uint32_t ticks = pwm_clock_mhz * us;
mbed_official 462:e03396e14338 122
mbed_official 462:e03396e14338 123 // set reset
mbed_official 462:e03396e14338 124 obj->pwm->TCR = TCR_RESET;
mbed_official 462:e03396e14338 125
mbed_official 462:e03396e14338 126 // set the global match register
mbed_official 462:e03396e14338 127 obj->pwm->MR0 = ticks;
mbed_official 462:e03396e14338 128
mbed_official 462:e03396e14338 129 // Scale the pulse width to preserve the duty ratio
mbed_official 462:e03396e14338 130 if (obj->pwm->MR0 > 0) {
mbed_official 462:e03396e14338 131 *obj->MR = (*obj->MR * ticks) / obj->pwm->MR0;
mbed_official 462:e03396e14338 132 }
mbed_official 462:e03396e14338 133
mbed_official 462:e03396e14338 134 // set the channel latch to update value at next period start
mbed_official 462:e03396e14338 135 obj->pwm->LER |= 1 << 0;
mbed_official 462:e03396e14338 136
mbed_official 462:e03396e14338 137 // enable counter and pwm, clear reset
mbed_official 462:e03396e14338 138 obj->pwm->TCR = TCR_CNT_EN | TCR_PWM_EN;
mbed_official 462:e03396e14338 139 }
mbed_official 462:e03396e14338 140
mbed_official 462:e03396e14338 141 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 462:e03396e14338 142 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 462:e03396e14338 143 }
mbed_official 462:e03396e14338 144
mbed_official 462:e03396e14338 145 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 462:e03396e14338 146 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 462:e03396e14338 147 }
mbed_official 462:e03396e14338 148
mbed_official 462:e03396e14338 149 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 462:e03396e14338 150 // calculate number of ticks
mbed_official 462:e03396e14338 151 uint32_t v = pwm_clock_mhz * us;
mbed_official 462:e03396e14338 152
mbed_official 462:e03396e14338 153 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
mbed_official 462:e03396e14338 154 if (v == obj->pwm->MR0) {
mbed_official 462:e03396e14338 155 v++;
mbed_official 462:e03396e14338 156 }
mbed_official 462:e03396e14338 157
mbed_official 462:e03396e14338 158 // set the match register value
mbed_official 462:e03396e14338 159 *obj->MR = v;
mbed_official 462:e03396e14338 160
mbed_official 462:e03396e14338 161 // set the channel latch to update value at next period start
mbed_official 462:e03396e14338 162 obj->pwm->LER |= 1 << obj->channel;
mbed_official 462:e03396e14338 163 }