mbed library sources: Modified to operate FRDM-KL25Z at 48MHz from internal 32kHz oscillator (nothing else changed).

Fork of mbed-src by mbed official

The only file that changed is: mbed-src-FLL48/targets/cmsis/TARGET_Freescale/TARGET_KL25Z/system_MKL25Z4.h

Committer:
bogdanm
Date:
Tue Sep 10 15:14:19 2013 +0300
Revision:
20:4263a77256ae
Parent:
19:398f4c622e1b
Sync with git revision 171dda705c947bf910926a0b73d6a4797802554d

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /* mbed Microcontroller Library
emilmont 10:3bc89ef62ce7 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 10:3bc89ef62ce7 3 *
emilmont 10:3bc89ef62ce7 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 10:3bc89ef62ce7 5 * you may not use this file except in compliance with the License.
emilmont 10:3bc89ef62ce7 6 * You may obtain a copy of the License at
emilmont 10:3bc89ef62ce7 7 *
emilmont 10:3bc89ef62ce7 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 10:3bc89ef62ce7 9 *
emilmont 10:3bc89ef62ce7 10 * Unless required by applicable law or agreed to in writing, software
emilmont 10:3bc89ef62ce7 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 10:3bc89ef62ce7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 10:3bc89ef62ce7 13 * See the License for the specific language governing permissions and
emilmont 10:3bc89ef62ce7 14 * limitations under the License.
emilmont 10:3bc89ef62ce7 15 */
emilmont 10:3bc89ef62ce7 16 #include "pwmout_api.h"
emilmont 10:3bc89ef62ce7 17
emilmont 10:3bc89ef62ce7 18 #include "cmsis.h"
emilmont 10:3bc89ef62ce7 19 #include "pinmap.h"
emilmont 10:3bc89ef62ce7 20 #include "error.h"
emilmont 10:3bc89ef62ce7 21
emilmont 10:3bc89ef62ce7 22 static const PinMap PinMap_PWM[] = {
emilmont 10:3bc89ef62ce7 23 // LEDs
emilmont 10:3bc89ef62ce7 24 {LED_RED , PWM_9 , 3}, // PTB18, TPM2 CH0
emilmont 10:3bc89ef62ce7 25 {LED_GREEN, PWM_10, 3}, // PTB19, TPM2 CH1
emilmont 10:3bc89ef62ce7 26 {LED_BLUE , PWM_2 , 4}, // PTD1 , TPM0 CH1
emilmont 10:3bc89ef62ce7 27
emilmont 10:3bc89ef62ce7 28 // Arduino digital pinout
emilmont 10:3bc89ef62ce7 29 {D0, PWM_9 , 3}, // PTA1 , TPM2 CH0
emilmont 10:3bc89ef62ce7 30 {D1, PWM_10, 3}, // PTA2 , TPM2 CH1
emilmont 10:3bc89ef62ce7 31 {D2, PWM_5 , 4}, // PTD4 , TPM0 CH4
emilmont 10:3bc89ef62ce7 32 {D3, PWM_7 , 3}, // PTA12, TPM1 CH0
emilmont 10:3bc89ef62ce7 33 {D4, PWM_2 , 3}, // PTA4 , TPM0 CH1
emilmont 10:3bc89ef62ce7 34 {D5, PWM_3 , 3}, // PTA5 , TPM0 CH2
emilmont 10:3bc89ef62ce7 35 {D6, PWM_5 , 3}, // PTC8 , TPM0 CH4
emilmont 10:3bc89ef62ce7 36 {D7, PWM_6 , 3}, // PTC9 , TPM0 CH5
emilmont 10:3bc89ef62ce7 37 {D8, PWM_8 , 3}, // PTA13, TPM1 CH1
emilmont 10:3bc89ef62ce7 38 {D9, PWM_6 , 4}, // PTD5 , TPM0 CH5
emilmont 10:3bc89ef62ce7 39 {D10, PWM_1 , 4}, // PTD0 , TPM0 CH0
emilmont 10:3bc89ef62ce7 40 {D11, PWM_3 , 4}, // PTD2 , TPM0 CH2
emilmont 10:3bc89ef62ce7 41 {D12, PWM_4 , 4}, // PTD3 , TPM0 CH3
bogdanm 20:4263a77256ae 42 {D13, PWM_2 , 4}, // PTD1 , TPM0 CH1,
bogdanm 20:4263a77256ae 43
bogdanm 20:4263a77256ae 44 {PTA0, PWM_6, 3},
bogdanm 20:4263a77256ae 45 {PTA3, PWM_1, 3},
bogdanm 20:4263a77256ae 46 {PTB0, PWM_7, 3},
bogdanm 20:4263a77256ae 47 {PTB1, PWM_8, 3},
bogdanm 20:4263a77256ae 48 {PTB2, PWM_9, 3},
bogdanm 20:4263a77256ae 49 {PTB3, PWM_10, 3},
bogdanm 20:4263a77256ae 50 {PTC1, PWM_1, 4},
bogdanm 20:4263a77256ae 51 {PTC2, PWM_2, 4},
bogdanm 20:4263a77256ae 52 {PTC3, PWM_3, 4},
bogdanm 20:4263a77256ae 53 {PTC4, PWM_4, 4},
bogdanm 20:4263a77256ae 54 {PTE20, PWM_7, 3},
bogdanm 20:4263a77256ae 55 {PTE21, PWM_8, 3},
bogdanm 20:4263a77256ae 56 {PTE22, PWM_9, 3},
bogdanm 20:4263a77256ae 57 {PTE23, PWM_10, 3},
bogdanm 20:4263a77256ae 58 {PTE24, PWM_1, 3},
bogdanm 20:4263a77256ae 59 {PTE25, PWM_2, 3},
bogdanm 20:4263a77256ae 60 {PTE29, PWM_3, 3},
bogdanm 20:4263a77256ae 61 {PTE30, PWM_4, 3},
bogdanm 20:4263a77256ae 62 {PTE31, PWM_5, 3},
bogdanm 20:4263a77256ae 63
emilmont 10:3bc89ef62ce7 64 {NC , NC , 0}
emilmont 10:3bc89ef62ce7 65 };
emilmont 10:3bc89ef62ce7 66
emilmont 10:3bc89ef62ce7 67 #define PWM_CLOCK_MHZ (0.75) // (48)MHz / 64 = (0.75)MHz
emilmont 10:3bc89ef62ce7 68
emilmont 10:3bc89ef62ce7 69 void pwmout_init(pwmout_t* obj, PinName pin) {
emilmont 10:3bc89ef62ce7 70 // determine the channel
emilmont 10:3bc89ef62ce7 71 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
bogdanm 19:398f4c622e1b 72 if (pwm == (PWMName)NC)
emilmont 10:3bc89ef62ce7 73 error("PwmOut pin mapping failed");
emilmont 10:3bc89ef62ce7 74
emilmont 10:3bc89ef62ce7 75 unsigned int port = (unsigned int)pin >> PORT_SHIFT;
emilmont 10:3bc89ef62ce7 76 unsigned int tpm_n = (pwm >> TPM_SHIFT);
emilmont 10:3bc89ef62ce7 77 unsigned int ch_n = (pwm & 0xFF);
emilmont 10:3bc89ef62ce7 78
emilmont 10:3bc89ef62ce7 79 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
emilmont 10:3bc89ef62ce7 80 SIM->SCGC6 |= 1 << (SIM_SCGC6_TPM0_SHIFT + tpm_n);
emilmont 10:3bc89ef62ce7 81 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: MCGFLLCLK or MCGPLLCLK
emilmont 10:3bc89ef62ce7 82
emilmont 10:3bc89ef62ce7 83 TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n);
emilmont 10:3bc89ef62ce7 84 tpm->SC = TPM_SC_CMOD(1) | TPM_SC_PS(6); // (48)MHz / 64 = (0.75)MHz
emilmont 10:3bc89ef62ce7 85 tpm->CONTROLS[ch_n].CnSC = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */
emilmont 10:3bc89ef62ce7 86
emilmont 10:3bc89ef62ce7 87 obj->CnV = &tpm->CONTROLS[ch_n].CnV;
emilmont 10:3bc89ef62ce7 88 obj->MOD = &tpm->MOD;
emilmont 10:3bc89ef62ce7 89 obj->CNT = &tpm->CNT;
emilmont 10:3bc89ef62ce7 90
emilmont 10:3bc89ef62ce7 91 // default to 20ms: standard for servos, and fine for e.g. brightness control
emilmont 10:3bc89ef62ce7 92 pwmout_period_ms(obj, 20);
emilmont 10:3bc89ef62ce7 93 pwmout_write (obj, 0);
emilmont 10:3bc89ef62ce7 94
emilmont 10:3bc89ef62ce7 95 // Wire pinout
emilmont 10:3bc89ef62ce7 96 pinmap_pinout(pin, PinMap_PWM);
emilmont 10:3bc89ef62ce7 97 }
emilmont 10:3bc89ef62ce7 98
emilmont 10:3bc89ef62ce7 99 void pwmout_free(pwmout_t* obj) {}
emilmont 10:3bc89ef62ce7 100
emilmont 10:3bc89ef62ce7 101 void pwmout_write(pwmout_t* obj, float value) {
emilmont 10:3bc89ef62ce7 102 if (value < 0.0) {
emilmont 10:3bc89ef62ce7 103 value = 0.0;
emilmont 10:3bc89ef62ce7 104 } else if (value > 1.0) {
emilmont 10:3bc89ef62ce7 105 value = 1.0;
emilmont 10:3bc89ef62ce7 106 }
emilmont 10:3bc89ef62ce7 107
emilmont 10:3bc89ef62ce7 108 *obj->CnV = (uint32_t)((float)(*obj->MOD) * value);
emilmont 10:3bc89ef62ce7 109 *obj->CNT = 0;
emilmont 10:3bc89ef62ce7 110 }
emilmont 10:3bc89ef62ce7 111
emilmont 10:3bc89ef62ce7 112 float pwmout_read(pwmout_t* obj) {
emilmont 10:3bc89ef62ce7 113 float v = (float)(*obj->CnV) / (float)(*obj->MOD);
emilmont 10:3bc89ef62ce7 114 return (v > 1.0) ? (1.0) : (v);
emilmont 10:3bc89ef62ce7 115 }
emilmont 10:3bc89ef62ce7 116
emilmont 10:3bc89ef62ce7 117 void pwmout_period(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 118 pwmout_period_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 119 }
emilmont 10:3bc89ef62ce7 120
emilmont 10:3bc89ef62ce7 121 void pwmout_period_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 122 pwmout_period_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 123 }
emilmont 10:3bc89ef62ce7 124
emilmont 10:3bc89ef62ce7 125 // Set the PWM period, keeping the duty cycle the same.
emilmont 10:3bc89ef62ce7 126 void pwmout_period_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 127 float dc = pwmout_read(obj);
emilmont 10:3bc89ef62ce7 128 *obj->MOD = PWM_CLOCK_MHZ * us;
emilmont 10:3bc89ef62ce7 129 pwmout_write(obj, dc);
emilmont 10:3bc89ef62ce7 130 }
emilmont 10:3bc89ef62ce7 131
emilmont 10:3bc89ef62ce7 132 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 133 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 134 }
emilmont 10:3bc89ef62ce7 135
emilmont 10:3bc89ef62ce7 136 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 137 pwmout_pulsewidth_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 138 }
emilmont 10:3bc89ef62ce7 139
emilmont 10:3bc89ef62ce7 140 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 141 *obj->CnV = PWM_CLOCK_MHZ * us;
emilmont 10:3bc89ef62ce7 142 }