mbed library sources modified for open wear

Dependents:   openwear-lifelogger-example

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jul 23 08:45:10 2014 +0100
Revision:
264:91fcbf887208
Parent:
255:20b371a9491b
Synchronized with git revision 6663575abc7a56be6750c7be598b3e383855a2f7

Full URL: https://github.com/mbedmicro/mbed/commit/6663575abc7a56be6750c7be598b3e383855a2f7/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 68:41613245dfd7 1 /* mbed Microcontroller Library
mbed_official 68:41613245dfd7 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 68:41613245dfd7 3 *
mbed_official 68:41613245dfd7 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 68:41613245dfd7 5 * you may not use this file except in compliance with the License.
mbed_official 68:41613245dfd7 6 * You may obtain a copy of the License at
mbed_official 68:41613245dfd7 7 *
mbed_official 68:41613245dfd7 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 68:41613245dfd7 9 *
mbed_official 68:41613245dfd7 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 68:41613245dfd7 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 68:41613245dfd7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 68:41613245dfd7 13 * See the License for the specific language governing permissions and
mbed_official 68:41613245dfd7 14 * limitations under the License.
mbed_official 68:41613245dfd7 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
mbed_official 68:41613245dfd7 17 #include "pwmout_api.h"
mbed_official 68:41613245dfd7 18
mbed_official 68:41613245dfd7 19 #include "cmsis.h"
mbed_official 68:41613245dfd7 20 #include "pinmap.h"
mbed_official 68:41613245dfd7 21
mbed_official 68:41613245dfd7 22 static const PinMap PinMap_PWM[] = {
mbed_official 68:41613245dfd7 23 // LEDs
mbed_official 73:299c67215126 24 {LED_RED , PWM_3 , 4}, // PTC3, FTM0 CH2
mbed_official 73:299c67215126 25 {LED_GREEN, PWM_5, 4}, // PTD4, FTM0 CH4
mbed_official 73:299c67215126 26 {LED_BLUE , PWM_8 , 3}, // PTA2, FTM0 CH7
mbed_official 68:41613245dfd7 27
mbed_official 68:41613245dfd7 28 // Arduino digital pinout
mbed_official 73:299c67215126 29 {D3, PWM_5 , 4}, // PTD4, FTM0 CH4
mbed_official 73:299c67215126 30 {D5, PWM_7 , 3}, // PTA1, FTM0 CH6
mbed_official 73:299c67215126 31 {D6, PWM_3 , 4}, // PTC3, FTM0 CH2
mbed_official 73:299c67215126 32 {D9, PWM_6 , 4}, // PTD5, FTM0 CH6
mbed_official 73:299c67215126 33 {D10, PWM_2 , 4}, // PTC2, FTM0 CH1
mbed_official 73:299c67215126 34
mbed_official 73:299c67215126 35 {PTA0, PWM_6 , 3}, // PTA0, FTM0 CH5
mbed_official 73:299c67215126 36 {PTA3, PWM_1 , 3}, // PTA3, FTM0 CH0
mbed_official 73:299c67215126 37 {PTA4, PWM_2 , 3}, // PTA4, FTM0 CH1
mbed_official 73:299c67215126 38 {PTA5, PWM_3 , 3}, // PTA5, FTM0 CH2
mbed_official 73:299c67215126 39 {PTA12, PWM_9 , 3}, // PTA12, FTM1 CH0
mbed_official 73:299c67215126 40 {PTA13, PWM_10, 3}, // PTA13, FTM1 CH1
mbed_official 73:299c67215126 41 {PTB0, PWM_9 , 3}, // PTB0, FTM1 CH0
mbed_official 73:299c67215126 42 {PTB1, PWM_10, 3}, // PTB1, FTM1 CH1
mbed_official 73:299c67215126 43 {PTC1, PWM_1 , 4}, // PTC1, FTM0 CH0
mbed_official 73:299c67215126 44 {PTD4, PWM_4 , 4}, // PTD4, FTM0 CH3
mbed_official 73:299c67215126 45 {PTD6, PWM_7 , 4}, // PTD6, FTM0 CH6
mbed_official 73:299c67215126 46 {PTD7, PWM_8 , 4}, // PTD7, FTM0 CH7
mbed_official 68:41613245dfd7 47
mbed_official 68:41613245dfd7 48 {NC , NC , 0}
mbed_official 68:41613245dfd7 49 };
mbed_official 68:41613245dfd7 50
mbed_official 73:299c67215126 51 static float pwm_clock = 0;
mbed_official 68:41613245dfd7 52
mbed_official 68:41613245dfd7 53 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 68:41613245dfd7 54 // determine the channel
mbed_official 68:41613245dfd7 55 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 227:7bd0639b8911 56 MBED_ASSERT(pwm != (PWMName)NC);
mbed_official 68:41613245dfd7 57
mbed_official 73:299c67215126 58 uint32_t clkdiv = 0;
mbed_official 73:299c67215126 59 float clkval = SystemCoreClock / 1000000.0f;
mbed_official 73:299c67215126 60
mbed_official 73:299c67215126 61 while (clkval > 1) {
mbed_official 73:299c67215126 62 clkdiv++;
mbed_official 73:299c67215126 63 clkval /= 2.0;
mbed_official 73:299c67215126 64 if (clkdiv == 7)
mbed_official 73:299c67215126 65 break;
mbed_official 73:299c67215126 66 }
mbed_official 73:299c67215126 67
mbed_official 73:299c67215126 68 pwm_clock = clkval;
mbed_official 68:41613245dfd7 69 unsigned int port = (unsigned int)pin >> PORT_SHIFT;
mbed_official 68:41613245dfd7 70 unsigned int ftm_n = (pwm >> TPM_SHIFT);
mbed_official 68:41613245dfd7 71 unsigned int ch_n = (pwm & 0xFF);
mbed_official 68:41613245dfd7 72
mbed_official 68:41613245dfd7 73 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
mbed_official 68:41613245dfd7 74 SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n);
mbed_official 68:41613245dfd7 75
mbed_official 68:41613245dfd7 76 FTM_Type *ftm = (FTM_Type *)(FTM0_BASE + 0x1000 * ftm_n);
mbed_official 68:41613245dfd7 77 ftm->CONF |= FTM_CONF_BDMMODE(3);
mbed_official 73:299c67215126 78 ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz
mbed_official 68:41613245dfd7 79 ftm->CONTROLS[ch_n].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */
mbed_official 68:41613245dfd7 80
mbed_official 68:41613245dfd7 81 obj->CnV = &ftm->CONTROLS[ch_n].CnV;
mbed_official 68:41613245dfd7 82 obj->MOD = &ftm->MOD;
mbed_official 68:41613245dfd7 83 obj->CNT = &ftm->CNT;
mbed_official 68:41613245dfd7 84
mbed_official 68:41613245dfd7 85 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 68:41613245dfd7 86 pwmout_period_ms(obj, 20);
mbed_official 73:299c67215126 87 pwmout_write(obj, 0);
mbed_official 68:41613245dfd7 88
mbed_official 68:41613245dfd7 89 // Wire pinout
mbed_official 68:41613245dfd7 90 pinmap_pinout(pin, PinMap_PWM);
mbed_official 68:41613245dfd7 91 }
mbed_official 68:41613245dfd7 92
mbed_official 68:41613245dfd7 93 void pwmout_free(pwmout_t* obj) {}
mbed_official 68:41613245dfd7 94
mbed_official 68:41613245dfd7 95 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 68:41613245dfd7 96 if (value < 0.0) {
mbed_official 68:41613245dfd7 97 value = 0.0;
mbed_official 68:41613245dfd7 98 } else if (value > 1.0) {
mbed_official 68:41613245dfd7 99 value = 1.0;
mbed_official 68:41613245dfd7 100 }
mbed_official 68:41613245dfd7 101
mbed_official 264:91fcbf887208 102 *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value);
mbed_official 68:41613245dfd7 103 }
mbed_official 68:41613245dfd7 104
mbed_official 68:41613245dfd7 105 float pwmout_read(pwmout_t* obj) {
mbed_official 264:91fcbf887208 106 float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1);
mbed_official 68:41613245dfd7 107 return (v > 1.0) ? (1.0) : (v);
mbed_official 68:41613245dfd7 108 }
mbed_official 68:41613245dfd7 109
mbed_official 68:41613245dfd7 110 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 68:41613245dfd7 111 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 68:41613245dfd7 112 }
mbed_official 68:41613245dfd7 113
mbed_official 68:41613245dfd7 114 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 68:41613245dfd7 115 pwmout_period_us(obj, ms * 1000);
mbed_official 68:41613245dfd7 116 }
mbed_official 68:41613245dfd7 117
mbed_official 68:41613245dfd7 118 // Set the PWM period, keeping the duty cycle the same.
mbed_official 68:41613245dfd7 119 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 68:41613245dfd7 120 float dc = pwmout_read(obj);
mbed_official 264:91fcbf887208 121 *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1;
mbed_official 68:41613245dfd7 122 pwmout_write(obj, dc);
mbed_official 68:41613245dfd7 123 }
mbed_official 68:41613245dfd7 124
mbed_official 68:41613245dfd7 125 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 68:41613245dfd7 126 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 68:41613245dfd7 127 }
mbed_official 68:41613245dfd7 128
mbed_official 68:41613245dfd7 129 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 68:41613245dfd7 130 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 68:41613245dfd7 131 }
mbed_official 68:41613245dfd7 132
mbed_official 68:41613245dfd7 133 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 73:299c67215126 134 *obj->CnV = (uint32_t)(pwm_clock * (float)us);
mbed_official 68:41613245dfd7 135 }