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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_mtime/] [main.c] - Blame information for rev 72

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 72 zero_gravi
// #################################################################################################
2
// # << NEORV32 - RISC-V Machine Timer (MTIME) Demo Program >>                                     #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2022, 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 demo_mtime/main.c
38
 * @author Stephan Nolting
39
 * @brief Simple machine timer (MTIME) usage example.
40
 **************************************************************************/
41
 
42
#include <neorv32.h>
43
 
44
 
45
/**********************************************************************//**
46
 * @name User configuration
47
 **************************************************************************/
48
/**@{*/
49
/** UART BAUD rate */
50
#define BAUD_RATE 19200
51
/**@}*/
52
 
53
 
54
// Prototypes
55
void mtime_irq_handler(void);
56
 
57
 
58
/**********************************************************************//**
59
 * This program blinks an LED at GPIO.output(0) at 1Hz using the machine timer interrupt.
60
 *
61
 * @note This program requires the MTIME unit to be synthesized (and UART0 and GPIO).
62
 *
63
 * @return Should not return;
64
 **************************************************************************/
65
int main() {
66
 
67
  // capture all exceptions and give debug info via UART
68
  neorv32_rte_setup();
69
 
70
  // init UART at default baud rate, no parity bits, no hw flow control
71
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
72
 
73
 
74
  // check if MTIME unit is implemented at all
75
  if (neorv32_mtime_available() == 0) {
76
    neorv32_uart0_print("ERROR! MTIME timer not implemented!\n");
77
    return 1;
78
  }
79
 
80
  // Intro
81
  neorv32_uart0_print("RISC-V Machine System Timer (MTIME) demo Program.\n"
82
                      "Toggles GPIO.output(0) at 1Hz using the RISC-V 'MTI' interrupt.\n\n");
83
 
84
 
85
  // clear GPIO output port
86
  neorv32_gpio_port_set(0);
87
 
88
 
89
  // install MTIME interrupt handler to RTE
90
  neorv32_rte_exception_install(RTE_TRAP_MTI, mtime_irq_handler);
91
 
92
  // configure MTIME timer's first interrupt to appear after SYSTEM_CLOCK / 2 cycles (toggle at 2Hz)
93
  // starting from _now_
94
  neorv32_mtime_set_timecmp(neorv32_mtime_get_time() + (NEORV32_SYSINFO.CLK / 2));
95
 
96
  // enable interrupt
97
  neorv32_cpu_irq_enable(CSR_MIE_MTIE); // enable MTIME interrupt
98
  neorv32_cpu_eint(); // enable global interrupt flag
99
 
100
 
101
  // go to sleep mode and wait for interrupt
102
  while(1) {
103
    neorv32_cpu_sleep();
104
  }
105
 
106
  return 0;
107
}
108
 
109
 
110
/**********************************************************************//**
111
 * MTIME IRQ handler.
112
 *
113
 * @warning This function has to be of type "void xyz(void)" and must not use any interrupt attributes!
114
 **************************************************************************/
115
void mtime_irq_handler(void) {
116
 
117
  // update MTIMECMP value for next IRQ (in SYSTEM_CLOCK / 2 cycles)
118
  // this will also ack/clear the current MTIME interrupt request
119
  neorv32_mtime_set_timecmp(neorv32_mtime_get_timecmp() + (NEORV32_SYSINFO.CLK / 2));
120
 
121
 
122
  neorv32_uart0_putc('.'); // send tick symbol via UART
123
  neorv32_gpio_pin_toggle(0); // toggle output port bit 0
124
}

powered by: WebSVN 2.1.0

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