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 #include "mbed_error.h"
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 static LPC_SCT0_Type *SCTs[4] = {
elessair 0:f269e3021894 23 (LPC_SCT0_Type*)LPC_SCT0,
elessair 0:f269e3021894 24 (LPC_SCT0_Type*)LPC_SCT1,
elessair 0:f269e3021894 25 (LPC_SCT0_Type*)LPC_SCT2,
elessair 0:f269e3021894 26 (LPC_SCT0_Type*)LPC_SCT3,
elessair 0:f269e3021894 27 };
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 // bit flags for used SCTs
elessair 0:f269e3021894 30 static unsigned char sct_used = (1 << 3);
elessair 0:f269e3021894 31 static int get_available_sct(void) {
elessair 0:f269e3021894 32 int i;
elessair 0:f269e3021894 33 for (i=0; i<4; i++) {
elessair 0:f269e3021894 34 if ((sct_used & (1 << i)) == 0)
elessair 0:f269e3021894 35 return i;
elessair 0:f269e3021894 36 }
elessair 0:f269e3021894 37 return -1;
elessair 0:f269e3021894 38 }
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 void pwmout_init(pwmout_t* obj, PinName pin) {
elessair 0:f269e3021894 41 MBED_ASSERT(pin != (uint32_t)NC);
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 int sct_n = get_available_sct();
elessair 0:f269e3021894 44 if (sct_n == -1) {
elessair 0:f269e3021894 45 error("No available SCT");
elessair 0:f269e3021894 46 }
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 sct_used |= (1 << sct_n);
elessair 0:f269e3021894 49 obj->pwm = SCTs[sct_n];
elessair 0:f269e3021894 50 obj->pwm_ch = sct_n;
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 // Enable the SCT clock
elessair 0:f269e3021894 55 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << (obj->pwm_ch + 2));
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 // Clear peripheral reset the SCT:
elessair 0:f269e3021894 58 LPC_SYSCON->PRESETCTRL1 |= (1 << (obj->pwm_ch + 2));
elessair 0:f269e3021894 59 LPC_SYSCON->PRESETCTRL1 &= ~(1 << (obj->pwm_ch + 2));
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 switch(obj->pwm_ch) {
elessair 0:f269e3021894 62 case 0:
elessair 0:f269e3021894 63 // SCT0_OUT0
elessair 0:f269e3021894 64 LPC_SWM->PINASSIGN[7] &= ~0x0000FF00;
elessair 0:f269e3021894 65 LPC_SWM->PINASSIGN[7] |= (pin << 8);
elessair 0:f269e3021894 66 break;
elessair 0:f269e3021894 67 case 1:
elessair 0:f269e3021894 68 // SCT1_OUT0
elessair 0:f269e3021894 69 LPC_SWM->PINASSIGN[8] &= ~0x000000FF;
elessair 0:f269e3021894 70 LPC_SWM->PINASSIGN[8] |= (pin);
elessair 0:f269e3021894 71 break;
elessair 0:f269e3021894 72 case 2:
elessair 0:f269e3021894 73 // SCT2_OUT0
elessair 0:f269e3021894 74 LPC_SWM->PINASSIGN[8] &= ~0xFF000000;
elessair 0:f269e3021894 75 LPC_SWM->PINASSIGN[8] |= (pin << 24);
elessair 0:f269e3021894 76 break;
elessair 0:f269e3021894 77 case 3:
elessair 0:f269e3021894 78 // SCT3_OUT0
elessair 0:f269e3021894 79 LPC_SWM->PINASSIGN[9] &= ~0x00FF0000;
elessair 0:f269e3021894 80 LPC_SWM->PINASSIGN[9] |= (pin << 16);
elessair 0:f269e3021894 81 break;
elessair 0:f269e3021894 82 default:
elessair 0:f269e3021894 83 break;
elessair 0:f269e3021894 84 }
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 // Unified 32-bit counter, autolimit
elessair 0:f269e3021894 87 pwm->CONFIG |= ((0x3 << 17) | 0x01);
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 // halt and clear the counter
elessair 0:f269e3021894 90 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 pwm->OUT0_SET = (1 << 0); // event 0
elessair 0:f269e3021894 93 pwm->OUT0_CLR = (1 << 1); // event 1
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 pwm->EV0_CTRL = (1 << 12);
elessair 0:f269e3021894 96 pwm->EV0_STATE = 0xFFFFFFFF;
elessair 0:f269e3021894 97 pwm->EV1_CTRL = (1 << 12) | (1 << 0);
elessair 0:f269e3021894 98 pwm->EV1_STATE = 0xFFFFFFFF;
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 // default to 20ms: standard for servos, and fine for e.g. brightness control
elessair 0:f269e3021894 101 pwmout_period_ms(obj, 20);
elessair 0:f269e3021894 102 pwmout_write (obj, 0);
elessair 0:f269e3021894 103 }
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 void pwmout_free(pwmout_t* obj) {
elessair 0:f269e3021894 106 // Disable the SCT clock
elessair 0:f269e3021894 107 LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1 << (obj->pwm_ch + 2));
elessair 0:f269e3021894 108 sct_used &= ~(1 << obj->pwm_ch);
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 void pwmout_write(pwmout_t* obj, float value) {
elessair 0:f269e3021894 112 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 113 if (value < 0.0f) {
elessair 0:f269e3021894 114 value = 0.0;
elessair 0:f269e3021894 115 } else if (value > 1.0f) {
elessair 0:f269e3021894 116 value = 1.0;
elessair 0:f269e3021894 117 }
elessair 0:f269e3021894 118 uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
elessair 0:f269e3021894 119 if (t_on > 0) {
elessair 0:f269e3021894 120 pwm->MATCHREL1 = t_on - 1;
elessair 0:f269e3021894 121
elessair 0:f269e3021894 122 // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
elessair 0:f269e3021894 123 if (pwm->CTRL & (1 << 2)) {
elessair 0:f269e3021894 124 pwm->MATCH1 = pwm->MATCHREL1;
elessair 0:f269e3021894 125 pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 126 }
elessair 0:f269e3021894 127 } else {
elessair 0:f269e3021894 128 // Halt the timer and force the output low
elessair 0:f269e3021894 129 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 130 pwm->OUTPUT = 0x00000000;
elessair 0:f269e3021894 131 }
elessair 0:f269e3021894 132 }
elessair 0:f269e3021894 133
elessair 0:f269e3021894 134 float pwmout_read(pwmout_t* obj) {
elessair 0:f269e3021894 135 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 136 uint32_t t_off = pwm->MATCHREL0 + 1;
elessair 0:f269e3021894 137 uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
elessair 0:f269e3021894 138 float v = (float)t_on/(float)t_off;
elessair 0:f269e3021894 139 return (v > 1.0f) ? (1.0f) : (v);
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 void pwmout_period(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 143 pwmout_period_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 144 }
elessair 0:f269e3021894 145
elessair 0:f269e3021894 146 void pwmout_period_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 147 pwmout_period_us(obj, ms * 1000);
elessair 0:f269e3021894 148 }
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 // Set the PWM period, keeping the duty cycle the same.
elessair 0:f269e3021894 151 void pwmout_period_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 152 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 153 uint32_t t_off = pwm->MATCHREL0 + 1;
elessair 0:f269e3021894 154 uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
elessair 0:f269e3021894 155 float v = (float)t_on/(float)t_off;
elessair 0:f269e3021894 156 uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
elessair 0:f269e3021894 157 uint32_t pulsewidth_ticks = period_ticks * v;
elessair 0:f269e3021894 158 pwm->MATCHREL0 = period_ticks - 1;
elessair 0:f269e3021894 159 if (pulsewidth_ticks > 0) {
elessair 0:f269e3021894 160 pwm->MATCHREL1 = pulsewidth_ticks - 1;
elessair 0:f269e3021894 161
elessair 0:f269e3021894 162 // Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
elessair 0:f269e3021894 163 if (pwm->CTRL & (1 << 2)) {
elessair 0:f269e3021894 164 pwm->MATCH0 = pwm->MATCHREL0;
elessair 0:f269e3021894 165 pwm->MATCH1 = pwm->MATCHREL1;
elessair 0:f269e3021894 166 pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 167 }
elessair 0:f269e3021894 168 } else {
elessair 0:f269e3021894 169 // Halt the timer and force the output low
elessair 0:f269e3021894 170 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 171 pwm->OUTPUT = 0x00000000;
elessair 0:f269e3021894 172
elessair 0:f269e3021894 173 // Ensure the new period will take immediate effect when the timer is un-halted
elessair 0:f269e3021894 174 pwm->MATCH0 = pwm->MATCHREL0;
elessair 0:f269e3021894 175 }
elessair 0:f269e3021894 176 }
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 179 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 180 }
elessair 0:f269e3021894 181
elessair 0:f269e3021894 182 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 183 pwmout_pulsewidth_us(obj, ms * 1000);
elessair 0:f269e3021894 184 }
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 187 LPC_SCT0_Type* pwm = obj->pwm;
elessair 0:f269e3021894 188 if (us > 0) {
elessair 0:f269e3021894 189 pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
elessair 0:f269e3021894 190
elessair 0:f269e3021894 191 // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
elessair 0:f269e3021894 192 if (pwm->CTRL & (1 << 2)) {
elessair 0:f269e3021894 193 pwm->MATCH1 = pwm->MATCHREL1;
elessair 0:f269e3021894 194 pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 195 }
elessair 0:f269e3021894 196 } else {
elessair 0:f269e3021894 197 // Halt the timer and force the output low
elessair 0:f269e3021894 198 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 199 pwm->OUTPUT = 0x00000000;
elessair 0:f269e3021894 200 }
elessair 0:f269e3021894 201 }
elessair 0:f269e3021894 202