Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos EthernetInterface
main.cpp@1:3c09c9c0324e, 2019-01-15 (annotated)
- Committer:
- kopelka
- Date:
- Tue Jan 15 23:07:16 2019 +0000
- Revision:
- 1:3c09c9c0324e
- Parent:
- 0:bbc9cfdee3bc
- Child:
- 2:e30076def8a7
blah
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
issaiass | 0:bbc9cfdee3bc | 1 | #include "mbed.h" |
issaiass | 0:bbc9cfdee3bc | 2 | #include "EthernetInterface.h" |
kopelka | 1:3c09c9c0324e | 3 | #include "fsl_enet_driver.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 | 1:3c09c9c0324e | 50 | void onCharReceived() |
kopelka | 1:3c09c9c0324e | 51 | { |
kopelka | 1:3c09c9c0324e | 52 | c = s.getc(); |
kopelka | 1:3c09c9c0324e | 53 | } |
kopelka | 1:3c09c9c0324e | 54 | void baud(int baudrate) { |
kopelka | 1:3c09c9c0324e | 55 | |
kopelka | 1:3c09c9c0324e | 56 | s.baud(baudrate); |
kopelka | 1:3c09c9c0324e | 57 | } |
kopelka | 1:3c09c9c0324e | 58 | |
kopelka | 1:3c09c9c0324e | 59 | uint32_t mii_read_data(uint32_t phyReg, uint32_t *dataPtr) |
kopelka | 1:3c09c9c0324e | 60 | { |
kopelka | 1:3c09c9c0324e | 61 | //uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr |
kopelka | 1:3c09c9c0324e | 62 | return enet_mii_read(0,0,phyReg,dataPtr); |
kopelka | 1:3c09c9c0324e | 63 | } |
kopelka | 1:3c09c9c0324e | 64 | uint32_t mii_write_data(uint32_t phyReg, uint32_t dataToWtite) |
kopelka | 1:3c09c9c0324e | 65 | { |
kopelka | 1:3c09c9c0324e | 66 | //enet_mii_write(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t data) |
kopelka | 1:3c09c9c0324e | 67 | return enet_mii_write(0,0,phyReg,dataToWtite); |
kopelka | 1:3c09c9c0324e | 68 | } |
kopelka | 1:3c09c9c0324e | 69 | |
kopelka | 1:3c09c9c0324e | 70 | void change_speed(uint32_t phyReg,uint32_t new_speed){ |
kopelka | 1:3c09c9c0324e | 71 | |
kopelka | 1:3c09c9c0324e | 72 | mii_write_data(phyReg,new_speed); |
kopelka | 1:3c09c9c0324e | 73 | |
kopelka | 1:3c09c9c0324e | 74 | } |
kopelka | 1:3c09c9c0324e | 75 | |
kopelka | 1:3c09c9c0324e | 76 | |
kopelka | 1:3c09c9c0324e | 77 | int get_connection_speed(uint32_t phyReg, uint32_t *dataPtr) |
kopelka | 1:3c09c9c0324e | 78 | { |
kopelka | 1:3c09c9c0324e | 79 | mii_read_data(phyReg,dataPtr); |
kopelka | 1:3c09c9c0324e | 80 | data &=kEnetPhySpeedDulpexMask; |
kopelka | 1:3c09c9c0324e | 81 | if ((kEnetPhy100HalfDuplex == data) || (kEnetPhy100FullDuplex == data)) |
kopelka | 1:3c09c9c0324e | 82 | { |
kopelka | 1:3c09c9c0324e | 83 | return 100; |
kopelka | 1:3c09c9c0324e | 84 | } |
kopelka | 1:3c09c9c0324e | 85 | |
kopelka | 1:3c09c9c0324e | 86 | return 10 ; |
kopelka | 1:3c09c9c0324e | 87 | } |
issaiass | 0:bbc9cfdee3bc | 88 | |
issaiass | 0:bbc9cfdee3bc | 89 | int main (void) { |
kopelka | 1:3c09c9c0324e | 90 | int speed; |
kopelka | 1:3c09c9c0324e | 91 | baud(115200); |
kopelka | 1:3c09c9c0324e | 92 | |
kopelka | 1:3c09c9c0324e | 93 | |
kopelka | 1:3c09c9c0324e | 94 | EthernetInterface eth; |
kopelka | 1:3c09c9c0324e | 95 | printf("Initializing interface...\r\n"); |
kopelka | 1:3c09c9c0324e | 96 | eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway |
kopelka | 1:3c09c9c0324e | 97 | |
issaiass | 0:bbc9cfdee3bc | 98 | |
kopelka | 1:3c09c9c0324e | 99 | printf ("Hello World! Enter task number:\n"); |
kopelka | 1:3c09c9c0324e | 100 | onCharReceived(); |
kopelka | 1:3c09c9c0324e | 101 | switch(c){ |
kopelka | 1:3c09c9c0324e | 102 | case '1': |
kopelka | 1:3c09c9c0324e | 103 | { |
kopelka | 1:3c09c9c0324e | 104 | printf("LEts find out about RJ-45 socket diodes\n"); |
kopelka | 1:3c09c9c0324e | 105 | printf("Connecting to network...\r\n"); |
kopelka | 1:3c09c9c0324e | 106 | printf("Wait for red LED and connect with putty\n"); |
kopelka | 1:3c09c9c0324e | 107 | eth.connect(); |
kopelka | 1:3c09c9c0324e | 108 | printf("IP Address is %s\n", eth.getIPAddress()); |
kopelka | 1:3c09c9c0324e | 109 | |
kopelka | 1:3c09c9c0324e | 110 | TCPSocketServer server; |
kopelka | 1:3c09c9c0324e | 111 | server.bind(ECHO_SERVER_PORT); |
kopelka | 1:3c09c9c0324e | 112 | server.listen(); |
kopelka | 1:3c09c9c0324e | 113 | DigitalOut myled(LED1); |
issaiass | 0:bbc9cfdee3bc | 114 | |
kopelka | 1:3c09c9c0324e | 115 | while (true) { |
kopelka | 1:3c09c9c0324e | 116 | printf("Write a message in putty \n"); |
issaiass | 0:bbc9cfdee3bc | 117 | TCPSocketConnection client; |
issaiass | 0:bbc9cfdee3bc | 118 | server.accept(client); |
kopelka | 1:3c09c9c0324e | 119 | client.set_blocking(false, 3000); // Timeout after (1.5)s |
issaiass | 0:bbc9cfdee3bc | 120 | |
issaiass | 0:bbc9cfdee3bc | 121 | printf("Connection from: %s\n", client.get_address()); |
issaiass | 0:bbc9cfdee3bc | 122 | char buffer[256]; |
issaiass | 0:bbc9cfdee3bc | 123 | while (true) { |
issaiass | 0:bbc9cfdee3bc | 124 | int n = client.receive(buffer, sizeof(buffer)); |
issaiass | 0:bbc9cfdee3bc | 125 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 126 | |
issaiass | 0:bbc9cfdee3bc | 127 | client.send_all(buffer, n); |
issaiass | 0:bbc9cfdee3bc | 128 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 129 | } |
issaiass | 0:bbc9cfdee3bc | 130 | |
kopelka | 1:3c09c9c0324e | 131 | client.close(); |
kopelka | 1:3c09c9c0324e | 132 | } |
kopelka | 1:3c09c9c0324e | 133 | break; |
kopelka | 1:3c09c9c0324e | 134 | } |
kopelka | 1:3c09c9c0324e | 135 | case '2': |
kopelka | 1:3c09c9c0324e | 136 | { |
kopelka | 1:3c09c9c0324e | 137 | printf("LEts check transmission speed\n"); |
kopelka | 1:3c09c9c0324e | 138 | printf("Connecting to network...\r\n"); |
kopelka | 1:3c09c9c0324e | 139 | eth.connect(); |
kopelka | 1:3c09c9c0324e | 140 | speed = get_connection_speed(kEnetPhyCt2, &data); |
kopelka | 1:3c09c9c0324e | 141 | printf("Connected at %d Mb/s\r\n", speed); |
kopelka | 1:3c09c9c0324e | 142 | printf("IP Address is %s\n", eth.getIPAddress()); |
kopelka | 1:3c09c9c0324e | 143 | break; |
kopelka | 1:3c09c9c0324e | 144 | } |
kopelka | 1:3c09c9c0324e | 145 | case '3':{ |
kopelka | 1:3c09c9c0324e | 146 | printf("Lets change speed\n"); |
kopelka | 1:3c09c9c0324e | 147 | printf("Changing speed to 10Mb\n"); |
kopelka | 1:3c09c9c0324e | 148 | printf("Connecting to network...\r\n"); |
kopelka | 1:3c09c9c0324e | 149 | change_speed(kEnetPhyCR, kEnetSpeed10M); |
kopelka | 1:3c09c9c0324e | 150 | eth.connect(); |
kopelka | 1:3c09c9c0324e | 151 | printf("IP Address is %s\n", eth.getIPAddress()); |
kopelka | 1:3c09c9c0324e | 152 | speed = get_connection_speed(kEnetPhyCt2, &data); |
kopelka | 1:3c09c9c0324e | 153 | printf("Connected at %d Mb/s\r\n", speed); |
kopelka | 1:3c09c9c0324e | 154 | |
kopelka | 1:3c09c9c0324e | 155 | break; |
kopelka | 1:3c09c9c0324e | 156 | } |
kopelka | 1:3c09c9c0324e | 157 | default: break; |
kopelka | 1:3c09c9c0324e | 158 | } |
kopelka | 1:3c09c9c0324e | 159 | while(1); |
kopelka | 1:3c09c9c0324e | 160 | } |
kopelka | 1:3c09c9c0324e | 161 |