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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_xirq.c] - Blame information for rev 61

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

Line No. Rev Author Line
1 61 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_xirq.c - External Interrupt controller HW Driver >>                       #
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 neorv32_xirq.h
38
 * @author Stephan Nolting
39
 * @brief External Interrupt controller HW driver source file.
40
 **************************************************************************/
41
 
42
#include "neorv32.h"
43
#include "neorv32_xirq.h"
44
 
45
 
46
/**********************************************************************//**
47
 * The >private< trap vector look-up table of the XIRQ.
48
 **************************************************************************/
49
static uint32_t __neorv32_xirq_vector_lut[32] __attribute__((unused)); // trap handler vector table
50
 
51
// private functions
52
static void __attribute__((aligned(16))) __attribute__((unused)) __neorv32_xirq_core(void);
53
static void __attribute__((unused)) __neorv32_xirq_dummy_handler(void);
54
 
55
 
56
/**********************************************************************//**
57
 * Check if external interrupt controller was synthesized.
58
 *
59
 * @return 0 if XIRQ was not synthesized, 1 if EXTIRQ is available.
60
 **************************************************************************/
61
int neorv32_xirq_available(void) {
62
 
63
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_XIRQ)) {
64
    return 1;
65
  }
66
  else {
67
    return 0;
68
  }
69
}
70
 
71
 
72
/**********************************************************************//**
73
 * Initialize XIRQ controller.
74
 *
75
 * @note All interrupt channels will be deactivated, all pending IRQs will be deleted and all
76
 * handler addresses will be deleted.
77
 * @return 0 if success, 1 if error.
78
 **************************************************************************/
79
int neorv32_xirq_setup(void) {
80
 
81
  XIRQ_IER = 0; // disable all input channels
82
  XIRQ_IPR = 0xffffffff; // clear/ack all pending IRQs
83
 
84
  int i;
85
  for (i=0; i<32; i++) {
86
    __neorv32_xirq_vector_lut[i] = (uint32_t)(&__neorv32_xirq_dummy_handler);
87
  }
88
 
89
  // register XIRQ handler in RTE
90
  return neorv32_rte_exception_install(XIRQ_RTE_ID, __neorv32_xirq_core);
91
}
92
 
93
 
94
/**********************************************************************//**
95
 * Globally enable XIRQ interrupts (via according FIRQ channel).
96
 **************************************************************************/
97
void neorv32_xirq_global_enable(void) {
98
 
99
  // enable XIRQ fast interrupt channel
100
  neorv32_cpu_irq_enable(XIRQ_FIRQ_ENABLE);
101
}
102
 
103
 
104
/**********************************************************************//**
105
 * Globally disable XIRQ interrupts (via according FIRQ channel).
106
 **************************************************************************/
107
void neorv32_xirq_global_disable(void) {
108
 
109
  // enable XIRQ fast interrupt channel
110
  neorv32_cpu_irq_disable(XIRQ_FIRQ_ENABLE);
111
}
112
 
113
 
114
/**********************************************************************//**
115
 * Get number of implemented XIRQ channels
116
 *
117
 * @return Number of implemented channels (0..32).
118
 **************************************************************************/
119
int neorv32_xirq_get_num(void) {
120
 
121
  uint32_t enable;
122
  int i, cnt;
123
 
124
  if (neorv32_xirq_available()) {
125
 
126
    neorv32_cpu_irq_disable(XIRQ_FIRQ_ENABLE); // make sure XIRQ cannot fire
127
    XIRQ_IER = 0xffffffff; // try to set all enable flags
128
    enable = XIRQ_IER; // read back actually set flags
129
 
130
    // count set bits in enable
131
    cnt = 0;
132
    for (i=0; i<32; i++) {
133
      if (enable & 1) {
134
        cnt++;
135
      }
136
      enable >>= 1;
137
    }
138
    return cnt;
139
  }
140
  else {
141
    return 0;
142
  }
143
}
144
 
145
 
146
/**********************************************************************//**
147
 * Install exception handler function for XIRQ channel.
148
 *
149
 * @note This will also activate the according XIRQ channel and clear a pending IRQ at this channel.
150
 *
151
 * @param[in] ch XIRQ interrupt channel (0..31).
152
 * @param[in] handler The actual handler function for the specified exception (function MUST be of type "void function(void);").
153
 * @return 0 if success, 1 if error.
154
 **************************************************************************/
155
int neorv32_xirq_install(uint8_t ch, void (*handler)(void)) {
156
 
157
  // channel valid?
158
  if (ch < 32) {
159
    __neorv32_xirq_vector_lut[ch] = (uint32_t)handler; // install handler
160
    uint32_t mask = 1 << ch;
161
    XIRQ_IPR = mask; // clear if pending
162
    XIRQ_IER |= mask; // enable channel
163
    return 0;
164
  }
165
  return 1;
166
}
167
 
168
 
169
/**********************************************************************//**
170
 * Uninstall exception handler function for XIRQ channel.
171
 *
172
 * @note This will also deactivate the according XIRQ channel and clear pending state.
173
 *
174
 * @param[in] ch XIRQ interrupt channel (0..31).
175
 * @return 0 if success, 1 if error.
176
 **************************************************************************/
177
int neorv32_xirq_uninstall(uint8_t ch) {
178
 
179
  // channel valid?
180
  if (ch < 32) {
181
    __neorv32_xirq_vector_lut[ch] = (uint32_t)(&__neorv32_xirq_dummy_handler); // override using dummy handler
182
    uint32_t mask = 1 << ch;
183
    XIRQ_IER &= ~mask; // disable channel
184
    XIRQ_IPR = mask; // clear if pending
185
    return 0;
186
  }
187
  return 1;
188
}
189
 
190
 
191
/**********************************************************************//**
192
 * This is the actual second-level IRQ handler for the XIRQ. It will call the previously installed handler
193
 * if an XIRQ fires.
194
 *
195
 * @note This function must no be used by the user.
196
 **************************************************************************/
197
static void __attribute__((aligned(16))) __attribute__((unused)) __neorv32_xirq_core(void) {
198
 
199
  register uint32_t src = XIRQ_SCR; // get IRQ source (with highest priority)
200
  src &= 0x1f;
201
 
202
  XIRQ_IPR = (uint32_t)(1 << src); // acknowledge pending interrupt
203
 
204
  // execute handler
205
  register uint32_t xirq_handler = __neorv32_xirq_vector_lut[src];
206
  void (*handler_pnt)(void);
207
  handler_pnt = (void*)xirq_handler;
208
  (*handler_pnt)();
209
}
210
 
211
 
212
/**********************************************************************//**
213
 * XIRQ dummy handler.
214
 **************************************************************************/
215
static void __attribute__((unused)) __neorv32_xirq_dummy_handler(void) {
216
 
217
  asm volatile ("nop");
218
}
219
 

powered by: WebSVN 2.1.0

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