4545
Fork of mbed-dev by
targets/TARGET_Freescale/TARGET_K20XX/pwmout_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_Freescale/TARGET_K20XX/pwmout_api.c@144:ef7eb2e8f9f7
- Child:
- 151:5eaa88a5bcc7
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2015 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 17 | #include "pwmout_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 21 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 22 | |
<> | 144:ef7eb2e8f9f7 | 23 | static float pwm_clock = 0; |
<> | 144:ef7eb2e8f9f7 | 24 | |
<> | 144:ef7eb2e8f9f7 | 25 | void pwmout_init(pwmout_t* obj, PinName pin) { |
<> | 144:ef7eb2e8f9f7 | 26 | // determine the channel |
<> | 144:ef7eb2e8f9f7 | 27 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 28 | MBED_ASSERT(pwm != (PWMName)NC); |
<> | 144:ef7eb2e8f9f7 | 29 | |
<> | 144:ef7eb2e8f9f7 | 30 | uint32_t clkdiv = 0; |
<> | 144:ef7eb2e8f9f7 | 31 | float clkval = SystemCoreClock / 1000000.0f; |
<> | 144:ef7eb2e8f9f7 | 32 | |
<> | 144:ef7eb2e8f9f7 | 33 | while (clkval > 1) { |
<> | 144:ef7eb2e8f9f7 | 34 | clkdiv++; |
<> | 144:ef7eb2e8f9f7 | 35 | clkval /= 2.0; |
<> | 144:ef7eb2e8f9f7 | 36 | if (clkdiv == 7) |
<> | 144:ef7eb2e8f9f7 | 37 | break; |
<> | 144:ef7eb2e8f9f7 | 38 | } |
<> | 144:ef7eb2e8f9f7 | 39 | |
<> | 144:ef7eb2e8f9f7 | 40 | pwm_clock = clkval; |
<> | 144:ef7eb2e8f9f7 | 41 | unsigned int ftm_n = (pwm >> TPM_SHIFT); |
<> | 144:ef7eb2e8f9f7 | 42 | unsigned int ch_n = (pwm & 0xFF); |
<> | 144:ef7eb2e8f9f7 | 43 | |
<> | 144:ef7eb2e8f9f7 | 44 | SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n); |
<> | 144:ef7eb2e8f9f7 | 45 | |
<> | 144:ef7eb2e8f9f7 | 46 | FTM_Type *ftm = (FTM_Type *)(FTM0_BASE + 0x1000 * ftm_n); |
<> | 144:ef7eb2e8f9f7 | 47 | ftm->CONF |= FTM_CONF_BDMMODE(3); |
<> | 144:ef7eb2e8f9f7 | 48 | ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz |
<> | 144:ef7eb2e8f9f7 | 49 | ftm->CONTROLS[ch_n].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */ |
<> | 144:ef7eb2e8f9f7 | 50 | ftm->MODE = FTM_MODE_FTMEN_MASK; |
<> | 144:ef7eb2e8f9f7 | 51 | ftm->SYNC = FTM_SYNC_CNTMIN_MASK; |
<> | 144:ef7eb2e8f9f7 | 52 | ftm->SYNCONF = FTM_SYNCONF_SYNCMODE_MASK | FTM_SYNCONF_SWSOC_MASK | FTM_SYNCONF_SWWRBUF_MASK; |
<> | 144:ef7eb2e8f9f7 | 53 | |
<> | 144:ef7eb2e8f9f7 | 54 | //Without SYNCEN set CnV does not seem to update |
<> | 144:ef7eb2e8f9f7 | 55 | ftm->COMBINE = FTM_COMBINE_SYNCEN0_MASK | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_SYNCEN2_MASK | FTM_COMBINE_SYNCEN3_MASK; |
<> | 144:ef7eb2e8f9f7 | 56 | |
<> | 144:ef7eb2e8f9f7 | 57 | obj->CnV = &ftm->CONTROLS[ch_n].CnV; |
<> | 144:ef7eb2e8f9f7 | 58 | obj->MOD = &ftm->MOD; |
<> | 144:ef7eb2e8f9f7 | 59 | obj->SYNC = &ftm->SYNC; |
<> | 144:ef7eb2e8f9f7 | 60 | |
<> | 144:ef7eb2e8f9f7 | 61 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
<> | 144:ef7eb2e8f9f7 | 62 | pwmout_period_ms(obj, 20); |
<> | 144:ef7eb2e8f9f7 | 63 | pwmout_write(obj, 0.0); |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | // Wire pinout |
<> | 144:ef7eb2e8f9f7 | 66 | pinmap_pinout(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 67 | } |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | void pwmout_free(pwmout_t* obj) {} |
<> | 144:ef7eb2e8f9f7 | 70 | |
<> | 144:ef7eb2e8f9f7 | 71 | void pwmout_write(pwmout_t* obj, float value) { |
<> | 144:ef7eb2e8f9f7 | 72 | if (value < 0.0) { |
<> | 144:ef7eb2e8f9f7 | 73 | value = 0.0; |
<> | 144:ef7eb2e8f9f7 | 74 | } else if (value > 1.0) { |
<> | 144:ef7eb2e8f9f7 | 75 | value = 1.0; |
<> | 144:ef7eb2e8f9f7 | 76 | } |
<> | 144:ef7eb2e8f9f7 | 77 | |
<> | 144:ef7eb2e8f9f7 | 78 | while(*obj->SYNC & FTM_SYNC_SWSYNC_MASK); |
<> | 144:ef7eb2e8f9f7 | 79 | *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value); |
<> | 144:ef7eb2e8f9f7 | 80 | *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; |
<> | 144:ef7eb2e8f9f7 | 81 | } |
<> | 144:ef7eb2e8f9f7 | 82 | |
<> | 144:ef7eb2e8f9f7 | 83 | float pwmout_read(pwmout_t* obj) { |
<> | 144:ef7eb2e8f9f7 | 84 | while(*obj->SYNC & FTM_SYNC_SWSYNC_MASK); |
<> | 144:ef7eb2e8f9f7 | 85 | float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1); |
<> | 144:ef7eb2e8f9f7 | 86 | return (v > 1.0) ? (1.0) : (v); |
<> | 144:ef7eb2e8f9f7 | 87 | } |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | void pwmout_period(pwmout_t* obj, float seconds) { |
<> | 144:ef7eb2e8f9f7 | 90 | pwmout_period_us(obj, seconds * 1000000.0f); |
<> | 144:ef7eb2e8f9f7 | 91 | } |
<> | 144:ef7eb2e8f9f7 | 92 | |
<> | 144:ef7eb2e8f9f7 | 93 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
<> | 144:ef7eb2e8f9f7 | 94 | pwmout_period_us(obj, ms * 1000); |
<> | 144:ef7eb2e8f9f7 | 95 | } |
<> | 144:ef7eb2e8f9f7 | 96 | |
<> | 144:ef7eb2e8f9f7 | 97 | // Set the PWM period, keeping the duty cycle the same. |
<> | 144:ef7eb2e8f9f7 | 98 | void pwmout_period_us(pwmout_t* obj, int us) { |
<> | 144:ef7eb2e8f9f7 | 99 | float dc = pwmout_read(obj); |
<> | 144:ef7eb2e8f9f7 | 100 | *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1; |
<> | 144:ef7eb2e8f9f7 | 101 | *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; |
<> | 144:ef7eb2e8f9f7 | 102 | pwmout_write(obj, dc); |
<> | 144:ef7eb2e8f9f7 | 103 | } |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 144:ef7eb2e8f9f7 | 105 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
<> | 144:ef7eb2e8f9f7 | 106 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
<> | 144:ef7eb2e8f9f7 | 107 | } |
<> | 144:ef7eb2e8f9f7 | 108 | |
<> | 144:ef7eb2e8f9f7 | 109 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
<> | 144:ef7eb2e8f9f7 | 110 | pwmout_pulsewidth_us(obj, ms * 1000); |
<> | 144:ef7eb2e8f9f7 | 111 | } |
<> | 144:ef7eb2e8f9f7 | 112 | |
<> | 144:ef7eb2e8f9f7 | 113 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
<> | 144:ef7eb2e8f9f7 | 114 | *obj->CnV = (uint32_t)(pwm_clock * (float)us); |
<> | 144:ef7eb2e8f9f7 | 115 | *obj->SYNC |= FTM_SYNC_SWSYNC_MASK; |
<> | 144:ef7eb2e8f9f7 | 116 | } |