Ethernet and Internet configuration

The file WebConfig.fth defines configuration data and tools for configuring the Ethernet controller, TCP/IP settings and other unit details such a remote web site that your equipment connects to. These tools are useful during both development and production for initial unit setup.

You can use this file as a model for your own hardware. Please copy this file from the installation folder to your application folder before use.

To avoid problems with defaults defined in other files, compile WebConfig.fth before the Ethernet driver or other PowerNet code.


include %AppDir%\PNconfig           \ PowerNet configuration
include %ExampleDir%\NetCode        \ network order stuff
include %AppDir%\WebConfig          \ configuration tools
include %CpuDir%\drivers\EtherSAM7x \ Ethernet driver
include %IpStack%\PowerNet.bld      \ PowerNet build

The following equates select Flash or EEPROM storage for configuration data. Only one should be set. Both assume that the standard MPE tools for EEPROM and Flash read/write are in use. If these tools are not being used you will have to write your own versions of DataFlash>RAM and RAM>DataFlash below}.

1 equ EEPROMconfig?     \ -- x
Set non-zero to use EEPROM configuration.

0 equ Flashconfig?      \ -- x
Set non-zero to use Flash configuration.

EEPROM/Flash area definition

The structure declared here defines the layout of the configuration data region.

Layout of serial EEPROM or a Flash region which holds configuration information.

#1024 constant /CfgData \ -- n
Size of the configuration data area.

#256 equ /URL   \ -- len
Size of a buffer of a domain or page name.

[cfg
  4 cfg: Emagic                 \ $5555AAAA
  4 cfg: Eunit#                 \ 4 byte unit number
  4 cfg: EcustID                \ 4 byte customer ID number
  6 cfg: EMACaddress            \ 6 byte Ethernet MAC address
  1 cfg: EDNS?                  \ non-zero if we can do DNS lookups
  1 cfg: EDHCP?                 \ non-zero for DHCP
  4 cfg: EIPaddress             \ 4 byte IPv4 IP addresss, 0/-1 for DHCP
  4 cfg: EEnetIPMask            \ 4 byte network IP mask
  4 cfg: EIPGateway             \ 4 byte IP gateway
  4 cfg: EPollServer            \ 4 byte Poll server IP address
  4 cfg: EPollPort              \ 4 byte Poll server port
/URL cfg: EPollURL              \ server domain name
/URL cfg: EPollPage             \ server page name
  4 cfg: ETransaction#          \ Transaction number
  0 cfg: Elast                  \ defines size used
cfg]

create CfgTemplate      \ -- addr
Default configuration which must match the configuration structure above.

Runtime data

6 buffer: EtherAddress  \ -- addr
Holds the Ethernet MAC address (six bytes). Note that you must obtain these from the IEEE (www.ieee.org) or from other sources.

1 buffer: DNS?          \ -- addr
If non-zero, the first byte indicates that DNS should be used to obtain the poll server's IP address.

1 buffer: DHCP?         \ -- addr
If non-zero, the first byte indicates that DHCP should be used to obtain the unit's IP address from a local DHCP server

4 buffer: IpAddress     \ -- addr
Holds the Ethernet IP address (four bytes). The range 192.168.xxx.yyy is commonly used for private networks. The data is in network order.

4 buffer: EnetIPMask    \ -- addr
IP mask for addresses on the Ethernet port. The data is in network order.

4 buffer: IPGateway     \ -- addr
Gateway attached to Ethernet port. The data is in network order. Set to 0.0.0.0 if there is no gateway.

4 buffer: PollServer    \ -- addr
Polling server IP address in network order. This is the server that the reader unit will connect to. The data is in network order.

4 buffer: PollPort      \ -- addr
Polling server port address, defaults to 80.

/URL buffer: PollURL    \ -- addr
Buffer for the poll server's domain name.

/URL buffer: PollPage   \ -- addr
Buffer for the page to be accessed by polling operations.

0 value Unit#   \ -- u
Unit serial number.

0 value CustID# \ -- u
Unit customer ID number.

0 value Transaction#    \ -- u
Transaction number used by the client code.

Flash and EEPROM routines

Select one set of these routines as required.

Flash

SecTab dup @ cells + @ FlashBase + constant CfgFlash    \ -- addr
For configurations in Flash, we default to using the last sector of the flash for configuration storage. Systems that use fixed size pages, e.g. Atmel SAM7X CPUs, should predefine CfgFlash.

Elast buffer: CfgBuff   \ -- addr
Buffer used during configuration read and write.

: SetDefaults   \ --
Flash: Write the default configuration to Flash. Usually run during production configuration before NetSetup below.

: DataFlash>RAM         \ --
Flash: Read the TCP/IP stack and unit settings from the Flash configuration area. Data is copied to RAM if the magic number is correct, otherwise, the default configuration is programmed and used.

: RAM>DataFlash         \ --
Flash: Write the TCP/IP stack settings to the configuration Flash.

Serial EEPROM

: DataFlash>RAM         \ --
EEPROM: Read the configuration from EEPROM.

: RAM>DataFlash         \ --
EEPROM: Write the configuration settings to the EEPROM.

Set up operations

Displaying and Entering IP addresses

: (GetIPaddress)        \ -- ipaddr|INADDR_NONE|0
Get an IPv4 address from the user. INADDR_NONE (-1) is is returned if the entry is bad, and zero is returned for a zero length entry. Use .IPv4 ( ipaddr -- ) to display an IP address.

: GetIPaddress  \ caddr -- ior
Get an IP address from the console in the form "192.168.0.55" (base is decimal). A good result is saved at caddr and ior is returned 0 for success or a null entry, or other for a bad entry. Note that caddr will always be modified.

: GetIP         \ caddr -- ior ; 0=success
Get an IP address from the console in the form "192.168.0.55" (base is decimal). A good result is saved at caddr.

Displaying and Entering MAC addresses

: .MACaddress   \ caddr --
Display the MAC address at caddr.

: getByte       \ addr char -- addr'
Collect an integer delimited by char and store it at addr, returning addr+1.

: getData       \ addr char n --
Collect n integers delimited by char, and store the bytes sequentially at addr.

: GetMACaddress \ caddr -- ior ; 0=success
Get an Ethernet MAC address from the console in the form "aa-bb-cc-dd-ee-ff" (base is hex). The result is saved at caddr and ior is returned 0 for success or a null entry, or other for a bad entry. Note that caddr will always be modified.

: GetMac        \ caddr -- ; input new MAC address
Get a new MAC address from the user console.

Setup proper

: y/n?          \ -- flag
Wait for a key and return true if the key is Y.

: EtherSetup    \ --
Set up the unit's Ethernet address.

: getC$         \ caddr --
Get a counted string of up to /URL characters from the keyboard and save it at caddr. This version is compiled if a heap is present.

: getC$         \ caddr --
Get a counted string of up to 62 characters from the keyboard and save it at caddr. This version is compiled if a heap is not present.

: (getU)        \ caddr -- ior
Get an unsigned number and place it at caddr. Return 0 for success or a null entry.

: getU          \ caddr --
Get an unsigned number and place it at caddr.

: .BadE$        \ --
Warning for unconfigured string.

: E$.           \ caddr --
Display the counted string unless the count is $00 or $FF, in which case display a warning.

: NetSetup      \ --
Configure the unit for the network and server.

: .Config       \ --
Display the Flash configuration area.

: (UnitSetup)   \ --
Used during production test to set up the unit serial number and customer number.

: UnitSetup     \ --
Used during production test to set up the unit serial number and customer number.

: .Config       \ --
Display the EEPROM configuration area.