1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #ifndef __riscv
22 #include <string.h>
23 #include "mxc_device.h"
24 #include "nvic_table.h"
25 
26 #if !defined(NVIC_USER_IRQ_OFFSET)
27 #define NVIC_USER_IRQ_OFFSET 16 /**! Offset for device specific IRQs */
28 #endif
29 
30 /* RAM vector_table needs to be aligned with the size of the vector table */
31 #if defined(__ICCARM__)
32 #pragma data_alignment = 512
33 #else
34 __attribute__((aligned(512)))
35 #endif
36 static void (*ramVectorTable[MXC_IRQ_COUNT])(void);
37 
NVIC_SetRAM(void)38 void NVIC_SetRAM(void)
39 {
40 #if defined(__ICCARM__)
41     extern void (*const __isr_vector[])(void);
42 #else
43     /* should be defined in starup_<device>.S */
44     extern uint32_t __isr_vector[MXC_IRQ_COUNT];
45 #endif
46 
47     memcpy(&ramVectorTable, &__isr_vector, sizeof(ramVectorTable));
48     SCB->VTOR = (uint32_t)&ramVectorTable;
49 }
50 
MXC_NVIC_SetVector(IRQn_Type irqn,void (* irq_handler)(void))51 void MXC_NVIC_SetVector(IRQn_Type irqn, void (*irq_handler)(void))
52 {
53     int index = irqn + 16; /* offset for externals */
54 
55     /* If not copied, do copy */
56     if (SCB->VTOR != (uint32_t)&ramVectorTable) {
57         NVIC_SetRAM();
58     }
59 
60     ramVectorTable[index] = irq_handler;
61     NVIC_EnableIRQ(irqn);
62 }
63 
MXC_NVIC_GetVector(IRQn_Type irqn)64 uint32_t MXC_NVIC_GetVector(IRQn_Type irqn)
65 {
66     uint32_t *vectors = (uint32_t *)SCB->VTOR;
67     return vectors[(int32_t)irqn + NVIC_USER_IRQ_OFFSET];
68 }
69 #endif // !__riscv
70