Kevin Kadooka / mbed-dev

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

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