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