Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 3 * @file pwmout_api.c
sahilmgandhi 18:6a4db94011d3 4 * @brief Implementation of a PWM driver
sahilmgandhi 18:6a4db94011d3 5 * @internal
sahilmgandhi 18:6a4db94011d3 6 * @author ON Semiconductor
sahilmgandhi 18:6a4db94011d3 7 * $Rev:
sahilmgandhi 18:6a4db94011d3 8 * $Date:
sahilmgandhi 18:6a4db94011d3 9 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
sahilmgandhi 18:6a4db94011d3 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
sahilmgandhi 18:6a4db94011d3 12 * under limited terms and conditions. The terms and conditions pertaining to the software
sahilmgandhi 18:6a4db94011d3 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
sahilmgandhi 18:6a4db94011d3 14 * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
sahilmgandhi 18:6a4db94011d3 15 * if applicable the software license agreement. Do not use this software and/or
sahilmgandhi 18:6a4db94011d3 16 * documentation unless you have carefully read and you agree to the limited terms and
sahilmgandhi 18:6a4db94011d3 17 * conditions. By using this software and/or documentation, you agree to the limited
sahilmgandhi 18:6a4db94011d3 18 * terms and conditions.
sahilmgandhi 18:6a4db94011d3 19 *
sahilmgandhi 18:6a4db94011d3 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
sahilmgandhi 18:6a4db94011d3 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
sahilmgandhi 18:6a4db94011d3 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
sahilmgandhi 18:6a4db94011d3 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
sahilmgandhi 18:6a4db94011d3 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
sahilmgandhi 18:6a4db94011d3 25 * @endinternal
sahilmgandhi 18:6a4db94011d3 26 */
sahilmgandhi 18:6a4db94011d3 27 #include "pwmout_api.h"
sahilmgandhi 18:6a4db94011d3 28 #include "PeripheralPins.h"
sahilmgandhi 18:6a4db94011d3 29 #include "mbed_assert.h"
sahilmgandhi 18:6a4db94011d3 30 #include "clock.h"
sahilmgandhi 18:6a4db94011d3 31
sahilmgandhi 18:6a4db94011d3 32 #if DEVICE_PWMOUT
sahilmgandhi 18:6a4db94011d3 33
sahilmgandhi 18:6a4db94011d3 34 /**
sahilmgandhi 18:6a4db94011d3 35 * \defgroup hal_pwmout Pwmout hal functions
sahilmgandhi 18:6a4db94011d3 36 * @{
sahilmgandhi 18:6a4db94011d3 37 */
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39 /** Initialize the pwm out peripheral and configure the pin
sahilmgandhi 18:6a4db94011d3 40 *
sahilmgandhi 18:6a4db94011d3 41 * @param obj The pwmout object to initialize
sahilmgandhi 18:6a4db94011d3 42 * @param pin The pwmout pin to initialize
sahilmgandhi 18:6a4db94011d3 43 */
sahilmgandhi 18:6a4db94011d3 44 void pwmout_init(pwmout_t *obj, PinName pin)
sahilmgandhi 18:6a4db94011d3 45 {
sahilmgandhi 18:6a4db94011d3 46 /* Get the base address of the PWM register using the pinmap functions ; pwmout_s struct contains base address only */
sahilmgandhi 18:6a4db94011d3 47 PWMName pwm;
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 50 MBED_ASSERT(pwm != (PWMName)NC);
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 pinmap_pinout(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 53
sahilmgandhi 18:6a4db94011d3 54 obj->pwmReg = (PwmReg_pt)pwm;
sahilmgandhi 18:6a4db94011d3 55 MBED_ASSERT(obj->pwmReg != 0x00000000);
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 CLOCK_ENABLE(CLOCK_PWM);
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 /* Configuration parameters of duty cycle 0x4000B000, and prescaler 0x4000B00C, shall be set to default values */
sahilmgandhi 18:6a4db94011d3 60 /* Duty cycle shall be 50% and prescaler shall be disabled by default */
sahilmgandhi 18:6a4db94011d3 61 obj->pwmReg->DUTYCYCLE = 0x80;
sahilmgandhi 18:6a4db94011d3 62
sahilmgandhi 18:6a4db94011d3 63 /* Write the PWM output enable register 0x4000B004, to 1 */
sahilmgandhi 18:6a4db94011d3 64 obj->pwmReg->PWM_ENABLE = 0x1;
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 obj->pwmReg->PRESCALE_DISABLE = 0x1;
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68 }
sahilmgandhi 18:6a4db94011d3 69
sahilmgandhi 18:6a4db94011d3 70 /** Deinitialize the pwmout object
sahilmgandhi 18:6a4db94011d3 71 *
sahilmgandhi 18:6a4db94011d3 72 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 73 */
sahilmgandhi 18:6a4db94011d3 74 void pwmout_free(pwmout_t *obj)
sahilmgandhi 18:6a4db94011d3 75 {
sahilmgandhi 18:6a4db94011d3 76 /* Write the PWM output disable register 0x4000B008, to 1 */
sahilmgandhi 18:6a4db94011d3 77 obj->pwmReg->PWM_DISABLE = 0x1;
sahilmgandhi 18:6a4db94011d3 78 }
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 /** Set the output duty-cycle in range <0.0f, 1.0f>
sahilmgandhi 18:6a4db94011d3 81 *
sahilmgandhi 18:6a4db94011d3 82 * Value 0.0f represents 0 percentage, 1.0f represents 100 percent.
sahilmgandhi 18:6a4db94011d3 83 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 84 * @param percent The floating-point percentage number
sahilmgandhi 18:6a4db94011d3 85 */
sahilmgandhi 18:6a4db94011d3 86 void pwmout_write(pwmout_t *obj, float percent)
sahilmgandhi 18:6a4db94011d3 87 {
sahilmgandhi 18:6a4db94011d3 88 if (percent == 0.0) {
sahilmgandhi 18:6a4db94011d3 89 obj->pwmReg->DUTYCYCLE = 0x00;
sahilmgandhi 18:6a4db94011d3 90 } else if (percent == 1.0) {
sahilmgandhi 18:6a4db94011d3 91 obj->pwmReg->DUTYCYCLE = 0xFF;
sahilmgandhi 18:6a4db94011d3 92 } else {
sahilmgandhi 18:6a4db94011d3 93 /* Write the duty cycle config register 0x4000B000, with the value passed on */
sahilmgandhi 18:6a4db94011d3 94 /* ((percent * 255) + 1) is the duty cycle. Plus 1 is for accounting for round off errors; like a ceil function */
sahilmgandhi 18:6a4db94011d3 95 obj->pwmReg->DUTYCYCLE = (uint8_t)((percent * 255) + 1);
sahilmgandhi 18:6a4db94011d3 96 }
sahilmgandhi 18:6a4db94011d3 97 }
sahilmgandhi 18:6a4db94011d3 98
sahilmgandhi 18:6a4db94011d3 99 /** Read the current float-point output duty-cycle
sahilmgandhi 18:6a4db94011d3 100 *
sahilmgandhi 18:6a4db94011d3 101 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 102 * @return A floating-point output duty-cycle
sahilmgandhi 18:6a4db94011d3 103 */
sahilmgandhi 18:6a4db94011d3 104 float pwmout_read(pwmout_t *obj)
sahilmgandhi 18:6a4db94011d3 105 {
sahilmgandhi 18:6a4db94011d3 106 float retVal = 0.0;
sahilmgandhi 18:6a4db94011d3 107 float dc = 0.0;
sahilmgandhi 18:6a4db94011d3 108
sahilmgandhi 18:6a4db94011d3 109 /* Read out the value of duty cycle register 0x4000B000 and return as a percent */
sahilmgandhi 18:6a4db94011d3 110 /* Read value / 255 is the percent returned */
sahilmgandhi 18:6a4db94011d3 111 dc = obj->pwmReg->DUTYCYCLE;
sahilmgandhi 18:6a4db94011d3 112 retVal = dc/ (float)255;
sahilmgandhi 18:6a4db94011d3 113
sahilmgandhi 18:6a4db94011d3 114 return(retVal);
sahilmgandhi 18:6a4db94011d3 115 }
sahilmgandhi 18:6a4db94011d3 116
sahilmgandhi 18:6a4db94011d3 117 /** Set the PWM period specified in seconds, keeping the duty cycle the same
sahilmgandhi 18:6a4db94011d3 118 *
sahilmgandhi 18:6a4db94011d3 119 * Periods smaller than microseconds (the lowest resolution) are set to zero.
sahilmgandhi 18:6a4db94011d3 120 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 121 * @param seconds The floating-point seconds period
sahilmgandhi 18:6a4db94011d3 122 */
sahilmgandhi 18:6a4db94011d3 123 void pwmout_period(pwmout_t *obj, float seconds)
sahilmgandhi 18:6a4db94011d3 124 {
sahilmgandhi 18:6a4db94011d3 125 /* Cannot be configured, prescaler is either 256 or 4096 */
sahilmgandhi 18:6a4db94011d3 126 return;
sahilmgandhi 18:6a4db94011d3 127 }
sahilmgandhi 18:6a4db94011d3 128
sahilmgandhi 18:6a4db94011d3 129 /** Set the PWM period specified in miliseconds, keeping the duty cycle the same
sahilmgandhi 18:6a4db94011d3 130 *
sahilmgandhi 18:6a4db94011d3 131 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 132 * @param ms The milisecond period
sahilmgandhi 18:6a4db94011d3 133 */
sahilmgandhi 18:6a4db94011d3 134 void pwmout_period_ms(pwmout_t *obj, int ms)
sahilmgandhi 18:6a4db94011d3 135 {
sahilmgandhi 18:6a4db94011d3 136 /* Cannot be configured, prescaler is either 256 or 4096 */
sahilmgandhi 18:6a4db94011d3 137 return;
sahilmgandhi 18:6a4db94011d3 138 }
sahilmgandhi 18:6a4db94011d3 139
sahilmgandhi 18:6a4db94011d3 140 /** Set the PWM period specified in microseconds, keeping the duty cycle the same
sahilmgandhi 18:6a4db94011d3 141 *
sahilmgandhi 18:6a4db94011d3 142 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 143 * @param us The microsecond period
sahilmgandhi 18:6a4db94011d3 144 */
sahilmgandhi 18:6a4db94011d3 145 void pwmout_period_us(pwmout_t *obj, int us)
sahilmgandhi 18:6a4db94011d3 146 {
sahilmgandhi 18:6a4db94011d3 147 /* Cannot be configured, prescaler is either 256 or 4096 */
sahilmgandhi 18:6a4db94011d3 148 return;
sahilmgandhi 18:6a4db94011d3 149 }
sahilmgandhi 18:6a4db94011d3 150
sahilmgandhi 18:6a4db94011d3 151 /** Set the PWM pulsewidth specified in seconds, keeping the period the same.
sahilmgandhi 18:6a4db94011d3 152 *
sahilmgandhi 18:6a4db94011d3 153 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 154 * @param seconds The floating-point pulsewidth in seconds
sahilmgandhi 18:6a4db94011d3 155 */
sahilmgandhi 18:6a4db94011d3 156 void pwmout_pulsewidth(pwmout_t *obj, float seconds)
sahilmgandhi 18:6a4db94011d3 157 {
sahilmgandhi 18:6a4db94011d3 158 /* Pulse width can never be in seconds since the period
sahilmgandhi 18:6a4db94011d3 159 * itself is limited to either 8uSec or 128uSec
sahilmgandhi 18:6a4db94011d3 160 */
sahilmgandhi 18:6a4db94011d3 161 return;
sahilmgandhi 18:6a4db94011d3 162 }
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 /** Set the PWM pulsewidth specified in miliseconds, keeping the period the same.
sahilmgandhi 18:6a4db94011d3 165 *
sahilmgandhi 18:6a4db94011d3 166 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 167 * @param ms The floating-point pulsewidth in miliseconds
sahilmgandhi 18:6a4db94011d3 168 */
sahilmgandhi 18:6a4db94011d3 169 void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
sahilmgandhi 18:6a4db94011d3 170 {
sahilmgandhi 18:6a4db94011d3 171
sahilmgandhi 18:6a4db94011d3 172 /* Pulse width can never be in seconds since the period
sahilmgandhi 18:6a4db94011d3 173 * itself is limited to either 8uSec or 128uSec
sahilmgandhi 18:6a4db94011d3 174 */
sahilmgandhi 18:6a4db94011d3 175 return;
sahilmgandhi 18:6a4db94011d3 176 }
sahilmgandhi 18:6a4db94011d3 177
sahilmgandhi 18:6a4db94011d3 178 /** Set the PWM pulsewidth specified in microseconds, keeping the period the same.
sahilmgandhi 18:6a4db94011d3 179 *
sahilmgandhi 18:6a4db94011d3 180 * @param obj The pwmout object
sahilmgandhi 18:6a4db94011d3 181 * @param us The floating-point pulsewidth in microseconds
sahilmgandhi 18:6a4db94011d3 182 */
sahilmgandhi 18:6a4db94011d3 183 void pwmout_pulsewidth_us(pwmout_t *obj, int us)
sahilmgandhi 18:6a4db94011d3 184 {
sahilmgandhi 18:6a4db94011d3 185 int pulseWidth = 0;
sahilmgandhi 18:6a4db94011d3 186
sahilmgandhi 18:6a4db94011d3 187 /* Check if the uSec value is greater than 128uSec, if so reject */
sahilmgandhi 18:6a4db94011d3 188 if (us > 128) {
sahilmgandhi 18:6a4db94011d3 189 return;
sahilmgandhi 18:6a4db94011d3 190 }
sahilmgandhi 18:6a4db94011d3 191 /* If pulsewidth is less than 128uSec, set the prescaler to 4096
sahilmgandhi 18:6a4db94011d3 192 * by enabling prescale register 0x4000B00C to 1 */
sahilmgandhi 18:6a4db94011d3 193 obj->pwmReg->PRESCALE_ENABLE = 0x1;
sahilmgandhi 18:6a4db94011d3 194
sahilmgandhi 18:6a4db94011d3 195 /* Calculate the duty cycle based on the width of the pulse */
sahilmgandhi 18:6a4db94011d3 196 /* ((255 * us) / 128) + 1 = duty cycle */
sahilmgandhi 18:6a4db94011d3 197 pulseWidth = (int)((float)(255 * us)/(float)128) + 1;
sahilmgandhi 18:6a4db94011d3 198 if (us == 0) {
sahilmgandhi 18:6a4db94011d3 199 obj->pwmReg->DUTYCYCLE = 0x0;
sahilmgandhi 18:6a4db94011d3 200 } else if (us == 128) {
sahilmgandhi 18:6a4db94011d3 201 obj->pwmReg->DUTYCYCLE = 0xFF;
sahilmgandhi 18:6a4db94011d3 202 } else {
sahilmgandhi 18:6a4db94011d3 203 obj->pwmReg->DUTYCYCLE = (uint8_t)pulseWidth;
sahilmgandhi 18:6a4db94011d3 204 }
sahilmgandhi 18:6a4db94011d3 205 }
sahilmgandhi 18:6a4db94011d3 206
sahilmgandhi 18:6a4db94011d3 207 /**@}*/
sahilmgandhi 18:6a4db94011d3 208
sahilmgandhi 18:6a4db94011d3 209 #endif // DEVICE_PWMOUT