int uart_read(char *c, int rsize) { struct uart_token token; token.cur_bytes = 0; token.req_bytes = rsize; token.buf = c; if(ps_cdev_read(&serial_device, token.buf, token.req_bytes, &read_callback, &token) < 0){ printf("Error reading from UART\n"); return -1; } read_sem_wait(); return token.cur_bytes; }
static void read_callback(ps_chardevice_t* device, enum chardev_status stat, size_t bytes_transfered, void* token){ struct uart_token* t; t = (struct uart_token*)token; /* We might get a short read due to a timeout. */ t->cur_bytes += bytes_transfered; t->buf += bytes_transfered; if(t->cur_bytes < t->req_bytes){ int ret; ret = ps_cdev_read(device, t->buf, t->req_bytes - t->cur_bytes, &read_callback, token); if(ret < 0){ printf("Error reading from UART\n"); read_sem_post(); } }else{ read_sem_post(); } }