Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 ******************************************************************************
sahilmgandhi 18:6a4db94011d3 3 * @file port_api.c
sahilmgandhi 18:6a4db94011d3 4 * @brief Implementation of a port API
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 */
sahilmgandhi 18:6a4db94011d3 26 #include "gpio.h"
sahilmgandhi 18:6a4db94011d3 27 #include "gpio_api.h"
sahilmgandhi 18:6a4db94011d3 28 #include "port_api.h"
sahilmgandhi 18:6a4db94011d3 29 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 30
sahilmgandhi 18:6a4db94011d3 31 #if DEVICE_PORTIN || DEVICE_PORTOUT
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 /** Get the pin name from the port's pin number
sahilmgandhi 18:6a4db94011d3 34 *
sahilmgandhi 18:6a4db94011d3 35 * @param port The port name
sahilmgandhi 18:6a4db94011d3 36 * @param pin_n The pin number within the specified port
sahilmgandhi 18:6a4db94011d3 37 * @return The pin name for the port's pin number
sahilmgandhi 18:6a4db94011d3 38 */
sahilmgandhi 18:6a4db94011d3 39 PinName port_pin(PortName port, int pin_n)
sahilmgandhi 18:6a4db94011d3 40 {
sahilmgandhi 18:6a4db94011d3 41 return((PinName)(pin_n));
sahilmgandhi 18:6a4db94011d3 42 }
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 /** Initilize the port
sahilmgandhi 18:6a4db94011d3 45 *
sahilmgandhi 18:6a4db94011d3 46 * @param obj The port object to initialize
sahilmgandhi 18:6a4db94011d3 47 * @param port The port name
sahilmgandhi 18:6a4db94011d3 48 * @param mask The bitmask to identify which bits in the port should be included (0 - ignore)
sahilmgandhi 18:6a4db94011d3 49 * @param dir The port direction
sahilmgandhi 18:6a4db94011d3 50 */
sahilmgandhi 18:6a4db94011d3 51 void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
sahilmgandhi 18:6a4db94011d3 52 {
sahilmgandhi 18:6a4db94011d3 53 uint8_t i;
sahilmgandhi 18:6a4db94011d3 54 PinName pin;
sahilmgandhi 18:6a4db94011d3 55
sahilmgandhi 18:6a4db94011d3 56 /* Store the port mask in obj */
sahilmgandhi 18:6a4db94011d3 57 obj->mask = mask;
sahilmgandhi 18:6a4db94011d3 58
sahilmgandhi 18:6a4db94011d3 59 /* Store the port name in obj */
sahilmgandhi 18:6a4db94011d3 60 obj->port = port;
sahilmgandhi 18:6a4db94011d3 61
sahilmgandhi 18:6a4db94011d3 62 /* Store GPIO base address */
sahilmgandhi 18:6a4db94011d3 63 obj->GPIOMEMBASE = GPIOREG;
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 for (i=0; i<NUMBER_OF_GPIO; i++) {
sahilmgandhi 18:6a4db94011d3 66 /* check for valid pin */
sahilmgandhi 18:6a4db94011d3 67 if (obj->mask & (1<<i)) {
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 /* Gpio numbers start from DIO#0 to #17, so can pass in "i" */
sahilmgandhi 18:6a4db94011d3 70 pin = port_pin(obj->port, i);
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 /* Set the pin as GPIO */
sahilmgandhi 18:6a4db94011d3 73 gpio_set(pin);
sahilmgandhi 18:6a4db94011d3 74 }
sahilmgandhi 18:6a4db94011d3 75 }
sahilmgandhi 18:6a4db94011d3 76
sahilmgandhi 18:6a4db94011d3 77 /* Call function to set pin direction */
sahilmgandhi 18:6a4db94011d3 78 port_dir(obj, dir);
sahilmgandhi 18:6a4db94011d3 79 }
sahilmgandhi 18:6a4db94011d3 80
sahilmgandhi 18:6a4db94011d3 81 /** Set the input port mode
sahilmgandhi 18:6a4db94011d3 82 *
sahilmgandhi 18:6a4db94011d3 83 * @param obj The port object
sahilmgandhi 18:6a4db94011d3 84 * @param mode THe port mode to be set
sahilmgandhi 18:6a4db94011d3 85 */
sahilmgandhi 18:6a4db94011d3 86 void port_mode(port_t *obj, PinMode mode)
sahilmgandhi 18:6a4db94011d3 87 {
sahilmgandhi 18:6a4db94011d3 88 uint8_t i = 0;
sahilmgandhi 18:6a4db94011d3 89 PinName pin;
sahilmgandhi 18:6a4db94011d3 90
sahilmgandhi 18:6a4db94011d3 91 /* For each pin in the mask, set the mode to that defined in "mode" parameter */
sahilmgandhi 18:6a4db94011d3 92 for (i=0; i < NUMBER_OF_GPIO; i++) {
sahilmgandhi 18:6a4db94011d3 93 /* check for valid pin */
sahilmgandhi 18:6a4db94011d3 94 if (obj->mask & (1<<i)) {
sahilmgandhi 18:6a4db94011d3 95
sahilmgandhi 18:6a4db94011d3 96 /* get the pin name */
sahilmgandhi 18:6a4db94011d3 97 pin = port_pin(obj->port, i);
sahilmgandhi 18:6a4db94011d3 98
sahilmgandhi 18:6a4db94011d3 99 /* Set the mode for the pin */
sahilmgandhi 18:6a4db94011d3 100 pin_mode(pin, mode);
sahilmgandhi 18:6a4db94011d3 101 }
sahilmgandhi 18:6a4db94011d3 102 }
sahilmgandhi 18:6a4db94011d3 103 }
sahilmgandhi 18:6a4db94011d3 104
sahilmgandhi 18:6a4db94011d3 105 /** Set port direction (in/out)
sahilmgandhi 18:6a4db94011d3 106 *
sahilmgandhi 18:6a4db94011d3 107 * @param obj The port object
sahilmgandhi 18:6a4db94011d3 108 * @param dir The port direction to be set
sahilmgandhi 18:6a4db94011d3 109 */
sahilmgandhi 18:6a4db94011d3 110 void port_dir(port_t *obj, PinDirection dir)
sahilmgandhi 18:6a4db94011d3 111 {
sahilmgandhi 18:6a4db94011d3 112 /* Enable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 113 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 if (dir == PIN_INPUT) {
sahilmgandhi 18:6a4db94011d3 116 obj->GPIOMEMBASE->W_IN = obj->mask;
sahilmgandhi 18:6a4db94011d3 117 } else if (dir == PIN_OUTPUT) {
sahilmgandhi 18:6a4db94011d3 118 obj->GPIOMEMBASE->W_OUT = obj->mask;
sahilmgandhi 18:6a4db94011d3 119 }
sahilmgandhi 18:6a4db94011d3 120
sahilmgandhi 18:6a4db94011d3 121 /* Disable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 122 CLOCK_DISABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 123 }
sahilmgandhi 18:6a4db94011d3 124
sahilmgandhi 18:6a4db94011d3 125 /** Write value to the port
sahilmgandhi 18:6a4db94011d3 126 *
sahilmgandhi 18:6a4db94011d3 127 * @param obj The port object
sahilmgandhi 18:6a4db94011d3 128 * @param value The value to be set
sahilmgandhi 18:6a4db94011d3 129 */
sahilmgandhi 18:6a4db94011d3 130 void port_write(port_t *obj, int value)
sahilmgandhi 18:6a4db94011d3 131 {
sahilmgandhi 18:6a4db94011d3 132 /* Enable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 133 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 134
sahilmgandhi 18:6a4db94011d3 135 obj->GPIOMEMBASE->R_STATE_W_SET = value;//(obj->mask & value);
sahilmgandhi 18:6a4db94011d3 136 obj->GPIOMEMBASE->R_IRQ_W_CLEAR = ~value;//(obj->mask ^ value);
sahilmgandhi 18:6a4db94011d3 137
sahilmgandhi 18:6a4db94011d3 138 /* Disable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 139 CLOCK_DISABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 140 }
sahilmgandhi 18:6a4db94011d3 141
sahilmgandhi 18:6a4db94011d3 142 /** Read the current value on the port
sahilmgandhi 18:6a4db94011d3 143 *
sahilmgandhi 18:6a4db94011d3 144 * @param obj The port object
sahilmgandhi 18:6a4db94011d3 145 * @return An integer with each bit corresponding to an associated port pin setting
sahilmgandhi 18:6a4db94011d3 146 */
sahilmgandhi 18:6a4db94011d3 147 int port_read(port_t *obj)
sahilmgandhi 18:6a4db94011d3 148 {
sahilmgandhi 18:6a4db94011d3 149 int gpio_level = 0;
sahilmgandhi 18:6a4db94011d3 150
sahilmgandhi 18:6a4db94011d3 151 /* Enable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 152 CLOCK_ENABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 153
sahilmgandhi 18:6a4db94011d3 154 gpio_level = obj->GPIOMEMBASE->R_STATE_W_SET;
sahilmgandhi 18:6a4db94011d3 155
sahilmgandhi 18:6a4db94011d3 156 /* Disable the GPIO clock */
sahilmgandhi 18:6a4db94011d3 157 CLOCK_DISABLE(CLOCK_GPIO);
sahilmgandhi 18:6a4db94011d3 158
sahilmgandhi 18:6a4db94011d3 159 return(gpio_level);
sahilmgandhi 18:6a4db94011d3 160 }
sahilmgandhi 18:6a4db94011d3 161
sahilmgandhi 18:6a4db94011d3 162 #endif