OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_newlib/] [main.c] - Blame information for rev 74

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 74 zero_gravi
// #################################################################################################
2
// # << NEORV32 - Newlib Demo/Test Program >>                                                      #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file demo_newlib/main.c
38
 * @author Stephan Nolting
39
 * @brief Demo/test program for NEORV32's newlib C standard library support.
40
 **************************************************************************/
41
#include <neorv32.h>
42
#include <unistd.h>
43
#include <stdlib.h>
44
 
45
 
46
/**********************************************************************//**
47
 * @name User configuration
48
 **************************************************************************/
49
/**@{*/
50
/** UART BAUD rate */
51
#define BAUD_RATE 19200
52
/**@}*/
53
 
54
 
55
/**********************************************************************//**
56
 * Main function: Check some of newlib's core functions.
57
 *
58
 * @note This program requires UART0.
59
 *
60
 * @return 0 if execution was successful
61
 **************************************************************************/
62
int main() {
63
 
64
  // setup NEORV32 runtime environment to keep us safe
65
  // -> catch all traps and give debug information via UART0
66
  neorv32_rte_setup();
67
 
68
  // setup UART0 at default baud rate, no parity bits, no HW flow control
69
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
70
 
71
  // check if UART0 is implemented at all
72
  if (neorv32_uart0_available() == 0) {
73
    neorv32_uart0_printf("Error! UART0 not synthesized!\n");
74
    return 1;
75
  }
76
 
77
 
78
  // say hello
79
  neorv32_uart0_printf("<<< Newlib demo/test program >>>\n\n");
80
 
81
 
82
  // check if newlib is really available
83
#ifndef __NEWLIB__
84
  neorv32_uart0_printf("ERROR! Seems like the compiler toolchain does not support newlib...\n");
85
  return -1;
86
#endif
87
 
88
  neorv32_uart0_printf("newlib version %i.%i\n\n", (int32_t)__NEWLIB__, (int32_t)__NEWLIB_MINOR__);
89
 
90
  neorv32_uart0_printf("<rand> test... ");
91
  srand(neorv32_cpu_csr_read(CSR_CYCLE)); // set random seed
92
  neorv32_uart0_printf("%i, %i, %i, %i ", rand() % 100, rand() % 100, rand() % 100, rand() % 100);
93
  neorv32_uart0_printf("ok\n");
94
 
95
 
96
  char *char_buffer; // pointer for dynamic memory allocation
97
 
98
  neorv32_uart0_printf("<malloc> test... ");
99
  char_buffer = (char *) malloc(4 * sizeof(char)); // 4 bytes
100
  neorv32_uart0_printf("ok\n");
101
 
102
  // do not test read & write in simulation as there would be no UART RX input
103
  if (NEORV32_SYSINFO.SOC & (1<<SYSINFO_SOC_IS_SIM)) {
104
    neorv32_uart0_printf("Skipping <read> & <write> tests as this seems to be a simulation.\n");
105
  }
106
  else {
107
    neorv32_uart0_printf("<read> test (waiting for 4 chars via UART0)... ");
108
    read((int)STDIN_FILENO, char_buffer, 4 * sizeof(char)); // get 4 chars from "STDIN" (UART0.RX)
109
    neorv32_uart0_printf("ok\n");
110
 
111
    neorv32_uart0_printf("<write> test to 'STDOUT'... (outputting the chars you have send)\n");
112
    write((int)STDOUT_FILENO, char_buffer, 4 * sizeof(char)); // send 4 chars to "STDOUT" (UART0.TX)
113
    neorv32_uart0_printf("\nok\n");
114
 
115
    neorv32_uart0_printf("<write> test to 'STDERR'... (outputting the chars you have send)\n");
116
    write((int)STDERR_FILENO, char_buffer, 4 * sizeof(char)); // send 4 chars to "STDERR" (UART0.TX)
117
    neorv32_uart0_printf("\nok\n");
118
  }
119
 
120
  neorv32_uart0_printf("<free> test... ");
121
  free(char_buffer);
122
  neorv32_uart0_printf("ok\n");
123
 
124
 
125
  // NOTE: exit is highly oversized as it also includes clean-up functions (destructors), which
126
  // is not required for bare-metal or RTOS applications... better use the simple 'return' or even better
127
  // make sure main never returns. however, let's test that 'exit' works.
128
  neorv32_uart0_printf("<exit> test...");
129
  exit(0);
130
 
131
  return 0; // should never be reached
132
}
133
 
134
 
135
/**********************************************************************//**
136
 * "after-main" handler that is executed after the application's
137
 * main function returns (called by crt0.S start-up code)
138
 **************************************************************************/
139
void __neorv32_crt0_after_main(int32_t return_code) {
140
 
141
  neorv32_uart0_printf("\n<RTE> main function returned with exit code %i </RTE>\n", return_code);
142
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.