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:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
324:406fd2029f23
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

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

ignore target files not supported by the yotta module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 324:406fd2029f23 1 /* mbed Microcontroller Library
mbed_official 324:406fd2029f23 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 324:406fd2029f23 3 *
mbed_official 324:406fd2029f23 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 324:406fd2029f23 5 * you may not use this file except in compliance with the License.
mbed_official 324:406fd2029f23 6 * You may obtain a copy of the License at
mbed_official 324:406fd2029f23 7 *
mbed_official 324:406fd2029f23 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 324:406fd2029f23 9 *
mbed_official 324:406fd2029f23 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 324:406fd2029f23 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 324:406fd2029f23 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 324:406fd2029f23 13 * See the License for the specific language governing permissions and
mbed_official 324:406fd2029f23 14 * limitations under the License.
mbed_official 324:406fd2029f23 15 */
mbed_official 324:406fd2029f23 16 #include "mbed_assert.h"
mbed_official 324:406fd2029f23 17 #include "pwmout_api.h"
mbed_official 324:406fd2029f23 18
mbed_official 324:406fd2029f23 19 #if DEVICE_PWMOUT
mbed_official 324:406fd2029f23 20
mbed_official 324:406fd2029f23 21 #include "cmsis.h"
mbed_official 324:406fd2029f23 22 #include "pinmap.h"
mbed_official 324:406fd2029f23 23 #include "fsl_ftm_hal.h"
mbed_official 324:406fd2029f23 24 #include "fsl_mcg_hal.h"
mbed_official 324:406fd2029f23 25 #include "fsl_clock_manager.h"
mbed_official 324:406fd2029f23 26 #include "PeripheralPins.h"
mbed_official 324:406fd2029f23 27
mbed_official 324:406fd2029f23 28 static float pwm_clock_mhz;
mbed_official 324:406fd2029f23 29
mbed_official 324:406fd2029f23 30 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 324:406fd2029f23 31 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 324:406fd2029f23 32 MBED_ASSERT(pwm != (PWMName)NC);
mbed_official 324:406fd2029f23 33
mbed_official 324:406fd2029f23 34 obj->pwm_name = pwm;
mbed_official 324:406fd2029f23 35
mbed_official 324:406fd2029f23 36 uint32_t pwm_base_clock;
mbed_official 324:406fd2029f23 37 CLOCK_SYS_GetFreq(kBusClock, &pwm_base_clock);
mbed_official 324:406fd2029f23 38 float clkval = (float)pwm_base_clock / 1000000.0f;
mbed_official 324:406fd2029f23 39 uint32_t clkdiv = 0;
mbed_official 324:406fd2029f23 40 while (clkval > 1) {
mbed_official 324:406fd2029f23 41 clkdiv++;
mbed_official 324:406fd2029f23 42 clkval /= 2.0f;
mbed_official 324:406fd2029f23 43 if (clkdiv == 7) {
mbed_official 324:406fd2029f23 44 break;
mbed_official 324:406fd2029f23 45 }
mbed_official 324:406fd2029f23 46 }
mbed_official 324:406fd2029f23 47
mbed_official 324:406fd2029f23 48 pwm_clock_mhz = clkval;
mbed_official 324:406fd2029f23 49 uint32_t channel = pwm & 0xF;
mbed_official 324:406fd2029f23 50 uint32_t instance = pwm >> TPM_SHIFT;
mbed_official 324:406fd2029f23 51 uint32_t ftm_addrs[] = FTM_BASE_ADDRS;
mbed_official 324:406fd2029f23 52 CLOCK_SYS_EnableFtmClock(instance);
mbed_official 324:406fd2029f23 53
mbed_official 324:406fd2029f23 54 FTM_HAL_SetTofFreq(ftm_addrs[instance], 3);
mbed_official 324:406fd2029f23 55 FTM_HAL_SetClockSource(ftm_addrs[instance], kClock_source_FTM_SystemClk);
mbed_official 324:406fd2029f23 56 FTM_HAL_SetClockPs(ftm_addrs[instance], (ftm_clock_ps_t)clkdiv);
mbed_official 324:406fd2029f23 57 FTM_HAL_SetCounter(ftm_addrs[instance], 0);
mbed_official 324:406fd2029f23 58 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 324:406fd2029f23 59 pwmout_period_ms(obj, 20);
mbed_official 324:406fd2029f23 60 pwmout_write (obj, 0);
mbed_official 324:406fd2029f23 61 ftm_pwm_param_t config = {
mbed_official 324:406fd2029f23 62 .mode = kFtmEdgeAlignedPWM,
mbed_official 324:406fd2029f23 63 .edgeMode = kFtmHighTrue
mbed_official 324:406fd2029f23 64 };
mbed_official 324:406fd2029f23 65 FTM_HAL_EnablePwmMode(ftm_addrs[instance], &config, channel);
mbed_official 324:406fd2029f23 66
mbed_official 324:406fd2029f23 67 // Wire pinout
mbed_official 324:406fd2029f23 68 pinmap_pinout(pin, PinMap_PWM);
mbed_official 324:406fd2029f23 69 }
mbed_official 324:406fd2029f23 70
mbed_official 324:406fd2029f23 71 void pwmout_free(pwmout_t* obj) {
mbed_official 324:406fd2029f23 72 }
mbed_official 324:406fd2029f23 73
mbed_official 324:406fd2029f23 74 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 324:406fd2029f23 75 uint32_t instance = obj->pwm_name >> TPM_SHIFT;
mbed_official 324:406fd2029f23 76 if (value < 0.0f) {
mbed_official 324:406fd2029f23 77 value = 0.0f;
mbed_official 324:406fd2029f23 78 } else if (value > 1.0f) {
mbed_official 324:406fd2029f23 79 value = 1.0f;
mbed_official 324:406fd2029f23 80 }
mbed_official 324:406fd2029f23 81 uint32_t ftm_addrs[] = FTM_BASE_ADDRS;
mbed_official 324:406fd2029f23 82 uint16_t mod = FTM_HAL_GetMod(ftm_addrs[instance]);
mbed_official 324:406fd2029f23 83 uint32_t new_count = (uint32_t)((float)(mod) * value);
mbed_official 324:406fd2029f23 84 // Stop FTM clock to ensure instant update of MOD register
mbed_official 324:406fd2029f23 85 FTM_HAL_SetClockSource(ftm_addrs[instance], kClock_source_FTM_None);
mbed_official 324:406fd2029f23 86 FTM_HAL_SetChnCountVal(ftm_addrs[instance], obj->pwm_name & 0xF, new_count);
mbed_official 324:406fd2029f23 87 FTM_HAL_SetCounter(ftm_addrs[instance], 0);
mbed_official 324:406fd2029f23 88 FTM_HAL_SetClockSource(ftm_addrs[instance], kClock_source_FTM_SystemClk);
mbed_official 324:406fd2029f23 89 }
mbed_official 324:406fd2029f23 90
mbed_official 324:406fd2029f23 91 float pwmout_read(pwmout_t* obj) {
mbed_official 324:406fd2029f23 92 uint32_t ftm_addrs[] = FTM_BASE_ADDRS;
mbed_official 324:406fd2029f23 93 uint16_t count = FTM_HAL_GetChnCountVal(ftm_addrs[obj->pwm_name >> TPM_SHIFT], obj->pwm_name & 0xF, 0);
mbed_official 324:406fd2029f23 94 uint16_t mod = FTM_HAL_GetMod(ftm_addrs[obj->pwm_name >> TPM_SHIFT]);
mbed_official 324:406fd2029f23 95 if (mod == 0)
mbed_official 324:406fd2029f23 96 return 0.0;
mbed_official 324:406fd2029f23 97 float v = (float)(count) / (float)(mod);
mbed_official 324:406fd2029f23 98 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 324:406fd2029f23 99 }
mbed_official 324:406fd2029f23 100
mbed_official 324:406fd2029f23 101 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 324:406fd2029f23 102 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 324:406fd2029f23 103 }
mbed_official 324:406fd2029f23 104
mbed_official 324:406fd2029f23 105 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 324:406fd2029f23 106 pwmout_period_us(obj, ms * 1000);
mbed_official 324:406fd2029f23 107 }
mbed_official 324:406fd2029f23 108
mbed_official 324:406fd2029f23 109 // Set the PWM period, keeping the duty cycle the same.
mbed_official 324:406fd2029f23 110 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 324:406fd2029f23 111 uint32_t instance = obj->pwm_name >> TPM_SHIFT;
mbed_official 324:406fd2029f23 112 uint32_t ftm_addrs[] = FTM_BASE_ADDRS;
mbed_official 324:406fd2029f23 113 float dc = pwmout_read(obj);
mbed_official 324:406fd2029f23 114 // Stop FTM clock to ensure instant update of MOD register
mbed_official 324:406fd2029f23 115 FTM_HAL_SetClockSource(ftm_addrs[instance], kClock_source_FTM_None);
mbed_official 324:406fd2029f23 116 FTM_HAL_SetMod(ftm_addrs[instance], (uint32_t)(pwm_clock_mhz * (float)us) - 1);
mbed_official 324:406fd2029f23 117 pwmout_write(obj, dc);
mbed_official 324:406fd2029f23 118 FTM_HAL_SetClockSource(ftm_addrs[instance], kClock_source_FTM_SystemClk);
mbed_official 324:406fd2029f23 119 }
mbed_official 324:406fd2029f23 120
mbed_official 324:406fd2029f23 121 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 324:406fd2029f23 122 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 324:406fd2029f23 123 }
mbed_official 324:406fd2029f23 124
mbed_official 324:406fd2029f23 125 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 324:406fd2029f23 126 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 324:406fd2029f23 127 }
mbed_official 324:406fd2029f23 128
mbed_official 324:406fd2029f23 129 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 324:406fd2029f23 130 uint32_t ftm_addrs[] = FTM_BASE_ADDRS;
mbed_official 324:406fd2029f23 131 uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us);
mbed_official 324:406fd2029f23 132 FTM_HAL_SetChnCountVal(ftm_addrs[obj->pwm_name >> TPM_SHIFT], obj->pwm_name & 0xF, value);
mbed_official 324:406fd2029f23 133 }
mbed_official 324:406fd2029f23 134
mbed_official 324:406fd2029f23 135 #endif