size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size) { if(uart == NULL || !uart->rx_enabled) { return 0; } if(uart->rx_buffer->size == new_size) { return uart->rx_buffer->size; } uint8_t * new_buf = (uint8_t*)malloc(new_size); if(!new_buf) { return uart->rx_buffer->size; } size_t new_wpos = 0; ETS_UART_INTR_DISABLE(); while(uart_rx_available(uart) && new_wpos < new_size) { new_buf[new_wpos++] = uart_read_char(uart); } uint8_t * old_buf = uart->rx_buffer->buffer; uart->rx_buffer->rpos = 0; uart->rx_buffer->wpos = new_wpos; uart->rx_buffer->size = new_size; uart->rx_buffer->buffer = new_buf; free(old_buf); ETS_UART_INTR_ENABLE(); return uart->rx_buffer->size; }
/* serial1 can not rx data, just tx for debug */ irom uint8_t serial_read() { uart_t *_uart = uart0; if(!_uart || !uart_rx_enabled(_uart)) { return -1; } if (_peek_char != -1) { uint8_t tmp = _peek_char; _peek_char = -1; return tmp; } return uart_read_char(_uart); }
// Receives a single character. char USART_Receive_char(void) { // A nice hint: With interrupts, your microcontroller can inform you whenever // a character comes in. There is an interrupt called USART_RX_vect for that. // If such an interrupt would fill a buffer with the received data, this // function here could return you one character from this buffer. You would no // longer need to 'wait for the byte to arrive', but could just fetch it out // of this buffer at any later point. And of course you've got a buffer, // right? // If the buffer is actually empty, you could maybe return a 0 or so to // indicate that to the user? char ch; ch = uart_read_char(); rb_put_char(rx_buf, ch); return ch; }
int uart_read_string(char *s, int size) { uint8_t i = 0; char c; while(i < size - 1){ c = uart_read_char(); //#walk:f; if(c == '#'){ i = 0; } s[i++] = c; if(c == ';') break; } s[i] = '\0'; return i + 1; }
static uint8_t uart_recv_frame() { volatile uint8_t ch; uint8_t count = 0; char* _shell_cmd = NULL; if (!is_uart_data_ready()) { return FALSE; } memset(gwDevInstance.gw_buf, 0, sizeof(gwDevInstance.gw_buf)); while(1) { ch = uart_read_char(); if (ch == TIMEOUT) { printf("timeout, no avaliable data\n"); return FALSE; } gwDevInstance.gw_buf[count] = ch; if(ch == '\r') { ch = uart_read_char(); gwDevInstance.gw_buf[++count] = ch; if(ch == '\n') { if(count > 2) { gwDevInstance.gw_buf[++count] = ch; if(gwDevInstance.gw_buf[0] == 'A' && gwDevInstance.gw_buf[1] == 'T') { if(gwDevInstance.gw_buf[2] == '*') //this cmd is for node { gwDevInstance.gw_buf_len = count; wireless_send_frame(gwDevInstance.gw_buf, gwDevInstance.gw_buf_len); break; } else if(gwDevInstance.gw_buf[2] == '+' || gwDevInstance.gw_buf[2] == '\r') //this cmd is for GW { //here need parset AT Command... //clean shell commands _shell_cmd = AtCmdClean((char*)gwDevInstance.gw_buf, count); if( _shell_cmd != NULL ) { printf(">%s", _shell_cmd); AtParserCmd(_shell_cmd); // execute user command if(TRUE == AtCmdMainProc()) { break; } } } // '+' } //if(cmd_buffer[0] == 'A' && cmd_buffer[1] == 'T') } //we get the wrong command, so return erro directly at_server_error(); break; //return error } } count ++; } return TRUE; }
int HardwareSerial::read(void) { // this may return -1, but that's okay return uart_read_char(_uart); }