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-2015 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 // PORT ID, PWM ID, Pin function
elessair 0:f269e3021894 25 static const PinMap PinMap_PWM[] = {
elessair 0:f269e3021894 26 {P1_18, PWM_1, 2},
elessair 0:f269e3021894 27 {P1_20, PWM_2, 2},
elessair 0:f269e3021894 28 {P1_21, PWM_3, 2},
elessair 0:f269e3021894 29 {P1_23, PWM_4, 2},
elessair 0:f269e3021894 30 {P1_24, PWM_5, 2},
elessair 0:f269e3021894 31 {P1_26, PWM_6, 2},
elessair 0:f269e3021894 32 {P2_0 , PWM_1, 1},
elessair 0:f269e3021894 33 {P2_1 , PWM_2, 1},
elessair 0:f269e3021894 34 {P2_2 , PWM_3, 1},
elessair 0:f269e3021894 35 {P2_3 , PWM_4, 1},
elessair 0:f269e3021894 36 {P2_4 , PWM_5, 1},
elessair 0:f269e3021894 37 {P2_5 , PWM_6, 1},
elessair 0:f269e3021894 38 {P3_24, PWM_1, 3},
elessair 0:f269e3021894 39 {P3_25, PWM_2, 3},
elessair 0:f269e3021894 40 {P3_26, PWM_3, 3},
elessair 0:f269e3021894 41 {P3_27, PWM_4, 3},
elessair 0:f269e3021894 42 {P3_28, PWM_5, 3},
elessair 0:f269e3021894 43 {P3_29, PWM_6, 3},
elessair 0:f269e3021894 44 {NC, NC, 0}
elessair 0:f269e3021894 45 };
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 __IO uint32_t *PWM_MATCH[] = {
elessair 0:f269e3021894 48 &(LPC_PWM1->MR0),
elessair 0:f269e3021894 49 &(LPC_PWM1->MR1),
elessair 0:f269e3021894 50 &(LPC_PWM1->MR2),
elessair 0:f269e3021894 51 &(LPC_PWM1->MR3),
elessair 0:f269e3021894 52 &(LPC_PWM1->MR4),
elessair 0:f269e3021894 53 &(LPC_PWM1->MR5),
elessair 0:f269e3021894 54 &(LPC_PWM1->MR6)
elessair 0:f269e3021894 55 };
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 #define TCR_PWM_EN 0x00000008
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 static unsigned int pwm_clock_mhz;
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 void pwmout_init(pwmout_t* obj, PinName pin) {
elessair 0:f269e3021894 62 // determine the channel
elessair 0:f269e3021894 63 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
elessair 0:f269e3021894 64 MBED_ASSERT(pwm != (PWMName)NC);
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 obj->pwm = pwm;
elessair 0:f269e3021894 67 obj->MR = PWM_MATCH[pwm];
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 // ensure the power is on
elessair 0:f269e3021894 70 LPC_SC->PCONP |= 1 << 6;
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 // ensure clock to /4
elessair 0:f269e3021894 73 LPC_SC->PCLKSEL0 &= ~(0x3 << 12); // pclk = /4
elessair 0:f269e3021894 74 LPC_PWM1->PR = 0; // no pre-scale
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 // ensure single PWM mode
elessair 0:f269e3021894 77 LPC_PWM1->MCR = 1 << 1; // reset TC on match 0
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 // enable the specific PWM output
elessair 0:f269e3021894 80 LPC_PWM1->PCR |= 1 << (8 + pwm);
elessair 0:f269e3021894 81
elessair 0:f269e3021894 82 pwm_clock_mhz = SystemCoreClock / 4000000;
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 // default to 20ms: standard for servos, and fine for e.g. brightness control
elessair 0:f269e3021894 85 pwmout_period_ms(obj, 20);
elessair 0:f269e3021894 86 pwmout_write (obj, 0);
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 // Wire pinout
elessair 0:f269e3021894 89 pinmap_pinout(pin, PinMap_PWM);
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 void pwmout_free(pwmout_t* obj) {
elessair 0:f269e3021894 93 // [TODO]
elessair 0:f269e3021894 94 }
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 void pwmout_write(pwmout_t* obj, float value) {
elessair 0:f269e3021894 97 if (value < 0.0f) {
elessair 0:f269e3021894 98 value = 0.0;
elessair 0:f269e3021894 99 } else if (value > 1.0f) {
elessair 0:f269e3021894 100 value = 1.0;
elessair 0:f269e3021894 101 }
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 // set channel match to percentage
elessair 0:f269e3021894 104 uint32_t v = (uint32_t)((float)(LPC_PWM1->MR0) * value);
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
elessair 0:f269e3021894 107 if (v == LPC_PWM1->MR0) {
elessair 0:f269e3021894 108 v++;
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 *obj->MR = v;
elessair 0:f269e3021894 112
elessair 0:f269e3021894 113 // accept on next period start
elessair 0:f269e3021894 114 LPC_PWM1->LER |= 1 << obj->pwm;
elessair 0:f269e3021894 115 }
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 float pwmout_read(pwmout_t* obj) {
elessair 0:f269e3021894 118 float v = (float)(*obj->MR) / (float)(LPC_PWM1->MR0);
elessair 0:f269e3021894 119 return (v > 1.0f) ? (1.0f) : (v);
elessair 0:f269e3021894 120 }
elessair 0:f269e3021894 121
elessair 0:f269e3021894 122 void pwmout_period(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 123 pwmout_period_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 124 }
elessair 0:f269e3021894 125
elessair 0:f269e3021894 126 void pwmout_period_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 127 pwmout_period_us(obj, ms * 1000);
elessair 0:f269e3021894 128 }
elessair 0:f269e3021894 129
elessair 0:f269e3021894 130 // Set the PWM period, keeping the duty cycle the same.
elessair 0:f269e3021894 131 void pwmout_period_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 132 // calculate number of ticks
elessair 0:f269e3021894 133 uint32_t ticks = pwm_clock_mhz * us;
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 // set reset
elessair 0:f269e3021894 136 LPC_PWM1->TCR = TCR_RESET;
elessair 0:f269e3021894 137
elessair 0:f269e3021894 138 // set the global match register
elessair 0:f269e3021894 139 LPC_PWM1->MR0 = ticks;
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 // Scale the pulse width to preserve the duty ratio
elessair 0:f269e3021894 142 if (LPC_PWM1->MR0 > 0) {
elessair 0:f269e3021894 143 *obj->MR = (*obj->MR * ticks) / LPC_PWM1->MR0;
elessair 0:f269e3021894 144 }
elessair 0:f269e3021894 145
elessair 0:f269e3021894 146 // set the channel latch to update value at next period start
elessair 0:f269e3021894 147 LPC_PWM1->LER |= 1 << 0;
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 // enable counter and pwm, clear reset
elessair 0:f269e3021894 150 LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN;
elessair 0:f269e3021894 151 }
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 154 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 155 }
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 158 pwmout_pulsewidth_us(obj, ms * 1000);
elessair 0:f269e3021894 159 }
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 162 // calculate number of ticks
elessair 0:f269e3021894 163 uint32_t v = pwm_clock_mhz * us;
elessair 0:f269e3021894 164
elessair 0:f269e3021894 165 // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
elessair 0:f269e3021894 166 if (v == LPC_PWM1->MR0) {
elessair 0:f269e3021894 167 v++;
elessair 0:f269e3021894 168 }
elessair 0:f269e3021894 169
elessair 0:f269e3021894 170 // set the match register value
elessair 0:f269e3021894 171 *obj->MR = v;
elessair 0:f269e3021894 172
elessair 0:f269e3021894 173 // set the channel latch to update value at next period start
elessair 0:f269e3021894 174 LPC_PWM1->LER |= 1 << obj->pwm;
elessair 0:f269e3021894 175 }