Library to handle the X-NUCLEO-IHM06A1 Motor Control Expansion Board based on the STSPIN220 component.

Dependencies:   ST_INTERFACES

Dependents:   HelloWorld_IHM06A1

Fork of X-NUCLEO-IHM06A1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STSpin220.h Source File

STSpin220.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    STSpin220.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_def.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         {
00107             error("Instantiation of the STSpin220 component failed: it can't be stacked on itself.\r\n");
00108         }
00109 
00110         /* ACTION 4 ----------------------------------------------------------*
00111          * Initialize here the component's member variables, one variable per *
00112          * line.                                                              *
00113          *                                                                    *
00114          * Example:                                                           *
00115          *   measure = 0;                                                     *
00116          *   instance_id = number_of_instances++;                             *
00117          *--------------------------------------------------------------------*/
00118         errorHandlerCallback = 0;
00119         memset(&devicePrm, 0, sizeof(devicePrm));
00120         deviceInstance = numberOfDevices++;
00121         
00122         fault_and_enable_pinName = fault_and_enable_pin;
00123     }
00124     
00125     /**
00126      * @brief Destructor.
00127      */
00128     virtual ~STSpin220(void) {}
00129     
00130 
00131     /*** Public Component Related Methods ***/
00132 
00133     /* ACTION 5 --------------------------------------------------------------*
00134      * Implement here the component's public methods, as wrappers of the C    *
00135      * component's functions.                                                 *
00136      * They should be:                                                        *
00137      *   + Methods with the same name of the C component's virtual table's    *
00138      *     functions (1);                                                     *
00139      *   + Methods with the same name of the C component's extended virtual   *
00140      *     table's functions, if any (2).                                     *
00141      *                                                                        *
00142      * Example:                                                               *
00143      *   virtual int get_value(float *pData) //(1)                            *
00144      *   {                                                                    *
00145      *     return COMPONENT_get_value(float *pfData);                         *
00146      *   }                                                                    *
00147      *                                                                        *
00148      *   virtual int enable_feature(void) //(2)                               *
00149      *   {                                                                    *
00150      *     return COMPONENT_enable_feature();                                 *
00151      *   }                                                                    *
00152      *------------------------------------------------------------------------*/
00153 
00154     /**
00155      * @brief Public functions inherited from the Component Class
00156      */
00157 
00158     /**
00159      * @brief  Initialize the component.
00160      * @param  init Pointer to device specific initalization structure.
00161      * @retval "0" in case of success, an error code otherwise.
00162      */
00163     virtual int init(void *init = NULL)
00164     {
00165         return (int) STSpin220_Init((void *) init);
00166     }
00167 
00168     /**
00169      * @brief  Getting the ID of the component.
00170      * @param  id Pointer to an allocated variable to store the ID into.
00171      * @retval "0" in case of success, an error code otherwise.
00172      */
00173     virtual int read_id(uint8_t *id = NULL)
00174     {
00175         return (int) STSpin220_ReadID((uint8_t *) id);
00176     }
00177 
00178     /**
00179      * @brief Public functions inherited from the StepperMotor Class
00180      */
00181 
00182     /**
00183      * @brief  Getting the value of the motor state .
00184      * @param  None.
00185      * @retval The motor state accoring to motorState_t in motor.h
00186      */
00187     virtual unsigned int get_status(void)
00188     {
00189         return (unsigned int) STSpin220_get_device_state();
00190     }
00191 
00192     /**
00193      * @brief  Getting the position.
00194      * @param  None.
00195      * @retval The position.
00196      */
00197     virtual signed int get_position(void)
00198     {
00199         return (signed int)STSpin220_GetPosition();
00200     }
00201 
00202     /**
00203      * @brief  Getting the marked position.
00204      * @param  None.
00205      * @retval The marked position.
00206      */
00207     virtual signed int get_mark(void)
00208     {
00209         return (signed int)STSpin220_GetMark();
00210     }
00211 
00212     /**
00213      * @brief  Getting the current speed in pps.
00214      * @param  None.
00215      * @retval The current speed in pps.
00216      */
00217     virtual unsigned int get_speed(void)
00218     {
00219         return (unsigned int)STSpin220_GetCurrentSpeed();
00220     }
00221 
00222     /**
00223      * @brief  Getting the maximum speed in pps.
00224      * @param  None.
00225      * @retval The maximum speed in pps.
00226      */
00227     virtual unsigned int get_max_speed(void)
00228     {
00229         return (unsigned int)STSpin220_GetMaxSpeed();
00230     }
00231 
00232     /**
00233      * @brief  Getting the minimum speed in pps.
00234      * @param  None.
00235      * @retval The minimum speed in pps.
00236      */
00237     virtual unsigned int get_min_speed(void)
00238     {
00239         return (unsigned int)STSpin220_GetMinSpeed();
00240     }
00241 
00242     /**
00243      * @brief  Getting the acceleration in pps^2.
00244      * @param  None.
00245      * @retval The acceleration in pps^2.
00246      */
00247     virtual unsigned int get_acceleration(void)
00248     {
00249         return (unsigned int)STSpin220_GetAcceleration();
00250     }
00251 
00252     /**
00253      * @brief  Getting the deceleration in pps^2.
00254      * @param  None.
00255      * @retval The deceleration in pps^2.
00256      */
00257     virtual unsigned int get_deceleration(void)
00258     {
00259         return (unsigned int)STSpin220_GetDeceleration();
00260     }
00261     
00262     /**
00263      * @brief  Getting the direction of rotation.
00264      * @param  None.
00265      * @retval The direction of rotation.
00266      */
00267     virtual direction_t get_direction(void)
00268     {
00269         if (STSpin220_GetDirection()!=BACKWARD)
00270         {
00271             return FWD;
00272         }
00273         else
00274         {
00275             return BWD;
00276         }
00277     }
00278     
00279     /**
00280      * @brief  Setting the current position to be the home position.
00281      * @param  None.
00282      * @retval None.
00283      */
00284     virtual void set_home(void)
00285     {
00286         STSpin220_SetHome();
00287     }
00288 
00289     /**
00290      * @brief  Setting the current position to be the marked position.
00291      * @param  None.
00292      * @retval None.
00293      */
00294     virtual void set_mark(void)
00295     {
00296         STSpin220_SetMark();
00297     }
00298 
00299     /**
00300      * @brief  Setting the maximum speed in pps.
00301      * @param  speed The maximum speed in pps.
00302      * @retval "true" in case of success, "false" otherwise.
00303      */
00304     virtual bool set_max_speed(unsigned int speed)
00305     {
00306         if (speed <= 0xFFFF)
00307         {
00308             return STSpin220_SetMaxSpeed((uint16_t) speed);
00309         }
00310         else
00311         {
00312             return false;
00313         }
00314     }
00315 
00316     /**
00317      * @brief  Setting the minimum speed in pps.
00318      * @param  speed The minimum speed in pps.
00319      * @retval "true" in case of success, "false" otherwise.
00320      */
00321     virtual bool set_min_speed(unsigned int speed)
00322     {
00323         if (speed <= 0xFFFF)
00324         {
00325             return STSpin220_SetMinSpeed((uint16_t) speed);
00326         }
00327         else
00328         {
00329             return false;
00330         }
00331     }
00332 
00333     /**
00334      * @brief  Setting the acceleration in pps^2.
00335      * @param  acceleration The acceleration in pps/s^2.
00336      * @retval "true" in case of success, "false" otherwise.
00337      */
00338     virtual bool set_acceleration(unsigned int acceleration)
00339     {
00340         if (acceleration <= 0xFFFF)
00341         {
00342             return STSpin220_SetAcceleration((uint16_t) acceleration);
00343         }
00344         else
00345         {
00346             return false;
00347         }
00348     }
00349 
00350     /**
00351      * @brief  Setting the deceleration in pps^2.
00352      * @param  deceleration The deceleration in pps^2.
00353      * @retval "true" in case of success, "false" otherwise.
00354      */
00355     virtual bool set_deceleration(unsigned int deceleration)
00356     {
00357         if (deceleration <= 0xFFFF)
00358         {
00359             return STSpin220_SetDeceleration((uint16_t) deceleration);
00360         }
00361         else
00362         {
00363             return false;
00364         }
00365     }
00366 
00367     /**
00368      * @brief  Setting the Step Mode.
00369      * @param  step_mode The Step Mode.
00370      * @retval "true" in case of success, "false" otherwise.
00371      * @note   step_mode can be one of the following:
00372      *           + STEP_MODE_FULL
00373      *           + STEP_MODE_WAVE
00374      *           + STEP_MODE_HALF
00375      *           + STEP_MODE_1_4
00376      *           + STEP_MODE_1_8
00377      *           + STEP_MODE_1_16
00378      */
00379     virtual bool set_step_mode(step_mode_t step_mode)
00380     {
00381         return STSpin220_SetStepMode((motorStepMode_t) step_mode);
00382     }
00383 
00384     /**
00385      * @brief  Going to a specified position.
00386      * @param  position The desired position.
00387      * @retval None.
00388      */
00389     virtual void go_to(signed int position)
00390     {
00391         STSpin220_GoTo((int32_t)position);
00392     }
00393 
00394     /**
00395      * @brief  Going to the home position.
00396      * @param  None.
00397      * @retval None.
00398      */
00399     virtual void go_home(void)
00400     {
00401         STSpin220_GoHome();
00402     }
00403 
00404     /**
00405      * @brief  Going to the marked position.
00406      * @param  None.
00407      * @retval None.
00408      */
00409     virtual void go_mark(void)
00410     {
00411         STSpin220_GoMark();
00412     }
00413 
00414     /**
00415      * @brief  Running the motor towards a specified direction.
00416      * @param  direction The direction of rotation.
00417      * @retval None.
00418      */
00419     virtual void run(direction_t direction)
00420     {
00421         STSpin220_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD));
00422     }
00423 
00424     /**
00425      * @brief  Moving the motor towards a specified direction for a certain number of steps.
00426      * @param  direction The direction of rotation.
00427      * @param  steps The desired number of steps.
00428      * @retval None.
00429      */
00430     virtual void move(direction_t direction, unsigned int steps)
00431     {
00432         STSpin220_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (uint32_t)steps);
00433     }
00434 
00435     /**
00436      * @brief  Stopping the motor through an immediate deceleration up to zero speed.
00437      * @param  None.
00438      * @retval None.
00439      */
00440     virtual void soft_stop(void)
00441     {
00442         STSpin220_SoftStop();
00443     }
00444 
00445     /**
00446      * @brief  Stopping the motor through an immediate infinite deceleration.
00447      * @param  None.
00448      * @retval None.
00449      */
00450     virtual void hard_stop(void)
00451     {
00452         STSpin220_HardStop();
00453     }
00454 
00455     /**
00456      * @brief  Disabling the power bridge after performing a deceleration to zero.
00457      * @param  None.
00458      * @retval None.
00459      */
00460     virtual void soft_hiz(void)
00461     {
00462         STSpin220_SetStopMode(HIZ_MODE);
00463         STSpin220_SoftStop();
00464     }
00465 
00466     /**
00467      * @brief  Disabling the power bridge immediately.
00468      * @param  None.
00469      * @retval None.
00470      */
00471     virtual void hard_hiz(void)
00472     {
00473         STSpin220_HardHiZ();
00474     }
00475 
00476     /**
00477      * @brief  Waiting while the motor is active.
00478      * @param  None.
00479      * @retval None.
00480      */
00481     virtual void wait_while_active(void)
00482     {
00483         STSpin220_WaitWhileActive();
00484     }  
00485 
00486     /**
00487      * @brief Public functions NOT inherited
00488      */
00489      
00490     /**
00491      * @brief  Attaching an error handler.
00492      * @param  fptr An error handler.
00493      * @retval None.
00494      */
00495     virtual void attach_error_handler(void (*fptr)(uint16_t error))
00496     {
00497         STSpin220_AttachErrorHandler((void (*)(uint16_t error)) fptr);
00498     }
00499 
00500     /**
00501      * @brief  Checks if the device is disabled or/and has an alarm flag set 
00502      * by reading the EN FAULT pin position.
00503      * @param  None.
00504      * @retval One if the EN FAULT pin is low (the device is disabled or/and 
00505      * has an alarm flag set), otherwise zero.
00506      */
00507     virtual unsigned int check_status_hw(void)
00508     {
00509         if (!fault_and_enable.read()) return 0x01;
00510         else return 0x00;
00511     }
00512     
00513     /**
00514      * @brief  Disabling the device.
00515      * @param  None.
00516      * @retval None.
00517      */
00518     virtual void disable(void)
00519     {
00520         STSpin220_Disable();
00521     }
00522     
00523     /**
00524      * @brief  Enabling the device.
00525      * @param  None.
00526      * @retval None.
00527      */
00528     virtual void enable(void)
00529     {
00530         STSpin220_Enable();
00531     }
00532     
00533     /**
00534      * @brief  Exit STSpin220 device from standby (low power consumption) by
00535      * setting STBY\RESET pin to high level and changing the motion state to
00536      * INACTIVE.
00537      * @param  None.
00538      * @retval None.
00539      */    
00540     virtual void exit_device_from_standby(void)
00541     {
00542         STSpin220_ExitDeviceFromStandby();
00543     }
00544     
00545     /**
00546      * @brief  Set the frequency of the VREFA and VREFB PWM
00547      * @param  frequency in Hz
00548      * @retval None.
00549      */  
00550     virtual uint32_t get_freq_vref_pwm(void)
00551     {
00552         return STSpin220_VrefPwmGetFreq();
00553     }    
00554     
00555     /**
00556      * @brief  Getting the version of the firmware.
00557      * @param  None.
00558      * @retval The version of the firmware.
00559      */
00560     virtual unsigned int get_fw_version(void)
00561     {
00562         return (unsigned int) STSpin220_GetFwVersion();
00563     }
00564     
00565     /**
00566      * @brief  Getting the motor step mode.
00567      * @param  None.
00568      * @retval The motor step mode.
00569      */
00570     virtual step_mode_t get_step_mode(void)
00571     {
00572       return (step_mode_t) STSpin220_GetStepMode();
00573     }
00574     
00575     /**
00576      * @brief  Getting the motor stop mode.
00577      * @param  None.
00578      * @retval The motor stop mode.
00579      */  
00580     virtual motorStopMode_t get_stop_mode(void)
00581     {
00582       return STSpin220_GetStopMode();
00583     }
00584     
00585     /**
00586      * @brief  Get the motor torque.
00587      * @param  torqueMode Torque mode as specified in enum motorTorqueMode_t
00588      * @retval the torqueValue in % (from 0 to 100)
00589      */       
00590     virtual uint8_t get_torque(motorTorqueMode_t torqueMode)
00591     {
00592         return STSpin220_GetTorque(torqueMode);
00593     }
00594     
00595     /**
00596      * @brief  Get the torque boost feature status.
00597      * @param  None.
00598      * @retval true if enabled, false if disabled
00599      */       
00600     virtual bool get_torque_boost_enable(void)
00601     {
00602         return STSpin220_GetTorqueBoostEnable();
00603     }
00604     
00605     /**
00606      * @brief  Get the torque boost threshold
00607      * @retval The torque boost threshold above which the step mode is
00608      * changed to full step
00609      */       
00610     virtual uint16_t get_torque_boost_threshold(void)
00611     {
00612         return STSpin220_GetTorqueBoostThreshold();
00613     }
00614     
00615     /**
00616      * @brief  Going to a specified position with a specificied direction.
00617      * @param  direction The desired direction.
00618      * @param  position The desired position.
00619      * @retval None.
00620      */
00621     virtual void go_to(direction_t direction, signed int position)
00622     {
00623         STSpin220_GoToDir((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD),(int32_t)position);
00624     }
00625 
00626     /**
00627      * @brief  Put STSpin220 device in standby (low power consumption) by 
00628      * setting STBY\RESET pin to low level and changing the motion state to
00629      * STANDBY.
00630      * @param  None.
00631      * @retval None.
00632      */    
00633     virtual void put_device_in_standby(void)
00634     {
00635         STSpin220_PutDeviceInStandby();
00636     }
00637 
00638     /**
00639      * @brief  Release the STSpin220 reset (Reset pin set to high level).
00640      * @param  None.
00641      * @retval None.
00642      */    
00643     virtual void release_reset(void)
00644     {
00645         STSpin220_Board_ReleaseReset();
00646     }
00647     
00648     /**
00649      * @brief  Reset the STSpin220 (Reset pin set to low level).
00650      * @param  None.
00651      * @retval None.
00652      */    
00653     virtual void reset(void)
00654     {
00655         STSpin220_Board_Reset();
00656     }
00657 
00658     /**
00659      * @brief  Set the motor direction.
00660      * @param  direction The desired direction.
00661      * @retval None.
00662      */
00663     virtual void set_direction(direction_t direction)
00664     { 
00665         STSpin220_SetDirection((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD));
00666     }
00667 
00668     /**
00669      * @brief  Set the frequency of the PWM for REF pin
00670      * @param  frequency in Hz
00671      * @retval None.
00672      */  
00673     virtual void set_freq_vref_pwm(uint32_t frequency)
00674     {
00675         STSpin220_VrefPwmSetFreq(frequency);
00676     }
00677 
00678     /**
00679      * @brief  Set the motor stop mode.
00680      * @param  stopMode The desired stop mode (HOLD_MODE, HIZ_MODE or
00681      * STANDBY_MODE).
00682      * @retval None.
00683      */       
00684     virtual void set_stop_mode(motorStopMode_t stopMode)
00685     {
00686         STSpin220_SetStopMode(stopMode);
00687     }
00688 
00689     /**
00690      * @brief  Set the motor torque.
00691      * @param  torqueMode Torque mode as specified in enum motorTorqueMode_t
00692      * @param  torqueValue in % (from 0 to 100)
00693      * @retval None.
00694      */       
00695     virtual void set_torque(motorTorqueMode_t torqueMode, uint8_t torqueValue)
00696     {
00697         STSpin220_SetTorque(torqueMode, torqueValue);
00698     }
00699 
00700     /**
00701      * @brief  Enable or disable the motor torque boost feature.
00702      * @param  enable enable true to enable torque boost, false to disable
00703      * @retval None.
00704      */       
00705     virtual void set_torque_boost_enable(bool enable)
00706     {
00707         STSpin220_SetTorqueBoostEnable(enable);
00708     }
00709     
00710     /**
00711      * @brief Set the torque boost threshold
00712      * @param[in] speedThreshold speed threshold above which the step mode is
00713      * changed to full step
00714      * @retval None.
00715      */       
00716     virtual void set_torque_boost_threshold(uint16_t speedThreshold)
00717     {
00718         STSpin220_SetTorqueBoostThreshold(speedThreshold);
00719     }
00720 
00721     /*** Public Interrupt Related Methods ***/
00722 
00723     /* ACTION 6 --------------------------------------------------------------*
00724      * Implement here interrupt related methods, if any.                      *
00725      * Note that interrupt handling is platform dependent, e.g.:              *
00726      *   + mbed:                                                              *
00727      *     InterruptIn feature_irq(pin); //Interrupt object.                  *
00728      *     feature_irq.rise(callback);   //Attach a callback.                 *
00729      *     feature_irq.mode(PullNone);   //Set interrupt mode.                *
00730      *     feature_irq.enable_irq();     //Enable interrupt.                  *
00731      *     feature_irq.disable_irq();    //Disable interrupt.                 *
00732      *   + Arduino:                                                           *
00733      *     attachInterrupt(pin, callback, RISING); //Attach a callback.       *
00734      *     detachInterrupt(pin);                   //Detach a callback.       *
00735      *                                                                        *
00736      * Example (mbed):                                                        *
00737      *   void attach_feature_irq(void (*fptr) (void))                         *
00738      *   {                                                                    *
00739      *     feature_irq.rise(fptr);                                            *
00740      *   }                                                                    *
00741      *                                                                        *
00742      *   void enable_feature_irq(void)                                        *
00743      *   {                                                                    *
00744      *     feature_irq.enable_irq();                                          *
00745      *   }                                                                    *
00746      *                                                                        *
00747      *   void disable_feature_irq(void)                                       *
00748      *   {                                                                    *
00749      *     feature_irq.disable_irq();                                         *
00750      *   }                                                                    *
00751      *------------------------------------------------------------------------*/
00752     /**
00753      * @brief  Attaching an interrupt handler to the FLAG interrupt.
00754      * @param  fptr An interrupt handler.
00755      * @retval None.
00756      */
00757      
00758     void attach_flag_irq(void (*fptr)(void))
00759     {
00760         fault_and_enable_irqFunctionPointer = &(*fptr);
00761         fault_and_enable.fall(fptr);
00762         fault_and_enable.mode(PullDown);
00763         wait_ms(1);
00764     }
00765     
00766     /**
00767      * @brief  Enabling the FLAG interrupt handling.
00768      * @param  None.
00769      * @retval None.
00770      */
00771     void enable_flag_irq(void)
00772     {
00773         fault_and_enable.enable_irq();
00774     }
00775     
00776 protected:
00777 
00778     /*** Protected Component Related Methods ***/
00779 
00780     /* ACTION 7 --------------------------------------------------------------*
00781      * Declare here the component's specific methods.                         *
00782      * They should be:                                                        *
00783      *   + Methods with the same name of the C component's virtual table's    *
00784      *     functions (1);                                                     *
00785      *   + Methods with the same name of the C component's extended virtual   *
00786      *     table's functions, if any (2);                                     *
00787      *   + Helper methods, if any, like functions declared in the component's *
00788      *     source files but not pointed by the component's virtual table (3). *
00789      *                                                                        *
00790      * Example:                                                               *
00791      *   status_t COMPONENT_get_value(float *f);   //(1)                      *
00792      *   status_t COMPONENT_enable_feature(void);  //(2)                      *
00793      *   status_t COMPONENT_compute_average(void); //(3)                      *
00794      *------------------------------------------------------------------------*/
00795     status_t STSpin220_Init(void *init);
00796     status_t STSpin220_ReadID(uint8_t *id);
00797     void STSpin220_AttachErrorHandler(void (*callback)(uint16_t error));
00798     void STSpin220_ApplyTorque(motorTorqueMode_t torqueMode);
00799     void STSpin220_Disable(void);
00800     void STSpin220_ErrorHandler(uint16_t error);
00801     void STSpin220_Enable(void);
00802     void STSpin220_ExitDeviceFromStandby(void);
00803     uint16_t STSpin220_GetAcceleration(void);
00804     uint16_t STSpin220_GetCurrentSpeed(void);
00805     uint16_t STSpin220_GetDeceleration(void);
00806     motorState_t STSpin220_get_device_state(void);
00807     motorDir_t STSpin220_GetDirection(void);
00808     uint32_t STSpin220_GetFwVersion(void);
00809     int32_t STSpin220_GetMark(void);
00810     uint16_t STSpin220_GetMaxSpeed(void);
00811     uint16_t STSpin220_GetMinSpeed(void);
00812     int32_t STSpin220_GetPosition(void);
00813     motorStepMode_t STSpin220_GetStepMode(void);
00814     motorStopMode_t STSpin220_GetStopMode(void);
00815     uint8_t STSpin220_GetTorque(motorTorqueMode_t torqueMode);
00816     bool STSpin220_GetTorqueBoostEnable(void);
00817     uint16_t STSpin220_GetTorqueBoostThreshold(void);
00818     void STSpin220_GoHome(void);  
00819     void STSpin220_GoMark(void);
00820     void STSpin220_GoTo(int32_t targetPosition);
00821     void STSpin220_GoToDir(motorDir_t direction, int32_t targetPosition);   
00822     void STSpin220_HardHiZ(void);
00823     void STSpin220_HardStop(void);
00824     void STSpin220_Move(motorDir_t direction, uint32_t stepCount);
00825     void STSpin220_PutDeviceInStandby(void);
00826     void STSpin220_Run(motorDir_t direction);
00827     bool STSpin220_SetAcceleration(uint16_t newAcc);
00828     bool STSpin220_SetDeceleration(uint16_t newDec);
00829     void STSpin220_SetDirection(motorDir_t direction);
00830     void STSpin220_SetHome(void);
00831     void STSpin220_SetMark(void);
00832     bool STSpin220_SetMaxSpeed(uint16_t volatile newSpeed);
00833     bool STSpin220_SetMinSpeed(uint16_t volatile newSpeed);    
00834     bool STSpin220_SetStepMode(motorStepMode_t stepMode);
00835     void STSpin220_SetStopMode(motorStopMode_t stopMode);
00836     bool STSpin220_SoftStop(void);
00837     void STSpin220_SetTorque(motorTorqueMode_t torqueMode, uint8_t torqueValue);
00838     void STSpin220_SetTorqueBoostEnable(bool enable);
00839     void STSpin220_SetTorqueBoostThreshold(uint16_t speedThreshold);
00840     uint32_t STSpin220_VrefPwmGetFreq(void);
00841     void STSpin220_VrefPwmSetFreq(uint32_t newFreq);
00842     void STSpin220_WaitWhileActive(void);
00843     
00844     /*** Functions intended to be used only internally ***/
00845     void STSpin220_ApplySpeed(uint16_t newSpeed);
00846     void STSpin220_ComputeSpeedProfile(uint32_t nbSteps);    
00847     void STSpin220_SetDeviceParamsToGivenValues(STSpin220_init_t* pInitDevicePrm);
00848     void STSpin220_SetDeviceParamsOtherValues(void);    
00849     void STSpin220_SetDeviceParamsToPredefinedValues(void);
00850     bool STSpin220_SetStepModeWithoutReset(motorStepMode_t stepMode);
00851     void STSpin220_StartMovement(void);
00852     void STSpin220_StepClockHandler(void);
00853     
00854     /*** Component's I/O Methods ***/
00855 
00856     /* ACTION 8 --------------------------------------------------------------*
00857      * Implement here other I/O methods beyond those already implemented      *
00858      * above, which are declared extern within the component's header file.   *
00859      *------------------------------------------------------------------------*/
00860     /**
00861      * @brief  Making the CPU wait.
00862      * @param  None.
00863      * @retval None.
00864      */
00865     void STSpin220_Board_Delay(uint32_t delay)
00866     {
00867         wait_ms(delay);
00868     }
00869     
00870     /**
00871      * @brief  Disable the power bridges (leave the output bridges HiZ).
00872      * @param  None.
00873      * @retval None.
00874      */
00875     void STSpin220_Board_Disable(void)
00876     {
00877         fault_and_enable.disable_irq();
00878         DigitalOut fault_and_enable(fault_and_enable_pinName);
00879         fault_and_enable.write(0);
00880     }
00881 
00882     /**
00883      * @brief  Disabling interrupts.
00884      * @param  None.
00885      * @retval None.
00886      */
00887     void STSpin220_Board_DisableIrq(void)
00888     {
00889         __disable_irq();
00890     }
00891     
00892     /**
00893      * @brief  Enable the power bridges (leave the output bridges HiZ).
00894      * @param  None.
00895      * @retval None.
00896      */
00897     void STSpin220_Board_Enable(void)
00898     {
00899         DigitalOut fault_and_enable_do(fault_and_enable_pinName);
00900         fault_and_enable_do.write(1);
00901         fault_and_enable.fall(fault_and_enable_irqFunctionPointer);
00902         fault_and_enable.mode(PullUp);
00903         wait_ms(1);
00904         fault_and_enable.enable_irq();
00905     }
00906     
00907     /**
00908      * @brief  Enabling interrupts.
00909      * @param  None.
00910      * @retval None.
00911      */
00912     void STSpin220_Board_EnableIrq(void)
00913     {
00914         __enable_irq();
00915     }
00916 
00917     /**
00918      * @brief  Set the duty cycle of the PwmOut used for the REF
00919      * reference voltage generation and actually start the pwm if the duty cycle
00920      * is not zero.
00921      * @param[in] dutyCycle 0 - 100%
00922      * @retval None
00923      */
00924     void STSpin220_Board_PwmRefSetDutyCycle(uint8_t dutyCycle)
00925     {
00926         pwm_ref.write(((float)(100-dutyCycle))/100);
00927     }
00928 
00929     /**
00930      * @brief  Set the frequency of the PwmOut used for the REF
00931      * reference voltage generation.
00932      * @param[in] newFreq in Hz.
00933      * @retval None.
00934      */
00935     void STSpin220_Board_PwmRefSetFreq(uint32_t newFreq)
00936     {
00937         pwm_ref.period_us(1000000.0f/newFreq);
00938     }
00939     
00940     /**
00941      * @brief  Start the PwmOut for the REF pin.
00942      * @param[in] frequency frequency of the PwmOut used to generate the REF
00943      * reference voltage for the bridges.
00944      * @param[in] torqueMode Torque mode as specified in enum motorTorqueMode_t    
00945      * @retval None.
00946      */ 
00947     void STSpin220_Board_PwmRefStart(uint32_t frequency, motorTorqueMode_t torqueMode)
00948     { 
00949         /* Setting the period of the PwmOut. */
00950         pwm_ref.period_us(1000000.0f/frequency);
00951         /* Setting the duty cycle and actually starting the of the PwmOut. */
00952         STSpin220_ApplyTorque(torqueMode);
00953         wait_ms(5*PWM_FILTER_TIME_CONSTANT);
00954     }
00955 
00956     /**
00957      * @brief  Exit the device from standby reset mode.
00958      * @param  None.
00959      * @retval None.
00960      */
00961     void STSpin220_Board_ReleaseReset(void)
00962     {
00963         stby_reset = 1;
00964     }
00965 
00966     /**
00967      * @brief  Put the device in standby reset mode.
00968      * @param  None.
00969      * @retval None.
00970      */
00971     void STSpin220_Board_Reset(void)
00972     {
00973         stby_reset = 0;
00974     }
00975 
00976     /**
00977      * @brief  Set the DIR\MODE4 pin.
00978      * @param  dir motor direction FORWARD or BACKWARD.
00979      * @retval None.
00980      */
00981     void STSpin220_Board_SetDirectionGpio(motorDir_t dir)
00982     {
00983         if (dir==BACKWARD)
00984         {
00985             direction_mode4 = 0;
00986         }
00987         else
00988         {
00989             direction_mode4 = 1;
00990         }
00991     }
00992  
00993     /**
00994      * @brief Select Full Step mode
00995      * @param  None.
00996      * @retval None
00997      */
00998     void STSpin220_Board_SetFullStep(void)
00999     {
01000         mode1 = 0;
01001         mode2 = 0;
01002     }
01003     
01004     /**
01005      * @brief Select the STSpin220 mode1, mode2, mode3 and mode4 pins levels.
01006      * @param[in] modePin1Level level of the mode1 gpio (0 low, 1+ high)
01007      * @param[in] modePin2Level level of the mode2 gpio (0 low, 1+ high)
01008      * @param[in] modePin3Level level of the mode3 gpio (0 low, 1+ high)
01009      * @param[in] modePin4Level level of the mode4 gpio (0 low, 1+ high)
01010      * @retval 1
01011      */
01012     uint8_t STSpin220_Board_SetModePins(uint8_t modePin1Level,\
01013         uint8_t modePin2Level,\
01014         uint8_t modePin3Level,\
01015         uint8_t modePin4Level)
01016     {
01017         mode1 = modePin1Level;
01018         mode2 = modePin2Level;
01019         stck_mode3 = modePin3Level;
01020         direction_mode4 = modePin4Level;
01021         return 1;
01022     }
01023 
01024     /**
01025      * @brief  Reset the STCK\MODE3 pin.
01026      * @param  None.
01027      * @retval None.
01028      */
01029     void STSpin220_Board_StckMode3_Reset(void)
01030     {
01031         stck_mode3 = 0;
01032     }
01033 
01034     /**
01035      * @brief  Set the STCK\MODE3 pin.
01036      * @param  None.
01037      * @retval None.
01038      */
01039     void STSpin220_Board_StckMode3_Set(void)
01040     {
01041         stck_mode3 = 1;
01042     }
01043 
01044     /**
01045      * @brief Initialises the step clock pin level
01046      * @param  None.
01047      * @retval None
01048      */
01049     void STSpin220_Board_TimStckInit(bool check)
01050     {
01051         if (monitor.is_connected())
01052         {
01053             monitor = 0;
01054         }
01055         if (check==false)
01056         {
01057             stck_mode3 = 0;
01058         }
01059         else
01060         {
01061             if (stck_mode3==1)
01062             {
01063                 STSpin220_ErrorHandler(STSPIN220_ERROR_STEP_CLOCK);
01064             }
01065         }
01066     }
01067 
01068     /**
01069      * @brief  Setting the Stck Timeout delay
01070      * and attaching a callback function to it.
01071      * @param  frequency The frequency corresponding to the delay.
01072      * @retval None.
01073      */
01074     void STSpin220_Board_TimStckSetFreq(uint16_t newFreq)
01075     {
01076         /* Computing the delay of the Timeout. */
01077         float delay_us = (1000000.0f / 2 )/ newFreq;
01078         
01079         /* Attaching a function which updates */
01080         /* the state machine after the elapsed period_us time. */
01081         tim_stck.attach_us(this, &STSpin220::STSpin220_StepClockHandler, delay_us);
01082     }
01083 
01084     void STSpin220_Board_Monitor_Set(void)
01085     {
01086         if (monitor.is_connected())
01087         {
01088             monitor = 1;
01089         }
01090     }
01091     
01092     void STSpin220_Board_Monitor_Reset(void)
01093     {
01094         if (monitor.is_connected())
01095         {
01096             monitor = 0;
01097         }
01098     }
01099 
01100     /**
01101      * @brief  Stopping the Timeout.
01102      * @param  None.
01103      * @retval None.
01104      */
01105     uint8_t STSpin220_Board_TimStckStop(volatile uint8_t *pToggleOdd)
01106     {
01107         __disable_irq();
01108         if (*pToggleOdd == 1)
01109         {
01110             __enable_irq();
01111             return 1;
01112         }
01113         if (stck_mode3 != 0)
01114         {
01115             __enable_irq();
01116             return 0;
01117         }
01118         tim_stck.detach();
01119         __enable_irq();
01120         return 1;
01121     }
01122 
01123     /**
01124      * @brief Unselect Full Step mode
01125      * @param  None.
01126      * @retval None
01127      */
01128     void STSpin220_Board_UnsetFullStep(void)
01129     {
01130         mode1 = 1;
01131     }
01132 
01133 protected:
01134 
01135     /*** Component's Instance Variables ***/
01136 
01137     /* ACTION 9 --------------------------------------------------------------*
01138      * Declare here interrupt related variables, if needed.                   *
01139      * Note that interrupt handling is platform dependent, see                *
01140      * "Interrupt Related Methods" above.                                     *
01141      *                                                                        *
01142      * Example:                                                               *
01143      *   + mbed:                                                              *
01144      *     InterruptIn feature_irq;                                           *
01145      *------------------------------------------------------------------------*/
01146     /* Fault Interrupt and chip enable. */
01147     InterruptIn fault_and_enable;
01148     
01149     /* Interrupt to toggle the MODE3\STCK pin */
01150     Timeout tim_stck;
01151 
01152     /* ACTION 10 -------------------------------------------------------------*
01153      * Declare here other pin related variables, if needed.                   *
01154      *                                                                        *
01155      * Example:                                                               *
01156      *   + mbed:                                                              *
01157      *     DigitalOut standby_reset;                                          *
01158      *------------------------------------------------------------------------*/
01159     /* STBY\RESET pin. */
01160     DigitalOut stby_reset;
01161     /* MODE4\DIR pin. */
01162     DigitalOut direction_mode4;
01163     /* MODE1 pin */
01164     DigitalOut mode1;
01165     /* MODE2 pin */
01166     DigitalOut mode2;
01167     /* MODE3\STCK pin. */
01168     DigitalOut stck_mode3;
01169 
01170     /* Pulse Width Modulation pin for REF pin */
01171     PwmOut pwm_ref;
01172  
01173     /* Monitoring of step clock handler duration */
01174     DigitalOut monitor;
01175 
01176     /* fault and enable pin */
01177     PinName fault_and_enable_pinName;
01178     
01179     /* ACTION 11 -------------------------------------------------------------*
01180      * Declare here communication related variables, if needed.               *
01181      *                                                                        *
01182      * Example:                                                               *
01183      *   + mbed:                                                              *
01184      *     DigitalOut ssel;                                                   *
01185      *     DevSPI &dev_spi;                                                   *
01186      *------------------------------------------------------------------------*/
01187     /* Configuration. */
01188 
01189     /* IO Device. */
01190 
01191     /* ACTION 12 -------------------------------------------------------------*
01192      * Declare here identity related variables, if needed.                    *
01193      * Note that there should be only a unique identifier for each component, *
01194      * which should be the "who_am_i" parameter.                              *
01195      *------------------------------------------------------------------------*/
01196     /* Identity */
01197     uint8_t who_am_i;
01198 
01199     /* ACTION 13 -------------------------------------------------------------*
01200      * Declare here the component's static and non-static data, one variable  *
01201      * per line.                                                              *
01202      *                                                                        *
01203      * Example:                                                               *
01204      *   float measure;                                                       *
01205      *   int instance_id;                                                     *
01206      *   static int number_of_instances;                                      *
01207      *------------------------------------------------------------------------*/
01208     /* Data. */
01209     void (*fault_and_enable_irqFunctionPointer)(void);
01210     void (*errorHandlerCallback)(uint16_t error);
01211     deviceParams_t devicePrm;
01212     uint8_t deviceInstance;
01213     volatile uint8_t toggleOdd;
01214 
01215     /* Static data. */
01216     static uint8_t numberOfDevices;
01217 
01218 public:
01219 
01220     /* Static data. */
01221     
01222 };
01223 
01224 #endif // __STSPIN220_CLASS_H
01225 
01226 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/