Davide Aliprandi / X-NUCLEO-IHM06A1

Dependencies:   ST_INTERFACES

Fork of X_NUCLEO_IHM06A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stspin220_class.h Source File

stspin220_class.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    stspin220_class.h
00004  * @author  IPC Rennes
00005  * @version V1.0.0
00006  * @date    July 27th, 2016
00007  * @brief   This file contains the class of a STSPIN220 Motor Control component.
00008  * @note    (C) COPYRIGHT 2016 STMicroelectronics
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Define to prevent recursive inclusion -------------------------------------*/
00041 
00042 #ifndef __STSPIN220_CLASS_H
00043 #define __STSPIN220_CLASS_H
00044 
00045 
00046 /* Includes ------------------------------------------------------------------*/
00047 
00048 /* ACTION 1 ------------------------------------------------------------------*
00049  * Include here platform specific header files.                               *
00050  *----------------------------------------------------------------------------*/        
00051 #include "mbed.h"
00052 
00053 /* ACTION 2 ------------------------------------------------------------------*
00054  * Include here component specific header files.                              *
00055  *----------------------------------------------------------------------------*/        
00056 #include "stspin220.h"
00057 /* ACTION 3 ------------------------------------------------------------------*
00058  * Include here interface specific header files.                              *
00059  *                                                                            *
00060  * Example:                                                                   *
00061  *   #include "HumiditySensor.h"                                              *
00062  *   #include "TemperatureSensor.h"                                           *
00063  *----------------------------------------------------------------------------*/
00064 #include "StepperMotor.h"
00065 
00066 
00067 /* Classes -------------------------------------------------------------------*/
00068 
00069 /**
00070  * @brief Class representing a STSPIN220 component.
00071  */
00072 class STSPIN220 : public StepperMotor
00073 {
00074 public:
00075     /*** Constructor and Destructor Methods ***/
00076     /**
00077      * @brief Constructor.
00078      * @param fault_and_enable_pin  pin name of the EN\FAULT pin of the component.
00079      * @param stby_reset_pin        pin name of the STBY\RESET pin of the component.
00080      * @param direction_mode4_pin   pin name of the MODE4\DIR pin of the component.
00081      * @param mode1_pin             pin name of the MODE1 pin of the component.
00082      * @param mode2_pin             pin name of the MODE2 pin of the component.
00083      * @param stck_mode3_pin        pin name of the MODE3\STCK pin of the component.
00084      * @param pwm_ref_pin           pin name of the PWM connected to the REF pin of the component.
00085      * @param Monitor_pin           pin name for the step clock handler duration Monitoring.
00086      */
00087     STSPIN220(PinName fault_and_enable_pin,
00088               PinName stby_reset_pin,
00089               PinName direction_mode4_pin,
00090               PinName mode1_pin,
00091               PinName mode2_pin,
00092               PinName stck_mode3_pin,
00093               PinName pwm_ref_pin,
00094               PinName monitor_pin = NC) : StepperMotor(),
00095                                      fault_and_enable(fault_and_enable_pin),
00096                                      stby_reset(stby_reset_pin),
00097                                      direction_mode4(direction_mode4_pin),
00098                                      mode1(mode1_pin),
00099                                      mode2(mode2_pin),
00100                                      stck_mode3(stck_mode3_pin),
00101                                      pwm_ref(pwm_ref_pin),
00102                                      monitor(monitor_pin)
00103     {
00104         /* Checking stackability. */
00105         if (numberOfDevices!=0)
00106             error("Instantiation of the STSPIN220 component failed: it can't be stacked on itself.\r\n");
00107 
00108         /* ACTION 4 ----------------------------------------------------------*
00109          * Initialize here the component's member variables, one variable per *
00110          * line.                                                              *
00111          *                                                                    *
00112          * Example:                                                           *
00113          *   measure = 0;                                                     *
00114          *   instance_id = number_of_instances++;                             *
00115          *--------------------------------------------------------------------*/
00116         errorHandlerCallback = 0;
00117         memset(&devicePrm, 0, sizeof(devicePrm));
00118         deviceInstance = numberOfDevices++;
00119         
00120         fault_and_enable_pinName = fault_and_enable_pin;
00121     }
00122     
00123     /**
00124      * @brief Destructor.
00125      */
00126     virtual ~STSPIN220(void) {}
00127     
00128 
00129     /*** Public Component Related Methods ***/
00130 
00131     /* ACTION 5 --------------------------------------------------------------*
00132      * Implement here the component's public methods, as wrappers of the C    *
00133      * component's functions.                                                 *
00134      * They should be:                                                        *
00135      *   + Methods with the same name of the C component's virtual table's    *
00136      *     functions (1);                                                     *
00137      *   + Methods with the same name of the C component's extended virtual   *
00138      *     table's functions, if any (2).                                     *
00139      *                                                                        *
00140      * Example:                                                               *
00141      *   virtual int GetValue(float *pData) //(1)                             *
00142      *   {                                                                    *
00143      *     return COMPONENT_GetValue(float *pfData);                          *
00144      *   }                                                                    *
00145      *                                                                        *
00146      *   virtual int EnableFeature(void) //(2)                                *
00147      *   {                                                                    *
00148      *     return COMPONENT_EnableFeature();                                  *
00149      *   }                                                                    *
00150      *------------------------------------------------------------------------*/
00151 
00152     /**
00153      * @brief Public functions inherited from the Component Class
00154      */
00155 
00156     /**
00157      * @brief  Initialize the component.
00158      * @param  init Pointer to device specific initalization structure.
00159      * @retval "0" in case of success, an error code otherwise.
00160      */
00161     virtual int Init(void *init = NULL)
00162     {
00163         return (int) Stspin220_Init((void *) init);
00164     }
00165 
00166     /**
00167      * @brief  Getting the ID of the component.
00168      * @param  id Pointer to an allocated variable to store the ID into.
00169      * @retval "0" in case of success, an error code otherwise.
00170      */
00171     virtual int ReadID(uint8_t *id = NULL)
00172     {
00173         return (int) Stspin220_ReadID((uint8_t *) id);
00174     }
00175 
00176     /**
00177      * @brief Public functions inherited from the StepperMotor Class
00178      */
00179 
00180     /**
00181      * @brief  Getting the value of the motor state .
00182      * @param  None.
00183      * @retval The motor state accoring to motorState_t in motor.h
00184      */
00185     virtual unsigned int GetStatus(void)
00186     {
00187         return (unsigned int) Stspin220_GetDeviceState();
00188     }
00189 
00190     /**
00191      * @brief  Getting the position.
00192      * @param  None.
00193      * @retval The position.
00194      */
00195     virtual signed int GetPosition(void)
00196     {
00197         return (signed int)Stspin220_GetPosition();
00198     }
00199 
00200     /**
00201      * @brief  Getting the marked position.
00202      * @param  None.
00203      * @retval The marked position.
00204      */
00205     virtual signed int GetMark(void)
00206     {
00207         return (signed int)Stspin220_GetMark();
00208     }
00209 
00210     /**
00211      * @brief  Getting the current speed in pps.
00212      * @param  None.
00213      * @retval The current speed in pps.
00214      */
00215     virtual unsigned int GetSpeed(void)
00216     {
00217         return (unsigned int)Stspin220_GetCurrentSpeed();
00218     }
00219 
00220     /**
00221      * @brief  Getting the maximum speed in pps.
00222      * @param  None.
00223      * @retval The maximum speed in pps.
00224      */
00225     virtual unsigned int GetMaxSpeed(void)
00226     {
00227         return (unsigned int)Stspin220_GetMaxSpeed();
00228     }
00229 
00230     /**
00231      * @brief  Getting the minimum speed in pps.
00232      * @param  None.
00233      * @retval The minimum speed in pps.
00234      */
00235     virtual unsigned int GetMinSpeed(void)
00236     {
00237         return (unsigned int)Stspin220_GetMinSpeed();
00238     }
00239 
00240     /**
00241      * @brief  Getting the acceleration in pps^2.
00242      * @param  None.
00243      * @retval The acceleration in pps^2.
00244      */
00245     virtual unsigned int GetAcceleration(void)
00246     {
00247         return (unsigned int)Stspin220_GetAcceleration();
00248     }
00249 
00250     /**
00251      * @brief  Getting the deceleration in pps^2.
00252      * @param  None.
00253      * @retval The deceleration in pps^2.
00254      */
00255     virtual unsigned int GetDeceleration(void)
00256     {
00257         return (unsigned int)Stspin220_GetDeceleration();
00258     }
00259     
00260     /**
00261      * @brief  Getting the direction of rotation.
00262      * @param  None.
00263      * @retval The direction of rotation.
00264      */
00265     virtual direction_t GetDirection(void)
00266     {
00267         if (Stspin220_GetDirection()!=BACKWARD)
00268         {
00269             return FWD;
00270         }
00271         else
00272         {
00273             return BWD;
00274         }
00275     }
00276     
00277     /**
00278      * @brief  Setting the current position to be the home position.
00279      * @param  None.
00280      * @retval None.
00281      */
00282     virtual void SetHome(void)
00283     {
00284         Stspin220_SetHome();
00285     }
00286 
00287     /**
00288      * @brief  Setting the current position to be the marked position.
00289      * @param  None.
00290      * @retval None.
00291      */
00292     virtual void SetMark(void)
00293     {
00294         Stspin220_SetMark();
00295     }
00296 
00297     /**
00298      * @brief  Setting the maximum speed in pps.
00299      * @param  speed The maximum speed in pps.
00300      * @retval "true" in case of success, "false" otherwise.
00301      */
00302     virtual bool SetMaxSpeed(unsigned int speed)
00303     {
00304         if (speed <= 0xFFFF)
00305         {
00306             return Stspin220_SetMaxSpeed((uint16_t) speed);
00307         }
00308         else
00309         {
00310             return false;
00311         }
00312     }
00313 
00314     /**
00315      * @brief  Setting the minimum speed in pps.
00316      * @param  speed The minimum speed in pps.
00317      * @retval "true" in case of success, "false" otherwise.
00318      */
00319     virtual bool SetMinSpeed(unsigned int speed)
00320     {
00321         if (speed <= 0xFFFF)
00322         {
00323             return Stspin220_SetMinSpeed((uint16_t) speed);
00324         }
00325         else
00326         {
00327             return false;
00328         }
00329     }
00330 
00331     /**
00332      * @brief  Setting the acceleration in pps^2.
00333      * @param  acceleration The acceleration in pps/s^2.
00334      * @retval "true" in case of success, "false" otherwise.
00335      */
00336     virtual bool SetAcceleration(unsigned int acceleration)
00337     {
00338         if (acceleration <= 0xFFFF)
00339         {
00340             return Stspin220_SetAcceleration((uint16_t) acceleration);
00341         }
00342         else
00343         {
00344             return false;
00345         }
00346     }
00347 
00348     /**
00349      * @brief  Setting the deceleration in pps^2.
00350      * @param  deceleration The deceleration in pps^2.
00351      * @retval "true" in case of success, "false" otherwise.
00352      */
00353     virtual bool SetDeceleration(unsigned int deceleration)
00354     {
00355         if (deceleration <= 0xFFFF)
00356         {
00357             return Stspin220_SetDeceleration((uint16_t) deceleration);
00358         }
00359         else
00360         {
00361             return false;
00362         }
00363     }
00364 
00365     /**
00366      * @brief  Setting the Step Mode.
00367      * @param  step_mode The Step Mode.
00368      * @retval "true" in case of success, "false" otherwise.
00369      * @note   step_mode can be one of the following:
00370      *           + STEP_MODE_FULL
00371      *           + STEP_MODE_WAVE
00372      *           + STEP_MODE_HALF
00373      *           + STEP_MODE_1_4
00374      *           + STEP_MODE_1_8
00375      *           + STEP_MODE_1_16
00376      */
00377     virtual bool SetStepMode(step_mode_t step_mode)
00378     {
00379         return Stspin220_SetStepMode((motorStepMode_t) step_mode);
00380     }
00381 
00382     /**
00383      * @brief  Going to a specified position.
00384      * @param  position The desired position.
00385      * @retval None.
00386      */
00387     virtual void GoTo(signed int position)
00388     {
00389         Stspin220_GoTo((int32_t)position);
00390     }
00391 
00392     /**
00393      * @brief  Going to the home position.
00394      * @param  None.
00395      * @retval None.
00396      */
00397     virtual void GoHome(void)
00398     {
00399         Stspin220_GoHome();
00400     }
00401 
00402     /**
00403      * @brief  Going to the marked position.
00404      * @param  None.
00405      * @retval None.
00406      */
00407     virtual void GoMark(void)
00408     {
00409         Stspin220_GoMark();
00410     }
00411 
00412     /**
00413      * @brief  Running the motor towards a specified direction.
00414      * @param  direction The direction of rotation.
00415      * @retval None.
00416      */
00417     virtual void Run(direction_t direction)
00418     {
00419         Stspin220_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD));
00420     }
00421 
00422     /**
00423      * @brief  Moving the motor towards a specified direction for a certain number of steps.
00424      * @param  direction The direction of rotation.
00425      * @param  steps The desired number of steps.
00426      * @retval None.
00427      */
00428     virtual void Move(direction_t direction, unsigned int steps)
00429     {
00430         Stspin220_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (uint32_t)steps);
00431     }
00432 
00433     /**
00434      * @brief  Stopping the motor through an immediate deceleration up to zero speed.
00435      * @param  None.
00436      * @retval None.
00437      */
00438     virtual void SoftStop(void)
00439     {
00440         Stspin220_SoftStop();
00441     }
00442 
00443     /**
00444      * @brief  Stopping the motor through an immediate infinite deceleration.
00445      * @param  None.
00446      * @retval None.
00447      */
00448     virtual void HardStop(void)
00449     {
00450         Stspin220_HardStop();
00451     }
00452 
00453     /**
00454      * @brief  Disabling the power bridge after performing a deceleration to zero.
00455      * @param  None.
00456      * @retval None.
00457      */
00458     virtual void SoftHiZ(void)
00459     {
00460         Stspin220_SetStopMode(HIZ_MODE);
00461         Stspin220_SoftStop();
00462     }
00463 
00464     /**
00465      * @brief  Disabling the power bridge immediately.
00466      * @param  None.
00467      * @retval None.
00468      */
00469     virtual void HardHiZ(void)
00470     {
00471         Stspin220_HardHiZ();
00472     }
00473 
00474     /**
00475      * @brief  Waiting while the motor is active.
00476      * @param  None.
00477      * @retval None.
00478      */
00479     virtual void WaitWhileActive(void)
00480     {
00481         Stspin220_WaitWhileActive();
00482     }  
00483 
00484     /**
00485      * @brief Public functions NOT inherited
00486      */
00487      
00488     /**
00489      * @brief  Attaching an error handler.
00490      * @param  fptr An error handler.
00491      * @retval None.
00492      */
00493     virtual void AttachErrorHandler(void (*fptr)(uint16_t error))
00494     {
00495         Stspin220_AttachErrorHandler((void (*)(uint16_t error)) fptr);
00496     }
00497 
00498     /**
00499      * @brief  Checks if the device is disabled or/and has an alarm flag set 
00500      * by reading the EN FAULT pin position.
00501      * @param  None.
00502      * @retval One if the EN FAULT pin is low (the device is disabled or/and 
00503      * has an alarm flag set), otherwise zero.
00504      */
00505     virtual unsigned int CheckStatusHw(void)
00506     {
00507         if (!fault_and_enable.read()) return 0x01;
00508         else return 0x00;
00509     }
00510     
00511     /**
00512      * @brief  Disabling the device.
00513      * @param  None.
00514      * @retval None.
00515      */
00516     virtual void Disable(void)
00517     {
00518         Stspin220_Disable();
00519     }
00520     
00521     /**
00522      * @brief  Enabling the device.
00523      * @param  None.
00524      * @retval None.
00525      */
00526     virtual void Enable(void)
00527     {
00528         Stspin220_Enable();
00529     }
00530     
00531     /**
00532      * @brief  Exit STSPIN220 device from standby (low power consumption) by
00533      * setting STBY\RESET pin to high level and changing the motion state to
00534      * INACTIVE.
00535      * @param  None.
00536      * @retval None.
00537      */    
00538     virtual void ExitDeviceFromStandby(void)
00539     {
00540         Stspin220_ExitDeviceFromStandby();
00541     }
00542     
00543     /**
00544      * @brief  Set the frequency of the VREFA and VREFB PWM
00545      * @param  frequency in Hz
00546      * @retval None.
00547      */  
00548     virtual uint32_t GetFreqVrefPwm(void)
00549     {
00550         return Stspin220_VrefPwmGetFreq();
00551     }    
00552     
00553     /**
00554      * @brief  Getting the version of the firmware.
00555      * @param  None.
00556      * @retval The version of the firmware.
00557      */
00558     virtual unsigned int GetFwVersion(void)
00559     {
00560         return (unsigned int) Stspin220_GetFwVersion();
00561     }
00562     
00563     /**
00564      * @brief  Getting the motor step mode.
00565      * @param  None.
00566      * @retval The motor step mode.
00567      */
00568     virtual step_mode_t GetStepMode(void)
00569     {
00570       return (step_mode_t) Stspin220_GetStepMode();
00571     }
00572     
00573     /**
00574      * @brief  Getting the motor stop mode.
00575      * @param  None.
00576      * @retval The motor stop mode.
00577      */  
00578     virtual motorStopMode_t GetStopMode(void)
00579     {
00580       return Stspin220_GetStopMode();
00581     }
00582     
00583     /**
00584      * @brief  Get the motor torque.
00585      * @param  torqueMode Torque mode as specified in enum motorTorqueMode_t
00586      * @retval the torqueValue in % (from 0 to 100)
00587      */       
00588     virtual uint8_t GetTorque(motorTorqueMode_t torqueMode)
00589     {
00590         return Stspin220_GetTorque(torqueMode);
00591     }
00592     
00593     /**
00594      * @brief  Get the torque boost feature status.
00595      * @param  None.
00596      * @retval true if enabled, false if disabled
00597      */       
00598     virtual bool GetTorqueBoostEnable(void)
00599     {
00600         return Stspin220_GetTorqueBoostEnable();
00601     }
00602     
00603     /**
00604      * @brief  Get the torque boost threshold
00605      * @retval The torque boost threshold above which the step mode is
00606      * changed to full step
00607      */       
00608     virtual uint16_t GetTorqueBoostThreshold(void)
00609     {
00610         return Stspin220_GetTorqueBoostThreshold();
00611     }
00612     
00613     /**
00614      * @brief  Going to a specified position with a specificied direction.
00615      * @param  direction The desired direction.
00616      * @param  position The desired position.
00617      * @retval None.
00618      */
00619     virtual void GoTo(direction_t direction, signed int position)
00620     {
00621         Stspin220_GoToDir((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD),(int32_t)position);
00622     }
00623 
00624     /**
00625      * @brief  Put STSPIN220 device in standby (low power consumption) by 
00626      * setting STBY\RESET pin to low level and changing the motion state to
00627      * STANDBY.
00628      * @param  None.
00629      * @retval None.
00630      */    
00631     virtual void PutDeviceInStandby(void)
00632     {
00633         Stspin220_PutDeviceInStandby();
00634     }
00635 
00636     /**
00637      * @brief  Release the STSPIN220 reset (Reset pin set to high level).
00638      * @param  None.
00639      * @retval None.
00640      */    
00641     virtual void ReleaseReset(void)
00642     {
00643         Stspin220_Board_ReleaseReset();
00644     }
00645     
00646     /**
00647      * @brief  Reset the STSPIN220 (Reset pin set to low level).
00648      * @param  None.
00649      * @retval None.
00650      */    
00651     virtual void Reset(void)
00652     {
00653         Stspin220_Board_Reset();
00654     }
00655 
00656     /**
00657      * @brief  Set the motor direction.
00658      * @param  direction The desired direction.
00659      * @retval None.
00660      */
00661     virtual void SetDirection(direction_t direction)
00662     { 
00663         Stspin220_SetDirection((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD));
00664     }
00665 
00666     /**
00667      * @brief  Set the frequency of the PWM for REF pin
00668      * @param  frequency in Hz
00669      * @retval None.
00670      */  
00671     virtual void SetFreqVrefPwm(uint32_t frequency)
00672     {
00673         Stspin220_VrefPwmSetFreq(frequency);
00674     }
00675 
00676     /**
00677      * @brief  Set the motor stop mode.
00678      * @param  stopMode The desired stop mode (HOLD_MODE, HIZ_MODE or
00679      * STANDBY_MODE).
00680      * @retval None.
00681      */       
00682     virtual void SetStopMode(motorStopMode_t stopMode)
00683     {
00684         Stspin220_SetStopMode(stopMode);
00685     }
00686 
00687     /**
00688      * @brief  Set the motor torque.
00689      * @param  torqueMode Torque mode as specified in enum motorTorqueMode_t
00690      * @param  torqueValue in % (from 0 to 100)
00691      * @retval None.
00692      */       
00693     virtual void SetTorque(motorTorqueMode_t torqueMode, uint8_t torqueValue)
00694     {
00695         Stspin220_SetTorque(torqueMode, torqueValue);
00696     }
00697 
00698     /**
00699      * @brief  Enable or disable the motor torque boost feature.
00700      * @param  enable enable true to enable torque boost, false to disable
00701      * @retval None.
00702      */       
00703     virtual void SetTorqueBoostEnable(bool enable)
00704     {
00705         Stspin220_SetTorqueBoostEnable(enable);
00706     }
00707     
00708     /**
00709      * @brief Set the torque boost threshold
00710      * @param[in] speedThreshold speed threshold above which the step mode is
00711      * changed to full step
00712      * @retval None.
00713      */       
00714     virtual void SetTorqueBoostThreshold(uint16_t speedThreshold)
00715     {
00716         Stspin220_SetTorqueBoostThreshold(speedThreshold);
00717     }
00718 
00719     /*** Public Interrupt Related Methods ***/
00720 
00721     /* ACTION 6 --------------------------------------------------------------*
00722      * Implement here interrupt related methods, if any.                      *
00723      * Note that interrupt handling is platform dependent, e.g.:              *
00724      *   + mbed:                                                              *
00725      *     InterruptIn feature_irq(pin); //Interrupt object.                  *
00726      *     feature_irq.rise(callback);   //Attach a callback.                 *
00727      *     feature_irq.mode(PullNone);   //Set interrupt mode.                *
00728      *     feature_irq.enable_irq();     //Enable interrupt.                  *
00729      *     feature_irq.disable_irq();    //Disable interrupt.                 *
00730      *   + Arduino:                                                           *
00731      *     attachInterrupt(pin, callback, RISING); //Attach a callback.       *
00732      *     detachInterrupt(pin);                   //Detach a callback.       *
00733      *                                                                        *
00734      * Example (mbed):                                                        *
00735      *   void AttachFeatureIRQ(void (*fptr) (void))                           *
00736      *   {                                                                    *
00737      *     feature_irq.rise(fptr);                                            *
00738      *   }                                                                    *
00739      *                                                                        *
00740      *   void EnableFeatureIRQ(void)                                          *
00741      *   {                                                                    *
00742      *     feature_irq.enable_irq();                                          *
00743      *   }                                                                    *
00744      *                                                                        *
00745      *   void DisableFeatureIRQ(void)                                         *
00746      *   {                                                                    *
00747      *     feature_irq.disable_irq();                                         *
00748      *   }                                                                    *
00749      *------------------------------------------------------------------------*/
00750     /**
00751      * @brief  Attaching an interrupt handler to the FLAG interrupt.
00752      * @param  fptr An interrupt handler.
00753      * @retval None.
00754      */
00755      
00756     void AttachFlagIRQ(void (*fptr)(void))
00757     {
00758         fault_and_enable_irqFunctionPointer = &(*fptr);
00759         fault_and_enable.fall(fptr);
00760         fault_and_enable.mode(PullDown);
00761         wait_ms(1);
00762     }
00763     
00764     /**
00765      * @brief  Enabling the FLAG interrupt handling.
00766      * @param  None.
00767      * @retval None.
00768      */
00769     void EnableFlagIRQ(void)
00770     {
00771         fault_and_enable.enable_irq();
00772     }
00773     
00774 protected:
00775 
00776     /*** Protected Component Related Methods ***/
00777 
00778     /* ACTION 7 --------------------------------------------------------------*
00779      * Declare here the component's specific methods.                         *
00780      * They should be:                                                        *
00781      *   + Methods with the same name of the C component's virtual table's    *
00782      *     functions (1);                                                     *
00783      *   + Methods with the same name of the C component's extended virtual   *
00784      *     table's functions, if any (2);                                     *
00785      *   + Helper methods, if any, like functions declared in the component's *
00786      *     source files but not pointed by the component's virtual table (3). *
00787      *                                                                        *
00788      * Example:                                                               *
00789      *   Status_t COMPONENT_GetValue(float *f);   //(1)                       *
00790      *   Status_t COMPONENT_EnableFeature(void);  //(2)                       *
00791      *   Status_t COMPONENT_ComputeAverage(void); //(3)                       *
00792      *------------------------------------------------------------------------*/
00793     Status_t Stspin220_Init(void *init);
00794     Status_t Stspin220_ReadID(uint8_t *id);
00795     void Stspin220_AttachErrorHandler(void (*callback)(uint16_t error));
00796     void Stspin220_ApplyTorque(motorTorqueMode_t torqueMode);
00797     void Stspin220_Disable(void);
00798     void Stspin220_ErrorHandler(uint16_t error);
00799     void Stspin220_Enable(void);
00800     void Stspin220_ExitDeviceFromStandby(void);
00801     uint16_t Stspin220_GetAcceleration(void);
00802     uint16_t Stspin220_GetCurrentSpeed(void);
00803     uint16_t Stspin220_GetDeceleration(void);
00804     motorState_t Stspin220_GetDeviceState(void);
00805     motorDir_t Stspin220_GetDirection(void);
00806     uint32_t Stspin220_GetFwVersion(void);
00807     int32_t Stspin220_GetMark(void);
00808     uint16_t Stspin220_GetMaxSpeed(void);
00809     uint16_t Stspin220_GetMinSpeed(void);
00810     int32_t Stspin220_GetPosition(void);
00811     motorStepMode_t Stspin220_GetStepMode(void);
00812     motorStopMode_t Stspin220_GetStopMode(void);
00813     uint8_t Stspin220_GetTorque(motorTorqueMode_t torqueMode);
00814     bool Stspin220_GetTorqueBoostEnable(void);
00815     uint16_t Stspin220_GetTorqueBoostThreshold(void);
00816     void Stspin220_GoHome(void);  
00817     void Stspin220_GoMark(void);
00818     void Stspin220_GoTo(int32_t targetPosition);
00819     void Stspin220_GoToDir(motorDir_t direction, int32_t targetPosition);   
00820     void Stspin220_HardHiZ(void);
00821     void Stspin220_HardStop(void);
00822     void Stspin220_Move(motorDir_t direction, uint32_t stepCount);
00823     void Stspin220_PutDeviceInStandby(void);
00824     void Stspin220_Run(motorDir_t direction);
00825     bool Stspin220_SetAcceleration(uint16_t newAcc);
00826     bool Stspin220_SetDeceleration(uint16_t newDec);
00827     void Stspin220_SetDirection(motorDir_t direction);
00828     void Stspin220_SetHome(void);
00829     void Stspin220_SetMark(void);
00830     bool Stspin220_SetMaxSpeed(uint16_t volatile newSpeed);
00831     bool Stspin220_SetMinSpeed(uint16_t volatile newSpeed);    
00832     bool Stspin220_SetStepMode(motorStepMode_t stepMode);
00833     void Stspin220_SetStopMode(motorStopMode_t stopMode);
00834     bool Stspin220_SoftStop(void);
00835     void Stspin220_SetTorque(motorTorqueMode_t torqueMode, uint8_t torqueValue);
00836     void Stspin220_SetTorqueBoostEnable(bool enable);
00837     void Stspin220_SetTorqueBoostThreshold(uint16_t speedThreshold);
00838     uint32_t Stspin220_VrefPwmGetFreq(void);
00839     void Stspin220_VrefPwmSetFreq(uint32_t newFreq);
00840     void Stspin220_WaitWhileActive(void);
00841     
00842     /*** Functions intended to be used only internally ***/
00843     void Stspin220_ApplySpeed(uint16_t newSpeed);
00844     void Stspin220_ComputeSpeedProfile(uint32_t nbSteps);    
00845     void Stspin220_SetDeviceParamsToGivenValues(Stspin220_Init_t* pInitDevicePrm);
00846     void Stspin220_SetDeviceParamsOtherValues(void);    
00847     void Stspin220_SetDeviceParamsToPredefinedValues(void);
00848     bool Stspin220_SetStepModeWithoutReset(motorStepMode_t stepMode);
00849     void Stspin220_StartMovement(void);
00850     void Stspin220_StepClockHandler(void);
00851     
00852     /*** Component's I/O Methods ***/
00853 
00854     /* ACTION 8 --------------------------------------------------------------*
00855      * Implement here other I/O methods beyond those already implemented      *
00856      * above, which are declared extern within the component's header file.   *
00857      *------------------------------------------------------------------------*/
00858     /**
00859      * @brief  Making the CPU wait.
00860      * @param  None.
00861      * @retval None.
00862      */
00863     void Stspin220_Board_Delay(uint32_t delay)
00864     {
00865         wait_ms(delay);
00866     }
00867     
00868     /**
00869      * @brief  Disable the power bridges (leave the output bridges HiZ).
00870      * @param  None.
00871      * @retval None.
00872      */
00873     void Stspin220_Board_Disable(void)
00874     {
00875         fault_and_enable.disable_irq();
00876         DigitalOut fault_and_enable(fault_and_enable_pinName);
00877         fault_and_enable.write(0);
00878     }
00879 
00880     /**
00881      * @brief  Disabling interrupts.
00882      * @param  None.
00883      * @retval None.
00884      */
00885     void Stspin220_Board_DisableIrq(void)
00886     {
00887         __disable_irq();
00888     }
00889     
00890     /**
00891      * @brief  Enable the power bridges (leave the output bridges HiZ).
00892      * @param  None.
00893      * @retval None.
00894      */
00895     void Stspin220_Board_Enable(void)
00896     {
00897         DigitalOut fault_and_enable_do(fault_and_enable_pinName);
00898         fault_and_enable_do.write(1);
00899         fault_and_enable.fall(fault_and_enable_irqFunctionPointer);
00900         fault_and_enable.mode(PullUp);
00901         wait_ms(1);
00902         fault_and_enable.enable_irq();
00903     }
00904     
00905     /**
00906      * @brief  Enabling interrupts.
00907      * @param  None.
00908      * @retval None.
00909      */
00910     void Stspin220_Board_EnableIrq(void)
00911     {
00912         __enable_irq();
00913     }
00914 
00915     /**
00916      * @brief  Set the duty cycle of the PwmOut used for the REF
00917      * reference voltage generation and actually start the pwm if the duty cycle
00918      * is not zero.
00919      * @param[in] dutyCycle 0 - 100%
00920      * @retval None
00921      */
00922     void Stspin220_Board_PwmRefSetDutyCycle(uint8_t dutyCycle)
00923     {
00924         pwm_ref.write(((float)(100-dutyCycle))/100);
00925     }
00926 
00927     /**
00928      * @brief  Set the frequency of the PwmOut used for the REF
00929      * reference voltage generation.
00930      * @param[in] newFreq in Hz.
00931      * @retval None.
00932      */
00933     void Stspin220_Board_PwmRefSetFreq(uint32_t newFreq)
00934     {
00935         pwm_ref.period_us(1000000.0f/newFreq);
00936     }
00937     
00938     /**
00939      * @brief  Start the PwmOut for the REF pin.
00940      * @param[in] frequency frequency of the PwmOut used to generate the REF
00941      * reference voltage for the bridges.
00942      * @param[in] torqueMode Torque mode as specified in enum motorTorqueMode_t    
00943      * @retval None.
00944      */ 
00945     void Stspin220_Board_PwmRefStart(uint32_t frequency, motorTorqueMode_t torqueMode)
00946     { 
00947         /* Setting the period of the PwmOut. */
00948         pwm_ref.period_us(1000000.0f/frequency);
00949         /* Setting the duty cycle and actually starting the of the PwmOut. */
00950         Stspin220_ApplyTorque(torqueMode);
00951         wait_ms(5*PWM_FILTER_TIME_CONSTANT);
00952     }
00953 
00954     /**
00955      * @brief  Exit the device from standby reset mode.
00956      * @param  None.
00957      * @retval None.
00958      */
00959     void Stspin220_Board_ReleaseReset(void)
00960     {
00961         stby_reset = 1;
00962     }
00963 
00964     /**
00965      * @brief  Put the device in standby reset mode.
00966      * @param  None.
00967      * @retval None.
00968      */
00969     void Stspin220_Board_Reset(void)
00970     {
00971         stby_reset = 0;
00972     }
00973 
00974     /**
00975      * @brief  Set the DIR\MODE4 pin.
00976      * @param  dir motor direction FORWARD or BACKWARD.
00977      * @retval None.
00978      */
00979     void Stspin220_Board_SetDirectionGpio(motorDir_t dir)
00980     {
00981         if (dir==BACKWARD) direction_mode4 = 0;
00982         else direction_mode4 = 1;
00983     }
00984  
00985     /**
00986      * @brief Select Full Step mode
00987      * @param  None.
00988      * @retval None
00989      */
00990     void Stspin220_Board_SetFullStep(void)
00991     {
00992         mode1 = 0;
00993         mode2 = 0;
00994     }
00995     
00996     /**
00997      * @brief Select the STSPIN220 mode1, mode2, mode3 and mode4 pins levels.
00998      * @param[in] modePin1Level level of the mode1 gpio (0 low, 1+ high)
00999      * @param[in] modePin2Level level of the mode2 gpio (0 low, 1+ high)
01000      * @param[in] modePin3Level level of the mode3 gpio (0 low, 1+ high)
01001      * @param[in] modePin4Level level of the mode4 gpio (0 low, 1+ high)
01002      * @retval 1
01003      */
01004     uint8_t Stspin220_Board_SetModePins(uint8_t modePin1Level,\
01005         uint8_t modePin2Level,\
01006         uint8_t modePin3Level,\
01007         uint8_t modePin4Level)
01008     {
01009         mode1 = modePin1Level;
01010         mode2 = modePin2Level;
01011         stck_mode3 = modePin3Level;
01012         direction_mode4 = modePin4Level;
01013         return 1;
01014     }
01015 
01016     /**
01017      * @brief  Reset the STCK\MODE3 pin.
01018      * @param  None.
01019      * @retval None.
01020      */
01021     void Stspin220_Board_StckMode3_Reset(void)
01022     {
01023         stck_mode3 = 0;
01024     }
01025 
01026     /**
01027      * @brief  Set the STCK\MODE3 pin.
01028      * @param  None.
01029      * @retval None.
01030      */
01031     void Stspin220_Board_StckMode3_Set(void)
01032     {
01033         stck_mode3 = 1;
01034     }
01035 
01036     /**
01037      * @brief Initialises the step clock pin level
01038      * @param  None.
01039      * @retval None
01040      */
01041     void Stspin220_Board_TimStckInit(bool check)
01042     {
01043         if (monitor.is_connected()) monitor = 0;
01044         if (check==false) stck_mode3 = 0;
01045         else if (stck_mode3==1) Stspin220_ErrorHandler(STSPIN220_ERROR_STEP_CLOCK);
01046     }
01047 
01048     /**
01049      * @brief  Setting the Stck Timeout delay
01050      * and attaching a callback function to it.
01051      * @param  frequency The frequency corresponding to the delay.
01052      * @retval None.
01053      */
01054     void Stspin220_Board_TimStckSetFreq(uint16_t newFreq)
01055     {
01056         /* Computing the delay of the Timeout. */
01057         float delay_us = (1000000.0f / 2 )/ newFreq;
01058         
01059         /* Attaching a function which updates */
01060         /* the state machine after the elapsed period_us time. */
01061         tim_stck.attach_us(this, &STSPIN220::Stspin220_StepClockHandler, delay_us);
01062     }
01063 
01064     void Stspin220_Board_Monitor_Set(void)
01065     {
01066         if (monitor.is_connected()) monitor = 1;
01067     }
01068     
01069     void Stspin220_Board_Monitor_Reset(void)
01070     {
01071         if (monitor.is_connected()) monitor = 0;
01072     }
01073 
01074     /**
01075      * @brief  Stopping the Timeout.
01076      * @param  None.
01077      * @retval None.
01078      */
01079     uint8_t Stspin220_Board_TimStckStop(volatile uint8_t *pToggleOdd)
01080     {
01081         __disable_irq();
01082         if (*pToggleOdd == 1)
01083         {
01084             __enable_irq();
01085             return 1;
01086         }
01087         if (stck_mode3 != 0)
01088         {
01089             __enable_irq();
01090             return 0;
01091         }
01092         tim_stck.detach();
01093         __enable_irq();
01094         return 1;
01095     }
01096 
01097     /**
01098      * @brief Unselect Full Step mode
01099      * @param  None.
01100      * @retval None
01101      */
01102     void Stspin220_Board_UnsetFullStep(void)
01103     {
01104         mode1 = 1;
01105     }
01106 
01107 protected:
01108 
01109     /*** Component's Instance Variables ***/
01110 
01111     /* ACTION 9 --------------------------------------------------------------*
01112      * Declare here interrupt related variables, if needed.                   *
01113      * Note that interrupt handling is platform dependent, see                *
01114      * "Interrupt Related Methods" above.                                     *
01115      *                                                                        *
01116      * Example:                                                               *
01117      *   + mbed:                                                              *
01118      *     InterruptIn feature_irq;                                           *
01119      *------------------------------------------------------------------------*/
01120     /* Fault Interrupt and chip enable. */
01121     InterruptIn fault_and_enable;
01122     
01123     /* Interrupt to toggle the MODE3\STCK pin */
01124     Timeout tim_stck;
01125 
01126     /* ACTION 10 -------------------------------------------------------------*
01127      * Declare here other pin related variables, if needed.                   *
01128      *                                                                        *
01129      * Example:                                                               *
01130      *   + mbed:                                                              *
01131      *     DigitalOut standby_reset;                                          *
01132      *------------------------------------------------------------------------*/
01133     /* STBY\RESET pin. */
01134     DigitalOut stby_reset;
01135     /* MODE4\DIR pin. */
01136     DigitalOut direction_mode4;
01137     /* MODE1 pin */
01138     DigitalOut mode1;
01139     /* MODE2 pin */
01140     DigitalOut mode2;
01141     /* MODE3\STCK pin. */
01142     DigitalOut stck_mode3;
01143 
01144     /* Pulse Width Modulation pin for REF pin */
01145     PwmOut pwm_ref;
01146  
01147     /* Monitoring of step clock handler duration */
01148     DigitalOut monitor;
01149 
01150     /* fault and enable pin */
01151     PinName fault_and_enable_pinName;
01152     
01153     /* ACTION 11 -------------------------------------------------------------*
01154      * Declare here communication related variables, if needed.               *
01155      *                                                                        *
01156      * Example:                                                               *
01157      *   + mbed:                                                              *
01158      *     DigitalOut ssel;                                                   *
01159      *     DevSPI &dev_spi;                                                   *
01160      *------------------------------------------------------------------------*/
01161     /* Configuration. */
01162 
01163     /* IO Device. */
01164 
01165     /* ACTION 12 -------------------------------------------------------------*
01166      * Declare here identity related variables, if needed.                    *
01167      * Note that there should be only a unique identifier for each component, *
01168      * which should be the "who_am_i" parameter.                              *
01169      *------------------------------------------------------------------------*/
01170     /* Identity */
01171     uint8_t who_am_i;
01172 
01173     /* ACTION 13 -------------------------------------------------------------*
01174      * Declare here the component's static and non-static data, one variable  *
01175      * per line.                                                              *
01176      *                                                                        *
01177      * Example:                                                               *
01178      *   float measure;                                                       *
01179      *   int instance_id;                                                     *
01180      *   static int number_of_instances;                                      *
01181      *------------------------------------------------------------------------*/
01182     /* Data. */
01183     void (*fault_and_enable_irqFunctionPointer)(void);
01184     void (*errorHandlerCallback)(uint16_t error);
01185     deviceParams_t devicePrm;
01186     uint8_t deviceInstance;
01187     volatile uint8_t toggleOdd;
01188 
01189     /* Static data. */
01190     static uint8_t numberOfDevices;
01191 
01192 public:
01193 
01194     /* Static data. */
01195     
01196 };
01197 
01198 #endif // __STSPIN220_CLASS_H
01199 
01200 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/