mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by Umar Naeem

Committer:
ranaumarnaeem
Date:
Tue May 23 12:54:50 2017 +0000
Revision:
165:2dd56e6daeec
Parent:
149:156823d33999
jhjg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 *******************************************************************************
<> 144:ef7eb2e8f9f7 3 * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
<> 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 ARM Limited 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 #include "W7500x_pwm.h"
<> 144:ef7eb2e8f9f7 39
<> 144:ef7eb2e8f9f7 40 static PWM_TimerModeInitTypeDef TimerModeStructure;
<> 144:ef7eb2e8f9f7 41
<> 144:ef7eb2e8f9f7 42 void pwmout_init(pwmout_t* obj, PinName pin)
<> 144:ef7eb2e8f9f7 43 {
<> 144:ef7eb2e8f9f7 44 // Get the peripheral name from the pin and assign it to the object
<> 144:ef7eb2e8f9f7 45 obj->PWM_CHx = (PWM_CHn_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 46
<> 144:ef7eb2e8f9f7 47 if (obj->PWM_CHx == (PWM_CHn_TypeDef *)NC) {
<> 144:ef7eb2e8f9f7 48 error("PWM error: pinout mapping failed.");
<> 144:ef7eb2e8f9f7 49 }
<> 144:ef7eb2e8f9f7 50
<> 144:ef7eb2e8f9f7 51 // Configure GPIO
<> 144:ef7eb2e8f9f7 52 pinmap_pinout(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 53
<> 144:ef7eb2e8f9f7 54 GetSystemClock();
<> 144:ef7eb2e8f9f7 55
<> 144:ef7eb2e8f9f7 56 obj->pin = pin;
<> 144:ef7eb2e8f9f7 57
<> 144:ef7eb2e8f9f7 58 pwmout_period_us(obj, 20000); // 20 ms per default
<> 144:ef7eb2e8f9f7 59 }
<> 144:ef7eb2e8f9f7 60
<> 144:ef7eb2e8f9f7 61 void pwmout_free(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 62 {
<> 144:ef7eb2e8f9f7 63 // Configure GPIO
<> 144:ef7eb2e8f9f7 64 pin_function(obj->pin, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_NOPULL, Px_AFSR_AF0));
<> 144:ef7eb2e8f9f7 65 }
<> 144:ef7eb2e8f9f7 66
<> 144:ef7eb2e8f9f7 67 void pwmout_write(pwmout_t* obj, float value)
<> 144:ef7eb2e8f9f7 68 {
<> 144:ef7eb2e8f9f7 69 if (value < (float)0.0) {
<> 144:ef7eb2e8f9f7 70 value = 0.0;
<> 144:ef7eb2e8f9f7 71 } else if (value > (float)1.0) {
<> 144:ef7eb2e8f9f7 72 value = 1.0;
<> 144:ef7eb2e8f9f7 73 }
<> 144:ef7eb2e8f9f7 74
<> 144:ef7eb2e8f9f7 75 obj->pulse = (uint32_t)((float)obj->period * value);
<> 144:ef7eb2e8f9f7 76
<> 144:ef7eb2e8f9f7 77 PWM_CHn_Stop(obj->PWM_CHx);
<> 144:ef7eb2e8f9f7 78
<> 144:ef7eb2e8f9f7 79 TimerModeStructure.PWM_CHn_PR = obj->PrescalerValue - 1;
<> 144:ef7eb2e8f9f7 80 TimerModeStructure.PWM_CHn_MR = obj->pulse;
<> 144:ef7eb2e8f9f7 81 TimerModeStructure.PWM_CHn_LR = obj->period;
<> 144:ef7eb2e8f9f7 82 TimerModeStructure.PWM_CHn_UDMR = PWM_CHn_UDMR_UpCount;
<> 144:ef7eb2e8f9f7 83 TimerModeStructure.PWM_CHn_PDMR = PWM_CHn_PDMR_Periodic;
<> 144:ef7eb2e8f9f7 84
<> 144:ef7eb2e8f9f7 85 PWM_TimerModeInit(obj->PWM_CHx, &TimerModeStructure);
<> 144:ef7eb2e8f9f7 86
<> 144:ef7eb2e8f9f7 87 PWM_CHn_Start(obj->PWM_CHx);
<> 144:ef7eb2e8f9f7 88 }
<> 144:ef7eb2e8f9f7 89
<> 144:ef7eb2e8f9f7 90 float pwmout_read(pwmout_t* obj)
<> 144:ef7eb2e8f9f7 91 {
<> 144:ef7eb2e8f9f7 92 float value = 0;
<> 144:ef7eb2e8f9f7 93 if (obj->period > 0) {
<> 144:ef7eb2e8f9f7 94 value = (float)(obj->pulse) / (float)(obj->period);
<> 144:ef7eb2e8f9f7 95 }
<> 144:ef7eb2e8f9f7 96 return ((value > (float)1.0) ? (float)(1.0) : (value));
<> 144:ef7eb2e8f9f7 97 }
<> 144:ef7eb2e8f9f7 98
<> 144:ef7eb2e8f9f7 99 void pwmout_period(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 100 {
<> 144:ef7eb2e8f9f7 101 pwmout_period_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 102 }
<> 144:ef7eb2e8f9f7 103
<> 144:ef7eb2e8f9f7 104 void pwmout_period_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 105 {
<> 144:ef7eb2e8f9f7 106 pwmout_period_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 107 }
<> 144:ef7eb2e8f9f7 108
<> 144:ef7eb2e8f9f7 109 void pwmout_period_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 110 {
<> 144:ef7eb2e8f9f7 111 PWM_CHn_Stop(obj->PWM_CHx);
<> 144:ef7eb2e8f9f7 112 // Update the SystemCoreClock variable
<> 144:ef7eb2e8f9f7 113 SystemCoreClockUpdate();
<> 144:ef7eb2e8f9f7 114
<> 144:ef7eb2e8f9f7 115 obj->period = (us * 2) - 1;
<> 144:ef7eb2e8f9f7 116 obj->pulse = us / 2;
<> 144:ef7eb2e8f9f7 117
<> 144:ef7eb2e8f9f7 118 obj->PrescalerValue = (SystemCoreClock / 1000000) / 2;
<> 144:ef7eb2e8f9f7 119 TimerModeStructure.PWM_CHn_PR = obj->PrescalerValue - 1;
<> 144:ef7eb2e8f9f7 120 TimerModeStructure.PWM_CHn_MR = obj->pulse;
<> 144:ef7eb2e8f9f7 121 TimerModeStructure.PWM_CHn_LR = obj->period;
<> 144:ef7eb2e8f9f7 122 TimerModeStructure.PWM_CHn_UDMR = PWM_CHn_UDMR_UpCount;
<> 144:ef7eb2e8f9f7 123 TimerModeStructure.PWM_CHn_PDMR = PWM_CHn_PDMR_Periodic;
<> 144:ef7eb2e8f9f7 124
<> 144:ef7eb2e8f9f7 125 PWM_TimerModeInit(obj->PWM_CHx, &TimerModeStructure);
<> 144:ef7eb2e8f9f7 126 PWM_CtrlPWMOutputEnable(obj->PWM_CHx);
<> 144:ef7eb2e8f9f7 127 }
<> 144:ef7eb2e8f9f7 128
<> 144:ef7eb2e8f9f7 129 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
<> 144:ef7eb2e8f9f7 130 {
<> 144:ef7eb2e8f9f7 131 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 144:ef7eb2e8f9f7 132 }
<> 144:ef7eb2e8f9f7 133
<> 144:ef7eb2e8f9f7 134 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
<> 144:ef7eb2e8f9f7 135 {
<> 144:ef7eb2e8f9f7 136 pwmout_pulsewidth_us(obj, ms * 1000);
<> 144:ef7eb2e8f9f7 137 }
<> 144:ef7eb2e8f9f7 138
<> 144:ef7eb2e8f9f7 139 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
<> 144:ef7eb2e8f9f7 140 {
<> 144:ef7eb2e8f9f7 141 float value = (float)(2 * us) / (float)obj->period;
<> 144:ef7eb2e8f9f7 142 pwmout_write(obj, value);
<> 144:ef7eb2e8f9f7 143 }
<> 144:ef7eb2e8f9f7 144
<> 144:ef7eb2e8f9f7 145 #endif