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 191

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

Line No. Rev Author Line
1 143 olivier.gi
#include "omsp_system.h"
2 136 olivier.gi
#include "hardware.h"
3
#include <stdlib.h>
4
#include <stdio.h>
5
 
6
//--------------------------------------------------//
7
//                   Delay function                 //
8
//--------------------------------------------------//
9
void delay(unsigned int d) {
10
   while(d--) {
11
      nop();
12
      nop();
13
   }
14
}
15
 
16
//--------------------------------------------------//
17
//                 putChar function                 //
18
//            (Send a byte to the UART)             //
19
//--------------------------------------------------//
20
int putchar (int txdata) {
21
 
22
  // Wait until the TX buffer is not full
23
  while (UART_STAT & UART_TX_FULL);
24
 
25
  // Write the output character
26
  UART_TXD = txdata;
27
 
28
  return 0;
29
}
30
 
31
//--------------------------------------------------//
32
//        UART RX interrupt service routine         //
33
//         (receive a byte from the UART)           //
34
//--------------------------------------------------//
35
volatile char rxdata;
36
 
37
wakeup interrupt (UART_RX_VECTOR) INT_uart_rx(void) {
38
 
39
  // Read the received data
40
  rxdata = UART_RXD;
41
 
42
  // Clear the receive pending flag
43
  UART_STAT = UART_RX_PND;
44
 
45
  // Exit the low power mode
46
  LPM0_EXIT;
47
}
48
 
49
 
50
//--------------------------------------------------//
51
// Main function with init an an endless loop that  //
52
// is synced with the interrupts trough the         //
53
// lowpower mode.                                   //
54
//--------------------------------------------------//
55
int main(void) {
56
    int reading = 0;
57
    int pos = 0;
58
    char buf[40];
59
    int led = 0;
60
 
61
    WDTCTL = WDTPW | WDTHOLD;           // Init watchdog timer
62
 
63
    P3DIR  = 0xff;
64
    P3OUT  = 0xff;                      // Light LED during init
65
 
66
    UART_BAUD = BAUD;                   // Init UART
67
    UART_CTL  = UART_EN | UART_IEN_RX;
68
 
69
    //    delay(65535);                       // Some delay
70
    //delay(65535);
71
 
72
    P3OUT  = 0x00;                      // Switch off LED
73
 
74
    printf("\r\n====== openMSP430 in action ======\r\n");   //say hello
75
    printf("\r\nSimple Line Editor Ready\r\n");
76
 
77
    eint();                             // Enable interrupts
78
 
79
    while (1) {                         //main loop, never ends...
80
 
81
        printf("> ");                   //show prompt
82
        reading = 1;
83
        while (reading) {               //loop and read characters
84
 
85
            LPM0;                       //sync, wakeup by irq
86
 
87
            led++;                      // Some lighting...
88
            if (led==9) {
89
              led = 0;
90
            }
91
            P3OUT = (0x01 << led);
92
 
93
            switch (rxdata) {
94
                //process RETURN key
95
                case '\r':
96
                //case '\n':
97
                    printf("\r\n");     //finish line
98
                    buf[pos++] = 0;     //to use printf...
99
                    printf(":%s\r\n", buf);
100
                    reading = 0;        //exit read loop
101
                    pos = 0;            //reset buffer
102
                    break;
103
                //backspace
104
                case '\b':
105
                    if (pos > 0) {      //is there a char to delete?
106
                        pos--;          //remove it in buffer
107
                        putchar('\b');  //go back
108
                        putchar(' ');   //erase on screen
109
                        putchar('\b');  //go back
110
                    }
111
                    break;
112
                //other characters
113
                default:
114
                    //only store characters if buffer has space
115
                    if (pos < sizeof(buf)) {
116
                        putchar(rxdata);     //echo
117
                        buf[pos++] = rxdata; //store
118
                    }
119
            }
120
        }
121
    }
122
}
123
 

powered by: WebSVN 2.1.0

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