//// modified for LASS , receive data from UART0 (sensor) static void ICACHE_FLASH_ATTR recvTask(os_event_t *events) { uint8_t i; uint16 length = 0; //char* p; //p=&LASSstring[0]; while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { WRITE_PERI_REG(0X60000914, 0x73); //WTD //length=0; while ((READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) && (length<MAX_UARTBUFFER)) uartbuffer[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; //refer to Plantower PMS5003 p1_0 = (uint32)uartbuffer[4]*256+(uint32)uartbuffer[5]; p2_5 = (uint32)uartbuffer[6]*256+(uint32)uartbuffer[7]; p10_0 = (uint32)uartbuffer[8]*256+(uint32)uartbuffer[9]; } 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); } else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) { WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); } INFO("%s\r\n",LASSstring); //uart_rx_intr_enable(UART0); ETS_UART_INTR_ENABLE(); ///// BUG ! TX malfunctioned! }
static void ICACHE_FLASH_ATTR recvTask(os_event_t *events) { uint8_t i; while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { WRITE_PERI_REG(0X60000914, 0x73); //WTD uint16 length = 0; while ((READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) && (length<MAX_UARTBUFFER)) uartbuffer[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; for (i = 0; i < MAX_CONN; ++i) if (connData[i].conn) espbuffsent(&connData[i], uartbuffer, length); } 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); } else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) { WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); } ETS_UART_INTR_ENABLE(); }
/** * @brief Uart receive task. * @param events: contain the uart receive data * @retval None */ static void ICACHE_FLASH_ATTR /////// at_recvTask(os_event_t *events) { uint8_t temp; while(READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; if (ser_count>64) ser_count = 0; ser[ser_count] = temp; ser_count++; ser[64] = ser_count; feedwdt(); } 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); } else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) { WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); } ETS_UART_INTR_ENABLE(); }
//Read from UART0(requires special uart.c!) static void ICACHE_FLASH_ATTR recvTask(os_event_t *events) { uint8_t c, i; char ch[1000]; c = 0; i = 0; //uart0_tx_buffer("uart",4); while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { WRITE_PERI_REG(0X60000914, 0x73); //WTD c = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; ch[i] = c; i++; } 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); } else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) { WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); } ETS_UART_INTR_ENABLE(); // send to Server if available if (pconn && i != 0) { espconn_sent(pconn, ch, i); } }
/* * UART rx Interrupt routine */ static void uart_isr(void *arg) { uint8_t temp; signed portBASE_TYPE ret; portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; if (UART_RXFIFO_FULL_INT_ST != (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)) { return; } 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)) { temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; ret = xQueueSendToBackFromISR ( uart_rx_queue, &temp, &xHigherPriorityTaskWoken ); if (ret != pdTRUE) { uart_rx_overruns++; } else { uart_rx_bytes++; } } portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); }
static __attribute__ ((section(".iram0.text"))) void uart0_rx_intr_handler(void *para) { /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents * uart1 and uart0 respectively */ //RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para; char RcvChar; if (UART_RXFIFO_FULL_INT_ST != (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)) { return; } 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; /* you can add your handle code below.*/ system_os_post(1, 0, RcvChar); if (RcvChar == 114) { ets_uart_printf("RcvChar \r\n"); } } }
/****************************************************************************** * FunctionName : uart0_rx_intr_handler * Description : Internal used function * UART0 interrupt handler, add self handle code inside * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg * Returns : NONE *******************************************************************************/ static void // must not use ICACHE_FLASH_ATTR ! uart0_rx_intr_handler(void *para) { // we assume that uart1 has interrupts disabled (it uses the same interrupt vector) uint8 uart_no = UART0; const uint32 one_sec = 1000000; // one second in usecs // we end up largely ignoring framing errors and we just print a warning every second max if (READ_PERI_REG(UART_INT_RAW(uart_no)) & UART_FRM_ERR_INT_RAW) { uint32 now = system_get_time(); if (last_frm_err == 0 || (now - last_frm_err) > one_sec) { os_printf("UART framing error (bad baud rate?)\n"); last_frm_err = now; } // clear rx fifo (apparently this is not optional at this point) SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST); CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST); // reset framing error WRITE_PERI_REG(UART_INT_CLR(UART0), UART_FRM_ERR_INT_CLR); // once framing errors are gone for 10 secs we forget about having seen them } else if (last_frm_err != 0 && (system_get_time() - last_frm_err) > 10*one_sec) { last_frm_err = 0; } if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST) || UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) { //DBG_UART("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); ETS_UART_INTR_DISABLE(); post_usr_task(uart_recvTaskNum, 0); } }
IRAM NOINSTR static void esp_handle_uart_int(struct mgos_uart_state *us) { if (us == NULL) return; const int uart_no = us->uart_no; /* Since both UARTs use the same int, we need to apply the mask manually. */ const unsigned int int_st = READ_PERI_REG(UART_INT_ST(uart_no)) & READ_PERI_REG(UART_INT_ENA(uart_no)); const struct mgos_uart_config *cfg = &us->cfg; if (int_st == 0) return; us->stats.ints++; if (int_st & UART_RXFIFO_OVF_INT_ST) us->stats.rx_overflows++; if (int_st & UART_CTS_CHG_INT_ST) { if (esp_uart_cts(uart_no) != 0 && esp_uart_tx_fifo_len(uart_no) > 0) { us->stats.tx_throttles++; } } if (int_st & (UART_RX_INTS | UART_TX_INTS)) { int int_ena = UART_INFO_INTS; if (int_st & UART_RX_INTS) us->stats.rx_ints++; if (int_st & UART_TX_INTS) us->stats.tx_ints++; if (adj_rx_fifo_full_thresh(us)) { int_ena |= UART_RXFIFO_FULL_INT_ENA; } else if (cfg->rx_fc_type == MGOS_UART_FC_SW) { /* Send XOFF and keep RX ints disabled */ while (esp_uart_tx_fifo_len(uart_no) >= 127) { } esp_uart_tx_byte(uart_no, MGOS_UART_XOFF_CHAR); us->xoff_sent = true; } WRITE_PERI_REG(UART_INT_ENA(uart_no), int_ena); mgos_uart_schedule_dispatcher(uart_no, true /* from_isr */); } WRITE_PERI_REG(UART_INT_CLR(uart_no), int_st); }
/****************************************************************************** * FunctionName : uart_config * Description : Internal used function * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled * UART1 just used for debug output * Parameters : uart_no, use UART0 or UART1 defined ahead * Returns : NONE *******************************************************************************/ 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); //PIN_PULLDWN_DIS(PERIPHS_IO_MUX_GPIO2_U); PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U); } else { /* rcv_buff size is 0x100 */ ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff)); PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U); //PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0TXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U); //PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0RXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); // FUNC_U0RXD==0 } uart_div_modify(uart_no, UART_CLK_FREQ / UartDev.baut_rate); if (uart_no == UART1) //UART 1 always 8 N 1 WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT)); else WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(UartDev.data_bits, UartDev.parity, UartDev.stop_bits)); //clear rx and tx fifo,not ready 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) { // Configure RX interrupt conditions as follows: trigger rx-full when there are 80 characters // in the buffer, trigger rx-timeout when the fifo is non-empty and nothing further has been // received for 4 character periods. // Set the hardware flow-control to trigger when the FIFO holds 100 characters, although // we don't really expect the signals to actually be wired up to anything. It doesn't hurt // to set the threshold here... // We do not enable framing error interrupts 'cause they tend to cause an interrupt avalanche // and instead just poll for them when we get a std RX interrupt. WRITE_PERI_REG(UART_CONF1(uart_no), ((80 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | ((100 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | UART_RX_FLOW_EN | (4 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S | UART_RX_TOUT_EN); SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA); } else { WRITE_PERI_REG(UART_CONF1(uart_no), ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S)); } //clear all interrupt WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff); }
void HardwareSerial::uart0_rx_intr_handler(void *para) { /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents * uart1 and uart0 respectively */ RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para; uint8 RcvChar; if (UART_RXFIFO_FULL_INT_ST != (READ_PERI_REG(UART_INT_ST(UART_ID_0)) & UART_RXFIFO_FULL_INT_ST)) return; WRITE_PERI_REG(UART_INT_CLR(UART_ID_0), UART_RXFIFO_FULL_INT_CLR); while (READ_PERI_REG(UART_STATUS(UART_ID_0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { RcvChar = READ_PERI_REG(UART_FIFO(UART_ID_0)) & 0xFF; /* you can add your handle code below.*/ if (memberData[UART_ID_0].useRxBuff) { *(pRxBuff->pWritePos) = RcvChar; // insert here for get one command line from uart if (RcvChar == '\n' ) pRxBuff->BuffState = WRITE_OVER; pRxBuff->pWritePos++; if (pRxBuff->pWritePos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) { // overflow ...we may need more error handle here. pRxBuff->pWritePos = pRxBuff->pRcvMsgBuff; } if (pRxBuff->pWritePos == pRxBuff->pReadPos) { // Prevent readbuffer overflow if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) { pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ; } else { pRxBuff->pReadPos++; } } } if (memberData[UART_ID_0].HWSDelegate) { unsigned short cc; cc = (pRxBuff->pWritePos < pRxBuff->pReadPos) ? ((pRxBuff->pWritePos + RX_BUFF_SIZE) - pRxBuff->pReadPos) : (pRxBuff->pWritePos - pRxBuff->pReadPos); memberData[UART_ID_0].HWSDelegate(Serial, RcvChar, cc); } } }
void uart_isr(void *arg) { uint32_t int_st = READ_PERI_REG(UART_INT_ST(0)); while (1) { uint32_t fifo_len = READ_PERI_REG(UART_STATUS(0)) & 0xff; if (fifo_len == 0) { break; } while (fifo_len-- > 0) { uint8_t byte = READ_PERI_REG(UART_FIFO(0)) & 0xff; uart_isr_receive(byte); } } WRITE_PERI_REG(UART_INT_CLR(0), int_st); }
void uart_isr(void *arg) { uint32_t int_st = READ_PERI_REG(UART_INT_ST(0)); struct uart_buf *ub = (struct uart_buf *) arg; while (1) { uint32_t fifo_len = READ_PERI_REG(UART_STATUS(0)) & 0xff; if (fifo_len == 0) break; while (fifo_len-- > 0) { uint8_t byte = READ_PERI_REG(UART_FIFO(0)) & 0xff; *ub->pw++ = byte; ub->nr++; if (ub->pw >= ub->data + UART_BUF_SIZE) ub->pw = ub->data; } } WRITE_PERI_REG(UART_INT_CLR(0), int_st); }
IRAM int esp_uart_dispatch_rx_top(int uart_no) { struct esp_uart_state *us = s_us[uart_no]; if (us == NULL || !us->rx_enabled) return 1; uint32_t rxn = 0; cs_rbuf_t *rxb = &us->rx_buf; /* RX */ if (rxb->avail > 0 && rx_fifo_len(uart_no) > 0) { int linger_counter = 0; /* 32 here is a constant measured (using system_get_time) to provide * linger time of rx_linger_micros. It basically means that one iteration * of the loop takes 3.2 us. * * Note: lingering may starve TX FIFO if the flow is bidirectional. * TODO(rojer): keep transmitting from tx_buf while lingering. */ int max_linger = us->cfg->rx_linger_micros / 10 * 32; #ifdef MEASURE_LINGER_TIME uint32_t st = system_get_time(); #endif while (rxb->avail > 0 && linger_counter <= max_linger) { int rx_len = rx_fifo_len(uart_no); if (rx_len > 0) { while (rx_len-- > 0 && rxb->avail > 0) { cs_rbuf_append_one(rxb, rx_byte(uart_no)); rxn++; } if (linger_counter > 0) { us->stats.rx_linger_conts++; linger_counter = 0; } } else { linger_counter++; } } #ifdef MEASURE_LINGER_TIME fprintf(stderr, "Time spent reading: %u us\n", system_get_time() - st); #endif us->stats.rx_bytes += rxn; } int rfl = rx_fifo_len(uart_no); if (rfl < us->cfg->rx_fifo_full_thresh) { CLEAR_PERI_REG_MASK(UART_INT_CLR(uart_no), UART_RX_INTS); } return rfl == 0; }
void HardwareSerial::uartReceiveInterruptHandler(void *para) { /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents * uart1 and uart0 respectively */ HardwareSerial* Self = hardwareSerialObjects[UART_ID_0]; uint8 RcvChar; if (UART_RXFIFO_FULL_INT_ST != (READ_PERI_REG(UART_INT_ST(UART_ID_0)) & UART_RXFIFO_FULL_INT_ST)) return; WRITE_PERI_REG(UART_INT_CLR(UART_ID_0), UART_RXFIFO_FULL_INT_CLR); while (READ_PERI_REG(UART_STATUS(UART_ID_0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { RcvChar = READ_PERI_REG(UART_FIFO(UART_ID_0)) & 0xFF; /* you can add your handle code below.*/ if (Self->useRxBuff) { Self->rxBuffer.Push(RcvChar); } if ((Self->HWSDelegate) || (Self->commandExecutor)) { SerialDelegateMessage serialDelegateMessage; serialDelegateMessage.uart = Self->uart; serialDelegateMessage.rcvChar = RcvChar; serialDelegateMessage.charCount = Self->rxBuffer.Len(); if (Self->HWSDelegate) { // system_os_post(USER_TASK_PRIO_0, SERIAL_SIGNAL_DELEGATE, serialQueueParameter); serialDelegateMessage.type = SERIAL_SIGNAL_DELEGATE; xQueueSendToBackFromISR ( serialDelegateQueue, &serialDelegateMessage, NULL); } if (Self->commandExecutor) { // system_os_post(USER_TASK_PRIO_0, SERIAL_SIGNAL_COMMAND, serialQueueParameter); serialDelegateMessage.type = SERIAL_SIGNAL_COMMAND; xQueueSendToBackFromISR ( serialDelegateQueue, &serialDelegateMessage, NULL); } } } }
static void ICACHE_FLASH_ATTR user_rx_task(os_event_t *events) { if(events->sig == 0){ uint8 fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT; uint8 d_tmp = 0; uint8 idx=0; for(idx=0;idx<fifo_len;idx++) { d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; uart_tx_one_char(UART0, d_tmp); if( has_space() ) { serial_buffer[putpos++ & POS_MASK] = d_tmp; } } WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR); uart_rx_intr_enable(UART0); if( serial_datalen() && user_rx_callback ) { user_rx_callback(serial_datalen()); } }
void mgos_uart_hal_dispatch_rx_top(struct mgos_uart_state *us) { int uart_no = us->uart_no; struct mbuf *rxb = &us->rx_buf; uint32_t rxn = 0; /* RX */ if (mgos_uart_rxb_free(us) > 0 && esp_uart_rx_fifo_len(uart_no) > 0) { int linger_counter = 0; /* 32 here is a constant measured (using system_get_time) to provide * linger time of rx_linger_micros. It basically means that one iteration * of the loop takes 3.2 us. * * Note: lingering may starve TX FIFO if the flow is bidirectional. * TODO(rojer): keep transmitting from tx_buf while lingering. */ int max_linger = us->cfg.rx_linger_micros / 10 * 32; #ifdef MEASURE_LINGER_TIME uint32_t st = system_get_time(); #endif while (mgos_uart_rxb_free(us) > 0 && linger_counter <= max_linger) { size_t rx_len = esp_uart_rx_fifo_len(uart_no); if (rx_len > 0) { rx_len = MIN(rx_len, mgos_uart_rxb_free(us)); if (rxb->size < rxb->len + rx_len) mbuf_resize(rxb, rxb->len + rx_len); while (rx_len > 0) { uint8_t b = rx_byte(uart_no); mbuf_append(rxb, &b, 1); rx_len--; rxn++; } if (linger_counter > 0) { us->stats.rx_linger_conts++; linger_counter = 0; } } else { linger_counter++; } } #ifdef MEASURE_LINGER_TIME fprintf(stderr, "Time spent reading: %u us\n", system_get_time() - st); #endif us->stats.rx_bytes += rxn; } CLEAR_PERI_REG_MASK(UART_INT_CLR(uart_no), UART_RX_INTS); }
void UARTInit() { //Enable TxD pin PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); //Set baud rate and other serial parameters to 115200,n,8,1 uart_div_modify(0, UART_CLK_FREQ/BIT_RATE_115200); WRITE_PERI_REG(UART_CONF0(0), (STICK_PARITY_DIS)|(ONE_STOP_BIT << UART_STOP_BIT_NUM_S)| \ (EIGHT_BITS << UART_BIT_NUM_S)); //Reset tx & rx fifo SET_PERI_REG_MASK(UART_CONF0(0), UART_RXFIFO_RST|UART_TXFIFO_RST); CLEAR_PERI_REG_MASK(UART_CONF0(0), UART_RXFIFO_RST|UART_TXFIFO_RST); //Clear pending interrupts WRITE_PERI_REG(UART_INT_CLR(0), 0xffff); //Install our own putchar handler os_install_putc1((void *)UARTPutChar); }
/****************************************************************************** * FunctionName : uart_recvTask * Description : system task triggered on receive interrupt, empties FIFO and calls callbacks *******************************************************************************/ static void ICACHE_FLASH_ATTR uart_recvTask(os_event_t *events) { while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { //WRITE_PERI_REG(0X60000914, 0x73); //WTD // commented out by TvE // read a buffer-full from the uart uint16 length = 0; uint16 l = 0; char buf[128]; char in_char; bool did_start = false; while ((READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) && (length < 128)) { in_char = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; length++; //look for beginning | char from BLE peripheral if(in_char == '|') { did_start = true; } if(did_start) { buf[l] = in_char; if(buf[l] == '\n' || buf[l] == '\r') { buf[l] = '\0'; break; } l++; } } //DBG_UART("%d ix %d\n", system_get_time(), length); for (int i=0; i<MAX_CB; i++) { //if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length); if (uart_recv_cb[i] != NULL && l > 3) (uart_recv_cb[i])(buf, l); } } WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR); ETS_UART_INTR_ENABLE(); }
IRAM NOINSTR static void esp_handle_uart_int(struct esp_uart_state *us) { const int uart_no = us->cfg->uart_no; /* Since both UARTs use the same int, we need to apply the mask manually. */ const unsigned int int_st = READ_PERI_REG(UART_INT_ST(uart_no)) & READ_PERI_REG(UART_INT_ENA(uart_no)); if (int_st == 0) return; us->stats.ints++; if (int_st & UART_RXFIFO_OVF_INT_ST) us->stats.rx_overflows++; if (int_st & UART_CTS_CHG_INT_ST) { if (cts(uart_no) != 0 && tx_fifo_len(uart_no) > 0) us->stats.tx_throttles++; } if (int_st & (UART_RX_INTS | UART_TX_INTS)) { if (int_st & UART_RX_INTS) us->stats.rx_ints++; if (int_st & UART_TX_INTS) us->stats.tx_ints++; /* Wake up the processor and disable TX and RX ints until it runs. */ WRITE_PERI_REG(UART_INT_ENA(uart_no), UART_INFO_INTS); us->cfg->dispatch_cb(uart_no); } WRITE_PERI_REG(UART_INT_CLR(uart_no), int_st); }
void HardwareSerial::begin(const uint32_t baud/* = 9600*/) { //TODO: Move to params! UartDev.baut_rate = (UartBautRate)baud; UartDev.parity = NONE_BITS; UartDev.exist_parity = STICK_PARITY_DIS; UartDev.stop_bits = ONE_STOP_BIT; UartDev.data_bits = EIGHT_BITS; ETS_UART_INTR_ATTACH((void*)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); uart_div_modify(uart, UART_CLK_FREQ / (UartDev.baut_rate)); WRITE_PERI_REG(UART_CONF0(uart), UartDev.exist_parity | UartDev.parity | (UartDev.stop_bits << UART_STOP_BIT_NUM_S) | (UartDev.data_bits << UART_BIT_NUM_S)); //clear rx and tx fifo,not ready SET_PERI_REG_MASK(UART_CONF0(uart), UART_RXFIFO_RST | UART_TXFIFO_RST); CLEAR_PERI_REG_MASK(UART_CONF0(uart), UART_RXFIFO_RST | UART_TXFIFO_RST); //set rx fifo trigger WRITE_PERI_REG(UART_CONF1(uart), (UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S); //clear all interrupt WRITE_PERI_REG(UART_INT_CLR(uart), 0xffff); //enable rx_interrupt SET_PERI_REG_MASK(UART_INT_ENA(uart), UART_RXFIFO_FULL_INT_ENA); ETS_UART_INTR_ENABLE(); delay(10); Serial.println("\r\n"); // after SPAM :) }
/****************************************************************************** * FunctionName : uart_recvTask * Description : system task triggered on receive interrupt, empties FIFO and calls callbacks *******************************************************************************/ static void ICACHE_FLASH_ATTR uart_recvTask(os_event_t *events) { while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { //WRITE_PERI_REG(0X60000914, 0x73); //WTD // commented out by TvE // read a buffer-full from the uart uint16 length = 0; char buf[128]; while ((READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) && (length < 128)) { buf[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; } #ifdef UART_DBG os_printf("%d ix %d\n", system_get_time(), length); #endif for (int i=0; i<MAX_CB; i++) { if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length); } } WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR); ETS_UART_INTR_ENABLE(); }
/** * @brief Uart receive task. * @param events: contain the uart receive data * @retval None */ static void ICACHE_FLASH_ATTR /////// at_recvTask(os_event_t *events) { static uint8_t atHead[2]; static uint8_t *pCmdLine; uint8_t temp; // temp = events->par; // temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; // temp = 'X'; //add transparent determine while(READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { // temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; WRITE_PERI_REG(0X60000914, 0x73); //WTD temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; /* if(at_state != at_statIpTraning) { temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; if((temp != '\n') && (echoFlag)) { uart_tx_one_char(temp); //display back } } * */ if((at_state != at_statIpTraning) && (temp != '\n') && (echoFlag)) { uart_tx_one_char(UART0,temp); //display back // uart_tx_one_char(temp); //display back } switch(at_state) { case at_statIdle: //serch "AT" head atHead[0] = atHead[1]; atHead[1] = temp; if((os_memcmp(atHead, "AT", 2) == 0) || (os_memcmp(atHead, "at", 2) == 0)) { at_state = at_statRecving; pCmdLine = at_cmdLine; atHead[1] = 0x00; } else if(temp == '\n') //only get enter { uart0_sendStr("\r\nError\r\n"); } break; case at_statRecving: //push receive data to cmd line *pCmdLine = temp; if(temp == '\n') { system_os_post(at_procTaskPrio, 0, 0); at_state = at_statProcess; if(echoFlag) { uart0_sendStr("\r\n"); /////////// } } else if(pCmdLine >= &at_cmdLine[at_cmdLenMax - 1]) { at_state = at_statIdle; } pCmdLine++; break; case at_statProcess: //process data if(temp == '\n') { // system_os_post(at_busyTaskPrio, 0, 1); uart0_sendStr("\r\nbusy p...\r\n"); } break; case at_statIpSending: *pDataLine = temp; if((pDataLine >= &at_dataLine[at_sendLen - 1]) || (pDataLine >= &at_dataLine[at_dataLenMax - 1])) { system_os_post(at_procTaskPrio, 0, 0); at_state = at_statIpSended; } pDataLine++; // *pDataLine = temp; // if (pDataLine == &UartDev.rcv_buff.pRcvMsgBuff[at_sendLen-1]) // { // system_os_post(at_procTaskPrio, 0, 0); // at_state = at_statIpSended; // } // pDataLine++; break; case at_statIpSended: //send data if(temp == '\n') { // system_os_post(at_busyTaskPrio, 0, 2); uart0_sendStr("busy s...\r\n"); } break; case at_statIpTraning: os_timer_disarm(&at_delayChack); // *pDataLine = temp; if(pDataLine > &at_dataLine[at_dataLenMax - 1]) { os_printf("exceed\r\n"); return; } else if(pDataLine == &at_dataLine[at_dataLenMax - 1]) { temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; *pDataLine = temp; pDataLine++; at_tranLen++; os_timer_arm(&at_delayChack, 1, 0); return; } else { temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; *pDataLine = temp; pDataLine++; at_tranLen++; // if(ipDataSendFlag == 0) // { // os_timer_arm(&at_delayChack, 20, 0); // } os_timer_arm(&at_delayChack, 20, 0); } break; // os_timer_disarm(&at_delayChack); // *pDataLine = temp; // if(pDataLine >= &at_dataLine[at_dataLenMax - 1]) // { //// ETS_UART_INTR_DISABLE(); //// pDataLine++; // at_tranLen++; //// os_timer_arm(&at_delayChack, 1, 0); ///// // system_os_post(at_procTaskPrio, 0, 0); // break; // } // pDataLine++; // at_tranLen++; // if(ipDataSendFlag == 0) // { // os_timer_arm(&at_delayChack, 20, 0); // } // break; default: if(temp == '\n') { } break; } } 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); } else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) { WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); } ETS_UART_INTR_ENABLE(); }
void UART_ClearIntrStatus(UART_Port uart_no, uint32 clr_mask) { WRITE_PERI_REG(UART_INT_CLR(uart_no), clr_mask); }