mbed library sources

Fork of mbed-src by mbed official

Committer:
HiAlgoBoost
Date:
Sun Aug 09 05:18:54 2015 +0000
Revision:
603:f00c7e78e8b4
Parent:
567:a97fd0eca828
Evening of August 8th version

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