summaryrefslogtreecommitdiff
path: root/ports/risc-v64/gnu/example_build/erbium/test/test_plic_trap.c
blob: 8dabb42b263d13bb7f42ce90356ac84664bbdfe9 (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
/***************************************************************************
 * Copyright (c) 2026 Eclipse ThreadX contributors
 *
 * 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.
 *
 * AI Disclosure: This file was largely AI-generated by Codex (GPT-6-Sol).
 * The AI-generated portions may be considered public domain (CC0-1.0)
 * and not subject to the project's licence. The human contributor has
 * reviewed and verified that the code is correct.
 *
 * SPDX-License-Identifier: MIT and CC0-1.0
 ***************************************************************************/

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

#define TEST_TIMER_INTERVAL 8u
#define TEST_MCAUSE_TIMER (((uintptr_t)1u << 63) | 7u)

extern volatile unsigned int test_irq_count;
extern volatile unsigned int test_expected_bit;

/* Toggle source 2 in timer context to preempt source 1 updates. */
void trap_handler(uintptr_t mcause, uintptr_t mepc, uintptr_t mtval)
{
    (void)mepc;
    (void)mtval;
    if (mcause != TEST_MCAUSE_TIMER)
    {
        uart_puts("TEST_PLIC_UNEXPECTED_TRAP");
        for (;;)
        {
        }
    }

    /* MISRA C:2012 Rule 11.4 deviation: the timer has a fixed MMIO address. */
    *(volatile uint64_t *)ERBIUM_MTIMECMP =
        *(volatile uint64_t *)ERBIUM_MTIME + TEST_TIMER_INTERVAL;
    if (test_expected_bit == 0u)
    {
        plic_irq_enable(2);
        test_expected_bit = 1u;
    }
    else
    {
        plic_irq_disable(2);
        test_expected_bit = 0u;
    }
    test_irq_count++;
}