]> rtime.felk.cvut.cz Git - fpga/virtex2/uart.git/blob - software/main.c
Peripheral connected to the quadcount module and an incremental encoder. Interrupt...
[fpga/virtex2/uart.git] / software / main.c
1 /*
2 see README.txt for details.
3
4 original by: chris <cliechti@gmx.net>
5
6 modified by: Vladimir Burian <buriavl2@fel.cvut.cz>
7 */
8 #include "hardware.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "swuart.h"
12 #include "fll.h"
13
14 /**
15 QuadCounter value
16 */
17 inline uint32_t qcount() {
18   uint32_t result = 0;
19   
20   result |= QCNTL;
21   result |= ((uint32_t)QCNTH << 16);
22   
23   return result;
24 }
25
26 /**
27 Handling of QuadCounter IRQ
28 */
29 interrupt(QCNT_VECTOR) qcount_isr() {
30   printf("[QCount = 0x%08lX]\n", qcount() >> 2);
31 }
32
33
34 /**
35 Delay function.
36 */
37 void delay(unsigned int d) {
38    while(d--) {
39       nop();
40       nop();
41    }
42 }
43
44 /**
45 Main function with init an an endless loop that is synced with the
46 interrupts trough the lowpower mode.
47 */
48 int main(void) {
49     int reading = 0;
50     int pos = 0;
51     char buf[40];
52     int led = 0;
53     
54     WDTCTL = WDTCTL_INIT;               //Init watchdog timer
55
56     P1OUT  = P1OUT_INIT;                //Init output data of port1
57     P1SEL  = P1SEL_INIT;                //Select port or module -function on port1
58     P1DIR  = P1DIR_INIT;                //Init port direction register of port1
59     P1IES  = P1IES_INIT;                //init port interrupts
60     P1IE   = P1IE_INIT;
61
62     P2OUT  = P2OUT_INIT;                //Init output data of port2
63     P2SEL  = P2SEL_INIT;                //Select port or module -function on port2
64     P2DIR  = P2DIR_INIT;                //Init port direction register of port2
65     P2IES  = P2IES_INIT;                //init port interrupts
66     P2IE   = P2IE_INIT;
67
68     P3DIR  = 0xff;
69     P3OUT  = 0xff;                      //light LED during init
70     delay(65535);                       //Wait for watch crystal startup
71     delay(65535);
72 //  fllInit();                          //Init FLL to desired frequency using the 32k768 cystal as reference.
73     P3OUT  = 0x00;                      //switch off LED
74     
75     TACTL  = TACTL_AFTER_FLL;           //setup timer (still stopped)
76     CCTL0  = CCIE|CAP|CM_2|CCIS_1|SCS;  //select P2.2 with UART signal
77     CCTL1  = 0;                         //
78     CCTL2  = 0;                         //
79     TACTL |= MC1;                       //start timer
80     
81     eint();                             //enable interrupts
82     
83     printf("\r\n====== openMSP430 in action ======\r\n");   //say hello
84     printf("\r\nSimple Line Editor Ready\r\n");   //say hello
85     
86     printf("\n[QCount = 0x%08lX]\n", qcount());
87     
88     while (1) {                         //main loop, never ends...
89         printf("> ");                   //show prompt
90         reading = 1;
91         while (reading) {               //loop and read characters
92             LPM0;                       //sync, wakeup by irq
93
94             led++;                      // Some lighting...
95             if (led==9) {
96               led = 0;
97             }
98             P3OUT = (0x01 << led);
99
100             switch (rxdata) {
101                 //process RETURN key
102                 case '\r':
103                 //case '\n':
104                     printf("\r\n");     //finish line
105                     buf[pos++] = 0;     //to use printf...
106                     printf(":%s\r\n", buf);
107                     reading = 0;        //exit read loop
108                     pos = 0;            //reset buffer
109                     break;
110                 //backspace
111                 case '\b':
112                     if (pos > 0) {      //is there a char to delete?
113                         pos--;          //remove it in buffer
114                         putchar('\b');  //go back
115                         putchar(' ');   //erase on screen
116                         putchar('\b');  //go back
117                     }
118                     break;
119                 //other characters
120                 default:
121                     //only store characters if buffer has space
122                     if (pos < sizeof(buf)) {
123                         putchar(rxdata);     //echo
124                         buf[pos++] = rxdata; //store
125                     }
126             }
127         }
128     }
129 }
130