Example #1
0
unsigned char Send(unsigned char sock, const unsigned char *buf,
		   unsigned int buflen)
{
	unsigned int ptr;
	unsigned int offaddr;
	unsigned int realaddr;
	unsigned int txsize;
	unsigned int timeout;
	unsigned int sockaddr;

	if (buflen == 0 || sock >= W5100_NUM_SOCKETS)
		return W5100_FAIL;	// ignore illegal requests
	sockaddr = W5100_SKT_BASE(sock);	// calc base addr for this socket
	// Make sure the TX Free Size Register is available
	txsize = W51_read(sockaddr + W5100_TX_FSR_OFFSET);	// make sure the TX free-size reg is available
	txsize =
	    (((txsize & 0x00FF) << 8) +
	     W51_read(sockaddr + W5100_TX_FSR_OFFSET + 1));

	timeout = 0;
	while (txsize < buflen) {
		_delay_ms(1);

		txsize = W51_read(sockaddr + W5100_TX_FSR_OFFSET);	// make sure the TX free-size reg is available
		txsize =
		    (((txsize & 0x00FF) << 8) +
		     W51_read(sockaddr + W5100_TX_FSR_OFFSET + 1));

		if (timeout++ > 1000)	// if max delay has passed...
		{
			DisconnectSocket(sock);	// can't connect, close it down
			return W5100_FAIL;	// show failure
		}
	}

	// Read the Tx Write Pointer
	ptr = W51_read(sockaddr + W5100_TX_WR_OFFSET);
	offaddr =
	    (((ptr & 0x00FF) << 8) +
	     W51_read(sockaddr + W5100_TX_WR_OFFSET + 1));

	while (buflen) {
		buflen--;
		realaddr = (W5100_TXBUFADDR + (0x0800 * sock)) + (offaddr & W5100_TX_BUF_MASK);	// calc W5100 physical buffer addr for this socket

		W51_write(realaddr, *buf);	// send a byte of application data to TX buffer
		offaddr++;	// next TX buffer addr
		buf++;		// next input buffer addr
	}

	W51_write(sockaddr + W5100_TX_WR_OFFSET, (offaddr & 0xFF00) >> 8);	// send MSB of new write-pointer addr
	W51_write(sockaddr + W5100_TX_WR_OFFSET + 1, (offaddr & 0x00FF));	// send LSB

	W51_write(sockaddr + W5100_CR_OFFSET, W5100_SKT_CR_SEND);	// start the send on its way
	while (W51_read(sockaddr + W5100_CR_OFFSET)) ;	// loop until socket starts the send (blocks!!)

	return W5100_OK;
}
Example #2
0
int testserver(void) {
    unsigned int sockaddr;
    unsigned char mysocket;
    unsigned int rsize;

    mysocket = 0; // magic number! declare the socket number we will use (0-3)
    sockaddr = 0x400;//W5100_SKT_BASE(mysocket); // calc address of W5100 register set for this socket

    /*
     *  The main loop.  Control stays in this loop forever, processing any received packets
     *  and sending any requested data.
     */
    while (1) {
        LATAbits.LATA0 ^= 1;
        int x = w5100_read(sockaddr + W5100_SR_OFFSET);
        switch (x) // based on current status of socket...
        {
            case W5100_SKT_SR_CLOSED: // if socket is closed...
                LATBbits.LATB8 = 0;
                if (OpenSocket(mysocket, W5100_SKT_MR_TCP, HTTP_PORT) == mysocket) // if successful opening a socket...
                {
                    Listen(mysocket);
                    __delay_ms(1);
                }
                break;

            case W5100_SKT_SR_ESTABLISHED: // if socket connection is established...
                LATBbits.LATB8 = 1;
                rsize = ReceivedSize(mysocket); // find out how many bytes
                if (rsize > 0) {
                    if (Receive(mysocket, buf, rsize) != W5100_OK) break; // if we had problems, all done
                    /*
                     *  Add code here to process the payload from the packet.
                     *
                     *  For now, we just ignore the payload and send a canned HTML page so the client at least
                     *  knows we are alive.
                     */
                    strcpy((char *) buf, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
                    strcat((char *) buf, "<html>\r\n<body>\r\n");
                    strcat((char *) buf, "<title>Phil's W5100 web server (ATmega644p)</title>\r\n");
                    strcat((char *) buf, "<h2>Phil's ATmega644p web server using Wiznet W5100 chip</h2>\r\n");
                    strcat((char *) buf, "<br /><hr>\r\n");
                    if (Send(mysocket, buf, strlen((char *) buf)) == W5100_FAIL) break; // just throw out the packet for now

                    strcpy((char *) buf, "This is part 2 of the page.");
                    strcat((char *) buf, "</body>\r\n</html>\r\n");
                    if (Send(mysocket, buf, strlen((char *) buf)) == W5100_FAIL) break; // just throw out the packet for now

                    DisconnectSocket(mysocket);
                } else // no data yet...
                {
                    __delay_us(10);
                }
                break;

            case W5100_SKT_SR_FIN_WAIT:
            case W5100_SKT_SR_CLOSING:
            case W5100_SKT_SR_TIME_WAIT:
            case W5100_SKT_SR_CLOSE_WAIT:
            case W5100_SKT_SR_LAST_ACK:
                CloseSocket(mysocket);
                break;
        }
    }

    return 0;
}
Example #3
0
int main(void)
{
	unsigned int sockaddr;
	unsigned char mysocket;
	unsigned int rsize;

	/* Initialize the UART for ATmega168 96008N1    */
	uart_init();

	stdout = &uart_stdout;	//Required for printf init

	mysocket = 0;		// magic number! declare the socket number we will us
	sockaddr = W5100_SKT_BASE(mysocket);	// calc address of W5100 register set for this socket

	puts("AVR Ethernet\r\n");
/*
 *  Initialize the ATmega168 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 << PORTB2);	// make sure SS is high
	SPI_DDR = (1 << PORTB3) | (1 << PORTB5) | (1 << PORTB2);	// 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

/*
 *  Load up the callback block, then initialize the Wiznet W5100
 */
	my_callbacks._select = &my_select;	// callback for selecting the W5100
	my_callbacks._xchg = &my_xchg;	// callback for exchanging data
	my_callbacks._deselect = &my_deselect;	// callback for deselecting the W5100
	my_callbacks._reset = &my_reset;	// callback for hardware-reset of the W5100

	W51_register(&my_callbacks);	// register our target-specific W5100 routines with the W5100 library
	W51_init();		// now initialize the W5100

/*
 *  Configure the W5100 device to handle PING requests.
 *  This requires configuring the chip, not a specific socket.
 */
	W51_config(&my_cfg);	// config the W5100 (MAC, TCP address, subnet, etc

	puts("Debug: AVR Ethernet after W5100 config\r\n");

/*
 *  The main loop.  Control stays in this loop forever, processing any received packets
 *  and sending any requested data.
 */
	while (1) {
		switch (W51_read(sockaddr + W5100_SR_OFFSET))	// based on current status of socket...
		{
		case W5100_SKT_SR_CLOSED:	// if socket is closed...
			if (OpenSocket(mysocket, W5100_SKT_MR_TCP, HTTP_PORT) == mysocket)	// if successful opening a socket...
			{
				Listen(mysocket);
				_delay_ms(1);
			}
			break;

		case W5100_SKT_SR_ESTABLISHED:	// if socket connection is established...
			rsize = ReceivedSize(mysocket);	// find out how many bytes
			if (rsize > 0) {
				if (Receive(mysocket, buf, rsize) != W5100_OK)
					break;	// if we had problems, all done
/*
 *  Add code here to process the payload from the packet.
 *
 *  For now, we just ignore the payload and send a canned HTML page so the client at least
 *  knows we are alive.
 */
				strcpy_P((char *)buf,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
				strcat_P((char *)buf,PSTR("<html>\r\n<body>\r\n"));
				strcat_P((char *)buf,PSTR("<title>Title</title>\r\n"));
				strcat_P((char *)buf,PSTR("<p>Hello world</p>\r\n"));
				strcat_P((char *)buf,PSTR("</body>\r\n</html>\r\n"));
				if (Send(mysocket, buf, strlen((char *)buf)) == W5100_FAIL) break;	// just throw out the packet for now

				DisconnectSocket(mysocket);
			} else	// no data yet...
			{
				_delay_us(10);
			}
			break;

		case W5100_SKT_SR_FIN_WAIT:
		case W5100_SKT_SR_CLOSING:
		case W5100_SKT_SR_TIME_WAIT:
		case W5100_SKT_SR_CLOSE_WAIT:
		case W5100_SKT_SR_LAST_ACK:
			CloseSocket(mysocket);
			break;
		}
	}

	return 0;
}