]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-lpc21xx/test/test_uart/uart_test.c
8b3785a4cc3407d81312aa82ec85774e173a3e0b
[sysless.git] / arch / arm / mach-lpc21xx / test / test_uart / uart_test.c
1 #include <LPC210x.h>
2 #include "uartx.h"
3
4
5
6 /**
7  *  Function Name: lowInit()
8  *
9 * Description:
10  *    This function starts up the PLL then sets up the GPIO pins before
11  *    waiting for the PLL to lock.  It finally engages the PLL and
12  *    returns
13  *
14 * Calling Sequence: 
15  *    void
16  *
17 * Returns:
18  *    void
19  *  
20  */
21 static void lowInit(void)
22 {
23     // set PLL multiplier & divisor.
24     // values computed from config.h
25     PLLCFG = PLLCFG_MSEL | PLLCFG_PSEL;
26
27     // enable PLL
28     PLLCON = PLLCON_PLLE;
29     PLLFEED = 0xAA;                       // Make it happen.  These two updates
30     PLLFEED = 0x55;                       // MUST occur in sequence.
31
32     // setup the parallel port pin
33     IOCLR = PIO_ZERO_BITS;                // clear the ZEROs output
34     IOSET = PIO_ONE_BITS;                 // set the ONEs output
35     IODIR = PIO_OUTPUT_BITS;              // set the output bit direction
36
37     // wait for PLL lock
38     while (!(PLLSTAT & PLLSTAT_LOCK))
39         continue;
40
41     // enable & connect PLL
42     PLLCON = PLLCON_PLLE | PLLCON_PLLC;
43     PLLFEED = 0xAA;                       // Make it happen.  These two updates
44     PLLFEED = 0x55;                       // MUST occur in sequence.
45
46     // setup & enable the MAM
47     MAMTIM = MAMTIM_CYCLES;
48     MAMCR = MAMCR_FULL;
49
50     // set the peripheral bus speed
51     // value computed from config.h
52     VPBDIV = VPBDIV_VALUE;                // set the peripheral bus clock speed
53 }
54
55
56 /**
57  *  Function Name: sysInit()
58  *
59 * Description:
60  *    This function is responsible for initializing the program
61  *    specific hardware
62  *
63 * Calling Sequence: 
64  *    void
65  *
66 * Returns:
67  *    void
68  *  
69  */
70 static void sysInit(void)
71 {
72     lowInit();                            // setup clocks and processor port pins
73
74     MEMMAP = 1;              // map interrupt vectors space into FLASH
75
76     VICIntEnClear = 0xFFFFFFFF;           // clear all interrupts
77     VICIntSelect = 0x00000000;            // clear all FIQ selections
78     VICDefVectAddr = (uint32_t)reset;     // point unvectored IRQs to reset()
79
80 }
81
82
83
84 /**
85  * Creates a delay
86  * @param d duration (unit not defined yet)
87  */
88 void
89         delay(int d)
90 {
91     volatile int x;
92     int i;
93     for (i = 0; i < 10; i++)
94         for(x = d; x; --x)
95             ;
96 }
97
98 #define NUM_LEDS 4
99 static int leds[] = { 0x10000, 0x20000, 0x40000, 0x80000 };
100
101 /*////////////////////////////LEDS INITIALISATION///////////////////////*/
102
103 /**
104  * Initializes leds.
105  */
106 static void ledInit()
107 {
108     IODIR |= 0x000F0000; /*leds connected to P0.16 17 18 & 19 should blink*/
109     IOSET = 0x00000000;   /* all leds are switched off */
110 }
111
112 /**
113  * Switches the leds off.
114  * @param led  switched off led number. (integer)
115  */
116 static void ledOff(int led)  /* Ioclr.i =1   =>   IOset.i cleared */
117 {
118     IOCLR = led;
119 }
120
121 /**
122  * Switches the leds on. 
123  * @param led  switched on led number. (integer)
124  */
125 static void  ledOn(int led)  /*  Ioset.i = 1   =>  P0.i = 1    */
126 {
127     IOSET = led;
128 }
129
130
131
132
133 /**
134  * this program uses UART1 to print hello as a test word
135  * if everything is correct, leds 0 and 1 should be switched on
136  * then the program ask to type a word and print it continuously (led 0 blinks at each write)
137  * @param  
138  * @return 
139  */
140 int main(void)
141 {
142     int i = 0;
143     int written_int_word = -1;    
144     char *testword = "hello";
145     
146     sysInit();
147     
148     ledInit();
149     ledOn(leds[0]);
150     
151         
152     uartInit(UART_SEL1, B9600 , UART_8N1, UART_FIFO_8);
153  
154     
155     ledOn(leds[1]);
156        
157         uartPutch(UART_SEL1, '\n');
158         uartPuts( UART_SEL1, testword);
159         
160         uartPuts( UART_SEL1, "\nPlease write one character:");
161         
162         uartPutch(UART_SEL1, '\n');
163         while(written_int_word == -1)
164             written_int_word = uartGetch(UART_SEL1);
165         
166         
167         while(1)
168         {  
169             if (i++ & 1) ledOn(leds[0]);
170             else ledOff(leds[0]);
171                 uartPutch(UART_SEL1, written_int_word);
172                 delay(200000);
173               
174         }
175     
176     return 0;
177     }