]> rtime.felk.cvut.cz Git - fpga/virtex2/uart.git/blob - software/main.c
+ README
[fpga/virtex2/uart.git] / software / main.c
1 /**
2 This is a sample application for openMSP430 softcore MCU with external HW UART
3 periphery <git@rtime.felk.cvut.cz:fpga/uart> and quadcount and pwm periphery.
4
5 First of all "Hello world" is printed and then application works like echo and
6 also prints quadrature count whenever its value is changed.
7
8 AB error of quadrature counter periphery is handled by irq routine which prints
9 message "IRQ: QCounter AB error!".
10
11 In endless main loop, when nothing else is doing, triangle singnal is being 
12 generated by the PWM periphery.
13
14 Baudrate is set to 115200.
15 */
16
17 #include "hardware.h"
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "uart.h"
21
22
23 /**
24 QuadCounter value
25 */
26 inline uint32_t qcount() {
27   uint32_t result = 0;
28
29   result |= QCNTL;
30   result |= ((uint32_t)QCNTH << 16);
31
32   return result;
33 }
34
35 /**
36 Handling of QuadCounter IRQ
37 */
38 interrupt(QCNT_VECTOR) qcount_isr() {
39   puts("IRQ: QCounter AB error!");
40   qcount();
41 }
42
43
44 /**
45 Print bits of byte in format b'xxxxxxxx'
46 */
47 void print_byte_flags(char flags) {
48   int i;
49
50   putchar('b');
51   putchar('\'');
52
53   for (i = 8;  i != 0;  i--) {
54     if ((flags & 0x80) != 0) {
55       putchar('1');
56     } else {
57       putchar('0');
58     }
59
60     flags = flags << 1;
61   }
62
63   putchar('\'');
64   putchar('\n');
65 }
66
67
68 /**
69 Delay function.
70 */
71 void delay(unsigned long int a, unsigned long int b) {
72   unsigned long int i;
73
74   while (a--) {
75     i = b;
76     while (i--) {
77       nop();
78       nop();
79     }
80   }
81 }
82
83
84 /**
85 Main function with init and an endless loop.
86 */
87 int main(void) {
88     uint32_t qcnt = qcount();
89     uint32_t qcnt_new;
90
91     int sign = 1;
92
93     //UBAUD = 0x04E1;                     //24.00MHz - 9600 baud
94     UBAUD = 0x0067;                     //24.00MHz - 115200 baud
95
96     eint();                             //enable interrupts
97
98
99     puts("Hello world\n");
100
101     for (;;) {
102       while (!isRxEmpty()) {
103         putchar(URX);
104       }
105
106       qcnt_new = qcount();
107       if (qcnt != qcnt_new) {
108         printf("[QCount = 0x%08lX]\n", qcnt_new);
109         qcnt = qcnt_new;
110       }
111       
112       if (sign) {
113         if (++PWM == 0xFFFF) sign = 0;
114       } else {
115         if (--PWM == 0x0000) sign = 1;
116       }
117     }
118
119 }
120