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 212

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 212 olivier.gi
      __nop();
12
      __nop();
13 136 olivier.gi
   }
14
}
15
 
16
//--------------------------------------------------//
17 212 olivier.gi
//                 tty_putc function                 //
18 136 olivier.gi
//            (Send a byte to the UART)             //
19
//--------------------------------------------------//
20 212 olivier.gi
int tty_putc (int txdata) {
21 136 olivier.gi
 
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 212 olivier.gi
 
45 136 olivier.gi
  // 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 212 olivier.gi
 
61 136 olivier.gi
    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 212 olivier.gi
    cprintf("\r\n====== openMSP430 in action ======\r\n");   //say hello
75
    cprintf("\r\nSimple Line Editor Ready\r\n");
76
 
77 136 olivier.gi
    eint();                             // Enable interrupts
78 212 olivier.gi
 
79 136 olivier.gi
    while (1) {                         //main loop, never ends...
80
 
81 212 olivier.gi
        cprintf("> ");                   //show prompt
82 136 olivier.gi
        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 212 olivier.gi
                    cprintf("\r\n");    //finish line
98
                    buf[pos++] = 0;     //to use cprintf...
99
                    cprintf(":%s\r\n", buf);
100 136 olivier.gi
                    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 212 olivier.gi
                        tty_putc('\b');  //go back
108
                        tty_putc(' ');   //erase on screen
109
                        tty_putc('\b');  //go back
110 136 olivier.gi
                    }
111
                    break;
112
                //other characters
113
                default:
114
                    //only store characters if buffer has space
115
                    if (pos < sizeof(buf)) {
116 212 olivier.gi
                        tty_putc(rxdata);     //echo
117 136 olivier.gi
                        buf[pos++] = rxdata; //store
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.