Network Configuration Using Hard Coded Configuration
From NutWiki
Contents |
Test Environments
| Hardware Comments | Nut/OS 4.6.4 | Nut/OS 4.7.4 | Nut/OS 4.8.0 | |
| Ethernut 1.3 H | OK Binaries | OK Binaries | OK Binaries | |
| Ethernut 2.1 B | OK Binaries | OK Binaries | OK Binaries | |
| Ethernut 3.0 E | OK Binaries | OK Binaries | OK Binaries | |
| EIR 1.0 C | Configuration Error. | Configuration Error. | OK Binaries | |
| Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0 | ||||
Description
The most simple way is to use hard coded configuration values in your application. This will work for any environment, but changing the configuration requires to re-compile the code.
Source Code
#include <dev/board.h> #include <sys/timer.h> #include <stdint.h> #include <stdio.h> #include <io.h> #include <arpa/inet.h> /* Hard coded network configuration. */ #define MY_MAC { 0x00, 0x06, 0x98, 0x30, 0x02, 0x76 } #define MY_IP "192.168.192.111" #define MY_MASK "255.255.255.0" int main(void) { u_long baud = 115200; uint8_t mac[6] = MY_MAC; uint32_t ip_addr = inet_addr(MY_IP); uint32_t ip_mask = inet_addr(MY_MASK); /* Assign stdout to the DEBUG device. */ NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); puts("Network Configuration Sample 1"); /* Register Ethernet controller. */ if (NutRegisterDevice(&DEV_ETHER, 0, 0)) { puts("Registering " DEV_ETHER_NAME " failed."); } /* Configure network. */ else if (NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask)) { puts("Configuring " DEV_ETHER_NAME " failed."); } /* Done. */ else { puts("Now try 'ping " MY_IP "' on your PC."); } for (;;) { NutSleep(1000); } return 0; }
Details
We use pre-processor macros to define the configuration values. They are located on top of the code, directly following the header includes. This makes it easier to find them for later modification.
#define MY_MAC { 0x00, 0x06, 0x98, 0x30, 0x02, 0x76 } #define MY_IP "192.168.192.111" #define MY_MASK "255.255.255.0"
Before configuring the network interface, we must first register the device.
if (NutRegisterDevice(&DEV_ETHER, 0, 0)) { puts("Registering " DEV_ETHER_NAME " failed."); }
Finally the network interface is configured by calling NutNetIfConfig. When this function returns 0, then the interface is up and running.
if (NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask)) { puts("Configuring " DEV_ETHER_NAME " failed."); }
We can now use ping from any PC in our network to check the configuration.
See also
- Network Configuration Overview
- Basic TCP Server
- Basic TCP Client
- More Nut/OS Examples
External Links
MAC address A Media Access Control address (MAC address) is a unique identifier assigned to network adapters for identification.
IP address A numerical identification that is assigned to devices participating in a computer network utilizing the Internet Protocol.
Dynamic Host Configuration Protocol A protocol used by networked devices to obtain the parameters necessary for operation in an Internet Protocol network.
| Languages: |
English |
