-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuart.c
75 lines (65 loc) · 1.59 KB
/
uart.c
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stddef.h>
#include <stdint.h>
#include "uart.h"
#include "ports.h"
#include "ccu.h"
// Set up the UART (serial port)
void uart_init()
{
// Configure port
set_pin_mode(PORTA, 4, 2);
// Enable clock
BUS_CLK_GATING3 |= (1<<16);
BUS_SOFT_RST4 |= (1<<16);
// Configure baud rate
UART0_LCR = (1<<7) | 3;
UART0_DLL = 13;
UART0_LCR = 3;
// Enable FIFO
UART0_FCR = 0x00000001;
}
// UART is ready to receive data to transmit?
unsigned char uart_tx_ready()
{
return (UART0_USR & 2);
}
// UART has received data?
unsigned char uart_rx_ready()
{
return (UART0_LSR & 1);
}
// Push one byte to the UART port (blocking until ready to transmit)
void uart_putc(unsigned char byte)
{
// Wait for UART transmit FIFO to be not full.
while ( ! uart_tx_ready() );
UART0_THR = byte;
}
// Write a zero terminated string to the UART
void uart_print(const char* str)
{
while(*str) {
uart_putc(*str);
str++;
}
}
// Print a char to the UART as ASCII HEX
void uart_print_uint8(unsigned char number)
{
unsigned char chars[] = "0123456789ABCDEF";
uart_putc(chars[(number >> 4) & 0xF]);
uart_putc(chars[(number >> 0) & 0xF]);
}
// Print a uint32 to the UART as ASCII HEX
void uart_print_uint32(uint32_t number)
{
unsigned char chars[] = "0123456789ABCDEF";
uart_putc(chars[(number >> 28) & 0xF]);
uart_putc(chars[(number >> 24) & 0xF]);
uart_putc(chars[(number >> 20) & 0xF]);
uart_putc(chars[(number >> 16) & 0xF]);
uart_putc(chars[(number >> 12) & 0xF]);
uart_putc(chars[(number >> 8) & 0xF]);
uart_putc(chars[(number >> 4) & 0xF]);
uart_putc(chars[(number >> 0) & 0xF]);
}