serial driver portability work

This commit is contained in:
pjht 2020-07-23 09:21:14 -05:00
parent 063cde8101
commit f01785d69e
3 changed files with 62 additions and 59 deletions

View File

@ -2,7 +2,6 @@
#include "../isr.h"
#include "../serial.h"
#include <cpu/ports.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
@ -104,7 +103,8 @@ void serial_init() {
}
}
static void serial_putc(char c) {
void serial_putc(char c) {
if (!configured[0]) return;
if (c=='\n') {
while (!is_transmit_fifo_empty(0)) continue;
port_byte_out(data_port(0),'\r');
@ -115,58 +115,3 @@ static void serial_putc(char c) {
port_byte_out(data_port(0),c);
}
}
void serial_write_string(const char* s) {
if (!configured[0]) return;
for (int i=0;s[i]!='\0';i++) {
serial_putc(s[i]);
}
}
void serial_printf(const char* format,...) {
va_list arg;
va_start(arg,format);
for(;*format!='\0';format++) {
if(*format!='%') {
serial_putc(*format);
continue;
}
format++;
switch(*format) {
case 'c': {
int i=va_arg(arg,int);
serial_putc(i);
break;
}
case 'd': {
int i=va_arg(arg,int); //Fetch Decimal/Integer argument
if(i<0) {
i=-i;
serial_putc('-');
}
char str[11];
int_to_ascii(i,str);
serial_write_string(str);
break;
}
// case 'o': {
// int i=va_arg(arg,unsigned int); //Fetch Octal representation
// puts(convert(i,8));
// break;
// }
case 's': {
char* s=va_arg(arg,char*);
serial_write_string(s);
break;
}
case 'x': {
uint32_t i=va_arg(arg,uint32_t);
char str[11];
str[0]='\0';
hex_to_ascii(i,str);
serial_write_string(str);
break;
}
}
}
}

View File

@ -2,7 +2,8 @@
#define SERIAL_H
void serial_init();
void serial_write_string(const char* s);
void serial_printf(const char* format,...);
void serial_putc(char c);
void serial_write_string(const char* s); //Provided by platform-independent code
void serial_printf(const char* format,...); //Provided by platform-independent code
#endif

57
kernel/cpu/serial_indep.c Normal file
View File

@ -0,0 +1,57 @@
#include "serial.h"
#include <stdint.h>
#include <stdarg.h>
void serial_write_string(const char* s) {
for (int i=0;s[i]!='\0';i++) {
serial_putc(s[i]);
}
}
void serial_printf(const char* format,...) {
va_list arg;
va_start(arg,format);
for(;*format!='\0';format++) {
if(*format!='%') {
serial_putc(*format);
continue;
}
format++;
switch(*format) {
case 'c': {
int i=va_arg(arg,int);
serial_putc(i);
break;
}
case 'd': {
int i=va_arg(arg,int); //Fetch Decimal/Integer argument
if(i<0) {
i=-i;
serial_putc('-');
}
char str[11];
int_to_ascii(i,str);
serial_write_string(str);
break;
}
// case 'o': {
// int i=va_arg(arg,unsigned int); //Fetch Octal representation
// puts(convert(i,8));
// break;
// }
case 's': {
char* s=va_arg(arg,char*);
serial_write_string(s);
break;
}
case 'x': {
uint32_t i=va_arg(arg,uint32_t);
char str[11];
str[0]='\0';
hex_to_ascii(i,str);
serial_write_string(str);
break;
}
}
}
}