![](/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_KL43Z/pwmout_api.c@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
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_tpm.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 TPM peripheral base address. */ |
kadonotakashi | 0:8fdf9a60065b | 28 | static TPM_Type *const tpm_addrs[] = TPM_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 | |
kadonotakashi | 0:8fdf9a60065b | 39 | /* TPM clock source is set to IRC48M during init */ |
kadonotakashi | 0:8fdf9a60065b | 40 | pwm_base_clock = CLOCK_GetFreq(kCLOCK_McgIrc48MClk); |
kadonotakashi | 0:8fdf9a60065b | 41 | float clkval = (float)pwm_base_clock / 1000000.0f; |
kadonotakashi | 0:8fdf9a60065b | 42 | uint32_t clkdiv = 0; |
kadonotakashi | 0:8fdf9a60065b | 43 | while (clkval > 1) { |
kadonotakashi | 0:8fdf9a60065b | 44 | clkdiv++; |
kadonotakashi | 0:8fdf9a60065b | 45 | clkval /= 2.0f; |
kadonotakashi | 0:8fdf9a60065b | 46 | if (clkdiv == 7) { |
kadonotakashi | 0:8fdf9a60065b | 47 | break; |
kadonotakashi | 0:8fdf9a60065b | 48 | } |
kadonotakashi | 0:8fdf9a60065b | 49 | } |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | pwm_clock_mhz = clkval; |
kadonotakashi | 0:8fdf9a60065b | 52 | uint32_t channel = pwm & 0xF; |
kadonotakashi | 0:8fdf9a60065b | 53 | uint32_t instance = pwm >> TPM_SHIFT; |
kadonotakashi | 0:8fdf9a60065b | 54 | tpm_config_t tpmInfo; |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | TPM_GetDefaultConfig(&tpmInfo); |
kadonotakashi | 0:8fdf9a60065b | 57 | tpmInfo.prescale = (tpm_clock_prescale_t)clkdiv; |
kadonotakashi | 0:8fdf9a60065b | 58 | /* Initialize TPM module */ |
kadonotakashi | 0:8fdf9a60065b | 59 | TPM_Init(tpm_addrs[instance], &tpmInfo); |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | tpm_chnl_pwm_signal_param_t config = { |
kadonotakashi | 0:8fdf9a60065b | 62 | .chnlNumber = (tpm_chnl_t)channel, |
kadonotakashi | 0:8fdf9a60065b | 63 | .level = kTPM_HighTrue, |
kadonotakashi | 0:8fdf9a60065b | 64 | .dutyCyclePercent = 0, |
kadonotakashi | 0:8fdf9a60065b | 65 | }; |
kadonotakashi | 0:8fdf9a60065b | 66 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
kadonotakashi | 0:8fdf9a60065b | 67 | TPM_SetupPwm(tpm_addrs[instance], &config, 1, kTPM_EdgeAlignedPwm, 50, pwm_base_clock); |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | TPM_StartTimer(tpm_addrs[instance], kTPM_SystemClock); |
kadonotakashi | 0:8fdf9a60065b | 70 | |
kadonotakashi | 0:8fdf9a60065b | 71 | // Wire pinout |
kadonotakashi | 0:8fdf9a60065b | 72 | pinmap_pinout(pin, PinMap_PWM); |
kadonotakashi | 0:8fdf9a60065b | 73 | } |
kadonotakashi | 0:8fdf9a60065b | 74 | |
kadonotakashi | 0:8fdf9a60065b | 75 | void pwmout_free(pwmout_t* obj) |
kadonotakashi | 0:8fdf9a60065b | 76 | { |
kadonotakashi | 0:8fdf9a60065b | 77 | TPM_Deinit(tpm_addrs[obj->pwm_name >> TPM_SHIFT]); |
kadonotakashi | 0:8fdf9a60065b | 78 | } |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | void pwmout_write(pwmout_t* obj, float value) |
kadonotakashi | 0:8fdf9a60065b | 81 | { |
kadonotakashi | 0:8fdf9a60065b | 82 | if (value < 0.0f) { |
kadonotakashi | 0:8fdf9a60065b | 83 | value = 0.0f; |
kadonotakashi | 0:8fdf9a60065b | 84 | } else if (value > 1.0f) { |
kadonotakashi | 0:8fdf9a60065b | 85 | value = 1.0f; |
kadonotakashi | 0:8fdf9a60065b | 86 | } |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | TPM_Type *base = tpm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 89 | uint16_t mod = base->MOD & TPM_MOD_MOD_MASK; |
kadonotakashi | 0:8fdf9a60065b | 90 | uint32_t new_count = (uint32_t)((float)(mod) * value); |
kadonotakashi | 0:8fdf9a60065b | 91 | // Update of CnV register |
kadonotakashi | 0:8fdf9a60065b | 92 | base->CONTROLS[obj->pwm_name & 0xF].CnV = new_count; |
kadonotakashi | 0:8fdf9a60065b | 93 | base->CNT = 0; |
kadonotakashi | 0:8fdf9a60065b | 94 | } |
kadonotakashi | 0:8fdf9a60065b | 95 | |
kadonotakashi | 0:8fdf9a60065b | 96 | float pwmout_read(pwmout_t* obj) |
kadonotakashi | 0:8fdf9a60065b | 97 | { |
kadonotakashi | 0:8fdf9a60065b | 98 | TPM_Type *base = tpm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 99 | uint16_t count = (base->CONTROLS[obj->pwm_name & 0xF].CnV) & TPM_CnV_VAL_MASK; |
kadonotakashi | 0:8fdf9a60065b | 100 | uint16_t mod = base->MOD & TPM_MOD_MOD_MASK; |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | if (mod == 0) |
kadonotakashi | 0:8fdf9a60065b | 103 | return 0.0; |
kadonotakashi | 0:8fdf9a60065b | 104 | float v = (float)(count) / (float)(mod); |
kadonotakashi | 0:8fdf9a60065b | 105 | return (v > 1.0f) ? (1.0f) : (v); |
kadonotakashi | 0:8fdf9a60065b | 106 | } |
kadonotakashi | 0:8fdf9a60065b | 107 | |
kadonotakashi | 0:8fdf9a60065b | 108 | void pwmout_period(pwmout_t* obj, float seconds) |
kadonotakashi | 0:8fdf9a60065b | 109 | { |
kadonotakashi | 0:8fdf9a60065b | 110 | pwmout_period_us(obj, seconds * 1000000.0f); |
kadonotakashi | 0:8fdf9a60065b | 111 | } |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | void pwmout_period_ms(pwmout_t* obj, int ms) |
kadonotakashi | 0:8fdf9a60065b | 114 | { |
kadonotakashi | 0:8fdf9a60065b | 115 | pwmout_period_us(obj, ms * 1000); |
kadonotakashi | 0:8fdf9a60065b | 116 | } |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | // Set the PWM period, keeping the duty cycle the same. |
kadonotakashi | 0:8fdf9a60065b | 119 | void pwmout_period_us(pwmout_t* obj, int us) |
kadonotakashi | 0:8fdf9a60065b | 120 | { |
kadonotakashi | 0:8fdf9a60065b | 121 | TPM_Type *base = tpm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 122 | float dc = pwmout_read(obj); |
kadonotakashi | 0:8fdf9a60065b | 123 | |
kadonotakashi | 0:8fdf9a60065b | 124 | // Stop TPM clock to ensure instant update of MOD register |
kadonotakashi | 0:8fdf9a60065b | 125 | base->MOD = TPM_MOD_MOD((pwm_clock_mhz * (float)us) - 1); |
kadonotakashi | 0:8fdf9a60065b | 126 | pwmout_write(obj, dc); |
kadonotakashi | 0:8fdf9a60065b | 127 | } |
kadonotakashi | 0:8fdf9a60065b | 128 | |
kadonotakashi | 0:8fdf9a60065b | 129 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
kadonotakashi | 0:8fdf9a60065b | 130 | { |
kadonotakashi | 0:8fdf9a60065b | 131 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
kadonotakashi | 0:8fdf9a60065b | 132 | } |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
kadonotakashi | 0:8fdf9a60065b | 135 | { |
kadonotakashi | 0:8fdf9a60065b | 136 | pwmout_pulsewidth_us(obj, ms * 1000); |
kadonotakashi | 0:8fdf9a60065b | 137 | } |
kadonotakashi | 0:8fdf9a60065b | 138 | |
kadonotakashi | 0:8fdf9a60065b | 139 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
kadonotakashi | 0:8fdf9a60065b | 140 | { |
kadonotakashi | 0:8fdf9a60065b | 141 | TPM_Type *base = tpm_addrs[obj->pwm_name >> TPM_SHIFT]; |
kadonotakashi | 0:8fdf9a60065b | 142 | uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us); |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | // Update of CnV register |
kadonotakashi | 0:8fdf9a60065b | 145 | base->CONTROLS[obj->pwm_name & 0xF].CnV = value; |
kadonotakashi | 0:8fdf9a60065b | 146 | } |
kadonotakashi | 0:8fdf9a60065b | 147 | |
kadonotakashi | 0:8fdf9a60065b | 148 | #endif |