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 gpio_api.c
sahilmgandhi 18:6a4db94011d3 4 * @brief Implementation of a GPIO driver
sahilmgandhi 18:6a4db94011d3 5 * @internal
sahilmgandhi 18:6a4db94011d3 6 * @author ON Semiconductor
sahilmgandhi 18:6a4db94011d3 7 * $Rev:
sahilmgandhi 18:6a4db94011d3 8 * $Date: 2015-11-04 $
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 * @ingroup gpio
sahilmgandhi 18:6a4db94011d3 28 *
sahilmgandhi 18:6a4db94011d3 29 * @details
sahilmgandhi 18:6a4db94011d3 30 *
sahilmgandhi 18:6a4db94011d3 31 * @internal
sahilmgandhi 18:6a4db94011d3 32 * <h1> Reference document(s) </h1>
sahilmgandhi 18:6a4db94011d3 33 * <p>
sahilmgandhi 18:6a4db94011d3 34 * Reference document: IPC7203 APB GPIO Design Specification v1.2</a>
sahilmgandhi 18:6a4db94011d3 35 * </p>
sahilmgandhi 18:6a4db94011d3 36 * @endinternal
sahilmgandhi 18:6a4db94011d3 37 *
sahilmgandhi 18:6a4db94011d3 38 * <h1> Functional description (internal) </h1>
sahilmgandhi 18:6a4db94011d3 39 * <p>
sahilmgandhi 18:6a4db94011d3 40 * Each GPIO line can be independently programmed as an input or an output. Separate Set
sahilmgandhi 18:6a4db94011d3 41 * and Clear registers are provided since it is likely that different software tasks may be
sahilmgandhi 18:6a4db94011d3 42 * servicing different I/O signals. Inputs are synchronized to the system clock
sahilmgandhi 18:6a4db94011d3 43 * through a pair of flip-flops. Each input can be programmed
sahilmgandhi 18:6a4db94011d3 44 * to cause an interrupt to be generated. The interrupt can be programmed to be level-sensitive
sahilmgandhi 18:6a4db94011d3 45 * or edge-sensitive and the level (high or low) or edge (rising, falling or either) that causes
sahilmgandhi 18:6a4db94011d3 46 * the interrupt can be selected. Interrupts can be individually enabled or disabled.
sahilmgandhi 18:6a4db94011d3 47 * Level-sensitive interrupts stay asserted until the interrupting condition is cleared.
sahilmgandhi 18:6a4db94011d3 48 * Edge-triggered interrupts are cleared by writing to the GPIO interrupt clear register.
sahilmgandhi 18:6a4db94011d3 49 * </p>
sahilmgandhi 18:6a4db94011d3 50 *
sahilmgandhi 18:6a4db94011d3 51 * <h1> Use of GPIO driver in SW </h1>
sahilmgandhi 18:6a4db94011d3 52 * <p>
sahilmgandhi 18:6a4db94011d3 53 * The user of the GPIO driver should set the pin as GPIO, using crossbar.
sahilmgandhi 18:6a4db94011d3 54 * Init the GPIO and configure the mode and direction.This will return a device pointer. One device controls all GPIO's. It is not
sahilmgandhi 18:6a4db94011d3 55 * needed nor supported to create a device per GPIO.
sahilmgandhi 18:6a4db94011d3 56 * Next, the user should call the fGpioOpen function with the device and options as paramter.
sahilmgandhi 18:6a4db94011d3 57 * </p>
sahilmgandhi 18:6a4db94011d3 58 * <p>
sahilmgandhi 18:6a4db94011d3 59 * Use the device driver fGpioIoctl function to change the behavior of the GPIO's and to register an
sahilmgandhi 18:6a4db94011d3 60 * interrupt handler for each IO that has an interrupt enabled. There is one interrupt for all GPIO's.
sahilmgandhi 18:6a4db94011d3 61 * The GPIO driver will look up what IO caused the interrupt and call the respective interrupt handler.
sahilmgandhi 18:6a4db94011d3 62 * </p>
sahilmgandhi 18:6a4db94011d3 63 */
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 #include "gpio.h"
sahilmgandhi 18:6a4db94011d3 66
sahilmgandhi 18:6a4db94011d3 67 /* Include from the mbed-hal layer */
sahilmgandhi 18:6a4db94011d3 68 #include "gpio_api.h"
sahilmgandhi 18:6a4db94011d3 69 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 70
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 /** Set the given pin as GPIO
sahilmgandhi 18:6a4db94011d3 73 *
sahilmgandhi 18:6a4db94011d3 74 * @param pin The pin to be set as GPIO
sahilmgandhi 18:6a4db94011d3 75 * @return The GPIO port mask for this pin
sahilmgandhi 18:6a4db94011d3 76 **/
sahilmgandhi 18:6a4db94011d3 77 uint32_t gpio_set(PinName pin)
sahilmgandhi 18:6a4db94011d3 78 {
sahilmgandhi 18:6a4db94011d3 79 if (pin != NC) {
sahilmgandhi 18:6a4db94011d3 80 /* Configure to GPIO using pin function API*/
sahilmgandhi 18:6a4db94011d3 81 pin_function(pin, CONFIGURE_AS_GPIO);
sahilmgandhi 18:6a4db94011d3 82
sahilmgandhi 18:6a4db94011d3 83 return ((uint32_t) 0x1 << pin);
sahilmgandhi 18:6a4db94011d3 84
sahilmgandhi 18:6a4db94011d3 85 }
sahilmgandhi 18:6a4db94011d3 86
sahilmgandhi 18:6a4db94011d3 87 return(0x00000000);
sahilmgandhi 18:6a4db94011d3 88
sahilmgandhi 18:6a4db94011d3 89 }
sahilmgandhi 18:6a4db94011d3 90
sahilmgandhi 18:6a4db94011d3 91 /** Initialize the GPIO pin
sahilmgandhi 18:6a4db94011d3 92 *
sahilmgandhi 18:6a4db94011d3 93 * @param obj The GPIO object to initialize
sahilmgandhi 18:6a4db94011d3 94 * @param pin The GPIO pin to initialize
sahilmgandhi 18:6a4db94011d3 95 */
sahilmgandhi 18:6a4db94011d3 96 void gpio_init(gpio_t *obj, PinName pin)
sahilmgandhi 18:6a4db94011d3 97 {
sahilmgandhi 18:6a4db94011d3 98 /* Initialize the GPIO membase */
sahilmgandhi 18:6a4db94011d3 99 obj->GPIOMEMBASE = GPIOREG;
sahilmgandhi 18:6a4db94011d3 100
sahilmgandhi 18:6a4db94011d3 101 /* Initialize the pin to be GPIO */
sahilmgandhi 18:6a4db94011d3 102 obj->gpioPin = pin;
sahilmgandhi 18:6a4db94011d3 103 obj->gpioMask = gpio_set(pin);
sahilmgandhi 18:6a4db94011d3 104
sahilmgandhi 18:6a4db94011d3 105 /* Enable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 106 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 107
sahilmgandhi 18:6a4db94011d3 108 /* Set the drive strength of the pin to 1 by default */
sahilmgandhi 18:6a4db94011d3 109 /** - Get PAD IO register address for the PAD number */
sahilmgandhi 18:6a4db94011d3 110 PadReg_t *PadRegOffset = (PadReg_t*)(PADREG_BASE + (pin * PAD_REG_ADRS_BYTE_SIZE));
sahilmgandhi 18:6a4db94011d3 111
sahilmgandhi 18:6a4db94011d3 112 /** - Enable the clock for PAD peripheral device */
sahilmgandhi 18:6a4db94011d3 113 CLOCK_ENABLE(CLOCK_PAD);
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 /** - Set drive type, pulltype & drive strength */
sahilmgandhi 18:6a4db94011d3 116 PadRegOffset->PADIO0.BITS.POWER = 1;
sahilmgandhi 18:6a4db94011d3 117
sahilmgandhi 18:6a4db94011d3 118 /** - Disable the clock for PAD peripheral device */
sahilmgandhi 18:6a4db94011d3 119 CLOCK_DISABLE(CLOCK_PAD);
sahilmgandhi 18:6a4db94011d3 120 }
sahilmgandhi 18:6a4db94011d3 121
sahilmgandhi 18:6a4db94011d3 122 /** Set the input pin mode
sahilmgandhi 18:6a4db94011d3 123 *
sahilmgandhi 18:6a4db94011d3 124 * @param obj The GPIO object
sahilmgandhi 18:6a4db94011d3 125 * @param mode The pin mode to be set
sahilmgandhi 18:6a4db94011d3 126 */
sahilmgandhi 18:6a4db94011d3 127 void gpio_mode(gpio_t *obj, PinMode mode)
sahilmgandhi 18:6a4db94011d3 128 {
sahilmgandhi 18:6a4db94011d3 129 uint32_t pin = obj->gpioPin;
sahilmgandhi 18:6a4db94011d3 130
sahilmgandhi 18:6a4db94011d3 131 /* Set the mode for the pin */
sahilmgandhi 18:6a4db94011d3 132 pin_mode((PinName)pin, mode);
sahilmgandhi 18:6a4db94011d3 133 }
sahilmgandhi 18:6a4db94011d3 134
sahilmgandhi 18:6a4db94011d3 135 /** Set the pin direction
sahilmgandhi 18:6a4db94011d3 136 *
sahilmgandhi 18:6a4db94011d3 137 * @param obj The GPIO object
sahilmgandhi 18:6a4db94011d3 138 * @param direction The pin direction to be set
sahilmgandhi 18:6a4db94011d3 139 */
sahilmgandhi 18:6a4db94011d3 140 void gpio_dir(gpio_t *obj, PinDirection direction)
sahilmgandhi 18:6a4db94011d3 141 {
sahilmgandhi 18:6a4db94011d3 142 /* Enable the GPIO clock which may have been switched off by other drivers */
sahilmgandhi 18:6a4db94011d3 143 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 144
sahilmgandhi 18:6a4db94011d3 145 if (direction == PIN_INPUT) {
sahilmgandhi 18:6a4db94011d3 146 obj->GPIOMEMBASE->W_IN = obj->gpioMask;
sahilmgandhi 18:6a4db94011d3 147 } else if (direction == PIN_OUTPUT) {
sahilmgandhi 18:6a4db94011d3 148 obj->GPIOMEMBASE->W_OUT = obj->gpioMask;
sahilmgandhi 18:6a4db94011d3 149 }
sahilmgandhi 18:6a4db94011d3 150
sahilmgandhi 18:6a4db94011d3 151 }
sahilmgandhi 18:6a4db94011d3 152
sahilmgandhi 18:6a4db94011d3 153 /** Set the output value
sahilmgandhi 18:6a4db94011d3 154 *
sahilmgandhi 18:6a4db94011d3 155 * @param obj The GPIO object
sahilmgandhi 18:6a4db94011d3 156 * @param value The value to be set
sahilmgandhi 18:6a4db94011d3 157 */
sahilmgandhi 18:6a4db94011d3 158 void gpio_write(gpio_t *obj, int value)
sahilmgandhi 18:6a4db94011d3 159 {
sahilmgandhi 18:6a4db94011d3 160
sahilmgandhi 18:6a4db94011d3 161 /* Enable the GPIO clock which may have been switched off by other drivers */
sahilmgandhi 18:6a4db94011d3 162 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 /* Set the GPIO based on value */
sahilmgandhi 18:6a4db94011d3 165 if (value) {
sahilmgandhi 18:6a4db94011d3 166 obj->GPIOMEMBASE->R_STATE_W_SET = obj->gpioMask;
sahilmgandhi 18:6a4db94011d3 167 } else {
sahilmgandhi 18:6a4db94011d3 168 obj->GPIOMEMBASE->R_IRQ_W_CLEAR = obj->gpioMask;
sahilmgandhi 18:6a4db94011d3 169 }
sahilmgandhi 18:6a4db94011d3 170
sahilmgandhi 18:6a4db94011d3 171 }
sahilmgandhi 18:6a4db94011d3 172
sahilmgandhi 18:6a4db94011d3 173 /** Read the input value
sahilmgandhi 18:6a4db94011d3 174 *
sahilmgandhi 18:6a4db94011d3 175 * @param obj The GPIO object
sahilmgandhi 18:6a4db94011d3 176 * @return An integer value 1 or 0
sahilmgandhi 18:6a4db94011d3 177 */
sahilmgandhi 18:6a4db94011d3 178 int gpio_read(gpio_t *obj)
sahilmgandhi 18:6a4db94011d3 179 {
sahilmgandhi 18:6a4db94011d3 180 int ret;
sahilmgandhi 18:6a4db94011d3 181
sahilmgandhi 18:6a4db94011d3 182 /* Enable the GPIO clock which may have been switched off by other drivers */
sahilmgandhi 18:6a4db94011d3 183 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 184
sahilmgandhi 18:6a4db94011d3 185 ret = (obj->GPIOMEMBASE->R_STATE_W_SET & obj->gpioMask) ? 1: 0;
sahilmgandhi 18:6a4db94011d3 186
sahilmgandhi 18:6a4db94011d3 187 return ret;
sahilmgandhi 18:6a4db94011d3 188 }
sahilmgandhi 18:6a4db94011d3 189
sahilmgandhi 18:6a4db94011d3 190 /* Checks if gpio object is connected (pin was not initialized with NC)
sahilmgandhi 18:6a4db94011d3 191 * @param pin The pin to be set as GPIO
sahilmgandhi 18:6a4db94011d3 192 * @return 0 if port is initialized with NC
sahilmgandhi 18:6a4db94011d3 193 **/
sahilmgandhi 18:6a4db94011d3 194 int gpio_is_connected(const gpio_t *obj)
sahilmgandhi 18:6a4db94011d3 195 {
sahilmgandhi 18:6a4db94011d3 196 if(obj->gpioPin != (PinName)NC) {
sahilmgandhi 18:6a4db94011d3 197 return 1;
sahilmgandhi 18:6a4db94011d3 198 } else {
sahilmgandhi 18:6a4db94011d3 199 return 0;
sahilmgandhi 18:6a4db94011d3 200 }
sahilmgandhi 18:6a4db94011d3 201 }