blob: de0919bde056de54383453fc0f9ba35b11a0ed9e [file] [log] [blame]
Alexander Steffen299bc622017-06-29 23:14:58 +02001#ifndef MICROPY_INCLUDED_ESP8266_UART_H
2#define MICROPY_INCLUDED_ESP8266_UART_H
Damien George075d5972014-11-27 20:30:33 +00003
daniel-kaa4ada92016-07-26 17:56:13 +02004#include <eagle_soc.h>
5
Damien George075d5972014-11-27 20:30:33 +00006#define UART0 (0)
7#define UART1 (1)
8
Damien George52bec932018-12-05 23:31:24 +11009#define UART0_STATIC_RXBUF_LEN (16)
10
Damien George075d5972014-11-27 20:30:33 +000011typedef enum {
12 UART_FIVE_BITS = 0x0,
13 UART_SIX_BITS = 0x1,
14 UART_SEVEN_BITS = 0x2,
15 UART_EIGHT_BITS = 0x3
16} UartBitsNum4Char;
17
18typedef enum {
Ein Terakawaab2c64c2017-08-16 10:23:27 +000019 UART_ONE_STOP_BIT = 0x1,
20 UART_ONE_HALF_STOP_BIT = 0x2,
21 UART_TWO_STOP_BIT = 0x3
Damien George075d5972014-11-27 20:30:33 +000022} UartStopBitsNum;
23
24typedef enum {
daniel-kaa4ada92016-07-26 17:56:13 +020025 UART_NONE_BITS = 0,
26 UART_ODD_BITS = BIT0,
27 UART_EVEN_BITS = 0
Damien George075d5972014-11-27 20:30:33 +000028} UartParityMode;
29
30typedef enum {
31 UART_STICK_PARITY_DIS = 0,
daniel-kaa4ada92016-07-26 17:56:13 +020032 UART_STICK_PARITY_EN = BIT1
Damien George075d5972014-11-27 20:30:33 +000033} UartExistParity;
34
35typedef enum {
36 UART_BIT_RATE_9600 = 9600,
37 UART_BIT_RATE_19200 = 19200,
38 UART_BIT_RATE_38400 = 38400,
39 UART_BIT_RATE_57600 = 57600,
40 UART_BIT_RATE_74880 = 74880,
41 UART_BIT_RATE_115200 = 115200,
42 UART_BIT_RATE_230400 = 230400,
43 UART_BIT_RATE_256000 = 256000,
44 UART_BIT_RATE_460800 = 460800,
45 UART_BIT_RATE_921600 = 921600
46} UartBautRate;
47
48typedef enum {
49 UART_NONE_CTRL,
50 UART_HARDWARE_CTRL,
51 UART_XON_XOFF_CTRL
52} UartFlowCtrl;
53
54typedef enum {
55 UART_EMPTY,
56 UART_UNDER_WRITE,
57 UART_WRITE_OVER
58} RcvMsgBuffState;
59
60typedef struct {
Damien George69661f32020-02-27 15:36:53 +110061 uint32 RcvBuffSize;
62 uint8 *pRcvMsgBuff;
63 uint8 *pWritePos;
64 uint8 *pReadPos;
stijn84fa3312020-04-16 09:13:57 +020065 uint8 TrigLvl; // JLU: may need to pad
Damien George69661f32020-02-27 15:36:53 +110066 RcvMsgBuffState BuffState;
Damien George075d5972014-11-27 20:30:33 +000067} RcvMsgBuff;
68
69typedef struct {
Damien George69661f32020-02-27 15:36:53 +110070 uint32 TrxBuffSize;
71 uint8 *pTrxBuff;
Damien George075d5972014-11-27 20:30:33 +000072} TrxMsgBuff;
73
74typedef enum {
75 UART_BAUD_RATE_DET,
76 UART_WAIT_SYNC_FRM,
77 UART_SRCH_MSG_HEAD,
78 UART_RCV_MSG_BODY,
79 UART_RCV_ESC_CHAR,
80} RcvMsgState;
81
82typedef struct {
Damien George69661f32020-02-27 15:36:53 +110083 UartBautRate baut_rate;
84 UartBitsNum4Char data_bits;
85 UartExistParity exist_parity;
86 UartParityMode parity; // chip size in byte
87 UartStopBitsNum stop_bits;
88 UartFlowCtrl flow_ctrl;
89 RcvMsgBuff rcv_buff;
90 TrxMsgBuff trx_buff;
91 RcvMsgState rcv_state;
92 int received;
stijn84fa3312020-04-16 09:13:57 +020093 int buff_uart_no; // indicate which uart use tx/rx buffer
Damien George075d5972014-11-27 20:30:33 +000094} UartDevice;
95
Damien George52bec932018-12-05 23:31:24 +110096extern uint8 uart_ringbuf_array[UART0_STATIC_RXBUF_LEN];
97
Damien George075d5972014-11-27 20:30:33 +000098void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
99int uart0_rx(void);
Damien George7652ab72016-04-21 15:19:00 +0100100bool uart_rx_wait(uint32_t timeout_us);
101int uart_rx_char(void);
Damien George075d5972014-11-27 20:30:33 +0000102void uart_tx_one_char(uint8 uart, uint8 TxChar);
Paul Sokolovskyd9d4a722016-01-03 07:33:42 +0200103void uart_flush(uint8 uart);
Damien George96eca222016-04-06 00:12:58 +0300104void uart_os_config(int uart);
Radomir Dopieralski35962ea2016-06-29 14:18:48 +0200105void uart_setup(uint8 uart);
Damien George52bec932018-12-05 23:31:24 +1100106int uart0_get_rxbuf_len(void);
107void uart0_set_rxbuf(uint8 *buf, int len);
marc hoffman91eb0152017-01-29 21:48:55 -0500108// check status of rx/tx
109int uart_rx_any(uint8 uart);
110int uart_tx_any_room(uint8 uart);
Damien George075d5972014-11-27 20:30:33 +0000111
Alexander Steffen299bc622017-06-29 23:14:58 +0200112#endif // MICROPY_INCLUDED_ESP8266_UART_H