Shop OBEX P1 Docs P2 Docs Learn Events
Passing Memory Address, I am stumped! — Parallax Forums

Passing Memory Address, I am stumped!

RS_JimRS_Jim Posts: 1,768
edited 2011-04-21 07:23 in Propeller 1
I have a program that I want to do that requires several cogs to read a global var while another cog updates that same var. Basicly a timer that is updated by one cog and read by several others. I used Jonny Mac's jm_etimer from OBEX and modified it so that the pasm routine loads the pointers to the timer from the init var I use in coginit.
Var
Long Timer[6]
okay := cog := cognew(@entry,@timer) + 1
DAT org 0
entry mov tmp1,PAR
mov mspntr,tmp1
add tmp1,#4
mov scpntr,tmp1
ETC
That all works Great. However when I attempt to pass the pointer from the top object, it fails.

top obj
VAR
mytimer
OBJ
et: "filename"

Pub Main
et.init(16,@mytimer)


filname
pub init(p,timer) | okay

'' Starts event timer driver
'' -- p is "heartbeat" pin (use -1 to disable)

finalize ' stop if already running

timer[5]:= clkfreq / 1_000 ' cnt ticks per millisecond
' if (p => 0) and (p =< 27) ' if valid pin (protect rx, tx, i2c)
hbmask := 1 << p ' create mask
' enable timer

okay := cog := cognew(@entry,timer) + 1 ' start timer cog

return okay

I did not use @timer because I under stood that in passing @mytimer that I had alread passed a pointer to the hub var mytimer. I modify the top program to just directly display the results of myvar assuming that cog "filename" has correctly written the results into mytimer. I observe two problems, first the heartbeat turns on and stays on and second the results printout in pst are all zeros. I am fairly comfortable in writing PSAM routines (more so than spin) but this global VAR passing has me stumped! I wished in his spin zone article where jonny mac talked about his joysticks where he passed the vars thusly : init:joy (dio,cio,clk,@joysticks) he had shown what the other end looked like Pub joy(d,c,k,VAR)

Here are my files that don't work
jm_etimer_demo - Archive [Date 2011.01.21 Time 06.22].zip


thanks for the help
RS_Jim

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-21 08:30
    I redesigned the timer so that you pass the address of an array instead of having the array in the timer object. With the registers as globals (declared by the top object) other spin cogs can have direct access to these registers (and the methods in the object as well).

    In the attached demo a secondary Spin cog will flash an LED when the 1s digit of the seconds register is 5 and reset the timer at the 1 minute mark. The main cog is simply displaying the running time.

    Note: I changed the etimer clear() method to reset().

    See updated files below
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-01-21 08:42
    RS_Jim,

    One thing I noticed in your code in the init method is:
      timer[5]:= clkfreq / 1_000                                   ' cnt ticks per   millisecond 
    
    should be changed to
      long[timer][5]:= clkfreq / 1_000                           ' cnt ticks per   millisecond 
    
    it looks like you are passing the pointer okay. You need to remember you are dealing with pointer on the other end.

    Duane

    Edit: I see Jon answered while I was reading you code. He has probably got it all fixed for you.
  • RS_JimRS_Jim Posts: 1,768
    edited 2011-01-21 09:21
    OK,
    I tested Jon's new code and the blinkers are working fine. I did change the pin #s of the pins for dasblinker and that cog is running fine. However, the terminal obj is just printing garbage. I just realize by rereading the code that the printing method is reading directly but I cnt see it because I am printing garbage. will try and restart pst and see what happens

    Jim
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-21 09:34
    Check the crystal setting -- my demo board has a 6.25MHz crystal in it and I may have neglected to change that to 5 before uploading.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-21 09:56
    This is why I don't post things in ObEx right away....

    Had I let the Starbucks kick in I would have use my 1.1 timer code which reveals the address of the timer variables. After loading the object you can call the address method and use that from any Spin object. See attached files (I reset to 5MHz before attaching).
  • RS_JimRS_Jim Posts: 1,768
    edited 2011-01-21 20:08
    Yes, I had one of thoes enlightening moments today as I was wandering through the mouse house when it occured to me that the crystal freq declaration was probably incorrect. V 2 comes closer to what I was hopeing to accomplish in my search for knowledge, but the PSAM pointers were still loaded through spin not through the par register. if I were totregs in the init statement okay := cognew(@etimer,tregs) would a readlong tmpntr,PAR contain the address of mytimer from the top method?
    Jim
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-21 21:30
    but the PSAM pointers were still loaded through spin not through the par register.

    That's easily modified but only matters if the assembly code is going to be used with another language. If the object is used with Spin it's no problem.
  • RS_JimRS_Jim Posts: 1,768
    edited 2011-01-22 05:04
    JonnyMac wrote: »
    That's easily modified but only matters if the assembly code is going to be used with another language. If the object is used with Spin it's no problem.

    How about propbasic?
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-22 09:37
    I thought that might be the case. I haven't used PropBASIC in ages so I don't know how you'll connect, but this PASM code does now load through par only, no poking values into the code before calling.

    * jm_etimer__v2_demo.spin
    * jm_etimer_v2.spin
  • RS_JimRS_Jim Posts: 1,768
    edited 2011-01-22 10:39
    Great Jon, Thanks for all the help. I have been struggling with the memory paramater passing for a year!
    RS_Jim
  • RS_JimRS_Jim Posts: 1,768
    edited 2011-04-21 07:23
    Jonny,
    This was so very helpful, how about posting V2 to obex? I did a hack on it that allowed the heartbeat to be every 10 ms as I am syncing an ADC to the hb. Works Great. Thanks again for the help, I have really been able to move forward on my pasm programing since this thread. I love those global VARS!
    Jim
Sign In or Register to comment.