mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Sep 04 09:30:10 2015 +0100
Revision:
619:034e698bc035
Parent:
567:a97fd0eca828
Synchronized with git revision 92d1bfad30082571776c810a56fd471d30514ccf

Full URL: https://github.com/mbedmicro/mbed/commit/92d1bfad30082571776c810a56fd471d30514ccf/

Change directory structure and move files.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 567:a97fd0eca828 1 /* mbed Microcontroller Library
mbed_official 567:a97fd0eca828 2 *******************************************************************************
mbed_official 567:a97fd0eca828 3 * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
mbed_official 567:a97fd0eca828 4 * All rights reserved.
mbed_official 567:a97fd0eca828 5 *
mbed_official 567:a97fd0eca828 6 * Redistribution and use in source and binary forms, with or without
mbed_official 567:a97fd0eca828 7 * modification, are permitted provided that the following conditions are met:
mbed_official 567:a97fd0eca828 8 *
mbed_official 567:a97fd0eca828 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 567:a97fd0eca828 10 * this list of conditions and the following disclaimer.
mbed_official 567:a97fd0eca828 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 567:a97fd0eca828 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 567:a97fd0eca828 13 * and/or other materials provided with the distribution.
mbed_official 567:a97fd0eca828 14 * 3. Neither the name of ARM Limited nor the names of its contributors
mbed_official 567:a97fd0eca828 15 * may be used to endorse or promote products derived from this software
mbed_official 567:a97fd0eca828 16 * without specific prior written permission.
mbed_official 567:a97fd0eca828 17 *
mbed_official 567:a97fd0eca828 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 567:a97fd0eca828 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 567:a97fd0eca828 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 567:a97fd0eca828 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 567:a97fd0eca828 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 567:a97fd0eca828 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 567:a97fd0eca828 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 567:a97fd0eca828 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 567:a97fd0eca828 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 567:a97fd0eca828 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 567:a97fd0eca828 28 *******************************************************************************
mbed_official 567:a97fd0eca828 29 */
mbed_official 567:a97fd0eca828 30 #include "pwmout_api.h"
mbed_official 567:a97fd0eca828 31
mbed_official 567:a97fd0eca828 32 #if DEVICE_PWMOUT
mbed_official 567:a97fd0eca828 33
mbed_official 567:a97fd0eca828 34 #include "cmsis.h"
mbed_official 567:a97fd0eca828 35 #include "pinmap.h"
mbed_official 567:a97fd0eca828 36 #include "mbed_error.h"
mbed_official 567:a97fd0eca828 37 #include "PeripheralPins.h"
mbed_official 619:034e698bc035 38 #include "W7500x_pwm.h"
mbed_official 567:a97fd0eca828 39
mbed_official 567:a97fd0eca828 40 static PWM_TimerModeInitTypeDef TimerModeStructure;
mbed_official 567:a97fd0eca828 41
mbed_official 567:a97fd0eca828 42 void pwmout_init(pwmout_t* obj, PinName pin)
mbed_official 567:a97fd0eca828 43 {
mbed_official 567:a97fd0eca828 44 // Get the peripheral name from the pin and assign it to the object
mbed_official 567:a97fd0eca828 45 obj->PWM_CHx = (PWM_CHn_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 567:a97fd0eca828 46
mbed_official 567:a97fd0eca828 47 if (obj->PWM_CHx == (PWM_CHn_TypeDef *)NC) {
mbed_official 567:a97fd0eca828 48 error("PWM error: pinout mapping failed.");
mbed_official 567:a97fd0eca828 49 }
mbed_official 567:a97fd0eca828 50
mbed_official 567:a97fd0eca828 51 // Configure GPIO
mbed_official 567:a97fd0eca828 52 pinmap_pinout(pin, PinMap_PWM);
mbed_official 567:a97fd0eca828 53
mbed_official 567:a97fd0eca828 54 GetSystemClock();
mbed_official 567:a97fd0eca828 55
mbed_official 567:a97fd0eca828 56 obj->pin = pin;
mbed_official 567:a97fd0eca828 57
mbed_official 567:a97fd0eca828 58 pwmout_period_us(obj, 20000); // 20 ms per default
mbed_official 567:a97fd0eca828 59 }
mbed_official 567:a97fd0eca828 60
mbed_official 567:a97fd0eca828 61 void pwmout_free(pwmout_t* obj)
mbed_official 567:a97fd0eca828 62 {
mbed_official 567:a97fd0eca828 63 // Configure GPIO
mbed_official 567:a97fd0eca828 64 pin_function(obj->pin, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_NOPULL, Px_AFSR_AF0));
mbed_official 567:a97fd0eca828 65 }
mbed_official 567:a97fd0eca828 66
mbed_official 567:a97fd0eca828 67 void pwmout_write(pwmout_t* obj, float value)
mbed_official 567:a97fd0eca828 68 {
mbed_official 567:a97fd0eca828 69 if (value < (float)0.0) {
mbed_official 567:a97fd0eca828 70 value = 0.0;
mbed_official 567:a97fd0eca828 71 } else if (value > (float)1.0) {
mbed_official 567:a97fd0eca828 72 value = 1.0;
mbed_official 567:a97fd0eca828 73 }
mbed_official 567:a97fd0eca828 74
mbed_official 567:a97fd0eca828 75 obj->pulse = (uint32_t)((float)obj->period * value);
mbed_official 567:a97fd0eca828 76
mbed_official 567:a97fd0eca828 77 PWM_CHn_Stop(obj->PWM_CHx);
mbed_official 567:a97fd0eca828 78
mbed_official 567:a97fd0eca828 79 TimerModeStructure.PWM_CHn_PR = obj->PrescalerValue - 1;
mbed_official 567:a97fd0eca828 80 TimerModeStructure.PWM_CHn_MR = obj->pulse;
mbed_official 567:a97fd0eca828 81 TimerModeStructure.PWM_CHn_LR = obj->period;
mbed_official 567:a97fd0eca828 82 TimerModeStructure.PWM_CHn_UDMR = PWM_CHn_UDMR_UpCount;
mbed_official 567:a97fd0eca828 83 TimerModeStructure.PWM_CHn_PDMR = PWM_CHn_PDMR_Periodic;
mbed_official 567:a97fd0eca828 84
mbed_official 567:a97fd0eca828 85 PWM_TimerModeInit(obj->PWM_CHx, &TimerModeStructure);
mbed_official 567:a97fd0eca828 86
mbed_official 567:a97fd0eca828 87 PWM_CHn_Start(obj->PWM_CHx);
mbed_official 567:a97fd0eca828 88 }
mbed_official 567:a97fd0eca828 89
mbed_official 567:a97fd0eca828 90 float pwmout_read(pwmout_t* obj)
mbed_official 567:a97fd0eca828 91 {
mbed_official 567:a97fd0eca828 92 float value = 0;
mbed_official 567:a97fd0eca828 93 if (obj->period > 0) {
mbed_official 567:a97fd0eca828 94 value = (float)(obj->pulse) / (float)(obj->period);
mbed_official 567:a97fd0eca828 95 }
mbed_official 567:a97fd0eca828 96 return ((value > (float)1.0) ? (float)(1.0) : (value));
mbed_official 567:a97fd0eca828 97 }
mbed_official 567:a97fd0eca828 98
mbed_official 567:a97fd0eca828 99 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 567:a97fd0eca828 100 {
mbed_official 567:a97fd0eca828 101 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 567:a97fd0eca828 102 }
mbed_official 567:a97fd0eca828 103
mbed_official 567:a97fd0eca828 104 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 567:a97fd0eca828 105 {
mbed_official 567:a97fd0eca828 106 pwmout_period_us(obj, ms * 1000);
mbed_official 567:a97fd0eca828 107 }
mbed_official 567:a97fd0eca828 108
mbed_official 567:a97fd0eca828 109 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 567:a97fd0eca828 110 {
mbed_official 567:a97fd0eca828 111 PWM_CHn_Stop(obj->PWM_CHx);
mbed_official 567:a97fd0eca828 112 // Update the SystemCoreClock variable
mbed_official 567:a97fd0eca828 113 SystemCoreClockUpdate();
mbed_official 567:a97fd0eca828 114
mbed_official 567:a97fd0eca828 115 obj->period = (us * 2) - 1;
mbed_official 567:a97fd0eca828 116 obj->pulse = us / 2;
mbed_official 567:a97fd0eca828 117
mbed_official 567:a97fd0eca828 118 obj->PrescalerValue = (SystemCoreClock / 1000000) / 2;
mbed_official 567:a97fd0eca828 119 TimerModeStructure.PWM_CHn_PR = obj->PrescalerValue - 1;
mbed_official 567:a97fd0eca828 120 TimerModeStructure.PWM_CHn_MR = obj->pulse;
mbed_official 567:a97fd0eca828 121 TimerModeStructure.PWM_CHn_LR = obj->period;
mbed_official 567:a97fd0eca828 122 TimerModeStructure.PWM_CHn_UDMR = PWM_CHn_UDMR_UpCount;
mbed_official 567:a97fd0eca828 123 TimerModeStructure.PWM_CHn_PDMR = PWM_CHn_PDMR_Periodic;
mbed_official 567:a97fd0eca828 124
mbed_official 567:a97fd0eca828 125 PWM_TimerModeInit(obj->PWM_CHx, &TimerModeStructure);
mbed_official 567:a97fd0eca828 126 PWM_CtrlPWMOutputEnable(obj->PWM_CHx);
mbed_official 567:a97fd0eca828 127 }
mbed_official 567:a97fd0eca828 128
mbed_official 567:a97fd0eca828 129 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 567:a97fd0eca828 130 {
mbed_official 567:a97fd0eca828 131 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 567:a97fd0eca828 132 }
mbed_official 567:a97fd0eca828 133
mbed_official 567:a97fd0eca828 134 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 567:a97fd0eca828 135 {
mbed_official 567:a97fd0eca828 136 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 567:a97fd0eca828 137 }
mbed_official 567:a97fd0eca828 138
mbed_official 567:a97fd0eca828 139 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 567:a97fd0eca828 140 {
mbed_official 567:a97fd0eca828 141 float value = (float)(2 * us) / (float)obj->period;
mbed_official 567:a97fd0eca828 142 pwmout_write(obj, value);
mbed_official 567:a97fd0eca828 143 }
mbed_official 567:a97fd0eca828 144
mbed_official 567:a97fd0eca828 145 #endif