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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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