/* W5100 device configuration Author: Philip Lindsay License: Copyright 2007-2008 // LGPL */ #include "w5100_device.h" W5100Device::W5100Device(int resetPin) { /* Store configuration and initialise the W5100 device */ // TODO: We should really allow the chip-select pin to be set here? // Or require that it's defined. (Currently in the library file 'types.h'.) _resetPin = resetPin; _init(); } void W5100Device::_init(void) { /* Initialise the W5100 device and driver. */ /* Initialise the W5100 chip (Originally I thought it was possible for the chip to function without a hardware reset but it seems not to be the case.) */ pinMode(_resetPin, OUTPUT); // We rely on the time between function calls to // be long enough for the chip to recognise the // reset. digitalWrite(_resetPin, HIGH); digitalWrite(_resetPin, LOW); // reset digitalWrite(_resetPin, HIGH); // Enable SPI bug fix // (We only need to do this to enable additional optional // devices on the SPI bus. The current version of the W5100 // requires SPI_EN to be low when you communicate with other // devices on the SPI bus.) #define PIN_SPI_EN 8 // WIZnet module SPI_EN pinMode(PIN_SPI_EN, OUTPUT); digitalWrite(PIN_SPI_EN, HIGH); // Chip initialisation by driver // Might be redundant following the above reset, // as this performs a software reset. iinchip_init(); // Initialise driver // (This is required to configure some variables used // internally by the driver--even if the default chip // configuration is used.) sysinit(0x55, 0x55); } byte * W5100Device::_packBuffer(byte b0, byte b1, byte b2, byte b3) { /* Utility function to pack four bytes into a buffer in order to pass on to the lower-level drive functions. */ return _packBuffer(b0, b1, b2, b3, 0, 0); // Adds two bytes of padding } byte * W5100Device::_packBuffer(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5) { /* Utility function to pack six bytes into a buffer in order to pass on to the lower-level drive functions. */ _scratchBuffer[0] = b0; _scratchBuffer[1] = b1; _scratchBuffer[2] = b2; _scratchBuffer[3] = b3; _scratchBuffer[4] = b4; _scratchBuffer[5] = b5; return _scratchBuffer; } void W5100Device::setIp(byte b0, byte b1, byte b2, byte b3) { /* Set device IP address. */ setSIPR(_packBuffer(b0, b1, b2, b3)); } void W5100Device::setMask(byte b0, byte b1, byte b2, byte b3) { /* Set device net mask. */ setSUBR(_packBuffer(b0, b1, b2, b3)); } void W5100Device::setGateway(byte b0, byte b1, byte b2, byte b3) { /* Set device gateway. (Note: this is required to access the internet from within a LAN.) */ setGAR(_packBuffer(b0, b1, b2, b3)); } void W5100Device::setMac(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5) { /* Set device MAC address. */ setSHAR(_packBuffer(b0, b1, b2, b3, b4, b5)); } // W5100Device W5100 = W5100Device(PIN_RESET);