Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ISRwrapper.inc
1/* ISRwrapper.inc - PIC32MX ISR wrapper code - macro ISR_wrapper
2**
3** Define ISR wrapper and vector branch code to support "C" ISR.
4** For a given vector_XX, create wrapper code to direct vector
5** XX to the named C-language ISR.
6**
7** To use with any Microchip headers (not recommended),
8** compile with "-fdollars-in-identifiers" option.
9**
10** History:
11** 20090505 DRNadler: Original coding
12**
13**
14** Copyright (c) 2009 Dave Nadler
15**
16** The authors hereby grant permission to use, copy, modify, distribute,
17** and license this software and its documentation for any purpose, provided
18** that existing copyright notices are retained in all copies and that this
19** notice is included verbatim in any distributions. No written agreement,
20** license, or royalty fee is required for any of the authorized uses.
21** Modifications to this software may be copyrighted by their authors
22** and need not follow the licensing terms described here, provided that
23** the new terms are clearly indicated on the first page of each file where
24** they apply.
25*/
26
27 .macro RAW_ISR_wrapper _XX: req,C_PURE_ISR_NAME:req
28 .globl \C_PURE_ISR_NAME
29 .section .vector_\_XX,"ax",%progbits
30 j \C_PURE_ISR_NAME
31 /* We have a bunch of NOPs here to ensure that if anyone else has
32 any code in the .vector_NN section then we trigger a linker error */
33 nop
34 nop
35 nop
36 nop
37 nop
38
39
40 .endm
41
42 .macro ISR_wrapper _XX:req,C_ISR_NAME:req
43
44 RAW_ISR_wrapper \_XX,vector_\_XX\‍()_ISR_wrapper
45
46 /*
47 ** First, fill in interrupt vector to branch to the ISR wrapper
48 */
49 /*
50 ** Second, create an ISR wrapper that calls the C-language routine
51 */
52 .section .text,"ax",%progbits
53 .align 2
54 .set nomips16
55 .globl vector_\_XX\()_ISR_wrapper
56 .type vector_\_XX\‍()_ISR_wrapper, %function
57 .ent vector_\_XX\()_ISR_wrapper
58vector_\_XX\‍()_ISR_wrapper:
59 .frame $fp,120,$31 # vars= 16, regs= 18/0, args= 16, gp= 0
60 .mask 0xc300fffc,-20
61 .fmask 0x00000000,0
62 .set noreorder
63 .set nomacro
64
65 rdpgpr $sp, $sp
66 mfc0 $k0, $13 /* read CAUSE register */
67 mfc0 $k1, $14 /* read EPC register */
68 addiu $sp, $sp, -120 /* 30 (!) words space on stack */
69 sw $k1, 116($sp) /* save EPC on stack */
70 mfc0 $k1, $12 /* read STATUS register */
71 srl $k0, $k0, 10 /* align RIPL to bit 0 */
72 sw $k1, 112($sp) /* save STATUS on stack */
73 ins $k1, $k0, 10, 6 /* insert RIPL to IPL field */
74 mflo $k0
75 sw $k0, 108($sp)
76 mfhi $k0
77 sw $k0, 104($sp)
78 ins $k1, $0, 1, 4
79 mtc0 $k1, $12 /* write STATUS register */
80 sw $31,100($sp)
81 sw $fp,96($sp)
82 sw $25,92($sp)
83 sw $24,88($sp)
84 sw $15,84($sp)
85 sw $14,80($sp)
86 sw $13,76($sp)
87 sw $12,72($sp)
88 sw $11,68($sp)
89 sw $10,64($sp)
90 sw $9,60($sp)
91 sw $8,56($sp)
92 sw $7,52($sp)
93 sw $6,48($sp)
94 sw $5,44($sp)
95 sw $4,40($sp)
96 sw $3,36($sp)
97 sw $2,32($sp)
98 move $fp,$sp
99
100 /* Finally, call the C-Language ISR */
101 jal \C_ISR_NAME /* jal stores return address in $31, already saved... */
102 nop
103
104 move $sp,$fp
105 lw $31,100($sp)
106 lw $fp,96($sp)
107 lw $25,92($sp)
108 lw $24,88($sp)
109 lw $15,84($sp)
110 lw $14,80($sp)
111 lw $13,76($sp)
112 lw $12,72($sp)
113 lw $11,68($sp)
114 lw $10,64($sp)
115 lw $9,60($sp)
116 lw $8,56($sp)
117 lw $7,52($sp)
118 lw $6,48($sp)
119 lw $5,44($sp)
120 lw $4,40($sp)
121 lw $3,36($sp)
122 lw $2,32($sp)
123 di /* disable interrupts - just in case ? where enabled ? */
124 ehb
125 lw $k0, 108($sp)
126 mtlo $k0
127 lw $k0, 104($sp)
128 mthi $k0
129 lw $k0, 116($sp) /* restore EPC from stack */
130 mtc0 $k0, $14
131 lw $k0, 112($sp)
132 addiu $sp, $sp, 120 /* restore stack pointer */
133 mtc0 $k0, $12
134 eret
135 .set macro
136 .set reorder
137 .end vector_\_XX\()_ISR_wrapper
138
139 .endm
140
141/* Header file in which portSAVE_CONTEXT and portRESTORE_CONTEXT are defined. */
142#include "ISR_Support.h"
143
144 .macro FREERTOS_ISR_wrapper _XX:req,C_ISR_NAME:req
145
146 RAW_ISR_wrapper \_XX,\C_ISR_NAME\()_wrapper
147
148 .section .rodata.\C_ISR_NAME\()_vector_number
149 .align 4
150 .globl \C_ISR_NAME\()_vector_number
151 .type \C_ISR_NAME\()_vector_number, %object
152\C_ISR_NAME\()_vector_number:
153 .4byte \_XX
154
155 .section .text,"ax",%progbits
156 .align 2
157 .set nomips16
158 .set noreorder
159 .set noat
160 .globl \C_ISR_NAME\()_wrapper
161 .type \C_ISR_NAME\()_wrapper, %function
162 .ent \C_ISR_NAME\()_wrapper
163\C_ISR_NAME\()_wrapper:
164 portSAVE_CONTEXT
165 jal \C_ISR_NAME
166 nop
167 portRESTORE_CONTEXT
168
169 .set macro
170 .set reorder
171 .end \C_ISR_NAME\()_wrapper
172
173 .endm
174