mbed-os
Fork of mbed-os by
targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c@1:3deb71413561, 2017-07-20 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2015 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 "PeripheralPins.h" |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | static float pwm_clock = 0; |
elessair | 0:f269e3021894 | 24 | |
elessair | 0:f269e3021894 | 25 | void pwmout_init(pwmout_t* obj, PinName pin) { |
elessair | 0:f269e3021894 | 26 | // determine the channel |
elessair | 0:f269e3021894 | 27 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
elessair | 0:f269e3021894 | 28 | MBED_ASSERT(pwm != (PWMName)NC); |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | uint32_t clkdiv = 0; |
elessair | 0:f269e3021894 | 31 | float clkval = SystemCoreClock / 1000000.0f; |
elessair | 0:f269e3021894 | 32 | |
elessair | 0:f269e3021894 | 33 | while (clkval > 1) { |
elessair | 0:f269e3021894 | 34 | clkdiv++; |
elessair | 0:f269e3021894 | 35 | clkval /= 2.0; |
elessair | 0:f269e3021894 | 36 | if (clkdiv == 7) |
elessair | 0:f269e3021894 | 37 | break; |
elessair | 0:f269e3021894 | 38 | } |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | pwm_clock = clkval; |
elessair | 0:f269e3021894 | 41 | unsigned int ftm_n = (pwm >> TPM_SHIFT); |
elessair | 0:f269e3021894 | 42 | unsigned int ch_n = (pwm & 0xFF); |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n); |
elessair | 0:f269e3021894 | 45 | |
elessair | 0:f269e3021894 | 46 | FTM_Type *ftm = (FTM_Type *)(FTM0_BASE + 0x1000 * ftm_n); |
elessair | 0:f269e3021894 | 47 | ftm->CONF |= FTM_CONF_BDMMODE(3); |
elessair | 0:f269e3021894 | 48 | ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz |
elessair | 0:f269e3021894 | 49 | ftm->CONTROLS[ch_n].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */ |
elessair | 0:f269e3021894 | 50 | ftm->MODE = FTM_MODE_FTMEN_MASK; |
elessair | 0:f269e3021894 | 51 | ftm->SYNC = FTM_SYNC_CNTMIN_MASK; |
elessair | 0:f269e3021894 | 52 | ftm->SYNCONF = FTM_SYNCONF_SYNCMODE_MASK | FTM_SYNCONF_SWSOC_MASK | FTM_SYNCONF_SWWRBUF_MASK; |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | //Without SYNCEN set CnV does not seem to update |
elessair | 0:f269e3021894 | 55 | ftm->COMBINE = FTM_COMBINE_SYNCEN0_MASK | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_SYNCEN2_MASK | FTM_COMBINE_SYNCEN3_MASK; |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | obj->CnV = &ftm->CONTROLS[ch_n].CnV; |
elessair | 0:f269e3021894 | 58 | obj->MOD = &ftm->MOD; |
elessair | 0:f269e3021894 | 59 | obj->SYNC = &ftm->SYNC; |
elessair | 0:f269e3021894 | 60 | |
elessair | 0:f269e3021894 | 61 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
elessair | 0:f269e3021894 | 62 | pwmout_period_ms(obj, 20); |
elessair | 0:f269e3021894 | 63 | pwmout_write(obj, 0.0); |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | // Wire pinout |
elessair | 0:f269e3021894 | 66 | pinmap_pinout(pin, PinMap_PWM); |
elessair | 0:f269e3021894 | 67 | } |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | void pwmout_free(pwmout_t* obj) {} |
elessair | 0:f269e3021894 | 70 | |
elessair | 0:f269e3021894 | 71 | void pwmout_write(pwmout_t* obj, float value) { |
elessair | 0:f269e3021894 | 72 | if (value < 0.0) { |
elessair | 0:f269e3021894 | 73 | value = 0.0; |
elessair | 0:f269e3021894 | 74 | } else if (value > 1.0) { |
elessair | 0:f269e3021894 | 75 | value = 1.0; |
elessair | 0:f269e3021894 | 76 | } |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | while(*obj->SYNC & FTM_SYNC_SWSYNC_MASK); |
elessair | 0:f269e3021894 | 79 | *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value); |
elessair | 0:f269e3021894 | 80 | *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; |
elessair | 0:f269e3021894 | 81 | } |
elessair | 0:f269e3021894 | 82 | |
elessair | 0:f269e3021894 | 83 | float pwmout_read(pwmout_t* obj) { |
elessair | 0:f269e3021894 | 84 | while(*obj->SYNC & FTM_SYNC_SWSYNC_MASK); |
elessair | 0:f269e3021894 | 85 | float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1); |
elessair | 0:f269e3021894 | 86 | return (v > 1.0) ? (1.0) : (v); |
elessair | 0:f269e3021894 | 87 | } |
elessair | 0:f269e3021894 | 88 | |
elessair | 0:f269e3021894 | 89 | void pwmout_period(pwmout_t* obj, float seconds) { |
elessair | 0:f269e3021894 | 90 | pwmout_period_us(obj, seconds * 1000000.0f); |
elessair | 0:f269e3021894 | 91 | } |
elessair | 0:f269e3021894 | 92 | |
elessair | 0:f269e3021894 | 93 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
elessair | 0:f269e3021894 | 94 | pwmout_period_us(obj, ms * 1000); |
elessair | 0:f269e3021894 | 95 | } |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | // Set the PWM period, keeping the duty cycle the same. |
elessair | 0:f269e3021894 | 98 | void pwmout_period_us(pwmout_t* obj, int us) { |
elessair | 0:f269e3021894 | 99 | float dc = pwmout_read(obj); |
elessair | 0:f269e3021894 | 100 | *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1; |
elessair | 0:f269e3021894 | 101 | *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; |
elessair | 0:f269e3021894 | 102 | pwmout_write(obj, dc); |
elessair | 0:f269e3021894 | 103 | } |
elessair | 0:f269e3021894 | 104 | |
elessair | 0:f269e3021894 | 105 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
elessair | 0:f269e3021894 | 106 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
elessair | 0:f269e3021894 | 107 | } |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
elessair | 0:f269e3021894 | 110 | pwmout_pulsewidth_us(obj, ms * 1000); |
elessair | 0:f269e3021894 | 111 | } |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
elessair | 0:f269e3021894 | 114 | *obj->CnV = (uint32_t)(pwm_clock * (float)us); |
elessair | 0:f269e3021894 | 115 | *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; |
elessair | 0:f269e3021894 | 116 | } |