libpynq (release 5EWC0-2023 version 0.2.6 of 2023-10-24 17:28)
Loading...
Searching...
No Matches
UART library

Enumerations

enum  uart_index_t { UART0 = 0 , UART1 = 1 , NUM_UARTS }
 

Functions

void uart_init (const int uart)
 
void uart_destroy (const int uart)
 
void uart_send (const int uart, const uint8_t data)
 
uint8_t uart_recv (const int uart)
 
bool uart_has_data (const int uart)
 
bool uart_has_space (const int uart)
 
void uart_reset_fifos (const int uart)
 

Detailed Description

Functions to use the Universal Asynchronous Receiver-Transmitter (UART).

Two UART channels can be instantiated, UART0 and UART1. Before sending and receiving bytes the UART must be connect to some I/O pins through the switchbox, e.g.

@ IO_AR1
Definition pinmap.h:50
@ IO_AR0
Analog reference pins (Arduino header).
Definition pinmap.h:49
void switchbox_set_pin(const io_t pin_number, const io_configuration_t io_type)
Set the type of a switch pin.
Definition switchbox.c:126
@ SWB_UART0_TX
Definition switchbox.h:68
@ SWB_UART0_RX
Definition switchbox.h:70

After that, an example of how to use this library for the MASTER.

#include <libpynq.h>
int main (void)
{
// initialise all I/O
// initialize UART 0
// flush FIFOs of UART 0
uint8_t byte[] = "Hello\n";
int i = 0;
while (byte[i] != '\0') {
uart_send (UART0, byte[i]);
printf("sent byte %d\n", byte[i]);
i++;
}
// clean up after use
return EXIT_SUCCESS;
}
void uart_reset_fifos(const int uart)
This function resets both the transmit and receive FIFOs of the UART specified by the uart parameter....
Definition uart.c:121
void uart_send(const int uart, const uint8_t data)
Send a byte of data on the specified UART index by waiting for the transmit FIFO to have space and th...
Definition uart.c:72
void uart_init(const int uart)
Initialize the UART specified by the index with a shared memory handle and a buffer size of 4096 byte...
Definition uart.c:48
@ UART0
Definition uart.h:107
void pynq_init(void)
Initialise the switchbox and GPIO of the PYNQ.
Definition libpynq.c:24
void pynq_destroy(void)
Reset and destroy the switchbox and GPIO of the PYNQ.
Definition libpynq.c:35

An example of how to use this library for the SLAVE.

#include <libpynq.h>
int main (void)
{
// initialise all I/O
// initialize UART channel 0
// flush FIFOs of UART 0
printf("listening\n");
do {
// get a byte from UART 0
uint8_t msg = uart_recv(UART0);
printf("received byte %d\n", msg);
} while (1);
// clean up after use
return EXIT_SUCCESS;
}
uint8_t uart_recv(const int uart)
Receive a byte of data from the specified UART index by waiting for the receive FIFO to have data and...
Definition uart.c:85

UARTs can be routed through the switch box (see switchbox.h). Note that switchbox numbering (SWB_UART0..SWB_UART1) is then used instead of 0..NUM_UARTS-1 (UART0..UART1).

Enumeration Type Documentation

◆ uart_index_t

Enum of UARTs. Functions use a switch numbered from 0..NUM_UARTS-1. Alternatively, you can use UARTi instead of just i if you find that clearer.

Enumerator
UART0 
UART1 
NUM_UARTS 

Definition at line 107 of file uart.h.

Function Documentation

◆ uart_destroy()

void uart_destroy ( const int  uart)
extern

Close the shared memory handle for the specified UART index.

Parameters
uartThe UART index to remove from the shared memory space.
Warning
Fails with program exit if the UART channel is outside valid range.

Definition at line 61 of file uart.c.

Here is the call graph for this function:

◆ uart_has_data()

bool uart_has_data ( const int  uart)
extern

Check if the receive FIFO for the specified UART index has data available.

Parameters
uartThe UART index used to check for data.
Returns
True if the receive FIFO has data, false otherwise.
Warning
Fails with program exit if the UART channel is outside valid range.

Definition at line 98 of file uart.c.

◆ uart_has_space()

bool uart_has_space ( const int  uart)
extern

Check if the transmit FIFO for the specified UART index has space available.

Parameters
uartThe UART index to check for space.
Returns
True if the FIFO has space, false otherwise.
Warning
Fails with program exit if the UART channel is outside valid range.

Definition at line 110 of file uart.c.

◆ uart_init()

void uart_init ( const int  uart)
extern

Initialize the UART specified by the index with a shared memory handle and a buffer size of 4096 bytes.

Parameters
uartThe UART index to initialize.
Warning
Fails with program exit if the UART channel is outside valid range or when the shared memory system has not been instantiated.

Definition at line 48 of file uart.c.

Here is the call graph for this function:

◆ uart_recv()

uint8_t uart_recv ( const int  uart)
extern

Receive a byte of data from the specified UART index by waiting for the receive FIFO to have data and then reading the data from the receive buffer.

Parameters
uartThe UART index to receive data from.
Returns
The received data byte.
Warning
Fails with program exit if the UART channel is outside valid range.

Definition at line 85 of file uart.c.

◆ uart_reset_fifos()

void uart_reset_fifos ( const int  uart)
extern

This function resets both the transmit and receive FIFOs of the UART specified by the uart parameter. This can be useful when there is data stuck in the FIFOs or when the FIFOs are not behaving as expected.

Parameters
uartThe UART index of the UART whose FIFOs should be reset.
Warning
This function is specific to UARTs that have FIFOs, and will have no effect on UARTs that do not have FIFOs.
Resetting the FIFOs will result in the loss of any data that is currently in the FIFOs. Therefore, this function should be used with caution, and only when it is absolutely necessary to do so.
Fails with program exit if the UART channel is outside valid range.

Definition at line 121 of file uart.c.

◆ uart_send()

void uart_send ( const int  uart,
const uint8_t  data 
)
extern

Send a byte of data on the specified UART index by waiting for the transmit FIFO to have space and then writing the data to the transmit buffer.

Parameters
uartThe UART index to send data to.
dataThe data to send to the UART index.
Warning
Fails with program exit if the UART channel is outside valid range.

Definition at line 72 of file uart.c.