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_2, PWM0_1, 3},
mbed_official 462:e03396e14338 27 {P1_3, PWM0_2, 3},
mbed_official 462:e03396e14338 28 {P1_5, PWM0_3, 3},
mbed_official 462:e03396e14338 29 {P1_6, PWM0_4, 3},
mbed_official 462:e03396e14338 30 {P1_7, PWM0_5, 3},
mbed_official 462:e03396e14338 31 {P1_11, PWM0_6, 3},
mbed_official 462:e03396e14338 32 {P1_18, PWM1_1, 2},
mbed_official 462:e03396e14338 33 {P1_20, PWM1_2, 2},
mbed_official 462:e03396e14338 34 {P1_21, PWM1_3, 2},
mbed_official 462:e03396e14338 35 {P1_23, PWM1_4, 2},
mbed_official 462:e03396e14338 36 {P1_24, PWM1_5, 2},
mbed_official 462:e03396e14338 37 {P1_26, PWM1_6, 2},
mbed_official 462:e03396e14338 38 {P2_0, PWM1_1, 1},
mbed_official 462:e03396e14338 39 {P2_1, PWM1_2, 1},
mbed_official 462:e03396e14338 40 {P2_2, PWM1_3, 1},
mbed_official 462:e03396e14338 41 {P2_3, PWM1_4, 1},
mbed_official 462:e03396e14338 42 {P2_4, PWM1_5, 1},
mbed_official 462:e03396e14338 43 {P2_5, PWM1_6, 1},
mbed_official 462:e03396e14338 44 {P3_16, PWM0_1, 2},
mbed_official 462:e03396e14338 45 {P3_17, PWM0_2, 2},
mbed_official 462:e03396e14338 46 {P3_18, PWM0_3, 2},
mbed_official 462:e03396e14338 47 {P3_19, PWM0_4, 2},
mbed_official 462:e03396e14338 48 {P3_20, PWM0_5, 2},
mbed_official 462:e03396e14338 49 {P3_21, PWM0_6, 2},
mbed_official 462:e03396e14338 50 {P3_24, PWM1_1, 2},
mbed_official 462:e03396e14338 51 {P3_25, PWM1_2, 2},
mbed_official 462:e03396e14338 52 {P3_26, PWM1_3, 2},
mbed_official 462:e03396e14338 53 {P3_27, PWM1_4, 2},
mbed_official 462:e03396e14338 54 {P3_28, PWM1_5, 2},
mbed_official 462:e03396e14338 55 {P3_29, PWM1_6, 2},
mbed_official 462:e03396e14338 56 {NC, NC, 0}
mbed_official 462:e03396e14338 57 };
mbed_official 462:e03396e14338 58
mbed_official 462:e03396e14338 59 static const uint32_t PWM_mr_offset[7] = {
mbed_official 462:e03396e14338 60 0x18, 0x1C, 0x20, 0x24, 0x40, 0x44, 0x48
mbed_official 462:e03396e14338 61 };
mbed_official 462:e03396e14338 62
mbed_official 462:e03396e14338 63 #define TCR_PWM_EN 0x00000008
mbed_official 462:e03396e14338 64 static unsigned int pwm_clock_mhz;
mbed_official 462:e03396e14338 65
mbed_official 462:e03396e14338 66 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 462:e03396e14338 67 // determine the channel
mbed_official 462:e03396e14338 68 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 462:e03396e14338 69 MBED_ASSERT(pwm != (PWMName)NC);
mbed_official 462:e03396e14338 70
mbed_official 462:e03396e14338 71 obj->channel = pwm;
mbed_official 462:e03396e14338 72 obj->pwm = LPC_PWM0;
mbed_official 462:e03396e14338 73
mbed_official 462:e03396e14338 74 if (obj->channel > 6) { // PWM1 is used if pwm > 6
mbed_official 462:e03396e14338 75 obj->channel -= 6;
mbed_official 462:e03396e14338 76 obj->pwm = LPC_PWM1;
mbed_official 462:e03396e14338 77 }
mbed_official 462:e03396e14338 78
mbed_official 462:e03396e14338 79 obj->MR = (__IO uint32_t *)((uint32_t)obj->pwm + PWM_mr_offset[obj->channel]);
mbed_official 462:e03396e14338 80
mbed_official 462:e03396e14338 81 // ensure the power is on
mbed_official 462:e03396e14338 82 if (obj->pwm == LPC_PWM0) {
mbed_official 462:e03396e14338 83 LPC_SC->PCONP |= 1 << 5;
mbed_official 462:e03396e14338 84 } else {
mbed_official 462:e03396e14338 85 LPC_SC->PCONP |= 1 << 6;
mbed_official 462:e03396e14338 86 }
mbed_official 462:e03396e14338 87
mbed_official 462:e03396e14338 88 obj->pwm->PR = 0; // no pre-scale
mbed_official 462:e03396e14338 89
mbed_official 462:e03396e14338 90 // ensure single PWM mode
mbed_official 462:e03396e14338 91 obj->pwm->MCR = 1 << 1; // reset TC on match 0
mbed_official 462:e03396e14338 92
mbed_official 462:e03396e14338 93 // enable the specific PWM output
mbed_official 462:e03396e14338 94 obj->pwm->PCR |= 1 << (8 + obj->channel);
mbed_official 462:e03396e14338 95
mbed_official 462:e03396e14338 96 pwm_clock_mhz = PeripheralClock / 1000000;
mbed_official 462:e03396e14338 97
mbed_official 462:e03396e14338 98 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 462:e03396e14338 99 pwmout_period_ms(obj, 20);
mbed_official 462:e03396e14338 100 pwmout_write (obj, 0);
mbed_official 462:e03396e14338 101
mbed_official 462:e03396e14338 102 // Wire pinout
mbed_official 462:e03396e14338 103 pinmap_pinout(pin, PinMap_PWM);
mbed_official 462:e03396e14338 104 }
mbed_official 462:e03396e14338 105
mbed_official 462:e03396e14338 106 void pwmout_free(pwmout_t* obj) {
mbed_official 462:e03396e14338 107 // [TODO]
mbed_official 462:e03396e14338 108 }
mbed_official 462:e03396e14338 109
mbed_official 462:e03396e14338 110 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 462:e03396e14338 111 if (value < 0.0f) {
mbed_official 462:e03396e14338 112 value = 0.0;
mbed_official 462:e03396e14338 113 } else if (value > 1.0f) {
mbed_official 462:e03396e14338 114 value = 1.0;
mbed_official 462:e03396e14338 115 }
mbed_official 462:e03396e14338 116
mbed_official 462:e03396e14338 117 // set channel match to percentage
mbed_official 462:e03396e14338 118 uint32_t v = (uint32_t)((float)(obj->pwm->MR0) * value);
mbed_official 462:e03396e14338 119
mbed_official 462:e03396e14338 120 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
mbed_official 462:e03396e14338 121 if (v == obj->pwm->MR0) {
mbed_official 462:e03396e14338 122 v++;
mbed_official 462:e03396e14338 123 }
mbed_official 462:e03396e14338 124
mbed_official 462:e03396e14338 125 *obj->MR = v;
mbed_official 462:e03396e14338 126
mbed_official 462:e03396e14338 127 // accept on next period start
mbed_official 462:e03396e14338 128 obj->pwm->LER |= 1 << obj->channel;
mbed_official 462:e03396e14338 129 }
mbed_official 462:e03396e14338 130
mbed_official 462:e03396e14338 131 float pwmout_read(pwmout_t* obj) {
mbed_official 462:e03396e14338 132 float v = (float)(*obj->MR) / (float)(obj->pwm->MR0);
mbed_official 462:e03396e14338 133 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 462:e03396e14338 134 }
mbed_official 462:e03396e14338 135
mbed_official 462:e03396e14338 136 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 462:e03396e14338 137 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 462:e03396e14338 138 }
mbed_official 462:e03396e14338 139
mbed_official 462:e03396e14338 140 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 462:e03396e14338 141 pwmout_period_us(obj, ms * 1000);
mbed_official 462:e03396e14338 142 }
mbed_official 462:e03396e14338 143
mbed_official 462:e03396e14338 144 // Set the PWM period, keeping the duty cycle the same.
mbed_official 462:e03396e14338 145 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 462:e03396e14338 146 // calculate number of ticks
mbed_official 462:e03396e14338 147 uint32_t ticks = pwm_clock_mhz * us;
mbed_official 462:e03396e14338 148
mbed_official 462:e03396e14338 149 // set reset
mbed_official 462:e03396e14338 150 obj->pwm->TCR = TCR_RESET;
mbed_official 462:e03396e14338 151
mbed_official 462:e03396e14338 152 // set the global match register
mbed_official 462:e03396e14338 153 obj->pwm->MR0 = ticks;
mbed_official 462:e03396e14338 154
mbed_official 462:e03396e14338 155 // Scale the pulse width to preserve the duty ratio
mbed_official 462:e03396e14338 156 if (obj->pwm->MR0 > 0) {
mbed_official 462:e03396e14338 157 *obj->MR = (*obj->MR * ticks) / obj->pwm->MR0;
mbed_official 462:e03396e14338 158 }
mbed_official 462:e03396e14338 159
mbed_official 462:e03396e14338 160 // set the channel latch to update value at next period start
mbed_official 462:e03396e14338 161 obj->pwm->LER |= 1 << 0;
mbed_official 462:e03396e14338 162
mbed_official 462:e03396e14338 163 // enable counter and pwm, clear reset
mbed_official 462:e03396e14338 164 obj->pwm->TCR = TCR_CNT_EN | TCR_PWM_EN;
mbed_official 462:e03396e14338 165 }
mbed_official 462:e03396e14338 166
mbed_official 462:e03396e14338 167 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 462:e03396e14338 168 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 462:e03396e14338 169 }
mbed_official 462:e03396e14338 170
mbed_official 462:e03396e14338 171 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 462:e03396e14338 172 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 462:e03396e14338 173 }
mbed_official 462:e03396e14338 174
mbed_official 462:e03396e14338 175 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 462:e03396e14338 176 // calculate number of ticks
mbed_official 462:e03396e14338 177 uint32_t v = pwm_clock_mhz * us;
mbed_official 462:e03396e14338 178
mbed_official 462:e03396e14338 179 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
mbed_official 462:e03396e14338 180 if (v == obj->pwm->MR0) {
mbed_official 462:e03396e14338 181 v++;
mbed_official 462:e03396e14338 182 }
mbed_official 462:e03396e14338 183
mbed_official 462:e03396e14338 184 // set the match register value
mbed_official 462:e03396e14338 185 *obj->MR = v;
mbed_official 462:e03396e14338 186
mbed_official 462:e03396e14338 187 // set the channel latch to update value at next period start
mbed_official 462:e03396e14338 188 obj->pwm->LER |= 1 << obj->channel;
mbed_official 462:e03396e14338 189 }