mbed
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NANO100/pwmout_api.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 177:447f873cad2f
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 175:b96e65c34a4d | 1 | /* mbed Microcontroller Library |
AnnaBridge | 175:b96e65c34a4d | 2 | * Copyright (c) 2015-2017 Nuvoton |
AnnaBridge | 175:b96e65c34a4d | 3 | * |
AnnaBridge | 175:b96e65c34a4d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 175:b96e65c34a4d | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 175:b96e65c34a4d | 6 | * You may obtain a copy of the License at |
AnnaBridge | 175:b96e65c34a4d | 7 | * |
AnnaBridge | 175:b96e65c34a4d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 175:b96e65c34a4d | 9 | * |
AnnaBridge | 175:b96e65c34a4d | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 175:b96e65c34a4d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 175:b96e65c34a4d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 175:b96e65c34a4d | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 175:b96e65c34a4d | 14 | * limitations under the License. |
AnnaBridge | 175:b96e65c34a4d | 15 | */ |
AnnaBridge | 175:b96e65c34a4d | 16 | |
AnnaBridge | 175:b96e65c34a4d | 17 | #include "pwmout_api.h" |
AnnaBridge | 175:b96e65c34a4d | 18 | |
AnnaBridge | 175:b96e65c34a4d | 19 | #if DEVICE_PWMOUT |
AnnaBridge | 175:b96e65c34a4d | 20 | |
AnnaBridge | 175:b96e65c34a4d | 21 | #include "cmsis.h" |
AnnaBridge | 175:b96e65c34a4d | 22 | #include "pinmap.h" |
AnnaBridge | 175:b96e65c34a4d | 23 | #include "PeripheralPins.h" |
AnnaBridge | 175:b96e65c34a4d | 24 | #include "nu_modutil.h" |
AnnaBridge | 175:b96e65c34a4d | 25 | #include "nu_miscutil.h" |
AnnaBridge | 175:b96e65c34a4d | 26 | #include "nu_bitutil.h" |
AnnaBridge | 175:b96e65c34a4d | 27 | |
AnnaBridge | 175:b96e65c34a4d | 28 | struct nu_pwm_var { |
AnnaBridge | 175:b96e65c34a4d | 29 | uint32_t en_msk; |
AnnaBridge | 175:b96e65c34a4d | 30 | }; |
AnnaBridge | 175:b96e65c34a4d | 31 | |
AnnaBridge | 175:b96e65c34a4d | 32 | static struct nu_pwm_var pwm0_01_var = { |
AnnaBridge | 175:b96e65c34a4d | 33 | .en_msk = 0 |
AnnaBridge | 175:b96e65c34a4d | 34 | }; |
AnnaBridge | 175:b96e65c34a4d | 35 | |
AnnaBridge | 175:b96e65c34a4d | 36 | static struct nu_pwm_var pwm0_23_var = { |
AnnaBridge | 175:b96e65c34a4d | 37 | .en_msk = 0 |
AnnaBridge | 175:b96e65c34a4d | 38 | }; |
AnnaBridge | 175:b96e65c34a4d | 39 | |
AnnaBridge | 175:b96e65c34a4d | 40 | static struct nu_pwm_var pwm1_01_var = { |
AnnaBridge | 175:b96e65c34a4d | 41 | .en_msk = 0 |
AnnaBridge | 175:b96e65c34a4d | 42 | }; |
AnnaBridge | 175:b96e65c34a4d | 43 | |
AnnaBridge | 175:b96e65c34a4d | 44 | static struct nu_pwm_var pwm1_23_var = { |
AnnaBridge | 175:b96e65c34a4d | 45 | .en_msk = 0 |
AnnaBridge | 175:b96e65c34a4d | 46 | }; |
AnnaBridge | 175:b96e65c34a4d | 47 | |
AnnaBridge | 175:b96e65c34a4d | 48 | static uint32_t pwm_modinit_mask = 0; |
AnnaBridge | 175:b96e65c34a4d | 49 | |
AnnaBridge | 175:b96e65c34a4d | 50 | static const struct nu_modinit_s pwm_modinit_tab[] = { |
AnnaBridge | 175:b96e65c34a4d | 51 | {PWM_0_0, PWM0_CH01_MODULE, CLK_CLKSEL1_PWM0_CH01_S_HCLK, 0, PWM0_RST, PWM0_IRQn, &pwm0_01_var}, |
AnnaBridge | 175:b96e65c34a4d | 52 | {PWM_0_1, PWM0_CH01_MODULE, CLK_CLKSEL1_PWM0_CH01_S_HCLK, 0, PWM0_RST, PWM0_IRQn, &pwm0_01_var}, |
AnnaBridge | 175:b96e65c34a4d | 53 | {PWM_0_2, PWM0_CH23_MODULE, CLK_CLKSEL1_PWM0_CH23_S_HCLK, 0, PWM0_RST, PWM0_IRQn, &pwm0_23_var}, |
AnnaBridge | 175:b96e65c34a4d | 54 | {PWM_0_3, PWM0_CH23_MODULE, CLK_CLKSEL1_PWM0_CH23_S_HCLK, 0, PWM0_RST, PWM0_IRQn, &pwm0_23_var}, |
AnnaBridge | 175:b96e65c34a4d | 55 | |
AnnaBridge | 175:b96e65c34a4d | 56 | {PWM_1_0, PWM1_CH01_MODULE, CLK_CLKSEL2_PWM1_CH01_S_HCLK, 0, PWM1_RST, PWM1_IRQn, &pwm1_01_var}, |
AnnaBridge | 175:b96e65c34a4d | 57 | {PWM_1_1, PWM1_CH01_MODULE, CLK_CLKSEL2_PWM1_CH01_S_HCLK, 0, PWM1_RST, PWM1_IRQn, &pwm1_01_var}, |
AnnaBridge | 175:b96e65c34a4d | 58 | {PWM_1_2, PWM1_CH23_MODULE, CLK_CLKSEL2_PWM1_CH23_S_HCLK, 0, PWM1_RST, PWM1_IRQn, &pwm1_23_var}, |
AnnaBridge | 175:b96e65c34a4d | 59 | {PWM_1_3, PWM1_CH23_MODULE, CLK_CLKSEL2_PWM1_CH23_S_HCLK, 0, PWM1_RST, PWM1_IRQn, &pwm1_23_var}, |
AnnaBridge | 175:b96e65c34a4d | 60 | |
AnnaBridge | 175:b96e65c34a4d | 61 | {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL} |
AnnaBridge | 175:b96e65c34a4d | 62 | }; |
AnnaBridge | 175:b96e65c34a4d | 63 | |
AnnaBridge | 175:b96e65c34a4d | 64 | static void pwmout_config(pwmout_t* obj); |
AnnaBridge | 175:b96e65c34a4d | 65 | |
AnnaBridge | 175:b96e65c34a4d | 66 | void pwmout_init(pwmout_t* obj, PinName pin) |
AnnaBridge | 175:b96e65c34a4d | 67 | { |
AnnaBridge | 175:b96e65c34a4d | 68 | obj->pwm = (PWMName) pinmap_peripheral(pin, PinMap_PWM); |
AnnaBridge | 175:b96e65c34a4d | 69 | MBED_ASSERT((int) obj->pwm != NC); |
AnnaBridge | 175:b96e65c34a4d | 70 | |
AnnaBridge | 175:b96e65c34a4d | 71 | const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 72 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 73 | MBED_ASSERT((PWMName) modinit->modname == obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 74 | |
AnnaBridge | 175:b96e65c34a4d | 75 | PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 76 | uint32_t chn = NU_MODSUBINDEX(obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 77 | |
AnnaBridge | 175:b96e65c34a4d | 78 | // NOTE: Channels 0/1, 2/3 share a clock source. |
AnnaBridge | 175:b96e65c34a4d | 79 | if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0xF) == 0) { |
AnnaBridge | 175:b96e65c34a4d | 80 | // Select clock source of paired channels |
AnnaBridge | 175:b96e65c34a4d | 81 | CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); |
AnnaBridge | 175:b96e65c34a4d | 82 | // Enable clock of paired channels |
AnnaBridge | 175:b96e65c34a4d | 83 | CLK_EnableModuleClock(modinit->clkidx); |
AnnaBridge | 175:b96e65c34a4d | 84 | } |
AnnaBridge | 175:b96e65c34a4d | 85 | |
AnnaBridge | 175:b96e65c34a4d | 86 | // Wire pinout |
AnnaBridge | 175:b96e65c34a4d | 87 | pinmap_pinout(pin, PinMap_PWM); |
AnnaBridge | 175:b96e65c34a4d | 88 | |
AnnaBridge | 175:b96e65c34a4d | 89 | // Default: period = 10 ms, pulse width = 0 ms |
AnnaBridge | 175:b96e65c34a4d | 90 | obj->period_us = 1000 * 10; |
AnnaBridge | 175:b96e65c34a4d | 91 | obj->pulsewidth_us = 0; |
AnnaBridge | 175:b96e65c34a4d | 92 | pwmout_config(obj); |
AnnaBridge | 175:b96e65c34a4d | 93 | // enable inverter to ensure the first PWM cycle is correct |
AnnaBridge | 175:b96e65c34a4d | 94 | pwm_base->CTL |= (PWM_CTL_CH0INV_Msk << (chn * 8)); |
AnnaBridge | 175:b96e65c34a4d | 95 | |
AnnaBridge | 175:b96e65c34a4d | 96 | // Enable output of the specified PWM channel |
AnnaBridge | 175:b96e65c34a4d | 97 | PWM_EnableOutput(pwm_base, 1 << chn); |
AnnaBridge | 175:b96e65c34a4d | 98 | PWM_Start(pwm_base, 1 << chn); |
AnnaBridge | 175:b96e65c34a4d | 99 | |
AnnaBridge | 175:b96e65c34a4d | 100 | ((struct nu_pwm_var *) modinit->var)->en_msk |= 1 << chn; |
AnnaBridge | 175:b96e65c34a4d | 101 | |
AnnaBridge | 175:b96e65c34a4d | 102 | if (((struct nu_pwm_var *) modinit->var)->en_msk) { |
AnnaBridge | 175:b96e65c34a4d | 103 | // Mark this module to be inited. |
AnnaBridge | 175:b96e65c34a4d | 104 | int i = modinit - pwm_modinit_tab; |
AnnaBridge | 175:b96e65c34a4d | 105 | pwm_modinit_mask |= 1 << i; |
AnnaBridge | 175:b96e65c34a4d | 106 | } |
AnnaBridge | 175:b96e65c34a4d | 107 | } |
AnnaBridge | 175:b96e65c34a4d | 108 | |
AnnaBridge | 175:b96e65c34a4d | 109 | void pwmout_free(pwmout_t* obj) |
AnnaBridge | 175:b96e65c34a4d | 110 | { |
AnnaBridge | 175:b96e65c34a4d | 111 | PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 112 | uint32_t chn = NU_MODSUBINDEX(obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 113 | PWM_ForceStop(pwm_base, 1 << chn); |
AnnaBridge | 175:b96e65c34a4d | 114 | |
AnnaBridge | 175:b96e65c34a4d | 115 | const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 116 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 117 | MBED_ASSERT((PWMName) modinit->modname == obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 118 | ((struct nu_pwm_var *) modinit->var)->en_msk &= ~(1 << chn); |
AnnaBridge | 175:b96e65c34a4d | 119 | |
AnnaBridge | 175:b96e65c34a4d | 120 | |
AnnaBridge | 175:b96e65c34a4d | 121 | if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0xF) == 0) { |
AnnaBridge | 175:b96e65c34a4d | 122 | CLK_DisableModuleClock(modinit->clkidx); |
AnnaBridge | 175:b96e65c34a4d | 123 | } |
AnnaBridge | 175:b96e65c34a4d | 124 | |
AnnaBridge | 175:b96e65c34a4d | 125 | if (((struct nu_pwm_var *) modinit->var)->en_msk == 0) { |
AnnaBridge | 175:b96e65c34a4d | 126 | // Mark this module to be deinited. |
AnnaBridge | 175:b96e65c34a4d | 127 | int i = modinit - pwm_modinit_tab; |
AnnaBridge | 175:b96e65c34a4d | 128 | pwm_modinit_mask &= ~(1 << i); |
AnnaBridge | 175:b96e65c34a4d | 129 | } |
AnnaBridge | 175:b96e65c34a4d | 130 | } |
AnnaBridge | 175:b96e65c34a4d | 131 | |
AnnaBridge | 175:b96e65c34a4d | 132 | void pwmout_write(pwmout_t* obj, float value) |
AnnaBridge | 175:b96e65c34a4d | 133 | { |
AnnaBridge | 175:b96e65c34a4d | 134 | obj->pulsewidth_us = NU_CLAMP((uint32_t) (value * obj->period_us), 0, obj->period_us); |
AnnaBridge | 175:b96e65c34a4d | 135 | pwmout_config(obj); |
AnnaBridge | 175:b96e65c34a4d | 136 | } |
AnnaBridge | 175:b96e65c34a4d | 137 | |
AnnaBridge | 175:b96e65c34a4d | 138 | float pwmout_read(pwmout_t* obj) |
AnnaBridge | 175:b96e65c34a4d | 139 | { |
AnnaBridge | 175:b96e65c34a4d | 140 | return NU_CLAMP((((float) obj->pulsewidth_us) / obj->period_us), 0.0f, 1.0f); |
AnnaBridge | 175:b96e65c34a4d | 141 | } |
AnnaBridge | 175:b96e65c34a4d | 142 | |
AnnaBridge | 175:b96e65c34a4d | 143 | void pwmout_period(pwmout_t* obj, float seconds) |
AnnaBridge | 175:b96e65c34a4d | 144 | { |
AnnaBridge | 175:b96e65c34a4d | 145 | pwmout_period_us(obj, seconds * 1000000.0f); |
AnnaBridge | 175:b96e65c34a4d | 146 | } |
AnnaBridge | 175:b96e65c34a4d | 147 | |
AnnaBridge | 175:b96e65c34a4d | 148 | void pwmout_period_ms(pwmout_t* obj, int ms) |
AnnaBridge | 175:b96e65c34a4d | 149 | { |
AnnaBridge | 175:b96e65c34a4d | 150 | pwmout_period_us(obj, ms * 1000); |
AnnaBridge | 175:b96e65c34a4d | 151 | } |
AnnaBridge | 175:b96e65c34a4d | 152 | |
AnnaBridge | 175:b96e65c34a4d | 153 | // Set the PWM period, keeping the duty cycle the same. |
AnnaBridge | 175:b96e65c34a4d | 154 | void pwmout_period_us(pwmout_t* obj, int us) |
AnnaBridge | 175:b96e65c34a4d | 155 | { |
AnnaBridge | 175:b96e65c34a4d | 156 | uint32_t period_us_old = obj->period_us; |
AnnaBridge | 175:b96e65c34a4d | 157 | uint32_t pulsewidth_us_old = obj->pulsewidth_us; |
AnnaBridge | 175:b96e65c34a4d | 158 | obj->period_us = us; |
AnnaBridge | 175:b96e65c34a4d | 159 | obj->pulsewidth_us = NU_CLAMP(obj->period_us * pulsewidth_us_old / period_us_old, 0, obj->period_us); |
AnnaBridge | 175:b96e65c34a4d | 160 | pwmout_config(obj); |
AnnaBridge | 175:b96e65c34a4d | 161 | } |
AnnaBridge | 175:b96e65c34a4d | 162 | |
AnnaBridge | 175:b96e65c34a4d | 163 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
AnnaBridge | 175:b96e65c34a4d | 164 | { |
AnnaBridge | 175:b96e65c34a4d | 165 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
AnnaBridge | 175:b96e65c34a4d | 166 | } |
AnnaBridge | 175:b96e65c34a4d | 167 | |
AnnaBridge | 175:b96e65c34a4d | 168 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
AnnaBridge | 175:b96e65c34a4d | 169 | { |
AnnaBridge | 175:b96e65c34a4d | 170 | pwmout_pulsewidth_us(obj, ms * 1000); |
AnnaBridge | 175:b96e65c34a4d | 171 | } |
AnnaBridge | 175:b96e65c34a4d | 172 | |
AnnaBridge | 175:b96e65c34a4d | 173 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
AnnaBridge | 175:b96e65c34a4d | 174 | { |
AnnaBridge | 175:b96e65c34a4d | 175 | obj->pulsewidth_us = NU_CLAMP(us, 0, obj->period_us); |
AnnaBridge | 175:b96e65c34a4d | 176 | pwmout_config(obj); |
AnnaBridge | 175:b96e65c34a4d | 177 | } |
AnnaBridge | 175:b96e65c34a4d | 178 | |
AnnaBridge | 175:b96e65c34a4d | 179 | static void pwmout_config(pwmout_t* obj) |
AnnaBridge | 175:b96e65c34a4d | 180 | { |
AnnaBridge | 175:b96e65c34a4d | 181 | PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 182 | uint32_t chn = NU_MODSUBINDEX(obj->pwm); |
AnnaBridge | 175:b96e65c34a4d | 183 | // NOTE: Support period < 1s |
AnnaBridge | 175:b96e65c34a4d | 184 | // NOTE: ARM mbed CI test fails due to first PWM pulse error. Workaround by: |
AnnaBridge | 175:b96e65c34a4d | 185 | // 1. Inverse duty cycle (100 - duty) |
AnnaBridge | 175:b96e65c34a4d | 186 | // 2. Inverse PWM output polarity |
AnnaBridge | 175:b96e65c34a4d | 187 | // This trick is here to pass ARM mbed CI test. First PWM pulse error still remains. |
AnnaBridge | 175:b96e65c34a4d | 188 | PWM_ConfigOutputChannel2(pwm_base, chn, 1000 * 1000, 100 - (obj->pulsewidth_us * 100 / obj->period_us), obj->period_us); |
AnnaBridge | 175:b96e65c34a4d | 189 | } |
AnnaBridge | 175:b96e65c34a4d | 190 | |
AnnaBridge | 175:b96e65c34a4d | 191 | #endif |