blob: 4f45a952ba81ce92d2527ce80d481d36d03037a4 [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();
162 system_os_post(UART_TASK_ID, 0, 0);
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200163#else
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300164 while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
165 uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
Damien George075d5972014-11-27 20:30:33 +0000166 uint16_t rx_buf_in_next = (rx_buf_in + 1) % RX_BUF_SIZE;
167 if (rx_buf_in_next != rx_buf_out) {
168 rx_buf[rx_buf_in] = RcvChar;
169 rx_buf_in = rx_buf_in_next;
170 }
171 }
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300172#endif
Damien George075d5972014-11-27 20:30:33 +0000173 }
174}
175
176int uart0_rx(void) {
177 if (rx_buf_out != rx_buf_in) {
178 int chr = rx_buf[rx_buf_out];
179 rx_buf_out = (rx_buf_out + 1) % RX_BUF_SIZE;
180 return chr;
181 } else {
182 return -1;
183 }
184}
185
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300186int uart_rx_one_char(uint8 uart_no) {
187 if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
188 return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
189 }
190 return -1;
191}
192
Damien George075d5972014-11-27 20:30:33 +0000193/******************************************************************************
194 * FunctionName : uart_init
195 * Description : user interface for init uart
196 * Parameters : UartBautRate uart0_br - uart0 bautrate
197 * UartBautRate uart1_br - uart1 bautrate
198 * Returns : NONE
199*******************************************************************************/
200void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) {
201 // rom use 74880 baut_rate, here reinitialize
202 UartDev.baut_rate = uart0_br;
203 uart_config(UART0);
204 UartDev.baut_rate = uart1_br;
205 uart_config(UART1);
206 ETS_UART_INTR_ENABLE();
207
Paul Sokolovskyc4506ed2016-03-29 21:10:10 +0300208 // install handler for "os" messages
Josef Gajdusek1c132c82015-05-13 15:39:25 +0200209 os_install_putc1((void *)uart_os_write_char);
Damien George075d5972014-11-27 20:30:33 +0000210}
211
212void ICACHE_FLASH_ATTR uart_reattach() {
213 uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
214}
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200215
216// Task-based UART interface
217
Damien Georgec98c1282015-05-06 00:02:58 +0100218#include "py/obj.h"
Damien George40274fe2015-11-09 13:13:09 +0000219#include "lib/utils/pyexec.h"
Damien Georgec98c1282015-05-06 00:02:58 +0100220
221void soft_reset(void);
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200222void mp_keyboard_interrupt(void);
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200223
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200224int interrupt_char;
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200225void uart_task_handler(os_event_t *evt) {
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300226 int c, ret = 0;
227 while ((c = uart_rx_one_char(UART_REPL)) >= 0) {
Paul Sokolovskyd3a4d392015-12-20 13:58:58 +0200228 if (c == interrupt_char) {
229 mp_keyboard_interrupt();
230 }
Paul Sokolovsky2fc1e642015-06-01 01:27:39 +0300231 ret = pyexec_event_repl_process_char(c);
232 if (ret & PYEXEC_FORCED_EXIT) {
233 break;
234 }
235 }
236
237 // Clear pending FIFO interrupts
238 WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
239 // Enable UART interrupts, so our task will receive events again from IRQ handler
240 ETS_UART_INTR_ENABLE();
241
Damien Georgec98c1282015-05-06 00:02:58 +0100242 if (ret & PYEXEC_FORCED_EXIT) {
243 soft_reset();
244 }
Paul Sokolovskyf12ea7c2015-01-16 01:54:40 +0200245}
246
247void uart_task_init() {
248 system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue));
249}