ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
neorv32_char_loopback.c
Go to the documentation of this file.
1// #################################################################################################
2// # << NEORV32 - Blinking LED Demo Program >> # #
3// *********************************************************************************************
4// # # BSD 3-Clause License # # # # Copyright (c) 2021, Stephan Nolting. All
5// rights reserved. # # # # Redistribution
6// and use in source and binary forms, with or without modification, are # #
7// permitted provided that the following conditions are met: # # # # 1.
8// Redistributions of source code must retain the above copyright notice, this
9// list of # # conditions and the following disclaimer. # # # # 2.
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of # # conditions and the following disclaimer in the
12// documentation and/or other materials # # provided with the
13// distribution. # #
14// # # 3. Neither the name of the copyright holder nor the names of its
15// contributors may be used to # # endorse or promote products derived from
16// this software without specific prior written # # permission. # # # #
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS # # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19// THE IMPLIED WARRANTIES OF # # MERCHANTABILITY AND FITNESS FOR A
20// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # # COPYRIGHT
21// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, # # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE # # GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED # # AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING # # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED # # OF THE POSSIBILITY OF SUCH DAMAGE. #
28// #
29// *********************************************************************************************
30// # # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan
31// Nolting #
32// #################################################################################################
33
34/**********************************************************************/ /**
35 * @file
36 *blink_led/main.c
37 * @author
38 *Stephan
39 *Nolting
40 * @brief
41 *Simple
42 *blinking
43 *LED
44 *demo
45 *program
46 *using
47 *the
48 *lowest
49 *8
50 *bits
51 *of
52 *the
53 *GPIO.output
54 *port.
55 **************************************************************************/
56
57#include <neorv32.h>
58#include <stdbool.h>
59
60/**********************************************************************/ /**
61 * @name
62 *User
63 *configuration
64 **************************************************************************/
65/**@{*/
66/** UART BAUD rate */
67#define BAUD_RATE 19200
68/**@}*/
69
70#define UART1_TX_BUFFER_FULL \
71 ((NEORV32_UART1.CTRL & (1 << UART_CTRL_TX_FULL)) != 0)
72
73char tmpChar;
74char loopbackChar;
75bool charReceived = false;
76int rx_status;
77
78/**********************************************************************/ /**
79 * Main
80 *function;
81 *shows
82 *an
83 *incrementing
84 *8-bit
85 *counter
86 *on
87 *GPIO.output(7:0).
88 *
89 * @note
90 *This
91 *program
92 *requires
93 *the
94 *GPIO
95 *controller
96 *to be
97 *synthesized
98 *(the
99 *UART
100 *is
101 *optional).
102 *
103 * @return
104 *0 if
105 *execution
106 *was
107 *successful
108 **************************************************************************/
109int main() {
110
111 // init UART (primary UART = UART0; if no id number is specified the primary
112 // UART is used) at default baud rate, no parity bits, ho hw flow control
113 neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
114
115 if (neorv32_uart1_available() == 0) {
116 neorv32_uart0_print("Error! UART1 not synthesized!\n");
117 return 1;
118 }
119
120 // capture all exceptions and give debug info via UART
121 // this is not required, but keeps us safe
122 neorv32_rte_setup();
123
124 neorv32_uart1_setup(9600, PARITY_NONE, FLOW_CONTROL_NONE);
125
126 // say hello
127 neorv32_uart0_print("Starting UART1 loopback demo program\n");
128
129 // Assumes uart has a larger than default fifo
130 while (1) {
131 if (!charReceived) {
132 rx_status = neorv32_uart1_getc_safe(&tmpChar);
133 if (rx_status == 0) { // successfully rx a char
134 loopbackChar = tmpChar;
135 charReceived = true;
136 }
137 }
138 if (charReceived && !UART1_TX_BUFFER_FULL) {
139 neorv32_uart1_putc(loopbackChar);
140 charReceived = false;
141 }
142 }
143
144 return 0;
145}
#define BAUD_RATE
int main()