OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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 431 julius
 * Relies on UART 0 receiving external stimulus.
7 349 julius
 *
8
 * Julius Baxter, julius.baxter@orsoc.se
9
 *
10
*/
11
 
12
 
13 397 julius
#include "cpu-utils.h"
14 349 julius
#include "spr-defs.h"
15
#include "board.h"
16
#include "uart.h"
17 431 julius
#include "printf.h"
18 349 julius
#include "int.h"
19 397 julius
#include "orpsoc-defines.h"
20 349 julius
 
21
#ifndef UART0
22
# error
23
# error UART0 missing and is required for UART interrupt (loopback) test
24
# error
25
#endif
26
 
27 431 julius
 
28
#define UART_TX_BUF_MAX 256
29 349 julius
struct uart_tx_ctrl
30
{
31 431 julius
  char buf[UART_TX_BUF_MAX]; /* 256byte buffer to print */
32
  int buf_count;
33
  int tx_count;
34 349 julius
};
35
 
36
volatile struct uart_tx_ctrl uart0_tx_ctrl;
37
 
38 431 julius
 
39
/* Reset buffer counter */
40
void uart0_tx_buffer_init(void)
41
{
42
  uart0_tx_ctrl.buf_count = 0;
43
  uart0_tx_ctrl.tx_count = 0;
44
}
45
 
46
/* Add characters to be transmitted */
47
void uart0_tx_buffer_add(int numchars, char* buf)
48
{
49
  // If we're not currently transmitting, ie. nothign in the buffer, then
50
  // we should transmit after this.
51
  int start_tx = (numchars && !uart0_tx_ctrl.buf_count);
52
  while(numchars)
53
    {
54
      /* Not good if this gets used outside of interrupt. */
55
      uart0_tx_ctrl.buf[uart0_tx_ctrl.buf_count%UART_TX_BUF_MAX] = *buf++;
56
      uart0_tx_ctrl.buf_count++;
57
      numchars--;
58
    }
59
 
60
  if (start_tx)
61
    {
62
      uart_txint_enable(0);
63
      uart_putc_noblock(0, uart0_tx_ctrl.buf[uart0_tx_ctrl.tx_count%UART_TX_BUF_MAX]);
64
      uart0_tx_ctrl.tx_count++;
65
    }
66
}
67
 
68
 
69 349 julius
void uart_int_handler(void* corenum);
70
 
71
void uart_int_handler(void* corenum)
72
{
73
 
74
  int core = *((int*)corenum);
75
 
76
  if (core)report(core);
77
 
78
  char iir = uart_get_iir(core);
79
 
80
  if ( (iir & UART_IIR_RLSI)  == UART_IIR_RLSI)
81
    uart_get_lsr(core); // Should clear this interrupt
82
  else if ( (iir & UART_IIR_RDI) == UART_IIR_RDI )
83
    {
84
      // Was potentially also a timeout. Do we care?
85
 
86 431 julius
      // Data received. Pull from the fifo and echo back.
87 349 julius
      char rxchar;
88
      while (uart_check_for_char(core))
89
        {
90 431 julius
 
91 349 julius
          rxchar = uart_getc(core);
92
          report(0xff & rxchar);
93 431 julius
          //printf("RX char: %c\n",rxchar);
94
          uart0_tx_buffer_add(1, &rxchar);
95
 
96 397 julius
          if (rxchar == 0x2a) // Exit simulation when RX char is '*'
97 431 julius
            {
98
              report(0x8000000d);
99
              exit(0);
100
            }
101 349 julius
        }
102
    }
103
  else if ( (iir & UART_IIR_THRI) ==  UART_IIR_THRI)
104
    {
105
      // Only trigered if we've set something to be transmitted
106
      // and enabled the interrupt.
107
      // Put next thing to be transmitted into buffer, check if it's
108
      // the last, if so, disable interrupts.
109 431 julius
      if (uart0_tx_ctrl.buf_count == uart0_tx_ctrl.tx_count)
110 349 julius
        {
111
          uart_txint_disable(core);
112
        }
113
      else // Transmit this byte
114
        {
115 431 julius
          uart_putc_noblock(0, uart0_tx_ctrl.buf[uart0_tx_ctrl.tx_count%UART_TX_BUF_MAX]);
116
          uart0_tx_ctrl.tx_count++;
117 349 julius
        }
118
    }
119
  else if ( (iir & UART_IIR_MSI) == UART_IIR_MSI )
120
    {
121
      // Just read the modem status register to clear this
122
      uart_get_msr(core);
123
    }
124
}
125
 
126
int main()
127
{
128
  int uart0_core = 0;
129
  int uart1_core = 1;
130
 
131
  /* Set up interrupt handler */
132
  int_init();
133
 
134
  /* Install UART core 0 interrupt handler */
135
  int_add(UART0_IRQ, uart_int_handler,(void*) &uart0_core);
136 397 julius
 
137 349 julius
  /* Install UART core 1 interrupt handler */
138 397 julius
  //int_add(UART1_IRQ, uart_int_handler,(void*) &uart1_core);
139 349 julius
 
140
  /* Enable interrupts in supervisor register */
141
  mtspr (SPR_SR, mfspr (SPR_SR) | SPR_SR_IEE);
142
 
143
  uart_init(uart0_core);
144
 
145 397 julius
  uart_rxint_enable(uart0_core);
146 349 julius
 
147 431 julius
  uart0_tx_buffer_init();
148 349 julius
 
149 431 julius
  printf("\n\tUART interrupt test.\n");
150
  printf("\n\tType to see characters echoed\n");
151 349 julius
 
152
  while(1); // will exit in the rx interrupt routine
153
 
154
}

powered by: WebSVN 2.1.0

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