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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [tests/] [uart/] [sim/] [uart-interrupt.c] - Blame information for rev 393

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

Line No. Rev Author Line
1 349 julius
/*
2
 * UART interrupt test
3
 *
4
 * Tests UART and interrupt routines servicing them.
5
 *
6
 * Relies on testbench having uart0's tx line tied to uart1's rx line
7
 *
8
 * Shows how single ISR can be used for two cores.
9
 *
10
 * Julius Baxter, julius.baxter@orsoc.se
11
 *
12
*/
13
 
14
 
15 393 julius
#include "uart-utils.h"
16 349 julius
#include "spr-defs.h"
17
#include "board.h"
18
#include "uart.h"
19
#include "int.h"
20
#include "design-defines.h"
21
 
22
#ifndef UART0
23
# error
24
# error UART0 missing and is required for UART interrupt (loopback) test
25
# error
26
#endif
27
#ifndef UART1
28
# error
29
# error UART1 missing and is required for UART interrupt (loopback) test
30
# error
31
#endif
32
 
33
 
34
struct uart_tx_ctrl
35
{
36
  char *bufptr;
37
  int busy;
38
};
39
 
40
volatile struct uart_tx_ctrl uart0_tx_ctrl;
41
 
42
void uart_int_handler(void* corenum);
43
 
44
void uart_int_handler(void* corenum)
45
{
46
 
47
  int core = *((int*)corenum);
48
 
49
  if (core)report(core);
50
 
51
  char iir = uart_get_iir(core);
52
 
53
  if ( (iir & UART_IIR_RLSI)  == UART_IIR_RLSI)
54
    uart_get_lsr(core); // Should clear this interrupt
55
  else if ( (iir & UART_IIR_RDI) == UART_IIR_RDI )
56
    {
57
      // Was potentially also a timeout. Do we care?
58
 
59
      // Data received. Pull all from the FIFO buffer, here we just report it
60
      // and throw it away
61
      char rxchar;
62
      while (uart_check_for_char(core))
63
        {
64
          rxchar = uart_getc(core);
65
          report(0xff & rxchar);
66
          if (rxchar == 0x2a) // when char is "*"
67
            exit(0x8000000d);
68
        }
69
    }
70
  else if ( (iir & UART_IIR_THRI) ==  UART_IIR_THRI)
71
    {
72
      // Only trigered if we've set something to be transmitted
73
      // and enabled the interrupt.
74
      // Put next thing to be transmitted into buffer, check if it's
75
      // the last, if so, disable interrupts.
76
      if (uart0_tx_ctrl.bufptr[0] == 0) // EOL, disable interrupt after this char
77
        {
78
          uart_txint_disable(core);
79
          uart0_tx_ctrl.busy = 0;
80
        }
81
      else // Transmit this byte
82
        {
83
          uart_putc_noblock(core, uart0_tx_ctrl.bufptr[0]);
84
          uart0_tx_ctrl.bufptr++;
85
        }
86
    }
87
  else if ( (iir & UART_IIR_MSI) == UART_IIR_MSI )
88
    {
89
      // Just read the modem status register to clear this
90
      uart_get_msr(core);
91
    }
92
}
93
 
94
 
95
void uart0_tx_buffer(char* buf)
96
{
97
  while (uart0_tx_ctrl.busy); // Wait until we can transmit more
98
  uart0_tx_ctrl.bufptr = buf;
99
  uart0_tx_ctrl.busy = 1;
100
  uart_txint_enable(0);
101
}
102
 
103
int main()
104
{
105
  int uart0_core = 0;
106
  int uart1_core = 1;
107
  uart0_tx_ctrl.busy = 0;
108
 
109
  /* Set up interrupt handler */
110
  int_init();
111
 
112
  /* Install UART core 0 interrupt handler */
113
  int_add(UART0_IRQ, uart_int_handler,(void*) &uart0_core);
114
 
115
  /* Install UART core 1 interrupt handler */
116
  int_add(UART1_IRQ, uart_int_handler,(void*) &uart1_core);
117
 
118
  /* Enable interrupts in supervisor register */
119
  mtspr (SPR_SR, mfspr (SPR_SR) | SPR_SR_IEE);
120
 
121
  uart_init(uart0_core);
122
  uart_init(uart1_core);
123
 
124
  uart_rxint_enable(uart1_core);
125
 
126
  char* teststring = "\n\tHello world, and UART 1, from UART 0\n\0";
127
 
128
  uart0_tx_buffer(teststring);
129
 
130
  // Do other things while we transmit
131
  float f1, f2, f3; int i;
132
  f1 = 0.2382; f2 = 4342.65; f3=0;
133
  for(i=0;i<32;i++) f3 += f1*f3 + f2;
134
 
135
  report(f3);
136
  report(0x4aaaaa1f);
137
 
138
  char* done_calculating = "\tDone with the number crunching!\n\0";
139
 
140
  uart0_tx_buffer(done_calculating);
141
 
142
  char* finish = "*\n\0";
143
 
144
  uart0_tx_buffer(finish);
145
 
146
  while(1); // will exit in the rx interrupt routine
147
 
148
}

powered by: WebSVN 2.1.0

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