summaryrefslogtreecommitdiff
path: root/ports/risc-v64/gnu/example_build/bananapi-f3/trap.c
blob: 6418b02c37d7b859bec492428f0796688d4b78c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/***************************************************************************
 * Copyright (c) 2025 10xEngineers
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/*
 * S-mode trap handler 
 */

#include "csr.h"
#include "uart.h"
#include "hwtimer.h"
#include "plic.h"
#include <stdint.h>
#include <tx_port.h>
#include <tx_api.h>

/* scause bit 63: interrupt flag. */
#define SCAUSE_INTERRUPT        0x8000000000000000ull

#define SCAUSE_S_TIMER_INT      (SCAUSE_INTERRUPT | 5u)
#define SCAUSE_S_EXTERNAL_INT   (SCAUSE_INTERRUPT | 9u)

extern void _tx_timer_interrupt(void);

void trap_handler(uintptr_t cause, uintptr_t epc, uintptr_t tval)
{
    (void)epc;
    (void)tval;

    if (cause & SCAUSE_INTERRUPT)
    {
        if (cause == SCAUSE_S_TIMER_INT)
        {
            hwtimer_handler();
            _tx_timer_interrupt();
        }
        else if (cause == SCAUSE_S_EXTERNAL_INT)
        {
            if (plic_irq_intr() != 0)
            {
                puts("[trap] PLIC dispatch failed");
                while (1)
                    ;
            }
        }
        else
        {
            puts("[trap] unhandled S-mode interrupt");
            while (1)
                ;
        }
    }
    else
    {
        puts("[trap] unhandled synchronous exception");
        while (1)
            ;
    }
}