Library to handle the X-NUCLEO-IHM01A1 Motor Control Expansion Board based on the L6474 component.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   Stepper_Matlab_Control SunTracker_BLE Stepper_Matlab_Control MemsMotorControl ... more

Fork of X_NUCLEO_IHM01A1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers L6474.h Source File

L6474.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    L6474.h
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    October 14th, 2015
00007  * @brief   This file contains the class of an L6474 Motor Control component.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 
00039 /* Generated with STM32CubeTOO -----------------------------------------------*/
00040 
00041 
00042 /* Revision ------------------------------------------------------------------*/
00043 /*
00044     Repository:       http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev
00045     Branch/Trunk/Tag: trunk
00046     Based on:         X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.h
00047     Revision:         0
00048 */
00049 
00050 
00051 /* Define to prevent recursive inclusion -------------------------------------*/
00052 
00053 #ifndef __L6474_CLASS_H
00054 #define __L6474_CLASS_H
00055 
00056 
00057 /* Includes ------------------------------------------------------------------*/
00058 
00059 /* ACTION 1 ------------------------------------------------------------------*
00060  * Include here platform specific header files.                               *
00061  *----------------------------------------------------------------------------*/        
00062 #include "mbed.h"
00063 #include "DevSPI.h"
00064 /* ACTION 2 ------------------------------------------------------------------*
00065  * Include here component specific header files.                              *
00066  *----------------------------------------------------------------------------*/        
00067 #include "L6474_def.h"
00068 /* ACTION 3 ------------------------------------------------------------------*
00069  * Include here interface specific header files.                              *
00070  *                                                                            *
00071  * Example:                                                                   *
00072  *   #include "HumiditySensor.h"                                              *
00073  *   #include "TemperatureSensor.h"                                           *
00074  *----------------------------------------------------------------------------*/
00075 #include "StepperMotor.h"
00076 
00077 
00078 /* Classes -------------------------------------------------------------------*/
00079 
00080 /**
00081  * @brief Class representing an L6474 component.
00082  */
00083 class L6474 : public StepperMotor 
00084 {
00085 public:
00086 
00087     /*** Constructor and Destructor Methods ***/
00088 
00089     /**
00090      * @brief Constructor.
00091      * @param flag_irq      pin name of the FLAG pin of the component.
00092      * @param standby_reset pin name of the STBY\RST pin of the component.
00093      * @param direction     pin name of the DIR pin of the component.
00094      * @param pwm           pin name of the PWM pin of the component.
00095      * @param ssel          pin name of the SSEL pin of the SPI device to be used for communication.
00096      * @param spi           SPI device to be used for communication.
00097      */
00098     L6474(PinName flag_irq, PinName standby_reset, PinName direction, PinName pwm, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), standby_reset(standby_reset), direction(direction), pwm(pwm), ssel(ssel), dev_spi(spi)
00099     {
00100         /* Checking stackability. */
00101         if (!(number_of_devices < MAX_NUMBER_OF_DEVICES)) {
00102             error("Instantiation of the L6474 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES);
00103         }
00104 
00105         /* ACTION 4 ----------------------------------------------------------*
00106          * Initialize here the component's member variables, one variable per *
00107          * line.                                                              *
00108          *                                                                    *
00109          * Example:                                                           *
00110          *   measure = 0;                                                     *
00111          *   instance_id = number_of_instances++;                             *
00112          *--------------------------------------------------------------------*/
00113         error_handler_callback = 0;
00114         device_instance = number_of_devices++;
00115         memset(spi_tx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t));
00116         memset(spi_rx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t));
00117     }
00118     
00119     /**
00120      * @brief Destructor.
00121      */
00122     virtual ~L6474(void) {}
00123 
00124 
00125     /*** Public Component Related Methods ***/
00126 
00127     /* ACTION 5 --------------------------------------------------------------*
00128      * Implement here the component's public methods, as wrappers of the C    *
00129      * component's functions.                                                 *
00130      * They should be:                                                        *
00131      *   + Methods with the same name of the C component's virtual table's    *
00132      *     functions (1);                                                     *
00133      *   + Methods with the same name of the C component's extended virtual   *
00134      *     table's functions, if any (2).                                     *
00135      *                                                                        *
00136      * Example:                                                               *
00137      *   virtual int get_value(float *p_data) //(1)                           *
00138      *   {                                                                    *
00139      *     return COMPONENT_get_value(float *pf_data);                        *
00140      *   }                                                                    *
00141      *                                                                        *
00142      *   virtual int enable_feature(void) //(2)                               *
00143      *   {                                                                    *
00144      *     return COMPONENT_enable_feature();                                 *
00145      *   }                                                                    *
00146      *------------------------------------------------------------------------*/
00147     /**
00148      * @brief  Initializing the component in 1/16 Microstepping mode.
00149      * @param  init Pointer to device specific initalization structure.
00150      * @retval "0" in case of success, an error code otherwise.
00151      */
00152     virtual int init(void *init = NULL)
00153     {
00154         return (int) L6474_Init((void *) init);
00155     }
00156 
00157     /**
00158      * @brief  Getting the ID of the component.
00159      * @param  id Pointer to an allocated variable to store the ID into.
00160      * @retval "0" in case of success, an error code otherwise.
00161      */
00162     virtual int read_id(uint8_t *id = NULL)
00163     {
00164         return (int) L6474_ReadID((uint8_t *) id);
00165     }
00166 
00167     /**
00168      * @brief  Getting the value of the Status Register.
00169      * @param  None.
00170      * @retval None.
00171      * @note   The Status Register's flags are cleared, contrary to the
00172      *         read_status_register() method.
00173      */
00174     virtual unsigned int get_status(void)
00175     {
00176         return (unsigned int) L6474_CmdGetStatus();
00177     }
00178 
00179     /**
00180      * @brief  Getting a parameter.
00181      * @param  parameter A parameter's register address.
00182      * @retval The parameter's value.
00183      * @note   The Status Register's flags are cleared, contrary to the
00184      *         read_status_register() method.
00185      *         The parameter can be one of the following:
00186      *           + L6474_ABS_POS
00187      *           + L6474_EL_POS
00188      *           + L6474_MARK
00189      *           + L6474_RESERVED_REG01
00190      *           + L6474_RESERVED_REG02
00191      *           + L6474_RESERVED_REG03
00192      *           + L6474_RESERVED_REG04
00193      *           + L6474_RESERVED_REG05
00194      *           + L6474_RESERVED_REG06
00195      *           + L6474_TVAL           : value in mA
00196      *           + L6474_RESERVED_REG07
00197      *           + L6474_RESERVED_REG08
00198      *           + L6474_RESERVED_REG09
00199      *           + L6474_RESERVED_REG10
00200      *           + L6474_T_FAST
00201      *           + L6474_TON_MIN        : value in us
00202      *           + L6474_TOFF_MIN       : value in us
00203      *           + L6474_RESERVED_REG11
00204      *           + L6474_ADC_OUT
00205      *           + L6474_OCD_TH
00206      *           + L6474_RESERVED_REG12
00207      *           + L6474_STEP_MODE
00208      *           + L6474_ALARM_EN
00209      *           + L6474_CONFIG
00210      *           + L6474_STATUS
00211      *           + L6474_RESERVED_REG13
00212      *           + L6474_RESERVED_REG14
00213      *           + L6474_INEXISTENT_REG
00214      */
00215     virtual float get_parameter(unsigned int parameter)
00216     {
00217         unsigned int register_value = (unsigned int) L6474_CmdGetParam((L6474_Registers_t) parameter);
00218         float value;
00219 
00220         switch ((L6474_Registers_t) parameter) {
00221             case L6474_TVAL:
00222                 value = L6474_Par_to_Tval_Current((float) register_value);
00223                 break;
00224             case L6474_TON_MIN:
00225             case L6474_TOFF_MIN:
00226                 value = L6474_Par_to_Tmin_Time((float) register_value);
00227                 break;
00228             default:
00229                 value = (float) register_value;
00230                 break;
00231         }
00232         
00233         return value;
00234     }
00235 
00236     /**
00237      * @brief  Getting the position.
00238      * @param  None.
00239      * @retval The position.
00240      */
00241     virtual signed int get_position(void)
00242     {
00243         return (signed int) L6474_GetPosition();
00244     }
00245 
00246     /**
00247      * @brief  Getting the marked position.
00248      * @param  None.
00249      * @retval The marked position.
00250      */
00251     virtual signed int get_mark(void)
00252     {
00253         return (signed int) L6474_GetMark();
00254     }
00255 
00256     /**
00257      * @brief  Getting the current speed in pps.
00258      * @param  None.
00259      * @retval The current speed in pps.
00260      */
00261     virtual unsigned int get_speed(void)
00262     {
00263         return (unsigned int) L6474_GetCurrentSpeed();
00264     }
00265 
00266     /**
00267      * @brief  Getting the maximum speed in pps.
00268      * @param  None.
00269      * @retval The maximum speed in pps.
00270      */
00271     virtual unsigned int get_max_speed(void)
00272     {
00273         return (unsigned int) L6474_GetMaxSpeed();
00274     }
00275 
00276     /**
00277      * @brief  Getting the minimum speed in pps.
00278      * @param  None.
00279      * @retval The minimum speed in pps.
00280      */
00281     virtual unsigned int get_min_speed(void)
00282     {
00283         return (unsigned int) L6474_GetMinSpeed();
00284     }
00285 
00286     /**
00287      * @brief  Getting the acceleration in pps^2.
00288      * @param  None.
00289      * @retval The acceleration in pps^2.
00290      */
00291     virtual unsigned int get_acceleration(void)
00292     {
00293         return (unsigned int) L6474_GetAcceleration();
00294     }
00295 
00296     /**
00297      * @brief  Getting the deceleration in pps^2.
00298      * @param  None.
00299      * @retval The deceleration in pps^2.
00300      */
00301     virtual unsigned int get_deceleration(void)
00302     {
00303         return (unsigned int) L6474_GetDeceleration();
00304     }
00305 
00306     /**
00307      * @brief  Getting the direction of rotation.
00308      * @param  None.
00309      * @retval The direction of rotation.
00310      */
00311     virtual direction_t get_direction(void)
00312     {
00313         return (direction_t) (L6474_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD);
00314     }
00315 
00316     /**
00317      * @brief   Setting a parameter.
00318      * @param   parameter A parameter's register address.
00319      * @param   value The parameter's value.
00320      * @retval  None.
00321      * @note    The parameter can be one of the following:
00322      *           + L6474_ABS_POS
00323      *           + L6474_EL_POS
00324      *           + L6474_MARK
00325      *           + L6474_RESERVED_REG01
00326      *           + L6474_RESERVED_REG02
00327      *           + L6474_RESERVED_REG03
00328      *           + L6474_RESERVED_REG04
00329      *           + L6474_RESERVED_REG05
00330      *           + L6474_RESERVED_REG06
00331      *           + L6474_TVAL           : value in mA
00332      *           + L6474_RESERVED_REG07
00333      *           + L6474_RESERVED_REG08
00334      *           + L6474_RESERVED_REG09
00335      *           + L6474_RESERVED_REG10
00336      *           + L6474_T_FAST
00337      *           + L6474_TON_MIN        : value in us
00338      *           + L6474_TOFF_MIN       : value in us
00339      *           + L6474_RESERVED_REG11
00340      *           + L6474_ADC_OUT
00341      *           + L6474_OCD_TH
00342      *           + L6474_RESERVED_REG12
00343      *           + L6474_STEP_MODE
00344      *           + L6474_ALARM_EN
00345      *           + L6474_CONFIG
00346      *           + L6474_STATUS
00347      *           + L6474_RESERVED_REG13
00348      *           + L6474_RESERVED_REG14
00349      *           + L6474_INEXISTENT_REG
00350      * @warning Some registers can only be written in particular conditions (see L6474's datasheet).
00351      *          Any attempt to write one of those registers when the conditions are not satisfied
00352      *          causes the command to be ignored and the NOTPERF_CMD flag to rise at the end of the
00353      *          last argument byte. Any attempt to set an inexistent register (wrong address value)
00354      *          causes the command to be ignored and the WRONG_CMD flag to rise.
00355      *          For example, setting some parameters requires first to disable the power bridge;
00356      *          this can be done through the soft_hiz() method.
00357      *          They are the following:
00358      *           + L6474_EL_POS
00359      *           + L6474_T_FAST
00360      *           + L6474_TON_MIN        : value in us
00361      *           + L6474_TOFF_MIN       : value in us
00362      *           + L6474_ADC_OUT
00363      *           + L6474_STEP_MODE
00364      *           + L6474_CONFIG
00365      *           + L6474_STATUS
00366      */
00367     virtual void set_parameter(unsigned int parameter, float value)
00368     {
00369         float register_value;
00370 
00371         switch ((L6474_Registers_t) parameter) {
00372             case L6474_TVAL:
00373                 register_value = L6474_Tval_Current_to_Par(value);
00374                 break;
00375             case L6474_TON_MIN:
00376             case L6474_TOFF_MIN:
00377                 register_value = L6474_Tmin_Time_to_Par(value);
00378                 break;
00379             default:
00380                 register_value = value;
00381                 break;
00382         }
00383 
00384         L6474_CmdSetParam((L6474_Registers_t) parameter, (unsigned int) register_value);
00385     }
00386 
00387     /**
00388      * @brief  Setting the current position to be the home position.
00389      * @param  None.
00390      * @retval None.
00391      */
00392     virtual void set_home(void)
00393     {
00394         L6474_SetHome();
00395     }
00396 
00397     /**
00398      * @brief  Setting the current position to be the marked position.
00399      * @param  None.
00400      * @retval None.
00401      */
00402     virtual void set_mark(void)
00403     {
00404         L6474_SetMark();
00405     }
00406 
00407     /**
00408      * @brief  Setting the maximum speed in pps.
00409      * @param  speed The maximum speed in pps.
00410      * @retval "true" in case of success, "false" otherwise.
00411      */
00412     virtual bool set_max_speed(unsigned int speed)
00413     {
00414         L6474_SetMaxSpeed((unsigned int) speed);
00415         return true;
00416     }
00417 
00418     /**
00419      * @brief  Setting the minimum speed in pps.
00420      * @param  speed The minimum speed in pps.
00421      * @retval "true" in case of success, "false" otherwise.
00422      */
00423     virtual bool set_min_speed(unsigned int speed)
00424     {
00425         L6474_SetMinSpeed((unsigned int) speed);
00426         return true;
00427     }
00428 
00429     /**
00430      * @brief  Setting the acceleration in pps^2.
00431      * @param  acceleration The acceleration in pps^2.
00432      * @retval "true" in case of success, "false" otherwise.
00433      */
00434     virtual bool set_acceleration(unsigned int acceleration)
00435     {
00436         L6474_SetAcceleration((unsigned int) acceleration);
00437         return true;
00438     }
00439 
00440     /**
00441      * @brief  Setting the deceleration in pps^2.
00442      * @param  deceleration The deceleration in pps^2.
00443      * @retval "true" in case of success, "false" otherwise.
00444      */
00445     virtual bool set_deceleration(unsigned int deceleration)
00446     {
00447         L6474_SetDeceleration((unsigned int) deceleration);
00448         return true;
00449     }
00450 
00451     /**
00452      * @brief  Going to a specified position.
00453      * @param  position The desired position.
00454      * @retval None.
00455      */
00456     virtual void go_to(signed int position)
00457     {
00458         L6474_GoTo((signed int) position);
00459     }
00460 
00461     /**
00462      * @brief  Going to the home position.
00463      * @param  None.
00464      * @retval None.
00465      */
00466     virtual void go_home(void)
00467     {
00468         L6474_GoHome();
00469     }
00470 
00471     /**
00472      * @brief  Going to the marked position.
00473      * @param  None.
00474      * @retval None.
00475      */
00476     virtual void go_mark(void)
00477     {
00478         L6474_GoMark();
00479     }
00480 
00481     /**
00482      * @brief  Running the motor towards a specified direction.
00483      * @param  direction The direction of rotation.
00484      * @retval None.
00485      */
00486     virtual void run(direction_t direction)
00487     {
00488         L6474_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD));
00489     }
00490 
00491     /**
00492      * @brief  Moving the motor towards a specified direction for a certain number of steps.
00493      * @param  direction The direction of rotation.
00494      * @param  steps The desired number of steps.
00495      * @retval None.
00496      */
00497     virtual void move(direction_t direction, unsigned int steps)
00498     {
00499         L6474_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (unsigned int) steps);
00500     }
00501 
00502     /**
00503      * @brief  Stopping the motor through an immediate deceleration up to zero speed.
00504      * @param  None.
00505      * @retval None.
00506      */
00507     virtual void soft_stop(void)
00508     {
00509         L6474_SoftStop();
00510     }
00511 
00512     /**
00513      * @brief  Stopping the motor through an immediate infinite deceleration.
00514      * @param  None.
00515      * @retval None.
00516      */
00517     virtual void hard_stop(void)
00518     {
00519         L6474_HardStop();
00520     }
00521 
00522     /**
00523      * @brief  Disabling the power bridge after performing a deceleration to zero.
00524      * @param  None.
00525      * @retval None.
00526      */
00527     virtual void soft_hiz(void)
00528     {
00529         L6474_SoftStop();
00530         L6474_CmdDisable();
00531     }
00532 
00533     /**
00534      * @brief  Disabling the power bridge immediately.
00535      * @param  None.
00536      * @retval None.
00537      */
00538     virtual void hard_hiz(void)
00539     {
00540         L6474_HardStop();
00541         L6474_CmdDisable();
00542     }
00543 
00544     /**
00545      * @brief  Waiting while the motor is active.
00546      * @param  None.
00547      * @retval None.
00548      */
00549     virtual void wait_while_active(void)
00550     {
00551         L6474_WaitWhileActive();
00552     }
00553 
00554    /**
00555      * @brief  Getting the device state.
00556      * @param  None.
00557      * @retval The device state.
00558      * @note   The device state can be one of the following:
00559      *           + ACCELERATING
00560      *           + DECELERATING
00561      *           + STEADY
00562      *           + INACTIVE
00563     */
00564     virtual motorState_t get_device_state(void)
00565     {
00566         return (motorState_t) L6474_GetDeviceState();
00567     }
00568 
00569     /**
00570      * @brief  Reading the Status Register.
00571      * @param  None.
00572      * @retval None.
00573      * @note   The Status Register's flags are not cleared, contrary to the
00574      *         GetStatus() method.
00575      */
00576     virtual uint16_t read_status_register(void)
00577     {
00578         return (uint16_t) L6474_ReadStatusRegister();
00579     }
00580 
00581     /**
00582      * @brief   Setting the Step Mode.
00583      * @param   step_mode The Step Mode.
00584      * @retval "true" in case of success, "false" otherwise.
00585      * @warning Setting the step mode implies first disabling the power bridge through
00586      *          the soft_hiz() method.
00587      * @warning Every time step mode is changed, the values of the home
00588      *          and mark positions lose meaning and are reset.
00589      */
00590     virtual bool set_step_mode(step_mode_t step_mode)
00591     {
00592         if ((motorStepMode_t) step_mode > STEP_MODE_1_16) {
00593             return false;
00594         }
00595 
00596         soft_hiz();
00597         L6474_SelectStepMode((motorStepMode_t) step_mode);
00598         return true;
00599     }
00600 
00601     /**
00602      * @brief  Attaching an error handler.
00603      * @param  fptr An error handler.
00604      * @retval None.
00605      */
00606     virtual void attach_error_handler(void (*fptr)(uint16_t error))
00607     {
00608         L6474_AttachErrorHandler((void (*)(uint16_t error)) fptr);
00609     }
00610 
00611     /**
00612      * @brief  Enabling the device.
00613      * @param  None.
00614      * @retval None.
00615      */
00616     virtual void enable(void)
00617     {
00618         L6474_CmdEnable();
00619     }
00620     
00621     /**
00622      * @brief  Disabling the device.
00623      * @param  None.
00624      * @retval None.
00625      */
00626     virtual void disable(void)
00627     {
00628         L6474_CmdDisable();
00629     }
00630 
00631     /**
00632      * @brief  Getting the version of the firmware.
00633      * @param  None.
00634      * @retval The version of the firmware.
00635      */
00636     virtual uint8_t get_fw_version(void)
00637     {
00638         return (uint8_t) L6474_GetFwVersion();
00639     }
00640 
00641 
00642     /*** Public Interrupt Related Methods ***/
00643 
00644     /* ACTION 6 --------------------------------------------------------------*
00645      * Implement here interrupt related methods, if any.                      *
00646      * Note that interrupt handling is platform dependent, e.g.:              *
00647      *   + mbed:                                                              *
00648      *     InterruptIn feature_irq(pin); //Interrupt object.                  *
00649      *     feature_irq.rise(callback);   //Attach a callback.                 *
00650      *     feature_irq.mode(PullNone);   //Set interrupt mode.                *
00651      *     feature_irq.enable_irq();     //Enable interrupt.                  *
00652      *     feature_irq.disable_irq();    //Disable interrupt.                 *
00653      *   + Arduino:                                                           *
00654      *     attachInterrupt(pin, callback, RISING); //Attach a callback.       *
00655      *     detachInterrupt(pin);                   //Detach a callback.       *
00656      *                                                                        *
00657      * Example (mbed):                                                        *
00658      *   void attach_feature_irq(void (*fptr) (void))                         *
00659      *   {                                                                    *
00660      *     feature_irq.rise(fptr);                                            *
00661      *   }                                                                    *
00662      *                                                                        *
00663      *   void enable_feature_irq(void)                                        *
00664      *   {                                                                    *
00665      *     feature_irq.enable_irq();                                          *
00666      *   }                                                                    *
00667      *                                                                        *
00668      *   void disable_feature_irq(void)                                       *
00669      *   {                                                                    *
00670      *     feature_irq.disable_irq();                                         *
00671      *   }                                                                    *
00672      *------------------------------------------------------------------------*/
00673     /**
00674      * @brief  Attaching an interrupt handler to the FLAG interrupt.
00675      * @param  fptr An interrupt handler.
00676      * @retval None.
00677      */
00678     void attach_flag_irq(void (*fptr)(void))
00679     {
00680         flag_irq.fall(fptr);
00681     }
00682     
00683     /**
00684      * @brief  Enabling the FLAG interrupt handling.
00685      * @param  None.
00686      * @retval None.
00687      */
00688     void enable_flag_irq(void)
00689     {
00690         flag_irq.enable_irq();
00691     }
00692     
00693     /**
00694      * @brief  Disabling the FLAG interrupt handling.
00695      * @param  None.
00696      * @retval None.
00697      */
00698     void disable_flag_irq(void)
00699     {
00700         flag_irq.disable_irq();
00701     }
00702 
00703 
00704 protected:
00705 
00706     /*** Protected Component Related Methods ***/
00707 
00708     /* ACTION 7 --------------------------------------------------------------*
00709      * Declare here the component's specific methods.                         *
00710      * They should be:                                                        *
00711      *   + Methods with the same name of the C component's virtual table's    *
00712      *     functions (1);                                                     *
00713      *   + Methods with the same name of the C component's extended virtual   *
00714      *     table's functions, if any (2);                                     *
00715      *   + Helper methods, if any, like functions declared in the component's *
00716      *     source files but not pointed by the component's virtual table (3). *
00717      *                                                                        *
00718      * Example:                                                               *
00719      *   status_t COMPONENT_get_value(float *f);   //(1)                      *
00720      *   status_t COMPONENT_enable_feature(void);  //(2)                      *
00721      *   status_t COMPONENT_compute_average(void); //(3)                      *
00722      *------------------------------------------------------------------------*/
00723     void L6474_AttachErrorHandler(void (*callback)(uint16_t error));
00724     status_t L6474_Init(void *init);
00725     status_t L6474_ReadID(uint8_t *id);
00726     uint16_t L6474_GetAcceleration(void);
00727     uint16_t L6474_GetCurrentSpeed(void);
00728     uint16_t L6474_GetDeceleration(void);
00729     motorState_t L6474_GetDeviceState(void);
00730     uint8_t L6474_GetFwVersion(void);
00731     int32_t L6474_GetMark(void);
00732     uint16_t L6474_GetMaxSpeed(void);
00733     uint16_t L6474_GetMinSpeed(void);
00734     int32_t L6474_GetPosition(void);
00735     void L6474_GoHome(void);
00736     void L6474_GoMark(void);
00737     void L6474_GoTo(int32_t targetPosition);
00738     void L6474_HardStop(void);
00739     void L6474_Move(motorDir_t direction, uint32_t stepCount);
00740     void L6474_Run(motorDir_t direction);
00741     bool L6474_SetAcceleration(uint16_t newAcc);
00742     bool L6474_SetDeceleration(uint16_t newDec);
00743     void L6474_SetHome(void);
00744     void L6474_SetMark(void);
00745     bool L6474_SetMaxSpeed(uint16_t newMaxSpeed);
00746     bool L6474_SetMinSpeed(uint16_t newMinSpeed);
00747     bool L6474_SoftStop(void);
00748     void L6474_WaitWhileActive(void);
00749     void L6474_CmdDisable(void);
00750     void L6474_CmdEnable(void);
00751     uint32_t L6474_CmdGetParam(L6474_Registers_t parameter);
00752     uint16_t L6474_CmdGetStatus(void);
00753     void L6474_CmdNop(void);
00754     void L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value);
00755     uint16_t L6474_ReadStatusRegister(void);
00756     void L6474_SelectStepMode(motorStepMode_t stepMod);
00757     motorDir_t L6474_GetDirection(void);
00758     void L6474_SetDirection(motorDir_t direction);
00759     void L6474_ApplySpeed(uint16_t newSpeed);
00760     void L6474_ComputeSpeedProfile(uint32_t nbSteps);
00761     int32_t L6474_ConvertPosition(uint32_t abs_position_reg);
00762     void L6474_ErrorHandler(uint16_t error);
00763     void L6474_SendCommand(uint8_t param);
00764     void L6474_SetRegisterToPredefinedValues(void);
00765     void L6474_SetRegisterToInitializationValues(L6474_init_t *init);
00766     void L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte);
00767     void L6474_SetDeviceParamsToPredefinedValues(void);
00768     void L6474_StartMovement(void);
00769     void L6474_StepClockHandler(void);
00770     float L6474_Tval_Current_to_Par(float current_mA);
00771     float L6474_Par_to_Tval_Current(float Tval);
00772     float L6474_Tmin_Time_to_Par(float ton_min_us);
00773     float L6474_Par_to_Tmin_Time(float Tmin);
00774 
00775 
00776     /*** Component's I/O Methods ***/
00777 
00778     /**
00779      * @brief      Utility function to read data from L6474.
00780      * @param[out] pBuffer pointer to the buffer to read data into.
00781      * @param[in]  NumBytesToRead number of bytes to read.
00782      * @retval     COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00783      */
00784     status_t Read(uint8_t* pBuffer, uint16_t NumBytesToRead)
00785     {
00786         if (dev_spi.spi_read(pBuffer, ssel, NumBytesToRead) != 0) {
00787             return COMPONENT_ERROR;
00788         }
00789         return COMPONENT_OK;
00790     }
00791     
00792     /**
00793      * @brief      Utility function to write data to L6474.
00794      * @param[in]  pBuffer pointer to the buffer of data to send.
00795      * @param[in]  NumBytesToWrite number of bytes to write.
00796      * @retval     COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00797      */
00798     status_t Write(uint8_t* pBuffer, uint16_t NumBytesToWrite)
00799     {
00800         if (dev_spi.spi_write(pBuffer, ssel, NumBytesToWrite) != 0) {
00801             return COMPONENT_ERROR;
00802         }
00803         return COMPONENT_OK;
00804     }
00805 
00806     /**
00807      * @brief      Utility function to read and write data from/to L6474 at the same time.
00808      * @param[out] pBufferToRead pointer to the buffer to read data into.
00809      * @param[in]  pBufferToWrite pointer to the buffer of data to send.
00810      * @param[in]  NumBytes number of bytes to read and write.
00811      * @retval     COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00812      */
00813     status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumBytes)
00814     {
00815         if (dev_spi.spi_read_write(pBufferToRead, pBufferToWrite, ssel, NumBytes) != 0) {
00816             return COMPONENT_ERROR;
00817         }
00818         return COMPONENT_OK;
00819     }
00820 
00821     /* ACTION 8 --------------------------------------------------------------*
00822      * Implement here other I/O methods beyond those already implemented      *
00823      * above, which are declared extern within the component's header file.   *
00824      *------------------------------------------------------------------------*/
00825     /**
00826      * @brief  Making the CPU wait.
00827      * @param  None.
00828      * @retval None.
00829      */
00830     void L6474_Delay(uint32_t delay)
00831     {
00832         wait_ms(delay);
00833     }
00834 
00835     /**
00836      * @brief  Enabling interrupts.
00837      * @param  None.
00838      * @retval None.
00839      */
00840     void L6474_EnableIrq(void)
00841     {
00842         __enable_irq();
00843     }
00844 
00845     /**
00846      * @brief  Disabling interrupts.
00847      * @param  None.
00848      * @retval None.
00849      */
00850     void L6474_DisableIrq(void)
00851     {
00852         __disable_irq();
00853     }
00854 
00855     /**
00856      * @brief  Initialising the PWM.
00857      * @param  None.
00858      * @retval None.
00859      */
00860     void L6474_PwmInit(void) {}
00861 
00862     /**
00863      * @brief  Setting the frequency of PWM.
00864      *         The frequency controls directly the speed of the device.
00865      * @param  frequency the frequency of PWM.
00866      * @retval None.
00867      */
00868     void L6474_PwmSetFreq(uint16_t frequency)
00869     {
00870         /* Computing the period of PWM. */
00871         double period = 1.0f / frequency;
00872         
00873         /* Setting the period and the duty-cycle of PWM. */
00874         pwm.period(period);
00875         pwm.write(0.5f);
00876 
00877         /* Setting a callback with the same period of PWM's, to update the state machine. */
00878         ticker.attach(Callback<void()>(this, &L6474::L6474_StepClockHandler), period);
00879     }
00880 
00881     /**
00882      * @brief  Stopping the PWM.
00883      * @param  None.
00884      * @retval None.
00885      */
00886     void L6474_PwmStop(void)
00887     {
00888         pwm.write(0.0f);
00889         ticker.detach();
00890     }
00891 
00892     /**
00893      * @brief  Putting the device in standby mode.
00894      * @param  None.
00895      * @retval None.
00896      */
00897     void L6474_ReleaseReset(void)
00898     {
00899         standby_reset = 1;
00900     }
00901 
00902     /**
00903      * @brief  Putting the device in reset mode.
00904      * @param  None.
00905      * @retval None.
00906      */
00907     void L6474_Reset(void)
00908     {
00909         standby_reset = 0;
00910     }
00911 
00912     /**
00913      * @brief  Setting the direction of rotation.
00914      * @param  gpioState direction of rotation: "1" for forward, "0" for backward.
00915      * @retval None.
00916      */
00917     void L6474_SetDirectionGpio(uint8_t gpioState)
00918     {
00919         direction = gpioState;
00920     }
00921 
00922     /**
00923      * @brief      Writing and reading bytes to/from the component through the SPI at the same time.
00924      * @param[in]  pByteToTransmit pointer to the buffer of data to send.
00925      * @param[out] pReceivedByte pointer to the buffer to read data into.
00926      * @retval     "0" in case of success, "1" otherwise.
00927      */
00928     uint8_t L6474_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte)
00929     {
00930         return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, number_of_devices) == COMPONENT_OK ? 0 : 1);
00931     }
00932 
00933 
00934     /*** Component's Instance Variables ***/
00935 
00936     /* ACTION 9 --------------------------------------------------------------*
00937      * Declare here interrupt related variables, if needed.                   *
00938      * Note that interrupt handling is platform dependent, see                *
00939      * "Interrupt Related Methods" above.                                     *
00940      *                                                                        *
00941      * Example:                                                               *
00942      *   + mbed:                                                              *
00943      *     InterruptIn feature_irq;                                           *
00944      *------------------------------------------------------------------------*/
00945     /* Flag Interrupt. */
00946     InterruptIn flag_irq;
00947 
00948     /* ACTION 10 -------------------------------------------------------------*
00949      * Declare here other pin related variables, if needed.                   *
00950      *                                                                        *
00951      * Example:                                                               *
00952      *   + mbed:                                                              *
00953      *     DigitalOut standby_reset;                                          *
00954      *------------------------------------------------------------------------*/
00955     /* Standby/reset pin. */
00956     DigitalOut standby_reset;
00957 
00958     /* Direction of rotation pin. */
00959     DigitalOut direction;
00960 
00961     /* Pulse Width Modulation pin. */
00962     PwmOut pwm;
00963 
00964     /* Timer to trigger the PWM callback at each PWM pulse. */
00965     Ticker ticker;
00966 
00967     /* ACTION 11 -------------------------------------------------------------*
00968      * Declare here communication related variables, if needed.               *
00969      *                                                                        *
00970      * Example:                                                               *
00971      *   + mbed:                                                              *
00972      *     DigitalOut ssel;                                                   *
00973      *     DevSPI &dev_spi;                                                   *
00974      *------------------------------------------------------------------------*/
00975     /* Configuration. */
00976     DigitalOut ssel;
00977 
00978     /* IO Device. */
00979     DevSPI &dev_spi;
00980 
00981     /* ACTION 12 -------------------------------------------------------------*
00982      * Declare here identity related variables, if needed.                    *
00983      * Note that there should be only a unique identifier for each component, *
00984      * which should be the "who_am_i" parameter.                              *
00985      *------------------------------------------------------------------------*/
00986     /* Identity */
00987     uint8_t who_am_i;
00988 
00989     /* ACTION 13 -------------------------------------------------------------*
00990      * Declare here the component's static and non-static data, one variable  *
00991      * per line.                                                              *
00992      *                                                                        *
00993      * Example:                                                               *
00994      *   float measure;                                                       *
00995      *   int instance_id;                                                     *
00996      *   static int number_of_instances;                                      *
00997      *------------------------------------------------------------------------*/
00998     /* Data. */
00999     void (*error_handler_callback)(uint16_t error);
01000     deviceParams_t device_prm;
01001     uint8_t device_instance;
01002 
01003     /* Static data. */
01004     static uint8_t number_of_devices;
01005     static uint8_t spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES];
01006     static uint8_t spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES];
01007 
01008 
01009 public:
01010 
01011     /* Static data. */
01012     static bool spi_preemtion_by_isr;
01013     static bool isr_flag;
01014 };
01015 
01016 #endif // __L6474_CLASS_H
01017 
01018 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/