summaryrefslogtreecommitdiffhomepage
path: root/src/rv64/rv64-uart.c
blob: 22b079362ab541feec0701e4855417a4cb89fa77 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* -------------------------------------------

  Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

#include <lib/boot.h>
#include <lib/string.h>

/// BUGS: 0

/* this file handles the UART */

static uint8_t* cb_uart_ptr = (uint8_t*) NB_UART_BASE;

utf_char_t cb_get_char(void) {
  uintptr_t ptr = NB_UART_BASE;

  while (!(*(((volatile uint8_t*) ptr) + 0x05) & 0x01))
    ;

  return (utf_char_t) *cb_uart_ptr;
}

// we need that one, to avoid sending mutliple chars to UART.
static boolean cb_locked_put_char = no;

void cb_put_char(utf_char_t ch) {
  int32_t timeout = 0;

  while (cb_locked_put_char) {
    ++timeout;

    if (timeout > 1000000) break;
  }

  cb_locked_put_char = yes;
  *cb_uart_ptr       = ch;
  cb_locked_put_char = no;
}

/// @brief UART put string
/// @param text the input text.
size_t cb_put_string(const char* text) {
  if (text == nil) return 0;

  size_t i = 0;

  for (; i < strlen(text); i++) {
    cb_put_char(text[i]);
  }

  return i;
}