uart.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /*
  2. * File : uart.c
  3. * Copyright (C) 2013 - 2016, Espressif Systems
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of version 3 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "ets_sys.h"
  18. #include "osapi.h"
  19. #include "driver/uart.h"
  20. #include "osapi.h"
  21. #include "driver/uart_register.h"
  22. #include "mem.h"
  23. #include "os_type.h"
  24. #if EP_PRJ_SPADEV
  25. #define UART_TX_EMPTY_THRESH_VAL 0x1
  26. #else
  27. #define UART_TX_EMPTY_THRESH_VAL 0x10
  28. #endif
  29. // UartDev is defined and initialized in rom code.
  30. extern UartDevice UartDev;
  31. #if UART_BUFF_EN
  32. LOCAL struct UartBuffer* pTxBuffer = NULL;
  33. LOCAL struct UartBuffer* pRxBuffer = NULL;
  34. #endif
  35. /*uart demo with a system task, to output what uart receives*/
  36. /*this is a example to process uart data from task,please change the priority to fit your application task if exists*/
  37. /*it might conflict with your task, if so,please arrange the priority of different task, or combine it to a different event in the same task. */
  38. #define uart_recvTaskPrio 2
  39. #define uart_recvTaskQueueLen 10
  40. os_event_t uart_recvTaskQueue[uart_recvTaskQueueLen];
  41. #define DBG
  42. #define DBG1 uart1_sendStr_no_wait
  43. #define DBG2 os_printf
  44. LOCAL void uart0_rx_intr_handler(void *para);
  45. #if (EP_PRJ_SPADEV)
  46. os_timer_t txbuff_timer_t;
  47. void ICACHE_FLASH_ATTR
  48. uart_txbuff_timer_cb()
  49. {
  50. //GPIO_OUTPUT_SET(GPIO_ID_PIN(4), 0);
  51. }
  52. #endif
  53. /******************************************************************************
  54. * FunctionName : uart_config
  55. * Description : Internal used function
  56. * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
  57. * UART1 just used for debug output
  58. * Parameters : uart_no, use UART0 or UART1 defined ahead
  59. * Returns : NONE
  60. *******************************************************************************/
  61. LOCAL void ICACHE_FLASH_ATTR
  62. uart_config(uint8 uart_no)
  63. {
  64. if (uart_no == UART1){
  65. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
  66. }else{
  67. /* rcv_buff size if 0x100 */
  68. ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff));
  69. PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
  70. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
  71. #if UART_HW_RTS
  72. PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS); //HW FLOW CONTROL RTS PIN
  73. #endif
  74. #if UART_HW_CTS
  75. PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_U0CTS); //HW FLOW CONTROL CTS PIN
  76. #endif
  77. }
  78. uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));//SET BAUDRATE
  79. WRITE_PERI_REG(UART_CONF0(uart_no), ((UartDev.exist_parity & UART_PARITY_EN_M) << UART_PARITY_EN_S) //SET BIT AND PARITY MODE
  80. | ((UartDev.parity & UART_PARITY_M) <<UART_PARITY_S )
  81. | ((UartDev.stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S)
  82. | ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));
  83. //clear rx and tx fifo,not ready
  84. SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); //RESET FIFO
  85. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
  86. if (uart_no == UART0){
  87. //set rx fifo trigger
  88. WRITE_PERI_REG(UART_CONF1(uart_no),
  89. ((100 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
  90. #if UART_HW_RTS
  91. ((110 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
  92. UART_RX_FLOW_EN | //enbale rx flow control
  93. #endif
  94. (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
  95. UART_RX_TOUT_EN|
  96. ((UART_TX_EMPTY_THRESH_VAL & UART_TXFIFO_EMPTY_THRHD)<<UART_TXFIFO_EMPTY_THRHD_S));//wjl
  97. #if UART_HW_CTS
  98. SET_PERI_REG_MASK( UART_CONF0(uart_no),UART_TX_FLOW_EN); //add this sentense to add a tx flow control via MTCK( CTS )
  99. #endif
  100. SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA |UART_FRM_ERR_INT_ENA);
  101. }else{
  102. WRITE_PERI_REG(UART_CONF1(uart_no),((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));//TrigLvl default val == 1
  103. }
  104. //clear all interrupt
  105. WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
  106. //enable rx_interrupt
  107. SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_OVF_INT_ENA);
  108. }
  109. /******************************************************************************
  110. * FunctionName : uart1_tx_one_char
  111. * Description : Internal used function
  112. * Use uart1 interface to transfer one char
  113. * Parameters : uint8 TxChar - character to tx
  114. * Returns : OK
  115. *******************************************************************************/
  116. STATUS uart_tx_one_char(uint8 uart, uint8 TxChar)
  117. {
  118. while (true){
  119. uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
  120. if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
  121. break;
  122. }
  123. }
  124. WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
  125. return OK;
  126. }
  127. /******************************************************************************
  128. * FunctionName : uart1_write_char
  129. * Description : Internal used function
  130. * Do some special deal while tx char is '\r' or '\n'
  131. * Parameters : char c - character to tx
  132. * Returns : NONE
  133. *******************************************************************************/
  134. LOCAL void ICACHE_FLASH_ATTR
  135. uart1_write_char(char c)
  136. {
  137. if (c == '\n'){
  138. uart_tx_one_char(UART1, '\r');
  139. uart_tx_one_char(UART1, '\n');
  140. }else if (c == '\r'){
  141. }else{
  142. uart_tx_one_char(UART1, c);
  143. }
  144. }
  145. //os_printf output to fifo or to the tx buffer
  146. LOCAL void ICACHE_FLASH_ATTR
  147. uart0_write_char_no_wait(char c)
  148. {
  149. #if UART_BUFF_EN //send to uart0 fifo but do not wait
  150. uint8 chr;
  151. if (c == '\n'){
  152. chr = '\r';
  153. tx_buff_enq(&chr, 1);
  154. chr = '\n';
  155. tx_buff_enq(&chr, 1);
  156. }else if (c == '\r'){
  157. }else{
  158. tx_buff_enq(&c,1);
  159. }
  160. #else //send to uart tx buffer
  161. if (c == '\n'){
  162. uart_tx_one_char_no_wait(UART0, '\r');
  163. uart_tx_one_char_no_wait(UART0, '\n');
  164. }else if (c == '\r'){
  165. }
  166. else{
  167. uart_tx_one_char_no_wait(UART0, c);
  168. }
  169. #endif
  170. }
  171. /******************************************************************************
  172. * FunctionName : uart0_tx_buffer
  173. * Description : use uart0 to transfer buffer
  174. * Parameters : uint8 *buf - point to send buffer
  175. * uint16 len - buffer len
  176. * Returns :
  177. *******************************************************************************/
  178. void ICACHE_FLASH_ATTR
  179. uart0_tx_buffer(uint8 *buf, uint16 len)
  180. {
  181. uint16 i;
  182. for (i = 0; i < len; i++)
  183. {
  184. uart_tx_one_char(UART0, buf[i]);
  185. }
  186. }
  187. /******************************************************************************
  188. * FunctionName : uart0_sendStr
  189. * Description : use uart0 to transfer buffer
  190. * Parameters : uint8 *buf - point to send buffer
  191. * uint16 len - buffer len
  192. * Returns :
  193. *******************************************************************************/
  194. void ICACHE_FLASH_ATTR
  195. uart0_sendStr(const char *str)
  196. {
  197. while(*str){
  198. uart_tx_one_char(UART0, *str++);
  199. }
  200. }
  201. void at_port_print(const char *str) __attribute__((alias("uart0_sendStr")));
  202. #if (EP_PRJ_SPADEV)
  203. uint32 t0[SPA_UART_BUF_MAX], t1[SPA_UART_BUF_MAX], t2[SPA_UART_BUF_MAX];
  204. uint8 buf0[SPA_UART_BUF_MAX][80];
  205. uint8 buf1[SPA_UART_BUF_MAX][80];
  206. uint8 buf2[SPA_UART_BUF_MAX][80];
  207. uint8 cl0[SPA_UART_BUF_MAX], cl1[SPA_UART_BUF_MAX], cl2[SPA_UART_BUF_MAX];
  208. uint8 ct0, ct1, ct2;
  209. #if defined(UART_INT_TWICE) /* added macro UART_INT_TWICE */
  210. void spadev_uart_recv(uint8 inttype)
  211. {
  212. static uint8 recv_break = 0;
  213. #if UART_BUFF_EN
  214. Uart_rx_buff_enq();
  215. #else
  216. uint8 fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
  217. uint8 d_tmp = 0;
  218. uint8 idx=0, l0;
  219. if(inttype == 0)
  220. {
  221. t0[ct0]=system_get_time();
  222. cl0[ct0] = fifo_len;
  223. l0 = fifo_len<2?fifo_len:2;
  224. for(idx=0;idx<l0;idx++) {
  225. d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  226. buf0[ct0][idx]=(d_tmp);
  227. }
  228. UART_SetRxFifoLength(UART0, 70);
  229. // WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
  230. if(buf0[ct0][0]==0x1A &&
  231. (buf0[ct0][1] == 0xff||buf0[ct0][1] == 0xfa||buf0[ct0][1] == 0xf5
  232. ||buf0[ct0][1] == 0x30)
  233. )
  234. {
  235. recv_break = 0;
  236. }
  237. else
  238. {
  239. recv_break = 1;
  240. UART_ResetRxFifo(UART0);
  241. }
  242. ct0++;
  243. }
  244. else if(inttype == 1)
  245. {
  246. if(recv_break == 0)
  247. {
  248. t1[ct1]=system_get_time();
  249. cl1[ct1] = fifo_len;
  250. for(idx=0;idx<fifo_len;idx++) {
  251. d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  252. buf1[ct1][idx]=(d_tmp);
  253. }
  254. }
  255. else
  256. {
  257. UART_ResetRxFifo(UART0);
  258. }
  259. // WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
  260. UART_SetRxFifoLength(UART0, 2);
  261. ct1++;
  262. }
  263. else
  264. {
  265. if(recv_break == 0)
  266. {
  267. t2[ct2]=system_get_time();
  268. cl2[ct2] = fifo_len;
  269. for(idx=0;idx<fifo_len;idx++) {
  270. d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  271. buf2[ct2][idx]=(d_tmp);
  272. }
  273. }
  274. else
  275. {
  276. UART_ResetRxFifo(UART0);
  277. }
  278. // WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_OVF_INT_CLR);
  279. UART_SetRxFifoLength(UART0, 2);
  280. ct2++;
  281. }
  282. WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR|UART_RXFIFO_OVF_INT_CLR);
  283. uart_rx_intr_enable(UART0);
  284. #endif
  285. }
  286. #else
  287. void spadev_uart_recv(uint8 inttype)
  288. {
  289. static uint8 recv_break = 0;
  290. #if UART_BUFF_EN
  291. Uart_rx_buff_enq();
  292. #else
  293. uint8 fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
  294. uint8 d_tmp = 0;
  295. uint8 idx=0, l0;
  296. if(inttype == 0)
  297. {
  298. t0[ct0]=system_get_time();
  299. cl0[ct0] = fifo_len;
  300. l0 = fifo_len<2?fifo_len:2;
  301. for(idx=0;idx<l0;idx++) {
  302. buf0[ct0][idx] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  303. }
  304. // UART_SetRxFifoLength(UART0, 70);
  305. // WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
  306. if(buf0[ct0][0]==0x1A &&
  307. (buf0[ct0][1] == 0xff||buf0[ct0][1] == 0xfa||buf0[ct0][1] == 0xf5
  308. ||buf0[ct0][1] == 0x30)
  309. )
  310. {
  311. recv_break = 0;
  312. for(;idx<fifo_len && idx<70;idx++) {
  313. buf0[ct0][idx] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  314. }
  315. if(idx>=70)
  316. UART_ResetRxFifo(UART0);
  317. }
  318. else
  319. {
  320. recv_break = 1;
  321. UART_ResetRxFifo(UART0);
  322. }
  323. ct0++;
  324. }
  325. else if(inttype == 1)
  326. {
  327. t1[ct1]=system_get_time();
  328. cl1[ct1] = fifo_len;
  329. l0 = fifo_len<2?fifo_len:2;
  330. for(idx=0;idx<l0;idx++) {
  331. buf1[ct1][idx] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  332. }
  333. // UART_SetRxFifoLength(UART0, 70);
  334. // WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
  335. if(buf1[ct1][0]==0x1A &&
  336. (buf1[ct1][1] == 0xff||buf1[ct1][1] == 0xfa||buf1[ct1][1] == 0xf5
  337. ||buf1[ct1][1] == 0x30)
  338. )
  339. {
  340. recv_break = 0;
  341. for(;idx<fifo_len && idx<70;idx++) {
  342. buf1[ct1][idx] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  343. }
  344. if(idx>=70)
  345. UART_ResetRxFifo(UART0);
  346. }
  347. else
  348. {
  349. recv_break = 1;
  350. UART_ResetRxFifo(UART0);
  351. }
  352. ct1++;
  353. }
  354. else
  355. {
  356. t2[ct2]=system_get_time();
  357. cl2[ct2] = fifo_len;
  358. l0 = fifo_len<2?fifo_len:2;
  359. for(idx=0;idx<l0;idx++) {
  360. buf2[ct2][idx] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  361. }
  362. // UART_SetRxFifoLength(UART0, 70);
  363. // WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
  364. if(buf2[ct2][0]==0x1A &&
  365. (buf2[ct2][1] == 0xff||buf2[ct2][1] == 0xfa||buf2[ct2][1] == 0xf5
  366. ||buf2[ct2][1] == 0x30)
  367. )
  368. {
  369. recv_break = 0;
  370. for(;idx<fifo_len && idx<70;idx++) {
  371. buf2[ct2][idx] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  372. }
  373. if(idx>=70)
  374. UART_ResetRxFifo(UART0);
  375. }
  376. else
  377. {
  378. recv_break = 1;
  379. UART_ResetRxFifo(UART0);
  380. }
  381. ct2++;
  382. }
  383. WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR|UART_RXFIFO_OVF_INT_CLR);
  384. uart_rx_intr_enable(UART0);
  385. #endif
  386. }
  387. #endif /* UART_INT_TWICE */
  388. #endif
  389. /******************************************************************************
  390. * FunctionName : uart0_rx_intr_handler
  391. * Description : Internal used function
  392. * UART0 interrupt handler, add self handle code inside
  393. * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
  394. * Returns : NONE
  395. *******************************************************************************/
  396. LOCAL void
  397. uart0_rx_intr_handler(void *para)
  398. {
  399. /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
  400. * uart1 and uart0 respectively
  401. */
  402. uint8 RcvChar;
  403. uint8 uart_no = UART0;//UartDev.buff_uart_no;
  404. uint8 fifo_len = 0;
  405. uint8 buf_idx = 0;
  406. uint8 temp,cnt;
  407. //RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
  408. /*ATTENTION:*/
  409. /*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/
  410. /*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */
  411. /*IF NOT , POST AN EVENT AND PROCESS IN SYSTEM TASK */
  412. if(UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)){
  413. DBG1("FRM_ERR\r\n");
  414. WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR);
  415. }else if(UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)){
  416. DBG("f");
  417. uart_rx_intr_disable(UART0);
  418. #if (EP_PRJ_SPADEV)
  419. spadev_uart_recv(0);
  420. #else
  421. WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
  422. system_os_post(uart_recvTaskPrio, 0, 0);
  423. #endif
  424. }else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)){
  425. DBG("t");
  426. uart_rx_intr_disable(UART0);
  427. #if (EP_PRJ_SPADEV)
  428. spadev_uart_recv(1);
  429. #else
  430. WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
  431. system_os_post(uart_recvTaskPrio, 0, 1);
  432. #endif
  433. }else if(UART_TXFIFO_EMPTY_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_TXFIFO_EMPTY_INT_ST)){
  434. DBG("e");
  435. /* to output uart data from uart buffer directly in empty interrupt handler*/
  436. /*instead of processing in system event, in order not to wait for current task/function to quit */
  437. /*ATTENTION:*/
  438. /*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/
  439. /*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */
  440. CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
  441. #if UART_BUFF_EN
  442. tx_start_uart_buffer(UART0);
  443. #endif
  444. //system_os_post(uart_recvTaskPrio, 1, 0);
  445. WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_TXFIFO_EMPTY_INT_CLR);
  446. }else if(UART_RXFIFO_OVF_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_OVF_INT_ST)){
  447. uart_rx_intr_disable(UART0);
  448. #if (EP_PRJ_SPADEV)
  449. spadev_uart_recv(2);
  450. #else
  451. WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_RXFIFO_OVF_INT_CLR);
  452. system_os_post(uart_recvTaskPrio, 0, 2);
  453. #endif
  454. DBG1("OVF!!\r\n");
  455. }
  456. }
  457. /******************************************************************************
  458. * FunctionName : uart_init
  459. * Description : user interface for init uart
  460. * Parameters : UartBautRate uart0_br - uart0 bautrate
  461. * UartBautRate uart1_br - uart1 bautrate
  462. * Returns : NONE
  463. *******************************************************************************/
  464. #if UART_SELFTEST&UART_BUFF_EN
  465. os_timer_t buff_timer_t;
  466. void ICACHE_FLASH_ATTR
  467. uart_test_rx()
  468. {
  469. uint8 uart_buf[128]={0};
  470. uint16 len = 0;
  471. len = rx_buff_deq(uart_buf, 128 );
  472. tx_buff_enq(uart_buf,len);
  473. }
  474. #endif
  475. LOCAL void ICACHE_FLASH_ATTR ///////
  476. uart_recvTask(os_event_t *events)
  477. {
  478. if(events->sig == 0){
  479. #if UART_BUFF_EN
  480. Uart_rx_buff_enq();
  481. #else
  482. uint8 fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
  483. uint8 d_tmp = 0;
  484. uint8 idx=0;
  485. for(idx=0;idx<fifo_len;idx++) {
  486. d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  487. uart_tx_one_char(UART0, d_tmp);
  488. }
  489. WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
  490. uart_rx_intr_enable(UART0);
  491. #endif
  492. }else if(events->sig == 1){
  493. #if UART_BUFF_EN
  494. //already move uart buffer output to uart empty interrupt
  495. //tx_start_uart_buffer(UART0);
  496. #else
  497. #endif
  498. }
  499. }
  500. void ICACHE_FLASH_ATTR
  501. uart_init(UartBautRate uart0_br, UartBautRate uart1_br)
  502. {
  503. if(uart0_br>0)
  504. {
  505. /*this is a example to process uart data from task,please change the priority to fit your application task if exists*/
  506. #if (EP_PRJ_SPADEV != 1) /* added macro EP_PRJ_SPADEV */
  507. system_os_task(uart_recvTask, uart_recvTaskPrio, uart_recvTaskQueue, uart_recvTaskQueueLen); //demo with a task to process the uart data
  508. #endif /* EP_PRJ_SPADEV */
  509. UartDev.baut_rate = uart0_br;
  510. uart_config(UART0);
  511. }
  512. if(uart1_br)
  513. {
  514. UartDev.baut_rate = uart1_br;
  515. uart_config(UART1);
  516. }
  517. if(uart0_br>0)
  518. ETS_UART_INTR_ENABLE();
  519. else
  520. ETS_UART_INTR_DISABLE();
  521. #if UART_BUFF_EN
  522. pTxBuffer = Uart_Buf_Init(UART_TX_BUFFER_SIZE);
  523. pRxBuffer = Uart_Buf_Init(UART_RX_BUFFER_SIZE);
  524. #endif
  525. /*option 1: use default print, output from uart0 , will wait some time if fifo is full */
  526. //do nothing...
  527. /*option 2: output from uart1,uart1 output will not wait , just for output debug info */
  528. /*os_printf output uart data via uart1(GPIO2)*/
  529. os_install_putc1((void *)uart1_write_char); //use this one to output debug information via uart1 //
  530. /*option 3: output from uart0 will skip current byte if fifo is full now... */
  531. /*see uart0_write_char_no_wait:you can output via a buffer or output directly */
  532. /*os_printf output uart data via uart0 or uart buffer*/
  533. //os_install_putc1((void *)uart0_write_char_no_wait); //use this to print via uart0
  534. #if UART_SELFTEST&UART_BUFF_EN
  535. os_timer_disarm(&buff_timer_t);
  536. os_timer_setfn(&buff_timer_t, uart_test_rx , NULL); //a demo to process the data in uart rx buffer
  537. os_timer_arm(&buff_timer_t,10,1);
  538. #endif
  539. }
  540. void ICACHE_FLASH_ATTR
  541. uart_reattach()
  542. {
  543. uart_init(BIT_RATE_115200, BIT_RATE_115200);
  544. }
  545. /******************************************************************************
  546. * FunctionName : uart_tx_one_char_no_wait
  547. * Description : uart tx a single char without waiting for fifo
  548. * Parameters : uint8 uart - uart port
  549. * uint8 TxChar - char to tx
  550. * Returns : STATUS
  551. *******************************************************************************/
  552. STATUS uart_tx_one_char_no_wait(uint8 uart, uint8 TxChar)
  553. {
  554. uint8 fifo_cnt = (( READ_PERI_REG(UART_STATUS(uart))>>UART_TXFIFO_CNT_S)& UART_TXFIFO_CNT);
  555. if (fifo_cnt < 126) {
  556. WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
  557. }
  558. return OK;
  559. }
  560. STATUS uart0_tx_one_char_no_wait(uint8 TxChar)
  561. {
  562. uint8 fifo_cnt = (( READ_PERI_REG(UART_STATUS(UART0))>>UART_TXFIFO_CNT_S)& UART_TXFIFO_CNT);
  563. if (fifo_cnt < 126) {
  564. WRITE_PERI_REG(UART_FIFO(UART0) , TxChar);
  565. }
  566. return OK;
  567. }
  568. /******************************************************************************
  569. * FunctionName : uart1_sendStr_no_wait
  570. * Description : uart tx a string without waiting for every char, used for print debug info which can be lost
  571. * Parameters : const char *str - string to be sent
  572. * Returns : NONE
  573. *******************************************************************************/
  574. void uart1_sendStr_no_wait(const char *str)
  575. {
  576. while(*str){
  577. uart_tx_one_char_no_wait(UART1, *str++);
  578. }
  579. }
  580. #if UART_BUFF_EN
  581. /******************************************************************************
  582. * FunctionName : Uart_Buf_Init
  583. * Description : tx buffer enqueue: fill a first linked buffer
  584. * Parameters : char *pdata - data point to be enqueue
  585. * Returns : NONE
  586. *******************************************************************************/
  587. struct UartBuffer* ICACHE_FLASH_ATTR
  588. Uart_Buf_Init(uint32 buf_size)
  589. {
  590. uint32 heap_size = system_get_free_heap_size();
  591. if(heap_size <=buf_size){
  592. DBG1("no buf for uart\n\r");
  593. return NULL;
  594. }else{
  595. DBG("test heap size: %d\n\r",heap_size);
  596. struct UartBuffer* pBuff = (struct UartBuffer* )os_malloc(sizeof(struct UartBuffer));
  597. pBuff->UartBuffSize = buf_size;
  598. pBuff->pUartBuff = (uint8*)os_malloc(pBuff->UartBuffSize);
  599. pBuff->pInPos = pBuff->pUartBuff;
  600. pBuff->pOutPos = pBuff->pUartBuff;
  601. pBuff->Space = pBuff->UartBuffSize;
  602. pBuff->BuffState = OK;
  603. pBuff->nextBuff = NULL;
  604. pBuff->TcpControl = RUN;
  605. return pBuff;
  606. }
  607. }
  608. //copy uart buffer
  609. LOCAL void Uart_Buf_Cpy(struct UartBuffer* pCur, char* pdata , uint16 data_len)
  610. {
  611. if(data_len == 0) return ;
  612. uint16 tail_len = pCur->pUartBuff + pCur->UartBuffSize - pCur->pInPos ;
  613. if(tail_len >= data_len){ //do not need to loop back the queue
  614. os_memcpy(pCur->pInPos , pdata , data_len );
  615. pCur->pInPos += ( data_len );
  616. pCur->pInPos = (pCur->pUartBuff + (pCur->pInPos - pCur->pUartBuff) % pCur->UartBuffSize );
  617. pCur->Space -=data_len;
  618. }else{
  619. os_memcpy(pCur->pInPos, pdata, tail_len);
  620. pCur->pInPos += ( tail_len );
  621. pCur->pInPos = (pCur->pUartBuff + (pCur->pInPos - pCur->pUartBuff) % pCur->UartBuffSize );
  622. pCur->Space -=tail_len;
  623. os_memcpy(pCur->pInPos, pdata+tail_len , data_len-tail_len);
  624. pCur->pInPos += ( data_len-tail_len );
  625. pCur->pInPos = (pCur->pUartBuff + (pCur->pInPos - pCur->pUartBuff) % pCur->UartBuffSize );
  626. pCur->Space -=( data_len-tail_len);
  627. }
  628. }
  629. /******************************************************************************
  630. * FunctionName : uart_buf_free
  631. * Description : deinit of the tx buffer
  632. * Parameters : struct UartBuffer* pTxBuff - tx buffer struct pointer
  633. * Returns : NONE
  634. *******************************************************************************/
  635. void ICACHE_FLASH_ATTR
  636. uart_buf_free(struct UartBuffer* pBuff)
  637. {
  638. os_free(pBuff->pUartBuff);
  639. os_free(pBuff);
  640. }
  641. //rx buffer dequeue
  642. uint16 ICACHE_FLASH_ATTR
  643. rx_buff_deq(char* pdata, uint16 data_len )
  644. {
  645. uint16 buf_len = (pRxBuffer->UartBuffSize- pRxBuffer->Space);
  646. uint16 tail_len = pRxBuffer->pUartBuff + pRxBuffer->UartBuffSize - pRxBuffer->pOutPos ;
  647. uint16 len_tmp = 0;
  648. len_tmp = ((data_len > buf_len)?buf_len:data_len);
  649. if(pRxBuffer->pOutPos <= pRxBuffer->pInPos){
  650. os_memcpy(pdata, pRxBuffer->pOutPos,len_tmp);
  651. pRxBuffer->pOutPos+= len_tmp;
  652. pRxBuffer->Space += len_tmp;
  653. }else{
  654. if(len_tmp>tail_len){
  655. os_memcpy(pdata, pRxBuffer->pOutPos, tail_len);
  656. pRxBuffer->pOutPos += tail_len;
  657. pRxBuffer->pOutPos = (pRxBuffer->pUartBuff + (pRxBuffer->pOutPos- pRxBuffer->pUartBuff) % pRxBuffer->UartBuffSize );
  658. pRxBuffer->Space += tail_len;
  659. os_memcpy(pdata+tail_len , pRxBuffer->pOutPos, len_tmp-tail_len);
  660. pRxBuffer->pOutPos+= ( len_tmp-tail_len );
  661. pRxBuffer->pOutPos= (pRxBuffer->pUartBuff + (pRxBuffer->pOutPos- pRxBuffer->pUartBuff) % pRxBuffer->UartBuffSize );
  662. pRxBuffer->Space +=( len_tmp-tail_len);
  663. }else{
  664. //os_printf("case 3 in rx deq\n\r");
  665. os_memcpy(pdata, pRxBuffer->pOutPos, len_tmp);
  666. pRxBuffer->pOutPos += len_tmp;
  667. pRxBuffer->pOutPos = (pRxBuffer->pUartBuff + (pRxBuffer->pOutPos- pRxBuffer->pUartBuff) % pRxBuffer->UartBuffSize );
  668. pRxBuffer->Space += len_tmp;
  669. }
  670. }
  671. if(pRxBuffer->Space >= UART_FIFO_LEN){
  672. uart_rx_intr_enable(UART0);
  673. }
  674. return len_tmp;
  675. }
  676. //move data from uart fifo to rx buffer
  677. void Uart_rx_buff_enq()
  678. {
  679. uint8 fifo_len,buf_idx;
  680. uint8 fifo_data;
  681. #if 1
  682. fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
  683. if(fifo_len >= pRxBuffer->Space){
  684. os_printf("buf full!!!\n\r");
  685. }else{
  686. buf_idx=0;
  687. while(buf_idx < fifo_len){
  688. buf_idx++;
  689. fifo_data = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  690. *(pRxBuffer->pInPos++) = fifo_data;
  691. if(pRxBuffer->pInPos == (pRxBuffer->pUartBuff + pRxBuffer->UartBuffSize)){
  692. pRxBuffer->pInPos = pRxBuffer->pUartBuff;
  693. }
  694. }
  695. pRxBuffer->Space -= fifo_len ;
  696. if(pRxBuffer->Space >= UART_FIFO_LEN){
  697. //os_printf("after rx enq buf enough\n\r");
  698. uart_rx_intr_enable(UART0);
  699. }
  700. }
  701. #endif
  702. }
  703. //fill the uart tx buffer
  704. void ICACHE_FLASH_ATTR
  705. tx_buff_enq(char* pdata, uint16 data_len )
  706. {
  707. CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
  708. if(pTxBuffer == NULL){
  709. DBG1("\n\rnull, create buffer struct\n\r");
  710. pTxBuffer = Uart_Buf_Init(UART_TX_BUFFER_SIZE);
  711. if(pTxBuffer!= NULL){
  712. Uart_Buf_Cpy(pTxBuffer , pdata, data_len );
  713. }else{
  714. DBG1("uart tx MALLOC no buf \n\r");
  715. }
  716. }else{
  717. if(data_len <= pTxBuffer->Space){
  718. Uart_Buf_Cpy(pTxBuffer , pdata, data_len);
  719. }else{
  720. DBG1("UART TX BUF FULL!!!!\n\r");
  721. }
  722. }
  723. #if 0
  724. if(pTxBuffer->Space <= URAT_TX_LOWER_SIZE){
  725. set_tcp_block();
  726. }
  727. #endif
  728. SET_PERI_REG_MASK(UART_CONF1(UART0), (UART_TX_EMPTY_THRESH_VAL & UART_TXFIFO_EMPTY_THRHD)<<UART_TXFIFO_EMPTY_THRHD_S);
  729. SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
  730. }
  731. //--------------------------------
  732. LOCAL void tx_fifo_insert(struct UartBuffer* pTxBuff, uint8 data_len, uint8 uart_no)
  733. {
  734. uint8 i;
  735. for(i = 0; i<data_len;i++){
  736. WRITE_PERI_REG(UART_FIFO(uart_no) , *(pTxBuff->pOutPos++));
  737. if(pTxBuff->pOutPos == (pTxBuff->pUartBuff + pTxBuff->UartBuffSize)){
  738. pTxBuff->pOutPos = pTxBuff->pUartBuff;
  739. }
  740. }
  741. pTxBuff->pOutPos = (pTxBuff->pUartBuff + (pTxBuff->pOutPos - pTxBuff->pUartBuff) % pTxBuff->UartBuffSize );
  742. pTxBuff->Space += data_len;
  743. }
  744. /******************************************************************************
  745. * FunctionName : tx_start_uart_buffer
  746. * Description : get data from the tx buffer and fill the uart tx fifo, co-work with the uart fifo empty interrupt
  747. * Parameters : uint8 uart_no - uart port num
  748. * Returns : NONE
  749. *******************************************************************************/
  750. void tx_start_uart_buffer(uint8 uart_no)
  751. {
  752. uint8 tx_fifo_len = (READ_PERI_REG(UART_STATUS(uart_no))>>UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT;
  753. uint8 fifo_remain = UART_FIFO_LEN - tx_fifo_len ;
  754. uint8 len_tmp;
  755. uint16 tail_ptx_len,head_ptx_len,data_len;
  756. //struct UartBuffer* pTxBuff = *get_buff_prt();
  757. if(pTxBuffer){
  758. data_len = (pTxBuffer->UartBuffSize - pTxBuffer->Space);
  759. if(data_len > fifo_remain){
  760. len_tmp = fifo_remain;
  761. tx_fifo_insert( pTxBuffer,len_tmp,uart_no);
  762. SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
  763. }else{
  764. len_tmp = data_len;
  765. tx_fifo_insert( pTxBuffer,len_tmp,uart_no);
  766. }
  767. }else{
  768. DBG1("pTxBuff null \n\r");
  769. }
  770. }
  771. #endif
  772. void uart_rx_intr_disable(uint8 uart_no)
  773. {
  774. #if 1
  775. CLEAR_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA|UART_RXFIFO_OVF_INT_ENA);
  776. #else
  777. ETS_UART_INTR_DISABLE();
  778. #endif
  779. }
  780. void uart_rx_intr_enable(uint8 uart_no)
  781. {
  782. #if 1
  783. SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA|UART_RXFIFO_OVF_INT_ENA);
  784. #else
  785. ETS_UART_INTR_ENABLE();
  786. #endif
  787. }
  788. //========================================================
  789. LOCAL void
  790. uart0_write_char(char c)
  791. {
  792. if (c == '\n') {
  793. uart_tx_one_char(UART0, '\r');
  794. uart_tx_one_char(UART0, '\n');
  795. } else if (c == '\r') {
  796. } else {
  797. uart_tx_one_char(UART0, c);
  798. }
  799. }
  800. void
  801. UART_SetRxFifoLength(uint8 uart_no, uint8 len)
  802. {
  803. SET_PERI_REG_BITS(UART_CONF1(uart_no),UART_RXFIFO_FULL_THRHD,len,UART_RXFIFO_FULL_THRHD_S);
  804. }
  805. void
  806. UART_ResetRxFifo(uint8 uart_no)
  807. {
  808. SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST);
  809. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST);
  810. }
  811. void ICACHE_FLASH_ATTR
  812. UART_SetWordLength(uint8 uart_no, UartBitsNum4Char len)
  813. {
  814. SET_PERI_REG_BITS(UART_CONF0(uart_no),UART_BIT_NUM,len,UART_BIT_NUM_S);
  815. }
  816. void ICACHE_FLASH_ATTR
  817. UART_SetStopBits(uint8 uart_no, UartStopBitsNum bit_num)
  818. {
  819. SET_PERI_REG_BITS(UART_CONF0(uart_no),UART_STOP_BIT_NUM,bit_num,UART_STOP_BIT_NUM_S);
  820. }
  821. void ICACHE_FLASH_ATTR
  822. UART_SetLineInverse(uint8 uart_no, UART_LineLevelInverse inverse_mask)
  823. {
  824. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_LINE_INV_MASK);
  825. SET_PERI_REG_MASK(UART_CONF0(uart_no), inverse_mask);
  826. }
  827. void ICACHE_FLASH_ATTR
  828. UART_SetParity(uint8 uart_no, UartParityMode Parity_mode)
  829. {
  830. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_PARITY |UART_PARITY_EN);
  831. if(Parity_mode==NONE_BITS){
  832. }else{
  833. SET_PERI_REG_MASK(UART_CONF0(uart_no), Parity_mode|UART_PARITY_EN);
  834. }
  835. }
  836. void ICACHE_FLASH_ATTR
  837. UART_SetBaudrate(uint8 uart_no,uint32 baud_rate)
  838. {
  839. uart_div_modify(uart_no, UART_CLK_FREQ /baud_rate);
  840. }
  841. void ICACHE_FLASH_ATTR
  842. UART_SetFlowCtrl(uint8 uart_no,UART_HwFlowCtrl flow_ctrl,uint8 rx_thresh)
  843. {
  844. if(flow_ctrl&USART_HardwareFlowControl_RTS){
  845. PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
  846. SET_PERI_REG_BITS(UART_CONF1(uart_no),UART_RX_FLOW_THRHD,rx_thresh,UART_RX_FLOW_THRHD_S);
  847. SET_PERI_REG_MASK(UART_CONF1(uart_no), UART_RX_FLOW_EN);
  848. }else{
  849. CLEAR_PERI_REG_MASK(UART_CONF1(uart_no), UART_RX_FLOW_EN);
  850. }
  851. if(flow_ctrl&USART_HardwareFlowControl_CTS){
  852. PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_UART0_CTS);
  853. SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_TX_FLOW_EN);
  854. }else{
  855. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_TX_FLOW_EN);
  856. }
  857. }
  858. void ICACHE_FLASH_ATTR
  859. UART_WaitTxFifoEmpty(uint8 uart_no , uint32 time_out_us) //do not use if tx flow control enabled
  860. {
  861. uint32 t_s = system_get_time();
  862. while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S)){
  863. if(( system_get_time() - t_s )> time_out_us){
  864. break;
  865. }
  866. WRITE_PERI_REG(0X60000914, 0X73);//WTD
  867. }
  868. }
  869. #if UART_BUFF_EN
  870. bool ICACHE_FLASH_ATTR
  871. UART_CheckOutputFinished(uint8 uart_no, uint32 time_out_us)
  872. {
  873. uint32 t_start = system_get_time();
  874. uint8 tx_fifo_len;
  875. uint32 tx_buff_len;
  876. while(1){
  877. tx_fifo_len =( (READ_PERI_REG(UART_STATUS(uart_no))>>UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT);
  878. if(pTxBuffer){
  879. tx_buff_len = ((pTxBuffer->UartBuffSize)-(pTxBuffer->Space));
  880. }else{
  881. tx_buff_len = 0;
  882. }
  883. if( tx_fifo_len==0 && tx_buff_len==0){
  884. return TRUE;
  885. }
  886. if( system_get_time() - t_start > time_out_us){
  887. return FALSE;
  888. }
  889. WRITE_PERI_REG(0X60000914, 0X73);//WTD
  890. }
  891. }
  892. #endif
  893. void ICACHE_FLASH_ATTR
  894. UART_ResetFifo(uint8 uart_no)
  895. {
  896. SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
  897. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
  898. }
  899. void ICACHE_FLASH_ATTR
  900. UART_ClearIntrStatus(uint8 uart_no,uint32 clr_mask)
  901. {
  902. WRITE_PERI_REG(UART_INT_CLR(uart_no), clr_mask);
  903. }
  904. void ICACHE_FLASH_ATTR
  905. UART_SetIntrEna(uint8 uart_no,uint32 ena_mask)
  906. {
  907. SET_PERI_REG_MASK(UART_INT_ENA(uart_no), ena_mask);
  908. }
  909. void ICACHE_FLASH_ATTR
  910. UART_SetPrintPort(uint8 uart_no)
  911. {
  912. if(uart_no==1){
  913. os_install_putc1(uart1_write_char);
  914. }else{
  915. /*option 1: do not wait if uart fifo is full,drop current character*/
  916. os_install_putc1(uart0_write_char_no_wait);
  917. /*option 2: wait for a while if uart fifo is full*/
  918. os_install_putc1(uart0_write_char);
  919. }
  920. }
  921. //========================================================
  922. /*test code*/
  923. void ICACHE_FLASH_ATTR
  924. uart_init_2(UartBautRate uart0_br, UartBautRate uart1_br)
  925. {
  926. // rom use 74880 baut_rate, here reinitialize
  927. UartDev.baut_rate = uart0_br;
  928. UartDev.exist_parity = STICK_PARITY_EN;
  929. UartDev.parity = EVEN_BITS;
  930. UartDev.stop_bits = ONE_STOP_BIT;
  931. UartDev.data_bits = EIGHT_BITS;
  932. uart_config(UART0);
  933. UartDev.baut_rate = uart1_br;
  934. uart_config(UART1);
  935. ETS_UART_INTR_ENABLE();
  936. // install uart1 putc callback
  937. os_install_putc1((void *)uart1_write_char);//print output at UART1
  938. }