ST Expansion SW Team / X_NUCLEO_IHM14A1

Dependencies:   ST_INTERFACES

Dependents:   HelloWorld_IHM14A1

Fork of X_NUCLEO_IHM14A1 by Davide Aliprandi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STSPIN820.h Source File

STSPIN820.h

Go to the documentation of this file.
00001 /**
00002  *******************************************************************************
00003  * @file    STSPIN820.h
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    May 3rd, 2018
00007  * @brief   This file contains the class definition of an STSPIN820 Motor
00008  *          Control component.
00009  * @note    (C) COPYRIGHT 2018 STMicroelectronics
00010  *******************************************************************************
00011  * @attention
00012  *
00013  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00014  *
00015  * Redistribution and use in source and binary forms, with or without modification,
00016  * are permitted provided that the following conditions are met:
00017  *   1. Redistributions of source code must retain the above copyright notice,
00018  *      this list of conditions and the following disclaimer.
00019  *   2. Redistributions in binary form must reproduce the above copyright notice,
00020  *      this list of conditions and the following disclaimer in the documentation
00021  *      and/or other materials provided with the distribution.
00022  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00023  *      may be used to endorse or promote products derived from this software
00024  *      without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00029  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00030  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00031  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00032  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00034  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  *******************************************************************************
00038  */
00039 
00040 
00041 /* Generated with STM32CubeTOO -----------------------------------------------*/
00042 
00043 
00044 /* Revision ------------------------------------------------------------------*/
00045 /*
00046     Repository:       http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev
00047     Branch/Trunk/Tag: trunk
00048     Based on:         X-CUBE-SPN14/trunk/Drivers/BSP/Components/STSPIN820/STSPIN820.h
00049     Revision:         0
00050 */
00051 
00052 
00053 /* Define to prevent recursive inclusion -------------------------------------*/
00054 
00055 #ifndef __STSPIN820_CLASS_H
00056 #define __STSPIN820_CLASS_H
00057 
00058 
00059 /* Includes ------------------------------------------------------------------*/
00060 
00061 /* ACTION 1 ------------------------------------------------------------------*
00062  * Include here platform specific header files.                               *
00063  *----------------------------------------------------------------------------*/        
00064 #include "mbed.h"
00065 /* ACTION 2 ------------------------------------------------------------------*
00066  * Include here component specific header files.                              *
00067  *----------------------------------------------------------------------------*/        
00068 #include "STSPIN820_def.h"
00069 /* ACTION 3 ------------------------------------------------------------------*
00070  * Include here interface specific header files.                              *
00071  *                                                                            *
00072  * Example:                                                                   *
00073  *   #include "../Interfaces/Humidity.h"                                      *
00074  *   #include "../Interfaces/Temperature.h"                                   *
00075  *----------------------------------------------------------------------------*/
00076 #include "StepperMotor.h"
00077 
00078 
00079 /* Classes -------------------------------------------------------------------*/
00080 
00081 /**
00082  * @brief Class representing an STSPIN820 component.
00083  */
00084 class STSPIN820 : public StepperMotor
00085 {
00086 public:
00087 
00088     /*** Constructor and Destructor Methods ***/
00089 
00090     /**
00091      * @brief Constructor.
00092      * @param en_fault_irq  pin name of the EN_FAULT pin of the component.
00093      * @param standby_reset pin name of the STBY\RST pin of the component.
00094      * @param direction     pin name of the DIR pin of the component.
00095      * @param ref_pwm       pin name of the REF_PWM pin of the component.
00096      * @param decay         pin name of the DECAY pin of the component.
00097      * @param step_clock    pin name of the STCK pin of the component.
00098      * @param mode1         pin name of the MODE1 pin of the component.
00099      * @param mode2         pin name of the MODE2 pin of the component.
00100      * @param mode3         pin name of the MODE3 pin of the component.
00101      */
00102     STSPIN820(PinName en_fault_irq, PinName standby_reset, PinName direction,
00103               PinName ref_pwm, PinName decay, PinName step_clock,
00104               PinName mode1, PinName mode2, PinName mode3) :
00105               StepperMotor(),
00106               en_fault_irq(en_fault_irq),
00107               standby_reset(standby_reset),
00108               direction(direction),
00109               ref_pwm(ref_pwm),
00110               decay(decay),
00111               step_clock(step_clock),
00112               mode1(mode1),
00113               mode2(mode2),
00114               mode3(mode3)
00115     {
00116         /* Checking stackability. */
00117         if (!(number_of_devices < MAX_NUMBER_OF_DEVICES)) {
00118             error("Instantiation of the STSPIN820 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES);
00119         }
00120 
00121         /* ACTION 4 ----------------------------------------------------------*
00122          * Initialize here the component's member variables, one variable per *
00123          * line.                                                              *
00124          *                                                                    *
00125          * Example:                                                           *
00126          *   measure = 0;                                                     *
00127          *   instance_id = number_of_instances++;                             *
00128          *--------------------------------------------------------------------*/
00129         flag_interrupt_callback = 0;
00130         error_handler_callback = 0;
00131         toggle_odd = 0;
00132         pwm_period = 1;
00133         pwm_duty_cycle = 0;
00134         device_instance = number_of_devices++;
00135     }
00136     
00137     /**
00138      * @brief Destructor.
00139      */
00140     virtual ~STSPIN820(void) {}
00141     
00142 
00143     /*** Public Component Related Methods ***/
00144 
00145     /* ACTION 5 --------------------------------------------------------------*
00146      * Implement here the component's public methods, as wrappers of the C    *
00147      * component's functions.                                                 *
00148      * They should be:                                                        *
00149      *   + Methods with the same name of the C component's virtual table's    *
00150      *     functions (1);                                                     *
00151      *   + Methods with the same name of the C component's extended virtual   *
00152      *     table's functions, if any (2).                                     *
00153      *                                                                        *
00154      * Example:                                                               *
00155      *   virtual int get_value(float *p_data) //(1)                           *
00156      *   {                                                                    *
00157      *     return COMPONENT_get_value(float *pf_data);                        *
00158      *   }                                                                    *
00159      *                                                                        *
00160      *   virtual int enable_feature(void) //(2)                               *
00161      *   {                                                                    *
00162      *     return COMPONENT_enable_feature();                                 *
00163      *   }                                                                    *
00164      *------------------------------------------------------------------------*/
00165     virtual int init(void *init = NULL)
00166     {
00167         return (int) STSPIN820_Init((void *) init);
00168     }
00169 
00170     virtual int read_id(uint8_t *id = NULL)
00171     {
00172         return (int) STSPIN820_ReadId((uint8_t *) id);
00173     }
00174 
00175     virtual void attach_error_handler(void (*callback)(uint16_t error))
00176     {
00177         STSPIN820_AttachErrorHandler((void (*)(uint16_t error)) callback);
00178     }
00179 
00180     virtual void attach_flag_interrupt(void (*callback)(void))
00181     {
00182         STSPIN820_AttachFlagInterrupt((void (*)(void)) callback);
00183     }
00184 
00185     virtual void flag_interrupt_handler(void)
00186     {
00187         STSPIN820_FlagInterruptHandler();
00188     }
00189 
00190     virtual unsigned int get_acceleration(void)
00191     {
00192         return (unsigned int) STSPIN820_GetAcceleration();
00193     }
00194 
00195     virtual unsigned int get_speed(void)
00196     {
00197         return (unsigned int) STSPIN820_GetCurrentSpeed();
00198     }
00199 
00200     virtual unsigned int get_deceleration(void)
00201     {
00202         return (unsigned int) STSPIN820_GetDeceleration();
00203     }
00204 
00205     virtual motor_state_t get_device_state(void)
00206     {
00207         return (motor_state_t) STSPIN820_GetDeviceState();
00208     }
00209 
00210     virtual uint32_t get_fw_version(void)
00211     {
00212         return (uint32_t) STSPIN820_GetFwVersion();
00213     }
00214 
00215     virtual signed int get_mark(void)
00216     {
00217         return (signed int) STSPIN820_GetMark();
00218     }
00219 
00220     virtual unsigned int get_max_speed(void)
00221     {
00222         return (unsigned int) STSPIN820_GetMaxSpeed();
00223     }
00224 
00225     virtual unsigned int get_min_speed(void)
00226     {
00227         return (unsigned int) STSPIN820_GetMinSpeed();
00228     }
00229 
00230     virtual signed int get_position(void)
00231     {
00232         return (signed int) STSPIN820_GetPosition();
00233     }
00234 
00235     virtual void go_home(void)
00236     {
00237         STSPIN820_GoHome();
00238     }
00239 
00240     virtual void go_mark(void)
00241     {
00242         STSPIN820_GoMark();
00243     }
00244 
00245     virtual void go_to(signed int position)
00246     {
00247         STSPIN820_GoTo((signed int) position);
00248     }
00249 
00250     /**
00251      * @brief  Stopping the motor through an immediate deceleration up to zero speed.
00252      * @param  None.
00253      * @retval None.
00254      */
00255     virtual void soft_stop(void)
00256     {
00257         STSPIN820_SoftStop();
00258     }
00259 
00260     /**
00261      * @brief  Stopping the motor through an immediate infinite deceleration.
00262      * @param  None.
00263      * @retval None.
00264      */
00265     virtual void hard_stop(void)
00266     {
00267         STSPIN820_HardStop();
00268     }
00269 
00270     /**
00271      * @brief  Disabling the power bridge after performing a deceleration to zero.
00272      * @param  None.
00273      * @retval None.
00274      */
00275     virtual void soft_hiz(void) {}
00276 
00277     /**
00278      * @brief  Disabling the power bridge immediately.
00279      * @param  None.
00280      * @retval None.
00281      */
00282     virtual void hard_hiz(void)
00283     {
00284         STSPIN820_HardHiZ();
00285     }
00286 
00287     virtual void move(direction_t direction, unsigned int steps)
00288     {
00289         STSPIN820_Move((motor_direction_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (unsigned int) steps);
00290     }
00291 
00292     virtual void run(direction_t direction)
00293     {
00294         STSPIN820_Run((motor_direction_t) direction);
00295     }
00296 
00297     virtual bool set_acceleration(unsigned int acceleration)
00298     {
00299         return (bool) STSPIN820_SetAcceleration((unsigned int) acceleration);
00300     }
00301 
00302     virtual bool set_deceleration(unsigned int deceleration)
00303     {
00304         return (bool) STSPIN820_SetDeceleration((unsigned int) deceleration);
00305     }
00306 
00307     virtual void set_home(void)
00308     {
00309         STSPIN820_SetHome();
00310     }
00311 
00312     virtual void set_mark(void)
00313     {
00314         STSPIN820_SetMark();
00315     }
00316 
00317     virtual bool set_max_speed(unsigned int speed)
00318     {
00319         return (bool) STSPIN820_SetMaxSpeed((unsigned int) speed);
00320     }
00321 
00322     virtual bool set_min_speed(unsigned int speed)
00323     {
00324         return (bool) STSPIN820_SetMinSpeed((unsigned int) speed);
00325     }
00326 
00327     virtual void wait_while_active(void)
00328     {
00329         STSPIN820_WaitWhileActive();
00330     }
00331 
00332     virtual void cmd_disable(void)
00333     {
00334         STSPIN820_Disable();
00335     }
00336 
00337     virtual void cmd_enable(void)
00338     {
00339         STSPIN820_Enable();
00340     }
00341 
00342     virtual bool set_step_mode(step_mode_t step_mode)
00343     {
00344         return (bool) STSPIN820_SetStepMode((motor_step_mode_t) step_mode);
00345     }
00346 
00347     virtual void set_direction(direction_t direction)
00348     {
00349         STSPIN820_SetDirection((motor_direction_t) direction);
00350     }
00351 
00352     virtual void go_to_dir(direction_t direction, int32_t targetPosition)
00353     {
00354         STSPIN820_GoToDir((motor_direction_t) direction, (int32_t) targetPosition);
00355     }
00356 
00357     virtual unsigned int get_status(void)
00358     {
00359         return (unsigned int) STSPIN820_CheckStatusHw();
00360     }
00361 
00362     virtual void cmd_reset_device(void)
00363     {
00364         STSPIN820_PutDeviceInStandby();
00365     }
00366 
00367     virtual uint8_t get_nb_devices(void)
00368     {
00369         return (uint8_t) STSPIN820_GetNbDevices();
00370     }
00371 
00372     virtual uint32_t get_bridge_input_pwm_freq(void)
00373     {
00374         return (uint32_t) STSPIN820_VrefPwmGetFreq();
00375     }
00376 
00377     virtual void set_bridge_input_pwm_freq(uint32_t newFreq)
00378     {
00379         STSPIN820_VrefPwmSetFreq((uint32_t) newFreq);
00380     }
00381 
00382     virtual void set_stop_mode(motor_stop_mode_t stopMode)
00383     {
00384         STSPIN820_SetStopMode((motor_stop_mode_t) stopMode);
00385     }
00386 
00387     virtual motor_stop_mode_t get_stop_mode(void)
00388     {
00389         return (motor_stop_mode_t) STSPIN820_GetStopMode();
00390     }
00391 
00392     virtual void set_decay_mode(motor_decay_mode_t decayMode)
00393     {
00394         STSPIN820_SetDecayMode((motor_decay_mode_t) decayMode);
00395     }
00396 
00397     virtual motor_decay_mode_t get_decay_mode(void)
00398     {
00399         return (motor_decay_mode_t) STSPIN820_GetDecayMode();
00400     }
00401 
00402     virtual motor_step_mode_t get_step_mode(void)
00403     {
00404         return (motor_step_mode_t) STSPIN820_GetStepMode();
00405     }
00406 
00407     virtual direction_t get_direction(void)
00408     {
00409         return (direction_t) (STSPIN820_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD);
00410     }
00411 
00412     virtual void exit_device_from_reset(void)
00413     {
00414         STSPIN820_ExitDeviceFromStandby();
00415     }
00416 
00417     virtual void set_torque(motor_torque_mode_t torqueMode, uint8_t torqueValue)
00418     {
00419         STSPIN820_SetTorque((motor_torque_mode_t) torqueMode, (uint8_t) torqueValue);
00420     }
00421 
00422     virtual uint8_t get_torque(motor_torque_mode_t torqueMode)
00423     {
00424         return (uint8_t) STSPIN820_GetTorque((motor_torque_mode_t) torqueMode);
00425     }
00426 
00427     virtual void set_torque_boost_enable(bool enable)
00428     {
00429         STSPIN820_SetTorqueBoostEnable((bool) enable);
00430     }
00431 
00432     virtual bool get_torque_boost_enable(void)
00433     {
00434         return (bool) STSPIN820_GetTorqueBoostEnable();
00435     }
00436 
00437     virtual void set_torque_boost_threshold(uint16_t speedThreshold)
00438     {
00439         STSPIN820_SetTorqueBoostThreshold((uint16_t) speedThreshold);
00440     }
00441 
00442     virtual uint16_t get_torque_boost_threshold(void)
00443     {
00444         return (uint16_t) STSPIN820_GetTorqueBoostThreshold();
00445     }
00446 
00447 
00448     /*** Public Interrupt Related Methods ***/
00449 
00450     /* ACTION 6 --------------------------------------------------------------*
00451      * Implement here interrupt related methods, if any.                      *
00452      * Note that interrupt handling is platform dependent, e.g.:              *
00453      *   + mbed:                                                              *
00454      *     InterruptIn feature_irq(pin); //Interrupt object.                  *
00455      *     feature_irq.rise(callback);   //Attach a callback.                 *
00456      *     feature_irq.mode(PullNone);   //Set interrupt mode.                *
00457      *     feature_irq.enable_irq();     //Enable interrupt.                  *
00458      *     feature_irq.disable_irq();    //Disable interrupt.                 *
00459      *   + Arduino:                                                           *
00460      *     attachInterrupt(pin, callback, RISING); //Attach a callback.       *
00461      *     detachInterrupt(pin);                   //Detach a callback.       *
00462      *                                                                        *
00463      * Example (mbed):                                                        *
00464      *   void attach_feature_irq(void (*fptr) (void))                         *
00465      *   {                                                                    *
00466      *     feature_irq.rise(fptr);                                            *
00467      *   }                                                                    *
00468      *                                                                        *
00469      *   void enable_feature_irq(void)                                        *
00470      *   {                                                                    *
00471      *     feature_irq.enable_irq();                                          *
00472      *   }                                                                    *
00473      *                                                                        *
00474      *   void disable_feature_irq(void)                                       *
00475      *   {                                                                    *
00476      *     feature_irq.disable_irq();                                         *
00477      *   }                                                                    *
00478      *------------------------------------------------------------------------*/
00479     /**
00480      * @brief  Attaching an interrupt handler to the FLAG interrupt.
00481      * @param  fptr An interrupt handler.
00482      * @retval None.
00483      */
00484     void attach_en_fault_irq(void (*fptr)(void))
00485     {
00486         en_fault_irq.mode(PullUp);
00487         en_fault_irq.fall(fptr);
00488     }
00489     
00490     /**
00491      * @brief  Enabling the FLAG interrupt handling.
00492      * @param  None.
00493      * @retval None.
00494      */
00495     void enable_en_fault_irq(void)
00496     {
00497         en_fault_irq.enable_irq();
00498     }
00499     
00500     /**
00501      * @brief  Disabling the FLAG interrupt handling.
00502      * @param  None.
00503      * @retval None.
00504      */
00505     void disable_en_fault_irq(void)
00506     {
00507         en_fault_irq.disable_irq();
00508     }
00509 
00510 
00511 protected:
00512 
00513     /*** Protected Component Related Methods ***/
00514 
00515     /* ACTION 7 --------------------------------------------------------------*
00516      * Declare here the component's specific methods.                         *
00517      * They should be:                                                        *
00518      *   + Methods with the same name of the C component's virtual table's    *
00519      *     functions (1);                                                     *
00520      *   + Methods with the same name of the C component's extended virtual   *
00521      *     table's functions, if any (2);                                     *
00522      *   + Helper methods, if any, like functions declared in the component's *
00523      *     source files but not pointed by the component's virtual table (3). *
00524      *                                                                        *
00525      * Example:                                                               *
00526      *   status_t COMPONENT_get_value(float *f);   //(1)                      *
00527      *   status_t COMPONENT_enable_feature(void);  //(2)                      *
00528      *   status_t COMPONENT_compute_average(void); //(3)                      *
00529      *------------------------------------------------------------------------*/
00530     //Apply speed
00531     void STSPIN820_ApplySpeed(uint16_t newSpeed);
00532     //Apply torque
00533     void STSPIN820_ApplyTorque(motor_torque_mode_t torqueMode);
00534     //Compute speed profile
00535     void STSPIN820_ComputeSpeedProfile(uint32_t nbSteps);
00536     //Handler for the flag interrupt
00537     void STSPIN820_FlagInterruptHandler(void);
00538     //Set device paramenters to other values
00539     void STSPIN820_SetDeviceParamsOtherValues(void);
00540     //Set device paramenters to given values
00541     void STSPIN820_SetDeviceParamsToGivenValues(STSPIN820_init_t* initDevicePrm);
00542     //Set device paramenters to predefined values
00543     void STSPIN820_SetDeviceParamsToPredefinedValues(void);
00544     //Start moving
00545     void STSPIN820_StartMovement(void);
00546     //Handle the step clock
00547     void STSPIN820_StepClockHandler(void);
00548     //Start the STSPIN820 library
00549     status_t STSPIN820_Init(void *init);
00550     //Read Id to get driver instance
00551     status_t STSPIN820_ReadId(uint8_t *id);
00552     //Attach a user callback to the error handler
00553     void STSPIN820_AttachErrorHandler(void (*callback)(uint16_t));
00554     //Attach a user callback to the flag Interrupt
00555     void STSPIN820_AttachFlagInterrupt(void (*callback)(void));
00556     //Check if STSPIN820 has a fault by reading EN pin position
00557     uint8_t STSPIN820_CheckStatusHw(void);
00558     //Disable the power stage of the specified device
00559     void STSPIN820_Disable(void);
00560     //Enable the power stage of the specified device
00561     void STSPIN820_Enable(void);
00562     //Error handler which calls the user callback
00563     void STSPIN820_ErrorHandler(uint16_t error);
00564     //Exit STSPIN820 device from standby 
00565     void STSPIN820_ExitDeviceFromStandby(void);
00566     //Return the acceleration in pps^2
00567     uint16_t STSPIN820_GetAcceleration(void);
00568     //Return the current speed in pps
00569     uint16_t STSPIN820_GetCurrentSpeed(void);
00570     //Return the device decay mode
00571     motor_decay_mode_t STSPIN820_GetDecayMode(void);
00572     //Return the deceleration in pps^2
00573     uint16_t STSPIN820_GetDeceleration(void);
00574     //Return the device state
00575     motor_state_t STSPIN820_GetDeviceState(void);
00576     //Get the motor current direction
00577     motor_direction_t STSPIN820_GetDirection(void);
00578     //Return the FW version
00579     uint32_t STSPIN820_GetFwVersion(void);
00580     //Return the mark position 
00581     int32_t STSPIN820_GetMark(void);
00582     //Return the max speed in pps
00583     uint16_t STSPIN820_GetMaxSpeed(void);
00584     //Return the min speed in pps
00585     uint16_t STSPIN820_GetMinSpeed(void);
00586     //Return the nupber of devices
00587     uint8_t STSPIN820_GetNbDevices(void);
00588     //Return the ABS_POSITION (32b signed)
00589     int32_t STSPIN820_GetPosition(void);
00590     //Get the motor step mode
00591     motor_step_mode_t STSPIN820_GetStepMode(void);
00592     //Get the selected mode to stop the motor
00593     motor_stop_mode_t STSPIN820_GetStopMode(void);
00594     //Get the torque
00595     uint8_t STSPIN820_GetTorque(motor_torque_mode_t torqueMode);
00596     //Get the torque boost feature status
00597     bool STSPIN820_GetTorqueBoostEnable(void);
00598     //Get the torque boost threshold
00599     uint16_t STSPIN820_GetTorqueBoostThreshold(void);
00600     //Move to the home position
00601     void STSPIN820_GoHome(void);
00602     //Move to the Mark position
00603     void STSPIN820_GoMark(void);
00604     //Go to the specified position
00605     void STSPIN820_GoTo(int32_t targetPosition);
00606     //Go to the specified position using the specified direction
00607     void STSPIN820_GoToDir(motor_direction_t direction, int32_t targetPosition);
00608     //Stop the motor and keep holding torque
00609     void STSPIN820_HardStop(void);
00610     //Stop the motor and disable the power bridge
00611     void STSPIN820_HardHiZ(void);
00612     //Move the motor of the specified number of steps
00613     void STSPIN820_Move(motor_direction_t direction, uint32_t stepCount);
00614     //Put STSPIN820 device in standby (low power consumption)
00615     void STSPIN820_PutDeviceInStandby(void);
00616     //Run the motor 
00617     void STSPIN820_Run(motor_direction_t direction);
00618     //Set the acceleration in pps^2
00619     bool STSPIN820_SetAcceleration(uint16_t newAcc);
00620     //Set the deceleration in pps^2
00621     bool STSPIN820_SetDeceleration(uint16_t newDec);
00622     //Set the STSPIN820 decay mode pin
00623     void STSPIN820_SetDecayMode(motor_decay_mode_t decay);
00624     //Set the STSPIN820 direction pin
00625     void STSPIN820_SetDirection(motor_direction_t direction);
00626     //Set current position to be the home position
00627     void STSPIN820_SetHome(void);
00628     //Set current position to be the mark position
00629     void STSPIN820_SetMark(void);
00630     //Set the max speed in pps
00631     bool STSPIN820_SetMaxSpeed(uint16_t newMaxSpeed);
00632     //Set the min speed in pps
00633     bool STSPIN820_SetMinSpeed(uint16_t newMinSpeed);
00634     //Set the number of devices
00635     bool STSPIN820_SetNbDevices(uint8_t nbDevices);
00636     //Step mode selection
00637     bool STSPIN820_SetStepMode(motor_step_mode_t stepMode);
00638     //Select the mode to stop the motor
00639     void STSPIN820_SetStopMode(motor_stop_mode_t stopMode);
00640     //Set the torque
00641     void STSPIN820_SetTorque(motor_torque_mode_t torqueMode, uint8_t torqueValue);
00642     //Enable or disable the torque boost feature
00643     void STSPIN820_SetTorqueBoostEnable(bool enable);
00644     //Set the torque boost threshold
00645     void STSPIN820_SetTorqueBoostThreshold(uint16_t speedThreshold);
00646     //Progressively stop the motor by using the device deceleration and set deceleration torque
00647     bool STSPIN820_SoftStop(void);
00648     //Get the frequency of REF PWM of the specified device
00649     uint32_t STSPIN820_VrefPwmGetFreq(void);
00650     //Set the frequency of REF PWM of the specified device
00651     void STSPIN820_VrefPwmSetFreq(uint32_t newFreq);
00652     //Wait for the device state becomes Inactive
00653     void STSPIN820_WaitWhileActive(void);
00654 
00655 
00656     /* ACTION 8 --------------------------------------------------------------*
00657      * Implement here other I/O methods beyond those already implemented      *
00658      * above, which are declared extern within the component's header file.   *
00659      *------------------------------------------------------------------------*/
00660     /**
00661      * @brief  Making the CPU wait.
00662      * @param  None.
00663      * @retval None.
00664      */
00665     void STSPIN820_Delay(uint32_t delay)
00666     {
00667         wait_ms(delay);
00668     }
00669 
00670     /**
00671      * @brief  Enabling the power bridges (leaving the output bridges at HiZ).
00672      * @param  None.
00673      * @retval None.
00674      */
00675     void STSPIN820_Enable_Power_Bridges(void)
00676     {
00677         if (en_fault_irq.read() == 0)
00678         {
00679             //en_fault_irq.write(1);
00680             wait_ms(BRIDGE_TURN_ON_DELAY_ms);
00681             enable_en_fault_irq();
00682         }
00683     }
00684 
00685     /**
00686      * @brief  Disabling the power bridges (forcing the output bridges at HiZ).
00687      * @param  None.
00688      * @retval None.
00689      */
00690     void STSPIN820_Disable_Power_Bridges(void)
00691     {
00692         disable_en_fault_irq();
00693         //en_fault_irq.write(0);
00694     }
00695 
00696     /**
00697      * @brief  Enabling interrupts.
00698      * @param  None.
00699      * @retval None.
00700      */
00701     void STSPIN820_EnableIrq(void)
00702     {
00703         __enable_irq();
00704     }
00705 
00706     /**
00707      * @brief  Disabling interrupts.
00708      * @param  None.
00709      * @retval None.
00710      */
00711     void STSPIN820_DisableIrq(void)
00712     {
00713         __disable_irq();
00714     }
00715 
00716     /**
00717      * @brief  Getting the state of the EN_FAULT pin.
00718      * @param  None.
00719      * @retval None.
00720      */
00721     uint32_t STSPIN820_EN_FAULT_PIN_GetState(void)
00722     {
00723         return (uint32_t) en_fault_irq;
00724     }
00725 
00726     /**
00727      * @brief  Initialising the GPIOs.
00728      * @param  None.
00729      * @retval None.
00730      */
00731     void STSPIN820_GpioInit(void)
00732     {
00733         disable_en_fault_irq();
00734         enable_en_fault_irq();
00735         STSPIN820_Reset();
00736     }
00737 
00738     /**
00739      * @brief  Initialising the PWM.
00740      * @param  None.
00741      * @retval None.
00742      */
00743     void STSPIN820_PwmRefInit(void) {}
00744 
00745     /**
00746      * @brief  Setting the frequency and the duty-cycle of PWM.
00747      *         The frequency controls directly the speed of the device.
00748      *         The duty-cycle controls the torque of the device.
00749      * @param  frequency  The frequency of PWM in Hz.
00750      * @param  duty_cycle The duty-cycle of PWM in [0..100]%.
00751      * @retval None.
00752      */
00753     void STSPIN820_PwmRefSetFreqAndDutyCycle(uint32_t frequency, uint8_t duty_cycle)
00754     {
00755         pwm_period = (double) (1.0f / frequency);
00756         pwm_duty_cycle = (double) duty_cycle;
00757     }
00758 
00759     /**
00760      * @brief  Starting the PWM.
00761      * @param  None.
00762      * @retval None.
00763      */
00764     void STSPIN820_PwmRefStart(void)
00765     {
00766         /* Setting the period and the duty-cycle of PWM. */
00767         ref_pwm.period(pwm_period);
00768         ref_pwm.write(pwm_duty_cycle);
00769 
00770         /* Setting a callback with the same period of PWM's, to update the state machine. */
00771         ticker.attach(Callback<void()>(this, &STSPIN820::STSPIN820_StepClockHandler), pwm_period);
00772     }
00773 
00774     /**
00775      * @brief  Stopping the PWM.
00776      * @param  None.
00777      * @retval None.
00778      */
00779     void STSPIN820_PwmRefStop(void)
00780     {
00781         ref_pwm.write(0.0f);
00782         ticker.detach();
00783     }
00784 
00785     /**
00786      * @brief  Putting the device in standby mode.
00787      * @param  None.
00788      * @retval None.
00789      */
00790     void STSPIN820_ReleaseReset(void)
00791     {
00792         standby_reset = 1;
00793     }
00794 
00795     /**
00796      * @brief  Putting the device in reset mode.
00797      * @param  None.
00798      * @retval None.
00799      */
00800     void STSPIN820_Reset(void)
00801     {
00802         standby_reset = 0;
00803     }
00804 
00805     /**
00806      * @brief  Setting the decay mode pin state.
00807      * @param  gpio_state State of the decay mode pin ("0" to reset, "1" to set).
00808      * @retval None.
00809      */
00810     void STSPIN820_SetDecayGpio(uint8_t gpio_state)
00811     {
00812         decay = gpio_state;
00813     }
00814 
00815     /**
00816      * @brief  Getting the decay mode pin state.
00817      * @param  None.
00818      * @retval None.
00819      */
00820     uint8_t STSPIN820_GetDecayGpio(void)
00821     {
00822         return (decay != 0 ? 1 : 0);
00823     }
00824 
00825     /**
00826      * @brief  Setting the direction pin state.
00827      * @param  gpio_state State of the direction pin ("0" to reset, "1" to set).
00828      * @retval None.
00829      */
00830     void STSPIN820_SetDirectionGpio(uint8_t gpio_state)
00831     {
00832         direction = gpio_state;
00833     }
00834 
00835     /**
00836      * @brief  Setting the full step mode.
00837      * @param  None.
00838      * @retval None.
00839      */
00840     void STSPIN820_SetFullStep(void)
00841     {
00842         mode1 = mode2 = mode3 = 0;
00843     }
00844 
00845     /**
00846      * @brief  Selecting the STSPIN820 mode1, mode2 and mode3 pins levels.
00847      * @param  mode1_level Level of the mode1 gpio (0 low, 1+ high).
00848      * @param  mode2_level Level of the mode2 gpio (0 low, 1+ high).
00849      * @param  mode3_level Level of the mode3 gpio (0 low, 1+ high).
00850      * @retval None.
00851      */
00852     void STSPIN820_SetModePins(uint8_t mode1_level, uint8_t mode2_level, uint8_t mode3_level)
00853     {
00854         mode1 = (mode1_level != 0 ? 1 : 0);
00855         mode2 = (mode2_level != 0 ? 1 : 0);
00856         mode3 = (mode3_level != 0 ? 1 : 0);
00857     }
00858 
00859     /**
00860      * @brief  Initializing step clock compare value.
00861      * @param  None.
00862      * @retval None.
00863      */
00864     void STSPIN820_TimStckCompareInit(void) {}
00865 
00866     /**
00867      * @brief  Deinitializing the timer used for the step clock.
00868      * @param  None.
00869      * @retval None.
00870      */
00871     void STSPIN820_TimStckDeInit(void) {}
00872 
00873     /**
00874      * @brief  Initializing the timer used for the step clock.
00875      * @param  None.
00876      * @retval None.
00877      */
00878     void STSPIN820_TimStckInit(void) {}
00879 
00880     /**
00881      * @brief  Setting the frequency of PWM.
00882      *         The frequency controls directly the speed of the device.
00883      * @param  frequency  The frequency of PWM in Hz.
00884      * @retval None.
00885      */
00886     void STSPIN820_TimStckSetFreq(uint32_t frequency)
00887     {
00888         pwm_period = (double) (1.0f / frequency);
00889     }
00890 
00891     /**
00892      * @brief  Starting the step clock.
00893      * @param  None.
00894      * @retval None.
00895      */
00896     void STSPIN820_TimStckStart(void) {}
00897 
00898     /**
00899      * @brief  Stopping the step clock.
00900      * @param  p_toggle_odd Pointer to the volatile toggleOdd variable.
00901      * @retval "1" if OK, "0" if the STCK pin is High (forbidden configuration).
00902      */
00903     uint8_t STSPIN820_TimStckStop(volatile uint8_t *p_toggle_odd )
00904     {
00905         __disable_irq();
00906         if (*p_toggle_odd == 1)
00907         {
00908             __enable_irq();
00909             return 1;
00910         }
00911         if (step_clock != 0)
00912         {
00913             __enable_irq();
00914             return 0;
00915         }
00916         //HAL_TIM_OC_Stop_IT(&hTimerStepClock, BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK);
00917         __enable_irq();
00918         STSPIN820_TimStckDeInit();
00919         return 1;
00920     }
00921 
00922 
00923     /*** Component's Instance Variables ***/
00924 
00925     /* ACTION 9 --------------------------------------------------------------*
00926      * Declare here interrupt related variables, if needed.                   *
00927      * Note that interrupt handling is platform dependent, see                *
00928      * "Interrupt Related Methods" above.                                     *
00929      *                                                                        *
00930      * Example:                                                               *
00931      *   + mbed:                                                              *
00932      *     InterruptIn feature_irq;                                           *
00933      *------------------------------------------------------------------------*/
00934     /* En Fault Interrupt. */
00935     InterruptIn en_fault_irq;
00936 
00937     /* ACTION 10 -------------------------------------------------------------*
00938      * Declare here other pin related variables, if needed.                   *
00939      *                                                                        *
00940      * Example:                                                               *
00941      *   + mbed:                                                              *
00942      *     DigitalOut standby_reset;                                          *
00943      *------------------------------------------------------------------------*/
00944     /* Standby/reset pin. */
00945     DigitalOut standby_reset;
00946 
00947     /* Direction of rotation pin. */
00948     DigitalOut direction;
00949 
00950     /* Pulse Width Modulation pin. */
00951     PwmOut ref_pwm;
00952 
00953     /* Decay pin. */
00954     DigitalOut decay;
00955     
00956     /* Step clock pin. */
00957     DigitalOut step_clock;
00958     
00959     /* Mode1 pin. */
00960     DigitalOut mode1;
00961     
00962     /* Mode2 pin. */
00963     DigitalOut mode2;
00964     
00965     /* Mode3 pin. */
00966     DigitalOut mode3;
00967 
00968     /* Timer to trigger the PWM callback at each PWM pulse. */
00969     Ticker ticker;
00970 
00971     /* ACTION 11 -------------------------------------------------------------*
00972      * Declare here communication related variables, if needed.               *
00973      *                                                                        *
00974      * Example:                                                               *
00975      *   + mbed:                                                              *
00976      *     DigitalOut ssel;                                                   *
00977      *     DevSPI &dev_spi;                                                   *
00978      *------------------------------------------------------------------------*/
00979 
00980     /* ACTION 12 -------------------------------------------------------------*
00981      * Declare here identity related variables, if needed.                    *
00982      * Note that there should be only a unique identifier for each component, *
00983      * which should be the "who_am_i" parameter.                              *
00984      *------------------------------------------------------------------------*/
00985     /* Identity */
00986     uint8_t who_am_i;
00987 
00988     /* ACTION 13 -------------------------------------------------------------*
00989      * Declare here the component's static and non-static data, one variable  *
00990      * per line.                                                              *
00991      *                                                                        *
00992      * Example:                                                               *
00993      *   float measure;                                                       *
00994      *   int instance_id;                                                     *
00995      *   static int number_of_instances;                                      *
00996      *------------------------------------------------------------------------*/
00997     /* Data. */
00998     void (*flag_interrupt_callback)(void);
00999     void (*error_handler_callback)(uint16_t error);
01000     volatile uint8_t toggle_odd;
01001     device_params_t device_prm;
01002     double pwm_period;
01003     double pwm_duty_cycle;
01004     uint8_t device_instance;
01005     
01006     /* Static data. */
01007     static uint8_t number_of_devices;
01008 };
01009 
01010 #endif /* __STSPIN820_CLASS_H */
01011 
01012 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/