test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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