blob: 1c317a1cb6a4f8cd0db4217dbec92f44724299c6 [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
Damien George602305b2016-05-29 10:30:27 +010022// seems that this is missing in the Espressif SDK
23#define FUNC_U0RXD 0
24
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +030025#define UART_REPL UART0
Damien George075d5972014-11-27 20:30:33 +000026
27// UartDev is defined and initialized in rom code.
28extern UartDevice UartDev;
29
Damien George96eca222016-04-06 00:12:58 +030030// the uart to which OS messages go; -1 to disable
31static int uart_os = UART_OS;
32
Damien Georgefb6cc962016-04-01 14:30:47 +030033#if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +020034static os_event_t uart_evt_queue[16];
Damien Georgefb6cc962016-04-01 14:30:47 +030035#endif
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +020036
Damien George075d5972014-11-27 20:30:33 +000037static void uart0_rx_intr_handler(void *para);
38
Damien George6a051a82016-04-01 14:53:01 +030039void soft_reset(void);
40void mp_keyboard_interrupt(void);
41
42int interrupt_char;
43
Damien George075d5972014-11-27 20:30:33 +000044/******************************************************************************
45 * FunctionName : uart_config
46 * Description : Internal used function
47 * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
48 * UART1 just used for debug output
49 * Parameters : uart_no, use UART0 or UART1 defined ahead
50 * Returns : NONE
51*******************************************************************************/
52static void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) {
53 if (uart_no == UART1) {
54 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
55 } else {
56 ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, NULL);
57 PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
58 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
Damien George602305b2016-05-29 10:30:27 +010059 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);
Damien George075d5972014-11-27 20:30:33 +000060 }
61
62 uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
63
64 WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity
65 | UartDev.parity
66 | (UartDev.stop_bits << UART_STOP_BIT_NUM_S)
67 | (UartDev.data_bits << UART_BIT_NUM_S));
68
69 // clear rx and tx fifo,not ready
70 SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
71 CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
72
73 if (uart_no == UART0) {
74 // set rx fifo trigger
75 WRITE_PERI_REG(UART_CONF1(uart_no),
76 ((0x10 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
77 ((0x10 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
78 UART_RX_FLOW_EN |
79 (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
80 UART_RX_TOUT_EN);
81 SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA |
82 UART_FRM_ERR_INT_ENA);
83 } else {
84 WRITE_PERI_REG(UART_CONF1(uart_no),
85 ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));
86 }
87
88 // clear all interrupt
89 WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
90 // enable rx_interrupt
91 SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
Damien George075d5972014-11-27 20:30:33 +000092}
93
94/******************************************************************************
95 * FunctionName : uart1_tx_one_char
96 * Description : Internal used function
97 * Use uart1 interface to transfer one char
98 * Parameters : uint8 TxChar - character to tx
99 * Returns : OK
100*******************************************************************************/
101void uart_tx_one_char(uint8 uart, uint8 TxChar) {
102 while (true) {
103 uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
104 if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
105 break;
106 }
107 }
108 WRITE_PERI_REG(UART_FIFO(uart), TxChar);
109}
110
Paul Sokolovskyd9d4a722016-01-03 07:33:42 +0200111void uart_flush(uint8 uart) {
112 while (true) {
113 uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
114 if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) == 0) {
115 break;
116 }
117 }
118}
119
Damien George075d5972014-11-27 20:30:33 +0000120/******************************************************************************
121 * FunctionName : uart1_write_char
122 * Description : Internal used function
123 * Do some special deal while tx char is '\r' or '\n'
124 * Parameters : char c - character to tx
125 * Returns : NONE
126*******************************************************************************/
127static void ICACHE_FLASH_ATTR
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200128uart_os_write_char(char c) {
Damien George96eca222016-04-06 00:12:58 +0300129 if (uart_os == -1) {
130 return;
131 }
Damien George075d5972014-11-27 20:30:33 +0000132 if (c == '\n') {
Damien George96eca222016-04-06 00:12:58 +0300133 uart_tx_one_char(uart_os, '\r');
134 uart_tx_one_char(uart_os, '\n');
Damien George075d5972014-11-27 20:30:33 +0000135 } else if (c == '\r') {
136 } else {
Damien George96eca222016-04-06 00:12:58 +0300137 uart_tx_one_char(uart_os, c);
Damien George075d5972014-11-27 20:30:33 +0000138 }
139}
140
Damien George96eca222016-04-06 00:12:58 +0300141void ICACHE_FLASH_ATTR
142uart_os_config(int uart) {
143 uart_os = uart;
144}
145
Damien George075d5972014-11-27 20:30:33 +0000146/******************************************************************************
147 * FunctionName : uart0_rx_intr_handler
148 * Description : Internal used function
149 * UART0 interrupt handler, add self handle code inside
150 * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
151 * Returns : NONE
152*******************************************************************************/
153
154static void uart0_rx_intr_handler(void *para) {
155 /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
156 * uart1 and uart0 respectively
157 */
158
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300159 uint8 uart_no = UART_REPL;
Damien George075d5972014-11-27 20:30:33 +0000160
161 if (UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)) {
162 // frame error
163 WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR);
164 }
165
166 if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)) {
167 // fifo full
Damien George075d5972014-11-27 20:30:33 +0000168 goto read_chars;
169 } 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 +0000170 read_chars:
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300171 ETS_UART_INTR_DISABLE();
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300172
173 while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
174 uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
Damien George6a051a82016-04-01 14:53:01 +0300175 if (RcvChar == interrupt_char) {
176 mp_keyboard_interrupt();
177 } else {
178 ringbuf_put(&input_buf, RcvChar);
179 }
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300180 }
181
182 mp_hal_signal_input();
183
184 // Clear pending FIFO interrupts
185 WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
186 ETS_UART_INTR_ENABLE();
Damien George075d5972014-11-27 20:30:33 +0000187 }
188}
189
Damien George7652ab72016-04-21 15:19:00 +0100190// Waits at most timeout microseconds for at least 1 char to become ready for reading.
191// Returns true if something available, false if not.
192bool uart_rx_wait(uint32_t timeout_us) {
193 uint32_t start = system_get_time();
194 for (;;) {
195 if (input_buf.iget != input_buf.iput) {
196 return true; // have at least 1 char ready for reading
197 }
198 if (system_get_time() - start >= timeout_us) {
199 return false; // timeout
200 }
201 ets_event_poll();
202 }
203}
204
205// Returns char from the input buffer, else -1 if buffer is empty.
206int uart_rx_char(void) {
207 return ringbuf_get(&input_buf);
208}
209
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300210int uart_rx_one_char(uint8 uart_no) {
211 if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
212 return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
213 }
214 return -1;
215}
216
Damien George075d5972014-11-27 20:30:33 +0000217/******************************************************************************
218 * FunctionName : uart_init
219 * Description : user interface for init uart
220 * Parameters : UartBautRate uart0_br - uart0 bautrate
221 * UartBautRate uart1_br - uart1 bautrate
222 * Returns : NONE
223*******************************************************************************/
224void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) {
225 // rom use 74880 baut_rate, here reinitialize
226 UartDev.baut_rate = uart0_br;
227 uart_config(UART0);
228 UartDev.baut_rate = uart1_br;
229 uart_config(UART1);
230 ETS_UART_INTR_ENABLE();
231
Paul Sokolovskyc4506ed2016-03-29 21:10:10 +0300232 // install handler for "os" messages
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200233 os_install_putc1((void *)uart_os_write_char);
Damien George075d5972014-11-27 20:30:33 +0000234}
235
236void ICACHE_FLASH_ATTR uart_reattach() {
237 uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
238}
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200239
240// Task-based UART interface
241
Damien Georgec98c1282015-05-06 00:02:58 +0100242#include "py/obj.h"
Damien George40274fe2015-11-09 13:13:09 +0000243#include "lib/utils/pyexec.h"
Damien Georgec98c1282015-05-06 00:02:58 +0100244
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300245#if MICROPY_REPL_EVENT_DRIVEN
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200246void uart_task_handler(os_event_t *evt) {
Paul Sokolovsky777232c2016-04-01 12:53:50 +0300247 if (pyexec_repl_active) {
248 // TODO: Just returning here isn't exactly right.
249 // What really should be done is something like
250 // enquing delayed event to itself, for another
251 // chance to feed data to REPL. Otherwise, there
252 // can be situation when buffer has bunch of data,
253 // and sits unprocessed, because we consumed all
254 // processing signals like this.
255 return;
256 }
257
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300258 int c, ret = 0;
Paul Sokolovsky61fa7c82016-03-30 18:50:38 +0300259 while ((c = ringbuf_get(&input_buf)) >= 0) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200260 if (c == interrupt_char) {
261 mp_keyboard_interrupt();
262 }
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300263 ret = pyexec_event_repl_process_char(c);
264 if (ret & PYEXEC_FORCED_EXIT) {
265 break;
266 }
267 }
268
Damien Georgec98c1282015-05-06 00:02:58 +0100269 if (ret & PYEXEC_FORCED_EXIT) {
270 soft_reset();
271 }
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200272}
273
274void uart_task_init() {
275 system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue));
276}
Paul Sokolovsky785cf9a2016-04-01 14:02:36 +0300277#endif