Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_MPS2/SDK/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 "mps2_ethernet_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "mbed_interface.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "mbed_toolchain.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | #include "ETH_MPS2.h" |
sahilmgandhi | 18:6a4db94011d3 | 24 | #include "mbed_wait_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 25 | |
sahilmgandhi | 18:6a4db94011d3 | 26 | #define TX_PKT_SIZE 256 |
sahilmgandhi | 18:6a4db94011d3 | 27 | #define RX_PKT_SIZE 300 |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | // Types |
sahilmgandhi | 18:6a4db94011d3 | 30 | #undef FALSE |
sahilmgandhi | 18:6a4db94011d3 | 31 | #undef TRUE |
sahilmgandhi | 18:6a4db94011d3 | 32 | #define FALSE 0 |
sahilmgandhi | 18:6a4db94011d3 | 33 | #define TRUE 1 |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | |
sahilmgandhi | 18:6a4db94011d3 | 36 | int smsc9220_check_id(void) |
sahilmgandhi | 18:6a4db94011d3 | 37 | { |
sahilmgandhi | 18:6a4db94011d3 | 38 | int error; |
sahilmgandhi | 18:6a4db94011d3 | 39 | unsigned int id; |
sahilmgandhi | 18:6a4db94011d3 | 40 | error = 0; |
sahilmgandhi | 18:6a4db94011d3 | 41 | |
sahilmgandhi | 18:6a4db94011d3 | 42 | id = smsc9220_read_id(); |
sahilmgandhi | 18:6a4db94011d3 | 43 | |
sahilmgandhi | 18:6a4db94011d3 | 44 | // If bottom and top halves of the word are the same |
sahilmgandhi | 18:6a4db94011d3 | 45 | if(((id >> 16) & 0xFFFF) == (id & 0xFFFF)) { |
sahilmgandhi | 18:6a4db94011d3 | 46 | error = 1; |
sahilmgandhi | 18:6a4db94011d3 | 47 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 48 | } |
sahilmgandhi | 18:6a4db94011d3 | 49 | switch(((id >> 16) & 0xFFFF)) { |
sahilmgandhi | 18:6a4db94011d3 | 50 | case 0x9220: |
sahilmgandhi | 18:6a4db94011d3 | 51 | break; |
sahilmgandhi | 18:6a4db94011d3 | 52 | |
sahilmgandhi | 18:6a4db94011d3 | 53 | default: |
sahilmgandhi | 18:6a4db94011d3 | 54 | error = 1; |
sahilmgandhi | 18:6a4db94011d3 | 55 | break; |
sahilmgandhi | 18:6a4db94011d3 | 56 | } |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 59 | } |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | int smsc9220_check_macaddress(void) |
sahilmgandhi | 18:6a4db94011d3 | 62 | { |
sahilmgandhi | 18:6a4db94011d3 | 63 | int error; |
sahilmgandhi | 18:6a4db94011d3 | 64 | const unsigned int mac_valid_high = 0xC00A; |
sahilmgandhi | 18:6a4db94011d3 | 65 | const unsigned int mac_valid_low = 0x00F70200; |
sahilmgandhi | 18:6a4db94011d3 | 66 | unsigned int mac_low; |
sahilmgandhi | 18:6a4db94011d3 | 67 | unsigned int mac_high; |
sahilmgandhi | 18:6a4db94011d3 | 68 | |
sahilmgandhi | 18:6a4db94011d3 | 69 | error = 0; |
sahilmgandhi | 18:6a4db94011d3 | 70 | |
sahilmgandhi | 18:6a4db94011d3 | 71 | // Read current mac address. |
sahilmgandhi | 18:6a4db94011d3 | 72 | smsc9220_mac_regread(SMSC9220_MAC_ADDRH, &mac_high); |
sahilmgandhi | 18:6a4db94011d3 | 73 | smsc9220_mac_regread(SMSC9220_MAC_ADDRL, &mac_low); |
sahilmgandhi | 18:6a4db94011d3 | 74 | |
sahilmgandhi | 18:6a4db94011d3 | 75 | // Writing temporary address: |
sahilmgandhi | 18:6a4db94011d3 | 76 | smsc9220_mac_regwrite(SMSC9220_MAC_ADDRH, mac_valid_high); |
sahilmgandhi | 18:6a4db94011d3 | 77 | smsc9220_mac_regwrite(SMSC9220_MAC_ADDRL, mac_valid_low); |
sahilmgandhi | 18:6a4db94011d3 | 78 | |
sahilmgandhi | 18:6a4db94011d3 | 79 | // Verify write was correct: |
sahilmgandhi | 18:6a4db94011d3 | 80 | smsc9220_mac_regread(SMSC9220_MAC_ADDRH, &mac_high); |
sahilmgandhi | 18:6a4db94011d3 | 81 | smsc9220_mac_regread(SMSC9220_MAC_ADDRL, &mac_low); |
sahilmgandhi | 18:6a4db94011d3 | 82 | |
sahilmgandhi | 18:6a4db94011d3 | 83 | |
sahilmgandhi | 18:6a4db94011d3 | 84 | if(mac_high != mac_valid_high || mac_low != mac_valid_low) { |
sahilmgandhi | 18:6a4db94011d3 | 85 | error = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 86 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 87 | } |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | return error; |
sahilmgandhi | 18:6a4db94011d3 | 90 | } |
sahilmgandhi | 18:6a4db94011d3 | 91 | |
sahilmgandhi | 18:6a4db94011d3 | 92 | void smsc9220_print_mac_registers() |
sahilmgandhi | 18:6a4db94011d3 | 93 | { |
sahilmgandhi | 18:6a4db94011d3 | 94 | unsigned int read; |
sahilmgandhi | 18:6a4db94011d3 | 95 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 96 | |
sahilmgandhi | 18:6a4db94011d3 | 97 | i = 0; |
sahilmgandhi | 18:6a4db94011d3 | 98 | read = 0; |
sahilmgandhi | 18:6a4db94011d3 | 99 | |
sahilmgandhi | 18:6a4db94011d3 | 100 | for(i = 1; i <= 0xC; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 101 | smsc9220_mac_regread(i, &read); |
sahilmgandhi | 18:6a4db94011d3 | 102 | } |
sahilmgandhi | 18:6a4db94011d3 | 103 | return; |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | |
sahilmgandhi | 18:6a4db94011d3 | 106 | |
sahilmgandhi | 18:6a4db94011d3 | 107 | void smsc9220_print_phy_registers() |
sahilmgandhi | 18:6a4db94011d3 | 108 | { |
sahilmgandhi | 18:6a4db94011d3 | 109 | unsigned short read; |
sahilmgandhi | 18:6a4db94011d3 | 110 | unsigned int i; |
sahilmgandhi | 18:6a4db94011d3 | 111 | |
sahilmgandhi | 18:6a4db94011d3 | 112 | i = 0; |
sahilmgandhi | 18:6a4db94011d3 | 113 | read = 0; |
sahilmgandhi | 18:6a4db94011d3 | 114 | for(i = 0; i <= 6; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 115 | smsc9220_phy_regread(i, &read); |
sahilmgandhi | 18:6a4db94011d3 | 116 | } |
sahilmgandhi | 18:6a4db94011d3 | 117 | smsc9220_phy_regread(i = 17, &read); |
sahilmgandhi | 18:6a4db94011d3 | 118 | |
sahilmgandhi | 18:6a4db94011d3 | 119 | smsc9220_phy_regread(i = 18, &read); |
sahilmgandhi | 18:6a4db94011d3 | 120 | |
sahilmgandhi | 18:6a4db94011d3 | 121 | smsc9220_phy_regread(i = 27, &read); |
sahilmgandhi | 18:6a4db94011d3 | 122 | |
sahilmgandhi | 18:6a4db94011d3 | 123 | smsc9220_phy_regread(i = 29, &read); |
sahilmgandhi | 18:6a4db94011d3 | 124 | |
sahilmgandhi | 18:6a4db94011d3 | 125 | smsc9220_phy_regread(i = 30, &read); |
sahilmgandhi | 18:6a4db94011d3 | 126 | |
sahilmgandhi | 18:6a4db94011d3 | 127 | smsc9220_phy_regread(i = 31, &read); |
sahilmgandhi | 18:6a4db94011d3 | 128 | |
sahilmgandhi | 18:6a4db94011d3 | 129 | return; |
sahilmgandhi | 18:6a4db94011d3 | 130 | } |
sahilmgandhi | 18:6a4db94011d3 | 131 | |
sahilmgandhi | 18:6a4db94011d3 | 132 | /*---------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 133 | Ethernet Device initialize |
sahilmgandhi | 18:6a4db94011d3 | 134 | *----------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 135 | |
sahilmgandhi | 18:6a4db94011d3 | 136 | int ethernet_transmission(unsigned char * pkt, unsigned int length) |
sahilmgandhi | 18:6a4db94011d3 | 137 | { |
sahilmgandhi | 18:6a4db94011d3 | 138 | smsc9220_xmit_packet(pkt, length); |
sahilmgandhi | 18:6a4db94011d3 | 139 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 140 | } |
sahilmgandhi | 18:6a4db94011d3 | 141 | |
sahilmgandhi | 18:6a4db94011d3 | 142 | int ethernet_reception(unsigned int *recvbuf, unsigned int *index) |
sahilmgandhi | 18:6a4db94011d3 | 143 | { |
sahilmgandhi | 18:6a4db94011d3 | 144 | return smsc9220_recv_packet((unsigned int *)recvbuf, index); |
sahilmgandhi | 18:6a4db94011d3 | 145 | } |
sahilmgandhi | 18:6a4db94011d3 | 146 | |
sahilmgandhi | 18:6a4db94011d3 | 147 | int ethernet_mac_address(char *mac) |
sahilmgandhi | 18:6a4db94011d3 | 148 | { |
sahilmgandhi | 18:6a4db94011d3 | 149 | return smsc9220_check_macaddress(); |
sahilmgandhi | 18:6a4db94011d3 | 150 | } |
sahilmgandhi | 18:6a4db94011d3 | 151 | |
sahilmgandhi | 18:6a4db94011d3 | 152 | unsigned int ethernet_check_ready(void) |
sahilmgandhi | 18:6a4db94011d3 | 153 | { |
sahilmgandhi | 18:6a4db94011d3 | 154 | return smsc9220_check_ready(); |
sahilmgandhi | 18:6a4db94011d3 | 155 | } |
sahilmgandhi | 18:6a4db94011d3 | 156 | |
sahilmgandhi | 18:6a4db94011d3 | 157 | unsigned int ethernet_intf() |
sahilmgandhi | 18:6a4db94011d3 | 158 | { |
sahilmgandhi | 18:6a4db94011d3 | 159 | unsigned int txfifo_inf; |
sahilmgandhi | 18:6a4db94011d3 | 160 | |
sahilmgandhi | 18:6a4db94011d3 | 161 | txfifo_inf = SMSC9220->TX_FIFO_INF; |
sahilmgandhi | 18:6a4db94011d3 | 162 | |
sahilmgandhi | 18:6a4db94011d3 | 163 | return txfifo_inf; |
sahilmgandhi | 18:6a4db94011d3 | 164 | |
sahilmgandhi | 18:6a4db94011d3 | 165 | } |
sahilmgandhi | 18:6a4db94011d3 | 166 |