Need an example of how to write to an array in the hub from the assembler in a cog.
frank freedman
Posts: 1,985
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
Thanks,
Frank

Comments
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
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
Thanks,
Frank
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 #.loopIf 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?
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.
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
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: 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