mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
173:7d866c31b3c5
Child:
177:447f873cad2f
This updates the lib to the mbed lib v 150

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 173:7d866c31b3c5 1 /* mbed Microcontroller Library
AnnaBridge 173:7d866c31b3c5 2 * Copyright (c) 2015-2016 Nuvoton
AnnaBridge 173:7d866c31b3c5 3 *
AnnaBridge 173:7d866c31b3c5 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 173:7d866c31b3c5 5 * you may not use this file except in compliance with the License.
AnnaBridge 173:7d866c31b3c5 6 * You may obtain a copy of the License at
AnnaBridge 173:7d866c31b3c5 7 *
AnnaBridge 173:7d866c31b3c5 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 173:7d866c31b3c5 9 *
AnnaBridge 173:7d866c31b3c5 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 173:7d866c31b3c5 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 173:7d866c31b3c5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 173:7d866c31b3c5 13 * See the License for the specific language governing permissions and
AnnaBridge 173:7d866c31b3c5 14 * limitations under the License.
AnnaBridge 173:7d866c31b3c5 15 */
AnnaBridge 173:7d866c31b3c5 16
AnnaBridge 173:7d866c31b3c5 17 #include "pwmout_api.h"
AnnaBridge 173:7d866c31b3c5 18
AnnaBridge 173:7d866c31b3c5 19 #if DEVICE_PWMOUT
AnnaBridge 173:7d866c31b3c5 20
AnnaBridge 173:7d866c31b3c5 21 #include "cmsis.h"
AnnaBridge 173:7d866c31b3c5 22 #include "pinmap.h"
AnnaBridge 173:7d866c31b3c5 23 #include "PeripheralPins.h"
AnnaBridge 173:7d866c31b3c5 24 #include "nu_modutil.h"
AnnaBridge 173:7d866c31b3c5 25 #include "nu_miscutil.h"
AnnaBridge 173:7d866c31b3c5 26 #include "nu_bitutil.h"
AnnaBridge 173:7d866c31b3c5 27
AnnaBridge 173:7d866c31b3c5 28 struct nu_pwm_var {
AnnaBridge 173:7d866c31b3c5 29 uint32_t en_msk;
AnnaBridge 173:7d866c31b3c5 30 };
AnnaBridge 173:7d866c31b3c5 31
AnnaBridge 173:7d866c31b3c5 32 static struct nu_pwm_var pwm0_var = {
AnnaBridge 173:7d866c31b3c5 33 .en_msk = 0
AnnaBridge 173:7d866c31b3c5 34 };
AnnaBridge 173:7d866c31b3c5 35
AnnaBridge 173:7d866c31b3c5 36 static struct nu_pwm_var pwm1_var = {
AnnaBridge 173:7d866c31b3c5 37 .en_msk = 0
AnnaBridge 173:7d866c31b3c5 38 };
AnnaBridge 173:7d866c31b3c5 39
AnnaBridge 173:7d866c31b3c5 40 static uint32_t pwm_modinit_mask = 0;
AnnaBridge 173:7d866c31b3c5 41
AnnaBridge 173:7d866c31b3c5 42 static const struct nu_modinit_s pwm_modinit_tab[] = {
AnnaBridge 173:7d866c31b3c5 43 {PWM_0_0, EPWM0_MODULE, CLK_CLKSEL2_EPWM0SEL_PCLK0, 0, EPWM0_RST, EPWM0P0_IRQn, &pwm0_var},
AnnaBridge 173:7d866c31b3c5 44 {PWM_0_1, EPWM0_MODULE, CLK_CLKSEL2_EPWM0SEL_PCLK0, 0, EPWM0_RST, EPWM0P0_IRQn, &pwm0_var},
AnnaBridge 173:7d866c31b3c5 45 {PWM_0_2, EPWM0_MODULE, CLK_CLKSEL2_EPWM0SEL_PCLK0, 0, EPWM0_RST, EPWM0P1_IRQn, &pwm0_var},
AnnaBridge 173:7d866c31b3c5 46 {PWM_0_3, EPWM0_MODULE, CLK_CLKSEL2_EPWM0SEL_PCLK0, 0, EPWM0_RST, EPWM0P1_IRQn, &pwm0_var},
AnnaBridge 173:7d866c31b3c5 47 {PWM_0_4, EPWM0_MODULE, CLK_CLKSEL2_EPWM0SEL_PCLK0, 0, EPWM0_RST, EPWM0P2_IRQn, &pwm0_var},
AnnaBridge 173:7d866c31b3c5 48 {PWM_0_5, EPWM0_MODULE, CLK_CLKSEL2_EPWM0SEL_PCLK0, 0, EPWM0_RST, EPWM0P2_IRQn, &pwm0_var},
AnnaBridge 173:7d866c31b3c5 49
AnnaBridge 173:7d866c31b3c5 50 {PWM_1_0, EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0, EPWM1_RST, EPWM1P0_IRQn, &pwm1_var},
AnnaBridge 173:7d866c31b3c5 51 {PWM_1_1, EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0, EPWM1_RST, EPWM1P0_IRQn, &pwm1_var},
AnnaBridge 173:7d866c31b3c5 52 {PWM_1_2, EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0, EPWM1_RST, EPWM1P1_IRQn, &pwm1_var},
AnnaBridge 173:7d866c31b3c5 53 {PWM_1_3, EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0, EPWM1_RST, EPWM1P1_IRQn, &pwm1_var},
AnnaBridge 173:7d866c31b3c5 54 {PWM_1_4, EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0, EPWM1_RST, EPWM1P2_IRQn, &pwm1_var},
AnnaBridge 173:7d866c31b3c5 55 {PWM_1_5, EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0, EPWM1_RST, EPWM1P2_IRQn, &pwm1_var},
AnnaBridge 173:7d866c31b3c5 56
AnnaBridge 173:7d866c31b3c5 57 {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL}
AnnaBridge 173:7d866c31b3c5 58 };
AnnaBridge 173:7d866c31b3c5 59
AnnaBridge 173:7d866c31b3c5 60 static void pwmout_config(pwmout_t* obj, int start);
AnnaBridge 173:7d866c31b3c5 61
AnnaBridge 173:7d866c31b3c5 62 void pwmout_init(pwmout_t* obj, PinName pin)
AnnaBridge 173:7d866c31b3c5 63 {
AnnaBridge 173:7d866c31b3c5 64 obj->pwm = (PWMName) pinmap_peripheral(pin, PinMap_PWM);
AnnaBridge 173:7d866c31b3c5 65 MBED_ASSERT((int) obj->pwm != NC);
AnnaBridge 173:7d866c31b3c5 66
AnnaBridge 173:7d866c31b3c5 67 const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab);
AnnaBridge 173:7d866c31b3c5 68 MBED_ASSERT(modinit != NULL);
AnnaBridge 173:7d866c31b3c5 69 MBED_ASSERT(modinit->modname == (int) obj->pwm);
AnnaBridge 173:7d866c31b3c5 70
AnnaBridge 173:7d866c31b3c5 71 // NOTE: All channels (identified by PWMName) share a PWM module. This reset will also affect other channels of the same PWM module.
AnnaBridge 173:7d866c31b3c5 72 if (! ((struct nu_pwm_var *) modinit->var)->en_msk) {
AnnaBridge 173:7d866c31b3c5 73 // Reset this module if no channel enabled
AnnaBridge 173:7d866c31b3c5 74 SYS_ResetModule(modinit->rsetidx);
AnnaBridge 173:7d866c31b3c5 75 }
AnnaBridge 173:7d866c31b3c5 76
AnnaBridge 173:7d866c31b3c5 77 uint32_t chn = NU_MODSUBINDEX(obj->pwm);
AnnaBridge 173:7d866c31b3c5 78
AnnaBridge 173:7d866c31b3c5 79 // NOTE: Channels 0/1/2/3/4/5 share a clock source.
AnnaBridge 173:7d866c31b3c5 80 if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0x3F) == 0) {
AnnaBridge 173:7d866c31b3c5 81 // Select clock source of paired channels
AnnaBridge 173:7d866c31b3c5 82 CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
AnnaBridge 173:7d866c31b3c5 83 // Enable clock of paired channels
AnnaBridge 173:7d866c31b3c5 84 CLK_EnableModuleClock(modinit->clkidx);
AnnaBridge 173:7d866c31b3c5 85 }
AnnaBridge 173:7d866c31b3c5 86
AnnaBridge 173:7d866c31b3c5 87 // Wire pinout
AnnaBridge 173:7d866c31b3c5 88 pinmap_pinout(pin, PinMap_PWM);
AnnaBridge 173:7d866c31b3c5 89
AnnaBridge 173:7d866c31b3c5 90 // Default: period = 10 ms, pulse width = 0 ms
AnnaBridge 173:7d866c31b3c5 91 obj->period_us = 1000 * 10;
AnnaBridge 173:7d866c31b3c5 92 obj->pulsewidth_us = 0;
AnnaBridge 173:7d866c31b3c5 93 pwmout_config(obj, 0);
AnnaBridge 173:7d866c31b3c5 94
AnnaBridge 173:7d866c31b3c5 95 ((struct nu_pwm_var *) modinit->var)->en_msk |= 1 << chn;
AnnaBridge 173:7d866c31b3c5 96
AnnaBridge 173:7d866c31b3c5 97 // Mark this module to be inited.
AnnaBridge 173:7d866c31b3c5 98 int i = modinit - pwm_modinit_tab;
AnnaBridge 173:7d866c31b3c5 99 pwm_modinit_mask |= 1 << i;
AnnaBridge 173:7d866c31b3c5 100 }
AnnaBridge 173:7d866c31b3c5 101
AnnaBridge 173:7d866c31b3c5 102 void pwmout_free(pwmout_t* obj)
AnnaBridge 173:7d866c31b3c5 103 {
AnnaBridge 173:7d866c31b3c5 104 EPWM_T *pwm_base = (EPWM_T *) NU_MODBASE(obj->pwm);
AnnaBridge 173:7d866c31b3c5 105 uint32_t chn = NU_MODSUBINDEX(obj->pwm);
AnnaBridge 173:7d866c31b3c5 106 EPWM_ForceStop(pwm_base, 1 << chn);
AnnaBridge 173:7d866c31b3c5 107
AnnaBridge 173:7d866c31b3c5 108 const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab);
AnnaBridge 173:7d866c31b3c5 109 MBED_ASSERT(modinit != NULL);
AnnaBridge 173:7d866c31b3c5 110 MBED_ASSERT(modinit->modname == (int) obj->pwm);
AnnaBridge 173:7d866c31b3c5 111 ((struct nu_pwm_var *) modinit->var)->en_msk &= ~(1 << chn);
AnnaBridge 173:7d866c31b3c5 112
AnnaBridge 173:7d866c31b3c5 113
AnnaBridge 173:7d866c31b3c5 114 if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0x3F) == 0) {
AnnaBridge 173:7d866c31b3c5 115 CLK_DisableModuleClock(modinit->clkidx);
AnnaBridge 173:7d866c31b3c5 116 }
AnnaBridge 173:7d866c31b3c5 117
AnnaBridge 173:7d866c31b3c5 118 // Mark this module to be deinited.
AnnaBridge 173:7d866c31b3c5 119 int i = modinit - pwm_modinit_tab;
AnnaBridge 173:7d866c31b3c5 120 pwm_modinit_mask &= ~(1 << i);
AnnaBridge 173:7d866c31b3c5 121 }
AnnaBridge 173:7d866c31b3c5 122
AnnaBridge 173:7d866c31b3c5 123 void pwmout_write(pwmout_t* obj, float value)
AnnaBridge 173:7d866c31b3c5 124 {
AnnaBridge 173:7d866c31b3c5 125 obj->pulsewidth_us = NU_CLAMP((uint32_t) (value * obj->period_us), 0, obj->period_us);
AnnaBridge 173:7d866c31b3c5 126 pwmout_config(obj, 1);
AnnaBridge 173:7d866c31b3c5 127 }
AnnaBridge 173:7d866c31b3c5 128
AnnaBridge 173:7d866c31b3c5 129 float pwmout_read(pwmout_t* obj)
AnnaBridge 173:7d866c31b3c5 130 {
AnnaBridge 173:7d866c31b3c5 131 return NU_CLAMP((((float) obj->pulsewidth_us) / obj->period_us), 0.0f, 1.0f);
AnnaBridge 173:7d866c31b3c5 132 }
AnnaBridge 173:7d866c31b3c5 133
AnnaBridge 173:7d866c31b3c5 134 void pwmout_period(pwmout_t* obj, float seconds)
AnnaBridge 173:7d866c31b3c5 135 {
AnnaBridge 173:7d866c31b3c5 136 pwmout_period_us(obj, seconds * 1000000.0f);
AnnaBridge 173:7d866c31b3c5 137 }
AnnaBridge 173:7d866c31b3c5 138
AnnaBridge 173:7d866c31b3c5 139 void pwmout_period_ms(pwmout_t* obj, int ms)
AnnaBridge 173:7d866c31b3c5 140 {
AnnaBridge 173:7d866c31b3c5 141 pwmout_period_us(obj, ms * 1000);
AnnaBridge 173:7d866c31b3c5 142 }
AnnaBridge 173:7d866c31b3c5 143
AnnaBridge 173:7d866c31b3c5 144 // Set the PWM period, keeping the duty cycle the same.
AnnaBridge 173:7d866c31b3c5 145 void pwmout_period_us(pwmout_t* obj, int us)
AnnaBridge 173:7d866c31b3c5 146 {
AnnaBridge 173:7d866c31b3c5 147 uint32_t period_us_old = obj->period_us;
AnnaBridge 173:7d866c31b3c5 148 uint32_t pulsewidth_us_old = obj->pulsewidth_us;
AnnaBridge 173:7d866c31b3c5 149 obj->period_us = us;
AnnaBridge 173:7d866c31b3c5 150 obj->pulsewidth_us = NU_CLAMP(obj->period_us * pulsewidth_us_old / period_us_old, 0, obj->period_us);
AnnaBridge 173:7d866c31b3c5 151 pwmout_config(obj, 1);
AnnaBridge 173:7d866c31b3c5 152 }
AnnaBridge 173:7d866c31b3c5 153
AnnaBridge 173:7d866c31b3c5 154 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
AnnaBridge 173:7d866c31b3c5 155 {
AnnaBridge 173:7d866c31b3c5 156 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
AnnaBridge 173:7d866c31b3c5 157 }
AnnaBridge 173:7d866c31b3c5 158
AnnaBridge 173:7d866c31b3c5 159 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
AnnaBridge 173:7d866c31b3c5 160 {
AnnaBridge 173:7d866c31b3c5 161 pwmout_pulsewidth_us(obj, ms * 1000);
AnnaBridge 173:7d866c31b3c5 162 }
AnnaBridge 173:7d866c31b3c5 163
AnnaBridge 173:7d866c31b3c5 164 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
AnnaBridge 173:7d866c31b3c5 165 {
AnnaBridge 173:7d866c31b3c5 166 obj->pulsewidth_us = NU_CLAMP(us, 0, obj->period_us);
AnnaBridge 173:7d866c31b3c5 167 pwmout_config(obj, 1);
AnnaBridge 173:7d866c31b3c5 168 }
AnnaBridge 173:7d866c31b3c5 169
AnnaBridge 173:7d866c31b3c5 170 int pwmout_allow_powerdown(void)
AnnaBridge 173:7d866c31b3c5 171 {
AnnaBridge 173:7d866c31b3c5 172 uint32_t modinit_mask = pwm_modinit_mask;
AnnaBridge 173:7d866c31b3c5 173 while (modinit_mask) {
AnnaBridge 173:7d866c31b3c5 174 int pwm_idx = nu_ctz(modinit_mask);
AnnaBridge 173:7d866c31b3c5 175 const struct nu_modinit_s *modinit = pwm_modinit_tab + pwm_idx;
AnnaBridge 173:7d866c31b3c5 176 if (modinit->modname != NC) {
AnnaBridge 173:7d866c31b3c5 177 EPWM_T *pwm_base = (EPWM_T *) NU_MODBASE(modinit->modname);
AnnaBridge 173:7d866c31b3c5 178 uint32_t chn = NU_MODSUBINDEX(modinit->modname);
AnnaBridge 173:7d866c31b3c5 179 // Disallow entering power-down mode if PWM counter is enabled.
AnnaBridge 173:7d866c31b3c5 180 if ((pwm_base->CNTEN & (1 << chn)) && pwm_base->CMPDAT[chn]) {
AnnaBridge 173:7d866c31b3c5 181 return 0;
AnnaBridge 173:7d866c31b3c5 182 }
AnnaBridge 173:7d866c31b3c5 183 }
AnnaBridge 173:7d866c31b3c5 184 modinit_mask &= ~(1 << pwm_idx);
AnnaBridge 173:7d866c31b3c5 185 }
AnnaBridge 173:7d866c31b3c5 186
AnnaBridge 173:7d866c31b3c5 187 return 1;
AnnaBridge 173:7d866c31b3c5 188 }
AnnaBridge 173:7d866c31b3c5 189
AnnaBridge 173:7d866c31b3c5 190 static void pwmout_config(pwmout_t* obj, int start)
AnnaBridge 173:7d866c31b3c5 191 {
AnnaBridge 173:7d866c31b3c5 192 EPWM_T *pwm_base = (EPWM_T *) NU_MODBASE(obj->pwm);
AnnaBridge 173:7d866c31b3c5 193 uint32_t chn = NU_MODSUBINDEX(obj->pwm);
AnnaBridge 173:7d866c31b3c5 194
AnnaBridge 173:7d866c31b3c5 195 // To avoid abnormal pulse on (re-)configuration, follow the sequence: stop/configure(/re-start).
AnnaBridge 173:7d866c31b3c5 196 // NOTE: The issue is met in ARM mbed CI test tests-api-pwm on M487.
AnnaBridge 173:7d866c31b3c5 197 EPWM_ForceStop(pwm_base, 1 << chn);
AnnaBridge 173:7d866c31b3c5 198
AnnaBridge 173:7d866c31b3c5 199 // NOTE: Support period < 1s
AnnaBridge 173:7d866c31b3c5 200 // NOTE: ARM mbed CI test fails due to first PWM pulse error. Workaround by:
AnnaBridge 173:7d866c31b3c5 201 // 1. Inverse duty cycle (100 - duty)
AnnaBridge 173:7d866c31b3c5 202 // 2. Inverse PWM output polarity
AnnaBridge 173:7d866c31b3c5 203 // This trick is here to pass ARM mbed CI test. First PWM pulse error still remains.
AnnaBridge 173:7d866c31b3c5 204 EPWM_ConfigOutputChannel2(pwm_base, chn, 1000 * 1000, 100 - obj->pulsewidth_us * 100 / obj->period_us, obj->period_us);
AnnaBridge 173:7d866c31b3c5 205 pwm_base->POLCTL |= 1 << (EPWM_POLCTL_PINV0_Pos + chn);
AnnaBridge 173:7d866c31b3c5 206
AnnaBridge 173:7d866c31b3c5 207 if (start) {
AnnaBridge 173:7d866c31b3c5 208 // Enable output of the specified PWM channel
AnnaBridge 173:7d866c31b3c5 209 EPWM_EnableOutput(pwm_base, 1 << chn);
AnnaBridge 173:7d866c31b3c5 210 EPWM_Start(pwm_base, 1 << chn);
AnnaBridge 173:7d866c31b3c5 211 }
AnnaBridge 173:7d866c31b3c5 212 }
AnnaBridge 173:7d866c31b3c5 213
AnnaBridge 173:7d866c31b3c5 214 #endif