Subversion Repositories HomeAutomation

Rev

Rev 389 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 389 Rev 541
Line 27... Line 27...
27
 
27
 
28
// please modify the following two lines. mac and ip have to be unique
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
29
// in your local area network. You can not have the same numbers in
30
// two devices:
30
// two devices:
31
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
31
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
-
 
32
//static uint8_t myip[4] = {192,168,0,50};
32
static uint8_t myip[4] = {193,11,254,22};
33
static uint8_t myip[4] = {193,11,254,22};
33
static uint16_t rmDumperPrt =1200; // remote port for dumping data
34
static uint16_t rmDumperPrt =1200; // remote port for dumping data
34
static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
35
static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data
35
static uint16_t locDumperPrt =1200; // listen port 
36
//static uint16_t locDumperPrt =1200; // listen port 
36
static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
37
static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data
37
static uint8_t gotdumpserver = 0;
38
static uint8_t gotdumpserver = 0;
38
static uint8_t gotcan2serserver = 0;
39
static uint8_t gotcan2serserver = 0;
39
 
40
 
40
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
41
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
Line 52... Line 53...
52
static char sendbuf[SEND_BUFFER_SIZE+1];
53
static char sendbuf[SEND_BUFFER_SIZE+1];
53
static uint8_t buffpoint;
54
static uint8_t buffpoint;
54
 
55
 
55
#define C2S_START_BYTE 253
56
#define C2S_START_BYTE 253
56
#define C2S_END_BYTE 250
57
#define C2S_END_BYTE 250
-
 
58
 
-
 
59
/* set to 0 for not sending timestamps on bus */
-
 
60
#define SENDTIMESTAMP	1
57
 
61
 
58
/*----------------------------------------------------------------------------
62
/*----------------------------------------------------------------------------
59
 * Putchar for udp
63
 * Putchar for udp
60
 * Implements a small buffer, sends packet over udp if buffer is full or 
64
 * Implements a small buffer, sends packet over udp if buffer is full or 
61
 * \n-char is sent.
65
 * \n-char is sent.
Line 81... Line 85...
81
 *---------------------------------------------------------------------------*/
85
 *---------------------------------------------------------------------------*/
82
int main(void) {
86
int main(void) {
83
 
87
 
84
	//ethernet vars
88
	//ethernet vars
85
    uint16_t plen = 0;
89
    uint16_t plen = 0;
86
    uint8_t ledi=0;
-
 
87
    uint8_t payloadlen=0;
90
    uint8_t payloadlen=0;
88
	buffpoint = 0;
91
	buffpoint = 0;
89
	
92
	
90
	//can vars
93
	//can vars
91
	Can_Message_t txMsg;
94
	Can_Message_t txMsg;
92
	Can_Message_t rxMsg;
95
	Can_Message_t rxMsg;
93
	txMsg.Id = 3;
96
	txMsg.Id = 3;
94
	txMsg.DataLength = 0;
97
	txMsg.DataLength = 0;
95
	txMsg.RemoteFlag = 0;
98
	txMsg.RemoteFlag = 0;
96
	txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
99
	txMsg.ExtendedFlag = 1;
97
 
-
 
98
 
100
 
99
 	uint32_t timeStamp = 0;
101
 	uint32_t timeStamp = 0;
100
	
102
	
101
	Mcu_Init();
103
	Mcu_Init();
102
	Timebase_Init();
104
	Timebase_Init();
103
	//Serial_Init();
-
 
104
 
105
 
105
    //alla printf ska skriva till udp
106
    //alla printf ska skriva till udp
106
    stdout = &mystdout;    //set the output stream 
107
    stdout = &mystdout;    //set the output stream 
107
    
108
    
108
	sei();
109
	sei();
109
 
110
 
110
//	printf("CanInit...");
-
 
111
	if (Can_Init() != CAN_OK) {
111
	if (Can_Init() != CAN_OK) {
-
 
112
	}
-
 
113
 
-
 
114
    /* set output to gnd, reset the ethernet chip */
-
 
115
    PORTC &= ~(1<<PC1);
-
 
116
    /* set PC1 as output */
112
//		printf(&quot;FAILED!\n&quot;);
117
    DDRC |= (1&lt;&lt;DDC1);
113
	}
118
 
114
	else {
119
    delay_ms(20);
-
 
120
    /* set PC1 as input, resetpin pulled high */
115
//		printf(&quot;OK!\n&quot;);
121
    DDRC &= ~(1&lt;&lt;DDC1);
116
	}
122
    
-
 
123
    delay_ms(100);
117
 
124
    
118
    /* enable PB0, reset as output */
-
 
119
    DDRB|= (1<<DDB0);
-
 
120
    /* set output to gnd, reset the ethernet chip */
-
 
121
    PORTB &= ~(1<<PB0);
-
 
122
    delay_ms(20);
-
 
123
    /* set output to Vcc, reset inactive */
-
 
124
    PORTB|= (1<<PB0);
-
 
125
    delay_ms(100);
-
 
126
    
-
 
127
    // LED
-
 
128
    /* enable PB1, LED as output */
-
 
129
    DDRD|= (1<<DDD6);
-
 
130
    /* set output to Vcc, LED off */
-
 
131
    PORTD|= (1<<PD6);
-
 
132
 
-
 
133
	//printf("Init enc\n");
-
 
134
    /*initialize enc28j60*/
125
    /*initialize enc28j60*/
135
    enc28j60Init(mymac);
126
    enc28j60Init(mymac);
136
    delay_ms(20);
127
    delay_ms(20);
137
    
128
    
138
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
129
    /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
139
    // LEDB=yellow LEDA=green
130
    // LEDB=yellow LEDA=green
140
    //
131
    //
141
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
132
    // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
142
    enc28j60PhyWrite(PHLCON,0x476);
133
    enc28j60PhyWrite(PHLCON,0x476);
143
    delay_ms(20);
134
    delay_ms(20);
144
    
-
 
145
    /* set output to GND, red LED on */
-
 
146
    PORTD &= ~(1<<PD6);
-
 
147
    ledi=1;
-
 
148
    
135
    
149
    //init the ethernet/ip layer:
136
    //init the ethernet/ip layer:
150
    init_ip_arp_udp_tcp(mymac,myip,80);
137
    init_ip_arp_udp_tcp(mymac,myip,80);
151
 
138
 
152
	/* main loop */
139
	/* main loop */
153
	while (1) {
140
	while (1) {
154
		/* service the CAN routines */
141
		/* service the CAN routines */
155
		Can_Service();
142
		Can_Service();
156
		
143
		
-
 
144
#if SENDTIMESTAMP == 1
157
		/* send CAN message and check for CAN errors once every second */
145
		/* send CAN message and check for CAN errors once every second */
158
		if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
146
		if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
159
			timeStamp = Timebase_CurrentTime();
147
			timeStamp = Timebase_CurrentTime();
160
			/* send txMsg */
148
			/* send timestamp, to keep network alive */
161
			//txMsg.Id++;
149
			txMsg.Id = 0;
162
			//txMsg.Id = 3;
150
			txMsg.DataLength = 0;
163
			//Can_Send(&txMsg);
151
			Can_Send(&txMsg);
164
			
152
			
165
		}
153
		}
-
 
154
#endif
166
		
155
		
167
		/* check if any messages have been received */
156
		/* check if any messages have been received */
168
		while (Can_Receive(&rxMsg) == CAN_OK) {
157
		while (Can_Receive(&rxMsg) == CAN_OK) {
169
			uint8_t i;
158
			uint8_t i;
170
 
159
 
171
			if (gotdumpserver != 0) {
160
			/*if (gotdumpserver != 0) {
172
				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));
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));
173
	    		printf("data={ ");
162
	    		printf("data={ ");
174
	    		for (i=0; i<rxMsg.DataLength; i++) {
163
	    		for (i=0; i<rxMsg.DataLength; i++) {
175
	    			printf("%x ", rxMsg.Data.bytes[i]);
164
	    			printf("%x ", rxMsg.Data.bytes[i]);
176
	    		}
165
	    		}
177
		   		printf("}\n");
166
		   		printf("}\n");
178
			}
167
			}*/
179
	   		
168
	   		
180
	   		if (gotcan2serserver != 0) {
169
	   		if (gotcan2serserver != 0) {
181
				//skicka ocks� som udppaket till rmCan2SerPrt
170
				//skicka ocks� som udppaket till rmCan2SerPrt
182
				char sendbuf2[17];
171
				char sendbuf2[17];
183
				
172
				
Line 210... Line 199...
210
		}
199
		}
211
		if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
200
		if ((enc28j60Read(ESTAT) & ESTAT_TXABRT)) {
212
			//printf("ESTAT_TXABRT set!\n");
201
			//printf("ESTAT_TXABRT set!\n");
213
			//printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
202
			//printf("MACLCON1: %i\n", enc28j60Read(MACLCON1));
214
			enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
203
			enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ESTAT, ESTAT_TXABRT);
215
		}
204
		}
216
		
205
		
217
        // get the next new packet:
206
        // get the next new packet:
218
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
207
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
219
 
208
 
220
        /*plen will ne unequal to zero if there is a valid 
209
        /*plen will ne unequal to zero if there is a valid 
Line 264... Line 253...
264
								for (i = 0; i < cm.DataLength; i++) {
253
								for (i = 0; i < cm.DataLength; i++) {
265
									cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
254
									cm.Data.bytes[i] = buf[UDP_DATA_P+8+i];
266
								}
255
								}
267
								Can_Send(&cm);
256
								Can_Send(&cm);
268
								
257
								
269
								if (gotdumpserver != 0) {
258
								/*if (gotdumpserver != 0) {
270
									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));
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));
271
									printf("data={ ");
260
									printf("data={ ");
272
									for (i=0; i<cm.DataLength; i++) {
261
									for (i=0; i<cm.DataLength; i++) {
273
										printf("%x ", cm.Data.bytes[i]);
262
										printf("%x ", cm.Data.bytes[i]);
274
									}
263
									}
275
	   								printf("}\n");
264
	   								printf("}\n");
276
								}
265
								}*/
277
							}
266
							}
278
						//om port 1200 (dumparporten) 
267
						//om port 1200 (dumparporten) 
279
                        } else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) {
268
                        } /*else if ((buf[UDP_DST_PORT_H_P]==((locDumperPrt>>8) & 0xff)) && (buf[UDP_DST_PORT_L_P] == (locDumperPrt & 0xff))) {
280
                	        if (gotdumpserver == 0) {
269
                	        if (gotdumpserver == 0) {
281
				                while(i<6){
270
				                while(i<6){
282
					                remotemac[i]=buf[ETH_SRC_MAC +i];
271
					                remotemac[i]=buf[ETH_SRC_MAC +i];
283
					                i++;
272
					                i++;
284
						        }
273
						        }
285
						        i=0;
274
						        i=0;
286
				                while(i<4){
275
				                while(i<4){
287
					                remoteip[i]=buf[IP_SRC_P +i];
276
					                remoteip[i]=buf[IP_SRC_P +i];
288
					                i++;
277
					                i++;
289
						        }
278
						        }
290
	                        	i=0;
279
	                        	i=0;
291
								
280
								
292
	                        	gotdumpserver = 1;
281
	                        	gotdumpserver = 1;
293
	                        	make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
282
	                        	make_udp_reply_from_request(buf,"Got server",10,rmDumperPrt);
294
                	        } else {
283
                	        } else {
295
                	        	make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
284
                	        	make_udp_reply_from_request(buf,"Already got server",18,rmDumperPrt);
296
                	        }
285
                	        }
297
                        }
286
                        }*/
298
	                }			            
287
	                }			            
299
			    }
288
			    }
300
	        }
289
	        }
301
        }
290
        }
302
        
291