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 * Copyright (c) 2006-2013 ARM Limited
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 5 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 6 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 13 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 14 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 15 */
sahilmgandhi 18:6a4db94011d3 16
sahilmgandhi 18:6a4db94011d3 17 #include "port_api.h"
sahilmgandhi 18:6a4db94011d3 18 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 19 #include "gpio_api.h"
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 // LPC114 IOCON offset table [port][pin]
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23 static uint8_t iocon_offset[4][12] = {
sahilmgandhi 18:6a4db94011d3 24 {0x0c,0x10,0x1c,0x2c,0x30,0x34,0x4c,0x50,0x60,0x64,0x68,0x74}, // PORT 0
sahilmgandhi 18:6a4db94011d3 25 {0x78,0x7c,0x80,0x90,0x94,0xa0,0xa4,0xa8,0x14,0x38,0x6c,0x98}, // PORT 1
sahilmgandhi 18:6a4db94011d3 26 {0x08,0x28,0x5c,0x8c,0x40,0x44,0x00,0x20,0x24,0x54,0x58,0x70}, // PORT 2
sahilmgandhi 18:6a4db94011d3 27 {0x84,0x88,0x9c,0xac,0x3c,0x48} // PORT 3
sahilmgandhi 18:6a4db94011d3 28 };
sahilmgandhi 18:6a4db94011d3 29
sahilmgandhi 18:6a4db94011d3 30 PinName port_pin(PortName port, int pin) {
sahilmgandhi 18:6a4db94011d3 31 return (PinName)((port << PORT_SHIFT) | (pin << PIN_SHIFT) | (uint32_t)iocon_offset[port][pin]);
sahilmgandhi 18:6a4db94011d3 32 }
sahilmgandhi 18:6a4db94011d3 33
sahilmgandhi 18:6a4db94011d3 34 void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
sahilmgandhi 18:6a4db94011d3 35 obj->port = port;
sahilmgandhi 18:6a4db94011d3 36 obj->mask = mask;
sahilmgandhi 18:6a4db94011d3 37
sahilmgandhi 18:6a4db94011d3 38 LPC_GPIO_TypeDef *port_reg = ((LPC_GPIO_TypeDef *) (LPC_GPIO0_BASE + (port * 0x10000)));
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 obj->reg_data = &port_reg->DATA;
sahilmgandhi 18:6a4db94011d3 41 obj->reg_dir = &port_reg->DIR;
sahilmgandhi 18:6a4db94011d3 42
sahilmgandhi 18:6a4db94011d3 43 uint32_t i;
sahilmgandhi 18:6a4db94011d3 44 // The function is set per pin: reuse gpio logic
sahilmgandhi 18:6a4db94011d3 45 for (i=0; i<12; i++) {
sahilmgandhi 18:6a4db94011d3 46 if (obj->mask & (1<<i)) {
sahilmgandhi 18:6a4db94011d3 47 gpio_set(port_pin(obj->port, i));
sahilmgandhi 18:6a4db94011d3 48 }
sahilmgandhi 18:6a4db94011d3 49 }
sahilmgandhi 18:6a4db94011d3 50
sahilmgandhi 18:6a4db94011d3 51 port_dir(obj, dir);
sahilmgandhi 18:6a4db94011d3 52 }
sahilmgandhi 18:6a4db94011d3 53
sahilmgandhi 18:6a4db94011d3 54 void port_mode(port_t *obj, PinMode mode) {
sahilmgandhi 18:6a4db94011d3 55 uint32_t i;
sahilmgandhi 18:6a4db94011d3 56 // The mode is set per pin: reuse pinmap logic
sahilmgandhi 18:6a4db94011d3 57 for (i=0; i<12; i++) {
sahilmgandhi 18:6a4db94011d3 58 if (obj->mask & (1<<i)) {
sahilmgandhi 18:6a4db94011d3 59 pin_mode(port_pin(obj->port, i), mode);
sahilmgandhi 18:6a4db94011d3 60 }
sahilmgandhi 18:6a4db94011d3 61 }
sahilmgandhi 18:6a4db94011d3 62 }
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 void port_dir(port_t *obj, PinDirection dir) {
sahilmgandhi 18:6a4db94011d3 65 switch (dir) {
sahilmgandhi 18:6a4db94011d3 66 case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break;
sahilmgandhi 18:6a4db94011d3 67 case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break;
sahilmgandhi 18:6a4db94011d3 68 }
sahilmgandhi 18:6a4db94011d3 69 }
sahilmgandhi 18:6a4db94011d3 70
sahilmgandhi 18:6a4db94011d3 71 void port_write(port_t *obj, int value) {
sahilmgandhi 18:6a4db94011d3 72 *obj->reg_data = (value & obj->mask);
sahilmgandhi 18:6a4db94011d3 73 }
sahilmgandhi 18:6a4db94011d3 74
sahilmgandhi 18:6a4db94011d3 75 int port_read(port_t *obj) {
sahilmgandhi 18:6a4db94011d3 76 return (*obj->reg_data & obj->mask);
sahilmgandhi 18:6a4db94011d3 77 }
sahilmgandhi 18:6a4db94011d3 78