-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
638 lines (533 loc) · 17.8 KB
/
device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//*****************************************************************************
//
// enet_io.c - I/O control via a web server.
//
// Copyright (c) 2007-2012 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 9453 of the EK-LM3S8962 Firmware Package.
//
//*****************************************************************************
// COMPILER
#include <stdarg.h>
#include "string.h"
// STELARISWARE
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/timer.h"
#include "utils/softeeprom.h"
#include "netif/etharp.h"
// PROJECT INCLUDES
#include "device.h"
#include "net/http_conf.h"
#include "net/ethernetwrapper.h"
#include "myutils/crc.h"
#include "myutils/uartstdio.h"
#include "fpga/fpga_defs.h"
#include "net/tcp_conn.h"
#include "net/udp_conn.h"
//*****************************************************************************
//
//!
//! ../../../tools/bin/makefsfile -i fs -o io_fsdata.h -r -h -q
//
//*****************************************************************************
//*****************************************************************************
//
// Defines for setting up the system clock.
//
//*****************************************************************************
#define SYSTICKHZ 100
#define SYSTICKMS (1000 / SYSTICKHZ)
#define SYSTICKUS (1000000 / SYSTICKHZ)
#define SYSTICKNS (1000000000 / SYSTICKHZ)
//*****************************************************************************
//
// A set of flags. The flag bits are defined as follows:
//
// 0 -> An indicator that a SysTick interrupt has occurred.
//
//*****************************************************************************
#define FLAG_SYSTICK 0
static volatile unsigned long g_ulFlags;
//*****************************************************************************
//
//! Global variables
//
//*****************************************************************************
unsigned long g_TimeCounter;
tDeviceSettings deviceSettings;
static volatile unsigned long systemFlags; //vlajky stavu systemu
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUGPRINT
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
void DebugInit(void)
{
#ifdef DEBUGPRINT
#define BAUD_RATE 9600
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioInitExpClk(0,BAUD_RATE);
#endif
}
//*****************************************************************************
//
//! Debug messages
//!
//! Description 1
//!
//! \param str is constant format of output
//!
//! \param ... is any other variables
//
//*****************************************************************************
void DebugMsg(const char* str, ...)
{
#ifdef DEBUGPRINT
va_list vaArgP;
va_start(vaArgP, str);
UARTvprintf(str,vaArgP);
va_end(vaArgP);
#endif
}
void ledgreen_pinset(int val)
{
if(val)
GPIOPinWrite(LED_PORT_BASE, LED_GREEN, 0xFF);
else
GPIOPinWrite(LED_PORT_BASE, LED_GREEN, 0x00);
}
//*****************************************************************************
void LED_init(void)
{
SysCtlPeripheralEnable(LED_PERIPHERAL);
GPIOPinTypeGPIOOutput(LED_PORT_BASE, LED_GREEN);
ledgreen_pinset(0);
}
//*****************************************************************************
//
// Write
//
//*****************************************************************************
int Settings_Write(tDeviceSettings *sett)
{
unsigned char *ptr_sett=(unsigned char*)sett;
unsigned short id;
unsigned short crc=0x0000;
DebugMsg("Size of Settings=%d\n",sizeof(tDeviceSettings));
sett->setIpConfig=0;
//
// write deviceSettings settings to emulation EEPROM
//
for(id=0;id<(sizeof(tDeviceSettings));id++) //div 2 WARNING !!!!!!!!!!!!!!!
{
if(SoftEEPROMWrite(id, ptr_sett[id])!=0)
{
DebugMsg("SoftEEPROM write failed on %d\n",id);
return 0;
}
crc = update_crc(crc, ptr_sett[id]);
}
DebugMsg("CRC: %04x",crc);
if(SoftEEPROMWrite(id, crc)!=0)
{
DebugMsg("SoftEEPROM write failed on %d\n",id);
return 0;
}
DebugMsg("Settings_Write OK\n");
return 1;
}
/**
* Factory defaults
*
*/
int Settings_Factory_Sett(tDeviceSettings *sett){
sett->macaddr[0] = 0x11;
sett->macaddr[1] = 0x11;
sett->macaddr[2] = 0x11;
sett->macaddr[3] = 0x11;
sett->macaddr[4] = 0x11;
sett->macaddr[5] = 0x11;
sett->serial_number = 1001;
sett->device_id = 1;
if(!Settings_Write(sett)){
return 0;
}
return 1;
}
//*****************************************************************************
//
// Default
//
//*****************************************************************************
int Settings_Default(tDeviceSettings *sett)
{
sett->dhcpOn=0;
sett->ipaddr[0]=192;
sett->ipaddr[1]=168;
sett->ipaddr[2]=1;
sett->ipaddr[3]=64;
sett->port = 3327;
sett->pr = 0; //TCP
sett->nmask[0]=255;
sett->nmask[1]=255;
sett->nmask[2]=255;
sett->nmask[3]=0;
sett->gw[0]=192;
sett->gw[1]=168;
sett->gw[2]=1;
sett->gw[3]=1;
sett->reipaddr[0]=192;
sett->reipaddr[1]=168;
sett->reipaddr[2]=1;
sett->reipaddr[3]=150;
sett->report = 3327;
sett->setIpConfig=0; //unset changing IP flag
//sett->mfEnabled = 0; //disable mac filter
//sett->macFilterListLen = 0; //clear mac filter list
sett->mfEnabled = 1;
sett->macFilterListLen = 1;
(sett->macFilter[0]).macaddr.addr[0] = 0x00;
(sett->macFilter[0]).macaddr.addr[1] = 0x21;
(sett->macFilter[0]).macaddr.addr[2] = 0x70;
(sett->macFilter[0]).macaddr.addr[3] = 0xb3;
(sett->macFilter[0]).macaddr.addr[4] = 0xce;
(sett->macFilter[0]).macaddr.addr[5] = 0x8b;
for (int i=0;i<CHANNELS;i++){
sett->channelSettings[i].controlReg = 0x19;//0b00011001;
sett->channelSettings[i].frameSelReg = 0xB0;//0b10110000;
}
//
//Save Default setting to Flash memory
//
if(!Settings_Write(sett)){
return 0;
}
return 1;
}
//*****************************************************************************
//
// Init
//
//*****************************************************************************
int Settings_Read(tDeviceSettings *sett)
{
unsigned char *ptr_sett=(unsigned char*)sett;
unsigned char id;
unsigned short data,crc=0x0000;
tBoolean found;
//
// read deviceSettings from emulation EEPROM
//
for(id=0;id<(sizeof(tDeviceSettings));id++)
{
if(SoftEEPROMRead(id, &data, &found)!=0)
{
DebugMsg("!!SoftEEPROM read failed on %d\n",id);
return 0;
}
//if read is succesfull than check founded data
if(found==true)
{
ptr_sett[id]=data;
crc = update_crc(crc, ptr_sett[id]);
}
else
{
DebugMsg("!!SoftEEPROM was not found on %d\n",id);
return 0;
}
}
if(SoftEEPROMRead(id, &data, &found)!=0)
{
DebugMsg("!!SoftEEPROM read failed on %d\n",id);
return 0;
}
//if read is succesfull than check founded data
if(found==false)
{
DebugMsg("!!SoftEEPROM was not found on %d\n",id);
return 0;
}
DebugMsg("SoftEEPROM CRC: %04x %04x\n",data, crc);
if((data!=crc) || (data==0) || (crc==0))
{
DebugMsg("WW:SoftEEPROM CRC fail, Setting Default\n");
if(!Settings_Default(sett))
return 0;
}
DebugMsg("IP=%d.%d.%d.%d\n", sett->ipaddr[0], sett->ipaddr[1],sett->ipaddr[2],sett->ipaddr[3]);
DebugMsg("Mask=%d.%d.%d.%d, GW=%d.%d.%d.%d\n",sett->nmask[0],
sett->nmask[1],sett->nmask[2],sett->nmask[3],
sett->gw[0],sett->gw[1],sett->gw[2],sett->gw[3]);
DebugMsg("Setting Read OK\n");
return 1;
}
//*****************************************************************************
int Settings_Init(void)
{
//
// initialize SoftEEPROM functionality and setup EEPROM area
//
if (SoftEEPROMInit(SOFTEEPROM_START, SOFTEEPROM_END, SOFTEEPROM_SIZE) != 0)
{
return 0;
}
return 1;
}
//*****************************************************************************
//
// The interrupt handler for the SysTick interrupt.
//
//*****************************************************************************
void SysTickIntHandler(void)
{
//
// Indicate that a SysTick interrupt has occurred.
//
HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 1;
//
// Call the lwIP timer handler.
//
lwIPTimer(SYSTICKMS);
}
void Timer0IntHandler(void)
{
// Clear the timer interrupt.
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
if (systemFlags & SYSFLAG_PINSTAT)
{
systemFlags &= ~SYSFLAG_PINSTAT;
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x01);
}
else
{
systemFlags |= SYSFLAG_PINSTAT;
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x00);
}
etharp_tmr();
// Interrut flag
systemFlags |= SYSFLAG_TIM0INT;
}
void Button_init(void)
{
SysCtlPeripheralEnable(BUTTON_PERIPHERAL);
GPIODirModeSet(BUTTON_IO, GPIO_DIR_MODE_IN);
GPIOPadConfigSet(BUTTON_IO, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
}
int main(void)
{
unsigned long *pucTemp;
unsigned long *ulIp, *ulMask, *ulGw;
//
// Set the clocking to run directly from the crystal.
//
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_8MHZ);
//
// Configure SysTick for a periodic interrupt.
//
SysTickPeriodSet(SysCtlClockGet() / SYSTICKHZ);
SysTickEnable();
SysTickIntEnable();
LED_init();
//
// Init Ports, Irq etc.
//
DebugInit();
//
// Calculate all variant of CRC for packet checksum radio modules
//
init_crc_tab();
//
// Initialization emulation EEPROM on FLASH memory
//
if(Settings_Init() == 0)
{
DebugMsg("!!!FATAL can't initialize soft EEPROM\n");
while(1);
}
//
// Read previous configuration from FLASH memory
//
DebugMsg("\nloading device settings\n");
if(Settings_Read(&deviceSettings) == 0)
{
DebugMsg("\nfallback: loading defaults");
Settings_Default(&deviceSettings);
}
DebugMsg("\nbooting ... \n");
DebugMsg("System clock = %d Hz\n", SysCtlClockGet());
//
// Configure Ethernet pins and mode
//
EthernetPeriphInit();
//
// Set Ethernet MAC address
//
//EthernetSetInternalMacAddr((unsigned char *)&deviceSettings.macaddr);
//
// Initialize the lwIP library
//
IpStackInit(&deviceSettings);
DebugMsg("\nstarting stack library \t\t\t\t[OK]\n");
//
// Initialize httpd server.
//
HttpdInit(&deviceSettings);
DebugMsg("\nstarting HTTPD \t\t\t\t\t[OK]\n");
//initialize fpga
DebugMsg("\ninitializing FPGA communication \t\t");
#ifdef SIMULATION_MODE
DebugMsg("[simulation mode]\n");
#else
FPGA_interface_init();
FPGA_channel_init(&deviceSettings);
DebugMsg("[OK]\n");
#endif
//initialize AFTN ethernet
DebugMsg("\nstarting TCP AFTN translator\t\t\t[OK]\n");
tcpConnInit(&deviceSettings);
DebugMsg("\nstarting UDP AFTN translator\t\t\t[OK]\n");
udpConnInit(&deviceSettings);
//initialize arp table
etharp_init();
DebugMsg("\nEthArp initialized \t\t\t\t[OK]\n");
//enable timer for etharp_tmr
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
// Set Timer0 period as 1/4 of the system clock, i.e. 4 interrupts per second
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet());
// Configure the timer to generate an interrupt on time-out
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// Enable the Timer Interrupt in the NVIC
IntEnable(INT_TIMER0A);
// Enable the timer
TimerEnable(TIMER0_BASE, TIMER_A);
//
// Set default settings if Reset button has been pressed.
//
// if(GPIOPinRead(BUTTON_IO) == 0)
// {
// DebugMsg("Button was push and Settings is Default\n");
// Settings_Default(&deviceSettings);
// }
//
// Enable processor interrupts.
//
IntMasterEnable();
ledgreen_pinset(1);
//
// Loop forever. All the work is done in interrupt handlers.
//
tcp_output_counter = 0;
while(1)
{
//process tcp communication
#ifdef SIMULATION_MODE
if(tcp_output_counter <= 0){
char str[] = "simulation mode";
for(int i=0;i<sizeof(str);i++){
tcp_output_buffer[tcp_output_counter].TCP_frame[i] = str[i];
}
tcp_output_buffer[tcp_output_counter].TCP_frame_length = sizeof(str);
tcp_output_counter = 1;
udpConnTx(&str[0],(u16_t) sizeof(str));
}
#else
// odesilani status paketu
if (systemFlags & SYSFLAG_TIM0INT)
{
TCP_frame_load_adapter_states(&deviceSettings, tcp_output_buffer[tcp_output_counter].TCP_frame, &tcp_output_buffer[tcp_output_counter].TCP_frame_length)
tcp_output_counter++;
if(tcp_output_counter >= OUTPUT_TCP_BUFFER_SIZE){
tcp_output_counter = 0;
}
udpConnTx((tcp_output_buffer[tcp_output_counter].TCP_frame[0]),(u16_t) tcp_output_buffer[tcp_output_counter].TCP_frame_length);
systemFlags &= ~SYSFLAG_TIM0INT;
}
char channel = check_for_new_message();
if (channel < 8) {
TCP_frame_load_new_message(channel, &deviceSettings, tcp_output_buffer[tcp_output_counter].TCP_frame, &tcp_output_buffer[tcp_output_counter].TCP_frame_length);
tcp_output_counter++;
if(tcp_output_counter >= OUTPUT_TCP_BUFFER_SIZE){
tcp_output_counter = 0;
}
udpConnTx((tcp_output_buffer[tcp_output_counter].TCP_frame[0]),(u16_t) tcp_output_buffer[tcp_output_counter].TCP_frame_length);
}
if(out_flag == 1){
send_message_to_FPGA(0, output_buffer[i], output_buffer_length);
out_flag = 0;
}
//
#endif
/*
if(deviceSettings.setIpConfig==1)
{
ulIp = (unsigned long *)&deviceSettings.ipaddr;
ulMask = (unsigned long *)&deviceSettings.nmask;
ulGw = (unsigned long *)&deviceSettings.gw;
//
// Wait a while
//
delay(2000000);
if(deviceSettings.dhcpOn==0)
lwIPNetworkConfigChange(htonl(*ulIp), htonl(*ulMask), htonl(*ulGw), IPADDR_USE_STATIC);
else
{
//
// If DHCP has already been started, we need to clear the IPs and
// switch to static. This forces the LWIP to get new IP address
// and retry the DHCP connection.
//
lwIPNetworkConfigChange(0, 0, 0, IPADDR_USE_STATIC);
//
//Restart the DHCP connection.
//
lwIPNetworkConfigChange(0, 0, 0, IPADDR_USE_DHCP);
DebugMsg("Getting IP address from DHCP...\n");
pucTemp = (unsigned long *)&deviceSettings.ipaddr;
//
// Wait until DHCP assign IP address
//
while((*pucTemp=lwIPLocalIPAddrGet())==0)
{
delay(1000000);
}
pucTemp = (unsigned long *)&deviceSettings.nmask;
*pucTemp=lwIPLocalNetMaskGet();
pucTemp = (unsigned long *)&deviceSettings.gw;
*pucTemp=lwIPLocalGWAddrGet();
}
deviceSettings.setIpConfig=0;
}
*/
}
}