• AVR Freaks

Helpful ReplyHot!dspic33ck problems using __builtin_add in interrupt routine

Author
Michiel_D
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/01/28 01:50:53
  • Location: 0
  • Status: offline
2019/08/22 01:17:04 (permalink)
0

dspic33ck problems using __builtin_add in interrupt routine

Hello,

When I use the __builtin_add function in the mainloop it works fine, but when I try to use it in an interrupt routine it doesn't work. The __builtin_sac function however works fine in an interrupt routine. Can someone help me out? See my code below.
 
Thanks in advance!

register int accumA asm("A");
volatile int Vapp_error;
volatile int Vapp_setpoint = 208;
volatile int Vapp_error_idt;

void __attribute__((interrupt, no_auto_psv))_ADCAN19Interrupt(void)
{
    Vapp_setpoint = ADCBUF19;
    Vapp_error = ADCBUF19-Vapp_setpoint;
    accumA = __builtin_add(accumA,Vapp_error,0);
    Vapp_error_idt = __builtin_sac(accumA, 6);
    _ADCAN19IF = 0;
}



#1
Michiel_D
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/01/28 01:50:53
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 00:13:03 (permalink)
0
It seems the compiler pushes the ACCUM and other DSP registers to stack and restores them at the end of the interrupt routine. Is there any way to stop the compiler from doing this?
#2
du00000001
Just Some Member
  • Total Posts : 3064
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 01:20:26 (permalink)
0
Saving all registers affected by the ISR prior executing the ISR code, restoring prior exiting the ISR is standard.
 
Do you expect the ISR to work with register contents that were written by non-ISR code?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#3
Michiel_D
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/01/28 01:50:53
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 02:04:49 (permalink)
0
du00000001
Saving all registers affected by the ISR prior executing the ISR code, restoring prior exiting the ISR is standard.
 
Do you expect the ISR to work with register contents that were written by non-ISR code?



Yes I expect it to work. I'm not using the DSP registers outside the ISR, I'm only using DSP code in the ISR.
#4
du00000001
Just Some Member
  • Total Posts : 3064
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 02:20:12 (permalink)
0
If you expect these registers to keep their content from one ISR call to the next: this won't happen automatically!
But you can save the contents to some variables prior exiting, restoring on entry of the ISR.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#5
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3292
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 02:56:19 (permalink)
0
It could be a compiler bug or your code.
 
"Built-in" inserts the code in-line, DSP instructions work with register groups, the builtin code moves data into the correct register. W4 and W5 in this case.
subr    M_MicroStep_Cnt,wreg
mov     [w0],w4
mac     w4*w5,B

B = B + W4 * W5
 
"mpy" in one cycle including any pre-fetch for the next operation.
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#6
JPortici
Super Member
  • Total Posts : 771
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 02:57:08 (permalink) ☄ Helpfulby Michiel_D 2019/08/26 05:21:26
0
If you intend to never modify the accumulators outside of the ISR, maybe you could take advantage of the automatic context switching.
in the dsPIC33CK you have several instances of the core register and the CPU can switch between these context.
 
In your case, in that particular context, it would effectively be as if the CPU was always looping through the ISR and if you inform the compiler that the ONLY function in that context was the ISR it should produce no code to save/restore the register .
 
In theory, as i have never used it on my own, but saw it applied in the digital compensator code examples
look for the context attribute in the XC16 manual, also for the "INTERRUPT SERVICE ROUTINE CONTEXT SAVING" chapter
#7
Michiel_D
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/01/28 01:50:53
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 03:11:34 (permalink)
0
Thank you for your answer.
But all this storing and restoring takes too much of time(I'm running the interrupt routine at 500kHz), and isn't necessary for me. I want the value of these registers to be kept during entry and exit of the ISR so I don't have to store and restore them myself. My question is if there is a way to do this?


I found a thread describing a similar problem:
https://www.microchip.com/forums/m462100.aspx
 
I know it could be done by coding the ISR routine in assembly. But I don't have sufficient experience with assembly. However examples are welcome.
#8
Michiel_D
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/01/28 01:50:53
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 03:12:40 (permalink)
0
Jack_M
If you intend to never modify the accumulators outside of the ISR, maybe you could take advantage of the automatic context switching.
in the dsPIC33CK you have several instances of the core register and the CPU can switch between these context.
 
In your case, in that particular context, it would effectively be as if the CPU was always looping through the ISR and if you inform the compiler that the ONLY function in that context was the ISR it should produce no code to save/restore the register .
 
In theory, as i have never used it on my own, but saw it applied in the digital compensator code examples
look for the context attribute in the XC16 manual, also for the "INTERRUPT SERVICE ROUTINE CONTEXT SAVING" chapter



Thanks! I will look into that!
#9
JPortici
Super Member
  • Total Posts : 771
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 03:23:58 (permalink)
0
The digital compensator examples also have ISRs in assembly.
post edited by JPortici - 2019/08/26 04:55:24
#10
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3292
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 03:33:08 (permalink) ☄ Helpfulby Michiel_D 2019/08/26 05:21:57
0
instead of using built-in, try inline asm which is basically the same but more control.
 
If it was me, I'd write the whole function in asm.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#11
nice
Super Member
  • Total Posts : 1089
  • Reward points : 0
  • Joined: 2004/09/18 11:42:25
  • Location: Germany
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 13:16:00 (permalink)
0
Michiel_D
It seems the compiler pushes the ACCUM and other DSP registers to stack and restores them at the end of the interrupt routine. Is there any way to stop the compiler from doing this?


 
Yes, use the context attribute
void __attribute__ ((interrupt, context, no_auto_psv)) _ADCAN7Interrupt (void)
{
    // …
}

and assign one of the available sets of alternate registers to the interrupt priority your ISR uses by e.g.
#pragma config CTXT1 = IPL7             // Specifies Interrupt Priority Level (IPL) Associated to Alternate Working Register 1 bits (Alternate Register set assigned to IPL level 7)

if the IPL of your ISR is set to 7.


#12
nice
Super Member
  • Total Posts : 1089
  • Reward points : 0
  • Joined: 2004/09/18 11:42:25
  • Location: Germany
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/26 13:34:25 (permalink)
0
Jack_M
If you intend to never modify the accumulators outside of the ISR, maybe you could take advantage of the automatic context switching.


One could even modify and read them outside of the ISR. That’s what the CTXTSWP instruction is for.
#13
Michiel_D
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/01/28 01:50:53
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/29 08:02:32 (permalink)
0
Could you provide me with a link to a digital compensator example? I can't find seem to find one on the microchip website. Thanks!
#14
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3292
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/29 09:15:15 (permalink)
0
These are 1cyc instructions that  _builtin_ inserts inline and it will add extra code to load the correct register set for DSP ops.
 
The accumulators are mapped into memory: ACCxU, ACCxH, ACCxL
 
In another context so to speak they are temporary or dynamic like W0 - W15
 
Result: z = ACC + x * y ;
 
No more contexting ideas, it's out of context, take the result from the operation.
 
These DSP opcodes can also fetch data for the next op.
If you did want the result
 
unsigned int acc[6]; //A dump
int multxy(int X, int Y, unsigned int R[6]); //C prototype;
 
.equ ptr, w2
 
_multxy: .globl _multxy
    mov.d    w0,w4
    mpy      w4 * w5,A
    sac      A,#0,[ptr++]
    sac.d    A,#0,[ptr++]
       mov      #ACCAU,acc
    repeat   #3-1
    mov      [acc++],[ptr++] ;40bit rset
    retlw    #A_OK,w0

post edited by Gort2015 - 2019/08/29 09:25:37

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#15
nice
Super Member
  • Total Posts : 1089
  • Reward points : 0
  • Joined: 2004/09/18 11:42:25
  • Location: Germany
  • Status: offline
Re: dspic33ck problems using __builtin_add in interrupt routine 2019/08/29 10:17:29 (permalink)
0
Michiel_D
Could you provide me with a link to a digital compensator example? I can't find seem to find one on the microchip website. Thanks!


Download the „dsPIC33CH Curiosity Board Demo“ here.
#16
Jump to:
© 2019 APG vNext Commercial Version 4.5