OpenCores
URL https://opencores.org/ocsvn/Aquarius/Aquarius/trunk

Subversion Repositories Aquarius

[/] [Aquarius/] [trunk/] [application/] [shc_monitor_release_v1/] [main.c] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 thorn_aitc
//===============================
2
// Monitor Program
3
//-------------------------------
4
// for VirtexE Evaluation Board
5
// May.1 2003 Ver.1
6
//===============================
7
 
8
//======================================================
9
// Address Map
10
//======================================================
11
// address           sz  wt wd device
12
// 00000000-00001FFF  8K 0  32 ROM (code)
13
// 00002000-00003DFF  6K 0  32 RAM (data)
14
// 00003E00-00003FFF 512 0  32 RAM (stack)
15
//
16
// 00000000-0000FFFF 64K 0  32 RAM (shadow every 16KB)
17
// 00010000-0001FFFF 64K 3  32 RAM (shadow every 16KB)
18
// 00020000-0002FFFF 64K 0  16 RAM (shadow every 16KB)
19
// 00030000-0003FFFF 64K 3  16 RAM (shadow every 16KB)
20
// 00040000-ABCCFFFF (shadow RAM)
21
// ABCD0000-ABCD00FF 256 3  32 PIO (shadow every 4B)
22
// ABCD0100-ABCD01FF 256 3  32 UART(shadow every 4B)
23
// ABCD0200-ABCD02FF 256 3  32 SYS (shadow every 8B)
24
// ABCD0300-FFFBFFFF (shadow RAM)
25
// FFFC0000-FFFCFFFF 64K 0  32 RAM (shadow every 16KB)
26
// FFFD0000-FFFDFFFF 64K 3  32 RAM (shadow every 16KB)
27
// FFFE0000-FFFEFFFF 64K 0  16 RAM (shadow every 16KB)
28
// FFFF0000-FFFFFFFF 64K 3  16 RAM (shadow every 16KB)
29
// 
30
//======================================================
31
// PORT OUTPUT
32
//======================================================
33
// ABCD0000 : reserved
34
//   31    30    29    28    27    26    25    24
35
//    7     6     5     4     3     2     1     0
36
//  -----------------------------------------------
37
// |     |     |     |     |     |     |     |     |
38
//  -----------------------------------------------;
39
// 
40
// ABCD0001 : KEYYO (Key SCAN Y-axis out)
41
//   23    22    21    20    19    18    17    16
42
//    7     6     5     4     3     2     1     0
43
//  -----------------------------------------------
44
// |     |     |     | KY4 | KY3 | KY2 | KY1 | KY0 |
45
//  -----------------------------------------------
46
// 
47
// ABCD0002 : LCDCON (LCD Control Signal)
48
//   15    14    13    12    11    10     9     8
49
//    7     6     5     4     3     2     1     0
50
//  -----------------------------------------------
51
// |     |     |     |     |     |  E  | R/W |  RS |
52
//  -----------------------------------------------
53
// 
54
// ABCD0003 : LCDOUT (Write Data to LCD)
55
//    7     6     5     4     3     2     1     0
56
//    7     6     5     4     3     2     1     0
57
//  -----------------------------------------------
58
// | DW7 | DW6 | DW5 | DW4 | DW3 | DW2 | DW1 | DW0 |
59
//  -----------------------------------------------
60
// 
61
//======================================================
62
// PORT INPUT
63
//======================================================
64
// ABCD0000 : reserved
65
//   31    30    29    28    27    26    25    24
66
//    7     6     5     4     3     2     1     0
67
//  -----------------------------------------------
68
// |     |     |     |     |     |     |     |     |
69
//  -----------------------------------------------;
70
// 
71
// ABCD0001 : KEYXI (Key SCAN X-axis in)
72
//   23    22    21    20    19    18    17    16
73
//    7     6     5     4     3     2     1     0
74
//  -----------------------------------------------
75
// |     |     |     | KX4 | KX3 | KX2 | KX1 | KX0 |
76
//  -----------------------------------------------
77
// 
78
// ABCD0002 : reserved
79
//  15    14    13    12    11    10     9     8
80
//    7     6     5     4     3     2     1     0
81
//  -----------------------------------------------
82
// |     |     |     |     |     |     |     |    |
83
//  -----------------------------------------------
84
// 
85
// ABCD0003 : LCDIN (Read Data from LCD)
86
//    7     6     5     4     3     2     1     0
87
//    7     6     5     4     3     2     1     0
88
//  -----------------------------------------------
89
// | DR7 | DR6 | DR5 | DR4 | DR3 | DR2 | DR1 | DR0 |
90
//  -----------------------------------------------
91
// 
92
//======================================================
93
// UART: SASC (www.opencores.com)
94
// Simple Asynchronous Serial Communication Device
95
//======================================================
96
// ABCD0100 : UARTBG0 Baud Rate Generator Div0 (R/W)
97
//   31    30    29    28    27    26    25    24
98
//    7     6     5     4     3     2     1     0
99
//  -----------------------------------------------
100
// | B07 | B06 | B05 | B04 | B03 | B02 | B01 | B00 |
101
//  -----------------------------------------------
102
// 
103
// ABCD0101 : UARTBG1 Baud Rate Generator Div1 (R/W)
104
//   23    22    21    20    19    18    17    16
105
//    7     6     5     4     3     2     1     0
106
//  -----------------------------------------------
107
// | B17 | B16 | B15 | B14 | B13 | B12 | B11 | B10 |
108
//  -----------------------------------------------
109
// 
110
// ABCD0102 : UARTCON (TXF=full_o, RXE=empty_o) (R only)
111
//   15    14    13    12    11    10     9      8
112
//    7     6     5     4     3     2     1      0
113
//  -----------------------------------------------
114
// |     |     |     |     |     |     | TXF | RXE |
115
//  -----------------------------------------------
116
// 
117
// ABCD0103 : UARTTXD(W only)/UARTRXD(R only)
118
//    7     6     5     4     3     2     1     0
119
//    7     6     5     4     3     2     1     0
120
//  -----------------------------------------------
121
// | TR7 | TR6 | TR5 | TR4 | TR3 | TR2 | TR1 | TR0 |
122
//  -----------------------------------------------
123
// 
124
//======================================================
125
// System Controller
126
// Interrupt and Exception Controller
127
//======================================================
128
// ABCD0200 : INTCTL Interrupt Control (32bit R/W only)
129
//   31    30    29    28    27    26    25    24
130
//  -----------------------------------------------
131
// |E_NMI|E_IRQ|E_CER|E_DER|E_MRS|     |TMRON|BRKON|
132
//  -----------------------------------------------
133
//   23    22    21    20    19    18    17    16
134
//  -----------------------------------------------
135
// |ILVL3|ILVL2|ILVL1|ILVL0|IVEC7|IVEC6|IVEC5|IVEC4|
136
//  ----------------------------------------------- 
137
//   15    14    13    12    11    10     9     8
138
//  -----------------------------------------------
139
// |IVEC3|IVEC2|IVEC1|IVEC0|TMR11|TMR10|TMR09|TMR08|
140
//  -----------------------------------------------  
141
//    7     6     5     4     3     2     1     0
142
//  -----------------------------------------------
143
// |TMR07|TMR06|TMR05|TMR04|TMR03|TMR02|TMR01|TMR00|
144
//  ----------------------------------------------- 
145
//     bit31: E_NMI Emulate NMI Interrupt     (W only)
146
//     bit30: E_IRQ Emulate IRQ Interrupt     (W only)
147
//     bit29: E_CER Emulate CPU Address Error (W only)
148
//     bit28: E_DER Emulate DMA Address Error (W only)
149
//     bit27: E_MRS Emulate Manual Reset      (W only)                               
150
//     bit26: reserved
151
//     bit25: TMRON INTTMR OFF/ON             (0:OFF, 1:ON)
152
//     bit24: BRKON Address Break(NMI) OFF/ON (0:OFF, 1:ON)
153
//     bit23: ILVL3 IRQ ILevel 3
154
//     bit22: ILVL2 IRQ ILevel 2
155
//     bit21: ILVL1 IRQ ILevel 1
156
//     bit20: ILVL0 IRQ ILevel 0
157
//     bit19: IVEC7 IRQ Vector 7
158
//     bit18: IVEC6 IRQ Vector 6
159
//     bit17: IVEC5 IRQ Vector 5
160
//     bit16: IVEC4 IRQ Vector 4
161
//     bit15: IVEC3 IRQ Vector 3
162
//     bit14: IVEC2 IRQ Vector 2
163
//     bit13: IVEC1 IRQ Vector 1
164
//     bit12: IVEC0 IRQ Vector 0
165
//     bit11: TMR11 (12 bit interval timer to generate IRQ)
166
//     ...
167
//     bit 0: TMR00 (12 bit interval timer to generate IRQ)
168
// 
169
// ABCD0204 : BRKADR Break Address (16bit R/W only)
170
//   15    14    13    12    11    10     9     8
171
//  -----------------------------------------------
172
// |ADR15|ADR14|ADR13|ADR12|ADR11|ADR10|ADR09|ADR08|
173
//  -----------------------------------------------  
174
//    7     6     5     4     3     2     1     0
175
//  -----------------------------------------------
176
// |ADR07|ADR06|ADR05|ADR04|ADR03|ADR02|ADR01|ADR00|
177
//  ----------------------------------------------- 
178
//     bit15: ADR15 Break Address15
179
//     ...
180
//     bit00: ADR00 Break Address00
181
// 
182
 
183
#include "common.h"
184
 
185
//=================
186
// Define Functions
187
//=================
188
void utility(void);
189
unsigned char command(unsigned long *data);
190
unsigned long get_sformat(void);
191
unsigned char get_byte_rx_echo(void);
192
unsigned char hex2asc(unsigned char hex);
193
unsigned char asc2hex(unsigned char asc);
194
 
195
void          waitNms(unsigned char n);
196
unsigned char key_scan();
197
unsigned char key_wait_on(void);
198
void          key_wait_off(void);
199
 
200
void          uart_tx(unsigned char data);
201
unsigned char uart_rx(void);
202
unsigned char uart_rx_echo(void);
203
void          uart_rx_flush(void);
204
void          uart_set_baudrate(void);
205
 
206
void          lcd_disp_long(unsigned long data);
207
void          lcd_cursor(unsigned char con);
208
void          lcd_erase(unsigned char pos);
209
void          lcd_message(unsigned char pos, unsigned char *pstr);
210
void          lcd_disp(unsigned char chr);
211
void          lcd_pos(unsigned char pos);
212
void          lcd_ready(void);
213
unsigned char lcd_rd(int);
214
void          lcd_wr(int, unsigned char);
215
void          lcd_init(void);
216
 
217
//============
218
// IRQ Handler
219
//============
220
void irq_handler(void)
221
{
222
    unsigned char mes_irq[]  = "IRQ: Push to RTE";
223
 
224
    lcd_message(16, mes_irq);
225
    key_wait_off();
226
    key_wait_on();
227
    key_wait_off();
228
}
229
 
230
//============
231
// NMI Handler
232
//============
233
void nmi_handler(void)
234
{
235
    unsigned char mes_nmi[]  = "NMI/BRK:Goto Mon";
236
 
237
    SYS.INTCTL = SYS.INTCTL & 0xfeffffff; // BRK off
238
 
239
    lcd_message(16, mes_nmi);
240
    key_wait_off();
241
    key_wait_on();
242
    key_wait_off();
243
}
244
 
245
//=============
246
// TRAP Handler
247
//=============
248
void trap_handler(void)
249
{
250
    unsigned char mes_trap[] = "TRAP:Push to RTE";
251
 
252
    lcd_message(16, mes_trap);
253
    key_wait_off();
254
    key_wait_on();
255
    key_wait_off();
256
}
257
 
258
//====================
259
// Illegal Instruction
260
//====================
261
void illegal(void)
262
{
263
    unsigned char mes_ilgl[] = "Illegal:Goto Mon";
264
 
265
    lcd_message(16, mes_ilgl);
266
    key_wait_off();
267
    key_wait_on();
268
    key_wait_off();
269
}
270
 
271
//==============
272
// Address Error
273
//==============
274
void error(void)
275
{
276
    unsigned char mes_error[]= "AdrsErr:Goto Mon";
277
 
278
    lcd_message(16, mes_error);
279
    key_wait_off();
280
    key_wait_on();
281
    key_wait_off();
282
}
283
 
284
//=============
285
// Main Routine
286
//=============
287
void main_sh(void)
288
{
289
    unsigned char mes_welcome[]   = "SH-2 Monitor v.1";
290
    unsigned char mes_copyright[] = "(C) 2003 T.Aitch";
291
    unsigned char mes_memory[]    = "Memory--";
292
    unsigned char mes_run[]       = "Run---Good Luck!";
293
 
294
    unsigned char key;
295
    unsigned long adrs;
296
    unsigned long data;
297
    unsigned long data_edit;
298
 
299
    void (* userfunc)(void);
300
    long i;
301
 
302
    //-----------
303
    // Initialize
304
    //-----------
305
    lcd_init();
306
    uart_set_baudrate();
307
    uart_rx_flush();
308
    key_wait_off();
309
 
310
    //----------------
311
    // Welcome Message
312
    //----------------
313
    lcd_message( 0, mes_welcome);
314
    lcd_message(16, mes_copyright);
315
    for (i = 0 ; i < 2000000 ; i++);
316
 
317
    //----------
318
    // Clear LCD
319
    //----------
320
    key_wait_off();
321
    lcd_erase(0);
322
    lcd_erase(16);
323
 
324
    //----------------
325
    // Main Event Loop
326
    //----------------
327
    adrs = 0x00000000;
328
 
329
    while(1)
330
    {
331
        adrs = adrs & 0xfffffffc;
332
        data = * (unsigned long *) adrs;
333
        lcd_pos(0);
334
        lcd_disp_long(adrs);
335
        lcd_disp_long(data);
336
 
337
        lcd_message(16, mes_memory);
338
 
339
        data_edit = data;
340
        key = command(&data_edit);
341
 
342
        switch(key)
343
        {
344
            case KEYUTL: utility();
345
                         break;
346
            case KEYRUN: lcd_message(16, mes_run);
347
                         key_wait_off();
348
                         adrs = adrs & 0xfffffffe;
349
                         userfunc = (void (*)) adrs;
350
                         (* userfunc)();
351
                         break;
352
            case KEYGET: adrs = get_sformat();
353
                         break;
354
            case KEYPUT: break;
355
            case KEYADR: adrs = data_edit;
356
                         break;
357
            case KEYINC: adrs = adrs + 4;
358
                         break;
359
            case KEYDEC: adrs = adrs - 4;
360
                         break;
361
            case KEYDAT: * (unsigned long *) adrs = data_edit;
362
                         adrs = adrs + 4;
363
                         break;
364
            default:     break;
365
        }
366
    }
367
}
368
 
369
//****************************************************
370
//*                                                  *
371
//*                Service Utilities                 *
372
//*                                                  *
373
//****************************************************
374
 
375
//====================
376
// "Utility" Functions
377
//--------------------
378
//     Input  : none
379
//     Output : none
380
//====================
381
void utility(void)
382
{
383
    unsigned char mes_util[]   = "BRK-Func Select?";
384
    unsigned char mes_menu[]   = "1:REG  2:BRK SET";
385
    unsigned char mes_break[]  = "Set Break Point.";
386
    unsigned char mes_adrs[]   = "Address?";
387
    unsigned char mes_brkok[]  = "Break Accepted. ";
388
    unsigned char mes_brkng[]  = "Break Canceled. ";
389
    unsigned char mes_SR[]     = "SR     :";
390
    unsigned char mes_PC[]     = "PC     :";
391
    unsigned char mes_SP[]     = "SP(R15):";
392
    unsigned char mes_PR[]     = "PR     :";
393
    unsigned char mes_R0[]     = "R0     :";
394
    unsigned char mes_R1[]     = "R1     :";
395
    unsigned char mes_R2[]     = "R2     :";
396
    unsigned char mes_R3[]     = "R3     :";
397
    unsigned char mes_R4[]     = "R4     :";
398
    unsigned char mes_R5[]     = "R5     :";
399
    unsigned char mes_R6[]     = "R6     :";
400
    unsigned char mes_R7[]     = "R7     :";
401
    unsigned char mes_R8[]     = "R8     :";
402
    unsigned char mes_R9[]     = "R9     :";
403
    unsigned char mes_R10[]    = "R10    :";
404
    unsigned char mes_R11[]    = "R11    :";
405
    unsigned char mes_R12[]    = "R12    :";
406
    unsigned char mes_R13[]    = "R13    :";
407
    unsigned char mes_R14[]    = "R14    :";
408
    unsigned char mes_R15[]    = "R15    :";
409
    unsigned char mes_MACH[]   = "MACH   :";
410
    unsigned char mes_MACL[]   = "MACL   :";
411
    unsigned char mes_GBR[]    = "GBR    :";
412
    unsigned char mes_VBR[]    = "VBR    :";
413
    unsigned char key;
414
    unsigned long adrs;
415
 
416
    lcd_message(0, mes_util);
417
    lcd_message(16, mes_menu);
418
    key_wait_off();
419
 
420
    key = key_wait_on();
421
    if (key == KEY1)       // Register Watcher
422
    {
423
        key_wait_off();
424
        lcd_message(0,  mes_SR);   lcd_disp_long(REG.SR);
425
        lcd_message(16, mes_PC);   lcd_disp_long(REG.PC);
426
        key_wait_on();
427
        key_wait_off();
428
        lcd_message(0,  mes_SP);   lcd_disp_long(REG.SP);
429
        lcd_message(16, mes_PR);   lcd_disp_long(REG.PR);
430
        key_wait_on();
431
        key_wait_off();
432
        lcd_message(0,  mes_R0);   lcd_disp_long(REG.R0);
433
        lcd_message(16, mes_R1);   lcd_disp_long(REG.R1);
434
        key_wait_on();
435
        key_wait_off();
436
        lcd_message(0,  mes_R2);   lcd_disp_long(REG.R2);
437
        lcd_message(16, mes_R3);   lcd_disp_long(REG.R3);
438
        key_wait_on();
439
        key_wait_off();
440
        lcd_message(0,  mes_R4);   lcd_disp_long(REG.R4);
441
        lcd_message(16, mes_R5);   lcd_disp_long(REG.R5);
442
        key_wait_on();
443
        key_wait_off();
444
        lcd_message(0,  mes_R6);   lcd_disp_long(REG.R6);
445
        lcd_message(16, mes_R7);   lcd_disp_long(REG.R7);
446
        key_wait_on();
447
        key_wait_off();
448
        lcd_message(0,  mes_R8);   lcd_disp_long(REG.R8);
449
        lcd_message(16, mes_R9);   lcd_disp_long(REG.R9);
450
        key_wait_on();
451
        key_wait_off();
452
        lcd_message(0,  mes_R10);  lcd_disp_long(REG.R10);
453
        lcd_message(16, mes_R11);  lcd_disp_long(REG.R11);
454
        key_wait_on();
455
        key_wait_off();
456
        lcd_message(0,  mes_R12);  lcd_disp_long(REG.R12);
457
        lcd_message(16, mes_R13);  lcd_disp_long(REG.R13);
458
        key_wait_on();
459
        key_wait_off();
460
        lcd_message(0,  mes_R14);  lcd_disp_long(REG.R14);
461
        lcd_message(16, mes_R15);  lcd_disp_long(REG.R15);
462
        key_wait_on();
463
        key_wait_off();
464
        lcd_message(0,  mes_MACH); lcd_disp_long(REG.MACH);
465
        lcd_message(16, mes_MACL); lcd_disp_long(REG.MACL);
466
        key_wait_on();
467
        key_wait_off();
468
        lcd_message(0,  mes_GBR);  lcd_disp_long(REG.GBR);
469
        lcd_message(16, mes_VBR);  lcd_disp_long(REG.VBR);
470
        key_wait_on();
471
        key_wait_off();
472
    }
473
    else if (key == KEY2)  // Break Setting
474
    {
475
        lcd_message(0, mes_break);
476
        lcd_message(16, mes_adrs);
477
        adrs = SYS.BRKADR;
478
        key = command(&adrs);
479
        if (key == KEYDAT)
480
        {
481
            lcd_message(0, mes_brkok);
482
            key_wait_off();
483
            SYS.BRKADR = adrs;
484
            SYS.INTCTL = SYS.INTCTL | 0x01000000; // BRK on
485
        }
486
        else
487
        {
488
            lcd_message(0, mes_brkng);
489
            key_wait_off();
490
            SYS.INTCTL = SYS.INTCTL & 0xfeffffff; // BRK off
491
        }
492
    }
493
}
494
 
495
//=========================================
496
// Command Handler
497
//-----------------------------------------
498
//     Input  : data    = display long data
499
//     Output : command = input command
500
//              data    = input long data
501
//=========================================
502
unsigned char command(unsigned long *data)
503
{
504
    unsigned char key;
505
    int i;
506
 
507
    lcd_pos(24);
508
    lcd_disp_long(*data);
509
 
510
    lcd_cursor(1);
511
    i = 0;
512
 
513
    do
514
    {
515
        if (i == 0) lcd_pos(24);
516
 
517
        key_wait_off();
518
        key = key_wait_on();
519
        if (key <= KEYF)
520
        {
521
            lcd_disp(hex2asc(key));
522
            *data = (*data & ~((0x0000000F) << ((7-i) * 4))) | (key << ((7-i) * 4));
523
        }
524
 
525
        i = (i + 1) % 8;
526
    } while (key <= KEYF);
527
 
528
    lcd_cursor(0);
529
    return(key);
530
}
531
 
532
//=================================================
533
// Get S-Format
534
//-------------------------------------------------
535
//     Input  : none
536
//     Output : get_sformat = address of top-record
537
//=================================================
538
unsigned long get_sformat(void)
539
{
540
    unsigned char mes_get[]       = "Get S-Format(S3)";
541
    unsigned char mes_send[]      = "please send.....";
542
    unsigned char mes_receive[]   = "-----OK!";
543
    unsigned char mes_get_error[] = "ERR:Bad S-Format";
544
 
545
    int topflag;
546
    int endflag;
547
    unsigned char rc;
548
    unsigned char rxdata;
549
    unsigned long adrs;
550
    unsigned long adrs_record;
551
    unsigned long adrs_top = 0;
552
    int count;
553
    unsigned char checksum;
554
    int error;
555
    int i;
556
 
557
    error = 0;
558
    topflag = 1;
559
    endflag = 0;
560
 
561
    uart_rx_flush();
562
    lcd_message( 0, mes_get);
563
    lcd_message(16, mes_send);
564
 
565
    while(endflag == 0)
566
    {
567
        //
568
        // Wait 'S' character
569
        //
570
        while (uart_rx_echo() != 'S');
571
        //
572
        // Check Record Type
573
        //
574
        switch(rc = uart_rx_echo())
575
        {
576
            case '3': //-------- Record of 4 byte length address
577
            case '7': //-------- End of Record
578
                      //
579
                      // Get Record Length
580
                      //
581
                      rxdata = get_byte_rx_echo();
582
                      checksum = rxdata;
583
                      count = rxdata - 5;
584
                      //
585
                      // Get Record Address
586
                      //
587
                      adrs = 0;
588
                      for (i = 0 ; i < 4 ; i++)
589
                      {
590
                          rxdata = get_byte_rx_echo();
591
                          checksum = checksum + rxdata;
592
                          adrs  = (adrs << 8) + (unsigned long) rxdata;
593
                      }
594
                      adrs_record = adrs;
595
                      if (topflag)
596
                      {
597
                          adrs_top = adrs;
598
                          topflag = 0;
599
                      }
600
                      //
601
                      // Get Record Data and Save to memory
602
                      //
603
                      while(count > 0)
604
                      {
605
                          rxdata = get_byte_rx_echo();
606
                          checksum = checksum + rxdata;
607
                          * (unsigned char *) adrs = rxdata;
608
                          adrs = adrs + 1;
609
                          count = count - 1;
610
                      }
611
                      //
612
                      // Check checksum
613
                      //
614
                      rxdata = get_byte_rx_echo();
615
                      checksum = (~checksum) & 0x0ff;
616
                      error = (checksum != rxdata);
617
                      //
618
                      // Continue ? or Finish ?
619
                      //
620
                      if (rc == '3') // S3 format
621
                          break;
622
                      else           // S7 format
623
                      {
624
                          endflag = 1;
625
                          break;
626
                      }
627
            //
628
            // ignore another Record
629
            //
630
            default : continue;
631
        }
632
        if (error)
633
        {
634
            lcd_message(16, mes_get_error);
635
            rc = key_wait_on();
636
            break;
637
        }
638
        else
639
        {
640
            lcd_pos(16);
641
            lcd_disp_long(adrs_record);
642
            lcd_message(24, mes_receive);
643
        }
644
    }
645
    return(adrs_top);
646
}
647
 
648
//==============================================
649
// Get Byte from Rx with echo
650
//----------------------------------------------
651
//     Input  : none
652
//     Output : get_byte_rx_echo = received byte
653
//==============================================
654
unsigned char get_byte_rx_echo(void)
655
{
656
    unsigned char hex;
657
 
658
    hex = asc2hex(uart_rx_echo());
659
    hex = (hex << 4) + asc2hex(uart_rx_echo());
660
 
661
    return(hex);
662
}
663
 
664
//=============================
665
// Convert Hex(nibble) to Ascii
666
//-----------------------------
667
//     Input  : hex (0x00-0x0F)
668
//     Output : hex2asc
669
//=============================
670
unsigned char hex2asc(unsigned char hex)
671
{
672
    hex = hex & 0x0f;
673
    if (hex <= 0x09)
674
        return(hex + '0');
675
    else
676
        return(hex - 0x0a + 'A');
677
}
678
 
679
//=============================
680
// Convert Ascii to Hex(nibble)
681
//-----------------------------
682
//     Input  : asc(0-9, A-F)
683
//     Output : asc2hex
684
//=============================
685
unsigned char asc2hex(unsigned char asc)
686
{
687
    if (asc <= '9')
688
        return((asc - '0') & 0x0f);
689
    else
690
        return((asc - 'A' + 0x0a) & 0x0f);
691
}
692
 
693
//****************************************************
694
//*                                                  *
695
//*                 Key Utilities                    *
696
//*                                                  *
697
//****************************************************
698
 
699
//===================================
700
// Wait until Key On
701
//-----------------------------------
702
//     Input  : none
703
//     Output : key = pushed key code
704
//===================================
705
unsigned char key_wait_on(void)
706
{
707
    unsigned char key;
708
 
709
    while((key = key_scan()) == KEYNONE) waitNms(8);
710
 
711
    return(key);
712
}
713
 
714
//===================
715
// Wait until Key Off
716
//-------------------
717
//     Input  : none
718
//     Output : none
719
//===================
720
void key_wait_off(void)
721
{
722
    while(key_scan() != KEYNONE) waitNms(8);
723
}
724
 
725
//===================================
726
// Key Scan
727
//-----------------------------------
728
//     Input  : none
729
//     Output : key = pushed key code
730
//===================================
731
unsigned char key_scan()
732
{
733
    unsigned char keyx4;
734
    unsigned char keyx3;
735
    unsigned char keyx2;
736
    unsigned char keyx1;
737
    unsigned char keyx0;
738
 
739
    PORTO.KEYYO.BYTE = 0xef; // KEYY4
740
    waitNms(1);
741
    keyx4 = PORTI.KEYXI.BYTE & 0x1f;
742
 
743
    PORTO.KEYYO.BYTE = 0xf7; // KEYY3
744
    waitNms(1);
745
    keyx3 = PORTI.KEYXI.BYTE & 0x1f;
746
 
747
    PORTO.KEYYO.BYTE = 0xfb; // KEYY2
748
    waitNms(1);
749
    keyx2 = PORTI.KEYXI.BYTE & 0x1f;
750
 
751
    PORTO.KEYYO.BYTE = 0xfd; // KEYY1
752
    waitNms(1);
753
    keyx1 = PORTI.KEYXI.BYTE & 0x1f;
754
 
755
    PORTO.KEYYO.BYTE = 0xfe; // KEYY0
756
    waitNms(1);
757
    keyx0 = PORTI.KEYXI.BYTE & 0x1f;
758
 
759
    switch(keyx4)
760
    {
761
        case 0x17 : return(KEYPUT);
762
        case 0x1b : return(KEYGET);
763
        case 0x1d : return(KEYRUN);
764
        case 0x1e : return(KEYUTL);
765
    }
766
    switch(keyx3)
767
    {
768
        case 0x0f : return(KEYADR);
769
        case 0x17 : return(KEYF);
770
        case 0x1b : return(KEYE);
771
        case 0x1d : return(KEYD);
772
        case 0x1e : return(KEYC);
773
    }
774
    switch(keyx2)
775
    {
776
        case 0x0f : return(KEYINC);
777
        case 0x17 : return(KEYB);
778
        case 0x1b : return(KEYA);
779
        case 0x1d : return(KEY9);
780
        case 0x1e : return(KEY8);
781
    }
782
    switch(keyx1)
783
    {
784
        case 0x0f : return(KEYDEC);
785
        case 0x17 : return(KEY7);
786
        case 0x1b : return(KEY6);
787
        case 0x1d : return(KEY5);
788
        case 0x1e : return(KEY4);
789
    }
790
    switch(keyx0)
791
    {
792
        case 0x0f : return(KEYDAT);
793
        case 0x17 : return(KEY3);
794
        case 0x1b : return(KEY2);
795
        case 0x1d : return(KEY1);
796
        case 0x1e : return(KEY0);
797
    }
798
    return(KEYNONE);
799
}
800
 
801
//==================
802
// Wait n ms
803
//------------------
804
//     Input  : n ms
805
//     Output : none
806
//==================
807
void waitNms(unsigned char n)
808
{
809
    int  i, j;
810
    unsigned char dummy;
811
 
812
    for (i = 0 ; i < n ; i++)
813
    {
814
        // 1ms = 20000 * 50ns (20MHz)
815
        for (j = 0 ; i <= 10000 ; i++) dummy = PORTI.RESERVED_0;
816
    }
817
}
818
 
819
//****************************************************
820
//*                                                  *
821
//*                UART Utilities                    *
822
//*                                                  *
823
//****************************************************
824
 
825
//==============================
826
// Send Tx
827
// -----------------------------
828
//     Input  : data = send data
829
//     Output : none
830
//==============================
831
void uart_tx(unsigned char data)
832
{
833
    while(UART.UARTCON.BIT.TXF);
834
    UART.BYTE.TX = data;
835
}
836
 
837
//====================================
838
// Receive RX
839
// -----------------------------------
840
//     Input  : none
841
//     Output : uart_rx = receive data
842
//====================================
843
unsigned char uart_rx(void)
844
{
845
    while(UART.UARTCON.BIT.RXE);
846
    return(UART.BYTE.RX);
847
}
848
 
849
//=========================================
850
// Receive RX with echo to TX
851
// ----------------------------------------
852
//     Input  : none
853
//     Output : uart_rx_echo = receive data
854
//=========================================
855
unsigned char uart_rx_echo(void)
856
{
857
    unsigned char data;
858
 
859
    while(UART.UARTCON.BIT.RXE);
860
    data = UART.BYTE.RX;
861
 
862
    while(UART.UARTCON.BIT.TXF);
863
    UART.BYTE.TX = data;
864
 
865
    return(data);
866
}
867
 
868
//==================
869
// Flush RXD FIFO
870
//------------------
871
//     Input  : none
872
//     Output : none
873
//==================
874
void uart_rx_flush(void)
875
{
876
    unsigned char dummy;
877
 
878
    while(UART.UARTCON.BIT.RXE == 0) dummy = UART.BYTE.RX;
879
}
880
 
881
//==============================
882
// Set Baud Rate 9600bps
883
//------------------------------
884
//     9600*4=38.4KHz
885
//     20MHz/38.4KHz=520.8=20*26
886
//     (BRG0 + 2) = 20, BRG0=18 =0x12
887
//     (BRG1 + 1) = 26, BRG1=25 =0x19
888
//
889
//     4800*4=19.2KHz
890
//     20MHz/19.2KHz=1041.7=20*52
891
//     (BRG0 + 2) = 20, BRG0=18 =0x12
892
//     (BRG1 + 1) = 52, BRG1=51 =0x33
893
//
894
//     2400*4=9.6KHz
895
//     20MHz/9.6KHz=2083.3=20*104
896
//     (BRG0 + 2) = 20, BRG0=18 =0x12
897
//     (BRG1 + 1) =104, BRG1=103=0x67
898
//
899
//     1200*4=4.8KHz
900
//     20MHz/4.8KHz=4166.7=20*208
901
//     (BRG0 + 2) = 20, BRG0=18 =0x12
902
//     (BRG1 + 1) =208, BRG1=207=0xcf
903
//
904
//     Input  : none
905
//     Output : none
906
//==============================
907
void uart_set_baudrate(void)
908
{
909
    int i;
910
    unsigned char dummy;
911
 
912
    UART.UARTBG0 = 18;
913
    UART.UARTBG1 = 207;
914
 
915
    for (i = 0; i < 65536; i++) dummy = PORTI.RESERVED_0;
916
}
917
 
918
//****************************************************
919
//*                                                  *
920
//*                 LCD Utilities                    *
921
//*                                                  *
922
//****************************************************
923
 
924
//======================================
925
// LCD Display Long Hex data (8 digit)
926
//--------------------------------------
927
//     Input  : data = display long data
928
//     Output : none
929
//======================================
930
void lcd_disp_long(unsigned long data)
931
{
932
    int i;
933
    unsigned char digit;
934
 
935
    for (i = 7 ; i >= 0 ; i--)
936
    {
937
        digit = (unsigned char) (data / (1 << i * 4)) % 0x10;
938
        lcd_disp(hex2asc(digit));
939
    }
940
}
941
 
942
//================================
943
// Cursor On/Off
944
//--------------------------------
945
//     Input  : cur = off(0)/on(1)
946
//     Output : none
947
//================================
948
void lcd_cursor(unsigned char con)
949
{
950
    lcd_ready();
951
    if (con)
952
        lcd_wr(INST, 0x0d);
953
    else
954
        lcd_wr(INST, 0x0c);
955
}
956
 
957
 
958
//===============================
959
// Erase 1 line from the position
960
//-------------------------------
961
//     Input  : pos = position
962
//               1st line = 00-15
963
//               2nd line = 16-31
964
//     Ouput  : none
965
//===============================
966
void lcd_erase(unsigned char pos)
967
{
968
    int i;
969
 
970
    lcd_pos(pos);
971
    for (i = 0; i < 16; i++) lcd_disp(' ');
972
}
973
 
974
//========================================
975
// Print a Message from Current Position
976
//----------------------------------------
977
//     Input  : pstr = message top address
978
//     Outpur : none
979
//========================================
980
void lcd_message(unsigned char pos, unsigned char *pstr)
981
{
982
    unsigned char *pmessage;
983
 
984
    lcd_pos(pos);
985
    pmessage = pstr;
986
    while (*pmessage) lcd_disp(*pmessage++);
987
}
988
 
989
//==========================================
990
// Display One Character on Current Position
991
//------------------------------------------
992
//     Input  : chr = display charcter
993
//     Output : none
994
//==========================================
995
void lcd_disp(unsigned char chr)
996
{
997
    lcd_ready();
998
    lcd_wr(DATA, chr);
999
}
1000
 
1001
//================================
1002
// Set LCD Display Position
1003
//--------------------------------
1004
//     Input  : pos = position
1005
//                1st line = 00-15
1006
//                2nd line = 16-31
1007
//     Ouput  : none
1008
//================================
1009
void lcd_pos(unsigned char pos)
1010
{
1011
    unsigned char phypos;
1012
 
1013
    phypos = pos % 32;
1014
    if (phypos >= 16)
1015
        phypos = (phypos - 16) + 64;
1016
    phypos = phypos | 0x80;
1017
 
1018
    lcd_ready();
1019
    lcd_wr(INST, phypos);
1020
}
1021
 
1022
//=====================
1023
// Wait until LCD ready
1024
//---------------------
1025
//     Input  : none
1026
//     Output : none 
1027
//=====================
1028
void lcd_ready(void)
1029
{
1030
    while ((lcd_rd(INST) & 0x80) == 0x80);
1031
}
1032
 
1033
//=================================
1034
// LCD Read
1035
//---------------------------------
1036
//     Input  : rs (0=Instr,1=Data)
1037
//     Output : lcd_rd (Read Data) 
1038
//=================================
1039
unsigned char lcd_rd(int rs)
1040
{
1041
    int i;
1042
    unsigned char dummy;
1043
    unsigned char data;
1044
 
1045
    PORTO.LCDCON.BYTE = 0x02 | (rs & 0x01);
1046
 
1047
    // wait 60ns = 2cyc@20MHz
1048
    dummy = PORTI.RESERVED_0;
1049
    dummy = PORTI.RESERVED_0;
1050
 
1051
 
1052
    PORTO.LCDCON.BYTE = 0x06 | (rs & 0x01);
1053
 
1054
    // wait 500ns = 10cyc@20MHz
1055
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
1056
 
1057
    data = PORTI.LCDIN;
1058
 
1059
    PORTO.LCDCON.BYTE = 0x02 | (rs & 0x01);
1060
 
1061
    // wait 500ns = 10cyc@20MHz
1062
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
1063
 
1064
    return(data);
1065
}
1066
 
1067
//=================================
1068
// LCD Write
1069
//---------------------------------
1070
//     Input  : rs (0=Instr,1=Data)
1071
//              wd (Write Data)
1072
//     Output : none
1073
//=================================
1074
void lcd_wr(int rs, unsigned char wd)
1075
{
1076
    int i;
1077
    unsigned char dummy;
1078
 
1079
    PORTO.LCDOUT = wd;
1080
 
1081
    PORTO.LCDCON.BYTE = 0x00 | (rs & 0x01);
1082
 
1083
    // wait 60ns = 2cyc@20MHz
1084
    dummy = PORTI.RESERVED_0;
1085
    dummy = PORTI.RESERVED_0;
1086
 
1087
    PORTO.LCDCON.BYTE = 0x04 | (rs & 0x01);
1088
 
1089
    // wait 500ns = 10cyc@20MHz
1090
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
1091
 
1092
    PORTO.LCDCON.BYTE = 0x00 | (rs & 0x01);
1093
 
1094
    // wait 500ns = 10cyc@20MHz
1095
    for (i = 0 ; i <= 10 ; i++) dummy = PORTI.RESERVED_0;
1096
}
1097
 
1098
//==================
1099
// LCD Initialize
1100
//------------------
1101
//     Input  : none
1102
//     Output : none
1103
//==================
1104
void lcd_init(void)
1105
{
1106
    int i;
1107
    unsigned char dummy;
1108
 
1109
    lcd_wr(INST, 0x30);
1110
 
1111
    // wait 4100us=82000cyc@20MHz
1112
    for (i = 0 ; i < 82000 ; i++) dummy = PORTI.RESERVED_0;
1113
 
1114
    lcd_wr(INST, 0x30);
1115
 
1116
    // wait 100us=2000cyc@20MHz
1117
    for (i = 0 ; i < 2000 ; i++) dummy = PORTI.RESERVED_0;
1118
 
1119
    lcd_wr(INST, 0x30);
1120
 
1121
    // set function (8bit, 2lines)
1122
    lcd_ready();
1123
    lcd_wr(INST, 0x38);
1124
 
1125
    // display (disp on, cursor off, blink off)
1126
    lcd_ready();
1127
    lcd_wr(INST, 0x0c);
1128
 
1129
    // clear
1130
    lcd_ready();
1131
    lcd_wr(INST, 0x01);
1132
 
1133
    // entry mode (increment, no-shift)
1134
    lcd_ready();
1135
    lcd_wr(INST, 0x06);
1136
}
1137
 
1138
//===============
1139
// End of Program
1140
//===============

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.