Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
sercons.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <sys/kassert.h>
#include <sys/irq.h>
#include "ioport.h"
#include "sercons.h"
Include dependency graph for sercons.c:

Go to the source code of this file.

Macros

#define COM1_BASE   0x3F8
 
#define COM2_BASE   0x2F8
 
#define COM3_BASE   0x3E8
 
#define COM4_BASE   0x2E8
 
#define COM1_IRQ   4
 
#define COM2_IRQ   3
 
#define COM3_IRQ   4
 
#define COM4_IRQ   3
 
#define UART_OFFSET_DATA   0 /* Data Register */
 
#define UART_OFFSET_IER   1 /* Interrupt Enable Register */
 
#define UART_OFFSET_IIR   2 /* Interrupt Identification & FIFO Control */
 
#define UART_OFFSET_LCR   3 /* Line Control Register */
 
#define UART_LCR_DLAB   0x80
 
#define UART_LCR_8N1   0x03
 
#define UART_OFFSET_MCR   4 /* Modem Control Register */
 
#define UART_OFFSET_LSR   5 /* Line Status Register */
 
#define UART_OFFSET_MSR   6 /* Modem Status Register */
 
#define UART_OFFSET_SR   7 /* Scratch Register */
 
#define UART_OFFSET_DIVLO   0 /* Divisors DLAB == 1 */
 
#define UART_OFFSET_DIVHI   1
 

Functions

void Serial_Init (void)
 
void Serial_LateInit (void)
 
void Serial_Interrupt (void *arg)
 
bool Serial_HasData ()
 
char Serial_Getc ()
 
void Serial_Putc (char ch)
 
void Serial_Puts (const char *str)
 

Variables

static IRQHandler handler
 
static uint16_t base
 
static uint8_t irq
 

Macro Definition Documentation

◆ COM1_BASE

#define COM1_BASE   0x3F8

Definition at line 11 of file sercons.c.

◆ COM1_IRQ

#define COM1_IRQ   4

Definition at line 16 of file sercons.c.

◆ COM2_BASE

#define COM2_BASE   0x2F8

Definition at line 12 of file sercons.c.

◆ COM2_IRQ

#define COM2_IRQ   3

Definition at line 17 of file sercons.c.

◆ COM3_BASE

#define COM3_BASE   0x3E8

Definition at line 13 of file sercons.c.

◆ COM3_IRQ

#define COM3_IRQ   4

Definition at line 18 of file sercons.c.

◆ COM4_BASE

#define COM4_BASE   0x2E8

Definition at line 14 of file sercons.c.

◆ COM4_IRQ

#define COM4_IRQ   3

Definition at line 19 of file sercons.c.

◆ UART_LCR_8N1

#define UART_LCR_8N1   0x03

Definition at line 27 of file sercons.c.

◆ UART_LCR_DLAB

#define UART_LCR_DLAB   0x80

Definition at line 26 of file sercons.c.

◆ UART_OFFSET_DATA

#define UART_OFFSET_DATA   0 /* Data Register */

Definition at line 22 of file sercons.c.

◆ UART_OFFSET_DIVHI

#define UART_OFFSET_DIVHI   1

Definition at line 34 of file sercons.c.

◆ UART_OFFSET_DIVLO

#define UART_OFFSET_DIVLO   0 /* Divisors DLAB == 1 */

Definition at line 33 of file sercons.c.

◆ UART_OFFSET_IER

#define UART_OFFSET_IER   1 /* Interrupt Enable Register */

Definition at line 23 of file sercons.c.

◆ UART_OFFSET_IIR

#define UART_OFFSET_IIR   2 /* Interrupt Identification & FIFO Control */

Definition at line 24 of file sercons.c.

◆ UART_OFFSET_LCR

#define UART_OFFSET_LCR   3 /* Line Control Register */

Definition at line 25 of file sercons.c.

◆ UART_OFFSET_LSR

#define UART_OFFSET_LSR   5 /* Line Status Register */

Definition at line 29 of file sercons.c.

◆ UART_OFFSET_MCR

#define UART_OFFSET_MCR   4 /* Modem Control Register */

Definition at line 28 of file sercons.c.

◆ UART_OFFSET_MSR

#define UART_OFFSET_MSR   6 /* Modem Status Register */

Definition at line 30 of file sercons.c.

◆ UART_OFFSET_SR

#define UART_OFFSET_SR   7 /* Scratch Register */

Definition at line 31 of file sercons.c.

Function Documentation

◆ Serial_Getc()

char Serial_Getc ( )

Definition at line 78 of file sercons.c.

79{
80 while ((inb(base + UART_OFFSET_LSR) & 0x01) == 0)
81 {
82 // Timeout!
83 }
84 return inb(base + UART_OFFSET_DATA);
85}
static INLINE uint8_t inb(uint16_t port)
Definition: amd64op.h:452
#define UART_OFFSET_DATA
Definition: sercons.c:22
static uint16_t base
Definition: sercons.c:37
#define UART_OFFSET_LSR
Definition: sercons.c:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serial_HasData()

bool Serial_HasData ( )

Definition at line 73 of file sercons.c.

74{
75 return (inb(base + UART_OFFSET_LSR) & 0x01) != 0;
76}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serial_Init()

void Serial_Init ( void  )

Definition at line 40 of file sercons.c.

41{
43 irq = COM1_IRQ;
44
45 // Disable interrupts
47
48 // Enable DLAB
50 outb(base + UART_OFFSET_DIVLO, 1); // 115200 Baud
53
54 // Enable interrupts
55 outb(base + UART_OFFSET_IIR, 0xC7);
56 outb(base + UART_OFFSET_MCR, 0x0B);
57}
static INLINE void outb(uint16_t port, uint8_t data)
Definition: amd64op.h:431
#define UART_OFFSET_DIVLO
Definition: sercons.c:33
#define UART_LCR_8N1
Definition: sercons.c:27
#define COM1_IRQ
Definition: sercons.c:16
#define UART_OFFSET_IER
Definition: sercons.c:23
#define UART_OFFSET_IIR
Definition: sercons.c:24
#define UART_OFFSET_LCR
Definition: sercons.c:25
#define COM1_BASE
Definition: sercons.c:11
#define UART_LCR_DLAB
Definition: sercons.c:26
#define UART_OFFSET_MCR
Definition: sercons.c:28
static uint8_t irq
Definition: sercons.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serial_Interrupt()

void Serial_Interrupt ( void *  arg)

Definition at line 68 of file sercons.c.

69{
70 kprintf("Serial interrupt!\n");
71}
int kprintf(const char *fmt,...)
Definition: printf.c:210
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serial_LateInit()

void Serial_LateInit ( void  )

Definition at line 59 of file sercons.c.

60{
61 handler.irq = irq;
64
66}
void IRQ_Register(int irq, struct IRQHandler *h)
Definition: irq.c:35
static IRQHandler handler
Definition: sercons.c:36
void Serial_Interrupt(void *arg)
Definition: sercons.c:68
#define NULL
Definition: stddef.h:6
void * arg
Definition: irq.h:10
int irq
Definition: irq.h:8
void(* cb)(void *)
Definition: irq.h:9
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serial_Putc()

void Serial_Putc ( char  ch)

Definition at line 87 of file sercons.c.

88{
89 while ((inb(base + UART_OFFSET_LSR) & 0x20) == 0)
90 {
91 // Timeout!
92 }
94
95 if (ch == '\b') {
96 Serial_Putc(0x1B);
97 Serial_Putc('[');
98 Serial_Putc('P');
99 }
100}
void Serial_Putc(char ch)
Definition: sercons.c:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serial_Puts()

void Serial_Puts ( const char *  str)

Definition at line 102 of file sercons.c.

103{
104 const char *p = str;
105 while (*p != '\0')
106 Serial_Putc(*p++);
107}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ base

uint16_t base
static

Definition at line 37 of file sercons.c.

◆ handler

IRQHandler handler
static

Definition at line 36 of file sercons.c.

◆ irq

uint8_t irq
static

Definition at line 38 of file sercons.c.