From bb386fcbbc0cb897de4f5b71eadcf91c110e3371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 16 Mar 2016 18:31:25 +0100 Subject: [PATCH] usart rx working, some cleaning --- Makefile | 8 +- esp_iot_sdk_v1.5.2/include/espconn.h | 75 ++++----- esp_iot_sdk_v1.5.2/include/espnow.h | 2 + esp_iot_sdk_v1.5.2/include/ping.h | 2 + esp_iot_sdk_v1.5.2/include/upgrade.h | 20 +-- esphttpd.pro | 44 ++++- esphttpd.pro.user | 79 +++++++-- libesphttpd/Makefile | 3 +- libesphttpd/include/espmissingincludes.h | 13 +- libesphttpd/util/captdns.c | 38 ++--- user/cfg_serial.c | 196 +++++++++++++++++++++++ user/{stdout.h => cfg_serial.h} | 2 +- user/io.c | 2 +- user/stdout.c | 69 -------- user/{uart.c => uart_driver.c} | 2 +- user/{uart.h => uart_driver.h} | 0 user/user_main.c | 18 ++- 17 files changed, 400 insertions(+), 173 deletions(-) create mode 100644 user/cfg_serial.c rename user/{stdout.h => cfg_serial.h} (77%) delete mode 100644 user/stdout.c rename user/{uart.c => uart_driver.c} (99%) rename user/{uart.h => uart_driver.h} (100%) diff --git a/Makefile b/Makefile index d486e95..d52bc58 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ ESP_FLASH_FREQ_DIV=0 ifeq ("$(OUTPUT_TYPE)","separate") -#In case of separate ESPFS and binaries, set the pos and length of the ESPFS here. +#In case of separate ESPFS and binaries, set the pos and length of the ESPFS here. ESPFS_POS = 0x18000 ESPFS_SIZE = 0x28000 endif @@ -28,10 +28,10 @@ FW_BASE = firmware # Base directory for the compiler. Needs a / at the end; if not set it'll use the tools that are in # the PATH. -XTENSA_TOOLS_ROOT ?= +XTENSA_TOOLS_ROOT ?= # base directory of the ESP8266 SDK package, absolute -SDK_BASE ?= esp_iot_sdk_v1.5.2 +SDK_BASE ?= ./esp_iot_sdk_v1.5.2 # Opensdk patches stdint.h when compiled with an internal SDK. If you run into compile problems pertaining to # redefinition of int types, try setting this to 'yes'. @@ -207,6 +207,6 @@ clean: $(Q) rm -f $(TARGET_OUT) $(Q) find $(BUILD_BASE) -type f | xargs rm -f $(Q) rm -rf $(FW_BASE) - + $(foreach bdir,$(BUILD_DIR),$(eval $(call compile-objects,$(bdir)))) diff --git a/esp_iot_sdk_v1.5.2/include/espconn.h b/esp_iot_sdk_v1.5.2/include/espconn.h index eeb603e..5c2454a 100644 --- a/esp_iot_sdk_v1.5.2/include/espconn.h +++ b/esp_iot_sdk_v1.5.2/include/espconn.h @@ -1,6 +1,9 @@ #ifndef __ESPCONN_H__ #define __ESPCONN_H__ +#include +#include + typedef sint8 err_t; typedef void *espconn_handle; @@ -30,39 +33,39 @@ typedef void (* espconn_reconnect_callback)(void *arg, sint8 err); /** Protocol family and type of the espconn */ enum espconn_type { - ESPCONN_INVALID = 0, - /* ESPCONN_TCP Group */ - ESPCONN_TCP = 0x10, - /* ESPCONN_UDP Group */ - ESPCONN_UDP = 0x20, + ESPCONN_INVALID = 0, + /* ESPCONN_TCP Group */ + ESPCONN_TCP = 0x10, + /* ESPCONN_UDP Group */ + ESPCONN_UDP = 0x20, }; /** Current state of the espconn. Non-TCP espconn are always in state ESPCONN_NONE! */ enum espconn_state { - ESPCONN_NONE, - ESPCONN_WAIT, - ESPCONN_LISTEN, - ESPCONN_CONNECT, - ESPCONN_WRITE, - ESPCONN_READ, - ESPCONN_CLOSE + ESPCONN_NONE, + ESPCONN_WAIT, + ESPCONN_LISTEN, + ESPCONN_CONNECT, + ESPCONN_WRITE, + ESPCONN_READ, + ESPCONN_CLOSE }; typedef struct _esp_tcp { - int remote_port; - int local_port; - uint8 local_ip[4]; - uint8 remote_ip[4]; - espconn_connect_callback connect_callback; - espconn_reconnect_callback reconnect_callback; - espconn_connect_callback disconnect_callback; + int remote_port; + int local_port; + uint8 local_ip[4]; + uint8 remote_ip[4]; + espconn_connect_callback connect_callback; + espconn_reconnect_callback reconnect_callback; + espconn_connect_callback disconnect_callback; espconn_connect_callback write_finish_fn; } esp_tcp; typedef struct _esp_udp { - int remote_port; - int local_port; - uint8 local_ip[4]; + int remote_port; + int local_port; + uint8 local_ip[4]; uint8 remote_ip[4]; } esp_udp; @@ -78,19 +81,19 @@ typedef void (* espconn_sent_callback)(void *arg); /** A espconn descriptor */ struct espconn { - /** type of the espconn (TCP, UDP) */ - enum espconn_type type; - /** current state of the espconn */ - enum espconn_state state; - union { - esp_tcp *tcp; - esp_udp *udp; - } proto; - /** A callback function that is informed about events for this espconn */ - espconn_recv_callback recv_callback; - espconn_sent_callback sent_callback; - uint8 link_cnt; - void *reverse; + /** type of the espconn (TCP, UDP) */ + enum espconn_type type; + /** current state of the espconn */ + enum espconn_state state; + union { + esp_tcp *tcp; + esp_udp *udp; + } proto; + /** A callback function that is informed about events for this espconn */ + espconn_recv_callback recv_callback; + espconn_sent_callback sent_callback; + uint8 link_cnt; + void *reverse; }; enum espconn_option{ @@ -417,7 +420,7 @@ typedef void (*dns_found_callback)(const char *name, ip_addr_t *ipaddr, void *ca * Description : Resolve a hostname (string) into an IP address. * Parameters : pespconn -- espconn to resolve a hostname * hostname -- the hostname that is to be queried - * addr -- pointer to a ip_addr_t where to store the address if + * addr -- pointer to a ip_addr_t where to store the address if * it is already cached in the dns_table (only valid if ESPCONN_OK * is returned!) * found -- a callback function to be called on success, failure diff --git a/esp_iot_sdk_v1.5.2/include/espnow.h b/esp_iot_sdk_v1.5.2/include/espnow.h index 3bb6f83..58715e2 100644 --- a/esp_iot_sdk_v1.5.2/include/espnow.h +++ b/esp_iot_sdk_v1.5.2/include/espnow.h @@ -6,6 +6,8 @@ #ifndef __ESPNOW_H__ #define __ESPNOW_H__ +#include + enum esp_now_role { ESP_NOW_ROLE_IDLE = 0, ESP_NOW_ROLE_CONTROLLER, diff --git a/esp_iot_sdk_v1.5.2/include/ping.h b/esp_iot_sdk_v1.5.2/include/ping.h index 4ecd032..ed2228c 100644 --- a/esp_iot_sdk_v1.5.2/include/ping.h +++ b/esp_iot_sdk_v1.5.2/include/ping.h @@ -1,6 +1,8 @@ #ifndef __PING_H__ #define __PING_H__ +#include + typedef void (* ping_recv_function)(void* arg, void *pdata); typedef void (* ping_sent_function)(void* arg, void *pdata); diff --git a/esp_iot_sdk_v1.5.2/include/upgrade.h b/esp_iot_sdk_v1.5.2/include/upgrade.h index cddf839..a6101ab 100644 --- a/esp_iot_sdk_v1.5.2/include/upgrade.h +++ b/esp_iot_sdk_v1.5.2/include/upgrade.h @@ -1,6 +1,8 @@ #ifndef __UPGRADE_H__ #define __UPGRADE_H__ +#include + #define SPI_FLASH_SEC_SIZE 4096 #define LIMIT_ERASE_SIZE 0x10000 @@ -19,19 +21,19 @@ typedef void (*upgrade_states_check_callback)(void * arg); //#define UPGRADE_SSL_ENABLE struct upgrade_server_info { - uint8 ip[4]; - uint16 port; + uint8 ip[4]; + uint16 port; - uint8 upgrade_flag; + uint8 upgrade_flag; - uint8 pre_version[16]; - uint8 upgrade_version[16]; + uint8 pre_version[16]; + uint8 upgrade_version[16]; - uint32 check_times; - uint8 *url; + uint32 check_times; + uint8 *url; - upgrade_states_check_callback check_cb; - struct espconn *pespconn; + upgrade_states_check_callback check_cb; + struct espconn *pespconn; }; #define UPGRADE_FLAG_IDLE 0x00 diff --git a/esphttpd.pro b/esphttpd.pro index 90f9591..daa3e93 100644 --- a/esphttpd.pro +++ b/esphttpd.pro @@ -9,7 +9,8 @@ INCLUDEPATH = . \ esp_iot_sdk_v1.5.2/include \ include \ libesphttpd/include \ - libesphttpd/espfs + libesphttpd/espfs \ + libesphttpd/core SOURCES += \ libesphttpd/core/auth.c \ @@ -38,9 +39,9 @@ SOURCES += \ user/cgi-test.c \ user/cgi.c \ user/io.c \ - user/stdout.c \ user/user_main.c \ - user/uart.c + user/cfg_serial.c \ + user/uart_driver.c HEADERS += \ include/uart_hw.h \ @@ -71,9 +72,38 @@ HEADERS += \ user/cgi-test.h \ user/cgi.h \ user/io.h \ - user/stdout.h \ - user/uart.h \ - user/uart_register.h + user/uart_register.h \ + user/cfg_serial.h \ + esp_iot_sdk_v1.5.2/include/json/json.h \ + esp_iot_sdk_v1.5.2/include/json/jsonparse.h \ + esp_iot_sdk_v1.5.2/include/json/jsontree.h \ + esp_iot_sdk_v1.5.2/include/airkiss.h \ + esp_iot_sdk_v1.5.2/include/at_custom.h \ + esp_iot_sdk_v1.5.2/include/c_types.h \ + esp_iot_sdk_v1.5.2/include/eagle_soc.h \ + esp_iot_sdk_v1.5.2/include/esp_sdk_ver.h \ + esp_iot_sdk_v1.5.2/include/espconn.h \ + esp_iot_sdk_v1.5.2/include/espnow.h \ + esp_iot_sdk_v1.5.2/include/ets_sys.h \ + esp_iot_sdk_v1.5.2/include/gpio.h \ + esp_iot_sdk_v1.5.2/include/ip_addr.h \ + esp_iot_sdk_v1.5.2/include/mem.h \ + esp_iot_sdk_v1.5.2/include/mesh.h \ + esp_iot_sdk_v1.5.2/include/os_type.h \ + esp_iot_sdk_v1.5.2/include/osapi.h \ + esp_iot_sdk_v1.5.2/include/ping.h \ + esp_iot_sdk_v1.5.2/include/pwm.h \ + esp_iot_sdk_v1.5.2/include/queue.h \ + esp_iot_sdk_v1.5.2/include/slc_register.h \ + esp_iot_sdk_v1.5.2/include/smartconfig.h \ + esp_iot_sdk_v1.5.2/include/sntp.h \ + esp_iot_sdk_v1.5.2/include/spi_flash.h \ + esp_iot_sdk_v1.5.2/include/spi_register.h \ + esp_iot_sdk_v1.5.2/include/uart_register.h \ + esp_iot_sdk_v1.5.2/include/upgrade.h \ + esp_iot_sdk_v1.5.2/include/user_interface.h \ + user/uart_driver.h DISTFILES += \ - style.astylerc + style.astylerc \ + Makefile diff --git a/esphttpd.pro.user b/esphttpd.pro.user index 1af52c7..63c03e4 100644 --- a/esphttpd.pro.user +++ b/esphttpd.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -63,22 +63,11 @@ {ee748544-c6c0-4f44-9cef-fbb65dc2525a} 0 0 - 0 + 1 - /home/ondra/devel/esp/projects/build-esphttpd-Desktop-Debug + /home/ondra/devel/esp/projects/esphttpd - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - false - false - - true Make @@ -91,7 +80,7 @@ - 2 + 1 Build ProjectExplorer.BuildSteps.Build @@ -117,7 +106,9 @@ 2 false - + + PATH=/usr/lib/qt/bin:/usr/bin:/home/ondra/GNUstep/Tools:/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/android-sdk/platform-tools:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/ondra/bin:/home/ondra/devel/esp/sdk/esp-open-sdk/xtensa-lx106-elf/bin/ + Debug Qt4ProjectManager.Qt4BuildConfiguration @@ -316,7 +307,61 @@ false true - 1 + + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + flash -j 4 + /bin/make + %{buildDir} + Run /bin/make + + ProjectExplorer.CustomExecutableRunConfiguration + 3768 + false + true + false + false + true + + 2 diff --git a/libesphttpd/Makefile b/libesphttpd/Makefile index 92cced9..6faa1c8 100644 --- a/libesphttpd/Makefile +++ b/libesphttpd/Makefile @@ -28,7 +28,8 @@ XTENSA_TOOLS_ROOT ?= # base directory of the ESP8266 SDK package, absolute # Only used for the non-FreeRTOS build -SDK_BASE ?= /opt/Espressif/ESP8266_SDK +SDK_BASE ?= ../esp_iot_sdk_v1.5.2 +#opt/Espressif/ESP8266_SDK # Base directory of the ESP8266 FreeRTOS SDK package, absolute # Only used for the FreeRTOS build diff --git a/libesphttpd/include/espmissingincludes.h b/libesphttpd/include/espmissingincludes.h index 4c313fd..ce77720 100644 --- a/libesphttpd/include/espmissingincludes.h +++ b/libesphttpd/include/espmissingincludes.h @@ -9,6 +9,7 @@ int strcasecmp(const char *a, const char *b); #ifndef FREERTOS #include #include +#include //Missing function prototypes in include folders. Gcc will warn on these if we don't define 'em anywhere. //MOST OF THESE ARE GUESSED! but they seem to swork and shut up the compiler. typedef struct espconn espconn; @@ -33,7 +34,9 @@ void ets_timer_arm_new(os_timer_t *a, int b, int c, int isMstimer); void ets_timer_disarm(os_timer_t *a); void ets_timer_setfn(os_timer_t *t, ETSTimerFunc *fn, void *parg); void ets_update_cpu_frequency(int freqmhz); + int os_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); + int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4))); int os_printf_plus(const char *format, ...) __attribute__ ((format (printf, 1, 2))); void uart_div_modify(int no, unsigned int freq); @@ -64,11 +67,11 @@ void pvPortFree(void *ptr, const char *file, int line); #ifdef PIN_FUNC_SELECT #undef PIN_FUNC_SELECT #define PIN_FUNC_SELECT(PIN_NAME, FUNC) do { \ - WRITE_PERI_REG(PIN_NAME, \ - (READ_PERI_REG(PIN_NAME) \ - & (~(PERIPHS_IO_MUX_FUNC< wrote this file. As long as you retain - * this notice you can do whatever you want with this stuff. If we meet some day, - * and you think this stuff is worth it, you can buy me a beer in return. + * Jeroen Domburg wrote this file. As long as you retain + * this notice you can do whatever you want with this stuff. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. * ---------------------------------------------------------------------------- */ /* This is a 'captive portal' DNS server: it basically replies with a fixed IP (in this case: -the one of the SoftAP interface of this ESP module) for any and all DNS queries. This can +the one of the SoftAP interface of this ESP module) for any and all DNS queries. This can be used to send mobile phones, tablets etc which connect to the ESP in AP mode directly to the internal webserver. */ @@ -95,17 +95,17 @@ typedef struct __attribute__ ((packed)) { //Function to put unaligned 16-bit network values static void ICACHE_FLASH_ATTR setn16(void *pp, int16_t n) { char *p=pp; - *p++=(n>>8); - *p++=(n&0xff); + *p++=(char)(n>>8); + *p++=(char)(n&0xff); } //Function to put unaligned 32-bit network values static void ICACHE_FLASH_ATTR setn32(void *pp, int32_t n) { char *p=pp; - *p++=(n>>24)&0xff; - *p++=(n>>16)&0xff; - *p++=(n>>8)&0xff; - *p++=(n&0xff); + *p++=(char)((n>>24)&0xff); + *p++=(char)((n>>16)&0xff); + *p++=(char)((n>>8)&0xff); + *p++=(char)(n&0xff); } static uint16_t ICACHE_FLASH_ATTR my_ntohs(uint16_t *in) { @@ -114,7 +114,7 @@ static uint16_t ICACHE_FLASH_ATTR my_ntohs(uint16_t *in) { } -//Parses a label into a C-string containing a dotted +//Parses a label into a C-string containing a dotted //Returns pointer to start of next fields in packet static char* ICACHE_FLASH_ATTR labelToStr(char *packet, char *labelPtr, int packetSz, char *res, int resMaxLen) { int i, j, k; @@ -153,7 +153,7 @@ static char ICACHE_FLASH_ATTR *strToLabel(char *str, char *label, int maxLen) { char *p=label+1; //ptr to next label byte to be written while (1) { if (*str=='.' || *str==0) { - *len=((p-len)-1); //write len of label bit + *len=(char)((p-len)-1); //write len of label bit len=p; //pos of len for next part p++; //data ptr is one past len if (*str==0) break; //done @@ -183,7 +183,7 @@ static void ICACHE_FLASH_ATTR captdnsRecv(struct sockaddr_in *premote_addr, char DnsHeader *hdr=(DnsHeader*)p; DnsHeader *rhdr=(DnsHeader*)&reply[0]; p+=sizeof(DnsHeader); -// httpd_printf("DNS packet: id 0x%X flags 0x%X rcode 0x%X qcnt %d ancnt %d nscount %d arcount %d len %d\n", +// httpd_printf("DNS packet: id 0x%X flags 0x%X rcode 0x%X qcnt %d ancnt %d nscount %d arcount %d len %d\n", // my_ntohs(&hdr->id), hdr->flags, hdr->rcode, my_ntohs(&hdr->qdcount), my_ntohs(&hdr->ancount), my_ntohs(&hdr->nscount), my_ntohs(&hdr->arcount), length); //Some sanity checks: if (length>DNS_LEN) return; //Packet is longer than DNS implementation allows @@ -276,14 +276,14 @@ static void captdnsTask(void *pvParameters) { socklen_t fromlen; struct ip_info ipconfig; char udp_msg[DNS_LEN]; - + memset(&ipconfig, 0, sizeof(ipconfig)); memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; + server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(53); server_addr.sin_len = sizeof(server_addr); - + do { sockFd=socket(AF_INET, SOCK_DGRAM, 0); if (sockFd==-1) { @@ -291,7 +291,7 @@ static void captdnsTask(void *pvParameters) { vTaskDelay(1000/portTICK_RATE_MS); } } while (sockFd==-1); - + do { ret=bind(sockFd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (ret!=0) { @@ -307,7 +307,7 @@ static void captdnsTask(void *pvParameters) { ret=recvfrom(sockFd, (u8 *)udp_msg, DNS_LEN, 0,(struct sockaddr *)&from,(socklen_t *)&fromlen); if (ret>0) captdnsRecv(&from,udp_msg,ret); } - + close(sockFd); vTaskDelete(NULL); } @@ -328,4 +328,4 @@ void ICACHE_FLASH_ATTR captdnsInit(void) { espconn_create(&conn); } -#endif \ No newline at end of file +#endif diff --git a/user/cfg_serial.c b/user/cfg_serial.c new file mode 100644 index 0000000..db1cb81 --- /dev/null +++ b/user/cfg_serial.c @@ -0,0 +1,196 @@ +//Stupid bit of code that does the bare minimum to make os_printf work. + +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * Jeroen Domburg wrote this file. As long as you retain + * this notice you can do whatever you want with this stuff. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. + * ---------------------------------------------------------------------------- + */ + +#include +#include "uart_driver.h" + + + +LOCAL void uart0_rx_intr_handler(void *para); +LOCAL void uart_recvTask(os_event_t *events); + +#define uart_recvTaskPrio 0 +#define uart_recvTaskQueueLen 10 +LOCAL os_event_t uart_recvTaskQueue[uart_recvTaskQueueLen]; + + + + +/** Clear the fifos */ +void ICACHE_FLASH_ATTR +clear_rxtx(int uart_no) +{ + 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); +} + + +/** + * @brief Configure UART 115200-8-N-1 + * @param uart_no + */ +void ICACHE_FLASH_ATTR +my_uart_init(UARTn uart_no) +{ + UART_SetParity(uart_no, PARITY_NONE); + UART_SetStopBits(uart_no, ONE_STOP_BIT); + UART_SetWordLength(uart_no, EIGHT_BITS); + UART_SetBaudrate(uart_no, BIT_RATE_115200); + UART_ResetFifo(uart_no); +} + + +void ICACHE_FLASH_ATTR +serialInit() +{ + // U0TXD + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); + PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); + + // U0RXD + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); + + // U1TXD (GPIO2) + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); + + // Configure the UART peripherals + my_uart_init(UART0); // main + my_uart_init(UART1); // debug (output only) + + // Select debug port + UART_SetPrintPort(UART1); + + + // Configure Rx on UART0 + + + // Start the Rx reading task + system_os_task(uart_recvTask, uart_recvTaskPrio, uart_recvTaskQueue, uart_recvTaskQueueLen); + + + // set handler + ETS_UART_INTR_ATTACH((void *)uart0_rx_intr_handler, &(UartDev.rcv_buff)); // the buf will be used as an arg + + + // fifo threshold config + uint32_t conf = ((100 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S); + conf |= ((0x10 & UART_TXFIFO_EMPTY_THRHD) << UART_TXFIFO_EMPTY_THRHD_S); + + // timeout config + conf |= ((0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S); // timeout threshold + conf |= UART_RX_TOUT_EN; // enable timeout + + WRITE_PERI_REG(UART_CONF1(UART0), conf); + + + // enable TOUT and ERR irqs + SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA); + + /* clear interrupt flags */ + WRITE_PERI_REG(UART_INT_CLR(UART0), 0xffff); + + /* enable RX interrupts */ + SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_OVF_INT_ENA); + + + // Enable IRQ in Extensa + ETS_UART_INTR_ENABLE(); +} + + +// ---- receive business ---- + + + +void uart_rx_intr_disable(uint8 uart_no) +{ + CLEAR_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA); +} + + +void uart_rx_intr_enable(uint8 uart_no) +{ + SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA); +} + + +/** + * @brief get number of bytes in UART tx fifo + * @param UART number + */ +#define UART_GetRxFifoCount(uart_no) ((READ_PERI_REG(UART_STATUS((uart_no))) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT) + + +LOCAL void ICACHE_FLASH_ATTR +uart_recvTask(os_event_t *events) +{ + if (events->sig == 0) { + uint8 fifo_len = UART_GetRxFifoCount(UART0); + + // read from the FIFO & print back + for (uint8 idx = 0; idx < fifo_len; idx++) { + uint8 d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; + UART_WriteChar(UART0, d_tmp, 0); + } + + // clear irq flags + WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR | UART_RXFIFO_TOUT_INT_CLR); + // enable rx irq again + uart_rx_intr_enable(UART0); + } else if (events->sig == 1) { + // ??? + } +} + + +/****************************************************************************** + * 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 +*******************************************************************************/ +LOCAL void +uart0_rx_intr_handler(void *para) +{ + uint32_t status_reg = READ_PERI_REG(UART_INT_ST(UART0)); + + if (status_reg & UART_FRM_ERR_INT_ST) { + // Framing Error + WRITE_PERI_REG(UART_INT_CLR(UART0), UART_FRM_ERR_INT_CLR); + } + + if (status_reg & UART_RXFIFO_FULL_INT_ST) { + // RX fifo full + uart_rx_intr_disable(UART0); + WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR); + + // run handler + system_os_post(uart_recvTaskPrio, 0, 0); /* -> notify the polling thread */ + } + + if (status_reg & UART_RXFIFO_TOUT_INT_ST) { + // Fifo timeout + uart_rx_intr_disable(UART0); + WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); + + // run handler + system_os_post(uart_recvTaskPrio, 0, 0); /* -> notify the polling thread */ + } + + if (status_reg & UART_TXFIFO_EMPTY_INT_ST) { + CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA); + } + + if (status_reg & UART_RXFIFO_OVF_INT_ST) { + WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_OVF_INT_CLR); + } +} diff --git a/user/stdout.h b/user/cfg_serial.h similarity index 77% rename from user/stdout.h rename to user/cfg_serial.h index 8852382..90b9419 100644 --- a/user/stdout.h +++ b/user/cfg_serial.h @@ -3,6 +3,6 @@ #include -void stdoutInit(); +void serialInit(); #endif diff --git a/user/io.c b/user/io.c index 4edda60..2e766b4 100644 --- a/user/io.c +++ b/user/io.c @@ -38,7 +38,7 @@ static void ICACHE_FLASH_ATTR resetBtnTimerCb(void *arg) { } else { if (resetCnt>=6) { //3 sec pressed wifi_station_disconnect(); - wifi_set_opmode(0x3); //reset to AP+STA mode + wifi_set_opmode(STATIONAP_MODE); //reset to AP+STA mode os_printf("Reset to AP mode. Restarting system...\n"); system_restart(); } diff --git a/user/stdout.c b/user/stdout.c deleted file mode 100644 index e751a5d..0000000 --- a/user/stdout.c +++ /dev/null @@ -1,69 +0,0 @@ -//Stupid bit of code that does the bare minimum to make os_printf work. - -/* - * ---------------------------------------------------------------------------- - * "THE BEER-WARE LICENSE" (Revision 42): - * Jeroen Domburg wrote this file. As long as you retain - * this notice you can do whatever you want with this stuff. If we meet some day, - * and you think this stuff is worth it, you can buy me a beer in return. - * ---------------------------------------------------------------------------- - */ - -#include -#include "uart.h" - - -///** USART1 write routine */ -//static void ICACHE_FLASH_ATTR stdoutUart1Txd(char c) -//{ -// //Wait until there is room in the FIFO -// while (((READ_PERI_REG(UART_STATUS(0)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) >= 126) ; -// //Send the character -// WRITE_PERI_REG(UART_FIFO(0), c); -//} - - -///** Putchar to USART1 */ -//static void ICACHE_FLASH_ATTR stdoutPutcharUSART1(char c) -//{ -// //convert \n -> \r\n -// if (c == '\n') stdoutUart1Txd('\r'); -// stdoutUart1Txd(c); -//} - - -/** - * @brief Configure UART 115200-8-N-1 - * @param uart_no - */ -void ICACHE_FLASH_ATTR -my_uart_init(UARTn uart_no) -{ - UART_SetParity(uart_no, PARITY_NONE); - UART_SetStopBits(uart_no, ONE_STOP_BIT); - UART_SetWordLength(uart_no, EIGHT_BITS); - UART_SetBaudrate(uart_no, BIT_RATE_115200); - UART_ResetFifo(uart_no); -} - - -void ICACHE_FLASH_ATTR -stdoutInit() -{ - // U0TXD - PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); - PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); - - // U0RXD - PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); - - // U1TXD (GPIO2) - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); - - // Configure the UART peripherals - my_uart_init(UART0); // main - my_uart_init(UART1); // debug (output only) - - // Select debug port - UART_SetPrintPort(UART1); -} diff --git a/user/uart.c b/user/uart_driver.c similarity index 99% rename from user/uart.c rename to user/uart_driver.c index 6d810e6..96f3d09 100644 --- a/user/uart.c +++ b/user/uart_driver.c @@ -2,7 +2,7 @@ * Driver file for ESP8266 UART, works with the SDK. */ -#include "uart.h" +#include "uart_driver.h" #include diff --git a/user/uart.h b/user/uart_driver.h similarity index 100% rename from user/uart.h rename to user/uart_driver.h diff --git a/user/user_main.c b/user/user_main.c index fe126e9..230345c 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -21,8 +21,9 @@ // user files #include "cgi.h" -#include "stdout.h" +#include "cfg_serial.h" #include "io.h" +#include "uart_driver.h" /** @@ -113,6 +114,13 @@ static HttpdBuiltInUrl builtInUrls[] = { }; +static ETSTimer prTestTimer; + +static void ICACHE_FLASH_ATTR test_timer_task(void *arg) { + const char * t = "Test\r\n"; + UART_WriteBuffer(0, (uint8_t*)t, strlen(t), 1000); +} + /** * Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done. @@ -120,7 +128,7 @@ static HttpdBuiltInUrl builtInUrls[] = { void user_init(void) { // set up the debuging output - stdoutInit(); + serialInit(); // reset button etc ioInit(); @@ -142,8 +150,12 @@ void user_init(void) httpdInit(builtInUrls, 80); - os_printf("\nReady\n"); + + // print TEST on the command interface every 500 ms + os_timer_disarm(&prTestTimer); + os_timer_setfn(&prTestTimer, test_timer_task, NULL); + os_timer_arm(&prTestTimer, 500, 1); }