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 Apr 04 10:45:08 2014 +0100
Revision:
150:49699a7d7064
Parent:
149:1fb5f62b92bd
Child:
152:3248c4875353
Synchronized with git revision d4566711265f6e274f03a79a4819636519bf3a42

Full URL: https://github.com/mbedmicro/mbed/commit/d4566711265f6e274f03a79a4819636519bf3a42/

K64F - port HAL corrections, pwm clock configuration store

Who changed what in which revision?

UserRevisionLine numberNew 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 146:f64d43ff0c18 16 #include "pwmout_api.h"
mbed_official 146:f64d43ff0c18 17
mbed_official 146:f64d43ff0c18 18 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 19 #include "pinmap.h"
mbed_official 146:f64d43ff0c18 20 #include "error.h"
mbed_official 146:f64d43ff0c18 21 #include "fsl_ftm_hal.h"
mbed_official 146:f64d43ff0c18 22 #include "fsl_mcg_hal.h"
mbed_official 146:f64d43ff0c18 23 #include "fsl_clock_manager.h"
mbed_official 146:f64d43ff0c18 24
mbed_official 146:f64d43ff0c18 25 static const PinMap PinMap_PWM[] = {
mbed_official 147:39a1839cac63 26 {PTA0 , PWM_6 , 3},
mbed_official 147:39a1839cac63 27 {PTA1 , PWM_7 , 3},
mbed_official 147:39a1839cac63 28 {PTA2 , PWM_8 , 3},
mbed_official 147:39a1839cac63 29 {PTA3 , PWM_1 , 3},
mbed_official 147:39a1839cac63 30 {PTA4 , PWM_2 , 3},
mbed_official 147:39a1839cac63 31 {PTA5 , PWM_3 , 3},
mbed_official 147:39a1839cac63 32 {PTA6 , PWM_4 , 3},
mbed_official 147:39a1839cac63 33 {PTA7 , PWM_5 , 3},
mbed_official 147:39a1839cac63 34 {PTA8 , PWM_9 , 3},
mbed_official 147:39a1839cac63 35 {PTA9 , PWM_10, 3},
mbed_official 147:39a1839cac63 36 {PTA10, PWM_17, 3},
mbed_official 147:39a1839cac63 37 {PTA11, PWM_18, 3},
mbed_official 147:39a1839cac63 38 {PTA12, PWM_9 , 3},
mbed_official 147:39a1839cac63 39 {PTA13, PWM_10, 3},
mbed_official 147:39a1839cac63 40
mbed_official 147:39a1839cac63 41 {PTB0 , PWM_9 , 3},
mbed_official 147:39a1839cac63 42 {PTB1 , PWM_10, 3},
mbed_official 146:f64d43ff0c18 43 {PTB18, PWM_17, 3},
mbed_official 147:39a1839cac63 44 {PTB19, PWM_18, 3},
mbed_official 147:39a1839cac63 45
mbed_official 147:39a1839cac63 46 {PTC1 , PWM_1 , 4},
mbed_official 147:39a1839cac63 47 {PTC2 , PWM_2 , 4},
mbed_official 147:39a1839cac63 48 {PTC3 , PWM_3 , 4},
mbed_official 147:39a1839cac63 49 {PTC4 , PWM_4 , 4},
mbed_official 147:39a1839cac63 50 {PTC5 , PWM_3 , 7},
mbed_official 147:39a1839cac63 51 {PTC8 , PWM_29, 3},
mbed_official 147:39a1839cac63 52 {PTC9 , PWM_30, 3},
mbed_official 147:39a1839cac63 53 {PTC10, PWM_31, 3},
mbed_official 147:39a1839cac63 54 {PTC11, PWM_32, 3},
mbed_official 147:39a1839cac63 55
mbed_official 147:39a1839cac63 56 {PTD0 , PWM_25, 4},
mbed_official 147:39a1839cac63 57 {PTD1 , PWM_26, 4},
mbed_official 147:39a1839cac63 58 {PTD2 , PWM_27, 4},
mbed_official 147:39a1839cac63 59 {PTD3 , PWM_28, 4},
mbed_official 147:39a1839cac63 60 {PTD4 , PWM_5 , 4},
mbed_official 147:39a1839cac63 61 {PTD5 , PWM_6 , 4},
mbed_official 147:39a1839cac63 62 {PTD6 , PWM_7 , 4},
mbed_official 147:39a1839cac63 63 {PTD4 , PWM_5 , 4},
mbed_official 147:39a1839cac63 64 {PTD7 , PWM_8 , 4},
mbed_official 147:39a1839cac63 65
mbed_official 147:39a1839cac63 66 {PTE5 , PWM_25, 6},
mbed_official 147:39a1839cac63 67 {PTE6 , PWM_26, 6},
mbed_official 147:39a1839cac63 68
mbed_official 146:f64d43ff0c18 69 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 70 };
mbed_official 146:f64d43ff0c18 71
mbed_official 146:f64d43ff0c18 72 static float pwm_clock;
mbed_official 146:f64d43ff0c18 73
mbed_official 146:f64d43ff0c18 74 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 146:f64d43ff0c18 75 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 146:f64d43ff0c18 76 if (pwm == (PWMName)NC) {
mbed_official 146:f64d43ff0c18 77 error("PwmOut pin mapping failed");
mbed_official 146:f64d43ff0c18 78 }
mbed_official 146:f64d43ff0c18 79 obj->pwm_name = pwm;
mbed_official 146:f64d43ff0c18 80
mbed_official 146:f64d43ff0c18 81 float clkval = clock_hal_get_fllclk() / 1000000.0f;
mbed_official 146:f64d43ff0c18 82 uint32_t clkdiv = 0;
mbed_official 146:f64d43ff0c18 83 while (clkval > 1) {
mbed_official 146:f64d43ff0c18 84 clkdiv++;
mbed_official 146:f64d43ff0c18 85 clkval /= 2.0f;
mbed_official 146:f64d43ff0c18 86 if (clkdiv == 7) {
mbed_official 146:f64d43ff0c18 87 break;
mbed_official 146:f64d43ff0c18 88 }
mbed_official 146:f64d43ff0c18 89 }
mbed_official 150:49699a7d7064 90
mbed_official 150:49699a7d7064 91 pwm_clock = clkval;
mbed_official 146:f64d43ff0c18 92 uint32_t channel = pwm & 0xF;
mbed_official 146:f64d43ff0c18 93 uint32_t instance = pwm >> TPM_SHIFT;
mbed_official 146:f64d43ff0c18 94 clock_manager_set_gate(kClockModuleFTM, instance, true);
mbed_official 150:49699a7d7064 95 ftm_hal_set_tof_frequency(instance, 3);
mbed_official 146:f64d43ff0c18 96 ftm_hal_set_clock_ps(instance, (ftm_clock_ps_t)clkdiv);
mbed_official 146:f64d43ff0c18 97 ftm_hal_set_clock_source(instance, kClock_source_FTM_SystemClk);
mbed_official 146:f64d43ff0c18 98 ftm_hal_set_channel_edge_level(instance, channel, 2);
mbed_official 146:f64d43ff0c18 99 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 146:f64d43ff0c18 100 pwmout_period_ms(obj, 20);
mbed_official 146:f64d43ff0c18 101 pwmout_write (obj, 0);
mbed_official 146:f64d43ff0c18 102
mbed_official 146:f64d43ff0c18 103 // Wire pinout
mbed_official 146:f64d43ff0c18 104 pinmap_pinout(pin, PinMap_PWM);
mbed_official 146:f64d43ff0c18 105 }
mbed_official 146:f64d43ff0c18 106
mbed_official 146:f64d43ff0c18 107 void pwmout_free(pwmout_t* obj) {
mbed_official 146:f64d43ff0c18 108 }
mbed_official 146:f64d43ff0c18 109
mbed_official 146:f64d43ff0c18 110 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 146:f64d43ff0c18 111 if (value < 0.0f) {
mbed_official 146:f64d43ff0c18 112 value = 0.0f;
mbed_official 146:f64d43ff0c18 113 } else if (value > 1.0f) {
mbed_official 146:f64d43ff0c18 114 value = 1.0f;
mbed_official 146:f64d43ff0c18 115 }
mbed_official 146:f64d43ff0c18 116 uint16_t mod = ftm_hal_get_mod(obj->pwm_name >> TPM_SHIFT);
mbed_official 146:f64d43ff0c18 117 uint32_t new_count = (uint32_t)((float)(mod) * value);
mbed_official 146:f64d43ff0c18 118 ftm_hal_set_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, new_count);
mbed_official 146:f64d43ff0c18 119 }
mbed_official 146:f64d43ff0c18 120
mbed_official 146:f64d43ff0c18 121 float pwmout_read(pwmout_t* obj) {
mbed_official 146:f64d43ff0c18 122 uint16_t count = ftm_hal_get_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, 0);
mbed_official 146:f64d43ff0c18 123 uint16_t mod = ftm_hal_get_mod(obj->pwm_name >> TPM_SHIFT);
mbed_official 146:f64d43ff0c18 124 float v = (float)(count) / (float)(mod);
mbed_official 146:f64d43ff0c18 125 return (v > 1.0f) ? (1.0f) : (v);
mbed_official 146:f64d43ff0c18 126 }
mbed_official 146:f64d43ff0c18 127
mbed_official 146:f64d43ff0c18 128 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 146:f64d43ff0c18 129 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 146:f64d43ff0c18 130 }
mbed_official 146:f64d43ff0c18 131
mbed_official 146:f64d43ff0c18 132 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 146:f64d43ff0c18 133 pwmout_period_us(obj, ms * 1000);
mbed_official 146:f64d43ff0c18 134 }
mbed_official 146:f64d43ff0c18 135
mbed_official 146:f64d43ff0c18 136 // Set the PWM period, keeping the duty cycle the same.
mbed_official 146:f64d43ff0c18 137 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 146:f64d43ff0c18 138 float dc = pwmout_read(obj);
mbed_official 146:f64d43ff0c18 139 ftm_hal_set_mod(obj->pwm_name >> TPM_SHIFT, (uint32_t)(pwm_clock * (float)us));
mbed_official 146:f64d43ff0c18 140 pwmout_write(obj, dc);
mbed_official 146:f64d43ff0c18 141
mbed_official 146:f64d43ff0c18 142 }
mbed_official 146:f64d43ff0c18 143
mbed_official 146:f64d43ff0c18 144 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 146:f64d43ff0c18 145 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 146:f64d43ff0c18 146 }
mbed_official 146:f64d43ff0c18 147
mbed_official 146:f64d43ff0c18 148 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 146:f64d43ff0c18 149 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 146:f64d43ff0c18 150 }
mbed_official 146:f64d43ff0c18 151
mbed_official 146:f64d43ff0c18 152 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 146:f64d43ff0c18 153 uint32_t value = (uint32_t)(pwm_clock * (float)us);
mbed_official 146:f64d43ff0c18 154 ftm_hal_set_channel_count_value(obj->pwm_name >> TPM_SHIFT, obj->pwm_name & 0xF, value);
mbed_official 146:f64d43ff0c18 155 }