use of #pragma disablecontextsaving

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 32-bit PIC MCUs
Post Reply
Author
Message
nealet
Posts: 142
Joined: 28 May 2013 12:07
Location: South Africa

use of #pragma disablecontextsaving

#1 Post by nealet » 12 Nov 2019 09:19

Good day
I have found an issue where the context saving entry and exit points of an ISR do not save all of the affected registers and cause subsequent exceptions. THis is an extremely disturbing problem in itself. To that end I am trying to write in assembler for the PIC32MX795F512L using the Mikro C Pro compiler, my own context savers for each interrupt that i use. At present I have selected the ICS_OFF Option at the definition of each ISR. so for example instead of saying;

void AriobusCan_Interrupt() iv IVT_EXTERNAL_2 ilevel 5 ics ICS_SOFT {
.... ISR contents....
}

I rather say;

void AriobusCan_Interrupt() iv IVT_EXTERNAL_2 ilevel 5 ics ICS_OFF {
.... ISR contents....
}

This seems to have the correct effect that when the .lst file is checked after compilation, there is no context saving of any registers. My problem comes in when I create an identical context saving for each ISR that the compiler would if I were not using the ICS_OFF option. (I basically copied and pasted the same assembler code that the compiler generates. This needs to EXCLUDE the saving of the Return address which is stored in the RA register as well as any ERET instructions since the compiler puts these in again anyway).

So when I compile using the compilers own generated context saving, the code works as expected. When I use my identical context saving and the ICS_OFF option, the code generates exceptions from the word go.

My question is simply this; The help documentation mentions the use of a pragma that should be used listed as follows:
#pragma disablecontextsaving

Does this need to be used in addition to the ICS_OFF option, because it seems to have no effect when placed at the top of the main() program below all my #include statements as shown below?
Thankyou

#include "uart2.h"
#include "modbus.h"
#include "spi3can.h"
#include "bit.h"
#include "testsnippets.h"

#pragma disablecontextsaving

PS. The original issue of the ISR not saving the correct context is basically as follows;
1) I call a routine in the main loop that passes two parameters, the compiler uses R25 and R26 to pass these parameters. The C -code of this is: unsigned int UART2_CalcCRC(const unsigned short int *MData, unsigned int Mlen)
the assembler version of this is:
;uart2.h, 216 :: ML_LC1 = UART2_CalcCRC(RS485_TX_Buffer, 6);
0x9D00B9A8 0x341A0006 ORI R26, R0, 6
0x9D00B9AC 0x3C19A000 LUI R25, hi_addr(_RS485_TX_Buffer+0)
0x9D00B9B0 0x0F4024C7 JAL _UART2_CalcCRC+0
0x9D00B9B4 0x37395362 ORI R25, R25, lo_addr(_RS485_TX_Buffer+0)
0x9D00B9B8 0xA422DCD0 SH R2, Offset(_ML_LC1+0)(GP)

I then have an ISR that runs fairly often, its context saving code from the .lst file looks like this;

_AriobusCan_Interrupt:
;spi3can.h, 4290 :: void AriobusCan_Interrupt() iv IVT_EXTERNAL_2 ilevel 5 ics ICS_OFF { //ICS_SOFT ICS_OFF
0x9D018DB4 0x27BDFFF8 ADDIU SP, SP, -8
0x9D018DB8 0xAFBF0000 SW RA, 0(SP)
;spi3can.h, 4293 :: asm ADDIU SP, SP, -40
0x9D018DBC 0x27BDFFD8 ADDIU SP, SP, -40
;spi3can.h, 4294 :: asm SW R30, 12(SP)
0x9D018DC0 0xAFBE000C SW R30, 12(SP)
;spi3can.h, 4295 :: asm MFC0 R30, 12, 2
0x9D018DC4 0x401E6002 MFC0 R30, 12, 2
;spi3can.h, 4296 :: asm SW R30, 8(SP)
0x9D018DC8 0xAFBE0008 SW R30, 8(SP)
;spi3can.h, 4297 :: asm MFC0 R30, 14, 0
0x9D018DCC 0x401E7000 MFC0 R30, 14, 0
;spi3can.h, 4298 :: asm SW R30, 4(SP)
0x9D018DD0 0xAFBE0004 SW R30, 4(SP)
;spi3can.h, 4299 :: asm MFC0 R30, 12, 0
0x9D018DD4 0x401E6000 MFC0 R30, 12, 0
;spi3can.h, 4300 :: asm SW R30, 0(SP)
0x9D018DD8 0xAFBE0000 SW R30, 0(SP)
;spi3can.h, 4301 :: asm INS R30, R0, 1, 15
0x9D018DDC 0x7C1E7844 INS R30, R0, 1, 15
;spi3can.h, 4302 :: asm ORI R30, R0, 5120
0x9D018DE0 0x341E1400 ORI R30, R0, 5120
;spi3can.h, 4303 :: asm MTC0 R30, 12, 0
0x9D018DE4 0x409E6000 MTC0 R30, 12, 0
;spi3can.h, 4304 :: asm SW R2, 16(SP)
0x9D018DE8 0xAFA20010 SW R2, 16(SP)
;spi3can.h, 4305 :: asm SW R3, 20(SP)
0x9D018DEC 0xAFA30014 SW R3, 20(SP)
;spi3can.h, 4306 :: asm SW R4, 24(SP)
0x9D018DF0 0xAFA40018 SW R4, 24(SP)
;spi3can.h, 4307 :: asm SW R5, 28(SP)
0x9D018DF4 0xAFA5001C SW R5, 28(SP)
;spi3can.h, 4308 :: asm SW R23, 32(SP)
0x9D018DF8 0xAFB70020 SW R23, 32(SP)
;spi3can.h, 4309 :: asm SW R24, 36(SP)
0x9D018DFC 0xAFB80024 SW R24, 36(SP)

Nowehere in the above code section does it mention R25 or R26, but of you carry on downwards in the natural progression of this same ISR, you will come accross the following code;

;spi3can.h, 4439 :: SPI3_CanRegisterRead(ARIOBUS, READ_REGISTER, RXB0D0, 1); // read RXB0D0
0x9D019144 0x341C0001 ORI R28, R0, 1
0x9D019148 0x341B0066 ORI R27, R0, 102
0x9D01914C 0x341A0003 ORI R26, R0, 3
0x9D019150 0x0F403C81 JAL _SPI3_CanRegisterRead+0
0x9D019154 0x34190001 ORI R25, R0, 1

NOte the disturbing re-use of registers R25 and R26 whose context has not been saved in the ISR context saving code shown above!!!!!!.

nealet
Posts: 142
Joined: 28 May 2013 12:07
Location: South Africa

Re: use of #pragma disablecontextsaving

#2 Post by nealet » 12 Nov 2019 13:00

As seems to be the case these days, i answered my own question and found a solution. Please see the following post:
viewtopic.php?f=164&t=75571

Post Reply

Return to “PIC32 PRO Compilers”