Shop OBEX P1 Docs P2 Docs Learn Events
Need an example of how to write to an array in the hub from the assembler in a cog. — Parallax Forums

Need an example of how to write to an array in the hub from the assembler in a cog.

frank freedmanfrank freedman Posts: 1,983
edited 2012-12-11 20:44 in Propeller 1
Now that the ADC function is working, I am looking for an example of how to write into an array in the hub established by the C part of the program from the high speed asm function in a cog. What I have tried does not quite work.

Thanks,

Frank

Comments

  • jac_goudsmitjac_goudsmit Posts: 418
    edited 2012-11-16 06:14
    Now that the ADC function is working, I am looking for an example of how to write into an array in the hub established by the C part of the program from the high speed asm function in a cog. What I have tried does not quite work.

    You could let the C compiler do the "heavy lifting" of accessing the array:
    // in module.cogc
    #include "header_containing_declaration_for_array.h"
    
    void myfunc(void)
    {
        unsigned index;
    
        for (index = 0; index < num_elements; index++)
        {
            int value; // will be compiled as cog memory variable or pseudo register
    
            __volatile__ __asm__ {
            // ... calculate value
            };
    
            array[index] = value;
        }
    }
    

    Once you get this code to compile, you can read the assembler that the compiler generates, and replace the "array[index] = value" line in the source code by assembly code if it's too inefficient.

    ===Jac
  • ersmithersmith Posts: 6,092
    edited 2012-11-16 08:46
    Now that the ADC function is working, I am looking for an example of how to write into an array in the hub established by the C part of the program from the high speed asm function in a cog. What I have tried does not quite work.

    Thanks,

    Frank

    Just plain wrlong (or wrbyte) should work fine. You'll need to get the address of the array into a register, of course -- that can be done either by passing the array address in PAR, or by using the GAS assembler and accessing the array label directly. See the gas_toggle demo for an example of the latter (it uses some integer variables rather than arrays, but the principle is exactly the same).

    Jac's suggestion of looking at the assembler output of the C compiler is also a good one.

    Eric
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-11-23 05:53
    Still getting build failures on this problem. I know there is an example showing how to pass a value in PAR when using a .c master file ans a .s file. It was not hard at all setting up the single values that the pin assignments use though so fsr they are only using a rdlong (haven't tried wrlong to readback /verify pin values). But array address is still not yet understood/working. If anyone has seen any working code to fill an array in hub from a .s file please drop a link in this thread.

    Thanks,
    Frank
  • jazzedjazzed Posts: 11,803
    edited 2012-11-23 15:24
    Still getting build failures on this problem. I know there is an example showing how to pass a value in PAR when using a .c master file ans a .s file. ...

    Hi Frank,

    I added some simple things to the gas_toggle demo and was able to write values back to the mailbox address.
        /* every 2 seconds update the flashing frequency so the
           light blinks faster and faster */
        while(1) {
          sleep(2);
          wait_time =  wait_time >> 1;
          if (wait_time < MIN_GAP)
            wait_time = _clkfreq;
          printf("pins 0x%x\n", pins); // <- add to demo reading from COG
        }
    
    .loop
                    xor     outa, pins          ' toggle pins
                    waitcnt nextcnt, waitdelay  ' wait for half second
            rdlong  waitdelay, wait_addr ' update wait delay
                    wrlong  nextcnt, pins_addr  ' <- add to demo writing a value back.
                    jmp     #.loop
    

    If you need fill an array, you will need to pass the array base address through the mailbox and save the address using the cog code.

    Maybe you can post an example of what you have now?
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-11-23 17:55
    Soon as i get home. Just finishing up with a wayward Axiom Artis cath lab table. Hydraulic lift assy replacement. PITA job, don't think there is s font big enough or bold enough; perhaps color it blue......

    Thanks,

    FF

    what I have:

    clockgen_firmware.s
    acq_firmware.s
    ADC.c

    First real hack in C in many years, not that much time with it to begin with though.
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-12-10 23:02
    Still trying different variations, no luck yet. Has anyone yet written or posted anything using C for the main program and GAS for the highspeed whatevers needed to run fast in an isolated cog? Specifically I am trying to get the cog to be able to write into an array in the hub memory and have that hub memory then be handled by routines in the C code. All under LMM. Just can't seem to get the base address of the array to pass to the cog via PAR.
  • jac_goudsmitjac_goudsmit Posts: 418
    edited 2012-12-11 09:11
    Still trying different variations, no luck yet. Has anyone yet written or posted anything using C for the main program and GAS for the highspeed whatevers needed to run fast in an isolated cog? Specifically I am trying to get the cog to be able to write into an array in the hub memory and have that hub memory then be handled by routines in the C code. All under LMM. Just can't seem to get the base address of the array to pass to the cog via PAR.

    Hi Frank,

    Could you post some code (even if they're only fragments), or perhaps give us a better idea of what you're working on? We might be able to help you better that way. We can't tell you what you're doing wrong if we have to guess what you're doing...

    ===Jac
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-12-11 19:25
    Post #6 what was current at the time I put it there. Let me know if you can not see/open it
    Hi Frank,

    Could you post some code (even if they're only fragments), or perhaps give us a better idea of what you're working on? We might be able to help you better that way. We can't tell you what you're doing wrong if we have to guess what you're doing...

    ===Jac
  • ersmithersmith Posts: 6,092
    edited 2012-12-11 20:44
    The second parameter of cognew() is the value to place into PAR, so for example:
    void acq1_cog(unsigned int *output_array)
    {
       cognew(_load_start_cogacq1, output_array);
    }
    
    would start a COG with PAR containing the address "&output_array[0]". Typically the COG code would look like:
       mov  dest_addr, PAR
       ...
       wrlong intvalue, dest_addr  '' write next value to array
       add     dest_addr, #4       '' point to next array destination
    
    This is a bit simplistic, but should get you started. A better solution would probably be to pass a pointer to a "mailbox" which contains parameters like the destination array and the number of things to put in the array. The cog_c_toggle demo does this, for example; it uses C code on the COG, but you could equally well use GAS code instead.

    Eric
Sign In or Register to comment.