Shop OBEX P1 Docs P2 Docs Learn Events
A few pointers concerning pointer please — Parallax Forums

A few pointers concerning pointer please

idbruceidbruce Posts: 6,197
edited 2011-01-06 10:47 in Propeller 1
Hello Everyone

I have been having a bit of an ordeal since the wee hours of the morning. I had a piece of code that I was working on and trying to implement a few suggestions that were made to me. However, the code has changed a little since the suggestions were made and now it is more difficult to implement.

My problem as the title suggests, relates to pointers. Please keep in mind that this code was working before making the lastest changes.


If you look at the code below, you will find CogDriveStepPin being called from the Main fuction. This call is passing several parameters, but there are different situations, which are:
  • Several parameters remain within the called method.
  • Several parameters pass right on through to another method. COGNEW(DriveStepPin......
  • One parameter is periodically modified, but a pointer to this parameter is passed to another method. Can you please point out the pointer mistakes. COGNEW(DriveStepPin.....
{
  This code is based upon a sample given by MagIO2, and with the assistance of JonnyMac.
  I thank them both of them, with my utmost appreciation.
}
CON
  _CLKMODE = XTAL1 + PLL16X
  _XINFREQ = 5_000_000
  STEP_PULSE_PIN_1 = 3
VAR
  LONG StepPulseWidth1
  LONG CycleOffset1
  LONG DriveStack1[10]
PUB Main
  StepPulseWidth1 := CLKFREQ / 500_000
  CycleOffset1 := 0
  CogDriveStepPin(@DriveStack1, CycleOffset1, STEP_PULSE_PIN_1, StepPulseWidth1, 100_000, 8_000, 2_000, 10)
PUB CogDriveStepPin(DriveStack, CycleOffset, StepPulsePin, StepPulseWidth,{
}     Steps, StartingSpeed, RunningSpeed, RampMultiplier) | {
}     StepRemainders, DefaultMaxSpeed, DefaultWaitTime
  DefaultWaitTime := 4_000
  DefaultMaxSpeed := 2_000
  CycleOffset := StartingSpeed
  StepRemainders := Steps - (((StartingSpeed - RunningSpeed) / RampMultiplier) * 2)  
  COGNEW(DriveStepPin(StepPulsePin, @CycleOffset, @StepPulseWidth), @DriveStack)
 
  ' wait for a while until the stepper COG is loaded and initialized
  WAITCNT(CLKFREQ / 100_000 + cnt )
  StepPulseWidth := -StepPulseWidth  
 
  'Ramp Up
  REPEAT CycleOffset FROM StartingSpeed TO RunningSpeed STEP RampMultiplier
    WAITCNT(DefaultWaitTime + CNT)
  'Maintain High Speed
  REPEAT StepRemainders
    WAITCNT(DefaultWaitTime + CNT)
 
  'Ramp Down
  REPEAT CycleOffset FROM RunningSpeed TO StartingSpeed STEP RampMultiplier
 
    WAITCNT(DefaultWaitTime + CNT)
  'Switch off pulse
  StepPulseWidth := 0
PUB DriveStepPin(StepPulsePin, CycleOffsetPntr, StepPulseWidthPntr) | Counter
  ' Configure Counter A to NCO
  CTRA[30..26] := %00100 
  CTRA[5..0] := StepPulsePin
  FRQA := 1
  DIRA[StepPulsePin]~~
  Counter := CNT
  repeat
    PHSA := LONG[StepPulseWidthPntr]
    WAITCNT(Counter += LONG[CycleOffsetPntr])

Any help that you may provide will be greatly appreciated
Bruce

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-06 08:54
    The problem is the @ in front of DriveStack (last parameter) in this call:

    COGNEW(DriveStepPin(StepPulsePin, @CycleOffset, @StepPulseWidth), @DriveStack)

    The call above this has already put the stack address into DriveStack so you just need to pass this value along. What you're passing here is the address of the local parameter, not the value in it.

    Another comment: If you call the CogDriveStepPin() method more than once you're going to be using another cog. You might want to do this:
    cog := COGNEW(DriveStepPin(StepPulsePin, @CycleOffset, @StepPulseWidth), DriveStack) + 1
    
      ' run the motor
    
      cogstop(cog-1)
    
  • idbruceidbruce Posts: 6,197
    edited 2011-01-06 09:05
    Jon

    I knew it was something simple, been trying to find it since 1:00 AM. I tried EVERYTHING but that. Before this project is finished, I will owe you two vacations.

    Thanks Again Jon

    Bruce
  • mparkmpark Posts: 1,305
    edited 2011-01-06 10:47
    I think there's another potential problem. You're also passing @CycleOffset and @StepPulseWidth, pointers to CogDriveStepPin parameters. A method's parameters and local variables cease to exist once the method returns, but DriveStepPin, running in a different cog, will outlive CogDriveStepPin and therefore will be holding pointers to memory that will be overwritten with another method's locals.

    The program you posted ends right after CogDriveStepPin so you won't see anything unexpected, but if you do more stuff after CogDriveStepPin, who knows what DriveStepPin will do.
Sign In or Register to comment.