Chat freely about anything...

User avatar
By Ravi S Kashi
#39121 Hello Guys,

I have been struggling to make a UART Echo on UART0. I am using a NodeMCU Amica board. I input 1234567890 into the Transmit of Terminal and see 1VQ5ka95 on the Receive.

Other os_printf() string after the uart_init() works - So, I believe the bit-rate is not an issue. Can you please help me out here?

User Main function:
Code: Select all/**
 * Main User function
 */
void ICACHE_FLASH_ATTR
user_init(void)
{
   os_printf("SDK version:%s\n", system_get_sdk_version());
   os_delay_us(1000);

   /* Maintaining the same bit rate as SDK default. */
   uart_init(BIT_RATE_74880,BIT_RATE_74880);

   /* Maintaining the same bit rate as SDK default. */
   os_printf("UART bit rate set to 74880.\n" );

}


uart_init function:
Code: Select allvoid ICACHE_FLASH_ATTR
uart_init(UartBautRate uart0_br, UartBautRate uart1_br)
{
   /*this is a example to process uart data from task,please change the priority to fit your application task if exists*/
   system_os_task(uart_recvTask, uart_recvTaskPrio, uart_recvTaskQueue,
         uart_recvTaskQueueLen);  //demo with a task to process the uart data

   UartDev.baut_rate = uart0_br;
   uart_config(UART0);
   UartDev.baut_rate = uart1_br;
   uart_config(UART1);
   ETS_UART_INTR_ENABLE();

}


uart_recvTask function:
Code: Select allLOCAL void ICACHE_FLASH_ATTR ///////
uart_recvTask(os_event_t *events)
{
   if (events->sig == 0)
   {
#if  UART_BUFF_EN
      Uart_rx_buff_enq();
#else
      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);
      }
      WRITE_PERI_REG(UART_INT_CLR(UART0),
            UART_RXFIFO_FULL_INT_CLR | UART_RXFIFO_TOUT_INT_CLR);
      uart_rx_intr_enable(UART0);
#endif
   }
   else if (events->sig == 1)
   {
#if UART_BUFF_EN
      //already move uart buffer output to uart empty interrupt
      //tx_start_uart_buffer(UART0);
#else

#endif
   }
}


uart0_rx_intr_handler function:

Code: Select allLOCAL 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
    */
   uint8 RcvChar;
   uint8 uart_no = UART0;  //UartDev.buff_uart_no;
   uint8 fifo_len = 0;
   uint8 buf_idx = 0;
   uint8 temp, cnt;
   //RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;

   /*ATTENTION:*/
   /*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/
   /*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */
   /*IF NOT , POST AN EVENT AND PROCESS IN SYSTEM TASK */
   if (UART_FRM_ERR_INT_ST
         == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST))
   {
      DBG1("FRM_ERR\r\n");
      WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR);
   }
   else if (UART_RXFIFO_FULL_INT_ST
         == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST))
   {
      DBG("f");
      uart_rx_intr_disable(UART0);
      WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
      system_os_post(uart_recvTaskPrio, 0, 0);
   }
   else if (UART_RXFIFO_TOUT_INT_ST
         == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST))
   {
      DBG("t");
      uart_rx_intr_disable(UART0);
      WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
      system_os_post(uart_recvTaskPrio, 0, 0);
   }
   else if (UART_TXFIFO_EMPTY_INT_ST
         == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_TXFIFO_EMPTY_INT_ST))
   {
      DBG("e");
      /* to output uart data from uart buffer directly in empty interrupt handler*/
      /*instead of processing in system event, in order not to wait for current task/function to quit */
      /*ATTENTION:*/
      /*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/
      /*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */
      CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
#if UART_BUFF_EN
      tx_start_uart_buffer(UART0);
#endif
      //system_os_post(uart_recvTaskPrio, 1, 0);
      WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_TXFIFO_EMPTY_INT_CLR);

   }
   else if (UART_RXFIFO_OVF_INT_ST
         == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_OVF_INT_ST))
   {
      WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_RXFIFO_OVF_INT_CLR);
      DBG1("RX OVF!!\r\n");
   }
}
Last edited by Ravi S Kashi on Mon Jan 18, 2016 12:18 pm, edited 1 time in total.
User avatar
By Ravi S Kashi
#39151 Update - I am using Terminal as my Serial Terminal - if I send 1 byte per transmission - it seems to work. But if I send more than 1 byte, then the fifo len is also sometimes wrong and the chars are also incorrect.

Does anyone know - why this happens?

As an experiment, I flashed the AT firmware on this device and it seems to work. Where am I missing?