mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

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 #if DEVICE_PWMOUT
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 // bit flags for used SCTs
elessair 0:f269e3021894 25 static unsigned char sct_used = 0;
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 static int get_available_sct()
elessair 0:f269e3021894 28 {
elessair 0:f269e3021894 29 int i;
elessair 0:f269e3021894 30 for (i = 0; i < 4; i++) {
elessair 0:f269e3021894 31 if ((sct_used & (1 << i)) == 0)
elessair 0:f269e3021894 32 return i;
elessair 0:f269e3021894 33 }
elessair 0:f269e3021894 34 return -1;
elessair 0:f269e3021894 35 }
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 void pwmout_init(pwmout_t* obj, PinName pin)
elessair 0:f269e3021894 38 {
elessair 0:f269e3021894 39 MBED_ASSERT(pin != (PinName)NC);
elessair 0:f269e3021894 40
elessair 0:f269e3021894 41 int sct_n = get_available_sct();
elessair 0:f269e3021894 42 if (sct_n == -1) {
elessair 0:f269e3021894 43 error("No available SCT");
elessair 0:f269e3021894 44 }
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 sct_used |= (1 << sct_n);
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 obj->pwm = (LPC_SCT_Type*)LPC_SCT;
elessair 0:f269e3021894 49 obj->pwm_ch = sct_n;
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 LPC_SCT_Type* pwm = obj->pwm;
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 // Enable the SCT clock
elessair 0:f269e3021894 54 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 // Clear peripheral reset the SCT:
elessair 0:f269e3021894 57 LPC_SYSCON->PRESETCTRL |= (1 << 8);
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 switch(sct_n) {
elessair 0:f269e3021894 60 case 0:
elessair 0:f269e3021894 61 // SCT_OUT0
elessair 0:f269e3021894 62 LPC_SWM->PINASSIGN[7] &= ~0xFF000000;
elessair 0:f269e3021894 63 LPC_SWM->PINASSIGN[7] |= ((pin >> PIN_SHIFT) << 24);
elessair 0:f269e3021894 64 break;
elessair 0:f269e3021894 65 case 1:
elessair 0:f269e3021894 66 // SCT_OUT1
elessair 0:f269e3021894 67 LPC_SWM->PINASSIGN[8] &= ~0x000000FF;
elessair 0:f269e3021894 68 LPC_SWM->PINASSIGN[8] |= (pin >> PIN_SHIFT);
elessair 0:f269e3021894 69 break;
elessair 0:f269e3021894 70 case 2:
elessair 0:f269e3021894 71 // SCT2_OUT2
elessair 0:f269e3021894 72 LPC_SWM->PINASSIGN[8] &= ~0x0000FF00;
elessair 0:f269e3021894 73 LPC_SWM->PINASSIGN[8] |= ((pin >> PIN_SHIFT) << 8);
elessair 0:f269e3021894 74 break;
elessair 0:f269e3021894 75 case 3:
elessair 0:f269e3021894 76 // SCT3_OUT3
elessair 0:f269e3021894 77 LPC_SWM->PINASSIGN[8] &= ~0x00FF0000;
elessair 0:f269e3021894 78 LPC_SWM->PINASSIGN[8] |= ((pin >> PIN_SHIFT) << 16);
elessair 0:f269e3021894 79 break;
elessair 0:f269e3021894 80 default:
elessair 0:f269e3021894 81 break;
elessair 0:f269e3021894 82 }
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 // Unified 32-bit counter, autolimit
elessair 0:f269e3021894 85 pwm->CONFIG |= ((0x3 << 17) | 0x01);
elessair 0:f269e3021894 86
elessair 0:f269e3021894 87 // halt and clear the counter
elessair 0:f269e3021894 88 pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 // System Clock -> us_ticker (1)MHz
elessair 0:f269e3021894 91 pwm->CTRL &= ~(0x7F << 5);
elessair 0:f269e3021894 92 pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
elessair 0:f269e3021894 93
elessair 0:f269e3021894 94 // Set event number
elessair 0:f269e3021894 95 pwm->OUT[sct_n].SET = (1 << ((sct_n * 2) + 0));
elessair 0:f269e3021894 96 pwm->OUT[sct_n].CLR = (1 << ((sct_n * 2) + 1));
elessair 0:f269e3021894 97
elessair 0:f269e3021894 98 pwm->EVENT[(sct_n * 2) + 0].CTRL = (1 << 12) | ((sct_n * 2) + 0); // match event
elessair 0:f269e3021894 99 pwm->EVENT[(sct_n * 2) + 0].STATE = 0xFFFFFFFF;
elessair 0:f269e3021894 100 pwm->EVENT[(sct_n * 2) + 1].CTRL = (1 << 12) | ((sct_n * 2) + 1);
elessair 0:f269e3021894 101 pwm->EVENT[(sct_n * 2) + 1].STATE = 0xFFFFFFFF;
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 // default to 20ms: standard for servos, and fine for e.g. brightness control
elessair 0:f269e3021894 104 pwmout_period_ms(obj, 20);
elessair 0:f269e3021894 105 pwmout_write (obj, 0);
elessair 0:f269e3021894 106 }
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 void pwmout_free(pwmout_t* obj)
elessair 0:f269e3021894 109 {
elessair 0:f269e3021894 110 // Disable the SCT clock
elessair 0:f269e3021894 111 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 8);
elessair 0:f269e3021894 112 sct_used &= ~(1 << obj->pwm_ch);
elessair 0:f269e3021894 113 }
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 void pwmout_write(pwmout_t* obj, float value)
elessair 0:f269e3021894 116 {
elessair 0:f269e3021894 117 if (value < 0.0f) {
elessair 0:f269e3021894 118 value = 0.0;
elessair 0:f269e3021894 119 } else if (value > 1.0f) {
elessair 0:f269e3021894 120 value = 1.0f;
elessair 0:f269e3021894 121 }
elessair 0:f269e3021894 122 uint32_t t_on = (uint32_t)((float)(obj->pwm->MATCHREL[obj->pwm_ch * 2] + 1) * value);
elessair 0:f269e3021894 123 if (t_on > 0) { // duty is not 0%
elessair 0:f269e3021894 124 if (value != 1.0f) { // duty is not 100%
elessair 0:f269e3021894 125 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] = t_on - 1;
elessair 0:f269e3021894 126 // unhalt the counter
elessair 0:f269e3021894 127 obj->pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 128 } else { // duty is 100%
elessair 0:f269e3021894 129 // halt and clear the counter
elessair 0:f269e3021894 130 obj->pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 131 // output level tied to high
elessair 0:f269e3021894 132 obj->pwm->OUTPUT |= (1 << obj->pwm_ch);
elessair 0:f269e3021894 133 }
elessair 0:f269e3021894 134 } else { // duty is 0%
elessair 0:f269e3021894 135 // halt and clear the counter
elessair 0:f269e3021894 136 obj->pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 137 // output level tied to low
elessair 0:f269e3021894 138 obj->pwm->OUTPUT &= ~(1 << obj->pwm_ch);
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 float pwmout_read(pwmout_t* obj)
elessair 0:f269e3021894 143 {
elessair 0:f269e3021894 144 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 0] + 1;
elessair 0:f269e3021894 145 uint32_t t_on = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] + 1;
elessair 0:f269e3021894 146 float v = (float)t_on/(float)t_off;
elessair 0:f269e3021894 147 return (v > 1.0f) ? (1.0f) : (v);
elessair 0:f269e3021894 148 }
elessair 0:f269e3021894 149
elessair 0:f269e3021894 150 void pwmout_period(pwmout_t* obj, float seconds)
elessair 0:f269e3021894 151 {
elessair 0:f269e3021894 152 pwmout_period_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 void pwmout_period_ms(pwmout_t* obj, int ms)
elessair 0:f269e3021894 156 {
elessair 0:f269e3021894 157 pwmout_period_us(obj, ms * 1000);
elessair 0:f269e3021894 158 }
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 // Set the PWM period, keeping the duty cycle the same.
elessair 0:f269e3021894 161 void pwmout_period_us(pwmout_t* obj, int us)
elessair 0:f269e3021894 162 {
elessair 0:f269e3021894 163 // The period are off by one for MATCHREL, so +1 to get actual value
elessair 0:f269e3021894 164 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 0] + 1;
elessair 0:f269e3021894 165 uint32_t t_on = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] + 1;
elessair 0:f269e3021894 166 float v = (float)t_on/(float)t_off;
elessair 0:f269e3021894 167 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 0] = (uint32_t)us - 1;
elessair 0:f269e3021894 168 if (us > 0) { // PWM period is not 0
elessair 0:f269e3021894 169 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] = (uint32_t)((float)us * (float)v) - 1;
elessair 0:f269e3021894 170 // unhalt the counter
elessair 0:f269e3021894 171 obj->pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 172 } else { // PWM period is 0
elessair 0:f269e3021894 173 // halt and clear the counter
elessair 0:f269e3021894 174 obj->pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 175 // output level tied to low
elessair 0:f269e3021894 176 obj->pwm->OUTPUT &= ~(1 << obj->pwm_ch);
elessair 0:f269e3021894 177 }
elessair 0:f269e3021894 178 }
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
elessair 0:f269e3021894 181 {
elessair 0:f269e3021894 182 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 183 }
elessair 0:f269e3021894 184
elessair 0:f269e3021894 185 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
elessair 0:f269e3021894 186 {
elessair 0:f269e3021894 187 pwmout_pulsewidth_us(obj, ms * 1000);
elessair 0:f269e3021894 188 }
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
elessair 0:f269e3021894 191 {
elessair 0:f269e3021894 192 if (us > 0) { // PWM peried is not 0
elessair 0:f269e3021894 193 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] = (uint32_t)us - 1;
elessair 0:f269e3021894 194 obj->pwm->CTRL &= ~(1 << 2);
elessair 0:f269e3021894 195 } else { //PWM period is 0
elessair 0:f269e3021894 196 // halt and clear the counter
elessair 0:f269e3021894 197 obj->pwm->CTRL |= (1 << 2) | (1 << 3);
elessair 0:f269e3021894 198 // output level tied to low
elessair 0:f269e3021894 199 obj->pwm->OUTPUT &= ~(1 << obj->pwm_ch);
elessair 0:f269e3021894 200 }
elessair 0:f269e3021894 201 }
elessair 0:f269e3021894 202
elessair 0:f269e3021894 203 #endif