pic33f in-line assembly in Task

ltran2016 wrote on Wednesday, March 16, 2016:

I know this probably isn’t a relevant disucssion, but it might be helpful for someone else later on. I’m attempting to use the the pic33 dsp operation, but I’m encountering some issues using the the built in function multiply-accumulate function. It is really general and does very stupid things that hinder the speed of the mac operation. I know this because I looked at the compiler output assembly listing. To get around this, I’m attempting to use inline assembly in a task. Microchip has a lot of documentation, but in this area it seems to gloss over some important details.

Any help is appreciate. Microchip’s forum is really thin and there doesn’t seem to be anyone who has encountered my problem. Apparently, very few people use mix C and assembly. I’ve exhausted all my options.

How do I access a variable defined on the task stack? I keep getting “variable undefined”. This only happens at

Thanks.

//-------------------------------------------------------------------------
static portTASK_FUNCTION( ADC_MatchFilter_Task, pvParam ) {
//-------------------------------------------------------------------------
   ADC_t         *pADC     = (ADC_t *)pvParam;
   U_INT_t        idx_dma;
   
   __asm__ volatile (
      "mov     _ref_buf, W8              ;\n"\
      "mov     W8, W9                    ;\n"\
      "add     #8, W9                    ;\n"\
      "mov     _dma_buf, W12             ;\n"\    <--- no problem here (defined global)
      "mov     #psvoffset(idx_dma), W2   ;\n"\    <--- problem here
      . . .
      . . . );

rtel wrote on Thursday, March 17, 2016:

This basically GCC, right?

The GCC inline assembler is extremely powerful, but like all things, the more powerful the more complex. This is really more a GCC question than a Microchip question.

There is lots of documentation online, but to find it you first need to know what to search for. Here is an example, which just goes to show how complex it can get: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html … so often it is easier to find a worked example and copy that.

Here is an example taken from the FreeRTOS Cortex-M port, it uses the extended asm syntax to copy a register value into a stack variable. Doing the opposite (reading from a stack variable rather than writing to it) requires different but very similar syntax you will find on the link above:

void vPortValidateInterruptPriority( void )
{
uint32_t ulCurrentInterrupt;
uint8_t ucCurrentPriority;

	/* Obtain the number of the currently executing interrupt. */
	__asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );

   /* rest of code here */
}

ltran2016 wrote on Friday, March 18, 2016:

Thanks for the pointer. I dug somewhat deeper and here’s what I found FYI in case someone asks in the future.

The has to be a modifier at the start of each operand (variable) passed in to and/or out of the inline assembly listing. The value of the operand is relative to the first variable. In my case I’m not passing anything out, so 0% will start with the input operands.

%0 = idx_dma
%1 = idx
%2 = pA
%3 = pB

constraints/modifiers
“g” - indicates general register, memory or immediate integer operand
“T” - indicates near/far data operand

static portTASK_FUNCTION( MF_Task, pvParam ) {

S_INT_t       *pA, *pB;
U_INT_t        idx, idx_dma;
S_INT_t        A [256];
S_INT_t        B [256];

pI = A;
pQ = B;

__asm__ volatile (
   "   mov      #_ref_buf, w8   ;\n" \
   "   mov      w8, w9          ;\n" \
   "   add      #8, w9          ;\n" \
   "   mov      #_dma_buf, w3   ;\n" \
   "   mov      %0, w2          ; copy "idx" variable to w2 \n" \
   "   sl       w2, w0          ;\n" \
   ...
   ...
   ...
   "   mov      %1, w2          ; copy "idx" variable to w2 \n" \
   "   sl       w2, w0          ;\n"
   "   mov      %2, w2          ; copy "pA" variable to w2 \n" \
   "   sac      a, [w0+w2]      ;\n" \
   "   mov      %3, w2          ;copy "pB" variable to w2 \n" \
   "   sac      b, [w0+w2]      ;\n"
   :                        /* output: none */
   : "gT" (idx_dma), "gT" (idx), "gT" (pA), "gT" (pB)  /* input : %0 - %3 */
);