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