]> rtime.felk.cvut.cz Git - fpga/virtex2/uart.git/blob - software/main.c
Added custom openMSP430 peripheral as an interface to the quadcount module.
[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 Delay function.
28 */
29 void delay(unsigned int d) {
30    while(d--) {
31       nop();
32       nop();
33    }
34 }
35
36 /**
37 Main function with init an an endless loop that is synced with the
38 interrupts trough the lowpower mode.
39 */
40 int main(void) {
41     int reading = 0;
42     int pos = 0;
43     char buf[40];
44     int led = 0;
45     
46     WDTCTL = WDTCTL_INIT;               //Init watchdog timer
47
48     P1OUT  = P1OUT_INIT;                //Init output data of port1
49     P1SEL  = P1SEL_INIT;                //Select port or module -function on port1
50     P1DIR  = P1DIR_INIT;                //Init port direction register of port1
51     P1IES  = P1IES_INIT;                //init port interrupts
52     P1IE   = P1IE_INIT;
53
54     P2OUT  = P2OUT_INIT;                //Init output data of port2
55     P2SEL  = P2SEL_INIT;                //Select port or module -function on port2
56     P2DIR  = P2DIR_INIT;                //Init port direction register of port2
57     P2IES  = P2IES_INIT;                //init port interrupts
58     P2IE   = P2IE_INIT;
59
60     P3DIR  = 0xff;
61     P3OUT  = 0xff;                      //light LED during init
62     delay(65535);                       //Wait for watch crystal startup
63     delay(65535);
64 //  fllInit();                          //Init FLL to desired frequency using the 32k768 cystal as reference.
65     P3OUT  = 0x00;                      //switch off LED
66     
67     TACTL  = TACTL_AFTER_FLL;           //setup timer (still stopped)
68     CCTL0  = CCIE|CAP|CM_2|CCIS_1|SCS;  //select P2.2 with UART signal
69     CCTL1  = 0;                         //
70     CCTL2  = 0;                         //
71     TACTL |= MC1;                       //start timer
72     
73     eint();                             //enable interrupts
74     
75     printf("\r\n====== openMSP430 in action ======\r\n");   //say hello
76     printf("\r\nSimple Line Editor Ready\r\n");   //say hello
77     
78     printf("\n[QCount = 0x%08lX]\n", qcount());
79     
80     while (1) {                         //main loop, never ends...
81         printf("> ");                   //show prompt
82         reading = 1;
83         while (reading) {               //loop and read characters
84             LPM0;                       //sync, wakeup by irq
85
86             led++;                      // Some lighting...
87             if (led==9) {
88               led = 0;
89             }
90             P3OUT = (0x01 << led);
91
92             switch (rxdata) {
93                 //process RETURN key
94                 case '\r':
95                 //case '\n':
96                     printf("\r\n");     //finish line
97                     buf[pos++] = 0;     //to use printf...
98                     printf(":%s\r\n", buf);
99                     reading = 0;        //exit read loop
100                     pos = 0;            //reset buffer
101                     break;
102                 //backspace
103                 case '\b':
104                     if (pos > 0) {      //is there a char to delete?
105                         pos--;          //remove it in buffer
106                         putchar('\b');  //go back
107                         putchar(' ');   //erase on screen
108                         putchar('\b');  //go back
109                     }
110                     break;
111                 //other characters
112                 default:
113                     //only store characters if buffer has space
114                     if (pos < sizeof(buf)) {
115                         putchar(rxdata);     //echo
116                         buf[pos++] = rxdata; //store
117                     }
118             }
119         }
120     }
121 }
122