123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #include "driver/uart.h"
- extern UartDevice UartDev;
- static void uart0_rx_intr_handler(void *para);
- static void ICACHE_FLASH_ATTR
- uart_config(uint8 uart_no)
- {
- if (uart_no == UART1) {
- PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
- }
- else {
- ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff));
- PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
- PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
-
- #if UART_HW_RTS
- PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
- #endif
- #if UART_HW_CTS
- PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_U0CTS);
- #endif
- }
- uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
- WRITE_PERI_REG(UART_CONF0(uart_no), ((UartDev.exist_parity & UART_PARITY_EN_M) << UART_PARITY_EN_S)
- | ((UartDev.parity & UART_PARITY_M) <<UART_PARITY_S )
- | ((UartDev.stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S)
- | ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));
-
- SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
- CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
- if (uart_no == UART0){
- WRITE_PERI_REG(UART_CONF1(uart_no),
- ((1 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
- #if UART_HW_RTS
- ((110 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
- UART_RX_FLOW_EN |
- #endif
- (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
- UART_RX_TOUT_EN|
- ((0x10 & UART_TXFIFO_EMPTY_THRHD)<<UART_TXFIFO_EMPTY_THRHD_S));
- #if UART_HW_CTS
- SET_PERI_REG_MASK( UART_CONF0(uart_no),UART_TX_FLOW_EN);
- #endif
-
- }
- else{
- WRITE_PERI_REG(UART_CONF1(uart_no),((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));
- }
- WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
- SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
- }
- static STATUS ICACHE_FLASH_ATTR
- uart1_tx_one_char(uint8 TxChar)
- {
- while (true)
- {
- uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(UART1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
- if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
- break;
- }
- }
- WRITE_PERI_REG(UART_FIFO(UART1) , TxChar);
- return OK;
- }
- static void ICACHE_FLASH_ATTR
- uart1_write_char(char c)
- {
- if (c == '\n') {
- uart1_tx_one_char('\r');
- uart1_tx_one_char('\n');
- } else if (c == '\r') {
- } else {
- uart1_tx_one_char(c);
- }
- }
- extern void process_command(char* str);
- static void uart0_rx_intr_handler(void *para)
- {
-
- RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
- uint8 RcvChar;
-
- if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)){
- WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
- while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
- RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
-
- uart_tx_one_char(RcvChar);
-
-
-
-
-
- if (RcvChar == '\r') continue;
- if (RcvChar == '\n') {
- int len;
- char *str;
- len = pRxBuff->pWritePos - pRxBuff->pReadPos;
- if (len == 0) continue;
- if (len < 0) len += RX_BUFF_SIZE;
- str = (char*)os_zalloc(len+1);
- if (str) {
- uint8 loop;
- for (loop = 0; loop < len; loop++) str[loop] = uart0_rx_one_char();
- str[len] = '\0';
- process_command(str);
- os_free(str);
- }
- }
-
- else {
- *(pRxBuff->pWritePos) = RcvChar;
- pRxBuff->pWritePos++;
- }
-
- if (pRxBuff->pWritePos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
-
- pRxBuff->pWritePos = pRxBuff->pRcvMsgBuff ;
- }
- }
- }
- }
- ICACHE_FLASH_ATTR int uart0_rx_one_char() {
- if(UartDev.rcv_buff.pReadPos == UartDev.rcv_buff.pWritePos) return -1;
- int ret = *UartDev.rcv_buff.pReadPos;
- UartDev.rcv_buff.pReadPos++;
- if(UartDev.rcv_buff.pReadPos == (UartDev.rcv_buff.pRcvMsgBuff + RX_BUFF_SIZE)) {
- UartDev.rcv_buff.pReadPos = UartDev.rcv_buff.pRcvMsgBuff;
- }
- return ret;
- }
- void ICACHE_FLASH_ATTR uart0_send(const char *str) {
- uint8 i = 0;
- while (str[i]) {
- uart_tx_one_char(str[i]);
- i++;
- }
- }
- void ICACHE_FLASH_ATTR
- uart_init(UartBautRate uart0_br, UartBautRate uart1_br, bool enable_uart1) {
-
- UartDev.baut_rate = uart0_br;
- uart_config(UART0);
- if (enable_uart1) {
- UartDev.baut_rate = uart1_br;
- uart_config(UART1);
- }
- ETS_UART_INTR_ENABLE();
- }
|