Skip to content

Commit

Permalink
do no skipping chars from uart
Browse files Browse the repository at this point in the history
  • Loading branch information
n800sau committed Feb 18, 2015
1 parent 1f5ac9e commit 72671bb
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 15 deletions.
18 changes: 18 additions & 0 deletions driver/uart.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion include/driver/uart.h
Expand Up @@ -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

15 changes: 12 additions & 3 deletions include/espmissingincludes.h
@@ -1,18 +1,19 @@
#ifndef ESPMISSINGINCLUIDES_H
#define ESPMISSINGINCLUIDES_H
#ifndef ESPMISSINGINCLUDES_H
#define ESPMISSINGINCLUDES_H

#include <ets_sys.h>
#include <stdint.h>

//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);
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)));
Expand All @@ -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
9 changes: 5 additions & 4 deletions user/server.c
Expand Up @@ -5,6 +5,7 @@
#include "mem.h"
#include "osapi.h"
#include <gpio.h>
#include <driver/uart.h>

#include "server.h"
#include "config.h"
Expand Down Expand Up @@ -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;
}
}
Expand Down
115 changes: 108 additions & 7 deletions user/user_main.c
Expand Up @@ -15,10 +15,13 @@
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "espmissingincludes.h"
#include <user_interface.h>
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"

#include "driver/uart.h"
#include "task.h"

Expand All @@ -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);
}
Expand All @@ -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()
{
Expand Down Expand Up @@ -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; j<n; j++) {
buf[j] = get_char();
os_printf("[%2X] ", buf[j]);
}
espconn_sent(connData[i].conn, (uint8_t*)buf, n);
os_printf("\nSent %d bytes\n", n);
}
}
}
}

void user_init(void)
{
// system_set_os_print(1);
flash_param_t *flash_param = flash_param_get();
// uart_init(flash_param->baud, 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();
Expand All @@ -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);
}

2 comments on commit 72671bb

@beckdac
Copy link

@beckdac beckdac commented on 72671bb May 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me more about this change vs. stock? How dos it perform?

@n800sau
Copy link
Owner Author

@n800sau n800sau commented on 72671bb May 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have noticed that the faster I type in serial terminal the more bytes are lost.
I guess it is because uart buffer gets full or does not exist at all.
So I added a buffer to store data coming from uart. I do not have that problem anymore.
Bigger buffer helps for slower tcp communication or bigger serial data rate.
The bigger size of the buffer the bigger baud rate can be used.

Please sign in to comment.