static void uart_putxmitchar(FAR uart_dev_t *dev, int ch) { irqstate_t flags; int nexthead; /* Increment to see what the next head pointer will be. We need to use the "next" * head pointer to determine when the circular buffer would overrun */ nexthead = dev->xmit.head + 1; if (nexthead >= dev->xmit.size) { nexthead = 0; } /* Loop until we are able to add the character to the TX buffer */ for (;;) { if (nexthead != dev->xmit.tail) { dev->xmit.buffer[dev->xmit.head] = ch; dev->xmit.head = nexthead; return; } else { /* Inform the interrupt level logic that we are waiting. * This and the following steps must be atomic. */ flags = irqsave(); dev->xmitwaiting = true; /* Wait for some characters to be sent from the buffer * with the TX interrupt enabled. When the TX interrupt * is enabled, uart_xmitchars should execute and remove * some of the data from the TX buffer. */ uart_enabletxint(dev); uart_takesem(&dev->xmitsem); uart_disabletxint(dev); irqrestore(flags); } } }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t ret = buflen; /* We may receive console writes through this path from * interrupt handlers and from debug output in the IDLE task! * In these cases, we will need to do things a little * differently. */ if (up_interrupt_context() || getpid() == 0) { if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return ERROR; } } /* Only one user can be accessing dev->xmit.head at once */ uart_takesem(&dev->xmit.sem); /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { int ch = *buffer++; /* Put the character into the transmit buffer */ uart_putxmitchar(dev, ch); /* If this is the console, then we should replace LF with LF-CR */ if (dev->isconsole && ch == '\n') { uart_putxmitchar(dev, '\r'); } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); return ret; }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t nread = buflen; int ret; /* We may receive console writes through this path from interrupt handlers and * from debug output in the IDLE task! In these cases, we will need to do things * a little differently. */ if (up_interrupt_context() || getpid() == 0) { /* up_putc() will be used to generate the output in a busy-wait loop. * up_putc() is only available for the console device. */ if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return -EPERM; } } /* Only one user can access dev->xmit.head at a time */ ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will * abort the transfer. After the transfer has started, we are committed * and signals will be ignored. */ return ret; } /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { int ch = *buffer++; /* If the ONLCR flag is set, we should translate \n to \r\n */ ret = OK; if ((ch == '\n') && (dev->termios_s.c_oflag && ONLCR)) { ret = uart_putxmitchar(dev, '\r'); } /* Put the character into the transmit buffer */ if (ret == OK) { ret = uart_putxmitchar(dev, ch); } /* Were we awakened by a signal? That should be the only condition that * uart_putxmitchar() should return an error. */ if (ret < 0) { /* POSIX requires that we return -1 and errno set if no data was * transferred. Otherwise, we return the number of bytes in the * interrupted transfer. */ if (buflen < nread) { /* Some data was transferred. Return the number of bytes that were * successfully transferred. */ nread -= buflen; } else { /* No data was transferred. Return -EINTR. The VFS layer will * set the errno value appropriately). */ nread = -EINTR; } break; } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); return nread; }
static int uart_putxmitchar(FAR uart_dev_t *dev, int ch) { irqstate_t flags; int nexthead; int ret; /* Increment to see what the next head pointer will be. We need to use the "next" * head pointer to determine when the circular buffer would overrun */ nexthead = dev->xmit.head + 1; if (nexthead >= dev->xmit.size) { nexthead = 0; } /* Loop until we are able to add the character to the TX buffer */ for (;;) { if (nexthead != dev->xmit.tail) { dev->xmit.buffer[dev->xmit.head] = ch; dev->xmit.head = nexthead; return OK; } else { /* Inform the interrupt level logic that we are waiting. This and * the following steps must be atomic. */ flags = irqsave(); dev->xmitwaiting = true; /* Wait for some characters to be sent from the buffer with the TX * interrupt enabled. When the TX interrupt is enabled, uart_xmitchars * should execute and remove some of the data from the TX buffer. */ uart_enabletxint(dev); ret = uart_takesem(&dev->xmitsem, true); uart_disabletxint(dev); irqrestore(flags); /* Check if we were awakened by signal. */ if (ret < 0) { /* A signal received while waiting for the xmit buffer to become * non-full will abort the transfer. */ return -EINTR; } } } /* We won't get here */ return OK; }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t nwritten = buflen; bool oktoblock; int ret; char ch; /* We may receive console writes through this path from interrupt handlers and * from debug output in the IDLE task! In these cases, we will need to do things * a little differently. */ if (up_interrupt_context() || getpid() == 0) { #ifdef CONFIG_SERIAL_REMOVABLE /* If the removable device is no longer connected, refuse to write to * the device. */ if (dev->disconnected) { return -ENOTCONN; } #endif /* up_putc() will be used to generate the output in a busy-wait loop. * up_putc() is only available for the console device. */ if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return -EPERM; } } /* Only one user can access dev->xmit.head at a time */ ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will * abort the transfer. After the transfer has started, we are committed * and signals will be ignored. */ return ret; } #ifdef CONFIG_SERIAL_REMOVABLE /* If the removable device is no longer connected, refuse to write to the * device. This check occurs after taking the xmit.sem because the * disconnection event might have occurred while we were waiting for * access to the transmit buffers. */ if (dev->disconnected) { uart_givesem(&dev->xmit.sem); return -ENOTCONN; } #endif /* Can the following loop block, waiting for space in the TX * buffer? */ oktoblock = ((filep->f_oflags & O_NONBLOCK) == 0); /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { ch = *buffer++; ret = OK; #ifdef CONFIG_SERIAL_TERMIOS /* Do output post-processing */ if (dev->tc_oflag & OPOST) { /* Mapping CR to NL? */ if ((ch == '\r') && (dev->tc_oflag & OCRNL)) { ch = '\n'; } /* Are we interested in newline processing? */ if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET))) { ret = uart_putxmitchar(dev, '\r', oktoblock); if (ret < 0) { break; } } /* Specifically not handled: * * OXTABS - primarily a full-screen terminal optimisation * ONOEOT - Unix interoperability hack * OLCUC - Not specified by Posix * ONOCR - low-speed interactive optimisation */ } #else /* !CONFIG_SERIAL_TERMIOS */ /* If this is the console, convert \n -> \r\n */ if (dev->isconsole && ch == '\n') { ret = uart_putxmitchar(dev, '\r', oktoblock); } #endif /* Put the character into the transmit buffer */ if (ret == OK) { ret = uart_putxmitchar(dev, ch, oktoblock); } /* uart_putxmitchar() might return an error under one of two * conditions: (1) The wait for buffer space might have been * interrupted by a signal (ret should be -EINTR), (2) if * CONFIG_SERIAL_REMOVABLE is defined, then uart_putxmitchar() * might also return if the serial device was disconnected * (with -ENOTCONN), or (3) if O_NONBLOCK is specified, then * then uart_putxmitchar() might return -EAGAIN if the output * TX buffer is full. */ if (ret < 0) { /* POSIX requires that we return -1 and errno set if no data was * transferred. Otherwise, we return the number of bytes in the * interrupted transfer. */ if (buflen < nwritten) { /* Some data was transferred. Return the number of bytes that * were successfully transferred. */ nwritten -= buflen; } else { /* No data was transferred. Return the negated errno value. * The VFS layer will set the errno value appropriately). */ nwritten = ret; } break; } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); return nwritten; }
static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock) { irqstate_t flags; int nexthead; int ret; /* Increment to see what the next head pointer will be. We need to use the "next" * head pointer to determine when the circular buffer would overrun */ nexthead = dev->xmit.head + 1; if (nexthead >= dev->xmit.size) { nexthead = 0; } /* Loop until we are able to add the character to the TX buffer */ for (;;) { if (nexthead != dev->xmit.tail) { dev->xmit.buffer[dev->xmit.head] = ch; dev->xmit.head = nexthead; return OK; } /* The buffer is full and no data is available now. Should be block, * waiting for the hardware to remove some data from the TX * buffer? */ else if (oktoblock) { /* Inform the interrupt level logic that we are waiting. This and * the following steps must be atomic. */ flags = irqsave(); #ifdef CONFIG_SERIAL_REMOVABLE /* Check if the removable device is no longer connected while we * have interrupts off. We do not want the transition to occur * as a race condition before we begin the wait. */ if (dev->disconnected) { ret = -ENOTCONN; } else #endif { /* Wait for some characters to be sent from the buffer with * the TX interrupt enabled. When the TX interrupt is * enabled, uart_xmitchars should execute and remove some * of the data from the TX buffer. */ dev->xmitwaiting = true; uart_enabletxint(dev); ret = uart_takesem(&dev->xmitsem, true); uart_disabletxint(dev); } irqrestore(flags); #ifdef CONFIG_SERIAL_REMOVABLE /* Check if the removable device was disconnected while we were * waiting. */ if (dev->disconnected) { return -ENOTCONN; } #endif /* Check if we were awakened by signal. */ if (ret < 0) { /* A signal received while waiting for the xmit buffer to become * non-full will abort the transfer. */ return -EINTR; } } /* The caller has request that we not block for data. So return the * EAGAIN error to signal this situation. */ else { return -EAGAIN; } } /* We won't get here. Some compilers may complain that this code is * unreachable. */ return OK; }
static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, size_t buflen) { FAR struct inode *inode = filep->f_inode; FAR uart_dev_t *dev = inode->i_private; ssize_t nread = buflen; int ret; char ch; /* We may receive console writes through this path from interrupt handlers and * from debug output in the IDLE task! In these cases, we will need to do things * a little differently. */ if (up_interrupt_context() || getpid() == 0) { /* up_putc() will be used to generate the output in a busy-wait loop. * up_putc() is only available for the console device. */ if (dev->isconsole) { irqstate_t flags = irqsave(); ret = uart_irqwrite(dev, buffer, buflen); irqrestore(flags); return ret; } else { return -EPERM; } } /* Only one user can access dev->xmit.head at a time */ ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will * abort the transfer. */ return ret; } /* Loop while we still have data to copy to the transmit buffer. * we add data to the head of the buffer; uart_xmitchars takes the * data from the end of the buffer. */ uart_disabletxint(dev); for (; buflen; buflen--) { ch = *buffer++; /* Do output post-processing */ #ifdef CONFIG_SERIAL_TERMIOS if (dev->tc_oflag & OPOST) { /* Mapping CR to NL? */ if ((ch == '\r') && (dev->tc_oflag & OCRNL)) { ch = '\n'; } /* Are we interested in newline processing? */ if ((ch == '\n') && (dev->tc_oflag & (ONLCR | ONLRET))) { ret = uart_putxmitchar(dev, '\r'); if (ret != OK) { break; } } /* Specifically not handled: * * OXTABS - primarily a full-screen terminal optimisation * ONOEOT - Unix interoperability hack * OLCUC - Not specified by Posix * ONOCR - low-speed interactive optimisation */ } #else /* !CONFIG_SERIAL_TERMIOS */ /* If this is the console, convert \n -> \r\n */ if (dev->isconsole && ch == '\n') { ret = uart_putxmitchar(dev, '\r'); } #endif /* Put the character into the transmit buffer */ ret = uart_putxmitchar(dev, ch); if (ret != OK) { break; } } if (dev->xmit.head != dev->xmit.tail) { uart_enabletxint(dev); } uart_givesem(&dev->xmit.sem); /* Were we interrupted by a signal? That should be the only condition that * uart_putxmitchar() should return an error. */ if (ret < 0) { /* POSIX requires that we return -1 and errno set if no data was * transferred. Otherwise, we return the number of bytes in the * interrupted transfer. */ if (buflen < nread) { /* Some data was transferred. Return the number of bytes that were * successfully transferred. */ nread -= buflen; } else { /* No data was transferred. Return -EINTR. The VFS layer will * set the errno value appropriately). */ nread = -EINTR; } } return nread; }