eunkyoung kim / mbed-src

Dependents:   SSD1306_smart_watch

Fork of mbed-src by eunkyoung kim

Committer:
eunkyoungkim
Date:
Mon Jun 29 05:49:52 2015 +0000
Revision:
577:545d200432a1
Parent:
516:b3fb5c6901a6
modify the i2c.c/i2c.h

Who changed what in which revision?

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