uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /******************************************************************************
  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 "driver/uart.h"
  15. #include "task/task.h"
  16. #include "user_config.h"
  17. #include "user_interface.h"
  18. #include "osapi.h"
  19. #define UART0 0
  20. #define UART1 1
  21. #ifndef FUNC_U0RXD
  22. #define FUNC_U0RXD 0
  23. #endif
  24. #ifndef FUNC_U0CTS
  25. #define FUNC_U0CTS 4
  26. #endif
  27. // For event signalling
  28. static task_handle_t sig = 0;
  29. static uint8 *sig_flag;
  30. static uint8 isr_flag = 0;
  31. // UartDev is defined and initialized in rom code.
  32. extern UartDevice UartDev;
  33. static os_timer_t autobaud_timer;
  34. static void (*alt_uart0_tx)(char txchar);
  35. LOCAL void ICACHE_RAM_ATTR
  36. uart0_rx_intr_handler(void *para);
  37. /******************************************************************************
  38. * FunctionName : uart_wait_tx_empty
  39. * Description : Internal used function
  40. * Wait for TX FIFO to become empty.
  41. * Parameters : uart_no, use UART0 or UART1 defined ahead
  42. * Returns : NONE
  43. *******************************************************************************/
  44. LOCAL void ICACHE_FLASH_ATTR
  45. uart_wait_tx_empty(uint8 uart_no)
  46. {
  47. while ((READ_PERI_REG(UART_STATUS(uart_no)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) > 0)
  48. ;
  49. }
  50. /******************************************************************************
  51. * FunctionName : uart_config
  52. * Description : Internal used function
  53. * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
  54. * UART1 just used for debug output
  55. * Parameters : uart_no, use UART0 or UART1 defined ahead
  56. * Returns : NONE
  57. *******************************************************************************/
  58. LOCAL void ICACHE_FLASH_ATTR
  59. uart_config(uint8 uart_no)
  60. {
  61. uart_wait_tx_empty(uart_no);
  62. if (uart_no == UART1) {
  63. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
  64. } else {
  65. /* rcv_buff size if 0x100 */
  66. ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff));
  67. PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
  68. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
  69. PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U);
  70. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);
  71. }
  72. uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
  73. WRITE_PERI_REG(UART_CONF0(uart_no), ((UartDev.exist_parity & UART_PARITY_EN_M) << UART_PARITY_EN_S) //SET BIT AND PARITY MODE
  74. | ((UartDev.parity & UART_PARITY_M) <<UART_PARITY_S )
  75. | ((UartDev.stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S)
  76. | ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));
  77. //clear rx and tx fifo,not ready
  78. SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
  79. CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
  80. //set rx fifo trigger
  81. WRITE_PERI_REG(UART_CONF1(uart_no), (UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S);
  82. //clear all interrupt
  83. WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
  84. //enable rx_interrupt
  85. SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
  86. }
  87. /******************************************************************************
  88. * FunctionName : uart0_alt
  89. * Description : Internal used function
  90. * UART0 pins changed to 13,15 if 'on' is set, else set to normal pins
  91. * Parameters : on - 1 = use alternate pins, 0 = use normal pins
  92. * Returns : NONE
  93. *******************************************************************************/
  94. void ICACHE_FLASH_ATTR
  95. uart0_alt(uint8 on)
  96. {
  97. uart_wait_tx_empty(UART0);
  98. if (on)
  99. {
  100. PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U);
  101. PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
  102. PIN_PULLUP_EN(PERIPHS_IO_MUX_MTCK_U);
  103. PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_U0CTS);
  104. // now make RTS/CTS behave as TX/RX
  105. IOSWAP |= (1 << IOSWAPU0);
  106. }
  107. else
  108. {
  109. PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
  110. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
  111. PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U);
  112. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);
  113. // now make RX/TX behave as TX/RX
  114. IOSWAP &= ~(1 << IOSWAPU0);
  115. }
  116. }
  117. /******************************************************************************
  118. * FunctionName : uart_tx_one_char
  119. * Description : Internal used function
  120. * Use uart interface to transfer one char
  121. * Parameters : uint8 TxChar - character to tx
  122. * Returns : OK
  123. *******************************************************************************/
  124. STATUS ICACHE_FLASH_ATTR
  125. uart_tx_one_char(uint8 uart, uint8 TxChar)
  126. {
  127. if (uart == 0 && alt_uart0_tx) {
  128. (*alt_uart0_tx)(TxChar);
  129. return OK;
  130. }
  131. while (true)
  132. {
  133. uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
  134. if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
  135. break;
  136. }
  137. }
  138. WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
  139. return OK;
  140. }
  141. /******************************************************************************
  142. * FunctionName : uart1_write_char
  143. * Description : Internal used function
  144. * Do some special deal while tx char is '\r' or '\n'
  145. * Parameters : char c - character to tx
  146. * Returns : NONE
  147. *******************************************************************************/
  148. LOCAL void ICACHE_FLASH_ATTR
  149. uart1_write_char(char c)
  150. {
  151. if (c == '\n')
  152. {
  153. uart_tx_one_char(UART1, '\r');
  154. uart_tx_one_char(UART1, '\n');
  155. }
  156. else if (c == '\r')
  157. {
  158. }
  159. else
  160. {
  161. uart_tx_one_char(UART1, c);
  162. }
  163. }
  164. /******************************************************************************
  165. * FunctionName : uart0_tx_buffer
  166. * Description : use uart0 to transfer buffer
  167. * Parameters : uint8 *buf - point to send buffer
  168. * uint16 len - buffer len
  169. * Returns :
  170. *******************************************************************************/
  171. void ICACHE_FLASH_ATTR
  172. uart0_tx_buffer(uint8 *buf, uint16 len)
  173. {
  174. uint16 i;
  175. for (i = 0; i < len; i++)
  176. {
  177. uart_tx_one_char(UART0, buf[i]);
  178. }
  179. }
  180. /******************************************************************************
  181. * FunctionName : uart0_sendStr
  182. * Description : use uart0 to transfer buffer
  183. * Parameters : uint8 *buf - point to send buffer
  184. * uint16 len - buffer len
  185. * Returns :
  186. *******************************************************************************/
  187. void ICACHE_FLASH_ATTR uart0_sendStr(const char *str)
  188. {
  189. while(*str)
  190. {
  191. // uart_tx_one_char(UART0, *str++);
  192. uart0_putc(*str++);
  193. }
  194. }
  195. /******************************************************************************
  196. * FunctionName : uart0_putc
  197. * Description : use uart0 to transfer char
  198. * Parameters : uint8 c - send char
  199. * Returns :
  200. *******************************************************************************/
  201. void ICACHE_FLASH_ATTR uart0_putc(const char c)
  202. {
  203. if (c == '\n')
  204. {
  205. uart_tx_one_char(UART0, '\r');
  206. uart_tx_one_char(UART0, '\n');
  207. }
  208. else if (c == '\r')
  209. {
  210. }
  211. else
  212. {
  213. uart_tx_one_char(UART0, c);
  214. }
  215. }
  216. /******************************************************************************
  217. * FunctionName : uart0_rx_intr_handler
  218. * Description : Internal used function
  219. * UART0 interrupt handler, add self handle code inside
  220. * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
  221. * Returns : NONE
  222. *******************************************************************************/
  223. LOCAL void
  224. uart0_rx_intr_handler(void *para)
  225. {
  226. /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
  227. * uart1 and uart0 respectively
  228. */
  229. RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
  230. uint8 RcvChar;
  231. bool got_input = false;
  232. if (UART_RXFIFO_FULL_INT_ST != (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)) {
  233. return;
  234. }
  235. WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
  236. while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
  237. RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
  238. /* you can add your handle code below.*/
  239. *(pRxBuff->pWritePos) = RcvChar;
  240. // insert here for get one command line from uart
  241. if (RcvChar == '\r' || RcvChar == '\n' ) {
  242. pRxBuff->BuffState = WRITE_OVER;
  243. }
  244. if (pRxBuff->pWritePos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
  245. // overflow ...we may need more error handle here.
  246. pRxBuff->pWritePos = pRxBuff->pRcvMsgBuff ;
  247. } else {
  248. pRxBuff->pWritePos++;
  249. }
  250. if (pRxBuff->pWritePos == pRxBuff->pReadPos){ // overflow one byte, need push pReadPos one byte ahead
  251. if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
  252. pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ;
  253. } else {
  254. pRxBuff->pReadPos++;
  255. }
  256. }
  257. got_input = true;
  258. }
  259. if (got_input && sig) {
  260. if (isr_flag == *sig_flag) {
  261. isr_flag ^= 0x01;
  262. task_post_low (sig, 0x8000 | isr_flag << 14 | false);
  263. }
  264. }
  265. }
  266. static void
  267. uart_autobaud_timeout(void *timer_arg)
  268. {
  269. uint32_t uart_no = (uint32_t) timer_arg;
  270. uint32_t divisor = uart_baudrate_detect(uart_no, 1);
  271. static int called_count = 0;
  272. // Shut off after two minutes to stop wasting CPU cycles if insufficient input received
  273. if (called_count++ > 10 * 60 * 2 || divisor) {
  274. os_timer_disarm(&autobaud_timer);
  275. }
  276. if (divisor) {
  277. uart_div_modify(uart_no, divisor);
  278. }
  279. }
  280. #include "pm/swtimer.h"
  281. static void
  282. uart_init_autobaud(uint32_t uart_no)
  283. {
  284. os_timer_setfn(&autobaud_timer, uart_autobaud_timeout, (void *) uart_no);
  285. SWTIMER_REG_CB(uart_autobaud_timeout, SWTIMER_DROP);
  286. //if autobaud hasn't done it's thing by the time light sleep triggered, it probably isn't going to happen.
  287. os_timer_arm(&autobaud_timer, 100, TRUE);
  288. }
  289. static void
  290. uart_stop_autobaud()
  291. {
  292. os_timer_disarm(&autobaud_timer);
  293. }
  294. /******************************************************************************
  295. * FunctionName : uart_init
  296. * Description : user interface for init uart
  297. * Parameters : UartBautRate uart0_br - uart0 bautrate
  298. * UartBautRate uart1_br - uart1 bautrate
  299. * os_signal_t sig_input - signal to post
  300. * uint8 *flag_input - flag of consumer task
  301. * Returns : NONE
  302. *******************************************************************************/
  303. void ICACHE_FLASH_ATTR
  304. uart_init(UartBautRate uart0_br, UartBautRate uart1_br, os_signal_t sig_input, uint8 *flag_input)
  305. {
  306. sig = sig_input;
  307. sig_flag = flag_input;
  308. // rom use 74880 baut_rate, here reinitialize
  309. UartDev.baut_rate = uart0_br;
  310. uart_config(UART0);
  311. UartDev.baut_rate = uart1_br;
  312. uart_config(UART1);
  313. #ifdef BIT_RATE_AUTOBAUD
  314. uart_init_autobaud(0);
  315. #endif
  316. }
  317. void ICACHE_FLASH_ATTR
  318. uart_setup(uint8 uart_no)
  319. {
  320. #ifdef BIT_RATE_AUTOBAUD
  321. uart_stop_autobaud();
  322. #endif
  323. // poll Tx FIFO empty outside before disabling interrupts
  324. uart_wait_tx_empty(uart_no);
  325. ETS_UART_INTR_DISABLE();
  326. uart_config(uart_no);
  327. ETS_UART_INTR_ENABLE();
  328. }
  329. void ICACHE_FLASH_ATTR uart_set_alt_output_uart0(void (*fn)(char)) {
  330. alt_uart0_tx = fn;
  331. }
  332. UartConfig ICACHE_FLASH_ATTR uart_get_config(uint8 uart_no) {
  333. UartConfig config;
  334. config.baut_rate = UART_CLK_FREQ / READ_PERI_REG(UART_CLKDIV(uart_no));
  335. uint32_t conf = READ_PERI_REG(UART_CONF0(uart_no));
  336. config.exist_parity = (conf >> UART_PARITY_EN_S) & UART_PARITY_EN_M;
  337. config.parity = (conf >> UART_PARITY_S) & UART_PARITY_M;
  338. config.stop_bits = (conf >> UART_STOP_BIT_NUM_S) & UART_STOP_BIT_NUM;
  339. config.data_bits = (conf >> UART_BIT_NUM_S) & UART_BIT_NUM;
  340. return config;
  341. }