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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [examples/] [isrdemo/] [src/] [helloworld.c] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
/*****************************************************************************
2
 * @file
3
 * @author   Sergey Khabarov
4
 * @brief    Firmware example.
5
 ****************************************************************************/
6
 
7
#include <inttypes.h>
8
#include <string.h>
9
#include <stdio.h>
10
#include "axi_maps.h"
11
 
12
extern char _end;
13
extern void init_mtvec(void);
14
extern void init_mtvec();
15
 
16
volatile int was_interrupt = 0;
17
 
18
typedef union csr_mcause_type {
19
    struct bits_type {
20
        uint64_t code   : 63;   // 11 - Machine external interrupt
21
        uint64_t irq    : 1;
22
    } bits;
23
    uint64_t value;
24
} csr_mcause_type;
25
 
26
 
27
/**
28
 * @name sbrk
29
 * @brief Increase program data space.
30
 * @details Malloc and related functions depend on this.
31
 */
32
char *sbrk(int incr) {
33
    return &_end;
34
}
35
 
36
void print_uart(const char *buf, int sz) {
37
    uart_map *uart = (uart_map *)ADDR_NASTI_SLAVE_UART1;
38
    for (int i = 0; i < sz; i++) {
39
        while (uart->status & UART_STATUS_TX_FULL) {}
40
        uart->data = buf[i];
41
    }
42
}
43
 
44
void print_uart_hex(long val) {
45
    unsigned char t, s;
46
    uart_map *uart = (uart_map *)ADDR_NASTI_SLAVE_UART1;
47
    for (int i = 0; i < 16; i++) {
48
        while (uart->status & UART_STATUS_TX_FULL) {}
49
 
50
        t = (unsigned char)((val >> ((15 - i) * 4)) & 0xf);
51
        if (t < 10) {
52
            s = t + '0';
53
        } else {
54
            s = (t - 10) + 'a';
55
        }
56
        uart->data = s;
57
    }
58
}
59
 
60
 
61
void isr_example_c(long cause, long epc, long long regs[32]) {
62
    irqctrl_map *p_irqctrl = (irqctrl_map *)ADDR_NASTI_SLAVE_IRQCTRL;
63
    gptimers_map *p_timer = (gptimers_map *)ADDR_NASTI_SLAVE_GPTIMERS;
64
    uint32_t pending;
65
 
66
    csr_mcause_type mcause;
67
    mcause.value = cause;
68
 
69
    print_uart("mcause:", 7);
70
    print_uart_hex(cause);
71
    print_uart(",mepc:", 6);
72
    print_uart_hex(epc);
73
    print_uart("\r\n", 2);
74
 
75
 
76
    p_timer->pending = 0;
77
 
78
    p_irqctrl->irq_clear = ~0;
79
 
80
    print_uart("IRQ\n", 4);
81
    was_interrupt = 1;
82
}
83
 
84
 
85
void helloWorld() {
86
    char ss[256];
87
    int ss_len;
88
    int i = 0;
89
    uint32_t tech;
90
    pnp_map *pnp = (pnp_map *)ADDR_NASTI_SLAVE_PNP;
91
    tech = pnp->tech & 0xff;
92
 
93
    init_mtvec();
94
 
95
    gptimers_map *p_timer = (gptimers_map *)ADDR_NASTI_SLAVE_GPTIMERS;
96
    if (tech == 0) {
97
        p_timer->timer[0].init_value = 20000ull;
98
    } else {
99
        p_timer->timer[0].init_value = 40000000ull;
100
    }
101
    p_timer->timer[0].control = 0x3;  // count_ena | irq_ena
102
 
103
    // enable GpTimer interrupts
104
    irqctrl_map *p_irqctrl = (irqctrl_map *)ADDR_NASTI_SLAVE_IRQCTRL;
105
    uint32_t bit = p_irqctrl->irq_mask;
106
    bit &= ~(1u << CFG_IRQ_GPTIMERS);
107
    p_irqctrl->irq_mask = bit;
108
    p_irqctrl->irq_lock = 0;
109
 
110
 
111
    while (1) {
112
        if (!was_interrupt) {
113
            continue;
114
        }
115
        was_interrupt = 0;
116
        ss_len = sprintf(ss, "RISC-V River CPU demo: %d!!!!\n", i++);
117
        print_uart(ss, ss_len);
118
    }
119
}
120
 

powered by: WebSVN 2.1.0

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