// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

// This software makes use of tools and libraries obtained from open source projects or released for

// use by relevant hardware manufactures. However, this software is NOT a part of any open source project.

// It is released only on a "need to know" basis for beta testers of the "RiceCNC Interpolation Engine".

// Recipents of this source code must respect the confidential nature of this software and prevent any

// distribution that could result in counterfeit copies of the "RiceCNC Interpolation Engine".

// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

#ifndef PowerStepAxis_h

#define PowerStepAxis_h

#include "Energia.h"

#include <Axis.h>

#include <PowerStepDefines.h>

class     PowerStepBuffer;

class     SsiDeviceQueue;

class     OutputPin;

class     PowerStepAxis : public Axis {



          char*          data,

          String*          msgPtr );

    virtual          ~PowerStepAxis();

     uint16_t     slewRates[ 4 ]  = { 320, 75, 110, 260 };

    SSI*                    ssi;

    SsiDeviceQueue*     ssiDeviceQueue;

     protected: // overridden virtual functions


//    virtual bool          isWaitingForMotor();

    virtual bool          usesSSI( SSI* aSsi );

    virtual bool          isConfigured();

    virtual bool


               char*          data,

               String*          msgPtr );

    virtual void          interpolationIsrEnd();

    virtual void          reportAxisStatus(     String* msgPtr );

     virtual void          calculateStopVelocity();

     int32_t                    microStepStopPointI();


     PowerStepConfigReg          powerStepConfigReg;

     PowerStepGateConfig1     powerStepGateConfig1;

     PowerStepGateConfig2     powerStepGateConfig2;

     PowerStepAlarmEnable     powerStepAlarmEnable;

    PowerStepBuffer*     gpBuf;

    PowerStepBuffer*     getStatusCR;

    PowerStepBuffer*     getSpeedCR;

    PowerStepBuffer*     getPosCR;

    PowerStepBuffer*     targetSpeedCR;

    void          configure_dSpin( uint32_t, OutputPin*, OutputPin* );

    bool          initialize(  String*  msg );

    // Callbacks

    void          gpDone(      uint32_t data );

    void          newPosition( uint32_t newPosU );

    void          newStatus(   uint32_t newStatus );

    void          newSpeed(    uint32_t newSpeed );


    uint32_t     deviceNum;

    uint32_t     runPower;

    uint32_t     accelPower;

    uint32_t     decelPower;

    uint32_t     holdPower;

    uint32_t     overCurrent;

    uint32_t     overCurrentEnable;

    uint32_t     stallDetect;

    uint32_t     lastPosU;

    uint32_t     slewRate;

    uint32_t     configurationFlags;

    int32_t          positionChangeI;


    uint32_t     lastReportedSpeed;

    uint16_t     reportedStatus;


    bool          configured;

#ifdef DEBUG

// TODO:

uint32_t     debugTimer;

uint32_t     debugTimer2;


     void          setTargetVelocityForSeekedPosition();

    void          seekVelocity( float velocity );

     union {

          struct {

               uint datum                         : 8;

               uint param                         : 8; } st;

          uint16_t     updateReg; } updateRegU;

     PowerStepStatusReg powerStepStatusReg;