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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_gptmr/] [main.c] - Blame information for rev 69

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 67 zero_gravi
// #################################################################################################
2
// # << NEORV32 - General Purpose Timer (GPTMR) Demo Program >>                                    #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2021, 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_gptmr/main.c
38
 * @author Stephan Nolting
39
 * @brief Simple GPTMR 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 gptmr_firq_handler(void);
56
 
57
 
58
/**********************************************************************//**
59
 * This program blinks an LED at GPIO.output(0) at 1Hz using the general purpose timer interrupt.
60
 *
61
 * @note This program requires the GPTMR unit to be synthesized.
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, ho hw flow control
71
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
72
 
73
 
74
  // check if GPTMR unit is implemented at all
75
  if (neorv32_gptmr_available() == 0) {
76
    neorv32_uart0_print("General purpose timer not implemented!\n");
77
    return 1;
78
  }
79
 
80
  // Intro
81
  neorv32_uart0_print("General purpose timer (GPTMR) demo Program.\n"
82
                      "Toggles GPIO.output(0) at 1Hz using the GPTMR interrupt.\n\n");
83
 
84
 
85
  // clear GPIO output port
86
  neorv32_gpio_port_set(0);
87
 
88
 
89
  // install GPTMR interrupt handler
90
  neorv32_rte_exception_install(GPTMR_RTE_ID, gptmr_firq_handler);
91
 
92
  // configure timer for 1Hz in continuous mode
93
  uint32_t soc_clock = NEORV32_SYSINFO.CLK;
94
  soc_clock = soc_clock / 2; // divide by two as we are using the 1/2 clock prescaler
95
  neorv32_gptmr_setup(CLK_PRSC_2, 1, soc_clock);
96
 
97
  // enable interrupt
98
  neorv32_cpu_irq_enable(GPTMR_FIRQ_ENABLE); // enable GPTRM FIRQ channel
99
  neorv32_cpu_eint(); // enable global interrupt flag
100
 
101
 
102
  // do nothing, wait for interrupt
103
  while(1);
104
 
105
  return 0;
106
}
107
 
108
 
109
/**********************************************************************//**
110
 * GPTMR FIRQ handler.
111
 *
112
 * @warning This function has to be of type "void xyz(void)" and must not use any interrupt attributes!
113
 **************************************************************************/
114
void gptmr_firq_handler(void) {
115
 
116 69 zero_gravi
  neorv32_cpu_csr_write(CSR_MIP, 1<<GPTMR_FIRQ_PENDING); // clear/ack pending FIRQ
117 67 zero_gravi
 
118
  neorv32_uart0_putc('.'); // send tick symbol via UART
119
  neorv32_gpio_pin_toggle(0); // toggle output port bit 0
120
}

powered by: WebSVN 2.1.0

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