int FGETC(FILE *f) { if ((stdout == f) || (stderr == f)) { return 0; } else if (stdin == f) { if (!appio_dummy) { uint32_t size; do { usb_protocol_poll(); size = vsf_fifo_get_data_length(&shell_stream.stream_tx.fifo); } while (!size); return vsf_fifo_pop8(&shell_stream.stream_tx.fifo); } } else { struct appio_file_t *file = appio_file_byfn(f); if (file != NULL) { return file->pos >= file->size ? EOF : file->addr[file->pos++]; } } return 0; }
uint32_t vsf_fifo_peek(struct vsf_fifo_t *fifo, uint32_t size, uint8_t *data) { uint32_t tmp32; uint32_t avail_len = vsf_fifo_get_data_length(fifo); #if __VSF_DEBUG__ if (NULL == fifo) { return 0; } #endif if (size > avail_len) { size = avail_len; } tmp32 = fifo->buffer.size - fifo->tail; if (size > tmp32) { memcpy(&data[0], &fifo->buffer.buffer[fifo->tail], tmp32); memcpy(&data[tmp32], &fifo->buffer.buffer[0], size - tmp32); } else { memcpy(data, &fifo->buffer.buffer[fifo->tail], size); } return size; }
int FFLUSH(FILE *f) { if ((stdout == f) || (stderr == f)) { if (!appio_dummy) { app_io_out_sync(); } return 0; } else if (stdin == f) { if (!appio_dummy) { uint32_t i, size = vsf_fifo_get_data_length(&shell_stream.stream_tx.fifo); for (i = 0; i < size; i++) { vsf_fifo_pop8(&shell_stream.stream_tx.fifo); } } return 0; } else { struct appio_file_t *file = appio_file_byfn(f); if (file != NULL) { // TODO: flush appio_file } } return 0; }
int32_t comm_flush_usbtocomm(void) { if (!usbtocomm_open) { return -1; } while (vsf_fifo_get_data_length(&usart_stream_p0.stream_tx.fifo) > 0) { usart_stream_poll(&usart_stream_p0); } while (vsf_fifo_get_data_length(&usart_stream_p0.stream_rx.fifo) > 0) { vsf_fifo_pop8(&usart_stream_p0.stream_rx.fifo); usart_stream_poll(&usart_stream_p0); } return 0; }
static void app_io_out_sync(void) { int free_space; do { usb_protocol_poll(); free_space = vsf_fifo_get_data_length(&shell_stream.stream_rx.fifo); } while (free_space); }
uint8_t vsf_fifo_pop8(struct vsf_fifo_t *fifo) { uint8_t data; if (vsf_fifo_get_data_length(fifo) <= 0) { return 0; } data = fifo->buffer.buffer[fifo->tail]; fifo->tail = vsf_fifo_get_next_index(fifo->tail, fifo->buffer.size); return data; }
vsf_err_t usart_status(uint8_t index, struct usart_status_t *status) { struct vsf_fifo_t *fifo_tx, *fifo_rx; switch (index) { case 0: if (NULL == status) { return VSFERR_INVALID_PTR; } fifo_tx = &usart_stream_p0.stream_tx.fifo; fifo_rx = &usart_stream_p0.stream_rx.fifo; status->tx_buff_avail = vsf_fifo_get_avail_length(fifo_tx); status->tx_buff_size = vsf_fifo_get_data_length(fifo_tx); status->rx_buff_avail = vsf_fifo_get_avail_length(fifo_rx); status->rx_buff_size = vsf_fifo_get_data_length(fifo_rx); return VSFERR_NONE; default: return VSFERR_NOT_SUPPORT; } }
uint32_t vsf_fifo_get_avail_length(struct vsf_fifo_t *fifo) { uint32_t len; #if __VSF_DEBUG__ if (NULL == fifo) { return 0; } #endif len = fifo->buffer.size - vsf_fifo_get_data_length(fifo); if (len > 0) { len--; } return len; }
int32_t comm_write_usbtocomm(uint8_t *buffer, uint32_t num_of_bytes) { struct vsf_buffer_t sbuffer; uint32_t start, end; int32_t data_write; if (!usbtocomm_open) { return -1; } data_write = 0; start = interfaces->tickclk.get_count(); while (data_write < num_of_bytes) { sbuffer.size = num_of_bytes - data_write; sbuffer.buffer = buffer + data_write; usart_stream_poll(&usart_stream_p0); sbuffer.size = usart_stream_tx(&usart_stream_p0, &sbuffer); end = interfaces->tickclk.get_count(); if (sbuffer.size) { data_write += sbuffer.size; start = end; } else if ((end - start) > 3000) { break; } } while (vsf_fifo_get_data_length(&usart_stream_p0.stream_tx.fifo) > 0) { usart_stream_poll(&usart_stream_p0); } return data_write; }
static uint32_t fifo_stream_get_data_length(struct vsf_stream_t *stream) { struct vsf_fifostream_t *fifostream = (struct vsf_fifostream_t *)stream; return vsf_fifo_get_data_length(&fifostream->mem); }
uint32_t stream_get_data_size(struct vsf_stream_t *stream) { return vsf_fifo_get_data_length(&stream->fifo); }
char* FGETS(char *buf, int count, FILE *f) { char cur_char, *result = buf; int size = 0, cur_size, pos; if ((NULL == buf) || (NULL == f) || (stdout == f) || (stderr == f)) { return NULL; } if (stdin == f) { if (!appio_dummy) { #if HW_HAS_LCM char vsprog_ui_buf[2]; #endif pos = 0; cur_char = '\0'; while ((size < count) && (cur_char != '\r')) { usb_protocol_poll(); cur_size = vsf_fifo_get_data_length(&shell_stream.stream_tx.fifo); while (cur_size && (size < count) && (cur_char != '\r')) { cur_char = (char)vsf_fifo_pop8(&shell_stream.stream_tx.fifo); if ('\r' == cur_char) { vsf_fifo_push8(&shell_stream.stream_rx.fifo, '\n'); #if HW_HAS_LCM vsprog_ui_print("\n\0"); #endif } else if ('\b' == cur_char) { if (pos) { vsf_fifo_push8(&shell_stream.stream_rx.fifo, '\b'); vsf_fifo_push8(&shell_stream.stream_rx.fifo, ' '); vsf_fifo_push8(&shell_stream.stream_rx.fifo, '\b'); #if HW_HAS_LCM vsprog_ui_print("\b \b\0"); #endif pos--; } cur_size--; continue; } else if (!((cur_char >= ' ') && (cur_char <= '~'))) { cur_size--; continue; } vsf_fifo_push8(&shell_stream.stream_rx.fifo, (uint8_t)cur_char); #if HW_HAS_LCM vsprog_ui_buf[0] = cur_char; vsprog_ui_buf[1] = '\0'; vsprog_ui_print(vsprog_ui_buf); #endif buf[pos++] = cur_char; size++; cur_size--; } } buf[pos] = '\0'; app_io_out_sync(); } else { return NULL; } } else { struct appio_file_t *file = appio_file_byfn(f); if (file != NULL) { if (count < 3) { return NULL; } count -= 3; while ((file->addr[file->pos] != '\0') && (file->addr[file->pos] != 0xFF) && (file->pos < file->size) && ((file->addr[file->pos] == '\n') || (file->addr[file->pos] == '\r'))) { file->pos++; } while (count-- && (file->pos < file->size) && (file->addr[file->pos] != '\0') && (file->addr[file->pos] != '\n') && (file->addr[file->pos] != '\r') && (file->addr[file->pos] != 0xFF)) { *buf++ = file->addr[file->pos++]; } if (result == buf) { return NULL; } *buf++ = '\n'; *buf++ = '\r'; *buf++ = '\0'; } } return result; }