-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCH56x_uart.h
141 lines (113 loc) · 6.46 KB
/
CH56x_uart.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/********************************** (C) COPYRIGHT *******************************
* File Name : CH56x_uart.h
* Author : WCH, bvernoux
* Version : V1.1
* Date : 2022/08/07
* Description
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Copyright (c) 2022 Benjamin VERNOUX
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __CH56x_UART_H__
#define __CH56x_UART_H__
#include "CH56xSFR.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Line Error Status Definition
*/
#define STA_ERR_BREAK RB_LSR_BREAK_ERR // Data interval error
#define STA_ERR_FRAME RB_LSR_FRAME_ERR // Data frame error
#define STA_ERR_PAR RB_LSR_PAR_ERR // Parity bit error
#define STA_ERR_FIFOOV RB_LSR_OVER_ERR // Receive data overflow
#define STA_TXFIFO_EMP RB_LSR_TX_FIFO_EMP // The current transmit FIFO is empty and can continue to fill the transmit data
#define STA_TXALL_EMP RB_LSR_TX_ALL_EMP // All data have been sent
#define STA_RECV_DATA RB_LSR_DATA_RDY // Currently receiving data
/**
* @brief Serial port byte trigger configuration
*/
typedef enum
{
UART_1BYTE_TRIG = 0, // 1 Byte trigger
UART_2BYTE_TRIG = 1, // 2 Byte trigger
UART_4BYTE_TRIG = 2, // 4 Byte trigger
UART_7BYTE_TRIG = 3, // 7 Byte trigger
} UARTByteTRIGTypeDef;
/****************** UART0 */
void UART0_init(uint32_t baudrate, uint32_t systemclck);
void UART0_DefInit(void); /* Serial Default initialization configuration */
void UART0_BaudRateCfg(uint32_t baudrate); /* Serial Baud rate configuration */
void UART0_ByteTrigCfg(UARTByteTRIGTypeDef b); /*Serial Byte-triggered interrupt configuration */
void UART0_INTCfg(uint8_t s, uint8_t i); /* Serial Interrupt configuration */
void UART0_Reset(void); /* Serial Software reset */
#define UART0_CLR_RXFIFO() (R8_UART0_FCR |= RB_FCR_RX_FIFO_CLR) /* Clear current RX FIFO */
#define UART0_CLR_TXFIFO() (R8_UART0_FCR |= RB_FCR_TX_FIFO_CLR) /* Clear the current TX FIFO */
#define UART0_GetITFlag() (R8_UART0_IIR & RB_IIR_INT_MASK) /* Get current interrupt flag */
// please refer to LINE error and status define
#define UART0_GetLinSTA() (R8_UART0_LSR) /* Get current communication status */
#define UART0_GetMSRSTA() (R8_UART0_MSR) /* Get the current flow control state, only applicable UART0 */
#define UART0_SendByte(b) (R8_UART0_THR = b) /* Serial Single byte send */
void UART0_SendString(puint8_t buf, uint16_t l); /* Serial Multibyte send */
#define UART0_tx UART0_SendString
#define UART0_RecvByte() (R8_UART0_RBR) /* Serial Read single byte */
uint16_t UART0_RecvString(puint8_t buf); /*Serial Read multiple bytes */
uint16_t UART0_rx(uint8_t* buf, int buf_len_max);
/****************** UART1 */
void UART1_init(uint32_t baudrate, uint32_t systemclck);
void UART1_DefInit(void); /*Serial Default initialization configuration */
void UART1_BaudRateCfg(uint32_t baudrate); /*Serial Baud rate configuration */
void UART1_ByteTrigCfg(UARTByteTRIGTypeDef b); /*Serial Byte-triggered interrupt configuration */
void UART1_INTCfg(uint8_t s, uint8_t i); /*Serial Interrupt configuration */
void UART1_Reset(void); /*Serial Software reset */
#define UART1_CLR_RXFIFO() (R8_UART1_FCR |= RB_FCR_RX_FIFO_CLR) /* Clear current receive FIFO */
#define UART1_CLR_TXFIFO() (R8_UART1_FCR |= RB_FCR_TX_FIFO_CLR) /* Clear the current TX FIFO */
#define UART1_GetITFlag() (R8_UART1_IIR&RB_IIR_INT_MASK) /* Get current interrupt flag */
// please refer to LINE error and status define
#define UART1_GetLinSTA() (R8_UART1_LSR) /* Get current communication status */
#define UART1_SendByte(b) (R8_UART1_THR = b) /*Serial Single byte send */
void UART1_SendString(puint8_t buf, uint16_t l); /*Serial Multibyte send */
#define UART1_tx UART1_SendString
#define UART1_RecvByte() (R8_UART1_RBR) /* Serial Read single byte */
uint16_t UART1_RecvString(puint8_t buf); /* Serial Read multiple bytes */
uint16_t UART1_rx(uint8_t* buf, int buf_len_max);
/****************** UART2 */
void UART2_init(uint32_t baudrate, uint32_t systemclck);
void UART2_DefInit(void); /*Serial Default initialization configuration */
void UART2_BaudRateCfg(uint32_t baudrate); /*Serial Baud rate configuration */
void UART2_ByteTrigCfg(UARTByteTRIGTypeDef b); /*Serial Byte-triggered interrupt configuration */
void UART2_INTCfg(uint8_t s, uint8_t i); /*Serial Interrupt configuration */
void UART2_Reset(void); /*Serial Software reset */
#define UART2_CLR_RXFIFO() (R8_UART2_FCR |= RB_FCR_RX_FIFO_CLR) /* Clear current receive FIFO */
#define UART2_CLR_TXFIFO() (R8_UART2_FCR |= RB_FCR_TX_FIFO_CLR) /* Clear the current TX FIFO */
#define UART2_GetITFlag() (R8_UART2_IIR&RB_IIR_INT_MASK) /* Get current interrupt flag */
// please refer to LINE error and status define
#define UART2_GetLinSTA() (R8_UART2_LSR) /* Get current communication status */
#define UART2_SendByte(b) (R8_UART2_THR = b) /* Serial Single byte send */
void UART2_SendString(puint8_t buf, uint16_t l); /* Serial Multibyte send */
#define UART2_tx UART2_SendString
#define UART2_RecvByte() (R8_UART2_RBR) /* Serial Read single byte */
uint16_t UART2_RecvString(puint8_t buf); /* Serial Read multiple bytes */
uint16_t UART2_rx(uint8_t* buf, int buf_len_max);
/****************** UART3 */
void UART3_init(uint32_t baudrate, uint32_t systemclck);
void UART3_DefInit(void); /* Serial Default initialization configuration */
void UART3_BaudRateCfg(uint32_t baudrate); /* Serial Baud rate configuration */
void UART3_ByteTrigCfg(UARTByteTRIGTypeDef b); /* Serial Byte-triggered interrupt configuration */
void UART3_INTCfg(uint8_t s, uint8_t i); /* Serial Interrupt configuration */
void UART3_Reset(void); /* Serial Software reset */
#define UART3_CLR_RXFIFO() (R8_UART3_FCR |= RB_FCR_RX_FIFO_CLR) /* Clear current RX FIFO */
#define UART3_CLR_TXFIFO() (R8_UART3_FCR |= RB_FCR_TX_FIFO_CLR) /* Clear the current TX FIFO */
#define UART3_GetITFlag() (R8_UART3_IIR&RB_IIR_INT_MASK) /* Get current interrupt flag */
// please refer to LINE error and status define
#define UART3_GetLinSTA() (R8_UART3_LSR) /* Get current communication status */
#define UART3_SendByte(b) (R8_UART3_THR = b) /*Serial Single byte send */
void UART3_SendString(puint8_t buf, uint16_t l); /*Serial Multibyte send */
#define UART3_tx UART3_SendString
#define UART3_RecvByte() (R8_UART3_RBR) /*Serial Read single byte */
uint16_t UART3_RecvString(puint8_t buf); /*Serial Read multiple bytes */
uint16_t UART3_rx(uint8_t* buf, int buf_len_max);
#ifdef __cplusplus
}
#endif
#endif // __CH56x_UART_H__