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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [fpga/] [xilinx_diligent_s3board/] [software/] [hw_uart/] [main.c] - Blame information for rev 136

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

Line No. Rev Author Line
1 136 olivier.gi
#include "hardware.h"
2
#include <stdlib.h>
3
#include <stdio.h>
4
 
5
//--------------------------------------------------//
6
//                   Delay function                 //
7
//--------------------------------------------------//
8
void delay(unsigned int d) {
9
   while(d--) {
10
      nop();
11
      nop();
12
   }
13
}
14
 
15
//--------------------------------------------------//
16
//                 putChar function                 //
17
//            (Send a byte to the UART)             //
18
//--------------------------------------------------//
19
int putchar (int txdata) {
20
 
21
  // Wait until the TX buffer is not full
22
  while (UART_STAT & UART_TX_FULL);
23
 
24
  // Write the output character
25
  UART_TXD = txdata;
26
 
27
  return 0;
28
}
29
 
30
//--------------------------------------------------//
31
//        UART RX interrupt service routine         //
32
//         (receive a byte from the UART)           //
33
//--------------------------------------------------//
34
volatile char rxdata;
35
 
36
wakeup interrupt (UART_RX_VECTOR) INT_uart_rx(void) {
37
 
38
  // Read the received data
39
  rxdata = UART_RXD;
40
 
41
  // Clear the receive pending flag
42
  UART_STAT = UART_RX_PND;
43
 
44
  // Exit the low power mode
45
  LPM0_EXIT;
46
}
47
 
48
 
49
//--------------------------------------------------//
50
// Main function with init an an endless loop that  //
51
// is synced with the interrupts trough the         //
52
// lowpower mode.                                   //
53
//--------------------------------------------------//
54
int main(void) {
55
    int reading = 0;
56
    int pos = 0;
57
    char buf[40];
58
    int led = 0;
59
 
60
    WDTCTL = WDTPW | WDTHOLD;           // Init watchdog timer
61
 
62
    P3DIR  = 0xff;
63
    P3OUT  = 0xff;                      // Light LED during init
64
 
65
    UART_BAUD = BAUD;                   // Init UART
66
    UART_CTL  = UART_EN | UART_IEN_RX;
67
 
68
    //    delay(65535);                       // Some delay
69
    //delay(65535);
70
 
71
    P3OUT  = 0x00;                      // Switch off LED
72
 
73
    printf("\r\n====== openMSP430 in action ======\r\n");   //say hello
74
    printf("\r\nSimple Line Editor Ready\r\n");
75
 
76
    eint();                             // Enable interrupts
77
 
78
    while (1) {                         //main loop, never ends...
79
 
80
        printf("> ");                   //show prompt
81
        reading = 1;
82
        while (reading) {               //loop and read characters
83
 
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
 

powered by: WebSVN 2.1.0

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