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.