mbed library sources. Supersedes mbed-src. Add PORTG support for STM32L476JG (SensorTile kit)
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M451/pwmout_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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 | |
<> | 149:156823d33999 | 102 | // Mark this module to be inited. |
<> | 149:156823d33999 | 103 | int i = modinit - pwm_modinit_tab; |
<> | 149:156823d33999 | 104 | pwm_modinit_mask |= 1 << i; |
<> | 149:156823d33999 | 105 | } |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | void pwmout_free(pwmout_t* obj) |
<> | 149:156823d33999 | 108 | { |
<> | 149:156823d33999 | 109 | PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm); |
<> | 149:156823d33999 | 110 | uint32_t chn = NU_MODSUBINDEX(obj->pwm); |
<> | 149:156823d33999 | 111 | PWM_ForceStop(pwm_base, 1 << chn); |
<> | 149:156823d33999 | 112 | |
<> | 149:156823d33999 | 113 | const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab); |
<> | 149:156823d33999 | 114 | MBED_ASSERT(modinit != NULL); |
<> | 149:156823d33999 | 115 | MBED_ASSERT(modinit->modname == obj->pwm); |
<> | 149:156823d33999 | 116 | ((struct nu_pwm_var *) modinit->var)->en_msk &= ~(1 << chn); |
<> | 149:156823d33999 | 117 | |
<> | 149:156823d33999 | 118 | |
<> | 149:156823d33999 | 119 | if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0x3F) == 0) { |
<> | 149:156823d33999 | 120 | CLK_DisableModuleClock(modinit->clkidx); |
<> | 149:156823d33999 | 121 | } |
<> | 149:156823d33999 | 122 | |
<> | 149:156823d33999 | 123 | // Mark this module to be deinited. |
<> | 149:156823d33999 | 124 | int i = modinit - pwm_modinit_tab; |
<> | 149:156823d33999 | 125 | pwm_modinit_mask &= ~(1 << i); |
<> | 149:156823d33999 | 126 | } |
<> | 149:156823d33999 | 127 | |
<> | 149:156823d33999 | 128 | void pwmout_write(pwmout_t* obj, float value) |
<> | 149:156823d33999 | 129 | { |
<> | 149:156823d33999 | 130 | obj->pulsewidth_us = NU_CLAMP((uint32_t) (value * obj->period_us), 0, obj->period_us); |
<> | 149:156823d33999 | 131 | pwmout_config(obj); |
<> | 149:156823d33999 | 132 | } |
<> | 149:156823d33999 | 133 | |
<> | 149:156823d33999 | 134 | float pwmout_read(pwmout_t* obj) |
<> | 149:156823d33999 | 135 | { |
<> | 149:156823d33999 | 136 | return NU_CLAMP((((float) obj->pulsewidth_us) / obj->period_us), 0.0f, 1.0f); |
<> | 149:156823d33999 | 137 | } |
<> | 149:156823d33999 | 138 | |
<> | 149:156823d33999 | 139 | void pwmout_period(pwmout_t* obj, float seconds) |
<> | 149:156823d33999 | 140 | { |
<> | 149:156823d33999 | 141 | pwmout_period_us(obj, seconds * 1000000.0f); |
<> | 149:156823d33999 | 142 | } |
<> | 149:156823d33999 | 143 | |
<> | 149:156823d33999 | 144 | void pwmout_period_ms(pwmout_t* obj, int ms) |
<> | 149:156823d33999 | 145 | { |
<> | 149:156823d33999 | 146 | pwmout_period_us(obj, ms * 1000); |
<> | 149:156823d33999 | 147 | } |
<> | 149:156823d33999 | 148 | |
<> | 149:156823d33999 | 149 | // Set the PWM period, keeping the duty cycle the same. |
<> | 149:156823d33999 | 150 | void pwmout_period_us(pwmout_t* obj, int us) |
<> | 149:156823d33999 | 151 | { |
<> | 149:156823d33999 | 152 | uint32_t period_us_old = obj->period_us; |
<> | 149:156823d33999 | 153 | uint32_t pulsewidth_us_old = obj->pulsewidth_us; |
<> | 149:156823d33999 | 154 | obj->period_us = us; |
<> | 149:156823d33999 | 155 | obj->pulsewidth_us = NU_CLAMP(obj->period_us * pulsewidth_us_old / period_us_old, 0, obj->period_us); |
<> | 149:156823d33999 | 156 | pwmout_config(obj); |
<> | 149:156823d33999 | 157 | } |
<> | 149:156823d33999 | 158 | |
<> | 149:156823d33999 | 159 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
<> | 149:156823d33999 | 160 | { |
<> | 149:156823d33999 | 161 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
<> | 149:156823d33999 | 162 | } |
<> | 149:156823d33999 | 163 | |
<> | 149:156823d33999 | 164 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
<> | 149:156823d33999 | 165 | { |
<> | 149:156823d33999 | 166 | pwmout_pulsewidth_us(obj, ms * 1000); |
<> | 149:156823d33999 | 167 | } |
<> | 149:156823d33999 | 168 | |
<> | 149:156823d33999 | 169 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
<> | 149:156823d33999 | 170 | { |
<> | 149:156823d33999 | 171 | obj->pulsewidth_us = NU_CLAMP(us, 0, obj->period_us); |
<> | 149:156823d33999 | 172 | pwmout_config(obj); |
<> | 149:156823d33999 | 173 | } |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | int pwmout_allow_powerdown(void) |
<> | 149:156823d33999 | 176 | { |
<> | 149:156823d33999 | 177 | uint32_t modinit_mask = pwm_modinit_mask; |
<> | 149:156823d33999 | 178 | while (modinit_mask) { |
<> | 149:156823d33999 | 179 | int pwm_idx = nu_ctz(modinit_mask); |
<> | 149:156823d33999 | 180 | const struct nu_modinit_s *modinit = pwm_modinit_tab + pwm_idx; |
<> | 149:156823d33999 | 181 | if (modinit->modname != NC) { |
<> | 149:156823d33999 | 182 | PWM_T *pwm_base = (PWM_T *) NU_MODBASE(modinit->modname); |
<> | 149:156823d33999 | 183 | uint32_t chn = NU_MODSUBINDEX(modinit->modname); |
<> | 149:156823d33999 | 184 | // Disallow entering power-down mode if PWM counter is enabled. |
<> | 149:156823d33999 | 185 | if ((pwm_base->CNTEN & (1 << chn)) && pwm_base->CMPDAT[chn]) { |
<> | 149:156823d33999 | 186 | return 0; |
<> | 149:156823d33999 | 187 | } |
<> | 149:156823d33999 | 188 | } |
<> | 149:156823d33999 | 189 | modinit_mask &= ~(1 << pwm_idx); |
<> | 149:156823d33999 | 190 | } |
<> | 149:156823d33999 | 191 | |
<> | 149:156823d33999 | 192 | return 1; |
<> | 149:156823d33999 | 193 | } |
<> | 149:156823d33999 | 194 | |
<> | 149:156823d33999 | 195 | static void pwmout_config(pwmout_t* obj) |
<> | 149:156823d33999 | 196 | { |
<> | 149:156823d33999 | 197 | PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm); |
<> | 149:156823d33999 | 198 | uint32_t chn = NU_MODSUBINDEX(obj->pwm); |
<> | 149:156823d33999 | 199 | // NOTE: Support period < 1s |
<> | 149:156823d33999 | 200 | //PWM_ConfigOutputChannel(pwm_base, chn, 1000 * 1000 / obj->period_us, obj->pulsewidth_us * 100 / obj->period_us); |
<> | 149:156823d33999 | 201 | PWM_ConfigOutputChannel2(pwm_base, chn, 1000 * 1000, obj->pulsewidth_us * 100 / obj->period_us, obj->period_us); |
<> | 149:156823d33999 | 202 | } |
<> | 149:156823d33999 | 203 | |
<> | 149:156823d33999 | 204 | #endif |