Nios® V Processor Software Developer Handbook

ID 743810
Date 10/06/2025
Public
Document Table of Contents

11.4. C Example

This C example demonstrating ECC error injection and exception code.
  • The processor injects an uncorrectable GPR ECC error.
  • The ECC exception handler is called and return to normal trap trampoline.
#include <stdio.h>
#include "intel_niosv.h"
#include "sys/alt_ecc_exception.h"
#include "sys/alt_ecc_error_inject.h"
#include "sys/alt_irq.h"

#define NIOSV_GPR_ECC_CORRECTABLE_ERROR	0
#define NIOSV_GPR_ECC_UNCORRECTABLE_ERROR	1

alt_ecc_error_type ecc_error_type;
unsigned int g_ecc_handler_trigger = 0;

void ecc_handler(void){
	g_ecc_handler_trigger = 1;

       // Reenable ECC exception in alt_ecc_status
    	NIOSV_WRITE_CSR(NIOSV_ECC_STATUS_CSR, NIOSV_ENABLE_ECC_ERROR |
            (1<<NIOSV_GPR_ECC_UNCORRECTABLE_ERROR)|
			      (1<<NIOSV_GPR_ECC_CORRECTABLE_ERROR));

	    // Return to normal trap trampoline
	    asm volatile ("j alt_not_ecc_exception");
}

int main() {
       // Disable all interrupts
	    alt_irq_disable_all();

    	// Enable ECC exception in alt_ecc_status
    	NIOSV_WRITE_CSR(NIOSV_ECC_STATUS_CSR, NIOSV_ENABLE_ECC_ERROR |
			          (1<<NIOSV_GPR_ECC_UNCORRECTABLE_ERROR)|
			          (1<<NIOSV_GPR_ECC_CORRECTABLE_ERROR));

    	// Register a ECC exception handler
    	alt_ecc_exception_register((alt_u32) &ecc_handler);
	
	    // Inject the error.
    	ecc_error_type = (alt_ecc_error_type)NIOSV_GPR_ECC_UNCORRECTABLE_ERROR;
    	alt_ecc_error_inject(ecc_error_type);
	
	    if (g_ecc_handler_trigger == 0) {
       	    printf("ECC exception handler is not triggered.\n");
    	} else {
    		    printf("ECC exception handler is triggered\n");
    	}
    
return 0;
}