libpynq (release 5EWC0-2023 version 0.1.0 of 2023-08-14 14:01)
Loading...
Searching...
No Matches
uart.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22#include "uart.h"
24#include "log.h"
25#include <platform.h>
26#include <stdio.h>
27
28static arm_shared uart0_handle, uart1_handle;
29static volatile uint32_t *uart0 = NULL;
30static volatile uint32_t *uart1 = NULL;
31
32void uart_init(const int uart) {
33 if (uart == UART0) {
34 uart0 = arm_shared_init(&uart0_handle, axi_uartlite_0, 4096);
35 } else if (uart == UART1) {
36 uart1 = arm_shared_init(&uart1_handle, axi_uartlite_1, 4096);
37 } else {
38 pynq_error("uart_init: invalid UART %d, must be 0..%d\n", uart, NUM_UARTS);
39 }
40}
41
42void uart_destroy(const int uart) {
43 if (uart == UART0) {
44 if (uart0 == NULL) {
45 pynq_error("uart_destroy: shared memory has not been instantiated\n");
46 }
47 arm_shared_close(&uart0_handle);
48 } else if (uart == UART1) {
49 if (uart1 == NULL) {
50 pynq_error("uart_destroy: shared memory has not been instantiated\n");
51 }
52 arm_shared_close(&uart1_handle);
53 } else {
54 pynq_error("uart_destroy: channel %d does not exist\n", uart);
55 }
56}
57
58void uart_send(const int uart, const uint8_t data) {
59 if (uart == UART0) {
60 while ((uart0[2] & 8) == 8)
61 ;
62 uart0[1] = data;
63 } else if (uart == UART1) {
64 while ((uart1[2] & 8) == 8)
65 ;
66 uart1[1] = data;
67 } else {
68 pynq_error("uart_send: invalid UART %d, must be 0..%d\n", uart, NUM_UARTS);
69 }
70}
71
72uint8_t uart_recv(const int uart) {
73 if (uart == UART0) {
74 while ((uart0[2] & 1) == 0) {
75 }
76 return uart0[0];
77 } else if (uart == UART1) {
78 while ((uart1[2] & 1) == 0) {
79 }
80 return uart1[0];
81 } else {
82 pynq_error("uart_recv: invalid UART %d, must be 0..%d\n", uart, NUM_UARTS);
83 }
84 return 0;
85}
86
87bool uart_has_data(const int uart) {
88 if (uart == UART0) {
89 return ((uart0[2] & 1) != 0);
90 } else if (uart == UART1) {
91 return ((uart1[2] & 1) != 0);
92 } else {
93 pynq_error("uart_has_data: invalid UART %d, must be 0..%d\n", uart,
94 NUM_UARTS);
95 }
96 return false;
97}
98
99bool uart_has_space(const int uart) {
100 if (uart == UART0) {
101 return ((uart0[2] & 8) == 0);
102 } else if (uart == UART1) {
103 return ((uart1[2] & 8) == 0);
104 } else {
105 pynq_error("uart_has_space: invalid UART %d, must be 0..%d\n", uart,
106 NUM_UARTS);
107 }
108 return false;
109}
110
111void uart_reset_fifos(const int uart) {
112 if (uart == UART0) {
113 uart0[3] = 3;
114 } else if (uart == UART1) {
115 uart1[3] = 3;
116 } else {
117 pynq_error("uart_reset_fifos: invalid UART %d, must be 0..%d\n", uart,
118 NUM_UARTS);
119 }
120}
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
#define pynq_error(...)
Definition log.h:118
void uart_destroy(const int uart)
Close the shared memory handle for the specified UART index.
Definition uart.c:42
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:111
bool uart_has_data(const int uart)
Check if the receive FIFO for the specified UART index has data available.
Definition uart.c:87
bool uart_has_space(const int uart)
Check if the transmit FIFO for the specified UART index has space available.
Definition uart.c:99
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:72
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:58
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:32
@ NUM_UARTS
Definition uart.h:100
@ UART0
Definition uart.h:100
@ UART1
Definition uart.h:100