blob: 573f0cb072e26dff215f5e3385fd36fa4bd08356 [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
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +030022#define UART_REPL UART0
Damien George075d5972014-11-27 20:30:33 +000023
24// UartDev is defined and initialized in rom code.
25extern UartDevice UartDev;
26
Damien George96eca222016-04-06 00:12:58 +030027// the uart to which OS messages go; -1 to disable
28static int uart_os = UART_OS;
29
Damien Georgefb6cc962016-04-01 14:30:47 +030030#if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +020031static os_event_t uart_evt_queue[16];
Damien Georgefb6cc962016-04-01 14:30:47 +030032#endif
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +020033
Damien George075d5972014-11-27 20:30:33 +000034static void uart0_rx_intr_handler(void *para);
35
Damien George6a051a82016-04-01 14:53:01 +030036void soft_reset(void);
37void mp_keyboard_interrupt(void);
38
39int interrupt_char;
40
Damien George075d5972014-11-27 20:30:33 +000041/******************************************************************************
42 * FunctionName : uart_config
43 * Description : Internal used function
44 * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
45 * UART1 just used for debug output
46 * Parameters : uart_no, use UART0 or UART1 defined ahead
47 * Returns : NONE
48*******************************************************************************/
49static void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) {
50 if (uart_no == UART1) {
51 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
52 } else {
53 ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, NULL);
54 PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
55 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
56 PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
57 }
58
59 uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
60
61 WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity
62 | UartDev.parity
63 | (UartDev.stop_bits << UART_STOP_BIT_NUM_S)
64 | (UartDev.data_bits << UART_BIT_NUM_S));
65
66 // clear rx and tx fifo,not ready
67 SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
68 CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
69
70 if (uart_no == UART0) {
71 // set rx fifo trigger
72 WRITE_PERI_REG(UART_CONF1(uart_no),
73 ((0x10 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
74 ((0x10 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
75 UART_RX_FLOW_EN |
76 (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
77 UART_RX_TOUT_EN);
78 SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA |
79 UART_FRM_ERR_INT_ENA);
80 } else {
81 WRITE_PERI_REG(UART_CONF1(uart_no),
82 ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));
83 }
84
85 // clear all interrupt
86 WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
87 // enable rx_interrupt
88 SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
Damien George075d5972014-11-27 20:30:33 +000089}
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 George96eca222016-04-06 00:12:58 +0300126 if (uart_os == -1) {
127 return;
128 }
Damien George075d5972014-11-27 20:30:33 +0000129 if (c == '\n') {
Damien George96eca222016-04-06 00:12:58 +0300130 uart_tx_one_char(uart_os, '\r');
131 uart_tx_one_char(uart_os, '\n');
Damien George075d5972014-11-27 20:30:33 +0000132 } else if (c == '\r') {
133 } else {
Damien George96eca222016-04-06 00:12:58 +0300134 uart_tx_one_char(uart_os, c);
Damien George075d5972014-11-27 20:30:33 +0000135 }
136}
137
Damien George96eca222016-04-06 00:12:58 +0300138void ICACHE_FLASH_ATTR
139uart_os_config(int uart) {
140 uart_os = uart;
141}
142
Damien George075d5972014-11-27 20:30:33 +0000143/******************************************************************************
144 * FunctionName : uart0_rx_intr_handler
145 * Description : Internal used function
146 * UART0 interrupt handler, add self handle code inside
147 * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
148 * Returns : NONE
149*******************************************************************************/
150
151static void uart0_rx_intr_handler(void *para) {
152 /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
153 * uart1 and uart0 respectively
154 */
155
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300156 uint8 uart_no = UART_REPL;
Damien George075d5972014-11-27 20:30:33 +0000157
158 if (UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)) {
159 // frame error
160 WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR);
161 }
162
163 if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)) {
164 // fifo full
Damien George075d5972014-11-27 20:30:33 +0000165 goto read_chars;
166 } 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 +0000167 read_chars:
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300168 ETS_UART_INTR_DISABLE();
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300169
170 while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
171 uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
Damien George6a051a82016-04-01 14:53:01 +0300172 if (RcvChar == interrupt_char) {
173 mp_keyboard_interrupt();
174 } else {
175 ringbuf_put(&input_buf, RcvChar);
176 }
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300177 }
178
179 mp_hal_signal_input();
180
181 // Clear pending FIFO interrupts
182 WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
183 ETS_UART_INTR_ENABLE();
Damien George075d5972014-11-27 20:30:33 +0000184 }
185}
186
Damien George7652ab72016-04-21 15:19:00 +0100187// Waits at most timeout microseconds for at least 1 char to become ready for reading.
188// Returns true if something available, false if not.
189bool uart_rx_wait(uint32_t timeout_us) {
190 uint32_t start = system_get_time();
191 for (;;) {
192 if (input_buf.iget != input_buf.iput) {
193 return true; // have at least 1 char ready for reading
194 }
195 if (system_get_time() - start >= timeout_us) {
196 return false; // timeout
197 }
198 ets_event_poll();
199 }
200}
201
202// Returns char from the input buffer, else -1 if buffer is empty.
203int uart_rx_char(void) {
204 return ringbuf_get(&input_buf);
205}
206
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300207int uart_rx_one_char(uint8 uart_no) {
208 if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
209 return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
210 }
211 return -1;
212}
213
Damien George075d5972014-11-27 20:30:33 +0000214/******************************************************************************
215 * FunctionName : uart_init
216 * Description : user interface for init uart
217 * Parameters : UartBautRate uart0_br - uart0 bautrate
218 * UartBautRate uart1_br - uart1 bautrate
219 * Returns : NONE
220*******************************************************************************/
221void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) {
222 // rom use 74880 baut_rate, here reinitialize
223 UartDev.baut_rate = uart0_br;
224 uart_config(UART0);
225 UartDev.baut_rate = uart1_br;
226 uart_config(UART1);
227 ETS_UART_INTR_ENABLE();
228
Paul Sokolovskyc4506ed2016-03-29 21:10:10 +0300229 // install handler for "os" messages
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200230 os_install_putc1((void *)uart_os_write_char);
Damien George075d5972014-11-27 20:30:33 +0000231}
232
233void ICACHE_FLASH_ATTR uart_reattach() {
234 uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
235}
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200236
237// Task-based UART interface
238
Damien Georgec98c1282015-05-06 00:02:58 +0100239#include "py/obj.h"
Damien George40274fe2015-11-09 13:13:09 +0000240#include "lib/utils/pyexec.h"
Damien Georgec98c1282015-05-06 00:02:58 +0100241
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300242#if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200243void uart_task_handler(os_event_t *evt) {
Paul Sokolovsky777232c2016-04-01 12:53:50 +0300244 if (pyexec_repl_active) {
245 // TODO: Just returning here isn't exactly right.
246 // What really should be done is something like
247 // enquing delayed event to itself, for another
248 // chance to feed data to REPL. Otherwise, there
249 // can be situation when buffer has bunch of data,
250 // and sits unprocessed, because we consumed all
251 // processing signals like this.
252 return;
253 }
254
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300255 int c, ret = 0;
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300256 while ((c = ringbuf_get(&input_buf)) >= 0) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200257 if (c == interrupt_char) {
258 mp_keyboard_interrupt();
259 }
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300260 ret = pyexec_event_repl_process_char(c);
261 if (ret & PYEXEC_FORCED_EXIT) {
262 break;
263 }
264 }
265
Damien Georgec98c1282015-05-06 00:02:58 +0100266 if (ret & PYEXEC_FORCED_EXIT) {
267 soft_reset();
268 }
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200269}
270
271void uart_task_init() {
272 system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue));
273}
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300274#endif