/* BlueDataDistributor - data distribution module from ethernet to 32 serial ports
 * version 0.1.1 date 2006-10-07
 * Copyright (C) 2006 Stefan Schuermans <stefan@blinkenarea.org>
 * a BlinkenArea project - http://www.blinkenarea.org/
 */

#include <avr/interrupt.h>
#include <avr/io.h>

#include "config.h"
#include "macros.h"

// MAC address
unsigned char ConfigMac[6] = { 0x02, 0x23, 0x42, 0x23, 0x42, 0x00 }; // (extern)

// IP configuration
unsigned char ConfigIp[4] = { 10, 23, 42, 0 }; // own IP address (extern)
unsigned char ConfigMask[4] = { 255, 255, 0, 0 }; // subnet mask (extern)
unsigned char ConfigGw[4] = { 10, 23, 23, 23 }; // gateway IP address (extern)

// initialize
void ConfigInit( void ) // (extern)
{
  // set up IO pins
  // PORTD is used as input port for config switches,
  // but it is not set up here because it is used as serial output port
}

// get configuration from switches
void ConfigGetFromSwitches( void ) // (extern)
{
  unsigned char sreg, portd, switch_val;

  // turn off interrupts
  sreg = SREG;
  cli( );

  // PORTD to input, pull-ups on
  portd = PORTD;
  DDRD = 0x00;
  PORTD = 0xFF;

  // give pullups enough time to pull signals up
  nop( );
  nop( );
  nop( );
  nop( );
  nop( );
  nop( );
  nop( );
  nop( );
  nop( );
  nop( );

  // read address
  switch_val = ~PIND;

  // PORTD back to output
  PORTD = portd;
  DDRD = 0xFF;

  // put read value as last byte into MAC and IP addresses
  ConfigMac[5] = switch_val;
  ConfigIp[3] = switch_val;

  // restore interrupt state
  SREG = sreg;
}

