![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/pwmout_api.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2006-2013 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #include "mbed_assert.h" |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "pwmout_api.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | |
kadonotakashi | 0:8fdf9a60065b | 19 | #if DEVICE_PWMOUT |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "cmsis.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "pinmap.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "fsl_ftm.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "PeripheralPins.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | static float pwm_clock_mhz; |
kadonotakashi | 0:8fdf9a60065b | 27 | /* Array of FTM peripheral base address. */ |
kadonotakashi | 0:8fdf9a60065b | 28 | static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS; |
kadonotakashi | 0:8fdf9a60065b | 29 | |
kadonotakashi | 0:8fdf9a60065b | 30 | void pwmout_init(pwmout_t* obj, PinName pin) |
kadonotakashi | 0:8fdf9a60065b | 31 | { |
kadonotakashi | 0:8fdf9a60065b | 32 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
kadonotakashi | 0:8fdf9a60065b | 33 | MBED_ASSERT(pwm != (PWMName)NC); |
kadonotakashi | 0:8fdf9a60065b | 34 | |
kadonotakashi | 0:8fdf9a60065b | 35 | obj->pwm_name = pwm; |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | uint32_t pwm_base_clock; |
kadonotakashi | 0:8fdf9a60065b | 38 | pwm_base_clock = CLOCK_GetFreq(kCLOCK_BusClk); |
kadonotakashi | 0:8fdf9a60065b | 39 | float clkval = (float)pwm_base_clock / 1000000.0f; |
kadonotakashi | 0:8fdf9a60065b | 40 | uint32_t clkdiv = 0; |
kadonotakashi | 0:8fdf9a60065b | 41 | while (clkval > 1) { |
kadonotakashi | 0:8fdf9a60065b | 42 | clkdiv++; |
kadonotakashi | 0:8fdf9a60065b | 43 | clkval /= 2.0f; |
kadonotakashi | 0:8fdf9a60065b | 44 | if (clkdiv == 7) { |
kadonotakashi | 0:8fdf9a60065b | 45 | break; |
kadonotakashi | 0:8fdf9a60065b | 46 | } |
kadonotakashi | 0:8fdf9a60065b | 47 | } |
kadonotakashi | 0:8fdf9a60065b | 48 | |
kadonotakashi | 0:8fdf9a60065b | 49 | pwm_clock_mhz = clkval; |
kadonotakashi | 0:8fdf9a60065b | 50 | uint32_t channel = pwm & 0xF; |
kadonotakashi | 0:8fdf9a60065b | 51 | uint32_t instance = pwm >> TPM_SHIFT; |
kadonotakashi | 0:8fdf9a60065b | 52 | ftm_config_t ftmInfo; |
kadonotakashi | 0:8fdf9a60065b | 53 | |
kadonotakashi | 0:8fdf9a60065b | 54 | FTM_GetDefaultConfig(&ftmInfo); |
kadonotakashi | 0:8fdf9a60065b | 55 | ftmInfo.prescale = (ftm_clock_prescale_t)clkdiv; |
kadonotakashi | 0:8fdf9a60065b | 56 | /* Initialize FTM module */ |
kadonotakashi | 0:8fdf9a60065b | 57 | FTM_Init(ftm_addrs[instance], &ftmInfo); |
kadonotakashi | 0:8fdf9a60065b | 58 | |
kadonotakashi | 0:8fdf9a60065b | 59 | ftm_addrs[instance]->CONF |= FTM_CONF_NUMTOF(3); |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | ftm_chnl_pwm_signal_param_t config = { |
kadonotakashi | 0:8fdf9a60065b | 62 | .chnlNumber = (ftm_chnl_t)channel, |
kadonotakashi | 0:8fdf9a60065b | 63 | .level = kFTM_HighTrue, |
kadonotakashi | 0:8fdf9a60065b | 64 | .dutyCyclePercent = 0, |
kadonotakashi | 0:8fdf9a60065b | 65 | .firstEdgeDelayPercent = 0 |
kadonotakashi | 0:8fdf9a60065b | 66 | }; |
kadonotakashi | 0:8fdf9a60065b | 67 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
kadonotakashi | 0:8fdf9a60065b | 68 | FTM_SetupPwm(ftm_addrs[instance], &config, 1, kFTM_EdgeAlignedPwm, 50, pwm_base_clock); |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | FTM_StartTimer(ftm_addrs[instance], kFTM_SystemClock); |
kadonotakashi | 0:8fdf9a60065b | 71 | |
kadonotakashi | 0:8fdf9a60065b | 72 | // Wire pinout |
kadonotakashi | 0:8fdf9a60065b | 73 | pinmap_pinout(pin, PinMap_PWM); |
kadonotakashi | 0:8fdf9a60065b | 74 | } |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | void pwmout_free(pwmout_t* obj) |
kadonotakashi | 0:8fdf9a60065b | 77 | { |
kadonotakashi | 0:8fdf9a60065b | 78 | FTM_Deinit(ftm_addrs[obj->pwm_name >> TPM_SHIFT]); |
kadonotakashi | 0:8fdf9a60065b | 79 | } |
kadonotakashi | 0:8fdf9a60065b | 80 | |
kadonotakashi | 0:8fdf9a60065b | 81 | void pwmout_write(pwmout_t* obj, float value) |
kadonotakashi | 0:8fdf9a60065b | 82 | { |
kadonotakashi | 0:8fdf9a60065b | 83 | if (value < 0.0f) { |
kadonotakashi | 0:8fdf9a60065b | 84 | value = 0.0f; |
kadonotakashi | 0:8fdf9a60065b | 85 | } else if (value > 1.0f) { |
kadonotakashi | 0:8fdf9a60065b | 86 | value = 1.0f; |
kadonotakashi | 0:8fdf9a60065b | 87 | } |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 90 | uint16_t mod = base->MOD & FTM_MOD_MOD_MASK; |
kadonotakashi | 0:8fdf9a60065b | 91 | uint32_t new_count = (uint32_t)((float)(mod) * value); |
kadonotakashi | 0:8fdf9a60065b | 92 | // Update of CnV register |
kadonotakashi | 0:8fdf9a60065b | 93 | base->CONTROLS[obj->pwm_name & 0xF].CnV = new_count; |
kadonotakashi | 0:8fdf9a60065b | 94 | base->CNT = 0; |
kadonotakashi | 0:8fdf9a60065b | 95 | /* Software trigger to update registers */ |
kadonotakashi | 0:8fdf9a60065b | 96 | FTM_SetSoftwareTrigger(base, true); |
kadonotakashi | 0:8fdf9a60065b | 97 | } |
kadonotakashi | 0:8fdf9a60065b | 98 | |
kadonotakashi | 0:8fdf9a60065b | 99 | float pwmout_read(pwmout_t* obj) |
kadonotakashi | 0:8fdf9a60065b | 100 | { |
kadonotakashi | 0:8fdf9a60065b | 101 | FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 102 | uint16_t count = (base->CONTROLS[obj->pwm_name & 0xF].CnV) & FTM_CnV_VAL_MASK; |
kadonotakashi | 0:8fdf9a60065b | 103 | uint16_t mod = base->MOD & FTM_MOD_MOD_MASK; |
kadonotakashi | 0:8fdf9a60065b | 104 | |
kadonotakashi | 0:8fdf9a60065b | 105 | if (mod == 0) |
kadonotakashi | 0:8fdf9a60065b | 106 | return 0.0; |
kadonotakashi | 0:8fdf9a60065b | 107 | float v = (float)(count) / (float)(mod); |
kadonotakashi | 0:8fdf9a60065b | 108 | return (v > 1.0f) ? (1.0f) : (v); |
kadonotakashi | 0:8fdf9a60065b | 109 | } |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | void pwmout_period(pwmout_t* obj, float seconds) |
kadonotakashi | 0:8fdf9a60065b | 112 | { |
kadonotakashi | 0:8fdf9a60065b | 113 | pwmout_period_us(obj, seconds * 1000000.0f); |
kadonotakashi | 0:8fdf9a60065b | 114 | } |
kadonotakashi | 0:8fdf9a60065b | 115 | |
kadonotakashi | 0:8fdf9a60065b | 116 | void pwmout_period_ms(pwmout_t* obj, int ms) |
kadonotakashi | 0:8fdf9a60065b | 117 | { |
kadonotakashi | 0:8fdf9a60065b | 118 | pwmout_period_us(obj, ms * 1000); |
kadonotakashi | 0:8fdf9a60065b | 119 | } |
kadonotakashi | 0:8fdf9a60065b | 120 | |
kadonotakashi | 0:8fdf9a60065b | 121 | // Set the PWM period, keeping the duty cycle the same. |
kadonotakashi | 0:8fdf9a60065b | 122 | void pwmout_period_us(pwmout_t* obj, int us) |
kadonotakashi | 0:8fdf9a60065b | 123 | { |
kadonotakashi | 0:8fdf9a60065b | 124 | FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 125 | float dc = pwmout_read(obj); |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | // Stop FTM clock to ensure instant update of MOD register |
kadonotakashi | 0:8fdf9a60065b | 128 | base->MOD = FTM_MOD_MOD((pwm_clock_mhz * (float)us) - 1); |
kadonotakashi | 0:8fdf9a60065b | 129 | pwmout_write(obj, dc); |
kadonotakashi | 0:8fdf9a60065b | 130 | } |
kadonotakashi | 0:8fdf9a60065b | 131 | |
kadonotakashi | 0:8fdf9a60065b | 132 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
kadonotakashi | 0:8fdf9a60065b | 133 | { |
kadonotakashi | 0:8fdf9a60065b | 134 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
kadonotakashi | 0:8fdf9a60065b | 135 | } |
kadonotakashi | 0:8fdf9a60065b | 136 | |
kadonotakashi | 0:8fdf9a60065b | 137 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
kadonotakashi | 0:8fdf9a60065b | 138 | { |
kadonotakashi | 0:8fdf9a60065b | 139 | pwmout_pulsewidth_us(obj, ms * 1000); |
kadonotakashi | 0:8fdf9a60065b | 140 | } |
kadonotakashi | 0:8fdf9a60065b | 141 | |
kadonotakashi | 0:8fdf9a60065b | 142 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
kadonotakashi | 0:8fdf9a60065b | 143 | { |
kadonotakashi | 0:8fdf9a60065b | 144 | FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 145 | uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us); |
kadonotakashi | 0:8fdf9a60065b | 146 | |
kadonotakashi | 0:8fdf9a60065b | 147 | // Update of CnV register |
kadonotakashi | 0:8fdf9a60065b | 148 | base->CONTROLS[obj->pwm_name & 0xF].CnV = value; |
kadonotakashi | 0:8fdf9a60065b | 149 | /* Software trigger to update registers */ |
kadonotakashi | 0:8fdf9a60065b | 150 | FTM_SetSoftwareTrigger(base, true); |
kadonotakashi | 0:8fdf9a60065b | 151 | } |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | #endif |