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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-newlib/] [newlib-1.17.0/] [libgloss/] [scarts_16/] [UART.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 jlechner
/* UART device driver for SCARTS.
2
 * Copyright (C) 2010, 2011 Embedded Computing Systems Group,
3
 * Department of Computer Engineering, Vienna University of Technology.
4
 * Contributed by Martin Walter <mwalter@opencores.org>
5
 *
6
 * The authors hereby grant permission to use, copy, modify, distribute,
7
 * and license this software and its documentation for any purpose, provided
8
 * that existing copyright notices are retained in all copies and that this
9
 * notice is included verbatim in any distributions. No written agreement,
10
 * license, or royalty fee is required for any of the authorized uses.
11
 * Modifications to this software may be copyrighted by their authors
12
 * and need not follow the licensing terms described here, provided that
13
 * the new terms are clearly indicated on the first page of each file where
14
 * they apply.
15
 */
16
 
17
#include "UART.h"
18
#include "modules.h"
19
 
20
static int  UART_getc (char *c);
21
static void UART_putc (const char c);
22
 
23
static int
24
UART_getc (char *c)
25
{
26
  int err = 0;
27
  uint16_t msg;
28
 
29
  /* Start the receiver. */
30
  MINI_UART_CMD = (1 << MINI_UART_CMD_AA_2);
31
 
32
  /* Wait until the receiver is ready. */
33
  while ((MINI_UART_STATUS_C & (1 << MINI_UART_STATUS_C_RBR)) == 0)
34
    asm ("nop");
35
 
36
  /* Check for errors from the receiver. */
37
  if ((MINI_UART_STATUS_C & (1 << MINI_UART_STATUS_C_FE)) != 0)
38
    err = EFRAME;
39
 
40
  if ((MINI_UART_STATUS_C & (1 << MINI_UART_STATUS_C_PE)) != 0)
41
    err = EPARITY;
42
 
43
  if ((MINI_UART_STATUS_C & (1 << MINI_UART_STATUS_C_OV)) != 0)
44
    err = EOVFLOW;
45
 
46
  msg = MINI_UART_MSG;
47
 
48
  /* Stop the receiver. */
49
  MINI_UART_CMD = (1 << MINI_UART_CMD_AA_2) | (1 << MINI_UART_CMD_AA_0);
50
 
51
  if (err == 0)
52
    *c = (char) msg;
53
 
54
  return err;
55
}
56
 
57
static void
58
UART_putc (const char c)
59
{
60
  /* Wait until transmitter is ready. */
61
  while ((MINI_UART_STATUS_C & (1 << MINI_UART_STATUS_C_TBR)) == 0)
62
    asm ("nop");
63
 
64
  MINI_UART_MSG = c;
65
 
66
  /* Start the transmitter. */
67
  MINI_UART_CMD = (1 << MINI_UART_CMD_AA_1) | (1 << MINI_UART_CMD_AA_0);
68
 
69
  while ((MINI_UART_STATUS_C & (1 << MINI_UART_STATUS_C_TBR)) == 0)
70
    asm ("nop");
71
 
72
  MINI_UART_CMD = 0;
73
}
74
 
75
int
76
UART_close (int file)
77
{
78
  return 0;
79
}
80
 
81
void
82
UART_init (UART_Cfg cfg)
83
{
84
  uint16_t ubrs;
85
 
86
  if (cfg.baud == 0)
87
    return;
88
 
89
  MINI_UART_CFG = 0;
90
 
91
  /* Set the parity bits. */
92
  if (cfg.frame.parity != UART_CFG_PARITY_NONE)
93
  {
94
    MINI_UART_CFG |= (1 << MINI_UART_CFG_PARENA);
95
 
96
    if (cfg.frame.parity == UART_CFG_PARITY_ODD)
97
      MINI_UART_CFG |= (1 << MINI_UART_CFG_ODD);
98
  }
99
 
100
  /* Set the stop bits. */
101
  if (cfg.frame.stop_bits == UART_CFG_STOP_BITS_2)
102
    MINI_UART_CFG |= (1 << MINI_UART_CFG_STOP);
103
 
104
  /* Set the message length. */
105
  if (cfg.frame.msg_len & 8)
106
    MINI_UART_CFG |= (1 << MINI_UART_CFG_MSGLEN_3);
107
 
108
  if (cfg.frame.msg_len & 4)
109
    MINI_UART_CFG |= (1 << MINI_UART_CFG_MSGLEN_2);
110
 
111
  if (cfg.frame.msg_len & 2)
112
    MINI_UART_CFG |= (1 << MINI_UART_CFG_MSGLEN_1);
113
 
114
  if (cfg.frame.msg_len & 1)
115
    MINI_UART_CFG |= (1 << MINI_UART_CFG_MSGLEN_0);
116
 
117
  /* Enable transmission control. */
118
  MINI_UART_CFG |= (1 << MINI_UART_CFG_TRCTRL);
119
 
120
  /* Set the baud rate. */
121
  ubrs = (uint16_t) (cfg.fclk / cfg.baud) << 4;
122
  MINI_UART_UBRS_H = (ubrs >> 8) & 0xFF;
123
  MINI_UART_UBRS_L = ubrs & 0xFF;
124
}
125
 
126
int
127
UART_open (const char *name, int flags, int mode)
128
{
129
  return 0;
130
}
131
 
132
int
133
UART_read (int file, char *ptr, int len)
134
{
135
  char c;
136
  int i;
137
 
138
  i = 0;
139
 
140
  if (len <= 0)
141
    return 0;
142
 
143
  do
144
  {
145
    if (UART_getc (&c) != 0)
146
      break;
147
 
148
    ptr[i++] = c;
149
  }
150
  while (i < len);
151
 
152
  return i;
153
}
154
 
155
int
156
UART_read_line (int file, char *ptr, int len)
157
{
158
  char c;
159
  int i;
160
 
161
  i = 0;
162
 
163
  if (len <= 0)
164
    return 0;
165
 
166
  do
167
  {
168
    if (UART_getc (&c) != 0)
169
      break;
170
 
171
    if (c == '\r')
172
      continue;
173
 
174
    if (c == '\n')
175
      break;
176
 
177
    ptr[i++] = c;
178
  }
179
  while (i < len);
180
 
181
  ptr[i] = '\0';
182
  return i;
183
}
184
 
185
int
186
UART_write (int file, char *ptr, int len)
187
{
188
  int i;
189
 
190
  if (len <= 0)
191
    return 0;
192
 
193
  for (i = 0; i < len; ++i)
194
    UART_putc (ptr[i]);
195
 
196
  return len;
197
}

powered by: WebSVN 2.1.0

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