libpynq (release 5EWC0-2023 version 0.2.0 of 2023-08-28 20:33)
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.

#define SWB_AR0
Analog reference pins.
Definition pinmap.h:49
#define SWB_AR1
Definition pinmap.h:50
void switchbox_set_pin(const int32_t pin_number, const uint8_t pin_type)
Set the type of a switch pin.
Definition switchbox.c:4
@ SWB_UART0_TX
Definition switchbox.h:66
@ SWB_UART0_RX
Definition switchbox.h:68

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 pynq_init(void)
Initialise the switchbox and GPIO of the PYNQ.
Definition libpynq.c:2
void pynq_destroy(void)
Reset and destroy the switchbox and GPIO of the PYNQ.
Definition libpynq.c:3
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:8
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:4
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:2
@ UART0
Definition uart.h:107

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:5

UARTs can also be used through GPIO (see gpio.h and pinmap.h). Note that GPIO 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)

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 3 of file uart.c.

Here is the call graph for this function:

◆ uart_has_data()

bool uart_has_data ( const int  uart)

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 6 of file uart.c.

◆ uart_has_space()

bool uart_has_space ( const int  uart)

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 7 of file uart.c.

◆ uart_init()

void uart_init ( const int  uart)

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 2 of file uart.c.

Here is the call graph for this function:

◆ uart_recv()

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 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 5 of file uart.c.

◆ uart_reset_fifos()

void uart_reset_fifos ( const int  uart)

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 8 of file uart.c.

◆ uart_send()

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 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 4 of file uart.c.