ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
sio.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #include <stdio.h>
17 #include <stddef.h>
18 #include <stdarg.h>
19 
20 #include "tamtypes.h"
21 #include "kernel.h"
22 #include "string.h"
23 #include "sio.h"
24 
25 #ifndef PS2LIB_STR_MAX
26 #define PS2LIB_STR_MAX 4096
27 #endif
28 
30 #define CPUCLK 294912000
32 #define LCR_SCS_VAL (1<<5)
33 
34 #ifdef F_sio_init
35 void sio_init(u32 baudrate, u8 lcr_ueps, u8 lcr_upen, u8 lcr_usbl, u8 lcr_umode)
36 {
37  u32 brd; /* Baud rate divisor. */
38  u8 bclk = 0; /* Baud rate generator clock. */
39 
40  _sw(LCR_SCS_VAL|((lcr_ueps & 1) << 4)|((lcr_upen & 1) << 3)|
41  ((lcr_usbl & 1) << 2)|(lcr_umode & 1), SIO_LCR);
42 
43  /* Disable all interrupts. */
44  _sw(0, SIO_IER);
45 
46  /* Reset the FIFOs. */
48  /* Enable them. */
49  _sw(0, SIO_FCR);
50 
51  brd = CPUCLK / (baudrate * 256);
52 
53  while ((brd >= 256) && (++bclk < 4))
54  brd /= 4;
55 
56  _sw((bclk << 8) | brd, SIO_BGR);
57 }
58 #endif
59 
60 #ifdef F_sio_putc
61 static u8 ___last_sio_putc = 0;
62 int sio_putc(int c)
63 {
64  if((c == '\n') && (___last_sio_putc != '\r'))
65  {
66  // hack: if the character to be outputted is a '\n'
67  // and the previously-outputted character is not a '\r',
68  // output a '\r' first.
69  sio_putc('\r');
70  }
71 
72  /* Block until we're ready to transmit. */
73  while ((_lw(SIO_ISR) & 0xf000) == 0x8000);
74 
75  _sb(c, SIO_TXFIFO);
76  ___last_sio_putc = c;
77  return c;
78 }
79 #endif
80 
81 #ifdef F_sio_getc
82 int sio_getc()
83 {
84  /* Do we have something in the RX FIFO? */
85  if (_lw(SIO_ISR) & 0xf00) {
86  u8 b = _lb(SIO_RXFIFO);
87  _sw(7, SIO_ISR);
88  return b;
89  }
90 
91  /* Return EOF. */
92  return -1;
93 }
94 #endif
95 
96 #ifdef F_sio_getc_block
97 int sio_getc_block()
98 {
99  /* Do we have something in the RX FIFO? */
100  while (!(_lw(SIO_ISR) & 0xf00));
101  return sio_getc();
102 }
103 #endif
104 
105 #ifdef F_sio_write
106 size_t sio_write(void *buf, size_t size)
107 {
108  u8 *p = (u8 *)buf;
109  size_t i;
110 
111  for (i = 0; i < size; i++)
112  sio_putc(p[i]);
113 
114  return size;
115 }
116 #endif
117 
118 #ifdef F_sio_read
119 size_t sio_read(void *buf, size_t size)
120 {
121  u8 *p = (u8 *)buf;
122  size_t i;
123  int c;
124 
125  for (i = 0; i < size; i++) {
126  if ((c = sio_getc()) == -1)
127  break;
128 
129  p[i] = (u8)c;
130  }
131 
132  return i;
133 }
134 #endif
135 
136 #ifdef F_sio_puts
137 int sio_puts(const char *str)
138 {
139  int res;
140 
141  res = sio_putsn(str);
142 
143  sio_putc('\r');
144  sio_putc('\n');
145  return res + 2;
146 }
147 #endif
148 
149 #ifdef F_sio_putsn
150 int sio_putsn(const char *str)
151 {
152  int res;
153 
154  for (res = 0; *str; res++, str++)
155  sio_putc(*str);
156 
157  return res;
158 }
159 #endif
160 
161 #ifdef F_sio_gets
162 char *sio_gets(char *str)
163 {
164  char *s = str;
165  int c;
166 
167  while (1) {
168  c = sio_getc_block();
169  /* Check for newline. */
170  if (c == '\n' || c == '\r')
171  break;
172 
173  *s++ = c;
174  }
175 
176  *s = '\0';
177  return str;
178 }
179 #endif
180 
181 #ifdef F_sio_flush
182 void sio_flush()
183 {
184  while (_lw(SIO_ISR) & 0xf00)
185  _lb(SIO_RXFIFO);
186 }
187 #endif
s32 s
Definition: ps2ipc.c:30
#define LCR_SCS_VAL
Definition: sio.c:32
#define CPUCLK
Definition: sio.c:30
int sio_putsn(const char *str)
int sio_putc(int c)
#define SIO_LCR
Definition: sio.h:29
char * sio_gets(char *str)
#define SIO_BGR
Definition: sio.h:77
#define SIO_IER
Definition: sio.h:55
void sio_flush(void)
#define SIO_FCR_RFRST
Definition: sio.h:72
#define SIO_FCR_TFRST
Definition: sio.h:74
int sio_puts(const char *str)
int sio_getc(void)
void sio_init(u32 baudrate, u8 lcr_ueps, u8 lcr_upen, u8 lcr_usbl, u8 lcr_umode)
int sio_getc_block(void)
#define SIO_ISR
Definition: sio.h:62
#define SIO_RXFIFO
Definition: sio.h:82
#define SIO_FCR_FRSTE
Definition: sio.h:70
#define SIO_TXFIFO
Definition: sio.h:80
size_t sio_read(void *buf, size_t size)
#define SIO_FCR
Definition: sio.h:68
size_t sio_write(void *buf, size_t size)
unsigned int u32
Definition: tamtypes.h:30
static u32 _lw(u32 addr)
Definition: tamtypes.h:96
static u8 _lb(u32 addr)
Definition: tamtypes.h:94
static void _sb(u8 val, u32 addr)
Definition: tamtypes.h:98
static void _sw(u32 val, u32 addr)
Definition: tamtypes.h:100
unsigned char u8
Definition: tamtypes.h:23