source: rtems/c/src/lib/libbsp/or1k/or1ksim/console/uart.c @ fbe59f7

4.115
Last change on this file since fbe59f7 was fbe59f7, checked in by Hesham ALMatary <heshamelmatary@…>, on 09/19/14 at 19:20:35

or1ksim: Console: implement uart_read_polled

Implement uart_read_polled which is needed for any application that
reads input from users. It works fine with pppd, capture, and all
termios tests.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup or1ksim_uart
5 *
6 * @brief UART support.
7 */
8
9/*
10 * COPYRIGHT (c) 2014 Hesham ALMatary <heshamelmatary@gmail.com>
11 *
12 * The license and distribution terms for this file may be
13 * found in the file LICENSE in this distribution or at
14 * http://www.rtems.org/license/LICENSE
15 */
16
17#include <libchip/sersupp.h>
18#include <bsp/or1ksim.h>
19#include <bsp.h>
20#include <bsp/irq.h>
21#include <bsp/uart.h>
22#include <rtems/score/isr.h>
23
24static rtems_vector_number uart_get_irq_number(const console_tbl *ct)
25{
26   return ct->ulIntVector;
27}
28
29static uint32_t uart_get_baud(const console_tbl *ct)
30{
31   return ct->ulClock;
32}
33
34static void uart_set_baud(int baud)
35{
36  int divisor = (OR1KSIM_BSP_CLOCK_FREQ) / (16 * baud);
37  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_CTRL) |=
38    OR1KSIM_BSP_UART_REG_LINE_CTRL_DLAB;
39
40  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_DEV_LATCH_LOW) = divisor & 0xff;
41
42  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_DEV_LATCH_HIGH) =
43    (divisor >> 8) & 0xff;
44
45  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_CTRL) &=
46    ~(OR1KSIM_BSP_UART_REG_LINE_CTRL_DLAB);
47}
48
49static void uart_initialize(int minor)
50{
51  /* Disable all interrupts */
52  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_INT_ENABLE) = 0x00;
53
54  /* Reset receiver and transmitter */
55   OR1KSIM_REG(OR1KSIM_BSP_UART_REG_FIFO_CTRL) =
56     OR1KSIM_BSP_UART_REG_FIFO_CTRL_ENABLE_FIFO |
57     OR1KSIM_BSP_UART_REG_FIFO_CTRL_CLEAR_RCVR  |
58     OR1KSIM_BSP_UART_REG_FIFO_CTRL_CLEAR_XMIT  |
59     OR1KSIM_BSP_UART_REG_FIFO_CTRL_TRIGGER_14;
60
61  /* Set data pattern configuration */
62  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_CTRL) =
63    OR1KSIM_BSP_UART_REG_LINE_CTRL_WLEN8 &
64      (OR1KSIM_BSP_UART_REG_LINE_CTRL_STOP |
65       OR1KSIM_BSP_UART_REG_LINE_CTRL_PARITY);
66
67  /* Set baud rate */
68  uart_set_baud(OR1KSIM_UART_DEFAULT_BAUD);
69}
70
71static int uart_first_open(int major, int minor, void *arg)
72{
73  rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg;
74  struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1;
75  const console_tbl *ct = Console_Port_Tbl [minor];
76  console_data *cd = &Console_Port_Data [minor];
77
78  cd->termios_data = tty;
79  rtems_termios_set_initial_baud(tty, ct->ulClock);
80
81  return 0;
82}
83
84static int uart_last_close(int major, int minor, void *arg)
85{
86  return 0;
87}
88
89static char uart_read_polled(int minor)
90{
91  unsigned char lsr;
92  char c;
93
94 /* Get a character when avaiable */
95  do {
96       lsr = OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_STATUS);
97  } while ((lsr & OR1KSIM_BSP_UART_REG_LINE_STATUS_DR)
98           != OR1KSIM_BSP_UART_REG_LINE_STATUS_DR);
99
100  return OR1KSIM_REG(OR1KSIM_BSP_UART_REG_RX);
101}
102
103static void uart_write_polled(int minor, char c)
104{
105  unsigned char lsr;
106  const uint32_t transmit_finished =
107    (OR1KSIM_BSP_UART_REG_LINE_STATUS_TEMT |
108     OR1KSIM_BSP_UART_REG_LINE_STATUS_THRE);
109
110  /* Wait until there is no pending data in the transmitter FIFO (empty) */
111  do {
112      lsr = OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_STATUS);
113  } while (!(lsr & OR1KSIM_BSP_UART_REG_LINE_STATUS_THRE));
114
115  OR1KSIM_REG(OR1KSIM_BSP_UART_REG_TX) = c;
116
117  /* Wait until trasmit data is finished */
118  do {
119      lsr = OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_STATUS);
120  } while ( (lsr & transmit_finished) != transmit_finished );
121}
122
123static ssize_t uart_write_support_polled(
124  int minor,
125  const char *s,
126  size_t n
127)
128{
129  ssize_t i = 0;
130
131  for (i = 0; i < n; ++i){
132    uart_write_polled(minor, s [i]);
133  }
134
135  return n;
136}
137
138static int uart_set_attributes(int minor, const struct termios *term)
139{
140  return -1;
141}
142
143const console_fns or1ksim_uart_fns = {
144  .deviceProbe = libchip_serial_default_probe,
145  .deviceFirstOpen = uart_first_open,
146  .deviceLastClose = uart_last_close,
147  .deviceRead = uart_read_polled,
148  .deviceWrite = uart_write_support_polled,
149  .deviceInitialize = uart_initialize,
150  .deviceWritePolled = uart_write_polled,
151  .deviceSetAttributes = uart_set_attributes,
152  .deviceOutputUsesInterrupts = false
153};
Note: See TracBrowser for help on using the repository browser.