| 1 |
2 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # << NEORV32: neorv32_rte.c - NEORV32 Runtime Environment >> #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # BSD 3-Clause License #
|
| 5 |
|
|
// # #
|
| 6 |
|
|
// # Copyright (c) 2020, 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 neorv32_rte.c
|
| 38 |
|
|
* @author Stephan Nolting
|
| 39 |
|
|
* @brief NEORV32 Runtime Environment.
|
| 40 |
|
|
**************************************************************************/
|
| 41 |
|
|
|
| 42 |
|
|
#include "neorv32.h"
|
| 43 |
|
|
#include "neorv32_rte.h"
|
| 44 |
|
|
|
| 45 |
|
|
// Privates
|
| 46 |
|
|
static void __neorv32_rte_dummy_exc_handler(void) __attribute__((unused));
|
| 47 |
|
|
static void __neorv32_rte_debug_exc_handler(void) __attribute__((unused));
|
| 48 |
|
|
|
| 49 |
|
|
|
| 50 |
|
|
/**********************************************************************//**
|
| 51 |
|
|
* Setup NEORV32 runtime environment in debug mode.
|
| 52 |
|
|
*
|
| 53 |
|
|
* @note This function installs a debug handler for ALL exception and interrupt sources, which
|
| 54 |
|
|
* gives detailed information about the exception/interrupt. Call this function before you
|
| 55 |
|
|
* install custom handler functions via neorv32_rte_exception_install(uint8_t exc_id, void (*handler)(void)),
|
| 56 |
|
|
* since this function will override all installed exception handlers.
|
| 57 |
|
|
*
|
| 58 |
|
|
* @warning This function should be used for debugging only, since it only shows the uninitialize exception/interrupt, but
|
| 59 |
|
|
* does not resolve the cause. Hence, it cannot guarantee to resume normal application execution after showing the debug messages.
|
| 60 |
|
|
**************************************************************************/
|
| 61 |
|
|
void neorv32_rte_enable_debug_mode(void) {
|
| 62 |
|
|
|
| 63 |
|
|
uint8_t id;
|
| 64 |
|
|
|
| 65 |
|
|
// install debug handler for all sources
|
| 66 |
|
|
for (id=0; id<32; id++) {
|
| 67 |
|
|
neorv32_rte_exception_install(id, __neorv32_rte_debug_exc_handler);
|
| 68 |
|
|
}
|
| 69 |
|
|
}
|
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
|
|
/**********************************************************************//**
|
| 73 |
|
|
* Install exception handler function to NEORV32 runtime environment.
|
| 74 |
|
|
*
|
| 75 |
|
|
* @note This function automatically activates the according CSR.mie bits when installing handlers for
|
| 76 |
|
|
* the MTIME (MTI), CLIC (MEI) or machine software interrupt (MSI). The global interrupt enable bit mstatus.mie has
|
| 77 |
|
|
* to be set by the user via neorv32_cpu_eint(void).
|
| 78 |
|
|
*
|
| 79 |
|
|
* @param[in] exc_id Identifier (type) of the targeted exception. See #NEORV32_EXCEPTION_IDS_enum.
|
| 80 |
|
|
* @param[in] handler The actual handler function for the specified exception (function must be of type "void function(void);").
|
| 81 |
|
|
* return 0 if success, 1 if error (invalid exc_id or targeted exception not supported).
|
| 82 |
|
|
**************************************************************************/
|
| 83 |
|
|
int neorv32_rte_exception_install(uint8_t exc_id, void (*handler)(void)) {
|
| 84 |
|
|
|
| 85 |
|
|
// id valid?
|
| 86 |
|
|
if ((exc_id == EXCID_I_MISALIGNED) || (exc_id == EXCID_I_ACCESS) || (exc_id == EXCID_I_ILLEGAL) ||
|
| 87 |
|
|
(exc_id == EXCID_BREAKPOINT) || (exc_id == EXCID_L_MISALIGNED) || (exc_id == EXCID_L_ACCESS) ||
|
| 88 |
|
|
(exc_id == EXCID_S_MISALIGNED) || (exc_id == EXCID_S_ACCESS) || (exc_id == EXCID_MENV_CALL) ||
|
| 89 |
|
|
(exc_id == EXCID_MSI) || (exc_id == EXCID_MTI) || (exc_id == EXCID_MEI)) {
|
| 90 |
|
|
|
| 91 |
|
|
if (exc_id == EXCID_MSI) { neorv32_cpu_irq_enable(CPU_MIE_MSIE); } // activate software interrupt
|
| 92 |
|
|
if (exc_id == EXCID_MTI) { neorv32_cpu_irq_enable(CPU_MIE_MTIE); } // activate timer interrupt
|
| 93 |
|
|
if (exc_id == EXCID_MEI) { neorv32_cpu_irq_enable(CPU_MIE_MEIE); } // activate external interrupt
|
| 94 |
|
|
|
| 95 |
|
|
uint32_t vt_base = neorv32_cpu_csr_read(CSR_MDSPACEBASE); // base address of vector table
|
| 96 |
|
|
vt_base = vt_base + (((uint32_t)exc_id) << 2);
|
| 97 |
|
|
(*(IO_REG32 (vt_base))) = (uint32_t)handler;
|
| 98 |
|
|
|
| 99 |
|
|
return 0;
|
| 100 |
|
|
}
|
| 101 |
|
|
return 1;
|
| 102 |
|
|
}
|
| 103 |
|
|
|
| 104 |
|
|
|
| 105 |
|
|
/**********************************************************************//**
|
| 106 |
|
|
* Uninstall exception handler function from NEORV32 runtime environment, which was
|
| 107 |
|
|
* previously installed via neorv32_rte_exception_install(uint8_t exc_id, void (*handler)(void)).
|
| 108 |
|
|
*
|
| 109 |
|
|
* @note This function automatically clears the according CSR.mie bits when uninstalling handlers for
|
| 110 |
|
|
* the MTIME (MTI), CLIC (MEI) or machine software interrupt (MSI). The global interrupt enable bit mstatus.mie has
|
| 111 |
|
|
* to be cleared by the user via neorv32_cpu_dint(void).
|
| 112 |
|
|
*
|
| 113 |
|
|
* @param[in] exc_id Identifier (type) of the targeted exception. See #NEORV32_EXCEPTION_IDS_enum.
|
| 114 |
|
|
* return 0 if success, 1 if error (invalid exc_id or targeted exception not supported).
|
| 115 |
|
|
**************************************************************************/
|
| 116 |
|
|
int neorv32_rte_exception_uninstall(uint8_t exc_id) {
|
| 117 |
|
|
|
| 118 |
|
|
// id valid?
|
| 119 |
|
|
if ((exc_id == EXCID_I_MISALIGNED) || (exc_id == EXCID_I_ACCESS) || (exc_id == EXCID_I_ILLEGAL) ||
|
| 120 |
|
|
(exc_id == EXCID_BREAKPOINT) || (exc_id == EXCID_L_MISALIGNED) || (exc_id == EXCID_L_ACCESS) ||
|
| 121 |
|
|
(exc_id == EXCID_S_MISALIGNED) || (exc_id == EXCID_S_ACCESS) || (exc_id == EXCID_MENV_CALL) ||
|
| 122 |
|
|
(exc_id == EXCID_MSI) || (exc_id == EXCID_MTI) || (exc_id == EXCID_MEI)) {
|
| 123 |
|
|
|
| 124 |
|
|
if (exc_id == EXCID_MSI) { neorv32_cpu_irq_disable(CPU_MIE_MSIE); } // deactivate software interrupt
|
| 125 |
|
|
if (exc_id == EXCID_MTI) { neorv32_cpu_irq_disable(CPU_MIE_MTIE); } // deactivate timer interrupt
|
| 126 |
|
|
if (exc_id == EXCID_MEI) { neorv32_cpu_irq_disable(CPU_MIE_MEIE); } // deactivate external interrupt
|
| 127 |
|
|
|
| 128 |
|
|
uint32_t vt_base = neorv32_cpu_csr_read(CSR_MDSPACEBASE); // base address of vector table
|
| 129 |
|
|
vt_base = vt_base + (((uint32_t)exc_id) << 2);
|
| 130 |
|
|
(*(IO_REG32 (vt_base))) = (uint32_t)(&__neorv32_rte_dummy_exc_handler); // use dummy handler in case the exception is triggered
|
| 131 |
|
|
|
| 132 |
|
|
return 0;
|
| 133 |
|
|
}
|
| 134 |
|
|
return 1;
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
|
| 138 |
|
|
/**********************************************************************//**
|
| 139 |
|
|
* NEORV32 runtime environment: Dummy exception handler (does nothing).
|
| 140 |
|
|
* @note This function is used by neorv32_rte_exception_uninstall(uint8_t exc_id) only.
|
| 141 |
|
|
**************************************************************************/
|
| 142 |
|
|
static void __neorv32_rte_dummy_exc_handler(void) {
|
| 143 |
|
|
|
| 144 |
|
|
asm volatile("nop");
|
| 145 |
|
|
}
|
| 146 |
|
|
|
| 147 |
|
|
|
| 148 |
|
|
/**********************************************************************//**
|
| 149 |
|
|
* NEORV32 runtime environment: Debug exception handler, printing various exception/interrupt information via UART.
|
| 150 |
|
|
* @note This function is used by neorv32_rte_enable_debug_mode(void) only.
|
| 151 |
|
|
**************************************************************************/
|
| 152 |
|
|
static void __neorv32_rte_debug_exc_handler(void) {
|
| 153 |
|
|
|
| 154 |
|
|
neorv32_uart_printf("\n\n\n<<< NEORV32 Runtime Environment >>>\n");
|
| 155 |
|
|
|
| 156 |
|
|
neorv32_uart_printf("System time: 0x%x_%x\n", neorv32_cpu_csr_read(CSR_TIMEH), neorv32_cpu_csr_read(CSR_TIME));
|
| 157 |
|
|
|
| 158 |
|
|
uint32_t exc_cause = neorv32_cpu_csr_read(CSR_MCAUSE);
|
| 159 |
|
|
|
| 160 |
|
|
if (exc_cause & 0x80000000) {
|
| 161 |
|
|
neorv32_uart_printf("INTERRUPT");
|
| 162 |
|
|
}
|
| 163 |
|
|
else {
|
| 164 |
|
|
neorv32_uart_printf("EXCEPTION");
|
| 165 |
|
|
}
|
| 166 |
|
|
neorv32_uart_printf(" at instruction address: 0x%x\n", neorv32_cpu_csr_read(CSR_MEPC));
|
| 167 |
|
|
|
| 168 |
|
|
neorv32_uart_printf("Cause: ");
|
| 169 |
|
|
switch (exc_cause) {
|
| 170 |
|
|
case 0x00000000: neorv32_uart_printf("Instruction address misaligned"); break;
|
| 171 |
|
|
case 0x00000001: neorv32_uart_printf("Instruction access fault"); break;
|
| 172 |
|
|
case 0x00000002: neorv32_uart_printf("Illegal instruction"); break;
|
| 173 |
|
|
case 0x00000003: neorv32_uart_printf("Breakpoint (EBREAK)"); break;
|
| 174 |
|
|
case 0x00000004: neorv32_uart_printf("Load address misaligned"); break;
|
| 175 |
|
|
case 0x00000005: neorv32_uart_printf("Load access fault"); break;
|
| 176 |
|
|
case 0x00000006: neorv32_uart_printf("Store address misaligned"); break;
|
| 177 |
|
|
case 0x00000007: neorv32_uart_printf("Store access fault"); break;
|
| 178 |
|
|
case 0x0000000B: neorv32_uart_printf("Environment call (ECALL)"); break;
|
| 179 |
|
|
case 0x80000003: neorv32_uart_printf("Machine software interrupt"); break;
|
| 180 |
|
|
case 0x80000007: neorv32_uart_printf("Machine timer interrupt (via MTIME)"); break;
|
| 181 |
|
|
case 0x8000000B: neorv32_uart_printf("Machine external interrupt (via CLIC)"); break;
|
| 182 |
|
|
default: neorv32_uart_printf("Unknown (0x%x)", exc_cause); break;
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
// fault address
|
| 186 |
|
|
if (exc_cause == 0x00000002) {
|
| 187 |
|
|
neorv32_uart_printf("\nFaulting instruction");
|
| 188 |
|
|
}
|
| 189 |
|
|
else {
|
| 190 |
|
|
neorv32_uart_printf("\nFaulting address");
|
| 191 |
|
|
}
|
| 192 |
|
|
neorv32_uart_printf(": 0x%x\n", neorv32_cpu_csr_read(CSR_MTVAL));
|
| 193 |
|
|
uint32_t trans_cmd = neorv32_cpu_csr_read(CSR_MTINST);
|
| 194 |
|
|
neorv32_uart_printf("Transf. instruction: 0x%x ", trans_cmd);
|
| 195 |
|
|
|
| 196 |
|
|
if (trans_cmd & (1 << 1)) {
|
| 197 |
|
|
neorv32_uart_printf("(uncompr.)\n");
|
| 198 |
|
|
}
|
| 199 |
|
|
else {
|
| 200 |
|
|
neorv32_uart_printf("(compr.)\n");
|
| 201 |
|
|
}
|
| 202 |
|
|
|
| 203 |
|
|
neorv32_uart_printf("Trying to resume application @ 0x%x...", neorv32_cpu_csr_read(CSR_MSCRATCH));
|
| 204 |
|
|
|
| 205 |
|
|
neorv32_uart_printf("\n<<</NEORV32 Runtime Environment >>>\n\n\n");
|
| 206 |
|
|
}
|
| 207 |
|
|
|