mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/pwmout_api.c@267:8673334f2cbe, 2014-07-29 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 29 18:45:06 2014 +0100
- Revision:
- 267:8673334f2cbe
- Parent:
- 265:9632ea190e16
Synchronized with git revision 1d586e1f8df5e4ff9eb4b8420095fd3f74426163
Full URL: https://github.com/mbedmicro/mbed/commit/1d586e1f8df5e4ff9eb4b8420095fd3f74426163/
Had duplicate set of api drivers in the directory - deleted
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 146:f64d43ff0c18 | 1 | /* mbed Microcontroller Library |
mbed_official | 146:f64d43ff0c18 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 146:f64d43ff0c18 | 3 | * |
mbed_official | 146:f64d43ff0c18 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 146:f64d43ff0c18 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 146:f64d43ff0c18 | 6 | * You may obtain a copy of the License at |
mbed_official | 146:f64d43ff0c18 | 7 | * |
mbed_official | 146:f64d43ff0c18 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 146:f64d43ff0c18 | 9 | * |
mbed_official | 146:f64d43ff0c18 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 146:f64d43ff0c18 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 146:f64d43ff0c18 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 146:f64d43ff0c18 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 146:f64d43ff0c18 | 14 | * limitations under the License. |
mbed_official | 146:f64d43ff0c18 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 146:f64d43ff0c18 | 17 | #include "pwmout_api.h" |
mbed_official | 146:f64d43ff0c18 | 18 | |
mbed_official | 267:8673334f2cbe | 19 | #if DEVICE_PWMOUT |
mbed_official | 267:8673334f2cbe | 20 | |
mbed_official | 146:f64d43ff0c18 | 21 | #include "cmsis.h" |
mbed_official | 146:f64d43ff0c18 | 22 | #include "pinmap.h" |
mbed_official | 146:f64d43ff0c18 | 23 | #include "fsl_ftm_hal.h" |
mbed_official | 146:f64d43ff0c18 | 24 | #include "fsl_mcg_hal.h" |
mbed_official | 146:f64d43ff0c18 | 25 | #include "fsl_clock_manager.h" |
mbed_official | 265:9632ea190e16 | 26 | #include "PeripheralPins.h" |
mbed_official | 146:f64d43ff0c18 | 27 | |
mbed_official | 152:3248c4875353 | 28 | static float pwm_clock_mhz; |
mbed_official | 146:f64d43ff0c18 | 29 | |
mbed_official | 146:f64d43ff0c18 | 30 | void pwmout_init(pwmout_t* obj, PinName pin) { |
mbed_official | 146:f64d43ff0c18 | 31 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
mbed_official | 227:7bd0639b8911 | 32 | MBED_ASSERT(pwm != (PWMName)NC); |
mbed_official | 227:7bd0639b8911 | 33 | |
mbed_official | 146:f64d43ff0c18 | 34 | obj->pwm_name = pwm; |
mbed_official | 146:f64d43ff0c18 | 35 | |
mbed_official | 152:3248c4875353 | 36 | uint32_t pwm_base_clock; |
mbed_official | 152:3248c4875353 | 37 | clock_manager_get_frequency(kBusClock, &pwm_base_clock); |
mbed_official | 152:3248c4875353 | 38 | float clkval = (float)pwm_base_clock / 1000000.0f; |
mbed_official | 146:f64d43ff0c18 | 39 | uint32_t clkdiv = 0; |
mbed_official | 146:f64d43ff0c18 | 40 | while (clkval > 1) { |
mbed_official | 146:f64d43ff0c18 | 41 | clkdiv++; |
mbed_official | 146:f64d43ff0c18 | 42 | clkval /= 2.0f; |
mbed_official | 146:f64d43ff0c18 | 43 | if (clkdiv == 7) { |
mbed_official | 146:f64d43ff0c18 | 44 | break; |
mbed_official | 146:f64d43ff0c18 | 45 | } |
mbed_official | 146:f64d43ff0c18 | 46 | } |
mbed_official | 150:49699a7d7064 | 47 | |
mbed_official | 152:3248c4875353 | 48 | pwm_clock_mhz = clkval; |
mbed_official | 146:f64d43ff0c18 | 49 | uint32_t channel = pwm & 0xF; |
mbed_official | 146:f64d43ff0c18 | 50 | uint32_t instance = pwm >> TPM_SHIFT; |
mbed_official | 146:f64d43ff0c18 | 51 | clock_manager_set_gate(kClockModuleFTM, instance, true); |
mbed_official | 150:49699a7d7064 | 52 | ftm_hal_set_tof_frequency(instance, 3); |
mbed_official | 152:3248c4875353 | 53 | ftm_hal_set_clock_source(instance, kClock_source_FTM_SystemClk); |
mbed_official | 146:f64d43ff0c18 | 54 | ftm_hal_set_clock_ps(instance, (ftm_clock_ps_t)clkdiv); |
mbed_official | 152:3248c4875353 | 55 | ftm_hal_set_counter_init_val(instance, 0); |
mbed_official | 146:f64d43ff0c18 | 56 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
mbed_official | 146:f64d43ff0c18 | 57 | pwmout_period_ms(obj, 20); |
mbed_official | 146:f64d43ff0c18 | 58 | pwmout_write (obj, 0); |
mbed_official | 152:3248c4875353 | 59 | ftm_config_t config = { |
mbed_official | 152:3248c4875353 | 60 | .mode = kFtmEdgeAlignedPWM, |
mbed_official | 152:3248c4875353 | 61 | .channel = channel, |
mbed_official | 152:3248c4875353 | 62 | .edge_mode = {.ftm_pwm_edge_mode = kFtmHighTrue} |
mbed_official | 152:3248c4875353 | 63 | }; |
mbed_official | 152:3248c4875353 | 64 | ftm_hal_enable_pwm_mode(instance, &config); |
mbed_official | 146:f64d43ff0c18 | 65 | |
mbed_official | 146:f64d43ff0c18 | 66 | // Wire pinout |
mbed_official | 146:f64d43ff0c18 | 67 | pinmap_pinout(pin, PinMap_PWM); |
mbed_official | 146:f64d43ff0c18 | 68 | } |
mbed_official | 146:f64d43ff0c18 | 69 | |
mbed_official | 146:f64d43ff0c18 | 70 | void pwmout_free(pwmout_t* obj) { |
mbed_official | 146:f64d43ff0c18 | 71 | } |
mbed_official | 146:f64d43ff0c18 | 72 | |
mbed_official | 146:f64d43ff0c18 | 73 | void pwmout_write(pwmout_t* obj, float value) { |
mbed_official | 153:a2bd7206fd97 | 74 | uint32_t instance = obj->pwm_name >> TPM_SHIFT; |
mbed_official | 146:f64d43ff0c18 | 75 | if (value < 0.0f) { |
mbed_official | 146:f64d43ff0c18 | 76 | value = 0.0f; |
mbed_official | 146:f64d43ff0c18 | 77 | } else if (value > 1.0f) { |
mbed_official | 146:f64d43ff0c18 | 78 | value = 1.0f; |
mbed_official | 146:f64d43ff0c18 | 79 | } |
mbed_official | 153:a2bd7206fd97 | 80 | uint16_t mod = ftm_hal_get_mod(instance); |
mbed_official | 146:f64d43ff0c18 | 81 | uint32_t new_count = (uint32_t)((float)(mod) * value); |
mbed_official | 153:a2bd7206fd97 | 82 | // Stop FTM clock to ensure instant update of MOD register |
mbed_official | 153:a2bd7206fd97 | 83 | ftm_hal_set_clock_source(instance, kClock_source_FTM_None); |
mbed_official | 153:a2bd7206fd97 | 84 | ftm_hal_set_channel_count_value(instance, obj->pwm_name & 0xF, new_count); |
mbed_official | 153:a2bd7206fd97 | 85 | ftm_hal_set_counter(instance, 0); |
mbed_official | 153:a2bd7206fd97 | 86 | ftm_hal_set_clock_source(instance, kClock_source_FTM_SystemClk); |
mbed_official | 146:f64d43ff0c18 | 87 | } |
mbed_official | 146:f64d43ff0c18 | 88 | |
mbed_official | 146:f64d43ff0c18 | 89 | float pwmout_read(pwmout_t* obj) { |
mbed_official | 146:f64d43ff0c18 | 90 | uint16_t count = ftm_hal_get_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, 0); |
mbed_official | 146:f64d43ff0c18 | 91 | uint16_t mod = ftm_hal_get_mod(obj->pwm_name >> TPM_SHIFT); |
mbed_official | 152:3248c4875353 | 92 | if (mod == 0) |
mbed_official | 152:3248c4875353 | 93 | return 0.0; |
mbed_official | 146:f64d43ff0c18 | 94 | float v = (float)(count) / (float)(mod); |
mbed_official | 146:f64d43ff0c18 | 95 | return (v > 1.0f) ? (1.0f) : (v); |
mbed_official | 146:f64d43ff0c18 | 96 | } |
mbed_official | 146:f64d43ff0c18 | 97 | |
mbed_official | 146:f64d43ff0c18 | 98 | void pwmout_period(pwmout_t* obj, float seconds) { |
mbed_official | 146:f64d43ff0c18 | 99 | pwmout_period_us(obj, seconds * 1000000.0f); |
mbed_official | 146:f64d43ff0c18 | 100 | } |
mbed_official | 146:f64d43ff0c18 | 101 | |
mbed_official | 146:f64d43ff0c18 | 102 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
mbed_official | 146:f64d43ff0c18 | 103 | pwmout_period_us(obj, ms * 1000); |
mbed_official | 146:f64d43ff0c18 | 104 | } |
mbed_official | 146:f64d43ff0c18 | 105 | |
mbed_official | 146:f64d43ff0c18 | 106 | // Set the PWM period, keeping the duty cycle the same. |
mbed_official | 146:f64d43ff0c18 | 107 | void pwmout_period_us(pwmout_t* obj, int us) { |
mbed_official | 152:3248c4875353 | 108 | uint32_t instance = obj->pwm_name >> TPM_SHIFT; |
mbed_official | 146:f64d43ff0c18 | 109 | float dc = pwmout_read(obj); |
mbed_official | 152:3248c4875353 | 110 | // Stop FTM clock to ensure instant update of MOD register |
mbed_official | 152:3248c4875353 | 111 | ftm_hal_set_clock_source(instance, kClock_source_FTM_None); |
mbed_official | 152:3248c4875353 | 112 | ftm_hal_set_mod(instance, (uint32_t)(pwm_clock_mhz * (float)us) - 1); |
mbed_official | 146:f64d43ff0c18 | 113 | pwmout_write(obj, dc); |
mbed_official | 152:3248c4875353 | 114 | ftm_hal_set_clock_source(instance, kClock_source_FTM_SystemClk); |
mbed_official | 146:f64d43ff0c18 | 115 | } |
mbed_official | 146:f64d43ff0c18 | 116 | |
mbed_official | 146:f64d43ff0c18 | 117 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
mbed_official | 146:f64d43ff0c18 | 118 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
mbed_official | 146:f64d43ff0c18 | 119 | } |
mbed_official | 146:f64d43ff0c18 | 120 | |
mbed_official | 146:f64d43ff0c18 | 121 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
mbed_official | 146:f64d43ff0c18 | 122 | pwmout_pulsewidth_us(obj, ms * 1000); |
mbed_official | 146:f64d43ff0c18 | 123 | } |
mbed_official | 146:f64d43ff0c18 | 124 | |
mbed_official | 146:f64d43ff0c18 | 125 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 152:3248c4875353 | 126 | uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us); |
mbed_official | 146:f64d43ff0c18 | 127 | ftm_hal_set_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, value); |
mbed_official | 146:f64d43ff0c18 | 128 | } |
mbed_official | 267:8673334f2cbe | 129 | |
mbed_official | 267:8673334f2cbe | 130 | #endif |