Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_MPS2/ethernet_api.c@46:b156ef445742, 2017-06-03 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2015 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 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 17 | |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "ethernet_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "mps2_ethernet_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "mbed_interface.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "mbed_toolchain.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 24 | #include "ETH_MPS2.h" |
sahilmgandhi | 18:6a4db94011d3 | 25 | #include "mbed_wait_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | #define TX_PKT_SIZE 256 |
sahilmgandhi | 18:6a4db94011d3 | 28 | #define RX_PKT_SIZE 300 |
sahilmgandhi | 18:6a4db94011d3 | 29 | |
sahilmgandhi | 18:6a4db94011d3 | 30 | // Types |
sahilmgandhi | 18:6a4db94011d3 | 31 | #undef FALSE |
sahilmgandhi | 18:6a4db94011d3 | 32 | #undef TRUE |
sahilmgandhi | 18:6a4db94011d3 | 33 | #define FALSE 0 |
sahilmgandhi | 18:6a4db94011d3 | 34 | #define TRUE 1 |
sahilmgandhi | 18:6a4db94011d3 | 35 | |
sahilmgandhi | 18:6a4db94011d3 | 36 | /*---------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 37 | Ethernet Device initialize |
sahilmgandhi | 18:6a4db94011d3 | 38 | *----------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 39 | int ethernet_init() |
sahilmgandhi | 18:6a4db94011d3 | 40 | { |
sahilmgandhi | 18:6a4db94011d3 | 41 | int error; |
sahilmgandhi | 18:6a4db94011d3 | 42 | error = 0; |
sahilmgandhi | 18:6a4db94011d3 | 43 | |
sahilmgandhi | 18:6a4db94011d3 | 44 | if(smsc9220_check_id()) { |
sahilmgandhi | 18:6a4db94011d3 | 45 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 46 | } |
sahilmgandhi | 18:6a4db94011d3 | 47 | |
sahilmgandhi | 18:6a4db94011d3 | 48 | if(smsc9220_soft_reset()) { |
sahilmgandhi | 18:6a4db94011d3 | 49 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 50 | } |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | smsc9220_set_txfifo(5); |
sahilmgandhi | 18:6a4db94011d3 | 53 | |
sahilmgandhi | 18:6a4db94011d3 | 54 | // Sets automatic flow control thresholds, and backpressure |
sahilmgandhi | 18:6a4db94011d3 | 55 | // threshold to defaults specified. |
sahilmgandhi | 18:6a4db94011d3 | 56 | SMSC9220->AFC_CFG = 0x006E3740; |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | if(smsc9220_wait_eeprom()) { |
sahilmgandhi | 18:6a4db94011d3 | 59 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 60 | } |
sahilmgandhi | 18:6a4db94011d3 | 61 | |
sahilmgandhi | 18:6a4db94011d3 | 62 | // Configure GPIOs as LED outputs. |
sahilmgandhi | 18:6a4db94011d3 | 63 | SMSC9220->GPIO_CFG = 0x70070000; |
sahilmgandhi | 18:6a4db94011d3 | 64 | |
sahilmgandhi | 18:6a4db94011d3 | 65 | smsc9220_init_irqs(); |
sahilmgandhi | 18:6a4db94011d3 | 66 | |
sahilmgandhi | 18:6a4db94011d3 | 67 | /* Configure MAC addresses here if needed. */ |
sahilmgandhi | 18:6a4db94011d3 | 68 | |
sahilmgandhi | 18:6a4db94011d3 | 69 | if(smsc9220_check_phy()) { |
sahilmgandhi | 18:6a4db94011d3 | 70 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 71 | } |
sahilmgandhi | 18:6a4db94011d3 | 72 | |
sahilmgandhi | 18:6a4db94011d3 | 73 | if(smsc9220_reset_phy()) { |
sahilmgandhi | 18:6a4db94011d3 | 74 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 75 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 76 | } |
sahilmgandhi | 18:6a4db94011d3 | 77 | |
sahilmgandhi | 18:6a4db94011d3 | 78 | wait_ms(100); |
sahilmgandhi | 18:6a4db94011d3 | 79 | // Checking whether phy reset completed successfully. |
sahilmgandhi | 18:6a4db94011d3 | 80 | { |
sahilmgandhi | 18:6a4db94011d3 | 81 | unsigned short phyreset; |
sahilmgandhi | 18:6a4db94011d3 | 82 | phyreset = 0; |
sahilmgandhi | 18:6a4db94011d3 | 83 | smsc9220_phy_regread(SMSC9220_PHY_BCONTROL, &phyreset); |
sahilmgandhi | 18:6a4db94011d3 | 84 | if(phyreset & (1 << 15)) { |
sahilmgandhi | 18:6a4db94011d3 | 85 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 86 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 87 | } |
sahilmgandhi | 18:6a4db94011d3 | 88 | } |
sahilmgandhi | 18:6a4db94011d3 | 89 | |
sahilmgandhi | 18:6a4db94011d3 | 90 | /* Advertise capabilities */ |
sahilmgandhi | 18:6a4db94011d3 | 91 | smsc9220_advertise_cap(); |
sahilmgandhi | 18:6a4db94011d3 | 92 | |
sahilmgandhi | 18:6a4db94011d3 | 93 | |
sahilmgandhi | 18:6a4db94011d3 | 94 | /* Begin to establish link */ |
sahilmgandhi | 18:6a4db94011d3 | 95 | smsc9220_establish_link(); // bit [12] of BCONTROL seems self-clearing. |
sahilmgandhi | 18:6a4db94011d3 | 96 | // Although it's not so in the manual. |
sahilmgandhi | 18:6a4db94011d3 | 97 | |
sahilmgandhi | 18:6a4db94011d3 | 98 | /* Interrupt threshold */ |
sahilmgandhi | 18:6a4db94011d3 | 99 | SMSC9220->FIFO_INT = 0xFF000000; |
sahilmgandhi | 18:6a4db94011d3 | 100 | |
sahilmgandhi | 18:6a4db94011d3 | 101 | smsc9220_enable_mac_xmit(); |
sahilmgandhi | 18:6a4db94011d3 | 102 | |
sahilmgandhi | 18:6a4db94011d3 | 103 | smsc9220_enable_xmit(); |
sahilmgandhi | 18:6a4db94011d3 | 104 | |
sahilmgandhi | 18:6a4db94011d3 | 105 | SMSC9220->RX_CFG = 0; |
sahilmgandhi | 18:6a4db94011d3 | 106 | |
sahilmgandhi | 18:6a4db94011d3 | 107 | smsc9220_enable_mac_recv(); |
sahilmgandhi | 18:6a4db94011d3 | 108 | |
sahilmgandhi | 18:6a4db94011d3 | 109 | // Rx status FIFO level irq threshold |
sahilmgandhi | 18:6a4db94011d3 | 110 | SMSC9220->FIFO_INT &= ~(0xFF); // Clear 2 bottom nibbles |
sahilmgandhi | 18:6a4db94011d3 | 111 | |
sahilmgandhi | 18:6a4db94011d3 | 112 | // This sleep is compulsory otherwise txmit/receive will fail. |
sahilmgandhi | 18:6a4db94011d3 | 113 | wait_ms(2000); |
sahilmgandhi | 18:6a4db94011d3 | 114 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 115 | |
sahilmgandhi | 18:6a4db94011d3 | 116 | } |
sahilmgandhi | 18:6a4db94011d3 | 117 | |
sahilmgandhi | 18:6a4db94011d3 | 118 | /*---------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 119 | Ethernet Device Uninitialize |
sahilmgandhi | 18:6a4db94011d3 | 120 | *----------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 121 | void ethernet_free() { |
sahilmgandhi | 18:6a4db94011d3 | 122 | } |
sahilmgandhi | 18:6a4db94011d3 | 123 | |
sahilmgandhi | 18:6a4db94011d3 | 124 | int ethernet_write(const char *data, int size) |
sahilmgandhi | 18:6a4db94011d3 | 125 | { |
sahilmgandhi | 18:6a4db94011d3 | 126 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 127 | } |
sahilmgandhi | 18:6a4db94011d3 | 128 | |
sahilmgandhi | 18:6a4db94011d3 | 129 | int ethernet_send() |
sahilmgandhi | 18:6a4db94011d3 | 130 | { |
sahilmgandhi | 18:6a4db94011d3 | 131 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 132 | } |
sahilmgandhi | 18:6a4db94011d3 | 133 | |
sahilmgandhi | 18:6a4db94011d3 | 134 | int ethernet_receive() |
sahilmgandhi | 18:6a4db94011d3 | 135 | { |
sahilmgandhi | 18:6a4db94011d3 | 136 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 137 | } |
sahilmgandhi | 18:6a4db94011d3 | 138 | |
sahilmgandhi | 18:6a4db94011d3 | 139 | // Read from an recevied ethernet packet. |
sahilmgandhi | 18:6a4db94011d3 | 140 | // After receive returnd a number bigger than 0 it is |
sahilmgandhi | 18:6a4db94011d3 | 141 | // possible to read bytes from this packet. |
sahilmgandhi | 18:6a4db94011d3 | 142 | // Read will write up to size bytes into data. |
sahilmgandhi | 18:6a4db94011d3 | 143 | // It is possible to use read multible times. |
sahilmgandhi | 18:6a4db94011d3 | 144 | // Each time read will start reading after the last read byte before. |
sahilmgandhi | 18:6a4db94011d3 | 145 | |
sahilmgandhi | 18:6a4db94011d3 | 146 | int ethernet_read(char *data, int dlen) |
sahilmgandhi | 18:6a4db94011d3 | 147 | { |
sahilmgandhi | 18:6a4db94011d3 | 148 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 149 | } |
sahilmgandhi | 18:6a4db94011d3 | 150 | |
sahilmgandhi | 18:6a4db94011d3 | 151 | void ethernet_address(char *mac) { |
sahilmgandhi | 18:6a4db94011d3 | 152 | mbed_mac_address(mac); |
sahilmgandhi | 18:6a4db94011d3 | 153 | } |
sahilmgandhi | 18:6a4db94011d3 | 154 | |
sahilmgandhi | 18:6a4db94011d3 | 155 | int ethernet_link(void) |
sahilmgandhi | 18:6a4db94011d3 | 156 | { |
sahilmgandhi | 18:6a4db94011d3 | 157 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 158 | } |
sahilmgandhi | 18:6a4db94011d3 | 159 | |
sahilmgandhi | 18:6a4db94011d3 | 160 | void ethernet_set_link(int speed, int duplex) |
sahilmgandhi | 18:6a4db94011d3 | 161 | { |
sahilmgandhi | 18:6a4db94011d3 | 162 | smsc9220_establish_link(); |
sahilmgandhi | 18:6a4db94011d3 | 163 | } |
sahilmgandhi | 18:6a4db94011d3 | 164 |