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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [docs/] [datasheet/] [software.adoc] - Blame information for rev 67

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 60 zero_gravi
:sectnums:
2
== Software Framework
3
 
4
To make actual use of the NEORV32 processor, the project comes with a complete software eco-system. This
5
ecosystem is based on the RISC-V port of the GCC GNU Compiler Collection and consists of the following elementary parts:
6
 
7
[cols="<6,<4"]
8
[grid="none"]
9
|=======================
10
| Application/bootloader start-up code | `sw/common/crt0.S`
11
| Application/bootloader linker script | `sw/common/neorv32.ld`
12
| Core hardware driver libraries | `sw/lib/include/` & `sw/lib/source/`
13 62 zero_gravi
| Central makefile | `sw/common/common.mk`
14 60 zero_gravi
| Auxiliary tool for generating NEORV32 executables | `sw/image_gen/`
15
| Default bootloader | `sw/bootloader/bootloader.c`
16
|=======================
17
 
18
Last but not least, the NEORV32 ecosystem provides some example programs for testing the hardware, for
19
illustrating the usage of peripherals and for general getting in touch with the project (`sw/example`).
20
 
21
// ####################################################################################################################
22
:sectnums:
23
=== Compiler Toolchain
24
 
25
The toolchain for this project is based on the free RISC-V GCC-port. You can find the compiler sources and
26
build instructions on the official RISC-V GNU toolchain GitHub page: https://github.com/riscv/riscv-gnutoolchain.
27
 
28
The NEORV32 implements a 32-bit base integer architecture (`rv32i`) and a 32-bit integer and soft-float ABI
29
(ilp32), so make sure you build an according toolchain.
30
 
31
Alternatively, you can download my prebuilt `rv32i/e` toolchains for 64-bit x86 Linux from: https://github.com/stnolting/riscv-gcc-prebuilt
32
 
33
The default toolchain prefix used by the project's makefiles is (can be changed in the makefiles): **`riscv32-unknown-elf`**
34
 
35
[TIP]
36
More information regarding the toolchain (building from scratch or downloading the prebuilt ones)
37 61 zero_gravi
can be found in the user guides' section https://stnolting.github.io/neorv32/ug/#_software_toolchain_setup[Software Toolchain Setup].
38 60 zero_gravi
 
39
 
40
 
41
<<<
42
// ####################################################################################################################
43
:sectnums:
44
=== Core Libraries
45
 
46
The NEORV32 project provides a set of C libraries that allows an easy usage of the processor/CPU features.
47
Just include the main NEORV32 library file in your application's source file(s):
48
 
49
[source,c]
50
----
51
#include 
52
----
53
 
54
Together with the makefile, this will automatically include all the processor's header files located in
55
`sw/lib/include` into your application. The actual source files of the core libraries are located in
56
`sw/lib/source` and are automatically included into the source list of your software project. The following
57
files are currently part of the NEORV32 core library:
58
 
59
[cols="<3,<4,<8"]
60
[options="header",grid="rows"]
61
|=======================
62
| C source file | C header file | Description
63
| -                  | `neorv32.h`            | main NEORV32 definitions and library file
64
| `neorv32_cfs.c`    | `neorv32_cfs.h`        | HW driver (stub)footnote:[This driver file only represents a stub, since the real CFS drivers are defined by the actual CFS implementation.] functions for the custom functions subsystem
65
| `neorv32_cpu.c`    | `neorv32_cpu.h`        | HW driver functions for the NEORV32 **CPU**
66
| `neorv32_gpio.c`   | `neorv32_gpio.h`       | HW driver functions for the **GPIO**
67 67 zero_gravi
| `neorv32_gptmr.c`  | `neorv32_gptmr.h`      | HW driver functions for the **GPTRM**
68 60 zero_gravi
| -                  | `neorv32_intrinsics.h` | macros for custom intrinsics/instructions
69 67 zero_gravi
| -                  | `neorv32_legacy.h`     | legacy back-compatibility layer
70 60 zero_gravi
| `neorv32_mtime.c`  | `neorv32_mtime.h`      | HW driver functions for the **MTIME**
71
| `neorv32_neoled.c` | `neorv32_neoled.h`     | HW driver functions for the **NEOLED**
72
| `neorv32_pwm.c`    | `neorv32_pwm.h`        | HW driver functions for the **PWM**
73
| `neorv32_rte.c`    | `neorv32_rte.h`        | NEORV32 **runtime environment** and helpers
74 67 zero_gravi
| `neorv32_slink.c`  | `neorv32_slink.h`      | HW driver functions for the **SLINK**
75 60 zero_gravi
| `neorv32_spi.c`    | `neorv32_spi.h`        | HW driver functions for the **SPI**
76
| `neorv32_trng.c`   | `neorv32_trng.h`       | HW driver functions for the **TRNG**
77
| `neorv32_twi.c`    | `neorv32_twi.h`        | HW driver functions for the **TWI**
78
| `neorv32_uart.c`   | `neorv32_uart.h`       | HW driver functions for the **UART0** and **UART1**
79
| `neorv32_wdt.c`    | `neorv32_wdt.h`        | HW driver functions for the **WDT**
80 67 zero_gravi
| `neorv32_xirq.c`   | `neorv32_xirq.h`       | HW driver functions for the **XIRQ**
81 60 zero_gravi
|=======================
82
 
83
.Documentation
84
[TIP]
85
All core library software sources are highly documented using _doxygen_. See section <>.
86
The documentation is automatically built and deployed to GitHub pages by the CI workflow (:https://stnolting.github.io/neorv32/sw/files.html).
87
 
88
 
89
 
90
 
91
<<<
92
// ####################################################################################################################
93
:sectnums:
94
=== Application Makefile
95
 
96 62 zero_gravi
Application compilation is based on a single, centralized **GNU makefiles** `sw/common/common.mk`. Each project in the
97
`sw/example` folder features a makefile that just includes this central makefile. When creating a new project, copy an existing project folder or
98 60 zero_gravi
at least the makefile to your new project folder. I suggest to create new projects also in `sw/example` to keep
99
the file dependencies. Of course, these dependencies can be manually configured via makefiles variables
100
when your project is located somewhere else.
101
 
102 62 zero_gravi
[NOTE]
103 60 zero_gravi
Before you can use the makefiles, you need to install the RISC-V GCC toolchain. Also, you have to add the
104 62 zero_gravi
installation folder of the compiler to your system's `PATH` variable. More information can be found in
105
https://stnolting.github.io/neorv32/ug/#_software_toolchain_setup[User Guide: Software Toolchain Setup].
106 60 zero_gravi
 
107
The makefile is invoked by simply executing make in your console:
108
 
109
[source,bash]
110
----
111
neorv32/sw/example/blink_led$ make
112
----
113
 
114
:sectnums:
115
==== Targets
116
 
117 62 zero_gravi
Just executing `make` (or executing `make help`) will show the help menu listing all available targets.
118 60 zero_gravi
 
119 62 zero_gravi
[source,makefile]
120
----
121
$ make
122
<<< NEORV32 Application Makefile >>>
123
Make sure to add the bin folder of RISC-V GCC to your PATH variable.
124
Targets:
125
 help       - show this text
126
 check      - check toolchain
127
 info       - show makefile/toolchain configuration
128
 exe        - compile and generate  executable for upload via bootloader
129
 hex        - compile and generate  executable raw file
130 65 zero_gravi
 image      - compile and generate VHDL IMEM boot image (for application) in local folder
131 62 zero_gravi
 install    - compile, generate and install VHDL IMEM boot image (for application)
132 65 zero_gravi
 sim        - in-console simulation using default/simple testbench and GHDL
133 62 zero_gravi
 all        - exe + hex + install
134
 elf_info   - show ELF layout info
135
 clean      - clean up project
136
 clean_all  - clean up project, core libraries and image generator
137 65 zero_gravi
 bl_image   - compile and generate VHDL BOOTROM boot image (for bootloader only!) in local folder
138 62 zero_gravi
 bootloader - compile, generate and install VHDL BOOTROM boot image (for bootloader only!)
139
----
140 60 zero_gravi
 
141
 
142
:sectnums:
143
==== Configuration
144
 
145 62 zero_gravi
The compilation flow is configured via variables right at the beginning of the **central**
146
makefile (`sw/common/common.mk`):
147 60 zero_gravi
 
148 62 zero_gravi
[TIP]
149
The makefile configuration variables can be (re-)defined directly when invoking the makefile. For
150 65 zero_gravi
example via `$ make MARCH=rv32ic clean_all exe`. You can also make project-specific definitions
151 62 zero_gravi
of all variables inside the project's actual makefile (e.g., `sw/example/blink_led/makefile`).
152
 
153 60 zero_gravi
[source,makefile]
154
----
155
# *****************************************************************************
156
# USER CONFIGURATION
157
# *****************************************************************************
158
# User's application sources (*.c, *.cpp, *.s, *.S); add additional files here
159
APP_SRC ?= $(wildcard ./*.c) $(wildcard ./*.s) $(wildcard ./*.cpp) $(wildcard ./*.S)
160
# User's application include folders (don't forget the '-I' before each entry)
161
APP_INC ?= -I .
162
# User's application include folders - for assembly files only (don't forget the '-I' before each
163
entry)
164
ASM_INC ?= -I .
165
# Optimization
166
EFFORT ?= -Os
167
# Compiler toolchain
168 62 zero_gravi
RISCV_PREFIX ?= riscv32-unknown-elf-
169 60 zero_gravi
# CPU architecture and ABI
170 65 zero_gravi
MARCH ?= rv32i
171
MABI  ?= ilp32
172 60 zero_gravi
# User flags for additional configuration (will be added to compiler flags)
173
USER_FLAGS ?=
174
# Relative or absolute path to the NEORV32 home folder
175
NEORV32_HOME ?= ../../..
176
# *****************************************************************************
177
----
178
 
179
[cols="<3,<10"]
180
[grid="none"]
181
|=======================
182
| _APP_SRC_         | The source files of the application (`*.c`, `*.cpp`, `*.S` and `*.s` files are allowed; file of these types in the project folder are automatically added via wildcards). Additional files can be added; separated by white spaces
183
| _APP_INC_         | Include file folders; separated by white spaces; must be defined with `-I` prefix
184
| _ASM_INC_         | Include file folders that are used only for the assembly source files (`*.S`/`*.s`).
185
| _EFFORT_          | Optimization level, optimize for size (`-Os`) is default; legal values: `-O0`, `-O1`, `-O2`, `-O3`, `-Os`
186 62 zero_gravi
| _RISCV_PREFIX_    | The toolchain prefix to be used; follows the naming convention "architecture-vendor-output-"
187 65 zero_gravi
| _MARCH_           | The targeted RISC-V architecture/ISA. Only `rv32` is supported by the NEORV32. Enable compiler support of optional CPU extension by adding the according extension letter (e.g. `rv32im` for _M_ CPU extension). See https://stnolting.github.io/neorv32/ug/#_enabling_risc_v_cpu_extensions[User Guide: Enabling RISC-V CPU Extensions] for more information.
188 60 zero_gravi
| _MABI_            | The default 32-bit integer ABI.
189
| _USER_FLAGS_      | Additional flags that will be forwarded to the compiler tools
190
| _NEORV32_HOME_    | Relative or absolute path to the NEORV32 project home folder. Adapt this if the makefile/project is not in the project's `sw/example folder`.
191
| _COM_PORT_        | Default serial port for executable upload to bootloader.
192
|=======================
193
 
194
:sectnums:
195
==== Default Compiler Flags
196
 
197
The following default compiler flags are used for compiling an application. These flags are defined via the
198
`CC_OPTS` variable. Custom flags can be appended via the `USER_FLAGS` variable to the `CC_OPTS` variable.
199
 
200
[cols="<3,<9"]
201
[grid="none"]
202
|=======================
203
| `-Wall` | Enable all compiler warnings.
204
| `-ffunction-sections` | Put functions and data segment in independent sections. This allows a code optimization as dead code and unused data can be easily removed.
205
| `-nostartfiles` | Do not use the default start code. The makefiles use the NEORV32-specific start-up code instead (`sw/common/crt0.S`).
206
| `-Wl,--gc-sections` | Make the linker perform dead code elimination.
207
| `-lm` | Include/link with `math.h`.
208
| `-lc` | Search for the standard C library when linking.
209
| `-lgcc` | Make sure we have no unresolved references to internal GCC library subroutines.
210 66 zero_gravi
| `-mno-fdiv` | Use built-in software functions for floating-point divisions and square roots (since the according instructions are not supported yet).
211 60 zero_gravi
| `-falign-functions=4` .4+| Force a 32-bit alignment of functions and labels (branch/jump/call targets). This increases performance as it simplifies instruction fetch when using the C extension. As a drawback this will also slightly increase the program code.
212
| `-falign-labels=4`
213
| `-falign-loops=4`
214
| `-falign-jumps=4`
215
|=======================
216
 
217
 
218
 
219
<<<
220
// ####################################################################################################################
221
:sectnums:
222
=== Executable Image Format
223
 
224 61 zero_gravi
In order to generate a file, which can be executed by the processor, all source files have to be compiler, linked
225
and packed into a final _executable_.
226 60 zero_gravi
 
227 61 zero_gravi
:sectnums:
228
==== Linker Script
229
 
230
When all the application sources have been compiled, they need to be _linked_ in order to generate a unified
231
program file. For this purpose the makefile uses the NEORV32-specific linker script `sw/common/neorv32.ld` for
232
linking all object files that were generated during compilation.
233
 
234
The linker script defines three memory _sections_: `rom`, `ram` and `iodev`. Each section provides specific
235
access _attributes_: read access (`r`), write access (`w`) and executable (`x`).
236
 
237
.Linker memory sections - general
238 60 zero_gravi
[cols="<2,^1,<7"]
239
[options="header",grid="rows"]
240
|=======================
241
| Memory section  | Attributes | Description
242 61 zero_gravi
| `ram`           | `rwx`      | Data memory address space (processor-internal/external DMEM)
243
| `rom`           | `rx`       | Instruction memory address space (processor-internal/external IMEM) _or_ internal bootloader ROM
244
| `iodev`         | `rw`       | Processor-internal memory-mapped IO/peripheral devices address space
245 60 zero_gravi
|=======================
246
 
247 61 zero_gravi
These sections are defined right at the beginning of the linker script:
248 60 zero_gravi
 
249 61 zero_gravi
.Linker memory sections - cut-out from linker script `neorv32.ld`
250
[source,c]
251
----
252
MEMORY
253
{
254
  ram  (rwx) : ORIGIN = 0x80000000, LENGTH = DEFINED(make_bootloader) ? 512 : 8*1024
255
  rom   (rx) : ORIGIN = DEFINED(make_bootloader) ? 0xFFFF0000 : 0x00000000, LENGTH = DEFINED(make_bootloader) ? 32K : 2048M
256
  iodev (rw) : ORIGIN = 0xFFFFFE00, LENGTH = 512
257
}
258
----
259 60 zero_gravi
 
260 61 zero_gravi
Each memory section provides a _base address_ `ORIGIN` and a _size_ `LENGTH`. The base address and size of the `iodev` section is
261
fixed and must not be altered. The base addresses and sizes of the `ram` and `rom` regions correspond to the total available instruction
262
and data memory address space (see section <<_address_space_layout>>).
263 60 zero_gravi
 
264 61 zero_gravi
[IMPORTANT]
265
`ORIGIN` of the `ram` section has to be always identical to the processor's `dspace_base_c` hardware configuration. Additionally,
266
`ORIGIN` of the `rom` section has to be always identical to the processor's `ispace_base_c` hardware configuration.
267
 
268
The sizes of `ram` section has to be equal to the size of the **physical available data instruction memory**. For example, if the processor
269
setup only uses processor-internal DMEM (<<_mem_int_dmem_en>> = _true_ and no external data memory attached) the `LENGTH` parameter of
270
this memory section has to be equal to the size configured by the <<_mem_int_dmem_size>> generic.
271
 
272
The sizes of `rom` section is a little bit more complicated. The default linker script configuration assumes a _maximum_ of 2GB _logical_
273
memory space, which is also the default configuration of the processor's hardware instruction memory address space. This size _does not_ have
274
to reflect the _actual_ physical size of the instruction memory (internal IMEM and/or processor-external memory). It just provides a maximum
275
limit. When uploading new executable via the bootloader, the bootloader itself checks if sufficient _physical_ instruction memory is available.
276
If a new executable is embedded right into the internal-IMEM the synthesis tool will check, if the configured instruction memory size
277
is sufficient (e.g., via the <<_mem_int_imem_size>> generic).
278
 
279
[IMPORTANT]
280
The `rom` region uses a conditional assignment (via the `make_bootloader` symbol) for `ORIGIN` and `LENGTH` that is used to place
281
"normal executable" (i.e. for the IMEM) or "the bootloader image" to their according memories. +
282
 +
283
The `ram` region also uses a conditional assignment (via the `make_bootloader` symbol) for `LENGTH`. When compiling the bootloader
284
(`make_bootloader` symbol is set) the generated bootloader will only use the _first_ 512 bytes of the data address space. This is
285
a fall-back to ensure the bootloader can operate independently of the actual _physical_ data memory size.
286
 
287
The linker maps all the regions from the compiled object files into four final sections: `.text`, `.rodata`, `.data` and `.bss`.
288
These four regions contain everything required for the application to run:
289
 
290
.Linker memory regions
291 60 zero_gravi
[cols="<1,<9"]
292
[options="header",grid="rows"]
293
|=======================
294 62 zero_gravi
| Region    | Description
295 60 zero_gravi
| `.text`   | Executable instructions generated from the start-up code and all application sources.
296
| `.rodata` | Constants (like strings) from the application; also the initial data for initialized variables.
297
| `.data`   | This section is required for the address generation of fixed (= global) variables only.
298
| `.bss`    | This section is required for the address generation of dynamic memory constructs only.
299
|=======================
300
 
301
The `.text` and `.rodata` sections are mapped to processor's instruction memory space and the `.data` and
302 61 zero_gravi
`.bss` sections are mapped to the processor's data memory space. Finally, the `.text`, `.rodata` and `.data`
303
sections are extracted and concatenated into a single file `main.bin`.
304 60 zero_gravi
 
305
 
306 61 zero_gravi
:sectnums:
307
==== Executable Image Generator
308 60 zero_gravi
 
309 61 zero_gravi
The `main.bin` file is packed by the NEORV32 image generator (`sw/image_gen`) to generate the final executable file.
310
 
311
[NOTE]
312
The sources of the image generator are automatically compiled when invoking the makefile.
313
 
314
The image generator can generate three types of executables, selected by a flag when calling the generator:
315
 
316 60 zero_gravi
[cols="<1,<9"]
317
[grid="none"]
318
|=======================
319
| `-app_bin` | Generates an executable binary file `neorv32_exe.bin` (for UART uploading via the bootloader).
320 62 zero_gravi
| `-app_hex` | Generates a plain ASCII hex-char file `neorv32_exe.hex` that can be used to initialize custom (instruction-) memories (in synthesis/simulation).
321 60 zero_gravi
| `-app_img` | Generates an executable VHDL memory initialization image for the processor-internal IMEM. This option generates the `rtl/core/neorv32_application_image.vhd` file.
322
| `-bld_img` | Generates an executable VHDL memory initialization image for the processor-internal BOOT ROM. This option generates the `rtl/core/neorv32_bootloader_image.vhd` file.
323
|=======================
324
 
325 61 zero_gravi
All these options are managed by the makefile. The _normal application_ compilation flow will generate the `neorv32_exe.bin`
326
executable to be upload via UART to the NEORV32 bootloader.
327 60 zero_gravi
 
328 61 zero_gravi
The image generator add a small header to the `neorv32_exe.bin` executable, which consists of three 32-bit words located right at the
329
beginning of the file. The first word of the executable is the signature word and is always `0x4788cafe`. Based on this word the bootloader
330
can identify a valid image file. The next word represents the size in bytes of the actual program
331 60 zero_gravi
image in bytes. A simple "complement" checksum of the actual program image is given by the third word. This
332
provides a simple protection against data transmission or storage errors.
333
 
334
 
335 61 zero_gravi
:sectnums:
336
==== Start-Up Code (crt0)
337 60 zero_gravi
 
338 61 zero_gravi
The CPU and also the processor require a minimal start-up and initialization code to bring the CPU (and the SoC)
339
into a stable and initialized state and to initialize the C runtime environment before the actual application can be executed.
340
This start-up code is located in `sw/common/crt0.S` and is automatically linked _every_ application program
341
and placed right before the actual application code so it gets executed right after reset.
342 60 zero_gravi
 
343 61 zero_gravi
The `crt0.S` start-up performs the following operations:
344 60 zero_gravi
 
345 61 zero_gravi
[start=1]
346
. Initialize all integer registers `x1 - x31` (or jsut `x1 - x15` when using the `E` CPU extension) to a defined value.
347
. Initialize the global pointer `gp` and the stack pointer `sp` according to the `.data` segment layout provided by the linker script.
348
. Initialize all CPU core CSRs and also install a default "dummy" trap handler for _all_ traps. This handler catches all traps during the early boot phase.
349
. Clear IO area: Write zero to all memory-mapped registers within the IO region (`iodev` section). If certain devices have not been implemented, a bus access fault exception will occur. This exception is captured by the dummy trap handler.
350
. Clear the `.bss` section defined by the linker script.
351
. Copy read-only data from the `.text` section to the `.data` section to set initialized variables.
352
. Call the application's `main` function (with _no_ arguments: `argc` = `argv` = 0).
353
. If the `main` function returns `crt0` can call an "after-main handler" (see below)
354
. If there is no after-main handler or after returning from the after-main handler the processor goes to an endless sleep mode (using a simple loop or via the `wfi` instruction if available).
355 60 zero_gravi
 
356 61 zero_gravi
:sectnums:
357
===== After-Main Handler
358
 
359
If the application's `main()` function actually returns, an _after main handler_ can be executed. This handler can be a normal function
360
since the C runtime is still available when executed. If this handler uses any kind of peripheral/IO modules make sure these are
361
already initialized within the application or you have to initialize them _inside_ the handler.
362
 
363
.After-main handler - function prototype
364
[source,c]
365
----
366
int __neorv32_crt0_after_main(int32_t return_code);
367
----
368
 
369
The function has exactly one argument (`return_code`) that provides the _return value_ of the application's main function.
370
For instance, this variable contains _-1_ if the main function returned with `return -1;`. The return value of the
371
`__neorv32_crt0_after_main` function is irrelevant as there is no further "software instance" executed afterwards that can check this.
372
However, the on-chip debugger could still evaluate the return value of the after-main handler.
373
 
374
A simple `printf` can be used to inform the user when the application main function return
375
(this example assumes that UART0 has been already properly configured in the actual application):
376
 
377
.After-main handler - example
378
[source,c]
379
----
380
int __neorv32_crt0_after_main(int32_t return_code) {
381
 
382 65 zero_gravi
  neorv32_uart0_printf("Main returned with code: %i\n", return_code);
383 61 zero_gravi
  return 0;
384
}
385
----
386
 
387
 
388 60 zero_gravi
<<<
389
// ####################################################################################################################
390
:sectnums:
391
=== Bootloader
392
 
393 61 zero_gravi
[NOTE]
394
This section illustrated the **default** bootloader from the repository. The bootloader can be customized
395
to target application-specific scenarios. See User Guide section
396
https://stnolting.github.io/neorv32/ug/#_customizing_the_internal_bootloader[Customizing the Internal Bootloader]
397
for more information.
398 60 zero_gravi
 
399 61 zero_gravi
The default NEORV32 bootloader (source code `sw/bootloader/bootloader.c`) provides a build-in firmware that
400
allows to upload new application executables via UART at every time and to optionally store/boot them to/from
401
an external SPI flash. It features a simple "automatic boot" feature that will try to fetch an executable
402
from SPI flash if there is _no_ UART user interaction. This allows to build processor setup with
403
non-volatile application storage, which can be updated at any time.
404 60 zero_gravi
 
405 61 zero_gravi
The bootloader is only implemented if the <<_int_bootloader_en>> generic is _true_. This will
406
select the <<_indirect_boot>> boot configuration.
407 60 zero_gravi
 
408 61 zero_gravi
.Hardware requirements of the _default_ NEORV32 bootloader
409 60 zero_gravi
[IMPORTANT]
410 61 zero_gravi
**REQUIRED**: The bootloader requires the CSR access CPU extension (<<_cpu_extension_riscv_zicsr>> generic is _true_)
411
and at least 512 bytes of data memory (processor-internal DMEM or external DMEM). +
412
 +
413
_RECOMMENDED_: For user interaction via UART (like uploading executables) the primary UART (UART0) has to be
414
implemented (<<_io_uart0_en>> generic is _true_). Without UART the bootloader does not make much sense. However, auto-boot
415
via SPI is still supported but the bootloader should be customized (see User Guide) for this purpose. +
416
 +
417
_OPTIONAL_: The default bootloader uses bit 0 of the GPIO output port as "heart beat" and status LED if the
418
GPIO controller is implemented (<<_io_gpio_en>> generic is _true_). +
419
 +
420
_OPTIONAL_: The MTIME machine timer (<<_io_mtime_en>> generic is _true_) and the SPI controller
421
(<<_io_spi_en>> generic is _true_) are required in order to use the bootloader's auto-boot feature
422
(automatic boot from external SPI flash if there is no user interaction via UART).
423 60 zero_gravi
 
424
To interact with the bootloader, connect the primary UART (UART0) signals (`uart0_txd_o` and
425
`uart0_rxd_o`) of the processor's top entity via a serial port (-adapter) to your computer (hardware flow control is
426
not used so the according interface signals can be ignored.), configure your
427 62 zero_gravi
terminal program using the following settings and perform a reset of the processor.
428 60 zero_gravi
 
429
Terminal console settings (`19200-8-N-1`):
430
 
431
* 19200 Baud
432
* 8 data bits
433
* no parity bit
434
* 1 stop bit
435
* newline on `\r\n` (carriage return, newline)
436
* no transfer protocol / control flow protocol - just the raw byte stuff
437
 
438
The bootloader uses the LSB of the top entity's `gpio_o` output port as high-active status LED (all other
439
output pin are set to low level by the bootloader). After reset, this LED will start blinking at ~2Hz and the
440
following intro screen should show up in your terminal:
441
 
442
[source]
443
----
444
<< NEORV32 Bootloader >>
445
 
446
BLDV: Mar 23 2021
447
HWV:  0x01050208
448
CLK:  0x05F5E100
449
MISA: 0x40901105
450 64 zero_gravi
CPU:  0x00000023
451
SOC:  0x0EFF0037
452 60 zero_gravi
IMEM: 0x00004000 bytes @ 0x00000000
453
DMEM: 0x00002000 bytes @ 0x80000000
454
 
455
Autoboot in 8s. Press key to abort.
456
----
457
 
458
This start-up screen also gives some brief information about the bootloader and several system configuration parameters:
459
 
460
[cols="<2,<15"]
461
[grid="none"]
462
|=======================
463
| `BLDV` | Bootloader version (built date).
464
| `HWV`  | Processor hardware version (from the `mimpid` CSR) in BCD format (example: `0x01040606` = v1.4.6.6).
465
| `CLK`  | Processor clock speed in Hz (via the SYSINFO module, from the _CLOCK_FREQUENCY_ generic).
466
| `MISA` | CPU extensions (from the `misa` CSR).
467 64 zero_gravi
| `CPU`  | CPU sub-extensions (via the `CPU` register in the SYSINFO module)
468
| `SOC`  | Processor configuration (via the `SOC` register in the SYSINFO module / from the IO_* and MEM_* configuration generics).
469 60 zero_gravi
| `IMEM` | IMEM memory base address and size in byte (from the _MEM_INT_IMEM_SIZE_ generic).
470
| `DMEM` | DMEM memory base address and size in byte (from the _MEM_INT_DMEM_SIZE_ generic).
471
|=======================
472
 
473
Now you have 8 seconds to press any key. Otherwise, the bootloader starts the auto boot sequence. When
474
you press any key within the 8 seconds, the actual bootloader user console starts:
475
 
476
[source]
477
----
478
<< NEORV32 Bootloader >>
479
 
480
BLDV: Mar 23 2021
481
HWV:  0x01050208
482
CLK:  0x05F5E100
483
USER: 0x10000DE0
484
MISA: 0x40901105
485 64 zero_gravi
CPU:  0x00000023
486
SOC:  0x0EFF0037
487 60 zero_gravi
IMEM: 0x00004000 bytes @ 0x00000000
488
DMEM: 0x00002000 bytes @ 0x80000000
489
 
490
Autoboot in 8s. Press key to abort.
491
Aborted.
492
 
493
Available commands:
494
h: Help
495
r: Restart
496
u: Upload
497
s: Store to flash
498
l: Load from flash
499
e: Execute
500
CMD:>
501
----
502
 
503
The auto-boot countdown is stopped and now you can enter a command from the list to perform the
504
corresponding operation:
505
 
506
* `h`: Show the help text (again)
507
* `r`: Restart the bootloader and the auto-boot sequence
508
* `u`: Upload new program executable (`neorv32_exe.bin`) via UART into the instruction memory
509
* `s`: Store executable to SPI flash at `spi_csn_o(0)`
510
* `l`: Load executable from SPI flash at `spi_csn_o(0)`
511
* `e`: Start the application, which is currently stored in the instruction memory (IMEM)
512
 
513
A new executable can be uploaded via UART by executing the `u` command. After that, the executable can be directly
514
executed via the `e` command. To store the recently uploaded executable to an attached SPI flash press `s`. To
515
directly load an executable from the SPI flash press `l`. The bootloader and the auto-boot sequence can be
516
manually restarted via the `r` command.
517
 
518
[TIP]
519
The CPU is in machine level privilege mode after reset. When the bootloader boots an application,
520
this application is also started in machine level privilege mode.
521
 
522 61 zero_gravi
[TIP]
523
For detailed information on using an SPI flash for application storage see User Guide section
524
https://stnolting.github.io/neorv32/ug/#_programming_an_external_spi_flash_via_the_bootloader[Programming an External SPI Flash via the Bootloader].
525 60 zero_gravi
 
526
 
527
:sectnums:
528
==== Auto Boot Sequence
529 61 zero_gravi
When you reset the NEORV32 processor, the bootloader waits 8 seconds for a UART console input before it
530 60 zero_gravi
starts the automatic boot sequence. This sequence tries to fetch a valid boot image from the external SPI
531 61 zero_gravi
flash, connected to SPI chip select `spi_csn_o(0)`. If a valid boot image is found that can be successfully
532
transferred into the instruction memory, it is automatically started. If no SPI flash is detected or if there
533
is no valid boot image found, and error code will be shown.
534 60 zero_gravi
 
535
 
536
:sectnums:
537
==== Bootloader Error Codes
538
 
539
If something goes wrong during bootloader operation, an error code is shown. In this case the processor
540
stalls, a bell command and one of the following error codes are send to the terminal, the bootloader status
541 61 zero_gravi
LED is permanently activated and the system must be manually reset.
542 60 zero_gravi
 
543
[cols="<2,<13"]
544
[grid="rows"]
545
|=======================
546 62 zero_gravi
| **`ERROR_0`** | If you try to transfer an invalid executable (via UART or from the external SPI flash), this error message shows up. There might be a transfer protocol configuration error in the terminal program. Also, if no SPI flash was found during an auto-boot attempt, this message will be displayed.
547
| **`ERROR_1`** | Your program is way too big for the internal processor’s instructions memory. Increase the memory size or reduce your application code.
548 60 zero_gravi
| **`ERROR_2`** | This indicates a checksum error. Something went wrong during the transfer of the program image (upload via UART or loading from the external SPI flash). If the error was caused by a UART upload, just try it again. When the error was generated during a flash access, the stored image might be corrupted.
549
| **`ERROR_3`** | This error occurs if the attached SPI flash cannot be accessed. Make sure you have the right type of flash and that it is properly connected to the NEORV32 SPI port using chip select #0.
550 66 zero_gravi
| **`ERR 0x???????? 0x???????? 0x????????`** | The bootloader encountered an exception during operation. This might be caused when it tries to access peripherals that were not implemented during synthesis. Example: executing `l` or `s` (SPI flash operations) without the SPI module beeing implemented.
551 60 zero_gravi
|=======================
552
 
553
 
554
 
555
<<<
556
// ####################################################################################################################
557
:sectnums:
558
=== NEORV32 Runtime Environment
559
 
560
The NEORV32 provides a minimal runtime environment (RTE) that takes care of a stable
561
and _safe_ execution environment by handling _all_ traps (including interrupts).
562
 
563
[NOTE]
564
Using the RTE is **optional**. The RTE provides a simple and comfortable way of delegating traps while making sure that all traps (even though they are not
565
explicitly used by the application) are handled correctly. Performance-optimized applications or embedded operating systems should not use the RTE for delegating traps.
566
 
567
When execution enters the application's `main` function, the actual runtime environment is responsible for catching all implemented exceptions
568
and interrupts. To activate the NEORV32 RTE execute the following function:
569
 
570
[source,c]
571
----
572
void neorv32_rte_setup(void);
573
----
574
 
575
This setup initializes the `mtvec` CSR, which provides the base entry point for all trap
576
handlers. The address stored to this register reflects the first-level exception handler provided by the
577
NEORV32 RTE. Whenever an exception or interrupt is triggered, this first-level handler is called.
578
 
579
The first-level handler performs a complete context save, analyzes the source of the exception/interrupt and
580
calls the according second-level exception handler, which actually takes care of the exception/interrupt
581
handling. For this, the RTE manages a private look-up table to store the addresses of the according trap
582
handlers.
583
 
584
After the initial setup of the RTE, each entry in the trap handler's look-up table is initialized with a debug
585
handler, that outputs detailed hardware information via the **primary UART (UART0)** when triggered. This
586
is intended as a fall-back for debugging or for accidentally-triggered exceptions/interrupts.
587 66 zero_gravi
For instance, an illegal instruction exception caught by the RTE debug handler might look like this in the UART0 output:
588 60 zero_gravi
 
589
[source]
590
----
591
 Illegal instruction @0x000002d6, MTVAL=0x00001537 
592
----
593
 
594
To install the **actual application's trap handlers** the NEORV32 RTE provides functions for installing and
595
un-installing trap handler for each implemented exception/interrupt source.
596
 
597
[source,c]
598
----
599
int neorv32_rte_exception_install(uint8_t id, void (*handler)(void));
600
----
601
 
602
[cols="<5,<12"]
603
[options="header",grid="rows"]
604
|=======================
605
| ID name [C] | Description / trap causing entry
606
| `RTE_TRAP_I_MISALIGNED` | instruction address misaligned
607
| `RTE_TRAP_I_ACCESS`     | instruction (bus) access fault
608
| `RTE_TRAP_I_ILLEGAL`    | illegal instruction
609
| `RTE_TRAP_BREAKPOINT`   | breakpoint (`ebreak` instruction)
610
| `RTE_TRAP_L_MISALIGNED` | load address misaligned
611
| `RTE_TRAP_L_ACCESS`     | load (bus) access fault
612
| `RTE_TRAP_S_MISALIGNED` | store address misaligned
613
| `RTE_TRAP_S_ACCESS`     | store (bus) access fault
614
| `RTE_TRAP_MENV_CALL`    | environment call from machine mode (`ecall` instruction)
615
| `RTE_TRAP_UENV_CALL`    | environment call from user mode (`ecall` instruction)
616
| `RTE_TRAP_MTI`          | machine timer interrupt
617
| `RTE_TRAP_MEI`          | machine external interrupt
618
| `RTE_TRAP_MSI`          | machine software interrupt
619
| `RTE_TRAP_FIRQ_0` : `RTE_TRAP_FIRQ_15` | fast interrupt channel 0..15
620
|=======================
621
 
622
When installing a custom handler function for any of these exception/interrupts, make sure the function uses
623
**no attributes** (especially no interrupt attribute!), has no arguments and no return value like in the following
624
example:
625
 
626
[source,c]
627
----
628
void handler_xyz(void) {
629
 
630
  // handle exception/interrupt...
631
}
632
----
633
 
634
[WARNING]
635
Do NOT use the `((interrupt))` attribute for the application exception handler functions! This
636
will place an `mret` instruction to the end of it making it impossible to return to the first-level
637
exception handler of the RTE, which will cause stack corruption.
638
 
639
Example: Installation of the MTIME interrupt handler:
640
 
641
[source,c]
642
----
643
neorv32_rte_exception_install(EXC_MTI, handler_xyz);
644
----
645
 
646
To remove a previously installed exception handler call the according un-install function from the NEORV32
647
runtime environment. This will replace the previously installed handler by the initial debug handler, so even
648
un-installed exceptions and interrupts are further captured.
649
 
650
[source,c]
651
----
652
int neorv32_rte_exception_uninstall(uint8_t id);
653
----
654
 
655
Example: Removing the MTIME interrupt handler:
656
 
657
[source,c]
658
----
659
neorv32_rte_exception_uninstall(EXC_MTI);
660
----
661
 
662
[TIP]
663
More information regarding the NEORV32 runtime environment can be found in the doxygen
664
software documentation (also available online at https://stnolting.github.io/neorv32/sw/files.html[GitHub pages]).

powered by: WebSVN 2.1.0

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