Skip to content

Commit 3e902d7

Browse files
authored
Initial commit
0 parents  commit 3e902d7

36 files changed

+10755
-0
lines changed

Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
PRJ = Net
2+
MCU = atmega328p
3+
#CLK = 16000000
4+
# CLK usually defined in config.h
5+
AVRPATH=c:\WinAVR-20100110\bin
6+
COMPORT=COM4
7+
8+
CC = ${AVRPATH}\avr-gcc
9+
AVRDUDE = avrdude
10+
OBJCOPY = ${AVRPATH}\avr-objcopy
11+
OBJDUMP = $(AVRPATH)\avr-objdump
12+
SIZE = $(AVRPATH)\avr-size --format=avr --mcu=$(MCU)
13+
CFLAGS = -Wall -Os -mmcu=$(MCU) -c -std=gnu99 -funsigned-char -funsigned-bitfields -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -gdwarf-2
14+
#-DF_CPU=$(CLK)
15+
SRCS = application.c lfsr.c init.c isp.c mem23SRAM.c w25q.c main.c network.c linkENC28J60.c transport.c md5.c ripemd160.c sha1.c sha256.c power.c
16+
#where.c
17+
18+
OBJS = $(patsubst %.c,obj/%.o,$(SRCS))
19+
# object files in subdirectory obj, which will (below) be created if needed
20+
DEPS := $(OBJS:.o=.d)
21+
# Dependency files for e.g. recompile when header file changes
22+
23+
all: ${PRJ}.hex
24+
25+
${PRJ}.hex: ${PRJ}.elf
26+
${OBJCOPY} -O ihex $< $@
27+
${SIZE} -C ${PRJ}.elf
28+
29+
${PRJ}.elf: ${OBJS}
30+
${CC} -mmcu=${MCU} -Wl,--gc-sections -o $@ $^
31+
# -Wl,--gc-sections removes unwanted code for space
32+
33+
install: ${PRJ}.hex
34+
avrdude -p $(MCU) -c STK500v2 -P $(COMPORT) -V -U flash:w:${PRJ}.hex
35+
36+
$(OBJS): | obj
37+
38+
obj:
39+
@mkdir -p $@
40+
41+
-include $(DEPS)
42+
43+
obj/%.o: %.c
44+
$(CC) $(CFLAGS) -MMD -MF $(patsubst %.o,%.d,$@) -o $@ $<
45+
# -MMD and -MF make the .d dependency files to ensure we recompile when needed
46+
47+
clean:
48+
rm -f ${PRJ}.elf ${PRJ}.hex ${OBJS} ${DEPS}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Microcontroller-TCPIP-stack
2+
A size-optimised stack for (primarily Atmel/Arduino) microcontrollers with UDP and TCP, and zero configuration networking.
3+
4+
Uses DHCP to obtain an IP address, or APIPA or a static IP address.
5+
6+
Is discoverable via zero-configuration protocols (LLMNR, mDNS).
7+
8+
Supports ARP, limited ICMP (PING) and IP, UDP and TCP. At the application layer, is a NTP client, a DNS client, and a HTTP client or server.
9+
10+
Is capable of sending TCP packets larger than the 2k microcontroller memory (e.g. by constructing both the boilerplate and possibly the payload on the fly)
11+
Is capable of receiving TCP packets also larger than the microcontroller memory by incrementally downloading them from the network card (which has typically 8k)
12+
13+
Functionality can be included/excluded at compile time to trade code size for capability

Receive stack.pdf

67.3 KB
Binary file not shown.

STK500.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* STK500 constants list, from AVRDUDE, from Optiboot
2+
*
3+
* Trivial set of constants derived from Atmel App Note AVR061
4+
* Not copyrighted. Released to the public domain.
5+
6+
Also from AVR068
7+
8+
*/
9+
10+
#define STK_OK 0x10
11+
#define STK_FAILED 0x11 // Not used
12+
#define STK_UNKNOWN 0x12 // Not used
13+
#define STK_NODEVICE 0x13 // Not used
14+
#define STK_INSYNC 0x14 // ' '
15+
#define STK_NOSYNC 0x15 // Not used
16+
#define ADC_CHANNEL_ERROR 0x16 // Not used
17+
#define ADC_MEASURE_OK 0x17 // Not used
18+
#define PWM_CHANNEL_ERROR 0x18 // Not used
19+
#define PWM_ADJUST_OK 0x19 // Not used
20+
#define CRC_EOP 0x20 // 'SPACE'
21+
#define STK_GET_SYNC 0x30 // '0'
22+
#define STK_GET_SIGN_ON 0x31 // '1'
23+
#define STK_SET_PARAMETER 0x40 // '@'
24+
#define STK_GET_PARAMETER 0x41 // 'A'
25+
#define STK_SET_DEVICE 0x42 // 'B'
26+
#define STK_SET_DEVICE_EXT 0x45 // 'E'
27+
#define STK_ENTER_PROGMODE 0x50 // 'P'
28+
#define STK_LEAVE_PROGMODE 0x51 // 'Q'
29+
#define STK_CHIP_ERASE 0x52 // 'R'
30+
#define STK_CHECK_AUTOINC 0x53 // 'S'
31+
#define STK_LOAD_ADDRESS 0x55 // 'U'
32+
#define STK_UNIVERSAL 0x56 // 'V'
33+
#define STK_PROG_FLASH 0x60 // '`'
34+
#define STK_PROG_DATA 0x61 // 'a'
35+
#define STK_PROG_FUSE 0x62 // 'b'
36+
#define STK_PROG_LOCK 0x63 // 'c'
37+
#define STK_PROG_PAGE 0x64 // 'd'
38+
#define STK_PROG_FUSE_EXT 0x65 // 'e'
39+
#define STK_READ_FLASH 0x70 // 'p'
40+
#define STK_READ_DATA 0x71 // 'q'
41+
#define STK_READ_FUSE 0x72 // 'r'
42+
#define STK_READ_LOCK 0x73 // 's'
43+
#define STK_READ_PAGE 0x74 // 't'
44+
#define STK_READ_SIGN 0x75 // 'u'
45+
#define STK_READ_OSCCAL 0x76 // 'v'
46+
#define STK_READ_FUSE_EXT 0x77 // 'w'
47+
#define STK_READ_OSCCAL_EXT 0x78 // 'x'
48+
#define STK_SW_MAJOR 0x81 // ' '
49+
#define STK_SW_MINOR 0x82 // ' '
50+
51+
52+
enum SpiProgrammingProtocolCommands {
53+
//**********************************************************
54+
// Protocol commands
55+
//**********************************************************
56+
SPI_CMD_LOAD_ADDRESS = 0x06, //! Load address
57+
SPI_CMD_ENTER_PROGMODE = 0x10, //! Enter programming mode
58+
SPI_CMD_LEAVE_PROGMODE = 0x11, //! Leave programming mode
59+
SPI_CMD_CHIP_ERASE = 0x12, //! Erase device
60+
SPI_CMD_PROGRAM_FLASH = 0x13, //! Program flash data
61+
SPI_CMD_READ_FLASH = 0x14, //! Read flash data
62+
SPI_CMD_PROGRAM_EEPROM = 0x15, //! Program EEPROM data
63+
SPI_CMD_READ_EEPROM = 0x16, //! Read EEPROM data
64+
SPI_CMD_PROGRAM_FUSE = 0x17, //! Program a fuse byte
65+
SPI_CMD_READ_FUSE = 0x18, //! Read a fuse byte
66+
SPI_CMD_PROGRAM_LOCK = 0x19, //! Program lock bits
67+
SPI_CMD_READ_LOCK = 0x1A, //! Read lock bits
68+
SPI_CMD_READ_SIGNATURE = 0x1B, //! Read a signature byte
69+
SPI_CMD_READ_OSCCAL = 0x1C, //! Read an OSCCAL byte
70+
SPI_CMD_SET_BAUD = 0x1D, //! Set baud rate
71+
SPI_CMD_GET_BAUD = 0x1E //! Read baud rate
72+
};
73+
74+
enum SpiProgrammingProtocolResponses {
75+
//**********************************************************
76+
// Protocol responses
77+
//**********************************************************
78+
// Success
79+
SPI_STATUS_CMD_OK = 0x00, //! All OK
80+
81+
// Warnings
82+
SPI_STATUS_CMD_TOUT = 0x80, //! Command timeout
83+
SPI_STATUS_RDY_BSY_TOUT = 0x81, //! Device busy
84+
85+
// Errors
86+
SPI_STATUS_CMD_FAILED = 0xC0, //! Command failed
87+
SPI_STATUS_CMD_UNKNOWN = 0xC9, //! Unknown error
88+
SPI_STATUS_PHY_ERROR = 0xCB, //! Physical error
89+
SPI_STATUS_CLOCK_ERROR = 0xCC, //! Speed error
90+
SPI_STATUS_BAUD_INVALID = 0xCD //! Baud value error
91+
};

Stack.pdf

44.6 KB
Binary file not shown.

Transmit stack.pdf

65 KB
Binary file not shown.

Transport.h

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*********************************************
2+
3+
Header for Transport layer protocols TCP, UDP
4+
5+
*********************************************/
6+
7+
#ifndef TRANSPORT_H
8+
#define TRANSPORT_H
9+
10+
#define UDP_PORT (1)
11+
#define TCP_PORT (2)
12+
13+
//#define START_DYNAMIC_PORTS (0xC000)
14+
#define START_DYNAMIC_PORTS (0xC080)
15+
// See http://www.iana.org/assignments/port-numbers
16+
17+
#define TCP_CLOSED (1)
18+
#define TCP_LISTEN (2)
19+
#define TCP_SYN_RCVD (3)
20+
#define TCP_SYN_SENT (4)
21+
#define TCP_ESTABLISHED (5)
22+
#define TCP_CLOSE_WAIT (6)
23+
#define TCP_FIN_WAIT1 (7)
24+
#define TCP_FIN_WAIT2 (8)
25+
#define TCP_CLOSING (9)
26+
#define TCP_TIME_WAIT (10)
27+
#define TCP_LAST_ACK (11)
28+
29+
#define MAX_TCP_ROLES (2) // Can combine the below to sum to less than total.
30+
#define TCP_CLIENT (0) // Used as array coordinates, hence 0,1
31+
#define TCP_SERVER (1)
32+
#define TCP_FTP_CLIENT (0) // TODO : currently duplicates above - so use one at time
33+
#define TCP_FTP_PASSIVE (1)
34+
35+
#define TCP_REJECT (MAX_TCP_ROLES+1) // Outside normal range
36+
// Used to reject a wrong connection - so deliberately not one we recognise
37+
38+
#define ACK_BIT (1)
39+
#define DATA_BIT (2)
40+
#define CONSEC_BIT (4)
41+
42+
typedef struct { // UDP header : 8 bytes
43+
uint16_t sourcePort;
44+
uint16_t destinationPort;
45+
uint16_t messageLength;
46+
uint16_t UDP_checksum;
47+
} UDP_header;
48+
49+
#define UDP_HEADER_SIZE (8)
50+
51+
#define FL_FIN (0x01)
52+
#define FL_SYN (0x02)
53+
#define FL_RST (0x04)
54+
#define FL_PSH (0x08)
55+
#define FL_ACK (0x10)
56+
#define FL_URG (0x20)
57+
58+
typedef struct { // TCP header : 20 bytes
59+
uint16_t sourcePort;
60+
uint16_t destinationPort;
61+
int32_t sequence; // Making these signed allows easy sequence comparisons
62+
int32_t ack; // See Comer, Internetworking with TCP/IP Vol 2 p173
63+
unsigned unused2 : 4;
64+
unsigned headerLength : 4; // In 32 bit words
65+
uint8_t flags;
66+
uint16_t windowSize;
67+
uint16_t TCP_checksum;
68+
uint16_t urgent;
69+
} TCP_header;
70+
71+
#define TCP_HEADER_SIZE (20)
72+
73+
#define MAX_RETX (10) // TCP Packets to keep ready to re transmit
74+
75+
typedef struct { // Details of TCP packet needed for possible Re-transmission
76+
uint8_t role;
77+
unsigned active :2;
78+
unsigned retries :6;
79+
uint8_t timeout;
80+
uint16_t payloadLength;
81+
uint16_t headerLength;
82+
int32_t lastByte;
83+
uint8_t * data; // Either the packet data ... or a callback to make it.
84+
uint16_t (* callback)(uint16_t start,uint16_t length,uint8_t * result);
85+
int32_t sequence; // Only needed for callback : the sequence no of start of TCP stream
86+
uint16_t start; // Only needed for callback : the offset for the callback
87+
} Retransmit;
88+
89+
typedef struct { // TCP Transmission Control Block (TCB)
90+
unsigned status :4; // State machine
91+
unsigned age :4; // countdown
92+
uint16_t localPort;
93+
uint16_t remotePort;
94+
IP4_address remoteIP;
95+
int32_t lastByteSent;
96+
// int32_t lastAckSent;
97+
int32_t lastByteReceived;
98+
int32_t lastAckReceived;
99+
uint16_t windowSize;
100+
} TCP_TCB;
101+
102+
#define TCP_MAX_AGE (5) // Unused connection will timeout after this many s.
103+
#define TCP_TIMEOUT (1) // seconds before retransmit, then BEB
104+
#define TCP_RETRIES (3) // Timeout * 2^Retries should be < 64.
105+
106+
typedef struct { // Headers only. Enough to ACK a TCP
107+
Ethernet_header Ethernet;
108+
IP4_header IP4;
109+
union {
110+
TCP_header TCP;
111+
struct {
112+
UDP_header UDP;
113+
uint8_t dummy[12];
114+
};
115+
};
116+
} MergedACK;
117+
118+
#include "application.h"
119+
120+
#define MAX_PACKET_PAYLOAD (MAX_PACKET_SIZE-IP_HEADER_SIZE-TCP_HEADER_SIZE-ETH_HEADER_SIZE)
121+
122+
// Global functions
123+
void initialiseTCP(void);
124+
void countdownTCP(void);
125+
void retxTCP(void);
126+
void cleanupOldTCP();
127+
void initiateTCPConnection(MergedPacket * Mash, uint16_t source_port,
128+
uint16_t destination_port, IP4_address ToIP, uint8_t role);
129+
void TCP_SimpleDataOut(const char * send,const uint8_t role,uint8_t reTx);
130+
void TCP_SimpleDataOutProgmem(const char * send,const uint8_t role,uint8_t reTx);
131+
void TCP_DataIn(MergedPacket * Mash, const uint16_t length, const uint8_t role);
132+
void TCP_ComplexDataOut(MergedPacket * Mash, const uint8_t role, const uint16_t payloadLength,
133+
uint16_t (* callback)(uint16_t start,uint16_t length,uint8_t * result),uint16_t offset,
134+
uint8_t reTx);
135+
136+
void handleTCP(MergedPacket * Mash);
137+
void handleUDP(MergedPacket * Mash, uint8_t flags);
138+
void launchUDP(MergedPacket * Mash, IP4_address * ToIP,uint16_t sourcePort, uint16_t destinationPort,
139+
uint16_t data_length,void (* callback)(uint16_t start,uint16_t length,uint8_t * result),uint16_t offset);
140+
uint16_t newPort(uint8_t protocol);
141+
void MemOverflow(void);
142+
143+
144+
#endif

0 commit comments

Comments
 (0)