Skip to content

Commit

Permalink
optimize the uart get process
Browse files Browse the repository at this point in the history
Also remove the while in uart_isr. Generally unlimited loop in interrupt
handler is not recommended.

Signed-off-by: Wang Chen <[email protected]>
  • Loading branch information
unicornx committed Mar 28, 2024
1 parent 0cacbd6 commit 7df4ae5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 84 deletions.
20 changes: 6 additions & 14 deletions code/os/06-interrupts/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,17 @@ void uart_puts(char *s)

int uart_getc(void)
{
if (uart_read_reg(LSR) & LSR_RX_READY){
return uart_read_reg(RHR);
} else {
return -1;
}
while (0 == (uart_read_reg(LSR) & LSR_RX_READY))
;
return uart_read_reg(RHR);
}

/*
* handle a uart interrupt, raised because input has arrived, called from trap.c.
*/
void uart_isr(void)
{
while (1) {
int c = uart_getc();
if (c == -1) {
break;
} else {
uart_putc((char)c);
uart_putc('\n');
}
}
uart_putc((char)uart_getc());
/* add a new line just to look better */
uart_putc('\n');
}
20 changes: 6 additions & 14 deletions code/os/07-hwtimer/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,17 @@ void uart_puts(char *s)

int uart_getc(void)
{
if (uart_read_reg(LSR) & LSR_RX_READY){
return uart_read_reg(RHR);
} else {
return -1;
}
while (0 == (uart_read_reg(LSR) & LSR_RX_READY))
;
return uart_read_reg(RHR);
}

/*
* handle a uart interrupt, raised because input has arrived, called from trap.c.
*/
void uart_isr(void)
{
while (1) {
int c = uart_getc();
if (c == -1) {
break;
} else {
uart_putc((char)c);
uart_putc('\n');
}
}
uart_putc((char)uart_getc());
/* add a new line just to look better */
uart_putc('\n');
}
20 changes: 6 additions & 14 deletions code/os/08-preemptive/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,17 @@ void uart_puts(char *s)

int uart_getc(void)
{
if (uart_read_reg(LSR) & LSR_RX_READY){
return uart_read_reg(RHR);
} else {
return -1;
}
while (0 == (uart_read_reg(LSR) & LSR_RX_READY))
;
return uart_read_reg(RHR);
}

/*
* handle a uart interrupt, raised because input has arrived, called from trap.c.
*/
void uart_isr(void)
{
while (1) {
int c = uart_getc();
if (c == -1) {
break;
} else {
uart_putc((char)c);
uart_putc('\n');
}
}
uart_putc((char)uart_getc());
/* add a new line just to look better */
uart_putc('\n');
}
20 changes: 6 additions & 14 deletions code/os/09-lock/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,17 @@ void uart_puts(char *s)

int uart_getc(void)
{
if (uart_read_reg(LSR) & LSR_RX_READY){
return uart_read_reg(RHR);
} else {
return -1;
}
while (0 == (uart_read_reg(LSR) & LSR_RX_READY))
;
return uart_read_reg(RHR);
}

/*
* handle a uart interrupt, raised because input has arrived, called from trap.c.
*/
void uart_isr(void)
{
while (1) {
int c = uart_getc();
if (c == -1) {
break;
} else {
uart_putc((char)c);
uart_putc('\n');
}
}
uart_putc((char)uart_getc());
/* add a new line just to look better */
uart_putc('\n');
}
20 changes: 6 additions & 14 deletions code/os/10-swtimer/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,17 @@ void uart_puts(char *s)

int uart_getc(void)
{
if (uart_read_reg(LSR) & LSR_RX_READY){
return uart_read_reg(RHR);
} else {
return -1;
}
while (0 == (uart_read_reg(LSR) & LSR_RX_READY))
;
return uart_read_reg(RHR);
}

/*
* handle a uart interrupt, raised because input has arrived, called from trap.c.
*/
void uart_isr(void)
{
while (1) {
int c = uart_getc();
if (c == -1) {
break;
} else {
uart_putc((char)c);
uart_putc('\n');
}
}
uart_putc((char)uart_getc());
/* add a new line just to look better */
uart_putc('\n');
}
20 changes: 6 additions & 14 deletions code/os/11-syscall/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,17 @@ void uart_puts(char *s)

int uart_getc(void)
{
if (uart_read_reg(LSR) & LSR_RX_READY){
return uart_read_reg(RHR);
} else {
return -1;
}
while (0 == (uart_read_reg(LSR) & LSR_RX_READY))
;
return uart_read_reg(RHR);
}

/*
* handle a uart interrupt, raised because input has arrived, called from trap.c.
*/
void uart_isr(void)
{
while (1) {
int c = uart_getc();
if (c == -1) {
break;
} else {
uart_putc((char)c);
uart_putc('\n');
}
}
uart_putc((char)uart_getc());
/* add a new line just to look better */
uart_putc('\n');
}

0 comments on commit 7df4ae5

Please sign in to comment.