mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jun 03 08:45:33 2015 +0100
Revision:
557:42efda18ac92
Parent:
516:b3fb5c6901a6
Synchronized with git revision e775613c6b67f6d67298fec0fe26ded074b8d4c3

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

Added tests for the WFI instruction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 337:6ed01c00b962 1 /* mbed Microcontroller Library
mbed_official 337:6ed01c00b962 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 337:6ed01c00b962 3 *
mbed_official 337:6ed01c00b962 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 337:6ed01c00b962 5 * you may not use this file except in compliance with the License.
mbed_official 337:6ed01c00b962 6 * You may obtain a copy of the License at
mbed_official 337:6ed01c00b962 7 *
mbed_official 337:6ed01c00b962 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 337:6ed01c00b962 9 *
mbed_official 337:6ed01c00b962 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 337:6ed01c00b962 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 337:6ed01c00b962 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 337:6ed01c00b962 13 * See the License for the specific language governing permissions and
mbed_official 337:6ed01c00b962 14 * limitations under the License.
mbed_official 337:6ed01c00b962 15 */
mbed_official 337:6ed01c00b962 16 #include "mbed_assert.h"
mbed_official 337:6ed01c00b962 17 #include "pwmout_api.h"
mbed_official 337:6ed01c00b962 18 #include "cmsis.h"
mbed_official 337:6ed01c00b962 19 #include "pinmap.h"
mbed_official 337:6ed01c00b962 20 #include "mbed_error.h"
mbed_official 337:6ed01c00b962 21
mbed_official 337:6ed01c00b962 22 #if DEVICE_PWMOUT
mbed_official 337:6ed01c00b962 23
mbed_official 337:6ed01c00b962 24 // bit flags for used SCTs
mbed_official 337:6ed01c00b962 25 static unsigned char sct_used = 0;
mbed_official 337:6ed01c00b962 26
mbed_official 337:6ed01c00b962 27 static int get_available_sct()
mbed_official 337:6ed01c00b962 28 {
mbed_official 337:6ed01c00b962 29 int i;
mbed_official 337:6ed01c00b962 30 for (i = 0; i < 4; i++) {
mbed_official 337:6ed01c00b962 31 if ((sct_used & (1 << i)) == 0)
mbed_official 337:6ed01c00b962 32 return i;
mbed_official 337:6ed01c00b962 33 }
mbed_official 337:6ed01c00b962 34 return -1;
mbed_official 337:6ed01c00b962 35 }
mbed_official 337:6ed01c00b962 36
mbed_official 337:6ed01c00b962 37 void pwmout_init(pwmout_t* obj, PinName pin)
mbed_official 337:6ed01c00b962 38 {
mbed_official 557:42efda18ac92 39 MBED_ASSERT(pin != (PinName)NC);
mbed_official 337:6ed01c00b962 40
mbed_official 337:6ed01c00b962 41 int sct_n = get_available_sct();
mbed_official 337:6ed01c00b962 42 if (sct_n == -1) {
mbed_official 337:6ed01c00b962 43 error("No available SCT");
mbed_official 337:6ed01c00b962 44 }
mbed_official 337:6ed01c00b962 45
mbed_official 337:6ed01c00b962 46 sct_used |= (1 << sct_n);
mbed_official 337:6ed01c00b962 47
mbed_official 337:6ed01c00b962 48 obj->pwm = (LPC_SCT_Type*)LPC_SCT;
mbed_official 337:6ed01c00b962 49 obj->pwm_ch = sct_n;
mbed_official 337:6ed01c00b962 50
mbed_official 516:b3fb5c6901a6 51 LPC_SCT_Type* pwm = obj->pwm;
mbed_official 337:6ed01c00b962 52
mbed_official 337:6ed01c00b962 53 // Enable the SCT clock
mbed_official 337:6ed01c00b962 54 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8);
mbed_official 337:6ed01c00b962 55
mbed_official 337:6ed01c00b962 56 // Clear peripheral reset the SCT:
mbed_official 337:6ed01c00b962 57 LPC_SYSCON->PRESETCTRL |= (1 << 8);
mbed_official 337:6ed01c00b962 58
mbed_official 337:6ed01c00b962 59 switch(sct_n) {
mbed_official 337:6ed01c00b962 60 case 0:
mbed_official 337:6ed01c00b962 61 // SCT_OUT0
mbed_official 337:6ed01c00b962 62 LPC_SWM->PINASSIGN[7] &= ~0xFF000000;
mbed_official 337:6ed01c00b962 63 LPC_SWM->PINASSIGN[7] |= ((pin >> PIN_SHIFT) << 24);
mbed_official 337:6ed01c00b962 64 break;
mbed_official 337:6ed01c00b962 65 case 1:
mbed_official 337:6ed01c00b962 66 // SCT_OUT1
mbed_official 337:6ed01c00b962 67 LPC_SWM->PINASSIGN[8] &= ~0x000000FF;
mbed_official 337:6ed01c00b962 68 LPC_SWM->PINASSIGN[8] |= (pin >> PIN_SHIFT);
mbed_official 337:6ed01c00b962 69 break;
mbed_official 337:6ed01c00b962 70 case 2:
mbed_official 337:6ed01c00b962 71 // SCT2_OUT2
mbed_official 337:6ed01c00b962 72 LPC_SWM->PINASSIGN[8] &= ~0x0000FF00;
mbed_official 337:6ed01c00b962 73 LPC_SWM->PINASSIGN[8] |= ((pin >> PIN_SHIFT) << 8);
mbed_official 337:6ed01c00b962 74 break;
mbed_official 337:6ed01c00b962 75 case 3:
mbed_official 337:6ed01c00b962 76 // SCT3_OUT3
mbed_official 337:6ed01c00b962 77 LPC_SWM->PINASSIGN[8] &= ~0x00FF0000;
mbed_official 337:6ed01c00b962 78 LPC_SWM->PINASSIGN[8] |= ((pin >> PIN_SHIFT) << 16);
mbed_official 337:6ed01c00b962 79 break;
mbed_official 337:6ed01c00b962 80 default:
mbed_official 337:6ed01c00b962 81 break;
mbed_official 337:6ed01c00b962 82 }
mbed_official 337:6ed01c00b962 83
mbed_official 516:b3fb5c6901a6 84 // Unified 32-bit counter, autolimit
mbed_official 516:b3fb5c6901a6 85 pwm->CONFIG |= ((0x3 << 17) | 0x01);
mbed_official 337:6ed01c00b962 86
mbed_official 337:6ed01c00b962 87 // halt and clear the counter
mbed_official 337:6ed01c00b962 88 pwm->CTRL |= (1 << 2) | (1 << 3);
mbed_official 337:6ed01c00b962 89
mbed_official 337:6ed01c00b962 90 // System Clock -> us_ticker (1)MHz
mbed_official 337:6ed01c00b962 91 pwm->CTRL &= ~(0x7F << 5);
mbed_official 337:6ed01c00b962 92 pwm->CTRL |= (((SystemCoreClock/1000000 - 1) & 0x7F) << 5);
mbed_official 337:6ed01c00b962 93
mbed_official 337:6ed01c00b962 94 pwm->OUT[sct_n].SET = (1 << ((sct_n * 2) + 0));
mbed_official 337:6ed01c00b962 95 pwm->OUT[sct_n].CLR = (1 << ((sct_n * 2) + 1));
mbed_official 337:6ed01c00b962 96
mbed_official 337:6ed01c00b962 97 pwm->EVENT[(sct_n * 2) + 0].CTRL = (1 << 12) | ((sct_n * 2) + 0); // match event
mbed_official 337:6ed01c00b962 98 pwm->EVENT[(sct_n * 2) + 0].STATE = 0xFFFFFFFF;
mbed_official 337:6ed01c00b962 99 pwm->EVENT[(sct_n * 2) + 1].CTRL = (1 << 12) | ((sct_n * 2) + 1);
mbed_official 337:6ed01c00b962 100 pwm->EVENT[(sct_n * 2) + 1].STATE = 0xFFFFFFFF;
mbed_official 337:6ed01c00b962 101
mbed_official 337:6ed01c00b962 102 // unhalt the counter:
mbed_official 337:6ed01c00b962 103 // - clearing bit 2 of the CTRL register
mbed_official 337:6ed01c00b962 104 pwm->CTRL &= ~(1 << 2);
mbed_official 337:6ed01c00b962 105
mbed_official 337:6ed01c00b962 106 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 337:6ed01c00b962 107 pwmout_period_ms(obj, 20);
mbed_official 337:6ed01c00b962 108 pwmout_write (obj, 0);
mbed_official 337:6ed01c00b962 109 }
mbed_official 337:6ed01c00b962 110
mbed_official 337:6ed01c00b962 111 void pwmout_free(pwmout_t* obj)
mbed_official 337:6ed01c00b962 112 {
mbed_official 337:6ed01c00b962 113 // Disable the SCT clock
mbed_official 337:6ed01c00b962 114 LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 8);
mbed_official 337:6ed01c00b962 115 sct_used &= ~(1 << obj->pwm_ch);
mbed_official 337:6ed01c00b962 116 }
mbed_official 337:6ed01c00b962 117
mbed_official 337:6ed01c00b962 118 void pwmout_write(pwmout_t* obj, float value)
mbed_official 337:6ed01c00b962 119 {
mbed_official 337:6ed01c00b962 120 if (value < 0.0f) {
mbed_official 337:6ed01c00b962 121 value = 0.0;
mbed_official 337:6ed01c00b962 122 } else if (value > 1.0f) {
mbed_official 337:6ed01c00b962 123 value = 1.0;
mbed_official 337:6ed01c00b962 124 }
mbed_official 337:6ed01c00b962 125 uint32_t t_on = (uint32_t)((float)(obj->pwm->MATCHREL[obj->pwm_ch * 2]) * value);
mbed_official 337:6ed01c00b962 126 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] = t_on;
mbed_official 337:6ed01c00b962 127 }
mbed_official 337:6ed01c00b962 128
mbed_official 337:6ed01c00b962 129 float pwmout_read(pwmout_t* obj)
mbed_official 337:6ed01c00b962 130 {
mbed_official 337:6ed01c00b962 131 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 0];
mbed_official 337:6ed01c00b962 132 uint32_t t_on = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1];
mbed_official 337:6ed01c00b962 133 float v = (float)t_on/(float)t_off;
mbed_official 337:6ed01c00b962 134 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 337:6ed01c00b962 135 }
mbed_official 337:6ed01c00b962 136
mbed_official 337:6ed01c00b962 137 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 337:6ed01c00b962 138 {
mbed_official 337:6ed01c00b962 139 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 337:6ed01c00b962 140 }
mbed_official 337:6ed01c00b962 141
mbed_official 337:6ed01c00b962 142 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 337:6ed01c00b962 143 {
mbed_official 337:6ed01c00b962 144 pwmout_period_us(obj, ms * 1000);
mbed_official 337:6ed01c00b962 145 }
mbed_official 337:6ed01c00b962 146
mbed_official 337:6ed01c00b962 147 // Set the PWM period, keeping the duty cycle the same.
mbed_official 337:6ed01c00b962 148 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 337:6ed01c00b962 149 {
mbed_official 337:6ed01c00b962 150 uint32_t t_off = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 0];
mbed_official 337:6ed01c00b962 151 uint32_t t_on = obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1];
mbed_official 337:6ed01c00b962 152 float v = (float)t_on/(float)t_off;
mbed_official 516:b3fb5c6901a6 153 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 0] = (uint32_t)us;
mbed_official 516:b3fb5c6901a6 154 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] = (uint32_t)((float)us * (float)v);
mbed_official 337:6ed01c00b962 155 }
mbed_official 337:6ed01c00b962 156
mbed_official 337:6ed01c00b962 157 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 337:6ed01c00b962 158 {
mbed_official 337:6ed01c00b962 159 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 337:6ed01c00b962 160 }
mbed_official 337:6ed01c00b962 161
mbed_official 337:6ed01c00b962 162 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 337:6ed01c00b962 163 {
mbed_official 337:6ed01c00b962 164 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 337:6ed01c00b962 165 }
mbed_official 337:6ed01c00b962 166
mbed_official 337:6ed01c00b962 167 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 337:6ed01c00b962 168 {
mbed_official 516:b3fb5c6901a6 169 obj->pwm->MATCHREL[(obj->pwm_ch * 2) + 1] = (uint32_t)us;
mbed_official 337:6ed01c00b962 170 }
mbed_official 337:6ed01c00b962 171
mbed_official 337:6ed01c00b962 172 #endif