Modestus Moon OS  R4
CS 450 project
serial.c File Reference
#include <stdint.h>
#include <string.h>
#include <core/io.h>
#include <core/serial.h>
#include <core/commhand.h>
#include <input.h>
Include dependency graph for serial.c:

Go to the source code of this file.

Macros

#define NO_ERROR   0
 

Functions

int init_serial (int device)
 
int serial_println (const char *msg)
 
int serial_print (const char *msg)
 
int set_serial_out (int device)
 
int set_serial_in (int device)
 
void clear_buff ()
 
void return_cursor (int cursor_loc, int length)
 
char * serial_poll (char in_string[MAX_LENGTH])
 

Variables

int serial_port_out = 0
 
int serial_port_in = 0
 

Macro Definition Documentation

#define NO_ERROR   0

Definition at line 16 of file serial.c.

Referenced by init_serial(), serial_print(), serial_println(), set_serial_in(), and set_serial_out().

Function Documentation

void clear_buff ( )

Definition at line 94 of file serial.c.

References serial_print().

Referenced by serial_poll().

94  {
95  serial_print("\033[2K");
96  serial_print("\033[G");
97  serial_print("> ");
98 }
int serial_print(const char *msg)
Definition: serial.c:59
int init_serial ( int  device)

Definition at line 26 of file serial.c.

References inb, NO_ERROR, and outb.

27 {
28  outb(device + 1, 0x00); //disable interrupts
29  outb(device + 3, 0x80); //set line control register
30  outb(device + 0, 115200/9600); //set bsd least sig bit
31  outb(device + 1, 0x00); //brd most significant bit
32  outb(device + 3, 0x03); //lock divisor; 8bits, no parity, one stop
33  outb(device + 2, 0xC7); //enable fifo, clear, 14byte threshold
34  outb(device + 4, 0x0B); //enable interrupts, rts/dsr set
35  (void)inb(device); //read bit to reset port
36  return NO_ERROR;
37 }
#define inb(port)
Definition: io.h:15
#define NO_ERROR
Definition: serial.c:16
#define outb(port, data)
Definition: io.h:8
void return_cursor ( int  cursor_loc,
int  length 
)

Definition at line 100 of file serial.c.

References serial_print().

Referenced by serial_poll().

100  {
101  int move = length - cursor_loc;
102  int i = 0;
103  for (; i < move; i++) {
104  serial_print("\033[D");
105  }
106 }
int serial_print(const char *msg)
Definition: serial.c:59
char* serial_poll ( char  in_string[MAX_LENGTH])

Definition at line 114 of file serial.c.

References BACKSPACE, clear_buff(), COM1, DELETE, ESC, in_string, inb, NEW_LINE, RETURN, return_cursor(), serial_print(), and strcpy().

Referenced by init_commhand(), and shutdownFunc().

114  {
115  //in_char is the character read from the register
116  //out_char is that character with a null terminator appended on the end
117  //length is the current size of the input
118  //cursor_loc is the current location of the cursor in the input
119  char in_char;
120  char out_char[2];
121  out_char[1] = '\0';
122  int length = 0;
123  int cursor_loc = 0;
124 
125  //polling for user input
126  while (1) {
127 
128  if (inb(COM1+5)&1) {
129  //the character is read from the register
130  in_char = inb(COM1);
131  out_char[0] = in_char;
132 
133  if (in_char == BACKSPACE) {
134  if (length != 0) {
135  if (cursor_loc == length)
136  {
137  cursor_loc--;
138  in_string[cursor_loc] = '\0';
139  clear_buff();
141  length--;
142  return_cursor(cursor_loc, length);
143 
144  }
145  else
146  {
147  cursor_loc--;
148  strcpy(&in_string[cursor_loc], &in_string[cursor_loc + 1]);
149  in_string[length - 1] = '\0';
150  clear_buff();
152  length--;
153  return_cursor(cursor_loc, length);
154  }
155  }
156  } else if (in_char == DELETE) {
157  } else if (in_char == RETURN || in_char == NEW_LINE) {
158 
159  return in_string;
160  } else if (in_char == ESC) {
161  //buffer through ansi escape keys ([)
162  in_char = inb(COM1);
163  in_char = inb(COM1);
164 
165  if (in_char == 'A') { //UP
166  clear_buff();
167  in_string[0] = '\0';
168  cursor_loc = 0;
169  length = 0;
170  //go backwards in command history
171  } else if (in_char == 'B') { //DOWN
172  clear_buff();
173  in_string[0] = '\0';
174  cursor_loc = 0;
175  length = 0;
176  //go forward in command history
177  } else if (in_char == 'C') { //RIGHT
178  if (cursor_loc < length) {
179  serial_print("\033[C");
180  cursor_loc++;
181  }
182  } else if (in_char == 'D') { //LEFT
183  if (cursor_loc > 0) {
184  serial_print("\033[D");
185  cursor_loc--;
186  }
187  } else if (in_char == '3') { //DEL
188  strcpy(&in_string[cursor_loc], &in_string[cursor_loc + 1]);
189  in_string[length] = '\0';
190  clear_buff();
192  length--;
193  return_cursor(cursor_loc, length);
194  }
195 
196  } else {
197  if (length < MAX_LENGTH - 1) {
198  if (length == cursor_loc) {
199  in_string[length] = in_char;
200  in_string[length + 1] = '\0';
201  serial_print(out_char);
202  cursor_loc++;
203  length++;
204  }
205  else {
206  int i = length;
207  in_string[length+1] = '\0';
208  for (; i >= cursor_loc; i--) {
209  in_string[i + 1] = in_string[i];
210  }
211  in_string[cursor_loc] = in_char;
212  length++;
213  cursor_loc++;
214  clear_buff();
216  return_cursor(cursor_loc, length);
217  }
218  }
219  }
220  }
221  }
222  return in_string;
223 }
#define BACKSPACE
Definition: input.h:9
void return_cursor(int cursor_loc, int length)
Definition: serial.c:100
#define MAX_LENGTH
Definition: input.h:6
int serial_print(const char *msg)
Definition: serial.c:59
void clear_buff()
Definition: serial.c:94
char * strcpy(char *s1, const char *s2)
strcpy copies one string to another string
Definition: string.c:26
#define RETURN
Definition: input.h:16
char in_string[MAX_LENGTH]
Definition: commhand.c:10
#define NEW_LINE
Definition: input.h:17
#define ESC
Definition: input.h:11
#define COM1
Definition: serial.h:6
#define DELETE
Definition: input.h:10
#define inb(port)
Definition: io.h:15
int serial_print ( const char *  msg)

Definition at line 59 of file serial.c.

References NO_ERROR, outb, and serial_port_out.

Referenced by clear_buff(), do_isr(), getTime(), init_commhand(), pcbFunc(), return_cursor(), serial_poll(), shutdownFunc(), and version().

60 {
61  int i;
62  for(i=0; *(i+msg)!='\0'; i++){
63  outb(serial_port_out,*(i+msg));
64  }
65  if (*msg == '\r') outb(serial_port_out,'\n');
66  return NO_ERROR;
67 }
int serial_port_out
Definition: serial.c:19
#define NO_ERROR
Definition: serial.c:16
#define outb(port, data)
Definition: io.h:8
int set_serial_in ( int  device)

Definition at line 87 of file serial.c.

References NO_ERROR, and serial_port_in.

Referenced by kmain().

88 {
89  serial_port_in = device;
90  return NO_ERROR;
91 }
int serial_port_in
Definition: serial.c:20
#define NO_ERROR
Definition: serial.c:16
int set_serial_out ( int  device)

Definition at line 75 of file serial.c.

References NO_ERROR, and serial_port_out.

Referenced by kmain().

76 {
77  serial_port_out = device;
78  return NO_ERROR;
79 }
int serial_port_out
Definition: serial.c:19
#define NO_ERROR
Definition: serial.c:16

Variable Documentation

int serial_port_in = 0

Definition at line 20 of file serial.c.

Referenced by set_serial_in().

int serial_port_out = 0

Definition at line 19 of file serial.c.

Referenced by serial_print(), serial_println(), and set_serial_out().