From 72671bba6757522747eb64ed12422165b57a8467 Mon Sep 17 00:00:00 2001 From: n800sau Date: Wed, 18 Feb 2015 20:39:24 +1100 Subject: [PATCH] do no skipping chars from uart --- driver/uart.c | 18 ++++++ include/driver/uart.h | 4 +- include/espmissingincludes.h | 15 ++++- user/server.c | 9 +-- user/user_main.c | 115 ++++++++++++++++++++++++++++++++--- 5 files changed, 146 insertions(+), 15 deletions(-) diff --git a/driver/uart.c b/driver/uart.c index 1f67572..8f97c01 100644 --- a/driver/uart.c +++ b/driver/uart.c @@ -200,6 +200,22 @@ uart0_sendStr(const char *str) } } +/****************************************************************************** + * FunctionName : uart1_sendStr + * Description : use uart1 to transfer buffer + * Parameters : uint8 *buf - point to send buffer + * uint16 len - buffer len + * Returns : +*******************************************************************************/ +void ICACHE_FLASH_ATTR +uart1_sendStr(const char *str) +{ + while(*str) + { + uart_tx_one_char(UART1, *str++); + } +} + /****************************************************************************** * FunctionName : uart0_rx_intr_handler * Description : Internal used function @@ -323,4 +339,6 @@ uart_reattach() uart_init(BIT_RATE_74880, BIT_RATE_74880); // ETS_UART_INTR_ATTACH(uart_rx_intr_handler_ssc, &(UartDev.rcv_buff)); // ETS_UART_INTR_ENABLE(); + // install uart1 putc callback + os_install_putc1((void *)uart1_write_char); } diff --git a/include/driver/uart.h b/include/driver/uart.h index 9927759..d6dfbd3 100644 --- a/include/driver/uart.h +++ b/include/driver/uart.h @@ -113,7 +113,9 @@ typedef struct { } UartDevice; void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); -void uart0_sendStr(const char *str); +void ICACHE_FLASH_ATTR uart0_sendStr(const char *str); +void ICACHE_FLASH_ATTR uart1_sendStr(const char *str); +void ICACHE_FLASH_ATTR uart0_tx_buffer(uint8 *buf, uint16 len); #endif diff --git a/include/espmissingincludes.h b/include/espmissingincludes.h index 1402bce..0fff213 100644 --- a/include/espmissingincludes.h +++ b/include/espmissingincludes.h @@ -1,10 +1,12 @@ -#ifndef ESPMISSINGINCLUIDES_H -#define ESPMISSINGINCLUIDES_H +#ifndef ESPMISSINGINCLUDES_H +#define ESPMISSINGINCLUDES_H #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; int atoi(const char *nptr); void ets_install_putc1(void *routine); @@ -12,7 +14,6 @@ void ets_isr_attach(int intr, void *handler, void *arg); void ets_isr_mask(unsigned intr); void ets_isr_unmask(unsigned intr); int ets_memcmp(const void *s1, const void *s2, size_t n); -void *ets_bzero(void *dest, size_t n); void *ets_memcpy(void *dest, const void *src, size_t n); void *ets_memset(void *s, int c, size_t n); int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); @@ -26,12 +27,20 @@ char *ets_strstr(const char *haystack, const char *needle); void ets_timer_arm_new(ETSTimer *a, int b, int c, int isMstimer); void ets_timer_disarm(ETSTimer *a); void ets_timer_setfn(ETSTimer *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 pvPortFree(void *ptr); void *pvPortMalloc(size_t xWantedSize); void *pvPortZalloc(size_t); void uart_div_modify(int no, unsigned int freq); void vPortFree(void *ptr); void *vPortMalloc(size_t xWantedSize); +uint8 wifi_get_opmode(void); +uint32 system_get_time(); +int os_random(); +int rand(void); +void ets_bzero(void *s, size_t n); +void ets_delay_us(int ms); #endif diff --git a/user/server.c b/user/server.c index f78417f..eeac2de 100644 --- a/user/server.c +++ b/user/server.c @@ -5,6 +5,7 @@ #include "mem.h" #include "osapi.h" #include +#include #include "server.h" #include "config.h" @@ -47,10 +48,10 @@ static void ICACHE_FLASH_ATTR serverRecvCb(void *arg, char *data, unsigned short os_printf("\r\n"); if( len == 2 && data[0] == 0x30 && data[1] == 0x20 && ! reset_sent ) { // send reset to arduino - GPIO_OUTPUT_SET(5, 0); - os_printf("Send Reset\n"); - os_delay_us(1000000L); - GPIO_OUTPUT_SET(5, 1); +// GPIO_OUTPUT_SET(5, 0); +// os_printf("Send Reset\n"); +// os_delay_us(1000000L); +// GPIO_OUTPUT_SET(5, 1); reset_sent = true; } } diff --git a/user/user_main.c b/user/user_main.c index e956796..e70743f 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -15,10 +15,13 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +#include "espmissingincludes.h" #include #include "ets_sys.h" #include "os_type.h" #include "osapi.h" +#include "mem.h" + #include "driver/uart.h" #include "task.h" @@ -30,24 +33,95 @@ os_event_t recvTaskQueue[recvTaskQueueLen]; extern serverConnData connData[MAX_CONN]; +#define UART_BUF_SIZE 100 +static char uart_buf[UART_BUF_SIZE]; +static int buf_pos = 0; + +#define TICK_TIME 100 +#define TICK_TIMEOUT 10000 +#define TICK_MAX (TICK_TIMEOUT / TICK_TIME) +#define SYNC_PAUSE 500 +#define SYNC_STEP (SYNC_PAUSE / TICK_TIME) + +static void add_char(char c) +{ + uart_buf[buf_pos] = c; + buf_pos++; + if(buf_pos >= sizeof(uart_buf)) { + buf_pos = sizeof(uart_buf) - 1; + // forget the oldest + memmove(uart_buf, uart_buf+1, sizeof(uart_buf) - 1); + } +} + +static char get_char() +{ + char rs = -1; + if(buf_pos > 0) { + rs = uart_buf[0]; + memmove(uart_buf, uart_buf+1, sizeof(uart_buf) - 1); + buf_pos--; + } + os_printf("get char : %2X\n", rs); + return rs; +} + +static int count_chars() +{ + return buf_pos; +} + +static void clean_chars() +{ + buf_pos = 0; +} + static void ICACHE_FLASH_ATTR recvTask(os_event_t *events) +{ + uint8_t temp; + + //add transparent determine + while(READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) + { + WRITE_PERI_REG(0X60000914, 0x73); //WTD + + temp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; + add_char(temp); + os_printf("char=%2X\n", temp); + } + 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(); +} + + + + + + +/*static void ICACHE_FLASH_ATTR recvTask(os_event_t *events) { uint8_t c, i; - //uart0_sendStr("\r\nrecTask called\r\n"); + uart1_sendStr("\r\nrecTask called\r\n"); 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; + os_printf("\r\n"); for (i = 0; i < MAX_CONN; ++i) { if (connData[i].conn) { -// os_printf("<< 0x%2.2X ", c); - espconn_sent(connData[i].conn, &c, 1); + os_printf("<< 0x%X\r\n", c); + char buf[10]; + os_sprintf(buf, "%c ", c); + espconn_sent(connData[i].conn, &buf, 2); } } -// os_printf("\r\n"); // echo // uart_tx_one_char(c); } @@ -61,7 +135,7 @@ static void ICACHE_FLASH_ATTR recvTask(os_event_t *events) WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR); } ETS_UART_INTR_ENABLE(); -} +}*/ void ds_init() { @@ -89,13 +163,37 @@ void ds_init() } +static ETSTimer delayTimer; + + +static void ICACHE_FLASH_ATTR xProcess(void *arg) +{ + char buf[64]; + int n = count_chars(), i, j; + if(n > 0) { + os_printf("n=%d\n", n); + if(n > sizeof(buf)) { + n = sizeof(buf); + } + for (i = 0; i < MAX_CONN; ++i) { + if (connData[i].conn) { + for(j=0; jbaud, BIT_RATE_115200); - uart_init(BIT_RATE_115200, BIT_RATE_115200); + uart_init(flash_param->baud, BIT_RATE_115200); +// uart_init(BIT_RATE_115200, BIT_RATE_115200); os_delay_us(1000); os_printf("Serial baud rate: %d\n", flash_param->baud); ds_init(); @@ -112,4 +210,7 @@ void user_init(void) serverInit(flash_param->port); system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen); + os_timer_disarm(&delayTimer); + os_timer_setfn(&delayTimer, xProcess, NULL); + os_timer_arm(&delayTimer, 100, 1); }