mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Oct 23 09:45:08 2014 +0100
Revision:
363:12a245e5c745
Parent:
264:91fcbf887208
Synchronized with git revision 6ff7dfe464a15417f666938479c7860707707331

Full URL: https://github.com/mbedmicro/mbed/commit/6ff7dfe464a15417f666938479c7860707707331/

Target: Freescale FRDM-KL43Z addition

Who changed what in which revision?

UserRevisionLine numberNew 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 363:12a245e5c745 33
mbed_official 363:12a245e5c745 34 #if defined(TARGET_KL43Z)
mbed_official 363:12a245e5c745 35 if (mcgirc_frequency()) {
mbed_official 363:12a245e5c745 36 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(3); // Clock source: MCGIRCLK
mbed_official 363:12a245e5c745 37 clkval = mcgirc_frequency() / 1000000.0f;
mbed_official 363:12a245e5c745 38 } else {
mbed_official 363:12a245e5c745 39 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: IRC48M
mbed_official 363:12a245e5c745 40 clkval = CPU_INT_IRC_CLK_HZ / 1000000.0f;
mbed_official 363:12a245e5c745 41 }
mbed_official 363:12a245e5c745 42 #else
mbed_official 82:0b31dbcd4769 43 if (mcgpllfll_frequency()) {
mbed_official 82:0b31dbcd4769 44 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: MCGFLLCLK or MCGPLLCLK
mbed_official 82:0b31dbcd4769 45 clkval = mcgpllfll_frequency() / 1000000.0f;
mbed_official 82:0b31dbcd4769 46 } else {
mbed_official 82:0b31dbcd4769 47 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(2); // Clock source: ExtOsc
mbed_official 82:0b31dbcd4769 48 clkval = extosc_frequency() / 1000000.0f;
mbed_official 227:7bd0639b8911 49 }
mbed_official 363:12a245e5c745 50 #endif
mbed_official 82:0b31dbcd4769 51 while (clkval > 1) {
mbed_official 82:0b31dbcd4769 52 clkdiv++;
mbed_official 227:7bd0639b8911 53 clkval /= 2.0;
mbed_official 82:0b31dbcd4769 54 if (clkdiv == 7)
mbed_official 82:0b31dbcd4769 55 break;
mbed_official 82:0b31dbcd4769 56 }
mbed_official 82:0b31dbcd4769 57
mbed_official 82:0b31dbcd4769 58 pwm_clock = clkval;
mbed_official 363:12a245e5c745 59 unsigned int port = (unsigned int)pin >> PORT_SHIFT;
mbed_official 82:0b31dbcd4769 60 unsigned int tpm_n = (pwm >> TPM_SHIFT);
mbed_official 363:12a245e5c745 61 unsigned int ch_n = (pwm & 0xFF);
mbed_official 82:0b31dbcd4769 62
mbed_official 82:0b31dbcd4769 63 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
mbed_official 82:0b31dbcd4769 64 SIM->SCGC6 |= 1 << (SIM_SCGC6_TPM0_SHIFT + tpm_n);
mbed_official 82:0b31dbcd4769 65
mbed_official 82:0b31dbcd4769 66 TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n);
mbed_official 82:0b31dbcd4769 67 tpm->SC = TPM_SC_CMOD(1) | TPM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz
mbed_official 82:0b31dbcd4769 68 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 69
mbed_official 82:0b31dbcd4769 70 obj->CnV = &tpm->CONTROLS[ch_n].CnV;
mbed_official 82:0b31dbcd4769 71 obj->MOD = &tpm->MOD;
mbed_official 82:0b31dbcd4769 72 obj->CNT = &tpm->CNT;
mbed_official 82:0b31dbcd4769 73
mbed_official 82:0b31dbcd4769 74 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 82:0b31dbcd4769 75 pwmout_period_ms(obj, 20);
mbed_official 82:0b31dbcd4769 76 pwmout_write (obj, 0);
mbed_official 82:0b31dbcd4769 77
mbed_official 82:0b31dbcd4769 78 // Wire pinout
mbed_official 82:0b31dbcd4769 79 pinmap_pinout(pin, PinMap_PWM);
mbed_official 82:0b31dbcd4769 80 }
mbed_official 82:0b31dbcd4769 81
mbed_official 82:0b31dbcd4769 82 void pwmout_free(pwmout_t* obj) {}
mbed_official 82:0b31dbcd4769 83
mbed_official 82:0b31dbcd4769 84 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 82:0b31dbcd4769 85 if (value < 0.0) {
mbed_official 82:0b31dbcd4769 86 value = 0.0;
mbed_official 82:0b31dbcd4769 87 } else if (value > 1.0) {
mbed_official 82:0b31dbcd4769 88 value = 1.0;
mbed_official 82:0b31dbcd4769 89 }
mbed_official 82:0b31dbcd4769 90
mbed_official 264:91fcbf887208 91 *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value);
mbed_official 82:0b31dbcd4769 92 *obj->CNT = 0;
mbed_official 82:0b31dbcd4769 93 }
mbed_official 82:0b31dbcd4769 94
mbed_official 82:0b31dbcd4769 95 float pwmout_read(pwmout_t* obj) {
mbed_official 264:91fcbf887208 96 float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1);
mbed_official 82:0b31dbcd4769 97 return (v > 1.0) ? (1.0) : (v);
mbed_official 82:0b31dbcd4769 98 }
mbed_official 82:0b31dbcd4769 99
mbed_official 82:0b31dbcd4769 100 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 82:0b31dbcd4769 101 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 82:0b31dbcd4769 102 }
mbed_official 82:0b31dbcd4769 103
mbed_official 82:0b31dbcd4769 104 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 82:0b31dbcd4769 105 pwmout_period_us(obj, ms * 1000);
mbed_official 82:0b31dbcd4769 106 }
mbed_official 82:0b31dbcd4769 107
mbed_official 82:0b31dbcd4769 108 // Set the PWM period, keeping the duty cycle the same.
mbed_official 82:0b31dbcd4769 109 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 82:0b31dbcd4769 110 float dc = pwmout_read(obj);
mbed_official 264:91fcbf887208 111 *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1;
mbed_official 82:0b31dbcd4769 112 pwmout_write(obj, dc);
mbed_official 82:0b31dbcd4769 113 }
mbed_official 82:0b31dbcd4769 114
mbed_official 82:0b31dbcd4769 115 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 82:0b31dbcd4769 116 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 82:0b31dbcd4769 117 }
mbed_official 82:0b31dbcd4769 118
mbed_official 82:0b31dbcd4769 119 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 82:0b31dbcd4769 120 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 82:0b31dbcd4769 121 }
mbed_official 82:0b31dbcd4769 122
mbed_official 82:0b31dbcd4769 123 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 82:0b31dbcd4769 124 *obj->CnV = (uint32_t)(pwm_clock * (float)us);
mbed_official 82:0b31dbcd4769 125 }