uart.c 11 KB

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