Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions drivers/serial/arm/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!(uart_regs->fr & PL011_FR_TXFF) && !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!(uart_regs->fr & PL011_FR_TXFF) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
uart_regs->dr = (uint32_t)c;
transferred = true;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ static void rx_return(void)
while (reprocess) {
while (!(uart_regs->fr & PL011_FR_RXFE) && !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = (char)(uart_regs->dr & PL011_DR_DATA_MASK);
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
serial_enqueue(&rx_queue_handle, NULL, c);
enqueued = true;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ static void uart_setup(void)
uart_regs->lcr_h |= PL011_LCR_PARTY_EN;

/* Enable receive interrupts when FIFO level exceeds 1/8 or after 32 ticks */
#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
uart_regs->ifls &= ~(PL011_IFLS_RX_MASK << PL011_IFLS_RX_SHFT);
uart_regs->imsc |= (PL011_IMSC_RX_TIMEOUT | PL011_IMSC_RX_INT);
#endif
Expand All @@ -165,7 +165,7 @@ static void uart_setup(void)
uart_regs->tcr |= PL011_CR_TX_EN;

/* Enable receive */
#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
uart_regs->tcr |= PL011_CR_RX_EN;
#endif
}
Expand All @@ -174,7 +174,7 @@ void init(void)
{
uart_setup();

#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
serial_queue_init(&rx_queue_handle, rx_queue, SERIAL_RX_DATA_REGION_CAPACITY_DRIV, rx_data);
#endif
serial_queue_init(&tx_queue_handle, tx_queue, SERIAL_TX_DATA_REGION_CAPACITY_DRIV, tx_data);
Expand Down
13 changes: 6 additions & 7 deletions drivers/serial/imx/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!(uart_regs->ts & UART_TST_TX_FIFO_FULL)
&& !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!(uart_regs->ts & UART_TST_TX_FIFO_FULL) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
uart_regs->txd = (uint32_t)c;
transferred = true;
}
Expand Down Expand Up @@ -87,7 +86,7 @@ static void rx_return(void)
while (reprocess) {
while (!(uart_regs->ts & UART_TST_RX_FIFO_EMPTY) && !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = (char) uart_regs->rxd;
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
serial_enqueue(&rx_queue_handle, NULL, c);
enqueued = true;
}

Expand Down Expand Up @@ -141,7 +140,7 @@ static void uart_setup(void)

/* Enable transmit and receive */
uart_regs->cr2 |= UART_CR2_TX_EN;
#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
uart_regs->cr2 |= UART_CR2_RX_EN;
#endif

Expand All @@ -161,7 +160,7 @@ static void uart_setup(void)

uint32_t fcr = uart_regs->fcr;
/* Enable receive interrupts every byte */
#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
fcr &= ~UART_FCR_RXTL_MASK;
fcr |= (1 << UART_FCR_RXTL_SHFT);
#endif
Expand All @@ -171,7 +170,7 @@ static void uart_setup(void)
fcr |= (2 << UART_FCR_TXTL_SHFT);

uart_regs->fcr = fcr;
#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
uart_regs->cr1 |= UART_CR1_RX_READY_INT;
#endif
}
Expand All @@ -180,7 +179,7 @@ void init(void)
{
uart_setup();

#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
serial_queue_init(&rx_queue_handle, rx_queue, SERIAL_RX_DATA_REGION_CAPACITY_DRIV, rx_data);
#endif
serial_queue_init(&tx_queue_handle, tx_queue, SERIAL_TX_DATA_REGION_CAPACITY_DRIV, tx_data);
Expand Down
8 changes: 4 additions & 4 deletions drivers/serial/meson/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!(uart_regs->sr & AML_UART_TX_FULL) && !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!(uart_regs->sr & AML_UART_TX_FULL) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
uart_regs->wfifo = (uint32_t)c;
transferred = true;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ static void rx_return(void)
while (reprocess) {
while (!(uart_regs->sr & AML_UART_RX_EMPTY) && !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = (char) uart_regs->rfifo;
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
serial_enqueue(&rx_queue_handle, NULL, c);
enqueued = true;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ static void uart_setup(void)

uint32_t irqc = uart_regs->irqc;
/* Enable receive interrupts every byte */
#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
irqc &= ~AML_UART_RECV_IRQ_MASK;
irqc |= AML_UART_RECV_IRQ(1);
cr |= (AML_UART_RX_INT_EN | AML_UART_RX_EN);
Expand All @@ -205,7 +205,7 @@ void init(void)
{
uart_setup();

#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
serial_queue_init(&rx_queue_handle, rx_queue, SERIAL_RX_DATA_REGION_CAPACITY_DRIV, rx_data);
#endif
serial_queue_init(&tx_queue_handle, tx_queue, SERIAL_TX_DATA_REGION_CAPACITY_DRIV, tx_data);
Expand Down
7 changes: 3 additions & 4 deletions drivers/serial/snps/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ static void tx_provide(void)
while (reprocess) {
char c;

while ((*REG_PTR(UART_LSR) & UART_LSR_THRE)
&& !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while ((*REG_PTR(UART_LSR) & UART_LSR_THRE) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
*REG_PTR(UART_THR) = c;
transferred = true;
}
Expand Down Expand Up @@ -95,7 +94,7 @@ static void rx_return(void)
while ((*REG_PTR(UART_LSR) & UART_LSR_DR)
&& !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = *REG_PTR(UART_RBR);
int err = serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
int err = serial_enqueue(&rx_queue_handle, NULL, c);
assert(!err);
enqueued = true;
}
Expand Down Expand Up @@ -169,7 +168,7 @@ void init(void)
/* Enable both Recieve Data Available and Transmit Holding Register Empty IRQs. */
*REG_PTR(UART_IER) = (UART_IER_ERBFI | UART_IER_ETBEI);

#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
serial_queue_init(&rx_queue_handle, rx_queue, SERIAL_RX_DATA_REGION_CAPACITY_DRIV, rx_data);
#endif
serial_queue_init(&tx_queue_handle, tx_queue, SERIAL_TX_DATA_REGION_CAPACITY_DRIV, tx_data);
Expand Down
9 changes: 4 additions & 5 deletions drivers/serial/virtio/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!virtio_avail_full_tx(&tx_virtq)
&& !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!virtio_avail_full_tx(&tx_virtq) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {

/* First, allocate somewhere to put the character */
uint32_t char_idx = -1;
Expand Down Expand Up @@ -250,7 +249,7 @@ static void rx_return(void)
assert(!(pkt.flags & VIRTQ_DESC_F_NEXT));

uint32_t char_idx = addr - virtio_rx_char_paddr;
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, virtio_rx_char[char_idx]);
serial_enqueue(&rx_queue_handle, NULL, virtio_rx_char[char_idx]);

/* Free the packet descriptor */
int err = ialloc_free(&rx_ialloc_desc, pkt_used.id);
Expand Down Expand Up @@ -417,7 +416,7 @@ void init()

console_setup();

#if !SERIAL_TX_ONLY
#if SERIAL_NUM_RX_CLIENTS > 0
serial_queue_init(&rx_queue_handle, rx_queue, SERIAL_RX_DATA_REGION_CAPACITY_DRIV, rx_data);
#endif
serial_queue_init(&tx_queue_handle, tx_queue, SERIAL_TX_DATA_REGION_CAPACITY_DRIV, tx_data);
Expand All @@ -442,4 +441,4 @@ void notified(microkit_channel ch)
LOG_DRIVER_ERR("received notification on unexpected channel: %u\n", ch);
break;
}
}
}
14 changes: 4 additions & 10 deletions examples/echo_server/include/serial_config/serial_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@
#include <sddf/serial/queue.h>
#include <stdint.h>

/* Number of clients that can be connected to the serial server. */
#define SERIAL_NUM_CLIENTS 3
/* Number of clients capable of transmission. */
#define SERIAL_NUM_TX_CLIENTS 3

/* Only support transmission and not receive. */
#define SERIAL_TX_ONLY 1
/* Number of clients capable of reception. */
#define SERIAL_NUM_RX_CLIENTS 0

/* Associate a colour with each client's output. */
#define SERIAL_WITH_COLOUR 1

/* Control character to switch input stream - ctrl \. To input character input twice. */
#define SERIAL_SWITCH_CHAR 28

/* Control character to terminate client number input. */
#define SERIAL_TERMINATE_NUM '\r'

/* Default baud rate of the uart device */
#define UART_DEFAULT_BAUD 115200

Expand Down
13 changes: 7 additions & 6 deletions examples/serial/include/serial_config/serial_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
#include <sddf/serial/queue.h>
#include <stdint.h>

#define SERIAL_NUM_CLIENTS 2
/* Number of clients capable of transmission. */
#define SERIAL_NUM_TX_CLIENTS 2

/* Only support transmission and not receive. */
#define SERIAL_TX_ONLY 0
/* Number of clients capable of reception. */
#define SERIAL_NUM_RX_CLIENTS 2

/* Associate a colour with each client's output. */
#define SERIAL_WITH_COLOUR 1

/* Default baud rate of the uart device */
#define UART_DEFAULT_BAUD 115200

/* Control character to switch input stream - ctrl \. To input character input twice. */
#define SERIAL_SWITCH_CHAR 28

/* Control character to terminate client number input. */
#define SERIAL_TERMINATE_NUM '\r'

/* Default baud rate of the uart device */
#define UART_DEFAULT_BAUD 115200

/* String to be printed to start console input */
#define SERIAL_CONSOLE_BEGIN_STRING "Begin input\n"
#define SERIAL_CONSOLE_BEGIN_STRING_LEN 12
Expand Down
2 changes: 1 addition & 1 deletion examples/serial/serial_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void notified(microkit_channel ch)
bool reprocess = true;
char c;
while (reprocess) {
while (!serial_dequeue(&rx_queue_handle, &rx_queue_handle.queue->head, &c)) {
while (!serial_dequeue(&rx_queue_handle, NULL, &c)) {
if (c == '\r') {
sddf_putchar_unbuffered('\\');
sddf_putchar_unbuffered('r');
Expand Down
18 changes: 12 additions & 6 deletions include/sddf/serial/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ static inline int serial_queue_full(serial_queue_handle_t *queue_handle, uint32_
* @param queue_handle queue to enqueue into.
* @param local_tail address of the tail to be incremented. This allows for clients to
* enqueue multiple characters before making the changes visible.
* Leave NULL to increment shared queue tail.
* @param character character to be enqueued.
*
* @return -1 when queue is empty, 0 on success.
*/
static inline int serial_enqueue(serial_queue_handle_t *queue_handle, uint32_t *local_tail,
char character)
{
if (serial_queue_full(queue_handle, *local_tail)) {
uint32_t *tail = (local_tail == NULL) ? &queue_handle->queue->tail : local_tail;

if (serial_queue_full(queue_handle, *tail)) {
return -1;
}

queue_handle->data_region[*local_tail % queue_handle->capacity] = character;
(*local_tail)++;
queue_handle->data_region[*tail % queue_handle->capacity] = character;
(*tail)++;

return 0;
}
Expand All @@ -85,19 +88,22 @@ static inline int serial_enqueue(serial_queue_handle_t *queue_handle, uint32_t *
* @param queue_handle queue to dequeue from.
* @param local_head address of the head to be incremented. This allows for clients to
* dequeue multiple characters before making the changes visible.
* Leave NULL to increment shared queue head.
* @param character character to copy into.
*
* @return -1 when queue is empty, 0 on success.
*/
static inline int serial_dequeue(serial_queue_handle_t *queue_handle, uint32_t *local_head,
char *character)
{
if (serial_queue_empty(queue_handle, *local_head)) {
uint32_t *head = (local_head == NULL) ? &queue_handle->queue->head : local_head;

if (serial_queue_empty(queue_handle, *head)) {
return -1;
}

*character = queue_handle->data_region[*local_head % queue_handle->capacity];
(*local_head)++;
*character = queue_handle->data_region[*head % queue_handle->capacity];
(*head)++;

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions serial/components/virt_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ char *rx_data_drv;
char *rx_data_cli0;

serial_queue_handle_t rx_queue_handle_drv;
serial_queue_handle_t rx_queue_handle_cli[SERIAL_NUM_CLIENTS];
serial_queue_handle_t rx_queue_handle_cli[SERIAL_NUM_RX_CLIENTS];

#define MAX_CLI_BASE_10 4
typedef enum mode {normal, switched, number} mode_t;
Expand All @@ -45,7 +45,7 @@ void rx_return(void)
uint32_t local_tail = rx_queue_handle_cli[current_client].queue->tail;
char c = '\0';
while (reprocess) {
while (!serial_dequeue(&rx_queue_handle_drv, &rx_queue_handle_drv.queue->head, &c)) {
while (!serial_dequeue(&rx_queue_handle_drv, NULL, &c)) {
switch (current_mode) {
case normal:
switch (c) {
Expand Down Expand Up @@ -78,7 +78,7 @@ void rx_return(void)
default:
if (c == SERIAL_TERMINATE_NUM) {
int input_number = sddf_atoi(next_client);
if (input_number >= 0 && input_number < SERIAL_NUM_CLIENTS) {
if (input_number >= 0 && input_number < SERIAL_NUM_RX_CLIENTS) {
if (transferred && serial_require_producer_signal(&rx_queue_handle_cli[current_client])) {
serial_update_visible_tail(&rx_queue_handle_cli[current_client], local_tail);
serial_cancel_producer_signal(&rx_queue_handle_cli[current_client]);
Expand Down
Loading
Loading