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 /* mbed Microcontroller Library
sahilmgandhi 18:6a4db94011d3 2 *******************************************************************************
sahilmgandhi 18:6a4db94011d3 3 * Copyright (c) 2017, STMicroelectronics
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 STMicroelectronics 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 "mbed_assert.h"
sahilmgandhi 18:6a4db94011d3 31 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 32 #include "PortNames.h"
sahilmgandhi 18:6a4db94011d3 33 #include "mbed_error.h"
sahilmgandhi 18:6a4db94011d3 34 #include "pin_device.h"
sahilmgandhi 18:6a4db94011d3 35
sahilmgandhi 18:6a4db94011d3 36 extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
sahilmgandhi 18:6a4db94011d3 37
sahilmgandhi 18:6a4db94011d3 38 const uint32_t ll_pin_defines[16] = {
sahilmgandhi 18:6a4db94011d3 39 LL_GPIO_PIN_0,
sahilmgandhi 18:6a4db94011d3 40 LL_GPIO_PIN_1,
sahilmgandhi 18:6a4db94011d3 41 LL_GPIO_PIN_2,
sahilmgandhi 18:6a4db94011d3 42 LL_GPIO_PIN_3,
sahilmgandhi 18:6a4db94011d3 43 LL_GPIO_PIN_4,
sahilmgandhi 18:6a4db94011d3 44 LL_GPIO_PIN_5,
sahilmgandhi 18:6a4db94011d3 45 LL_GPIO_PIN_6,
sahilmgandhi 18:6a4db94011d3 46 LL_GPIO_PIN_7,
sahilmgandhi 18:6a4db94011d3 47 LL_GPIO_PIN_8,
sahilmgandhi 18:6a4db94011d3 48 LL_GPIO_PIN_9,
sahilmgandhi 18:6a4db94011d3 49 LL_GPIO_PIN_10,
sahilmgandhi 18:6a4db94011d3 50 LL_GPIO_PIN_11,
sahilmgandhi 18:6a4db94011d3 51 LL_GPIO_PIN_12,
sahilmgandhi 18:6a4db94011d3 52 LL_GPIO_PIN_13,
sahilmgandhi 18:6a4db94011d3 53 LL_GPIO_PIN_14,
sahilmgandhi 18:6a4db94011d3 54 LL_GPIO_PIN_15
sahilmgandhi 18:6a4db94011d3 55 };
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 /**
sahilmgandhi 18:6a4db94011d3 58 * Configure pin (mode, speed, output type and pull-up/pull-down)
sahilmgandhi 18:6a4db94011d3 59 */
sahilmgandhi 18:6a4db94011d3 60 void pin_function(PinName pin, int data)
sahilmgandhi 18:6a4db94011d3 61 {
sahilmgandhi 18:6a4db94011d3 62 MBED_ASSERT(pin != (PinName)NC);
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 // Get the pin informations
sahilmgandhi 18:6a4db94011d3 65 uint32_t mode = STM_PIN_FUNCTION(data);
sahilmgandhi 18:6a4db94011d3 66 uint32_t afnum = STM_PIN_AFNUM(data);
sahilmgandhi 18:6a4db94011d3 67 uint32_t port = STM_PORT(pin);
sahilmgandhi 18:6a4db94011d3 68 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
sahilmgandhi 18:6a4db94011d3 69 uint32_t ll_mode = 0;
sahilmgandhi 18:6a4db94011d3 70
sahilmgandhi 18:6a4db94011d3 71 // Enable GPIO clock
sahilmgandhi 18:6a4db94011d3 72 GPIO_TypeDef *gpio = Set_GPIO_Clock(port);
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 /* Set default speed to high.
sahilmgandhi 18:6a4db94011d3 75 * This is done before other settings on purpose:
sahilmgandhi 18:6a4db94011d3 76 * For most families there are dedicated registers so it is
sahilmgandhi 18:6a4db94011d3 77 * not so important, register can be set at any time.
sahilmgandhi 18:6a4db94011d3 78 * But for families like F1, speed only applies to output. so we set
sahilmgandhi 18:6a4db94011d3 79 * it here, and then if input is selected, this setting might be
sahilmgandhi 18:6a4db94011d3 80 * overriden by the input one.
sahilmgandhi 18:6a4db94011d3 81 */
sahilmgandhi 18:6a4db94011d3 82 LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
sahilmgandhi 18:6a4db94011d3 83
sahilmgandhi 18:6a4db94011d3 84 switch (mode) {
sahilmgandhi 18:6a4db94011d3 85 case STM_PIN_INPUT:
sahilmgandhi 18:6a4db94011d3 86 ll_mode = LL_GPIO_MODE_INPUT;
sahilmgandhi 18:6a4db94011d3 87 break;
sahilmgandhi 18:6a4db94011d3 88 case STM_PIN_OUTPUT:
sahilmgandhi 18:6a4db94011d3 89 ll_mode = LL_GPIO_MODE_OUTPUT;
sahilmgandhi 18:6a4db94011d3 90 break;
sahilmgandhi 18:6a4db94011d3 91 case STM_PIN_ALTERNATE:
sahilmgandhi 18:6a4db94011d3 92 ll_mode = LL_GPIO_MODE_ALTERNATE;
sahilmgandhi 18:6a4db94011d3 93 // In case of ALT function, also set he afnum
sahilmgandhi 18:6a4db94011d3 94 stm_pin_SetAFPin(gpio, pin, afnum);
sahilmgandhi 18:6a4db94011d3 95 break;
sahilmgandhi 18:6a4db94011d3 96 case STM_PIN_ANALOG:
sahilmgandhi 18:6a4db94011d3 97 ll_mode = LL_GPIO_MODE_ANALOG;
sahilmgandhi 18:6a4db94011d3 98 break;
sahilmgandhi 18:6a4db94011d3 99 default:
sahilmgandhi 18:6a4db94011d3 100 MBED_ASSERT(0);
sahilmgandhi 18:6a4db94011d3 101 break;
sahilmgandhi 18:6a4db94011d3 102 }
sahilmgandhi 18:6a4db94011d3 103 LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
sahilmgandhi 18:6a4db94011d3 104
sahilmgandhi 18:6a4db94011d3 105 #if defined(GPIO_ASCR_ASC0)
sahilmgandhi 18:6a4db94011d3 106 /* For families where Analog Control ASC0 register is present */
sahilmgandhi 18:6a4db94011d3 107 if (STM_PIN_ANALOG_CONTROL(data)) {
sahilmgandhi 18:6a4db94011d3 108 LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
sahilmgandhi 18:6a4db94011d3 109 } else {
sahilmgandhi 18:6a4db94011d3 110 LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
sahilmgandhi 18:6a4db94011d3 111 }
sahilmgandhi 18:6a4db94011d3 112 #endif
sahilmgandhi 18:6a4db94011d3 113
sahilmgandhi 18:6a4db94011d3 114 /* For now by default use Speed HIGH for output or alt modes */
sahilmgandhi 18:6a4db94011d3 115 if ((mode == STM_PIN_OUTPUT) ||(mode == STM_PIN_ALTERNATE)) {
sahilmgandhi 18:6a4db94011d3 116 if (STM_PIN_OD(data)) {
sahilmgandhi 18:6a4db94011d3 117 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
sahilmgandhi 18:6a4db94011d3 118 } else {
sahilmgandhi 18:6a4db94011d3 119 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
sahilmgandhi 18:6a4db94011d3 120 }
sahilmgandhi 18:6a4db94011d3 121 }
sahilmgandhi 18:6a4db94011d3 122
sahilmgandhi 18:6a4db94011d3 123 stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
sahilmgandhi 18:6a4db94011d3 124
sahilmgandhi 18:6a4db94011d3 125 stm_pin_DisconnectDebug(pin);
sahilmgandhi 18:6a4db94011d3 126 }
sahilmgandhi 18:6a4db94011d3 127
sahilmgandhi 18:6a4db94011d3 128 /**
sahilmgandhi 18:6a4db94011d3 129 * Configure pin pull-up/pull-down
sahilmgandhi 18:6a4db94011d3 130 */
sahilmgandhi 18:6a4db94011d3 131 void pin_mode(PinName pin, PinMode mode)
sahilmgandhi 18:6a4db94011d3 132 {
sahilmgandhi 18:6a4db94011d3 133 MBED_ASSERT(pin != (PinName)NC);
sahilmgandhi 18:6a4db94011d3 134
sahilmgandhi 18:6a4db94011d3 135 uint32_t port_index = STM_PORT(pin);
sahilmgandhi 18:6a4db94011d3 136 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
sahilmgandhi 18:6a4db94011d3 137 // Enable GPIO clock
sahilmgandhi 18:6a4db94011d3 138 GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
sahilmgandhi 18:6a4db94011d3 139 uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
sahilmgandhi 18:6a4db94011d3 140
sahilmgandhi 18:6a4db94011d3 141 if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE))
sahilmgandhi 18:6a4db94011d3 142 {
sahilmgandhi 18:6a4db94011d3 143 if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
sahilmgandhi 18:6a4db94011d3 144 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
sahilmgandhi 18:6a4db94011d3 145 } else {
sahilmgandhi 18:6a4db94011d3 146 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
sahilmgandhi 18:6a4db94011d3 147 }
sahilmgandhi 18:6a4db94011d3 148 }
sahilmgandhi 18:6a4db94011d3 149
sahilmgandhi 18:6a4db94011d3 150 if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
sahilmgandhi 18:6a4db94011d3 151 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
sahilmgandhi 18:6a4db94011d3 152 } else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
sahilmgandhi 18:6a4db94011d3 153 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
sahilmgandhi 18:6a4db94011d3 154 } else {
sahilmgandhi 18:6a4db94011d3 155 stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
sahilmgandhi 18:6a4db94011d3 156 }
sahilmgandhi 18:6a4db94011d3 157 }