mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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