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