Katarzyna Kopel / Mbed 2 deprecated FRDM_K64F-Ethernet_PHY

Dependencies:   mbed mbed-rtos EthernetInterface

Committer:
kopelka
Date:
Wed Jan 16 01:12:28 2019 +0000
Revision:
3:76e748d8d250
Parent:
2:e30076def8a7
cable test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
issaiass 0:bbc9cfdee3bc 1 #include "mbed.h"
issaiass 0:bbc9cfdee3bc 2 #include "EthernetInterface.h"
kopelka 2:e30076def8a7 3 #include "nowy_brzydki_plik.h"
kopelka 1:3c09c9c0324e 4
issaiass 0:bbc9cfdee3bc 5
issaiass 0:bbc9cfdee3bc 6 #define MBED_DEV_IP "192.168.0.52"
issaiass 0:bbc9cfdee3bc 7 #define MBED_DEV_MASK "255.255.255.0"
issaiass 0:bbc9cfdee3bc 8 #define MBED_DEV_GW "0.0.0.0"
issaiass 0:bbc9cfdee3bc 9 #define ECHO_SERVER_PORT 5000
issaiass 0:bbc9cfdee3bc 10
kopelka 1:3c09c9c0324e 11
kopelka 1:3c09c9c0324e 12 Serial s(USBTX, USBRX);
kopelka 1:3c09c9c0324e 13 volatile char c = '\0'; // Initialized to the NULL character
kopelka 1:3c09c9c0324e 14 uint32_t data;
kopelka 1:3c09c9c0324e 15
kopelka 1:3c09c9c0324e 16 /*! @brief Defines the PHY register.*/
kopelka 1:3c09c9c0324e 17 typedef enum _enet_phy_register
kopelka 1:3c09c9c0324e 18 {
kopelka 1:3c09c9c0324e 19 kEnetPhyCR = 0, /*!< PHY control register */
kopelka 1:3c09c9c0324e 20 kEnetPhySR = 1, /*!< PHY status register*/
kopelka 1:3c09c9c0324e 21 kEnetPhyId1 = 2, /*!< PHY identification register 1*/
kopelka 1:3c09c9c0324e 22 kEnetPhyId2 = 3, /*!< PHY identification register 2*/
kopelka 1:3c09c9c0324e 23 kEnetPhyCt2 = 0x1e /*!< PHY control2 register*/
kopelka 1:3c09c9c0324e 24 } enet_phy_register_t;
kopelka 1:3c09c9c0324e 25 /*! @brief Defines the PHY status.*/
kopelka 1:3c09c9c0324e 26 typedef enum _enet_phy_status
kopelka 1:3c09c9c0324e 27 {
kopelka 1:3c09c9c0324e 28 kEnetPhyLinkStatus = 0x4, /*!< ENET PHY link status bit*/
kopelka 1:3c09c9c0324e 29 kEnetPhyAutoNegAble = 0x08, /*!< ENET PHY auto negotiation ability*/
kopelka 1:3c09c9c0324e 30 kEnetPhyAutoNegComplete = 0x20, /*!< ENET PHY auto negotiation complete*/
kopelka 1:3c09c9c0324e 31 kEnetPhySpeedDulpexMask = 0x07 /*!< ENET PHY speed mask on status register 2*/
kopelka 1:3c09c9c0324e 32 } enet_phy_status_t;
kopelka 1:3c09c9c0324e 33 /*! @brief Defines the control flag.*/
kopelka 1:3c09c9c0324e 34 typedef enum _enet_phy_control
kopelka 1:3c09c9c0324e 35 {
kopelka 1:3c09c9c0324e 36 kEnetPhyAutoNeg = 0x1000,/*!< ENET PHY auto negotiation control*/
kopelka 1:3c09c9c0324e 37 kEnetPhySpeed = 0x2000, /*! ENET PHY speed control*/
kopelka 1:3c09c9c0324e 38 kEnetPhyLoop = 0x4000, /*!< ENET PHY loop control*/
kopelka 1:3c09c9c0324e 39 kEnetPhyReset = 0x8000, /*!< ENET PHY reset control*/
kopelka 1:3c09c9c0324e 40 kEnetPhy10HalfDuplex = 0x01, /*!< ENET PHY 10 M half duplex*/
kopelka 1:3c09c9c0324e 41 kEnetPhy100HalfDuplex = 0x02,/*!< ENET PHY 100 M half duplex*/
kopelka 1:3c09c9c0324e 42 kEnetPhy10FullDuplex = 0x05,/*!< ENET PHY 10 M full duplex*/
kopelka 1:3c09c9c0324e 43 kEnetPhy100FullDuplex = 0x06/*!< ENET PHY 100 M full duplex*/
kopelka 1:3c09c9c0324e 44 } enet_phy_control_t;
kopelka 1:3c09c9c0324e 45 typedef enum _enet_phy_speed
kopelka 1:3c09c9c0324e 46 {
kopelka 1:3c09c9c0324e 47 kEnetSpeed10M = 0, /*!< ENET PHY 10 M speed*/
kopelka 1:3c09c9c0324e 48 kEnetSpeed100M = 1 /*!< ENET PHY 100 M speed*/
kopelka 1:3c09c9c0324e 49 } enet_phy_speed_t;
kopelka 3:76e748d8d250 50
kopelka 3:76e748d8d250 51 typedef enum _link_md
kopelka 3:76e748d8d250 52 {
kopelka 3:76e748d8d250 53 autoMDIRegister = 0x1F, /*!< ENET PHY 10 M speed*/
kopelka 3:76e748d8d250 54 disableAutoMDI = 0x2000,
kopelka 3:76e748d8d250 55 startCableDiagREgister = 0x1D,
kopelka 3:76e748d8d250 56 enableCableDiag = 0x8000
kopelka 3:76e748d8d250 57 } link_md_t;
kopelka 3:76e748d8d250 58
kopelka 1:3c09c9c0324e 59 void onCharReceived()
kopelka 1:3c09c9c0324e 60 {
kopelka 1:3c09c9c0324e 61 c = s.getc();
kopelka 1:3c09c9c0324e 62 }
kopelka 1:3c09c9c0324e 63 void baud(int baudrate) {
kopelka 1:3c09c9c0324e 64
kopelka 1:3c09c9c0324e 65 s.baud(baudrate);
kopelka 1:3c09c9c0324e 66 }
kopelka 1:3c09c9c0324e 67
kopelka 1:3c09c9c0324e 68 uint32_t mii_read_data(uint32_t phyReg, uint32_t *dataPtr)
kopelka 1:3c09c9c0324e 69 {
kopelka 1:3c09c9c0324e 70 //uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr
kopelka 1:3c09c9c0324e 71 return enet_mii_read(0,0,phyReg,dataPtr);
kopelka 1:3c09c9c0324e 72 }
kopelka 1:3c09c9c0324e 73 uint32_t mii_write_data(uint32_t phyReg, uint32_t dataToWtite)
kopelka 1:3c09c9c0324e 74 {
kopelka 1:3c09c9c0324e 75 //enet_mii_write(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t data)
kopelka 1:3c09c9c0324e 76 return enet_mii_write(0,0,phyReg,dataToWtite);
kopelka 1:3c09c9c0324e 77 }
kopelka 1:3c09c9c0324e 78
kopelka 1:3c09c9c0324e 79 void change_speed(uint32_t phyReg,uint32_t new_speed){
kopelka 1:3c09c9c0324e 80
kopelka 1:3c09c9c0324e 81 mii_write_data(phyReg,new_speed);
kopelka 1:3c09c9c0324e 82
kopelka 1:3c09c9c0324e 83 }
kopelka 3:76e748d8d250 84 void turn_on_autonegotiation(uint32_t phyReg,uint32_t message){
kopelka 3:76e748d8d250 85 mii_write_data(phyReg,message);
kopelka 3:76e748d8d250 86 }
kopelka 1:3c09c9c0324e 87
kopelka 1:3c09c9c0324e 88 int get_connection_speed(uint32_t phyReg, uint32_t *dataPtr)
kopelka 1:3c09c9c0324e 89 {
kopelka 1:3c09c9c0324e 90 mii_read_data(phyReg,dataPtr);
kopelka 1:3c09c9c0324e 91 data &=kEnetPhySpeedDulpexMask;
kopelka 1:3c09c9c0324e 92 if ((kEnetPhy100HalfDuplex == data) || (kEnetPhy100FullDuplex == data))
kopelka 1:3c09c9c0324e 93 {
kopelka 1:3c09c9c0324e 94 return 100;
kopelka 1:3c09c9c0324e 95 }
kopelka 1:3c09c9c0324e 96
kopelka 1:3c09c9c0324e 97 return 10 ;
kopelka 1:3c09c9c0324e 98 }
issaiass 0:bbc9cfdee3bc 99
issaiass 0:bbc9cfdee3bc 100 int main (void) {
kopelka 1:3c09c9c0324e 101 int speed;
kopelka 1:3c09c9c0324e 102 baud(115200);
kopelka 1:3c09c9c0324e 103
kopelka 1:3c09c9c0324e 104
kopelka 1:3c09c9c0324e 105 EthernetInterface eth;
kopelka 1:3c09c9c0324e 106 printf("Initializing interface...\r\n");
kopelka 1:3c09c9c0324e 107 eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway
kopelka 1:3c09c9c0324e 108
issaiass 0:bbc9cfdee3bc 109
kopelka 1:3c09c9c0324e 110 printf ("Hello World! Enter task number:\n");
kopelka 1:3c09c9c0324e 111 onCharReceived();
kopelka 1:3c09c9c0324e 112 switch(c){
kopelka 1:3c09c9c0324e 113 case '1':
kopelka 1:3c09c9c0324e 114 {
kopelka 1:3c09c9c0324e 115 printf("LEts find out about RJ-45 socket diodes\n");
kopelka 1:3c09c9c0324e 116 printf("Connecting to network...\r\n");
kopelka 1:3c09c9c0324e 117 printf("Wait for red LED and connect with putty\n");
kopelka 1:3c09c9c0324e 118 eth.connect();
kopelka 1:3c09c9c0324e 119 printf("IP Address is %s\n", eth.getIPAddress());
kopelka 1:3c09c9c0324e 120
kopelka 1:3c09c9c0324e 121 TCPSocketServer server;
kopelka 1:3c09c9c0324e 122 server.bind(ECHO_SERVER_PORT);
kopelka 1:3c09c9c0324e 123 server.listen();
kopelka 1:3c09c9c0324e 124 DigitalOut myled(LED1);
issaiass 0:bbc9cfdee3bc 125
kopelka 1:3c09c9c0324e 126 while (true) {
kopelka 1:3c09c9c0324e 127 printf("Write a message in putty \n");
issaiass 0:bbc9cfdee3bc 128 TCPSocketConnection client;
issaiass 0:bbc9cfdee3bc 129 server.accept(client);
kopelka 1:3c09c9c0324e 130 client.set_blocking(false, 3000); // Timeout after (1.5)s
issaiass 0:bbc9cfdee3bc 131
issaiass 0:bbc9cfdee3bc 132 printf("Connection from: %s\n", client.get_address());
issaiass 0:bbc9cfdee3bc 133 char buffer[256];
issaiass 0:bbc9cfdee3bc 134 while (true) {
issaiass 0:bbc9cfdee3bc 135 int n = client.receive(buffer, sizeof(buffer));
issaiass 0:bbc9cfdee3bc 136 if (n <= 0) break;
issaiass 0:bbc9cfdee3bc 137
issaiass 0:bbc9cfdee3bc 138 client.send_all(buffer, n);
issaiass 0:bbc9cfdee3bc 139 if (n <= 0) break;
issaiass 0:bbc9cfdee3bc 140 }
issaiass 0:bbc9cfdee3bc 141
kopelka 1:3c09c9c0324e 142 client.close();
kopelka 1:3c09c9c0324e 143 }
kopelka 1:3c09c9c0324e 144 break;
kopelka 1:3c09c9c0324e 145 }
kopelka 1:3c09c9c0324e 146 case '2':
kopelka 1:3c09c9c0324e 147 {
kopelka 1:3c09c9c0324e 148 printf("LEts check transmission speed\n");
kopelka 1:3c09c9c0324e 149 printf("Connecting to network...\r\n");
kopelka 1:3c09c9c0324e 150 eth.connect();
kopelka 1:3c09c9c0324e 151 speed = get_connection_speed(kEnetPhyCt2, &data);
kopelka 1:3c09c9c0324e 152 printf("Connected at %d Mb/s\r\n", speed);
kopelka 1:3c09c9c0324e 153 printf("IP Address is %s\n", eth.getIPAddress());
kopelka 1:3c09c9c0324e 154 break;
kopelka 1:3c09c9c0324e 155 }
kopelka 1:3c09c9c0324e 156 case '3':{
kopelka 1:3c09c9c0324e 157 printf("Lets change speed\n");
kopelka 1:3c09c9c0324e 158 printf("Changing speed to 10Mb\n");
kopelka 1:3c09c9c0324e 159 printf("Connecting to network...\r\n");
kopelka 1:3c09c9c0324e 160 change_speed(kEnetPhyCR, kEnetSpeed10M);
kopelka 1:3c09c9c0324e 161 eth.connect();
kopelka 1:3c09c9c0324e 162 printf("IP Address is %s\n", eth.getIPAddress());
kopelka 1:3c09c9c0324e 163 speed = get_connection_speed(kEnetPhyCt2, &data);
kopelka 1:3c09c9c0324e 164 printf("Connected at %d Mb/s\r\n", speed);
kopelka 1:3c09c9c0324e 165
kopelka 1:3c09c9c0324e 166 break;
kopelka 1:3c09c9c0324e 167 }
kopelka 3:76e748d8d250 168 case '4':{
kopelka 3:76e748d8d250 169 uint32_t counter;
kopelka 3:76e748d8d250 170 uint32_t data1;
kopelka 3:76e748d8d250 171
kopelka 3:76e748d8d250 172 printf("Autonegotiation\n");
kopelka 3:76e748d8d250 173
kopelka 3:76e748d8d250 174 speed = get_connection_speed(kEnetPhyCt2, &data);
kopelka 3:76e748d8d250 175 printf("Connected at %d Mb/s\r\n", speed);
kopelka 3:76e748d8d250 176 printf("Changing speed to 10Mb\n");
kopelka 3:76e748d8d250 177 change_speed(kEnetPhyCR, kEnetSpeed10M);
kopelka 3:76e748d8d250 178 turn_on_autonegotiation(kEnetPhyCR, kEnetPhyAutoNeg);
kopelka 3:76e748d8d250 179
kopelka 3:76e748d8d250 180 for (counter = 0; counter < 0x10000; counter++)
kopelka 3:76e748d8d250 181 {
kopelka 3:76e748d8d250 182 if (enet_mii_read(0,0,kEnetPhySR,&data1) == 0)
kopelka 3:76e748d8d250 183 {
kopelka 3:76e748d8d250 184 if ((data1 & kEnetPhyAutoNegComplete) != 0)
kopelka 3:76e748d8d250 185 {
kopelka 3:76e748d8d250 186 printf("Autonegotiaion enabled\n");
kopelka 3:76e748d8d250 187 break;
kopelka 3:76e748d8d250 188 }
kopelka 3:76e748d8d250 189 }
kopelka 3:76e748d8d250 190 }
kopelka 3:76e748d8d250 191
kopelka 3:76e748d8d250 192 printf("Connecting to network...\r\n");
kopelka 3:76e748d8d250 193 eth.connect();
kopelka 3:76e748d8d250 194
kopelka 3:76e748d8d250 195 speed = get_connection_speed(kEnetPhyCt2, &data);
kopelka 3:76e748d8d250 196 printf("Connected at %d Mb/s\r\n", speed);
kopelka 3:76e748d8d250 197 break;
kopelka 3:76e748d8d250 198 }
kopelka 3:76e748d8d250 199 case '5':
kopelka 3:76e748d8d250 200 {
kopelka 3:76e748d8d250 201 uint32_t counter;
kopelka 3:76e748d8d250 202 uint32_t data1;
kopelka 3:76e748d8d250 203 double result;
kopelka 3:76e748d8d250 204 printf("Cable Test...\n");
kopelka 3:76e748d8d250 205 printf("Disable auto MDI/MDI-X\n");
kopelka 3:76e748d8d250 206 mii_write_data(autoMDIRegister,disableAutoMDI);
kopelka 3:76e748d8d250 207 printf("Start cable diagnostic\n");
kopelka 3:76e748d8d250 208 mii_write_data(startCableDiagREgister,enableCableDiag);
kopelka 3:76e748d8d250 209 printf("Wait ...\n");
kopelka 3:76e748d8d250 210 for (counter = 0; counter < 0x10000; counter++)
kopelka 3:76e748d8d250 211 {
kopelka 3:76e748d8d250 212 if (enet_mii_read(0,0,startCableDiagREgister,&data1) == 0)
kopelka 3:76e748d8d250 213 {
kopelka 3:76e748d8d250 214 if (!(data1 & enableCableDiag))
kopelka 3:76e748d8d250 215 {
kopelka 3:76e748d8d250 216 printf("Test completed\n");
kopelka 3:76e748d8d250 217 break;
kopelka 3:76e748d8d250 218 }
kopelka 3:76e748d8d250 219 }
kopelka 3:76e748d8d250 220 }
kopelka 3:76e748d8d250 221 if ((data1 & 0x6000))
kopelka 3:76e748d8d250 222 printf("Test failed\n");
kopelka 3:76e748d8d250 223 printf("Print data %d\n", data);
kopelka 3:76e748d8d250 224 result = (double)(0x00FF & data1);
kopelka 3:76e748d8d250 225 printf("Result = %lf \n", result*0.38);
kopelka 3:76e748d8d250 226 break;
kopelka 3:76e748d8d250 227 }
kopelka 1:3c09c9c0324e 228 default: break;
kopelka 1:3c09c9c0324e 229 }
kopelka 1:3c09c9c0324e 230 while(1);
kopelka 1:3c09c9c0324e 231 }
kopelka 1:3c09c9c0324e 232