-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
514 lines (430 loc) · 13.4 KB
/
main.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
/**
* \file demoMain.c
*
* \brief This file contains the main() and other functions.
*
*/
#include "main.h"
#include "demoTimer.h"
#include "enet.h"
#include "demoLedIf.h"
#include "demoRtc.h"
#include "demoSdRw.h"
#include "demoSdFs.h"
#include "i2c.h"
#include "net.h"
#include "ff.h"
#include "mmcsd_proto.h"
#include "interrupt.h"
#include "soc_AM335x.h"
#include "cp15.h"
#include "beaglebone.h"
#include "uartStdio.h"
#include "delay.h"
#include <string.h>
/****************************************************************************
** INTERNAL MACRO DEFINITIONS
****************************************************************************/
#define NUM_OF_IMAGES (9u)
#define RGN_L2_WBWA (8u)
#define SECT_ATTR(NS, nG, S, AP2, TEX, AP, Dom, XN, C, B) \
(0x02 | (NS << 19) | (nG << 17) | (S <<16) | \
(AP2 <<15) | (TEX << 12) | (AP << 10) | (Dom << 5)\
| (XN << 4) | (C << 3) | (B << 2))
#define CACHEABLE_TLB_ATTR SECT_ATTR(1, 0, 0, 0, 5, 3, 0, 0, 1, 0)
#define NORM_TLB_ATTR SECT_ATTR(1, 0, 0, 0, 0, 3, 0, 1, 0, 0)
/****************************************************************************
** LOCAL FUNCTION PROTOTYPES
****************************************************************************/
static void EnetStatusCheckNUpdate(void);
static void PeripheralsSetUp(void);
static void ContextReset(void);
static void dummyIsr(void);
/****************************************************************************
** EXTERNAL VARIABLE DECLARATIONS
****************************************************************************/
extern unsigned int ipAddr;
extern volatile tBoolean bConnected;
/*******************************************************************************
** EXTERNAL FUNCTION DECLARATIONS
*******************************************************************************/
extern void etharp_tmr(void);
/****************************************************************************
** GLOBAL VARIABLES DEFINITIONS
****************************************************************************/
mmcsdCardInfo SDCard;
unsigned int IPAddress = 0;
unsigned char runData[ 1024 ];
unsigned int runCommand = 0;
unsigned int runIndex = 0;
unsigned char uartData[ 32 ];
unsigned int clickIdx = 0;
#ifdef __IAR_SYSTEMS_ICC__
#pragma data_alignment=(16*1024)
static volatile unsigned int pageTable[4*1024];
#elif defined(__TMS470__)
#pragma DATA_ALIGN(pageTable, (16*1024));
static volatile unsigned int pageTable[4*1024];
#else
static volatile unsigned int pageTable[4*1024] __attribute__((aligned(16*1024)));
#endif
/****************************************************************************
** FUNCTION DEFINITIONS
****************************************************************************/
/*
** Enable all the peripherals in use
*/
static void PeripheralsSetUp(void)
{
DMTimer2ModuleClkConfig();
DMTimer3ModuleClkConfig();
DMTimer4ModuleClkConfig();
RTCModuleClkConfig();
CPSWPinMuxSetup();
CPSWClkEnable();
EDMAModuleClkConfig();
GPIO1ModuleClkConfig();
GPIO1Pin23PinMuxSetup();
HSMMCSDPinMuxSetup();
HSMMCSDModuleClkConfig();
}
/*
** Resets the state
*/
static void ContextReset(void)
{
tmrFlag = FALSE;
LedOff( USER_LED_3 );
rtcSetFlag = FALSE;
rtcSecUpdate = FALSE;
// sdCardAccessFlag = FALSE;
}
/*
** Read config info from SD card files
*/
void configRead()
{
FIL fileObj;
FRESULT fresult;
char data[ 32 ];
unsigned short bytesRead = 0;
unsigned int i;
// Open and read the IP Address file
fresult = f_open( &fileObj, "ipaddr", FA_READ );
if( fresult != FR_OK )
{
return;
}
fresult = f_read( &fileObj, data, 8, &bytesRead );
if( fresult != FR_OK )
{
UARTPuts( "Couldn't read ipaddr file... Using Dynamic IP", -1 );
IPAddress = 0;
f_close( &fileObj );
return;
}
data[ bytesRead ] = 0;
UARTPuts( "IP Addr File: Using 0x", -1 );
UARTPuts( data, -1 );
UARTPuts( "\n\r", -1 );
f_close( &fileObj );
// Decode IP Address
for( i = 0; i < 8; ++i )
{
if( data[ i ] >= '0' && data[ i ] <= '9' )
{
data[ i ] -= 0x30;
}
else if( data[ i ] >= 'A' && data[ i ] <= 'F' )
{
data[ i ] -= 0x37;
}
else if( data[ i ] >= 'a' && data[ i ] <= 'f' )
{
data[ i ] -= 0x57;
}
IPAddress += (unsigned int)(data[ i ]) << ( 28 - i * 4 );
}
}
/*
** Main function. The application starts here.
*/
int main(void)
{
unsigned int index;
unsigned int j;
int result = 0;
/*
** Sets up Section page tables. This is only first level
** page table, each page is of size 1MB
*/
for(index = 0; index < (4*1024); index++)
{
/* Set the cacheable memory attributes */
if((index >= 0x800 && index < 0x880) || (index == 0x403))
{
pageTable[index] = (index << 20) | CACHEABLE_TLB_ATTR;
}
/* Set the non-cacheable memory attributes */
else
{
pageTable[index] = (index << 20) | NORM_TLB_ATTR;
}
}
/* Invalidate the TLB, pipeline */
CP15TlbInvalidate();
CP15BranchPredictorInvalidate();
CP15BranchPredictionEnable();
CP15DomainAccessClientSet();
/* Set TTB0 value. We use only TTB0 here (N = 0) */
CP15Ttb0Set(((unsigned int )pageTable) | RGN_L2_WBWA);
/* Enables MMU */
CP15MMUEnable();
/* Flush and enable Instruction Cache */
CP15ICacheFlush();
CP15ICacheEnable();
PeripheralsSetUp();
/* Initialize the ARM Interrupt Controller */
IntAINTCInit();
/* Register the ISRs */
Timer2IntRegister();
Timer4IntRegister();
EnetIntRegister();
RtcIntRegister();
HSMMCSDIntRegister();
IntRegister(127, dummyIsr);
IntMasterIRQEnable();
/* Enable system interrupts */
IntSystemEnable(SYS_INT_RTCINT);
IntPrioritySet(SYS_INT_RTCINT, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(SYS_INT_3PGSWTXINT0);
IntPrioritySet(SYS_INT_3PGSWTXINT0, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(SYS_INT_3PGSWRXINT0);
IntPrioritySet(SYS_INT_3PGSWRXINT0, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(SYS_INT_TINT2);
IntPrioritySet(SYS_INT_TINT2, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(SYS_INT_TINT4);
IntPrioritySet(SYS_INT_TINT4, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(SYS_INT_MMCSD0INT);
IntPrioritySet(SYS_INT_MMCSD0INT, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(SYS_INT_EDMACOMPINT);
IntPrioritySet(SYS_INT_EDMACOMPINT, 0, AINTC_HOSTINT_ROUTE_IRQ);
IntSystemEnable(127);
IntPrioritySet(127, 0, AINTC_HOSTINT_ROUTE_IRQ);
RtcInit();
UARTStdioInit();
HSMMCSDContolInit();
DelayTimerSetup();
Timer2Config();
Timer4Config();
LedIfConfig();
Timer2IntEnable();
Timer4IntEnable();
RtcSecIntEnable();
InitI2C();
Timer4Start();
// Read config from files
HSMMCSDCardAccessSetup();
configRead();
LedOn( USER_LED_1 );
for( j = 0; j < 1000000; ++j );
LedOff( USER_LED_1 );
LedOn( USER_LED_2 );
for( j = 0; j < 1000000; ++j );
LedOff( USER_LED_2 );
LedOn( USER_LED_3 );
for( j = 0; j < 1000000; ++j );
LedOff( USER_LED_3 );
LedOn( USER_LED_4 );
for( j = 0; j < 1000000; ++j );
LedOff( USER_LED_4 );
// TEMP
//i2cTest();
/*
** Loop for ever. Necessary actions shall be taken
** after detecting the click.
*/
while( 1 )
{
EnetStatusCheckNUpdate();
if( runCommand )
{
// Command blink
LedOn( USER_LED_1 );
if( runData[ runIndex + 1 ] == 'D' )
{
if( runData[ runIndex + 2 ] == 'S' )
{
UARTPuts( "** DAC SET\n\r", -1 );
if( runData[ runIndex + 3 ] == 'A' )
{
i2cDAC_Set( 0,
runData[ runIndex + 4 ], runData[ runIndex + 5 ] );
}
if( runData[ runIndex + 6 ] == 'B' )
{
i2cDAC_Set( 1,
runData[ runIndex + 7 ], runData[ runIndex + 8 ] );
}
if( runData[ runIndex + 9 ] == 'C' )
{
i2cDAC_Set( 2,
runData[ runIndex + 10 ], runData[ runIndex + 11 ] );
}
if( runData[ runIndex + 12 ] == 'D' )
{
i2cDAC_Set( 3,
runData[ runIndex + 13 ], runData[ runIndex + 14 ] );
}
}
else if( runData[ runIndex + 2 ] == 'G' )
{
UARTPuts( "** DAC GET\n\r", -1 );
uartData[ 0 ] = 15;
uartData[ 1 ] = 'D';
uartData[ 2 ] = 'G';
uartData[ 3 ] = 'A';
i2cDAC_Get( 0, &uartData[ 4 ] );
uartData[ 6 ] = 'B';
i2cDAC_Get( 1, &uartData[ 7 ] );
uartData[ 9 ] = 'C';
i2cDAC_Get( 2, &uartData[ 10 ] );
uartData[ 12 ] = 'D';
i2cDAC_Get( 3, &uartData[ 13 ] );
net_ext_send( uartData, 15 );
}
}
else if( runData[ runIndex + 1 ] == 'G' )
{
if( runData[ runIndex + 2 ] == 'S' )
{
if( runData[ runIndex + 3 ] == 0 ) // Off
{
UARTPuts( "** GPIO OFF\n\r", -1 );
i2cGPIO_Off( 0, 1 << runData[ runIndex + 4 ] );
}
else if( runData[ runIndex + 3 ] == 1 ) // On
{
UARTPuts( "** GPIO ON\n\r", -1 );
i2cGPIO_On( 0, 1 << runData[ runIndex + 4 ] );
}
}
else if( runData[ runIndex + 2 ] == 'G' )
{
UARTPuts( "** GPIO GET\n\r", -1 );
uartData[ 0 ] = 5;
uartData[ 1 ] = 'G';
uartData[ 2 ] = 'G';
i2cGPIO_Get( &uartData[ 3 ] );
net_ext_send( uartData, 5 );
}
}
else if( runData[ runIndex + 1 ] == 'U' )
{
UARTPuts( "** UART\n\r", -1 );
i2cUART_Send( &(runData[ runIndex + 2 ]), 6 );
}
runCommand -= runData[ runIndex + 0 ];
if( runCommand > 0 )
{
runIndex += runData[ runIndex + 0 ];
}
else
{
runIndex = 0;
}
LedOff( USER_LED_1 );
}
result = i2cUART_Recv( &( uartData[ 2 ] ), 30 );
if( result > 0 )
{
UARTPuts( "** UART Recv: ", -1 );
for( index = 0; index < result; ++index )
{
UARTPutHexNum( uartData[ index + 2 ] );
UARTPutc( ' ' );
}
UARTPutc( '\n' );
UARTPutc( '\r' );
// Return the stage position info to the GUI
if( uartData[ 3 ] == 0x0A ||
uartData[ 3 ] == 0x3C )
{
uartData[ 0 ] = result + 2;
uartData[ 1 ] = 'U';
net_ext_send( uartData, result + 2 );
}
}
}
}
/*
** Check for any change in ethernet link status. If so, update
** ip address
*/
static void EnetStatusCheckNUpdate(void)
{
unsigned int linkFlag = FALSE;
static unsigned int prevEnState = 0;
static unsigned int nxtEnState = 1;
if(prevEnState != nxtEnState)
{
if(!EnetIfIsUp())
{
ContextReset();
linkFlag = FALSE;
EnetHttpServerInit( IPAddress );
if(ipAddr)
{
linkFlag = TRUE;
prevEnState = 1;
}
}
else
{
if(EnetLinkIsUp())
{
linkFlag = TRUE;
nxtEnState = 1;
}
else
{
ContextReset();
linkFlag = FALSE;
prevEnState = 0;
nxtEnState = 0;
}
}
if((TRUE == linkFlag) && (ipAddr != 0))
{
prevEnState = 1;
UARTPuts("\n\rAccess the home page using http://", -1);
IpAddrDisplay();
UARTPuts("/index.html \n\r", -1);
LedOn( USER_LED_4 );
}
else
{
UARTPuts("\n\rNetwork Connection failed.\n\r", -1);
LedOff( USER_LED_4 );
}
}
else
{
if(EnetLinkIsUp())
{
nxtEnState = 1;
}
else
{
nxtEnState = 0;
}
}
}
/*
** Dummy ISR to handle spurious interrupts
*/
static void dummyIsr(void)
{
; /* Perform nothing */
}
/****************************** End of file *********************************/