So, In the last few days I am wrapping up some UART code on the esp8266, and as you can tell, it’s been quite a ride. I’ve been working with these Nextion HMI touch screen’s and I gotta say these thing’s are pretty awesome. I love the idea and it’s easy to work with. I’ll post some update’s once I get some program made but so far it’s pretty easy. Now only if I can get this ESP8266 to talk to this thing, everything would be rolling a bit smoother. Anyways to all the other UC / Arudino junkies, Highly recommended. šŸ™‚


//https://www.mikrocontroller.net/attachment/263828/The-ESP8266-Book-August-2015.pdf
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "ip_addr.h"
#include "mem.h"
#include "user_interface.h"
#include "lwip/stats.h"
#include "espconn.h"

#include "c_types.h" ////ONE WIRE

#include "../library/uart.h" //Copy these from your Driver Lib to your local folder
#include "../library/gpio16.h" //Copy these from your Driver Lib to your local folder

#include "../library/common.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t user_procTaskQueue[user_procTaskQueueLen];

void uart0_tx_buffer(uint8 *buf, uint16 len);

char rxbuff[31];
char rxindex = 0;

void UartReceive(char ByteReceived)
{
   os_printf("[%s] Got UART Callback!\r\n", __func__);
   os_printf("Byte Recevied: %c\r\n", ByteReceived);

   rxbuff[rxindex++] = ByteReceived;
   if (rxindex == sizeof(rxbuff)) 
   {
      rxindex = 0;
   }
   else
   {
      os_printf("%d != %d\r\n", rxindex, sizeof(rxbuff));
   }
   os_printf("Buffer: %s\r\n", rxbuff);
}

void ICACHE_FLASH_ATTR sdk_init_done_cb(void) 
{ 
   os_printf("[%s] initializing ESP8266!\n", __func__);
   while(true)
   {
      uart0_tx_buffer("test", 4);
      delay_second();
      delay_second();
   }
}

void ICACHE_FLASH_ATTR user_init()
{
   uart_init(BIT_RATE_115200, BIT_RATE_115200, &UartReceive); //This only seems to be kicked off via the COM-USB onboard, Not the TX/RX port
   //system_uart_swap(); //http://smallbits.marshall-tribe.net/blog/2016/11/13/esp8266-quiet-uart - Makes D7 RX and D8 TX
   system_init_done_cb(sdk_init_done_cb);

   wifi_set_opmode(0);
   wifi_set_sleep_type( NONE_SLEEP_T );

   ETS_GPIO_INTR_DISABLE();// Disable gpio interrupts
   gpio_init();   

   SetAllGPIOPinsAsOutput();

   /* Need to fix this to display value   */
   uint32 VDDADCByte[4] = {0};
   spi_flash_read(0x3fc06b, (uint32 *)&VDDADCByte, 1); //Read pads the other 3 bytes with FF

   os_printf("\r\n\r\nStarting ESP8266 OTA!\r\nSDK version:%s\r\nLoaded from: %02x\r\nVdd33_Const: %02x\r\n", system_get_sdk_version(), system_get_userbin_addr(), (VDDADCByte[0] & 0xff));
   //Turn off LED
   PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
   gpio_output_set((1 << 2), 0, 0, 0);

   //Start os task
   system_init_done_cb(sdk_init_done_cb);
   //system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); //Task to Signal for later
}

LOCAL void ICACHE_FLASH_ATTR uart_config(uint8 uart_no, void * callback(uint8))
{
   os_printf("uart_config:%d", uart_no);
   if (uart_no == UART1)
   {
      PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
   }
   else
   {
      /* UART0 */
      ETS_UART_INTR_ATTACH(uart0_rx_intr_handler,  callback);
      PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
      PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
      //Enable RxD pin
      //PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U);
      //PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3);      
      //PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
   }

   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));
      WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(UartDev.data_bits, NONE_BITS, UartDev.stop_bits)); //Override default settings
   }

   //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);

   //set rx fifo trigger
   //  WRITE_PERI_REG(UART_CONF1(uart_no),
   //                 ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
   //                 ((96 & UART_TXFIFO_EMPTY_THRHD) << UART_TXFIFO_EMPTY_THRHD_S) |
   //                 UART_RX_FLOW_EN);
   if (uart_no == UART0)
   {
      //set rx fifo trigger to receive 0x01 byte for each call
      WRITE_PERI_REG(UART_CONF1(uart_no), ((0x01 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | ((0x01 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | UART_RX_FLOW_EN | (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S | UART_RX_TOUT_EN);
      //WRITE_PERI_REG(UART_CONF1(uart_no), ((0x01 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | ((0x01 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | UART_RX_FLOW_EN);
      //SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA); //This worked
      //Clear pending interrupts
      WRITE_PERI_REG(UART_INT_CLR(UART0), 0xffff);
      //enable rx_interrupt
      SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_FULL_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);
   //enable rx_interrupt
   SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax