hal_tick.h changed for the L432KC target in TARGET/../device/ in order to reassign the system ticker from TIM2 to TIM7, since TIM2 was needed as a 32bit encoder counter.
Dependents: Nucleo_L432KC_Quadrature_Decoder_with_ADC_and_DAC
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L0/pwmout_api.c@161:bd0311f1ad86, 2017-05-27 (annotated)
- Committer:
- tonnyleonard
- Date:
- Sat May 27 01:26:18 2017 +0000
- Revision:
- 161:bd0311f1ad86
- Parent:
- 149:156823d33999
Testing ADC with shunt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 3 | * Copyright (c) 2015, STMicroelectronics |
<> | 144:ef7eb2e8f9f7 | 4 | * All rights reserved. |
<> | 144:ef7eb2e8f9f7 | 5 | * |
<> | 144:ef7eb2e8f9f7 | 6 | * Redistribution and use in source and binary forms, with or without |
<> | 144:ef7eb2e8f9f7 | 7 | * modification, are permitted provided that the following conditions are met: |
<> | 144:ef7eb2e8f9f7 | 8 | * |
<> | 144:ef7eb2e8f9f7 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
<> | 144:ef7eb2e8f9f7 | 10 | * this list of conditions and the following disclaimer. |
<> | 144:ef7eb2e8f9f7 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
<> | 144:ef7eb2e8f9f7 | 12 | * this list of conditions and the following disclaimer in the documentation |
<> | 144:ef7eb2e8f9f7 | 13 | * and/or other materials provided with the distribution. |
<> | 144:ef7eb2e8f9f7 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
<> | 144:ef7eb2e8f9f7 | 15 | * may be used to endorse or promote products derived from this software |
<> | 144:ef7eb2e8f9f7 | 16 | * without specific prior written permission. |
<> | 144:ef7eb2e8f9f7 | 17 | * |
<> | 144:ef7eb2e8f9f7 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
<> | 144:ef7eb2e8f9f7 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
<> | 144:ef7eb2e8f9f7 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
<> | 144:ef7eb2e8f9f7 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
<> | 144:ef7eb2e8f9f7 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
<> | 144:ef7eb2e8f9f7 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
<> | 144:ef7eb2e8f9f7 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
<> | 144:ef7eb2e8f9f7 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
<> | 144:ef7eb2e8f9f7 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
<> | 144:ef7eb2e8f9f7 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
<> | 144:ef7eb2e8f9f7 | 28 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 29 | */ |
<> | 144:ef7eb2e8f9f7 | 30 | #include "pwmout_api.h" |
<> | 144:ef7eb2e8f9f7 | 31 | |
<> | 144:ef7eb2e8f9f7 | 32 | #if DEVICE_PWMOUT |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 35 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 36 | #include "mbed_error.h" |
<> | 144:ef7eb2e8f9f7 | 37 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 38 | |
<> | 144:ef7eb2e8f9f7 | 39 | static TIM_HandleTypeDef TimHandle; |
<> | 144:ef7eb2e8f9f7 | 40 | |
<> | 144:ef7eb2e8f9f7 | 41 | void pwmout_init(pwmout_t* obj, PinName pin) |
<> | 144:ef7eb2e8f9f7 | 42 | { |
<> | 144:ef7eb2e8f9f7 | 43 | // Get the peripheral name from the pin and assign it to the object |
<> | 144:ef7eb2e8f9f7 | 44 | obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 45 | MBED_ASSERT(obj->pwm != (PWMName)NC); |
<> | 144:ef7eb2e8f9f7 | 46 | |
<> | 144:ef7eb2e8f9f7 | 47 | // Get the pin function and assign the used channel to the object |
<> | 144:ef7eb2e8f9f7 | 48 | uint32_t function = pinmap_function(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 49 | MBED_ASSERT(function != (uint32_t)NC); |
<> | 144:ef7eb2e8f9f7 | 50 | obj->channel = STM_PIN_CHANNEL(function); |
<> | 144:ef7eb2e8f9f7 | 51 | obj->inverted = STM_PIN_INVERTED(function); |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | // Enable TIM clock |
<> | 144:ef7eb2e8f9f7 | 54 | if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); |
<> | 144:ef7eb2e8f9f7 | 55 | #if defined(TIM3_BASE) |
<> | 144:ef7eb2e8f9f7 | 56 | if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); |
<> | 144:ef7eb2e8f9f7 | 57 | #endif |
<> | 144:ef7eb2e8f9f7 | 58 | if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE(); |
<> | 144:ef7eb2e8f9f7 | 59 | #if defined(TIM22_BASE) |
<> | 144:ef7eb2e8f9f7 | 60 | if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE(); |
<> | 144:ef7eb2e8f9f7 | 61 | #endif |
<> | 144:ef7eb2e8f9f7 | 62 | |
<> | 144:ef7eb2e8f9f7 | 63 | // Configure GPIO |
<> | 144:ef7eb2e8f9f7 | 64 | pinmap_pinout(pin, PinMap_PWM); |
<> | 144:ef7eb2e8f9f7 | 65 | |
<> | 144:ef7eb2e8f9f7 | 66 | obj->pin = pin; |
<> | 144:ef7eb2e8f9f7 | 67 | obj->period = 0; |
<> | 144:ef7eb2e8f9f7 | 68 | obj->pulse = 0; |
<> | 144:ef7eb2e8f9f7 | 69 | obj->prescaler = 1; |
<> | 144:ef7eb2e8f9f7 | 70 | |
<> | 144:ef7eb2e8f9f7 | 71 | pwmout_period_us(obj, 20000); // 20 ms per default |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | void pwmout_free(pwmout_t* obj) |
<> | 144:ef7eb2e8f9f7 | 75 | { |
<> | 144:ef7eb2e8f9f7 | 76 | // Configure GPIO |
<> | 144:ef7eb2e8f9f7 | 77 | pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
<> | 144:ef7eb2e8f9f7 | 78 | } |
<> | 144:ef7eb2e8f9f7 | 79 | |
<> | 144:ef7eb2e8f9f7 | 80 | void pwmout_write(pwmout_t* obj, float value) |
<> | 144:ef7eb2e8f9f7 | 81 | { |
<> | 144:ef7eb2e8f9f7 | 82 | TIM_OC_InitTypeDef sConfig; |
<> | 144:ef7eb2e8f9f7 | 83 | int channel = 0; |
<> | 144:ef7eb2e8f9f7 | 84 | |
<> | 144:ef7eb2e8f9f7 | 85 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
<> | 144:ef7eb2e8f9f7 | 86 | |
<> | 144:ef7eb2e8f9f7 | 87 | if (value < (float)0.0) { |
<> | 144:ef7eb2e8f9f7 | 88 | value = 0.0; |
<> | 144:ef7eb2e8f9f7 | 89 | } else if (value > (float)1.0) { |
<> | 144:ef7eb2e8f9f7 | 90 | value = 1.0; |
<> | 144:ef7eb2e8f9f7 | 91 | } |
<> | 144:ef7eb2e8f9f7 | 92 | |
<> | 144:ef7eb2e8f9f7 | 93 | obj->pulse = (uint32_t)((float)obj->period * value); |
<> | 144:ef7eb2e8f9f7 | 94 | |
<> | 144:ef7eb2e8f9f7 | 95 | // Configure channels |
<> | 144:ef7eb2e8f9f7 | 96 | sConfig.OCMode = TIM_OCMODE_PWM1; |
<> | 144:ef7eb2e8f9f7 | 97 | sConfig.Pulse = obj->pulse / obj->prescaler; |
<> | 144:ef7eb2e8f9f7 | 98 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
<> | 144:ef7eb2e8f9f7 | 99 | sConfig.OCFastMode = TIM_OCFAST_ENABLE; |
<> | 144:ef7eb2e8f9f7 | 100 | |
<> | 144:ef7eb2e8f9f7 | 101 | switch (obj->channel) { |
<> | 144:ef7eb2e8f9f7 | 102 | case 1: |
<> | 144:ef7eb2e8f9f7 | 103 | channel = TIM_CHANNEL_1; |
<> | 144:ef7eb2e8f9f7 | 104 | break; |
<> | 144:ef7eb2e8f9f7 | 105 | case 2: |
<> | 144:ef7eb2e8f9f7 | 106 | channel = TIM_CHANNEL_2; |
<> | 144:ef7eb2e8f9f7 | 107 | break; |
<> | 144:ef7eb2e8f9f7 | 108 | case 3: |
<> | 144:ef7eb2e8f9f7 | 109 | channel = TIM_CHANNEL_3; |
<> | 144:ef7eb2e8f9f7 | 110 | break; |
<> | 144:ef7eb2e8f9f7 | 111 | case 4: |
<> | 144:ef7eb2e8f9f7 | 112 | channel = TIM_CHANNEL_4; |
<> | 144:ef7eb2e8f9f7 | 113 | break; |
<> | 144:ef7eb2e8f9f7 | 114 | default: |
<> | 144:ef7eb2e8f9f7 | 115 | return; |
<> | 144:ef7eb2e8f9f7 | 116 | } |
<> | 144:ef7eb2e8f9f7 | 117 | |
<> | 144:ef7eb2e8f9f7 | 118 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { |
<> | 144:ef7eb2e8f9f7 | 119 | error("Cannot initialize PWM"); |
<> | 144:ef7eb2e8f9f7 | 120 | } |
<> | 144:ef7eb2e8f9f7 | 121 | |
<> | 144:ef7eb2e8f9f7 | 122 | HAL_TIM_PWM_Start(&TimHandle, channel); |
<> | 144:ef7eb2e8f9f7 | 123 | } |
<> | 144:ef7eb2e8f9f7 | 124 | |
<> | 144:ef7eb2e8f9f7 | 125 | float pwmout_read(pwmout_t* obj) |
<> | 144:ef7eb2e8f9f7 | 126 | { |
<> | 144:ef7eb2e8f9f7 | 127 | float value = 0; |
<> | 144:ef7eb2e8f9f7 | 128 | if (obj->period > 0) { |
<> | 144:ef7eb2e8f9f7 | 129 | value = (float)(obj->pulse) / (float)(obj->period); |
<> | 144:ef7eb2e8f9f7 | 130 | } |
<> | 144:ef7eb2e8f9f7 | 131 | return ((value > (float)1.0) ? (float)(1.0) : (value)); |
<> | 144:ef7eb2e8f9f7 | 132 | } |
<> | 144:ef7eb2e8f9f7 | 133 | |
<> | 144:ef7eb2e8f9f7 | 134 | void pwmout_period(pwmout_t* obj, float seconds) |
<> | 144:ef7eb2e8f9f7 | 135 | { |
<> | 144:ef7eb2e8f9f7 | 136 | pwmout_period_us(obj, seconds * 1000000.0f); |
<> | 144:ef7eb2e8f9f7 | 137 | } |
<> | 144:ef7eb2e8f9f7 | 138 | |
<> | 144:ef7eb2e8f9f7 | 139 | void pwmout_period_ms(pwmout_t* obj, int ms) |
<> | 144:ef7eb2e8f9f7 | 140 | { |
<> | 144:ef7eb2e8f9f7 | 141 | pwmout_period_us(obj, ms * 1000); |
<> | 144:ef7eb2e8f9f7 | 142 | } |
<> | 144:ef7eb2e8f9f7 | 143 | |
<> | 144:ef7eb2e8f9f7 | 144 | void pwmout_period_us(pwmout_t* obj, int us) |
<> | 144:ef7eb2e8f9f7 | 145 | { |
<> | 144:ef7eb2e8f9f7 | 146 | TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); |
<> | 144:ef7eb2e8f9f7 | 147 | |
<> | 144:ef7eb2e8f9f7 | 148 | float dc = pwmout_read(obj); |
<> | 144:ef7eb2e8f9f7 | 149 | |
<> | 144:ef7eb2e8f9f7 | 150 | __HAL_TIM_DISABLE(&TimHandle); |
<> | 144:ef7eb2e8f9f7 | 151 | |
<> | 144:ef7eb2e8f9f7 | 152 | /* To make it simple, we use to possible prescaler values which lead to: |
<> | 144:ef7eb2e8f9f7 | 153 | * pwm unit = 1us, period/pulse can be from 1us to 65535us |
<> | 144:ef7eb2e8f9f7 | 154 | * or |
<> | 144:ef7eb2e8f9f7 | 155 | * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec |
<> | 144:ef7eb2e8f9f7 | 156 | * Be careful that all the channels of a PWM shares the same prescaler |
<> | 144:ef7eb2e8f9f7 | 157 | */ |
<> | 144:ef7eb2e8f9f7 | 158 | if (us > 0xFFFF) { |
<> | 144:ef7eb2e8f9f7 | 159 | obj->prescaler = 500; |
<> | 144:ef7eb2e8f9f7 | 160 | } else { |
<> | 144:ef7eb2e8f9f7 | 161 | obj->prescaler = 1; |
<> | 144:ef7eb2e8f9f7 | 162 | } |
<> | 144:ef7eb2e8f9f7 | 163 | TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; |
<> | 144:ef7eb2e8f9f7 | 164 | |
<> | 144:ef7eb2e8f9f7 | 165 | if (TimHandle.Init.Prescaler > 0xFFFF) |
<> | 144:ef7eb2e8f9f7 | 166 | error("PWM: out of range prescaler"); |
<> | 144:ef7eb2e8f9f7 | 167 | |
<> | 144:ef7eb2e8f9f7 | 168 | TimHandle.Init.Period = (us - 1) / obj->prescaler; |
<> | 144:ef7eb2e8f9f7 | 169 | if (TimHandle.Init.Period > 0xFFFF) |
<> | 144:ef7eb2e8f9f7 | 170 | error("PWM: out of range period"); |
<> | 144:ef7eb2e8f9f7 | 171 | |
<> | 144:ef7eb2e8f9f7 | 172 | TimHandle.Init.ClockDivision = 0; |
<> | 144:ef7eb2e8f9f7 | 173 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
<> | 144:ef7eb2e8f9f7 | 174 | |
<> | 144:ef7eb2e8f9f7 | 175 | if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { |
<> | 144:ef7eb2e8f9f7 | 176 | error("Cannot initialize PWM"); |
<> | 144:ef7eb2e8f9f7 | 177 | } |
<> | 144:ef7eb2e8f9f7 | 178 | |
<> | 144:ef7eb2e8f9f7 | 179 | // Save for future use |
<> | 144:ef7eb2e8f9f7 | 180 | obj->period = us; |
<> | 144:ef7eb2e8f9f7 | 181 | |
<> | 144:ef7eb2e8f9f7 | 182 | // Set duty cycle again |
<> | 144:ef7eb2e8f9f7 | 183 | pwmout_write(obj, dc); |
<> | 144:ef7eb2e8f9f7 | 184 | |
<> | 144:ef7eb2e8f9f7 | 185 | __HAL_TIM_ENABLE(&TimHandle); |
<> | 144:ef7eb2e8f9f7 | 186 | } |
<> | 144:ef7eb2e8f9f7 | 187 | |
<> | 144:ef7eb2e8f9f7 | 188 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
<> | 144:ef7eb2e8f9f7 | 189 | { |
<> | 144:ef7eb2e8f9f7 | 190 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
<> | 144:ef7eb2e8f9f7 | 191 | } |
<> | 144:ef7eb2e8f9f7 | 192 | |
<> | 144:ef7eb2e8f9f7 | 193 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
<> | 144:ef7eb2e8f9f7 | 194 | { |
<> | 144:ef7eb2e8f9f7 | 195 | pwmout_pulsewidth_us(obj, ms * 1000); |
<> | 144:ef7eb2e8f9f7 | 196 | } |
<> | 144:ef7eb2e8f9f7 | 197 | |
<> | 144:ef7eb2e8f9f7 | 198 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
<> | 144:ef7eb2e8f9f7 | 199 | { |
<> | 144:ef7eb2e8f9f7 | 200 | float value = (float)us / (float)obj->period; |
<> | 144:ef7eb2e8f9f7 | 201 | pwmout_write(obj, value); |
<> | 144:ef7eb2e8f9f7 | 202 | } |
<> | 144:ef7eb2e8f9f7 | 203 | |
<> | 144:ef7eb2e8f9f7 | 204 | #endif |