Skip to content

Commit

Permalink
fix an issue with serial buffer and improve reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
nemik committed Jul 21, 2016
1 parent a55415f commit 12069c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ SHA := $(shell if git diff --quiet HEAD; then git rev-parse --short HEAD | c
else echo "development"; fi)
#VERSION ?=esp-link $(BRANCH) - $(DATE) - $(SHA)

VERSION ?=version 11
VERSION ?=version 10

This comment has been minimized.

Copy link
@jakkubu

jakkubu Jul 23, 2016

Shouldn't it be 12?


# Output directors to store intermediate compiled files
# relative to the project directory
Expand Down
29 changes: 5 additions & 24 deletions esp-link/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ uart_config(uint8 uart_no)
CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);

if (uart_no == UART0) {
// Configure RX interrupt conditions as follows: trigger rx-full when there are 127 characters
// Configure RX interrupt conditions as follows: trigger rx-full when there are 80 characters
// in the buffer, trigger rx-timeout when the fifo is non-empty and nothing further has been
// received for 4 character periods.
// Set the hardware flow-control to trigger when the FIFO holds 100 characters, although
Expand All @@ -83,7 +83,7 @@ uart_config(uint8 uart_no)
// We do not enable framing error interrupts 'cause they tend to cause an interrupt avalanche
// and instead just poll for them when we get a std RX interrupt.
WRITE_PERI_REG(UART_CONF1(uart_no),
((127 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
((80 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
((100 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
UART_RX_FLOW_EN |
(4 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
Expand Down Expand Up @@ -222,35 +222,16 @@ uart_recvTask(os_event_t *events)

// read a buffer-full from the uart
uint16 length = 0;
uint16 l = 0;
char buf[128];
char in_char;
bool did_start = false;
memset(buf, 0, 128);
while ((READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) &&
(length < 128)) {
in_char = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
length++;
//look for beginning | char from BLE peripheral
if(in_char == '|')
{
did_start = true;
}
if(did_start)
{
buf[l] = in_char;
if(buf[l] == '\n' || buf[l] == '\r')
{
buf[l] = '\0';
break;
}
l++;
}
buf[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
}
//DBG_UART("%d ix %d\n", system_get_time(), length);

for (int i=0; i<MAX_CB; i++) {
//if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length);
if (uart_recv_cb[i] != NULL && l > 3) (uart_recv_cb[i])(buf, l);
if (uart_recv_cb[i] != NULL) (uart_recv_cb[i])(buf, length);
}
}
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
Expand Down
34 changes: 31 additions & 3 deletions user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ static char ble_is_scan_resp [2];
static char ble_type [2];
static char ble_data[100];

static char serial_buffer[200];

char beacon_uuid [33];
char beacon_major [5];
char beacon_minor [5];
Expand Down Expand Up @@ -165,11 +167,10 @@ void ICACHE_FLASH_ATTR hex2str(char *out, unsigned char *s, size_t len)
#define ibeacon_include_1 "0201"
#define ibeacon_include_2 "1aff4c0002"

// callback with a buffer of characters that have arrived on the uart
void ICACHE_FLASH_ATTR
bleBridgeUartCb(char *buf, short length)
process_serial(char *buf)
{
os_printf("********* got serial %d ** %s \n\r", length, buf);
os_printf("********* got serial to process %d ** %s \n\r", strlen(buf), buf);
char * pch;
pch = strtok(buf, "|");
int i = 0;
Expand Down Expand Up @@ -358,6 +359,33 @@ bleBridgeUartCb(char *buf, short length)
sendMQTTraw(ble_mac_addr, ble_rssi, ble_is_scan_resp, ble_type, ble_data);
}

// callback with a buffer of characters that have arrived on the uart
void ICACHE_FLASH_ATTR
bleBridgeUartCb(char *buf, short length)
{
//os_printf("********* got serial %d ** %s \n\r", length, buf);
// append to serial_buffer
strcat(serial_buffer, buf);

// check if buffer contains a \n or \r, if does, then is end of string and pass it for processing.
char * nl = NULL;
char * cr = NULL;
nl = strchr(serial_buffer, '\n');
cr = strchr(serial_buffer, '\r');
if(nl != NULL)
{
if(cr != NULL)
{
serial_buffer[cr-serial_buffer] = '\0';
}

// pass on for processing
process_serial(serial_buffer);
// clear the buffer
memset(serial_buffer, 0, 200);
}
}

//startup connect Timer event
static void ICACHE_FLASH_ATTR startup_connect_check(void *arg)
{
Expand Down

0 comments on commit 12069c1

Please sign in to comment.