-->
Page 1 of 2

UART0 Rx interrupt handler

PostPosted: Sat Apr 20, 2019 12:48 am
by hilogic
How to receive UART0 interrupt bytes in user_main.c. As of now i have to directly modify the uart_recvTask function in uart.c file in the SDK directory. uart.c is a peripheral driver and should not be modified or this is the way it is done in ESP8266 nonos SDK.

Re: UART0 Rx interrupt handler

PostPosted: Mon Apr 22, 2019 10:24 am
by Agentsmithers
Hey Hilogic!
I've reviewed a grip of uart.c and .h files and it appears there are a lot of custom versions out there including my own. I plan to be back in my office later today. I'll post my samples that worked for me currently and in the past as well here later today to see if that'll help ya get going.

Re: UART0 Rx interrupt handler

PostPosted: Mon Nov 16, 2020 6:43 am
by YRabbit
So can I look at the your correct uart.c and uart.h? :)

Re: UART0 Rx interrupt handler

PostPosted: Tue Nov 17, 2020 8:28 pm
by Agentsmithers
Still need it? I can post it if still required, The OP has not come back?

*Update* I posted the rest of my code here in the next post.. Here is a UserLevel of UART

Code: Select all//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);

   hex_printf(rxbuff,rxindex);
   return;
}

void ICACHE_FLASH_ATTR sdk_init_done_cb(void)
{
   os_printf("[%s] initializing ESP8266!\n", __func__);
   while(true)
   {
      os_printf("test");
      uart0_tx_buffer("page 1\xff\xff\xff", 9); //This is our Prod Port
      uart1_tx_buffer("page 2\xff\xff\xff", 9); //This is our Debug Port
      delay_second();
      delay_second();
   }
}

void ICACHE_FLASH_ATTR SetupUART(void)
{
   //Careful when the callback is triggered that it does not kick off any premature events like sending network packets before the network is connected, thus crashing your app
   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_set_os_print(1); //Turns os_PrintF Log Printing On or Off
   //We use UART swap because the RXpin is always High from external outputs causing issues when loading new firmware.
   //Keep in mind uart swap only swaps the RTS and CTS of the same UART to the RX and TX of the same UART, NOT UART1 to UART0
   //"UART0 swap. Use MTCK as UART0 Rx, MTDO as UART0 Tx, so ROM log will not output from this new UART0. MTDO (U0RTS) and MTCK (U0CTS) also need to be used as UART0 in hardware
   system_uart_swap(); //http://smallbits.marshall-tribe.net/blog/2016/11/13/esp8266-quiet-uart - Makes D7 RX (MTCK) and D8 TX (MTDO)
   os_install_putc1((void *)uart1_write_char); //Redirect OS_PRINTF to UART1, our debug port
}

void ICACHE_FLASH_ATTR user_init()
{
   SetupUART();

   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, We cannot touch this pin as this is our debug pin D4/GPIO2
   //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
}