0

I'm trying to use a W5100 Arduino Ethernet Shield on a ATMega2560 using avr code.

There exists a library written in C, which should work http://www.seanet.com/~karllunt/w5100_webserv.zip from http://www.seanet.com/~karllunt/w5100_library.html

The w5100_webserv.c should be a working example for an ATmega644p. He mentions that the four callback functions should be modified, namely my_select(), my_deselect(), my_xchg() and my_reset()

The w5100_webserv.c also contains the following definitions for SPI_PORT/DDR CS_PORT/DDR/BIT and RESET_PORT/DDR/SPI:

/*
*  Define the SPI port, used to exchange data with a W5100 chip.
*/
#define SPI_PORT     PORTB                /* target-specific port containing the SPI lines */
#define SPI_DDR      DDRB                /* target-specific DDR for the SPI port lines */

#define CS_DDR        DDRD                /* target-specific DDR for chip-select */
#define CS_PORT     PORTD                /* target-specific port used as chip-select */
#define CS_BIT        2                    /* target-specific port line used as chip-select */

#define RESET_DDR   DDRD                /* target-specific DDR for reset */
#define RESET_PORT    PORTD                /* target-specific port used for reset */
#define RESET_BIT    3                    /* target-specific port line used as reset */

I've changed it to the following according to what i think the ATmega2560's datasheet specifies

/*
 *  Define the SPI port, used to exchange data with a W5100 chip.
 */
#define SPI_PORT     PORTB                /* target-specific port containing the SPI lines */
#define SPI_DDR      DDRB                /* target-specific DDR for the SPI port lines */

#define CS_DDR        DDRB                /* target-specific DDR for chip-select */
#define CS_PORT     PORTB                /* target-specific port used as chip-select */
#define CS_BIT        10                  /* target-specific port line used as chip-select */

#define RESET_DDR   DDRB                /* target-specific DDR for reset */
#define RESET_PORT    PORTB                /* target-specific port used for reset */
#define RESET_BIT    20                   /* target-specific port line used as reset */

Furthermore, in the main(), there are also specific code for the AT644p:

/*  * Initialize the ATmega644p SPI subsystem */
CS_PORT |= (1<<CS_BIT);                                    // pull CS pin high
CS_DDR |= (1<<CS_BIT);                                    // now make it an output

SPI_PORT = SPI_PORT | (1<<PORTB4);                        // make sure SS is high
SPI_DDR = (1<<PORTB4)|(1<<PORTB5)|(1<<PORTB7);            // set MOSI, SCK and SS as output, others as input
SPCR = (1<<SPE)|(1<<MSTR);                                // enable SPI, master mode 0
SPSR |= (1<<SPI2X);                                        // set the clock rate fck/2

I've changed it to the following according to what i think the ATmega2560's datasheet specifies

/*
 *  Initialize the ATmega2560 SPI subsystem
 */
CS_PORT |= CS_BIT;                                    // pull CS pin high
CS_DDR |= CS_BIT;                                    // now make it an output

SPI_PORT = SPI_PORT | (1<<PORTB0);                        // make sure SS is high
SPI_DDR = (1<<PORTB0)|(1<<PORTB2)|(1<<PORTB1);            // set MOSI, SCK and SS as output, others as input
SPCR = (1<<SPE)|(1<<MSTR);                                // enable SPI, master mode 0
SPSR |= (1<<SPI2X);                                        // set the clock rate fck/2

Lastly i've changed the my_reset() function: void my_reset(void) { RESET_PORT |= RESET_BIT; // pull reset line high RESET_DDR |= RESET_BIT; // now make it an output RESET_PORT &=~RESET_BIT; // pull the line low _delay_ms(5); // let the device reset RESET_PORT |= RESET_BIT; // done with reset, pull the line high _delay_ms(10); // let the chip wake up }

I am not sure whether my changes are 100 % correct and i don't know what changes needs to be done to my_select() my_deselect and my_xchg. I hope someone can suggest som corrections.

Thanks in advance.

5
  • Have you actually tried it? Does it work?
    – Majenko
    Nov 4, 2015 at 15:43
  • Yes. I've compiled it several times using avr-gcc and afterwards used avrdude to transfer it to the atmega2560. To ensure that there isn't anything wrong with the board, i've tested with a simpel blinking example.
    – Peter D
    Nov 4, 2015 at 15:50
  • And what did your tests reveal? So far you have told us everything except if it actually worked or not, and if not, in what way did it not work?
    – Majenko
    Nov 4, 2015 at 15:59
  • None of my tests revealed anything. I haven't been able to connect to the specified IP-address at all. So well it did not work in anyway. It compiles without problems, it can be transfered without problem. But the webserver is not running on the Atmega2560.
    – Peter D
    Nov 4, 2015 at 16:08
  • If you have any idea what i need to change in the PORTS and DDR registers, please say so :).
    – Peter D
    Nov 4, 2015 at 17:56

1 Answer 1

0

I've managed to specify the correct ports and bits in order to make the library function on the Arduino ATmega2560 using their Ethernet Shield.

The SPI port definitions has to be changed to the following:

#define SPI_PORT    PORTB               /* target-specific port containing the SPI lines */
#define SPI_DDR     DDRB                /* target-specific DDR for the SPI port lines */

#define CS_DDR      DDRB                /* target-specific DDR for chip-select */
#define CS_PORT     PORTB               /* target-specific port used as chip-select */
#define CS_BIT      4                   /* target-specific port line used as chip-select */

#define RESET_DDR   DDRB                /* target-specific DDR for reset */
#define RESET_PORT  PORTB               /* target-specific port used for reset */
#define RESET_BIT   8                   /* target-specific port line used as reset */

The section about the AT644p in the main() function has to be changed to the following:

/*
 *  Initialize the ATmega2560 SPI subsystem
 */
// Initial SPI Peripheral
SPI_DDR = (1<<PORTB2)|(1<<PORTB1)|(1<<PORTB0);

// CS pin is not active
SPI_PORT |= (1<<CS_BIT);

    // Enable SPI, Master Mode 0, set the clock rate fck/2
SPCR = (1<<SPE)|(1<<MSTR);
SPSR |= (1<<SPI2X);

//Disable the SD card
DDRB |= (1<<CS_BIT);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.