Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/soc/uart.h
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2019-2020 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#ifndef _UART_H_
19
#define _UART_H_
20
21
#include <utils/types.h>
22
23
#define UART_A 0
24
#define UART_B 1
25
#define UART_C 2
26
#define UART_D 3
27
#define UART_E 4
28
29
#define BAUD_115200 115200
30
31
#define UART_TX_IDLE BIT(0)
32
#define UART_RX_RDYR BIT(1)
33
34
#define UART_TX_FIFO_FULL BIT(8)
35
#define UART_RX_FIFO_EMPTY BIT(9)
36
37
#define UART_INVERT_RXD BIT(0)
38
#define UART_INVERT_TXD BIT(1)
39
#define UART_INVERT_CTS BIT(2)
40
#define UART_INVERT_RTS BIT(3)
41
42
#define UART_IER_DLAB_IE_EORD BIT(5)
43
44
#define UART_LCR_WORD_LENGTH_8 0x3
45
#define UART_LCR_STOP BIT(2)
46
#define UART_LCR_DLAB BIT(7)
47
48
#define UART_LSR_RDR BIT(0)
49
#define UART_LSR_THRE BIT(5)
50
#define UART_LSR_TMTY BIT(6)
51
#define UART_LSR_FIFOE BIT(7)
52
53
#define UART_IIR_FCR_EN_FIFO BIT(0)
54
#define UART_IIR_FCR_RX_CLR BIT(1)
55
#define UART_IIR_FCR_TX_CLR BIT(2)
56
57
#define UART_IIR_NO_INT BIT(0)
58
#define UART_IIR_INT_MASK 0xF
59
/* Custom returned interrupt results. Actual interrupts are -1 */
60
#define UART_IIR_NOI 0 // No interrupt.
61
#define UART_IIR_MSI 1 // Modem status interrupt.
62
#define UART_IIR_THRI 2 // Transmitter holding register empty.
63
#define UART_IIR_RDI 3 // Receiver data interrupt.
64
#define UART_IIR_ERROR 4 // Overrun Error, Parity Error, Framing Error, Break.
65
#define UART_IIR_REDI 5 // Receiver end of data interrupt.
66
#define UART_IIR_RDTI 7 // Receiver data timeout interrupt.
67
68
#define UART_MCR_DTR BIT(0)
69
#define UART_MCR_RTS BIT(1)
70
#define UART_MCR_CTS_EN BIT(5)
71
#define UART_MCR_RTS_EN BIT(6)
72
73
#define UART_FIFO_SIZE 36
74
75
typedef struct _uart_t
76
{
77
/* 0x00 */ vu32 UART_THR_DLAB;
78
/* 0x04 */ vu32 UART_IER_DLAB;
79
/* 0x08 */ vu32 UART_IIR_FCR;
80
/* 0x0C */ vu32 UART_LCR;
81
/* 0x10 */ vu32 UART_MCR;
82
/* 0x14 */ vu32 UART_LSR;
83
/* 0x18 */ vu32 UART_MSR;
84
/* 0x1C */ vu32 UART_SPR;
85
/* 0x20 */ vu32 UART_IRDA_CSR;
86
/* 0x24 */ vu32 UART_RX_FIFO_CFG;
87
/* 0x28 */ vu32 UART_MIE;
88
/* 0x2C */ vu32 UART_VENDOR_STATUS;
89
/* 0x30 */ u8 _pad_30[0xC];
90
/* 0x3C */ vu32 UART_ASR;
91
} uart_t;
92
93
//! TODO: Commented out modes are not supported yet.
94
typedef enum _uart_mode_t
95
{
96
UART_AO_TX_AO_RX = 0,
97
//UART_MN_TX_AO_RX = UART_MCR_RTS | UART_MCR_DTR,
98
UART_AO_TX_MN_RX = UART_MCR_RTS, // Up to 36 bytes read.
99
//UART_MN_TX_AO_RX = UART_MCR_DTR,
100
//UART_HW_TX_HW_RX = UART_MCR_RTS_EN | UART_MCR_CTS_EN,
101
UART_AO_TX_HW_RX = UART_MCR_RTS_EN,
102
//UART_HW_TX_AO_RX = UART_MCR_CTS_EN,
103
} uart_mode_t;
104
105
void uart_init(u32 idx, u32 baud, u32 mode);
106
void uart_wait_xfer(u32 idx, u32 which);
107
void uart_send(u32 idx, const u8 *buf, u32 len);
108
u32 uart_recv(u32 idx, u8 *buf, u32 len);
109
void uart_invert(u32 idx, bool enable, u32 invert_mask);
110
void uart_set_mode(u32 idx, u32 mode);
111
u32 uart_get_IIR(u32 idx);
112
void uart_set_IIR(u32 idx);
113
void uart_empty_fifo(u32 idx, u32 which);
114
#ifdef DEBUG_UART_PORT
115
void uart_printf(const char *fmt, ...);
116
#endif
117
118
#endif
119
120