1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < neo430_exirqt.c - External interrupts controler driver functions > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2020, 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 NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
#include "neo430.h"
|
36 |
|
|
#include "neo430_exirq.h"
|
37 |
|
|
|
38 |
|
|
// Private variables
|
39 |
|
|
static uint16_t neo430_exirq_vectors[8] __attribute__((unused)); // do not ouput a warning when this variable is unused
|
40 |
|
|
|
41 |
|
|
// Private function prototypes
|
42 |
|
|
static void __attribute__((__interrupt__)) _exirq_irq_handler_(void);
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
/* ------------------------------------------------------------
|
46 |
|
|
* INFO Enable external interrupts controller
|
47 |
|
|
* ------------------------------------------------------------ */
|
48 |
|
|
void neo430_exirq_enable(void) {
|
49 |
|
|
|
50 |
|
|
EXIRQ_CT |= (1<<EXIRQ_CT_EN);
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
/* ------------------------------------------------------------
|
55 |
|
|
* INFO Disable external interrupts controller
|
56 |
|
|
* ------------------------------------------------------------ */
|
57 |
|
|
void neo430_exirq_disable(void) {
|
58 |
|
|
|
59 |
|
|
EXIRQ_CT &= ~(1<<EXIRQ_CT_EN);
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/* ------------------------------------------------------------
|
64 |
|
|
* INFO Configure external interrupts controller
|
65 |
|
|
* PARAM Struct containing the handler functions addresses and the channel enable bits
|
66 |
|
|
* ------------------------------------------------------------ */
|
67 |
|
|
void neo430_exirq_config(struct neo430_exirq_config_t config) {
|
68 |
|
|
|
69 |
|
|
// reset controller
|
70 |
|
|
EXIRQ_CT = 0;
|
71 |
|
|
|
72 |
|
|
// get handler function address
|
73 |
|
|
neo430_exirq_vectors[0] = config.address[0];
|
74 |
|
|
neo430_exirq_vectors[1] = config.address[1];
|
75 |
|
|
neo430_exirq_vectors[2] = config.address[2];
|
76 |
|
|
neo430_exirq_vectors[3] = config.address[3];
|
77 |
|
|
neo430_exirq_vectors[4] = config.address[4];
|
78 |
|
|
neo430_exirq_vectors[5] = config.address[5];
|
79 |
|
|
neo430_exirq_vectors[6] = config.address[6];
|
80 |
|
|
neo430_exirq_vectors[7] = config.address[7];
|
81 |
|
|
|
82 |
|
|
// set correct CPU external interrupts request handler address
|
83 |
|
|
IRQVEC_EXT = (uint16_t)(&_exirq_irq_handler_);
|
84 |
|
|
|
85 |
|
|
// configure channel enables
|
86 |
|
|
uint16_t enable = (uint16_t)config.enable;
|
87 |
|
|
EXIRQ_CT = (enable<<EXIRQ_CT_IRQ0_EN);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
/* ------------------------------------------------------------
|
92 |
|
|
* INFO Trigger IRQ channel by software
|
93 |
|
|
* PARAM id (0..7) indicating which (ENABLED!) channel shall be triggered
|
94 |
|
|
* ------------------------------------------------------------ */
|
95 |
|
|
void neo430_exirq_sw_irq(uint8_t id) {
|
96 |
|
|
|
97 |
|
|
uint16_t irq_sel = (uint16_t)(id & 7);
|
98 |
|
|
|
99 |
|
|
// apply sw irq enable bit and according irq select
|
100 |
|
|
uint16_t exirq_ctrl = EXIRQ_CT & (~(0b111 << EXIRQ_CT_SEL0)); // clear IRQ src output
|
101 |
|
|
EXIRQ_CT = exirq_ctrl | (1<<EXIRQ_CT_SW_IRQ) | (irq_sel<<EXIRQ_CT_SEL0); // set dst. IRQ and set SW_IRQ flag
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
/* ------------------------------------------------------------
|
106 |
|
|
* INFO Actual external interrupts controller IRQ handler
|
107 |
|
|
* INFO This function is automatically installed
|
108 |
|
|
* ------------------------------------------------------------ */
|
109 |
|
|
static void __attribute__((__interrupt__)) _exirq_irq_handler_(void) {
|
110 |
|
|
|
111 |
|
|
register uint16_t exirq_ctrl = EXIRQ_CT;
|
112 |
|
|
|
113 |
|
|
EXIRQ_CT = exirq_ctrl | (1 << EXIRQ_CT_ACK_IRQ); // ACK IRQ
|
114 |
|
|
|
115 |
|
|
register uint16_t src = exirq_ctrl & (0b111 << EXIRQ_CT_SEL0); // get IRQ source
|
116 |
|
|
register uint16_t adr = neo430_exirq_vectors[src];
|
117 |
|
|
asm volatile ("call %0" : : "r" (adr)); // call according handler
|
118 |
|
|
}
|