MacroRat / MouseCode

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Wed May 24 01:57:01 2017 +0000
Revision:
29:ec2c5a69acd6
Parent:
18:6a4db94011d3
Need to change ir2-ir3 to now be ir1 - ir4

Who changed what in which revision?

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