summaryrefslogtreecommitdiff
path: root/src/kernel/drivers/serial.c
blob: 25f89adf92f4ef188aca1e8bbbd45e486fa7bc3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdint.h>
#include <serial.h>
static inline void outb(uint16_t port, uint8_t value) {
  asm volatile(
      "outb %0, %1" :: "a"(value), "Nd"(port)
      );
}

static inline uint8_t inb(uint16_t port) {
  uint8_t ret;
  asm volatile(
      "inb %1, %0" : "=a"(ret) : "Nd"(port)
      );
  return(ret);
}

int init_serial(uint16_t port) {
  outb(port + 1, 0x00); 
  outb(port + 3, 0x80); 
  outb(port + 0, 0x06); 
  outb(port + 1, 0x00);
  outb(port + 3, 0x03); 
  outb(port + 2, 0xc7); 
  outb(port + 4, 0x0b); 
  outb(port + 4, 0x1e); 

  outb(port + 0, 0xae); // test char

  if(inb(port + 0) != 0xae)
    return 1;

  outb(port + 4, 0x0f); // dissable interupts
  return 0;
}

void _putchar_serial(uint16_t port, char msg) {
  while(!(inb(port + 5) & 0x20)); //wait for transmit to be done
  outb(port, msg);
}