blob: c4d08eac4b45a5fc83708cabacda4f7c56c8edb5 [file] [log] [blame]
Damien George075d5972014-11-27 20:30:33 +00001/******************************************************************************
2 * Copyright 2013-2014 Espressif Systems (Wuxi)
3 *
4 * FileName: uart.c
5 *
6 * Description: Two UART mode configration and interrupt handler.
7 * Check your hardware connection while use this mode.
8 *
9 * Modification history:
10 * 2014/3/12, v1.0 create this file.
11*******************************************************************************/
12#include "ets_sys.h"
13#include "osapi.h"
14#include "uart.h"
15#include "osapi.h"
16#include "uart_register.h"
17#include "etshal.h"
18#include "c_types.h"
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +020019#include "user_interface.h"
20#include "esp_mphal.h"
Damien George075d5972014-11-27 20:30:33 +000021
22#define RX_BUF_SIZE (256)
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +030023#define UART_REPL UART0
Damien George075d5972014-11-27 20:30:33 +000024
25// UartDev is defined and initialized in rom code.
26extern UartDevice UartDev;
27
28// circular buffer for RX buffering
29static uint16_t rx_buf_in;
30static uint16_t rx_buf_out;
31static uint8_t rx_buf[RX_BUF_SIZE];
32
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +020033static os_event_t uart_evt_queue[16];
34
Damien George075d5972014-11-27 20:30:33 +000035static void uart0_rx_intr_handler(void *para);
36
37/******************************************************************************
38 * FunctionName : uart_config
39 * Description : Internal used function
40 * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
41 * UART1 just used for debug output
42 * Parameters : uart_no, use UART0 or UART1 defined ahead
43 * Returns : NONE
44*******************************************************************************/
45static void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) {
46 if (uart_no == UART1) {
47 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
48 } else {
49 ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, NULL);
50 PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
51 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
52 PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
53 }
54
55 uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
56
57 WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity
58 | UartDev.parity
59 | (UartDev.stop_bits << UART_STOP_BIT_NUM_S)
60 | (UartDev.data_bits << UART_BIT_NUM_S));
61
62 // clear rx and tx fifo,not ready
63 SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
64 CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
65
66 if (uart_no == UART0) {
67 // set rx fifo trigger
68 WRITE_PERI_REG(UART_CONF1(uart_no),
69 ((0x10 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
70 ((0x10 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
71 UART_RX_FLOW_EN |
72 (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
73 UART_RX_TOUT_EN);
74 SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA |
75 UART_FRM_ERR_INT_ENA);
76 } else {
77 WRITE_PERI_REG(UART_CONF1(uart_no),
78 ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));
79 }
80
81 // clear all interrupt
82 WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
83 // enable rx_interrupt
84 SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
85
86 // init RX buffer
87 rx_buf_in = 0;
88 rx_buf_out = 0;
89}
90
91/******************************************************************************
92 * FunctionName : uart1_tx_one_char
93 * Description : Internal used function
94 * Use uart1 interface to transfer one char
95 * Parameters : uint8 TxChar - character to tx
96 * Returns : OK
97*******************************************************************************/
98void uart_tx_one_char(uint8 uart, uint8 TxChar) {
99 while (true) {
100 uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
101 if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
102 break;
103 }
104 }
105 WRITE_PERI_REG(UART_FIFO(uart), TxChar);
106}
107
Paul Sokolovskyd9d4a722016-01-03 07:33:42 +0200108void uart_flush(uint8 uart) {
109 while (true) {
110 uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
111 if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) == 0) {
112 break;
113 }
114 }
115}
116
Damien George075d5972014-11-27 20:30:33 +0000117/******************************************************************************
118 * FunctionName : uart1_write_char
119 * Description : Internal used function
120 * Do some special deal while tx char is '\r' or '\n'
121 * Parameters : char c - character to tx
122 * Returns : NONE
123*******************************************************************************/
124static void ICACHE_FLASH_ATTR
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200125uart_os_write_char(char c) {
Damien George075d5972014-11-27 20:30:33 +0000126 if (c == '\n') {
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200127 uart_tx_one_char(UART_OS, '\r');
128 uart_tx_one_char(UART_OS, '\n');
Damien George075d5972014-11-27 20:30:33 +0000129 } else if (c == '\r') {
130 } else {
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200131 uart_tx_one_char(UART_OS, c);
Damien George075d5972014-11-27 20:30:33 +0000132 }
133}
134
135/******************************************************************************
136 * FunctionName : uart0_rx_intr_handler
137 * Description : Internal used function
138 * UART0 interrupt handler, add self handle code inside
139 * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
140 * Returns : NONE
141*******************************************************************************/
142
143static void uart0_rx_intr_handler(void *para) {
144 /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
145 * uart1 and uart0 respectively
146 */
147
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300148 uint8 uart_no = UART_REPL;
Damien George075d5972014-11-27 20:30:33 +0000149
150 if (UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)) {
151 // frame error
152 WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR);
153 }
154
155 if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)) {
156 // fifo full
Damien George075d5972014-11-27 20:30:33 +0000157 goto read_chars;
158 } else if (UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) {
Damien George075d5972014-11-27 20:30:33 +0000159 read_chars:
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200160#if 1 //MICROPY_REPL_EVENT_DRIVEN is not available here
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300161 ETS_UART_INTR_DISABLE();
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300162
163 while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
164 uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
165 ringbuf_put(&input_buf, RcvChar);
166 }
167
168 mp_hal_signal_input();
169
170 // Clear pending FIFO interrupts
171 WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
172 ETS_UART_INTR_ENABLE();
173
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200174#else
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300175 while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
176 uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
Damien George075d5972014-11-27 20:30:33 +0000177 uint16_t rx_buf_in_next = (rx_buf_in + 1) % RX_BUF_SIZE;
178 if (rx_buf_in_next != rx_buf_out) {
179 rx_buf[rx_buf_in] = RcvChar;
180 rx_buf_in = rx_buf_in_next;
181 }
182 }
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300183#endif
Damien George075d5972014-11-27 20:30:33 +0000184 }
185}
186
187int uart0_rx(void) {
188 if (rx_buf_out != rx_buf_in) {
189 int chr = rx_buf[rx_buf_out];
190 rx_buf_out = (rx_buf_out + 1) % RX_BUF_SIZE;
191 return chr;
192 } else {
193 return -1;
194 }
195}
196
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300197int uart_rx_one_char(uint8 uart_no) {
198 if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
199 return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
200 }
201 return -1;
202}
203
Damien George075d5972014-11-27 20:30:33 +0000204/******************************************************************************
205 * FunctionName : uart_init
206 * Description : user interface for init uart
207 * Parameters : UartBautRate uart0_br - uart0 bautrate
208 * UartBautRate uart1_br - uart1 bautrate
209 * Returns : NONE
210*******************************************************************************/
211void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) {
212 // rom use 74880 baut_rate, here reinitialize
213 UartDev.baut_rate = uart0_br;
214 uart_config(UART0);
215 UartDev.baut_rate = uart1_br;
216 uart_config(UART1);
217 ETS_UART_INTR_ENABLE();
218
Paul Sokolovskyc4506ed2016-03-29 21:10:10 +0300219 // install handler for "os" messages
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200220 os_install_putc1((void *)uart_os_write_char);
Damien George075d5972014-11-27 20:30:33 +0000221}
222
223void ICACHE_FLASH_ATTR uart_reattach() {
224 uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
225}
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200226
227// Task-based UART interface
228
Damien Georgec98c1282015-05-06 00:02:58 +0100229#include "py/obj.h"
Damien George40274fe2015-11-09 13:13:09 +0000230#include "lib/utils/pyexec.h"
Damien Georgec98c1282015-05-06 00:02:58 +0100231
232void soft_reset(void);
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200233void mp_keyboard_interrupt(void);
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200234
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200235int interrupt_char;
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200236void uart_task_handler(os_event_t *evt) {
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300237 int c, ret = 0;
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300238 while ((c = ringbuf_get(&input_buf)) >= 0) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200239 if (c == interrupt_char) {
240 mp_keyboard_interrupt();
241 }
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300242 ret = pyexec_event_repl_process_char(c);
243 if (ret & PYEXEC_FORCED_EXIT) {
244 break;
245 }
246 }
247
Damien Georgec98c1282015-05-06 00:02:58 +0100248 if (ret & PYEXEC_FORCED_EXIT) {
249 soft_reset();
250 }
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200251}
252
253void uart_task_init() {
254 system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue));
255}