Subversion Repositories HomeAutomation

Rev

Rev 541 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
205 arune 1
/**
2
 * 
280 arune 3
 * 
4
 * 
205 arune 5
 * @date	2006-11-21
280 arune 6
 * @author	Jimmy Myhrman, Anders Runeson
205 arune 7
 *   
8
 */
9
 
10
/*-----------------------------------------------------------------------------
11
 * Includes
12
 *---------------------------------------------------------------------------*/
13
/* system files */
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
17
#include <stdio.h>
18
/* lib files */
19
#include <can.h>
280 arune 20
//#include <serial.h>
205 arune 21
#include <timebase.h>
22
#include <ip_arp_udp_tcp.h>
23
#include <enc28j60.h>
24
#include <timeout.h>
25
#include <avr_compat.h>
26
#include <net.h>
27
 
28
// please modify the following two lines. mac and ip have to be unique
29
// in your local area network. You can not have the same numbers in
30
// two devices:
31
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
541 arune 32
//static uint8_t myip[4] = {192,168,0,50};
205 arune 33
static uint8_t myip[4] = {193,11,254,22};
280 arune 34
static uint16_t rmDumperPrt =1200; // remote port for dumping data
35
static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
541 arune 36
//static uint16_t locDumperPrt =1200; // listen port 
280 arune 37
static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
379 arune 38
static uint8_t gotdumpserver = 0;
39
static uint8_t gotcan2serserver = 0;
205 arune 40
 
41
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
42
 
43
static uint8_t remotemac[6];
44
static uint8_t remoteip[4];
45
 
379 arune 46
static uint8_t prgremotemac[6];
47
static uint8_t prgremoteip[4];
48
 
205 arune 49
#define BUFFER_SIZE 250
50
static uint8_t buf[BUFFER_SIZE+1];
51
 
212 arune 52
#define SEND_BUFFER_SIZE 20
53
static char sendbuf[SEND_BUFFER_SIZE+1];
54
static uint8_t buffpoint;
55
 
281 arune 56
#define C2S_START_BYTE 253
57
#define C2S_END_BYTE 250
58
 
541 arune 59
/* set to 0 for not sending timestamps on bus */
60
#define SENDTIMESTAMP	1
61
 
212 arune 62
/*----------------------------------------------------------------------------
63
 * Putchar for udp
64
 * Implements a small buffer, sends packet over udp if buffer is full or 
65
 * \n-char is sent.
66
 * Needs a timeout to force send. 
67
 *--------------------------------------------------------------------------*/
205 arune 68
int udp_putchar(char c, FILE* stream) {
379 arune 69
	if (gotdumpserver != 0) {
212 arune 70
        sendbuf[buffpoint] = c;
71
        buffpoint++;
72
 
73
        if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) {
280 arune 74
        	send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip);
212 arune 75
        	buffpoint = 0;
205 arune 76
        }
77
	}
78
	return 0;
79
}
80
 
81
static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); 
82
 
83
/*-----------------------------------------------------------------------------
84
 * Main Program
85
 *---------------------------------------------------------------------------*/
86
int main(void) {
87
 
88
	//ethernet vars
89
    uint16_t plen = 0;
90
    uint8_t payloadlen=0;
212 arune 91
	buffpoint = 0;
205 arune 92
 
93
	//can vars
94
	Can_Message_t txMsg;
95
	Can_Message_t rxMsg;
96
	txMsg.Id = 3;
97
	txMsg.DataLength = 0;
98
	txMsg.RemoteFlag = 0;
541 arune 99
	txMsg.ExtendedFlag = 1;
212 arune 100
 
101
 	uint32_t timeStamp = 0;
205 arune 102
 
212 arune 103
	Mcu_Init();
205 arune 104
	Timebase_Init();
212 arune 105
 
106
    //alla printf ska skriva till udp
107
    stdout = &mystdout;    //set the output stream 
205 arune 108
 
109
	sei();
110
 
111
	if (Can_Init() != CAN_OK) {
112
	}
113
 
114
    /* set output to gnd, reset the ethernet chip */
541 arune 115
    PORTC &= ~(1<<PC1);
116
    /* set PC1 as output */
117
    DDRC |= (1<<DDC1);
118
 
205 arune 119
    delay_ms(20);
541 arune 120
    /* set PC1 as input, resetpin pulled high */
121
    DDRC &= ~(1<<DDC1);
122
 
205 arune 123
    delay_ms(100);
124
 
125
    /*initialize enc28j60*/
126
    enc28j60Init(mymac);
127
    delay_ms(20);
128
 
129
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
130
    // LEDB=yellow LEDA=green
131
    //
132
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
133
    enc28j60PhyWrite(PHLCON,0x476);
134
    delay_ms(20);
135
 
136
    //init the ethernet/ip layer:
137
    init_ip_arp_udp_tcp(mymac,myip,80);
138
 
139
	/* main loop */
140
	while (1) {
141
		/* service the CAN routines */
142
		Can_Service();
143
 
541 arune 144
#if SENDTIMESTAMP == 1
205 arune 145
		/* send CAN message and check for CAN errors once every second */
541 arune 146
		if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
205 arune 147
			timeStamp = Timebase_CurrentTime();
541 arune 148
			/* send timestamp, to keep network alive */
149
			txMsg.Id = 0;
150
			txMsg.DataLength = 0;
151
			Can_Send(&txMsg);
205 arune 152
 
153
		}
541 arune 154
#endif
205 arune 155
 
156
		/* check if any messages have been received */
157
		while (Can_Receive(&rxMsg) == CAN_OK) {
281 arune 158
			uint8_t i;
379 arune 159
 
541 arune 160
			/*if (gotdumpserver != 0) {
379 arune 161
				printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
162
	    		printf("data={ ");
163
	    		for (i=0; i<rxMsg.DataLength; i++) {
164
	    			printf("%x ", rxMsg.Data.bytes[i]);
165
	    		}
166
		   		printf("}\n");
541 arune 167
			}*/
212 arune 168
 
379 arune 169
	   		if (gotcan2serserver != 0) {
170
				//skicka ocks� som udppaket till rmCan2SerPrt
171
				char sendbuf2[17];
172
 
173
				sendbuf2[0] = C2S_START_BYTE;
174
				sendbuf2[1] = (uint8_t)rxMsg.Id;
175
				sendbuf2[2] = (uint8_t)(rxMsg.Id>>8);
176
				sendbuf2[3] = (uint8_t)(rxMsg.Id>>16);
177
				sendbuf2[4] = (uint8_t)(rxMsg.Id>>24);
178
				sendbuf2[5] = rxMsg.ExtendedFlag;
179
				sendbuf2[6] = rxMsg.RemoteFlag;
180
				sendbuf2[7] = rxMsg.DataLength;
181
	    		for (i=0; i<8; i++) {
182
	    			sendbuf2[8+i] = rxMsg.Data.bytes[i];
183
	    		}
184
				sendbuf2[16] = C2S_END_BYTE;
185
 
186
				send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip);
187
	   		}
205 arune 188
		}
189
 
212 arune 190
		//test-debug-kod
191
		if ((enc28j60Read(EIR) & EIR_TXERIF)) {
192
			//printf("EIR_TXERIF set!\n");
193
			if ((enc28j60Read(ECON1) & ECON1_TXRTS)) {
194
				//printf("ECON1_TXRTS set!\n");
195
			}
196
		}
197
		if ((enc28j60Read(EIR) & EIR_TXIF)) {
198
			//printf("EIR_TXIF set! ");
199
		}
200
		if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
201
			//printf("ESTAT_TXABRT set!\n");
202
			//printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
203
			enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
204
		}
205
 
205 arune 206
        // get the next new packet:
207
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
208
 
209
        /*plen will ne unequal to zero if there is a valid 
210
         * packet (without crc error) */
211
        if(plen!=0){
212
	        // arp is broadcast if unknown but a host may also
213
	        // verify the mac address by sending it to 
214
	        // a unicast address.
215
	        if(eth_type_is_arp_and_my_ip(buf,plen)){
216
	                make_arp_answer_from_request(buf);
217
	        } else {
218
			    // check if ip packets (icmp or udp) are for us:
219
			    if(eth_type_is_ip_and_my_ip(buf,plen)!=0){
220
 
221
			        if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
222
			                // a ping packet, let's send pong
223
			                make_echo_reply_from_request(buf,plen);
212 arune 224
			                //txMsg.Id = 6;
225
			                //Can_Send(&txMsg);
205 arune 226
 
227
			        }
228
	                if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
229
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
379 arune 230
						uint8_t i = 0;
231
 
232
                        //om port 1100 (programmeringsporten)
280 arune 233
                        if ((buf[UDP_DST_PORT_H_P]==((locCan2SerPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locCan2SerPrt & 0xff))) {
379 arune 234
			                while(i<6){
235
				                prgremotemac[i]=buf[ETH_SRC_MAC +i];
236
				                i++;
237
					        }
238
					        i=0;
239
			                while(i<4){
240
				                prgremoteip[i]=buf[IP_SRC_P +i];
241
				                i++;
242
					        }
243
					        gotcan2serserver = 1;
244
 
245
							if ((buf[UDP_DATA_P]==C2S_START_BYTE) && (buf[UDP_DATA_P+16]==C2S_END_BYTE) && (payloadlen==17)) {
246
								Can_Message_t cm;
247
								//cm.Id = 0;
248
								cm.DataLength = buf[UDP_DATA_P+7];
249
								cm.RemoteFlag = buf[UDP_DATA_P+6];
250
								cm.ExtendedFlag = buf[UDP_DATA_P+5];
251
								cm.Id = (uint32_t)buf[UDP_DATA_P+1] + ((uint32_t)buf[UDP_DATA_P+2] << 8) + ((uint32_t)buf[UDP_DATA_P+3] << 16) + ((uint32_t)buf[UDP_DATA_P+4] << 24);
252
								uint8_t i;
253
								for (i = 0; i < cm.DataLength; i++) {
254
									cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
255
								}
256
								Can_Send(&cm);
257
 
541 arune 258
								/*if (gotdumpserver != 0) {
379 arune 259
									printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", cm.Id, (uint16_t)(cm.DataLength), (uint16_t)(cm.ExtendedFlag), (uint16_t)(cm.RemoteFlag));
260
									printf("data={ ");
261
									for (i=0; i<cm.DataLength; i++) {
262
										printf("%x ", cm.Data.bytes[i]);
281 arune 263
									}
379 arune 264
	   								printf("}\n");
541 arune 265
								}*/
281 arune 266
							}
379 arune 267
						//om port 1200 (dumparporten) 
541 arune 268
                        } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) {
379 arune 269
                	        if (gotdumpserver == 0) {
212 arune 270
				                while(i<6){
271
					                remotemac[i]=buf[ETH_SRC_MAC +i];
272
					                i++;
273
						        }
274
						        i=0;
275
				                while(i<4){
276
					                remoteip[i]=buf[IP_SRC_P +i];
277
					                i++;
278
						        }
279
	                        	i=0;
280
 
379 arune 281
	                        	gotdumpserver = 1;
280 arune 282
	                        	make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
212 arune 283
                	        } else {
280 arune 284
                	        	make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
212 arune 285
                	        }
541 arune 286
                        }*/
205 arune 287
	                }			            
288
			    }
289
	        }
290
        }
291
 
292
 
293
 
294
 
295
	}
296
 
297
	return 0;
298
}