mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_KLXX/pwmout_api.c@264:91fcbf887208, 2014-07-23 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jul 23 08:45:10 2014 +0100
- Revision:
- 264:91fcbf887208
- Parent:
- 227:7bd0639b8911
Synchronized with git revision 6663575abc7a56be6750c7be598b3e383855a2f7
Full URL: https://github.com/mbedmicro/mbed/commit/6663575abc7a56be6750c7be598b3e383855a2f7/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 82:0b31dbcd4769 | 1 | /* mbed Microcontroller Library |
mbed_official | 82:0b31dbcd4769 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 82:0b31dbcd4769 | 3 | * |
mbed_official | 82:0b31dbcd4769 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 82:0b31dbcd4769 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 82:0b31dbcd4769 | 6 | * You may obtain a copy of the License at |
mbed_official | 82:0b31dbcd4769 | 7 | * |
mbed_official | 82:0b31dbcd4769 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 82:0b31dbcd4769 | 9 | * |
mbed_official | 82:0b31dbcd4769 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 82:0b31dbcd4769 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 82:0b31dbcd4769 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 82:0b31dbcd4769 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 82:0b31dbcd4769 | 14 | * limitations under the License. |
mbed_official | 82:0b31dbcd4769 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 82:0b31dbcd4769 | 17 | #include "pwmout_api.h" |
mbed_official | 82:0b31dbcd4769 | 18 | |
mbed_official | 82:0b31dbcd4769 | 19 | #include "cmsis.h" |
mbed_official | 82:0b31dbcd4769 | 20 | #include "pinmap.h" |
mbed_official | 82:0b31dbcd4769 | 21 | #include "clk_freqs.h" |
mbed_official | 82:0b31dbcd4769 | 22 | #include "PeripheralPins.h" |
mbed_official | 82:0b31dbcd4769 | 23 | |
mbed_official | 82:0b31dbcd4769 | 24 | static float pwm_clock; |
mbed_official | 82:0b31dbcd4769 | 25 | |
mbed_official | 82:0b31dbcd4769 | 26 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 82:0b31dbcd4769 | 27 | // determine the channel |
mbed_official | 82:0b31dbcd4769 | 28 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 227:7bd0639b8911 | 29 | MBED_ASSERT(pwm != (PWMName)NC); |
mbed_official | 227:7bd0639b8911 | 30 | |
mbed_official | 82:0b31dbcd4769 | 31 | uint32_t clkdiv = 0; |
mbed_official | 82:0b31dbcd4769 | 32 | float clkval; |
mbed_official | 82:0b31dbcd4769 | 33 | if (mcgpllfll_frequency()) { |
mbed_official | 82:0b31dbcd4769 | 34 | SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: MCGFLLCLK or MCGPLLCLK |
mbed_official | 82:0b31dbcd4769 | 35 | clkval = mcgpllfll_frequency() / 1000000.0f; |
mbed_official | 82:0b31dbcd4769 | 36 | } else { |
mbed_official | 82:0b31dbcd4769 | 37 | SIM->SOPT2 |= SIM_SOPT2_TPMSRC(2); // Clock source: ExtOsc |
mbed_official | 82:0b31dbcd4769 | 38 | clkval = extosc_frequency() / 1000000.0f; |
mbed_official | 227:7bd0639b8911 | 39 | } |
mbed_official | 82:0b31dbcd4769 | 40 | |
mbed_official | 82:0b31dbcd4769 | 41 | while (clkval > 1) { |
mbed_official | 82:0b31dbcd4769 | 42 | clkdiv++; |
mbed_official | 227:7bd0639b8911 | 43 | clkval /= 2.0; |
mbed_official | 82:0b31dbcd4769 | 44 | if (clkdiv == 7) |
mbed_official | 82:0b31dbcd4769 | 45 | break; |
mbed_official | 82:0b31dbcd4769 | 46 | } |
mbed_official | 82:0b31dbcd4769 | 47 | |
mbed_official | 82:0b31dbcd4769 | 48 | pwm_clock = clkval; |
mbed_official | 82:0b31dbcd4769 | 49 | unsigned int port = (unsigned int)pin >> PORT_SHIFT; |
mbed_official | 82:0b31dbcd4769 | 50 | unsigned int tpm_n = (pwm >> TPM_SHIFT); |
mbed_official | 82:0b31dbcd4769 | 51 | unsigned int ch_n = (pwm & 0xFF); |
mbed_official | 82:0b31dbcd4769 | 52 | |
mbed_official | 82:0b31dbcd4769 | 53 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
mbed_official | 82:0b31dbcd4769 | 54 | SIM->SCGC6 |= 1 << (SIM_SCGC6_TPM0_SHIFT + tpm_n); |
mbed_official | 82:0b31dbcd4769 | 55 | |
mbed_official | 82:0b31dbcd4769 | 56 | TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n); |
mbed_official | 82:0b31dbcd4769 | 57 | tpm->SC = TPM_SC_CMOD(1) | TPM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz |
mbed_official | 82:0b31dbcd4769 | 58 | tpm->CONTROLS[ch_n].CnSC = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */ |
mbed_official | 82:0b31dbcd4769 | 59 | |
mbed_official | 82:0b31dbcd4769 | 60 | obj->CnV = &tpm->CONTROLS[ch_n].CnV; |
mbed_official | 82:0b31dbcd4769 | 61 | obj->MOD = &tpm->MOD; |
mbed_official | 82:0b31dbcd4769 | 62 | obj->CNT = &tpm->CNT; |
mbed_official | 82:0b31dbcd4769 | 63 | |
mbed_official | 82:0b31dbcd4769 | 64 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
mbed_official | 82:0b31dbcd4769 | 65 | pwmout_period_ms(obj, 20); |
mbed_official | 82:0b31dbcd4769 | 66 | pwmout_write (obj, 0); |
mbed_official | 82:0b31dbcd4769 | 67 | |
mbed_official | 82:0b31dbcd4769 | 68 | // Wire pinout |
mbed_official | 82:0b31dbcd4769 | 69 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 82:0b31dbcd4769 | 70 | } |
mbed_official | 82:0b31dbcd4769 | 71 | |
mbed_official | 82:0b31dbcd4769 | 72 | void pwmout_free(pwmout_t* obj) {} |
mbed_official | 82:0b31dbcd4769 | 73 | |
mbed_official | 82:0b31dbcd4769 | 74 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 82:0b31dbcd4769 | 75 | if (value < 0.0) { |
mbed_official | 82:0b31dbcd4769 | 76 | value = 0.0; |
mbed_official | 82:0b31dbcd4769 | 77 | } else if (value > 1.0) { |
mbed_official | 82:0b31dbcd4769 | 78 | value = 1.0; |
mbed_official | 82:0b31dbcd4769 | 79 | } |
mbed_official | 82:0b31dbcd4769 | 80 | |
mbed_official | 264:91fcbf887208 | 81 | *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value); |
mbed_official | 82:0b31dbcd4769 | 82 | *obj->CNT = 0; |
mbed_official | 82:0b31dbcd4769 | 83 | } |
mbed_official | 82:0b31dbcd4769 | 84 | |
mbed_official | 82:0b31dbcd4769 | 85 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 264:91fcbf887208 | 86 | float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1); |
mbed_official | 82:0b31dbcd4769 | 87 | return (v > 1.0) ? (1.0) : (v); |
mbed_official | 82:0b31dbcd4769 | 88 | } |
mbed_official | 82:0b31dbcd4769 | 89 | |
mbed_official | 82:0b31dbcd4769 | 90 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 82:0b31dbcd4769 | 91 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 82:0b31dbcd4769 | 92 | } |
mbed_official | 82:0b31dbcd4769 | 93 | |
mbed_official | 82:0b31dbcd4769 | 94 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 82:0b31dbcd4769 | 95 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 82:0b31dbcd4769 | 96 | } |
mbed_official | 82:0b31dbcd4769 | 97 | |
mbed_official | 82:0b31dbcd4769 | 98 | // Set the PWM period, keeping the duty cycle the same. |
mbed_official | 82:0b31dbcd4769 | 99 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 82:0b31dbcd4769 | 100 | float dc = pwmout_read(obj); |
mbed_official | 264:91fcbf887208 | 101 | *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1; |
mbed_official | 82:0b31dbcd4769 | 102 | pwmout_write(obj, dc); |
mbed_official | 82:0b31dbcd4769 | 103 | } |
mbed_official | 82:0b31dbcd4769 | 104 | |
mbed_official | 82:0b31dbcd4769 | 105 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 82:0b31dbcd4769 | 106 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 82:0b31dbcd4769 | 107 | } |
mbed_official | 82:0b31dbcd4769 | 108 | |
mbed_official | 82:0b31dbcd4769 | 109 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 82:0b31dbcd4769 | 110 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 82:0b31dbcd4769 | 111 | } |
mbed_official | 82:0b31dbcd4769 | 112 | |
mbed_official | 82:0b31dbcd4769 | 113 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 82:0b31dbcd4769 | 114 | *obj->CnV = (uint32_t)(pwm_clock * (float)us); |
mbed_official | 82:0b31dbcd4769 | 115 | } |