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
Motor Control Library
Introduction
Library to handle the X-NUCLEO-IHM01A1 Motor Control Expansion Board based on the the L6474 component.
Daisy-Chain Configuration
This board can be stacked up to three times so that the L6474 components will be connected in daisy-chain configuration. For this purpose, some resistors must be correctly connected on the boards as depicted here below:
Platform compatibility
- NUCLEO boards have been tested with the default configuration provided by the HelloWorld_IHM01A1 example.
- LPCXpresso11U68 board has been tested with the following patch:
- to connect with a wire from the LPCX’s
D4
pin to the IHM01A1’sD9
pin; - to initialize the pwm PinName variable with
D4
rather thanD9
.
- to connect with a wire from the LPCX’s
- FRDM-K64F board has been tested with the following patch:
- to connect with a wire from the FRDM’s
D4
pin to the IHM01A1’sD8
pin; - to initialize the standby_reset PinName variable with
D4
rather thanD8
.
- to connect with a wire from the FRDM’s
Example Applications
- HelloWorld_IHM01A1
- HelloWorld_IHM01A1_2Motors
- MotorControl_IHM01A1
- MemsMotorControl
- MemsMotorControl_IHM01A1_IKS01A2
Diff: Components/l6474/l6474.h
- Revision:
- 33:8daea0279301
- Parent:
- 27:990963ed581b
diff -r 6e5198e46287 -r 8daea0279301 Components/l6474/l6474.h --- a/Components/l6474/l6474.h Tue Feb 28 16:12:31 2017 +0000 +++ b/Components/l6474/l6474.h Fri Mar 10 11:07:50 2017 +0100 @@ -1,11 +1,10 @@ /** ****************************************************************************** - * @file l6474.h - * @author IPC Rennes - * @version V1.5.0 - * @date November 12, 2014 - * @brief Header for L6474 driver (fully integrated microstepping motor driver) - * @note (C) COPYRIGHT 2014 STMicroelectronics + * @file L6474.h + * @author Davide Aliprandi, STMicroelectronics + * @version V1.0.0 + * @date October 14th, 2015 + * @brief This file contains the class of an L6474 Motor Control component. ****************************************************************************** * @attention * @@ -37,564 +36,983 @@ */ +/* Generated with STM32CubeTOO -----------------------------------------------*/ + + +/* Revision ------------------------------------------------------------------*/ +/* + Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev + Branch/Trunk/Tag: trunk + Based on: X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.h + Revision: 0 +*/ + + /* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __L6474_H -#define __L6474_H - -#ifdef __cplusplus - extern "C" { -#endif +#ifndef __L6474_CLASS_H +#define __L6474_CLASS_H /* Includes ------------------------------------------------------------------*/ -#include "l6474_target_config.h" -#include "../Common/motor.h" +/* ACTION 1 ------------------------------------------------------------------* + * Include here platform specific header files. * + *----------------------------------------------------------------------------*/ +#include "mbed.h" +#include "DevSPI.h" +/* ACTION 2 ------------------------------------------------------------------* + * Include here component specific header files. * + *----------------------------------------------------------------------------*/ +#include "L6474_def.h" +/* ACTION 3 ------------------------------------------------------------------* + * Include here interface specific header files. * + * * + * Example: * + * #include "HumiditySensor.h" * + * #include "TemperatureSensor.h" * + *----------------------------------------------------------------------------*/ +#include "StepperMotor.h" + + +/* Classes -------------------------------------------------------------------*/ + +/** + * @brief Class representing an L6474 component. + */ +class L6474 : public StepperMotor +{ +public: + + /*** Constructor and Destructor Methods ***/ + + /** + * @brief Constructor. + * @param flag_irq pin name of the FLAG pin of the component. + * @param standby_reset pin name of the STBY\RST pin of the component. + * @param direction pin name of the DIR pin of the component. + * @param pwm pin name of the PWM pin of the component. + * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. + * @param spi SPI device to be used for communication. + */ + 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) + { + /* Checking stackability. */ + if (!(number_of_devices < MAX_NUMBER_OF_DEVICES)) { + error("Instantiation of the L6474 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES); + } + + /* ACTION 4 ----------------------------------------------------------* + * Initialize here the component's member variables, one variable per * + * line. * + * * + * Example: * + * measure = 0; * + * instance_id = number_of_instances++; * + *--------------------------------------------------------------------*/ + error_handler_callback = 0; + device_instance = number_of_devices++; + memset(spi_tx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); + memset(spi_rx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); + } + + /** + * @brief Destructor. + */ + virtual ~L6474(void) {} -/* Definitions ---------------------------------------------------------------*/ - -/** @addtogroup BSP - * @{ - */ - -/** @addtogroup Components - * @{ - */ - -/** @addtogroup L6474 - * @{ - */ - -/** @defgroup L6474_Exported_Defines L6474_Exported_Defines - * @{ - */ - -/// Current FW version -#define L6474_FW_VERSION (5) + /*** Public Component Related Methods ***/ -/// L6474 max number of bytes of command & arguments to set a parameter -#define L6474_CMD_ARG_MAX_NB_BYTES (4) - -/// L6474 command + argument bytes number for GET_STATUS command -#define L6474_CMD_ARG_NB_BYTES_GET_STATUS (1) - -/// L6474 response bytes number -#define L6474_RSP_NB_BYTES_GET_STATUS (2) + /* ACTION 5 --------------------------------------------------------------* + * Implement here the component's public methods, as wrappers of the C * + * component's functions. * + * They should be: * + * + Methods with the same name of the C component's virtual table's * + * functions (1); * + * + Methods with the same name of the C component's extended virtual * + * table's functions, if any (2). * + * * + * Example: * + * virtual int get_value(float *p_data) //(1) * + * { * + * return COMPONENT_get_value(float *pf_data); * + * } * + * * + * virtual int enable_feature(void) //(2) * + * { * + * return COMPONENT_enable_feature(); * + * } * + *------------------------------------------------------------------------*/ + /** + * @brief Initializing the component in 1/16 Microstepping mode. + * @param init Pointer to device specific initalization structure. + * @retval "0" in case of success, an error code otherwise. + */ + virtual int init(void *init = NULL) + { + return (int) L6474_Init((void *) init); + } -/// L6474 value mask for ABS_POS register -#define L6474_ABS_POS_VALUE_MASK ((uint32_t) 0x003FFFFF) - -/// L6474 sign bit mask for ABS_POS register -#define L6474_ABS_POS_SIGN_BIT_MASK ((uint32_t) 0x00200000) + /** + * @brief Getting the ID of the component. + * @param id Pointer to an allocated variable to store the ID into. + * @retval "0" in case of success, an error code otherwise. + */ + virtual int read_id(uint8_t *id = NULL) + { + return (int) L6474_ReadID((uint8_t *) id); + } - -/* Types ---------------------------------------------------------------------*/ - -/** @defgroup L6474_Exported_Types - * @{ - */ + /** + * @brief Getting the value of the Status Register. + * @param None. + * @retval None. + * @note The Status Register's flags are cleared, contrary to the + * read_status_register() method. + */ + virtual unsigned int get_status(void) + { + return (unsigned int) L6474_CmdGetStatus(); + } -/** @defgroup L6474_Fast_Decay_Time_Options - * @{ - */ -///TOFF_FAST values for T_FAST register -typedef enum { - L6474_TOFF_FAST_2us = ((uint8_t) 0x00 << 4), - L6474_TOFF_FAST_4us = ((uint8_t) 0x01 << 4), - L6474_TOFF_FAST_6us = ((uint8_t) 0x02 << 4), - L6474_TOFF_FAST_8us = ((uint8_t) 0x03 << 4), - L6474_TOFF_FAST_10us = ((uint8_t) 0x04 << 4), - L6474_TOFF_FAST_12us = ((uint8_t) 0x05 << 4), - L6474_TOFF_FAST_14us = ((uint8_t) 0x06 << 4), - L6474_TOFF_FAST_16us = ((uint8_t) 0x07 << 4), - L6474_TOFF_FAST_18us = ((uint8_t) 0x08 << 4), - L6474_TOFF_FAST_20us = ((uint8_t) 0x09 << 4), - L6474_TOFF_FAST_22us = ((uint8_t) 0x0A << 4), - L6474_TOFF_FAST_24us = ((uint8_t) 0x0B << 4), - L6474_TOFF_FAST_26us = ((uint8_t) 0x0C << 4), - L6474_TOFF_FAST_28us = ((uint8_t) 0x0D << 4), - L6474_TOFF_FAST_30us = ((uint8_t) 0x0E << 4), - L6474_TOFF_FAST_32us = ((uint8_t) 0x0F << 4) -} L6474_TOFF_FAST_t; -/** - * @} - */ + /** + * @brief Getting a parameter. + * @param parameter A parameter's register address. + * @retval The parameter's value. + * @note The Status Register's flags are cleared, contrary to the + * read_status_register() method. + * The parameter can be one of the following: + * + L6474_ABS_POS + * + L6474_EL_POS + * + L6474_MARK + * + L6474_RESERVED_REG01 + * + L6474_RESERVED_REG02 + * + L6474_RESERVED_REG03 + * + L6474_RESERVED_REG04 + * + L6474_RESERVED_REG05 + * + L6474_RESERVED_REG06 + * + L6474_TVAL : value in mA + * + L6474_RESERVED_REG07 + * + L6474_RESERVED_REG08 + * + L6474_RESERVED_REG09 + * + L6474_RESERVED_REG10 + * + L6474_T_FAST + * + L6474_TON_MIN : value in us + * + L6474_TOFF_MIN : value in us + * + L6474_RESERVED_REG11 + * + L6474_ADC_OUT + * + L6474_OCD_TH + * + L6474_RESERVED_REG12 + * + L6474_STEP_MODE + * + L6474_ALARM_EN + * + L6474_CONFIG + * + L6474_STATUS + * + L6474_RESERVED_REG13 + * + L6474_RESERVED_REG14 + * + L6474_INEXISTENT_REG + */ + virtual float get_parameter(unsigned int parameter) + { + unsigned int register_value = (unsigned int) L6474_CmdGetParam((L6474_Registers_t) parameter); + float value; -/** @defgroup L6474_Fall_Step_Time_Options - * @{ - */ -///FAST_STEP values for T_FAST register -typedef enum { - L6474_FAST_STEP_2us = ((uint8_t) 0x00), - L6474_FAST_STEP_4us = ((uint8_t) 0x01), - L6474_FAST_STEP_6us = ((uint8_t) 0x02), - L6474_FAST_STEP_8us = ((uint8_t) 0x03), - L6474_FAST_STEP_10us = ((uint8_t) 0x04), - L6474_FAST_STEP_12us = ((uint8_t) 0x05), - L6474_FAST_STEP_14us = ((uint8_t) 0x06), - L6474_FAST_STEP_16us = ((uint8_t) 0x07), - L6474_FAST_STEP_18us = ((uint8_t) 0x08), - L6474_FAST_STEP_20us = ((uint8_t) 0x09), - L6474_FAST_STEP_22us = ((uint8_t) 0x0A), - L6474_FAST_STEP_24us = ((uint8_t) 0x0B), - L6474_FAST_STEP_26us = ((uint8_t) 0x0C), - L6474_FAST_STEP_28us = ((uint8_t) 0x0D), - L6474_FAST_STEP_30us = ((uint8_t) 0x0E), - L6474_FAST_STEP_32us = ((uint8_t) 0x0F) -} L6474_FAST_STEP_t; -/** - * @} - */ + switch ((L6474_Registers_t) parameter) { + case L6474_TVAL: + value = L6474_Par_to_Tval_Current((float) register_value); + break; + case L6474_TON_MIN: + case L6474_TOFF_MIN: + value = L6474_Par_to_Tmin_Time((float) register_value); + break; + default: + value = (float) register_value; + break; + } + + return value; + } + + /** + * @brief Getting the position. + * @param None. + * @retval The position. + */ + virtual signed int get_position(void) + { + return (signed int) L6474_GetPosition(); + } -/** @defgroup L6474_Overcurrent_Threshold_options - * @{ - */ -///OCD_TH register -typedef enum { - L6474_OCD_TH_375mA = ((uint8_t) 0x00), - L6474_OCD_TH_750mA = ((uint8_t) 0x01), - L6474_OCD_TH_1125mA = ((uint8_t) 0x02), - L6474_OCD_TH_1500mA = ((uint8_t) 0x03), - L6474_OCD_TH_1875mA = ((uint8_t) 0x04), - L6474_OCD_TH_2250mA = ((uint8_t) 0x05), - L6474_OCD_TH_2625mA = ((uint8_t) 0x06), - L6474_OCD_TH_3000mA = ((uint8_t) 0x07), - L6474_OCD_TH_3375mA = ((uint8_t) 0x08), - L6474_OCD_TH_3750mA = ((uint8_t) 0x09), - L6474_OCD_TH_4125mA = ((uint8_t) 0x0A), - L6474_OCD_TH_4500mA = ((uint8_t) 0x0B), - L6474_OCD_TH_4875mA = ((uint8_t) 0x0C), - L6474_OCD_TH_5250mA = ((uint8_t) 0x0D), - L6474_OCD_TH_5625mA = ((uint8_t) 0x0E), - L6474_OCD_TH_6000mA = ((uint8_t) 0x0F) -} L6474_OCD_TH_t; -/** - * @} - */ + /** + * @brief Getting the marked position. + * @param None. + * @retval The marked position. + */ + virtual signed int get_mark(void) + { + return (signed int) L6474_GetMark(); + } + + /** + * @brief Getting the current speed in pps. + * @param None. + * @retval The current speed in pps. + */ + virtual unsigned int get_speed(void) + { + return (unsigned int) L6474_GetCurrentSpeed(); + } + + /** + * @brief Getting the maximum speed in pps. + * @param None. + * @retval The maximum speed in pps. + */ + virtual unsigned int get_max_speed(void) + { + return (unsigned int) L6474_GetMaxSpeed(); + } -/** @defgroup L6474_STEP_MODE_Register_Masks - * @{ - */ -///STEP_MODE register -typedef enum { - L6474_STEP_MODE_STEP_SEL = ((uint8_t) 0x07), - L6474_STEP_MODE_SYNC_SEL = ((uint8_t) 0x70) -} L6474_STEP_MODE_Masks_t; -/** - * @} - */ + /** + * @brief Getting the minimum speed in pps. + * @param None. + * @retval The minimum speed in pps. + */ + virtual unsigned int get_min_speed(void) + { + return (unsigned int) L6474_GetMinSpeed(); + } -/** @defgroup L6474_STEP_SEL_Options_For_STEP_MODE_Register - * @{ - */ -///STEP_SEL field of STEP_MODE register -typedef enum { - L6474_STEP_SEL_1 = ((uint8_t) 0x08), //full step - L6474_STEP_SEL_1_2 = ((uint8_t) 0x09), //half step - L6474_STEP_SEL_1_4 = ((uint8_t) 0x0A), //1/4 microstep - L6474_STEP_SEL_1_8 = ((uint8_t) 0x0B), //1/8 microstep - L6474_STEP_SEL_1_16 = ((uint8_t) 0x0C) //1/16 microstep -} L6474_STEP_SEL_t; -/** - * @} - */ + /** + * @brief Getting the acceleration in pps^2. + * @param None. + * @retval The acceleration in pps^2. + */ + virtual unsigned int get_acceleration(void) + { + return (unsigned int) L6474_GetAcceleration(); + } + + /** + * @brief Getting the deceleration in pps^2. + * @param None. + * @retval The deceleration in pps^2. + */ + virtual unsigned int get_deceleration(void) + { + return (unsigned int) L6474_GetDeceleration(); + } -/** @defgroup L6474_SYNC_SEL_Options_For_STEP_MODE_Register - * @{ - */ -///SYNC_SEL field of STEP_MODE register -typedef enum { - L6474_SYNC_SEL_1_2 = ((uint8_t) 0x80), - L6474_SYNC_SEL_1 = ((uint8_t) 0x90), - L6474_SYNC_SEL_2 = ((uint8_t) 0xA0), - L6474_SYNC_SEL_4 = ((uint8_t) 0xB0), - L6474_SYNC_SEL_8 = ((uint8_t) 0xC0), - L6474_SYNC_SEL_UNUSED = ((uint8_t) 0xD0) -} L6474_SYNC_SEL_t; -/** - * @} - */ - -/** @defgroup L6474_ALARM_EN_Register_Options - * @{ - */ -///ALARM_EN register -typedef enum { - L6474_ALARM_EN_OVERCURRENT = ((uint8_t) 0x01), - L6474_ALARM_EN_THERMAL_SHUTDOWN = ((uint8_t) 0x02), - L6474_ALARM_EN_THERMAL_WARNING = ((uint8_t) 0x04), - L6474_ALARM_EN_UNDERVOLTAGE = ((uint8_t) 0x08), - L6474_ALARM_EN_SW_TURN_ON = ((uint8_t) 0x40), - L6474_ALARM_EN_WRONG_NPERF_CMD = ((uint8_t) 0x80) -} L6474_ALARM_EN_t; -/** - * @} - */ + /** + * @brief Getting the direction of rotation. + * @param None. + * @retval The direction of rotation. + */ + virtual direction_t get_direction(void) + { + return (direction_t) (L6474_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD); + } -/** @defgroup L6474_CONFIG_Register_Masks - * @{ - */ -///CONFIG register -typedef enum { - L6474_CONFIG_OSC_SEL = ((uint16_t) 0x0007), - L6474_CONFIG_EXT_CLK = ((uint16_t) 0x0008), - L6474_CONFIG_EN_TQREG = ((uint16_t) 0x0020), - L6474_CONFIG_OC_SD = ((uint16_t) 0x0080), - L6474_CONFIG_POW_SR = ((uint16_t) 0x0300), - L6474_CONFIG_TOFF = ((uint16_t) 0x7C00) -} L6474_CONFIG_Masks_t; -/** - * @} - */ - -/** @defgroup L6474_Clock_Source_Options_For_CONFIG_Register - * @{ - */ -///Clock source option for CONFIG register -typedef enum { - L6474_CONFIG_INT_16MHZ = ((uint16_t) 0x0000), - L6474_CONFIG_INT_16MHZ_OSCOUT_2MHZ = ((uint16_t) 0x0008), - L6474_CONFIG_INT_16MHZ_OSCOUT_4MHZ = ((uint16_t) 0x0009), - L6474_CONFIG_INT_16MHZ_OSCOUT_8MHZ = ((uint16_t) 0x000A), - L6474_CONFIG_INT_16MHZ_OSCOUT_16MHZ = ((uint16_t) 0x000B), - L6474_CONFIG_EXT_8MHZ_XTAL_DRIVE = ((uint16_t) 0x0004), - L6474_CONFIG_EXT_16MHZ_XTAL_DRIVE = ((uint16_t) 0x0005), - L6474_CONFIG_EXT_24MHZ_XTAL_DRIVE = ((uint16_t) 0x0006), - L6474_CONFIG_EXT_32MHZ_XTAL_DRIVE = ((uint16_t) 0x0007), - L6474_CONFIG_EXT_8MHZ_OSCOUT_INVERT = ((uint16_t) 0x000C), - L6474_CONFIG_EXT_16MHZ_OSCOUT_INVERT = ((uint16_t) 0x000D), - L6474_CONFIG_EXT_24MHZ_OSCOUT_INVERT = ((uint16_t) 0x000E), - L6474_CONFIG_EXT_32MHZ_OSCOUT_INVERT = ((uint16_t) 0x000F) -} L6474_CONFIG_OSC_MGMT_t; -/** - * @} - */ - -/** @defgroup L6474_External_Torque_Regulation_Options_For_CONFIG_Register - * @{ - */ -///External Torque regulation options for CONFIG register -typedef enum { - L6474_CONFIG_EN_TQREG_TVAL_USED = ((uint16_t) 0x0000), - L6474_CONFIG_EN_TQREG_ADC_OUT = ((uint16_t) 0x0020) -} L6474_CONFIG_EN_TQREG_t; -/** - * @} - */ + /** + * @brief Setting a parameter. + * @param parameter A parameter's register address. + * @param value The parameter's value. + * @retval None. + * @note The parameter can be one of the following: + * + L6474_ABS_POS + * + L6474_EL_POS + * + L6474_MARK + * + L6474_RESERVED_REG01 + * + L6474_RESERVED_REG02 + * + L6474_RESERVED_REG03 + * + L6474_RESERVED_REG04 + * + L6474_RESERVED_REG05 + * + L6474_RESERVED_REG06 + * + L6474_TVAL : value in mA + * + L6474_RESERVED_REG07 + * + L6474_RESERVED_REG08 + * + L6474_RESERVED_REG09 + * + L6474_RESERVED_REG10 + * + L6474_T_FAST + * + L6474_TON_MIN : value in us + * + L6474_TOFF_MIN : value in us + * + L6474_RESERVED_REG11 + * + L6474_ADC_OUT + * + L6474_OCD_TH + * + L6474_RESERVED_REG12 + * + L6474_STEP_MODE + * + L6474_ALARM_EN + * + L6474_CONFIG + * + L6474_STATUS + * + L6474_RESERVED_REG13 + * + L6474_RESERVED_REG14 + * + L6474_INEXISTENT_REG + * @warning Some registers can only be written in particular conditions (see L6474's datasheet). + * Any attempt to write one of those registers when the conditions are not satisfied + * causes the command to be ignored and the NOTPERF_CMD flag to rise at the end of the + * last argument byte. Any attempt to set an inexistent register (wrong address value) + * causes the command to be ignored and the WRONG_CMD flag to rise. + * For example, setting some parameters requires first to disable the power bridge; + * this can be done through the soft_hiz() method. + * They are the following: + * + L6474_EL_POS + * + L6474_T_FAST + * + L6474_TON_MIN : value in us + * + L6474_TOFF_MIN : value in us + * + L6474_ADC_OUT + * + L6474_STEP_MODE + * + L6474_CONFIG + * + L6474_STATUS + */ + virtual void set_parameter(unsigned int parameter, float value) + { + float register_value; -/** @defgroup L6474_Over_Current_Shutdown_Options_For_CONFIG_Register - * @{ - */ -///Over Current Shutdown options for CONFIG register -typedef enum { - L6474_CONFIG_OC_SD_DISABLE = ((uint16_t) 0x0000), - L6474_CONFIG_OC_SD_ENABLE = ((uint16_t) 0x0080) -} L6474_CONFIG_OC_SD_t; -/** - * @} - */ + switch ((L6474_Registers_t) parameter) { + case L6474_TVAL: + register_value = L6474_Tval_Current_to_Par(value); + break; + case L6474_TON_MIN: + case L6474_TOFF_MIN: + register_value = L6474_Tmin_Time_to_Par(value); + break; + default: + register_value = value; + break; + } + + L6474_CmdSetParam((L6474_Registers_t) parameter, (unsigned int) register_value); + } + + /** + * @brief Setting the current position to be the home position. + * @param None. + * @retval None. + */ + virtual void set_home(void) + { + L6474_SetHome(); + } -/** @defgroup L6474_Power_Bridge_Output_Slew_Rate_Options - * @{ - */ -/// POW_SR values for CONFIG register -typedef enum { - L6474_CONFIG_SR_320V_us =((uint16_t)0x0000), - L6474_CONFIG_SR_075V_us =((uint16_t)0x0100), - L6474_CONFIG_SR_110V_us =((uint16_t)0x0200), - L6474_CONFIG_SR_260V_us =((uint16_t)0x0300) -} L6474_CONFIG_POW_SR_t; -/** - * @} - */ + /** + * @brief Setting the current position to be the marked position. + * @param None. + * @retval None. + */ + virtual void set_mark(void) + { + L6474_SetMark(); + } + + /** + * @brief Setting the maximum speed in pps. + * @param speed The maximum speed in pps. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_max_speed(unsigned int speed) + { + L6474_SetMaxSpeed((unsigned int) speed); + return true; + } + + /** + * @brief Setting the minimum speed in pps. + * @param speed The minimum speed in pps. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_min_speed(unsigned int speed) + { + L6474_SetMinSpeed((unsigned int) speed); + return true; + } -/** @defgroup L6474_Off_Time_Options - * @{ - */ -/// TOFF values for CONFIG register -typedef enum { - L6474_CONFIG_TOFF_004us = (((uint16_t) 0x01) << 10), - L6474_CONFIG_TOFF_008us = (((uint16_t) 0x02) << 10), - L6474_CONFIG_TOFF_012us = (((uint16_t) 0x03) << 10), - L6474_CONFIG_TOFF_016us = (((uint16_t) 0x04) << 10), - L6474_CONFIG_TOFF_020us = (((uint16_t) 0x05) << 10), - L6474_CONFIG_TOFF_024us = (((uint16_t) 0x06) << 10), - L6474_CONFIG_TOFF_028us = (((uint16_t) 0x07) << 10), - L6474_CONFIG_TOFF_032us = (((uint16_t) 0x08) << 10), - L6474_CONFIG_TOFF_036us = (((uint16_t) 0x09) << 10), - L6474_CONFIG_TOFF_040us = (((uint16_t) 0x0A) << 10), - L6474_CONFIG_TOFF_044us = (((uint16_t) 0x0B) << 10), - L6474_CONFIG_TOFF_048us = (((uint16_t) 0x0C) << 10), - L6474_CONFIG_TOFF_052us = (((uint16_t) 0x0D) << 10), - L6474_CONFIG_TOFF_056us = (((uint16_t) 0x0E) << 10), - L6474_CONFIG_TOFF_060us = (((uint16_t) 0x0F) << 10), - L6474_CONFIG_TOFF_064us = (((uint16_t) 0x10) << 10), - L6474_CONFIG_TOFF_068us = (((uint16_t) 0x11) << 10), - L6474_CONFIG_TOFF_072us = (((uint16_t) 0x12) << 10), - L6474_CONFIG_TOFF_076us = (((uint16_t) 0x13) << 10), - L6474_CONFIG_TOFF_080us = (((uint16_t) 0x14) << 10), - L6474_CONFIG_TOFF_084us = (((uint16_t) 0x15) << 10), - L6474_CONFIG_TOFF_088us = (((uint16_t) 0x16) << 10), - L6474_CONFIG_TOFF_092us = (((uint16_t) 0x17) << 10), - L6474_CONFIG_TOFF_096us = (((uint16_t) 0x18) << 10), - L6474_CONFIG_TOFF_100us = (((uint16_t) 0x19) << 10), - L6474_CONFIG_TOFF_104us = (((uint16_t) 0x1A) << 10), - L6474_CONFIG_TOFF_108us = (((uint16_t) 0x1B) << 10), - L6474_CONFIG_TOFF_112us = (((uint16_t) 0x1C) << 10), - L6474_CONFIG_TOFF_116us = (((uint16_t) 0x1D) << 10), - L6474_CONFIG_TOFF_120us = (((uint16_t) 0x1E) << 10), - L6474_CONFIG_TOFF_124us = (((uint16_t) 0x1F) << 10) -} L6474_CONFIG_TOFF_t; -/** - * @} - */ + /** + * @brief Setting the acceleration in pps^2. + * @param acceleration The acceleration in pps^2. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_acceleration(unsigned int acceleration) + { + L6474_SetAcceleration((unsigned int) acceleration); + return true; + } + + /** + * @brief Setting the deceleration in pps^2. + * @param deceleration The deceleration in pps^2. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_deceleration(unsigned int deceleration) + { + L6474_SetDeceleration((unsigned int) deceleration); + return true; + } + + /** + * @brief Going to a specified position. + * @param position The desired position. + * @retval None. + */ + virtual void go_to(signed int position) + { + L6474_GoTo((signed int) position); + } -/** @defgroup L6474_STATUS_Register_Bit_Masks - * @{ - */ -///STATUS Register Bit Masks -typedef enum { - L6474_STATUS_HIZ = (((uint16_t) 0x0001)), - L6474_STATUS_DIR = (((uint16_t) 0x0010)), - L6474_STATUS_NOTPERF_CMD = (((uint16_t) 0x0080)), - L6474_STATUS_WRONG_CMD = (((uint16_t) 0x0100)), - L6474_STATUS_UVLO = (((uint16_t) 0x0200)), - L6474_STATUS_TH_WRN = (((uint16_t) 0x0400)), - L6474_STATUS_TH_SD = (((uint16_t) 0x0800)), - L6474_STATUS_OCD = (((uint16_t) 0x1000)) -} L6474_STATUS_Masks_t; -/** - * @} - */ + /** + * @brief Going to the home position. + * @param None. + * @retval None. + */ + virtual void go_home(void) + { + L6474_GoHome(); + } + + /** + * @brief Going to the marked position. + * @param None. + * @retval None. + */ + virtual void go_mark(void) + { + L6474_GoMark(); + } -/** @defgroup L6474_Direction_Field_Of_STATUS_Register - * @{ - */ -///Diretion field of STATUS register -typedef enum { - L6474_STATUS_DIR_FORWARD = (((uint16_t) 0x0001) << 4), - L6474_STATUS_DIR_REVERSE = (((uint16_t) 0x0000) << 4) -} L6474_STATUS_DIR_t; -/** - * @} - */ + /** + * @brief Running the motor towards a specified direction. + * @param direction The direction of rotation. + * @retval None. + */ + virtual void run(direction_t direction) + { + L6474_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); + } + + /** + * @brief Moving the motor towards a specified direction for a certain number of steps. + * @param direction The direction of rotation. + * @param steps The desired number of steps. + * @retval None. + */ + virtual void move(direction_t direction, unsigned int steps) + { + L6474_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (unsigned int) steps); + } -/** @defgroup L6474_Internal_Register_Addresses - * @{ - */ -/// Internal L6474 register addresses -typedef enum { - L6474_ABS_POS = ((uint8_t) 0x01), - L6474_EL_POS = ((uint8_t) 0x02), - L6474_MARK = ((uint8_t) 0x03), - L6474_RESERVED_REG01 = ((uint8_t) 0x04), - L6474_RESERVED_REG02 = ((uint8_t) 0x05), - L6474_RESERVED_REG03 = ((uint8_t) 0x06), - L6474_RESERVED_REG04 = ((uint8_t) 0x07), - L6474_RESERVED_REG05 = ((uint8_t) 0x08), - L6474_RESERVED_REG06 = ((uint8_t) 0x15), - L6474_TVAL = ((uint8_t) 0x09), - L6474_RESERVED_REG07 = ((uint8_t) 0x0A), - L6474_RESERVED_REG08 = ((uint8_t) 0x0B), - L6474_RESERVED_REG09 = ((uint8_t) 0x0C), - L6474_RESERVED_REG10 = ((uint8_t) 0x0D), - L6474_T_FAST = ((uint8_t) 0x0E), - L6474_TON_MIN = ((uint8_t) 0x0F), - L6474_TOFF_MIN = ((uint8_t) 0x10), - L6474_RESERVED_REG11 = ((uint8_t) 0x11), - L6474_ADC_OUT = ((uint8_t) 0x12), - L6474_OCD_TH = ((uint8_t) 0x13), - L6474_RESERVED_REG12 = ((uint8_t) 0x14), - L6474_STEP_MODE = ((uint8_t) 0x16), - L6474_ALARM_EN = ((uint8_t) 0x17), - L6474_CONFIG = ((uint8_t) 0x18), - L6474_STATUS = ((uint8_t) 0x19), - L6474_RESERVED_REG13 = ((uint8_t) 0x1A), - L6474_RESERVED_REG14 = ((uint8_t) 0x1B), - L6474_INEXISTENT_REG = ((uint8_t) 0x1F) -} L6474_Registers_t; -/** - * @} - */ + /** + * @brief Stopping the motor through an immediate deceleration up to zero speed. + * @param None. + * @retval None. + */ + virtual void soft_stop(void) + { + L6474_SoftStop(); + } + + /** + * @brief Stopping the motor through an immediate infinite deceleration. + * @param None. + * @retval None. + */ + virtual void hard_stop(void) + { + L6474_HardStop(); + } + + /** + * @brief Disabling the power bridge after performing a deceleration to zero. + * @param None. + * @retval None. + */ + virtual void soft_hiz(void) + { + L6474_SoftStop(); + L6474_CmdDisable(); + } -/** @defgroup L6474_Command_Set - * @{ - */ -/// L6474 command set -typedef enum { - L6474_NOP = ((uint8_t) 0x00), - L6474_SET_PARAM = ((uint8_t) 0x00), - L6474_GET_PARAM = ((uint8_t) 0x20), - L6474_ENABLE = ((uint8_t) 0xB8), - L6474_DISABLE = ((uint8_t) 0xA8), - L6474_GET_STATUS = ((uint8_t) 0xD0), - L6474_RESERVED_CMD1 = ((uint8_t) 0xEB), - L6474_RESERVED_CMD2 = ((uint8_t) 0xF8) -} L6474_Commands_t; + /** + * @brief Disabling the power bridge immediately. + * @param None. + * @retval None. + */ + virtual void hard_hiz(void) + { + L6474_HardStop(); + L6474_CmdDisable(); + } -/** - * @brief L6474 driver initialization structure definition. - */ -/* ACTION --------------------------------------------------------------------* - * Declare here the component's initialization structure, if any, one * - * variable per line without initialization. * - * * - * Example: * - * typedef struct * - * { * - * int frequency; * - * int update_mode; * - * } COMPONENT_Init_t; * - *----------------------------------------------------------------------------*/ -typedef struct -{ - /* Acceleration rate in pps^2. Range: (0..+inf). */ - int acceleration_pps_2; + /** + * @brief Waiting while the motor is active. + * @param None. + * @retval None. + */ + virtual void wait_while_active(void) + { + L6474_WaitWhileActive(); + } - /* Deceleration rate in pps^2. Range: (0..+inf). */ - int deceleration_pps_2; - - /* Maximum speed in pps. Range: (30..10000]. */ - int maximum_speed_pps; + /** + * @brief Getting the device state. + * @param None. + * @retval The device state. + * @note The device state can be one of the following: + * + ACCELERATING + * + DECELERATING + * + STEADY + * + INACTIVE + */ + virtual motorState_t get_device_state(void) + { + return (motorState_t) L6474_GetDeviceState(); + } - /* Minimum speed in pps. Range: [30..10000). */ - int minimum_speed_pps; - - /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ - float torque_regulation_current_mA; - - /* Overcurrent threshold (OCD_TH register). */ - L6474_OCD_TH_t overcurrent_threshold; - - /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ - L6474_CONFIG_OC_SD_t overcurrent_shutwdown; - - /* Torque regulation method (EN_TQREG field of CONFIG register). */ - L6474_CONFIG_EN_TQREG_t torque_regulation_method; - - /* Step selection (STEP_SEL field of STEP_MODE register). */ - L6474_STEP_SEL_t step_selection; + /** + * @brief Reading the Status Register. + * @param None. + * @retval None. + * @note The Status Register's flags are not cleared, contrary to the + * GetStatus() method. + */ + virtual uint16_t read_status_register(void) + { + return (uint16_t) L6474_ReadStatusRegister(); + } - /* Sync selection (SYNC_SEL field of STEP_MODE register). */ - L6474_SYNC_SEL_t sync_selection; - - /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ - L6474_FAST_STEP_t fall_time; - - /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ - L6474_TOFF_FAST_t maximum_fast_decay_time; + /** + * @brief Setting the Step Mode. + * @param step_mode The Step Mode. + * @retval "true" in case of success, "false" otherwise. + * @warning Setting the step mode implies first disabling the power bridge through + * the soft_hiz() method. + * @warning Every time step mode is changed, the values of the home + * and mark positions lose meaning and are reset. + */ + virtual bool set_step_mode(step_mode_t step_mode) + { + if ((motorStepMode_t) step_mode > STEP_MODE_1_16) { + return false; + } - /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ - float minimum_ON_time_us; - - /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ - float minimum_OFF_time_us; - - /* Target Swicthing Period (field TOFF of CONFIG register). */ - L6474_CONFIG_TOFF_t target_swicthing_period; - - /* Slew rate (POW_SR field of CONFIG register). */ - L6474_CONFIG_POW_SR_t slew_rate; + soft_hiz(); + L6474_SelectStepMode((motorStepMode_t) step_mode); + return true; + } - /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ - L6474_CONFIG_OSC_MGMT_t clock; - - /* Alarm (ALARM_EN register). */ - int alarm; -} L6474_Init_t; + /** + * @brief Attaching an error handler. + * @param fptr An error handler. + * @retval None. + */ + virtual void attach_error_handler(void (*fptr)(uint16_t error)) + { + L6474_AttachErrorHandler((void (*)(uint16_t error)) fptr); + } -/** - * @brief L6474 driver data structure definition. - */ -/* ACTION --------------------------------------------------------------------* - * Declare here the structure of component's data, if any, one variable per * - * line without initialization. * - * * - * Example: * - * typedef struct * - * { * - * int T0_out; * - * int T1_out; * - * float T0_degC; * - * float T1_degC; * - * } COMPONENT_Data_t; * - *----------------------------------------------------------------------------*/ -typedef struct -{ - /// Function pointer to flag interrupt call back - void (*flagInterruptCallback)(void); - /// Function pointer to error handler call back - void (*errorHandlerCallback)(uint16_t error); - bool spiPreemtionByIsr; // = FALSE; - bool isrFlag; // = FALSE; - /// L6474 Device Paramaters structure - deviceParams_t devicePrm; //[MAX_NUMBER_OF_DEVICES]; - uint8_t number_of_devices; - uint8_t device_instance; - uint8_t spiTxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; - uint8_t spiRxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; -} L6474_Data_t; + /** + * @brief Enabling the device. + * @param None. + * @retval None. + */ + virtual void enable(void) + { + L6474_CmdEnable(); + } + + /** + * @brief Disabling the device. + * @param None. + * @retval None. + */ + virtual void disable(void) + { + L6474_CmdDisable(); + } + + /** + * @brief Getting the version of the firmware. + * @param None. + * @retval The version of the firmware. + */ + virtual uint8_t get_fw_version(void) + { + return (uint8_t) L6474_GetFwVersion(); + } -/* Functions -----------------------------------------------------------------*/ + /*** Public Interrupt Related Methods ***/ -/** @addtogroup BSP - * @{ - */ + /* ACTION 6 --------------------------------------------------------------* + * Implement here interrupt related methods, if any. * + * Note that interrupt handling is platform dependent, e.g.: * + * + mbed: * + * InterruptIn feature_irq(pin); //Interrupt object. * + * feature_irq.rise(callback); //Attach a callback. * + * feature_irq.mode(PullNone); //Set interrupt mode. * + * feature_irq.enable_irq(); //Enable interrupt. * + * feature_irq.disable_irq(); //Disable interrupt. * + * + Arduino: * + * attachInterrupt(pin, callback, RISING); //Attach a callback. * + * detachInterrupt(pin); //Detach a callback. * + * * + * Example (mbed): * + * void attach_feature_irq(void (*fptr) (void)) * + * { * + * feature_irq.rise(fptr); * + * } * + * * + * void enable_feature_irq(void) * + * { * + * feature_irq.enable_irq(); * + * } * + * * + * void disable_feature_irq(void) * + * { * + * feature_irq.disable_irq(); * + * } * + *------------------------------------------------------------------------*/ + /** + * @brief Attaching an interrupt handler to the FLAG interrupt. + * @param fptr An interrupt handler. + * @retval None. + */ + void attach_flag_irq(void (*fptr)(void)) + { + flag_irq.fall(fptr); + } + + /** + * @brief Enabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void enable_flag_irq(void) + { + flag_irq.enable_irq(); + } + + /** + * @brief Disabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void disable_flag_irq(void) + { + flag_irq.disable_irq(); + } + + +protected: + + /*** Protected Component Related Methods ***/ -/** @addtogroup Components - * @{ - */ + /* ACTION 7 --------------------------------------------------------------* + * Declare here the component's specific methods. * + * They should be: * + * + Methods with the same name of the C component's virtual table's * + * functions (1); * + * + Methods with the same name of the C component's extended virtual * + * table's functions, if any (2); * + * + Helper methods, if any, like functions declared in the component's * + * source files but not pointed by the component's virtual table (3). * + * * + * Example: * + * status_t COMPONENT_get_value(float *f); //(1) * + * status_t COMPONENT_enable_feature(void); //(2) * + * status_t COMPONENT_compute_average(void); //(3) * + *------------------------------------------------------------------------*/ + void L6474_AttachErrorHandler(void (*callback)(uint16_t error)); + status_t L6474_Init(void *init); + status_t L6474_ReadID(uint8_t *id); + uint16_t L6474_GetAcceleration(void); + uint16_t L6474_GetCurrentSpeed(void); + uint16_t L6474_GetDeceleration(void); + motorState_t L6474_GetDeviceState(void); + uint8_t L6474_GetFwVersion(void); + int32_t L6474_GetMark(void); + uint16_t L6474_GetMaxSpeed(void); + uint16_t L6474_GetMinSpeed(void); + int32_t L6474_GetPosition(void); + void L6474_GoHome(void); + void L6474_GoMark(void); + void L6474_GoTo(int32_t targetPosition); + void L6474_HardStop(void); + void L6474_Move(motorDir_t direction, uint32_t stepCount); + void L6474_Run(motorDir_t direction); + bool L6474_SetAcceleration(uint16_t newAcc); + bool L6474_SetDeceleration(uint16_t newDec); + void L6474_SetHome(void); + void L6474_SetMark(void); + bool L6474_SetMaxSpeed(uint16_t newMaxSpeed); + bool L6474_SetMinSpeed(uint16_t newMinSpeed); + bool L6474_SoftStop(void); + void L6474_WaitWhileActive(void); + void L6474_CmdDisable(void); + void L6474_CmdEnable(void); + uint32_t L6474_CmdGetParam(L6474_Registers_t parameter); + uint16_t L6474_CmdGetStatus(void); + void L6474_CmdNop(void); + void L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value); + uint16_t L6474_ReadStatusRegister(void); + void L6474_SelectStepMode(motorStepMode_t stepMod); + motorDir_t L6474_GetDirection(void); + void L6474_SetDirection(motorDir_t direction); + void L6474_ApplySpeed(uint16_t newSpeed); + void L6474_ComputeSpeedProfile(uint32_t nbSteps); + int32_t L6474_ConvertPosition(uint32_t abs_position_reg); + void L6474_ErrorHandler(uint16_t error); + void L6474_SendCommand(uint8_t param); + void L6474_SetRegisterToPredefinedValues(void); + void L6474_SetRegisterToInitializationValues(L6474_init_t *init); + void L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte); + void L6474_SetDeviceParamsToPredefinedValues(void); + void L6474_StartMovement(void); + void L6474_StepClockHandler(void); + float L6474_Tval_Current_to_Par(float current_mA); + float L6474_Par_to_Tval_Current(float Tval); + float L6474_Tmin_Time_to_Par(float ton_min_us); + float L6474_Par_to_Tmin_Time(float Tmin); -/** @addtogroup L6474 - * @{ - */ + + /*** Component's I/O Methods ***/ -/** @defgroup L6474_Imported_Functions L6474_Imported_Functions - * @{ - */ + /** + * @brief Utility function to read data from L6474. + * @param[out] pBuffer pointer to the buffer to read data into. + * @param[in] NumBytesToRead number of bytes to read. + * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. + */ + status_t Read(uint8_t* pBuffer, uint16_t NumBytesToRead) + { + if (dev_spi.spi_read(pBuffer, ssel, NumBytesToRead) != 0) { + return COMPONENT_ERROR; + } + return COMPONENT_OK; + } + + /** + * @brief Utility function to write data to L6474. + * @param[in] pBuffer pointer to the buffer of data to send. + * @param[in] NumBytesToWrite number of bytes to write. + * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. + */ + status_t Write(uint8_t* pBuffer, uint16_t NumBytesToWrite) + { + if (dev_spi.spi_write(pBuffer, ssel, NumBytesToWrite) != 0) { + return COMPONENT_ERROR; + } + return COMPONENT_OK; + } + + /** + * @brief Utility function to read and write data from/to L6474 at the same time. + * @param[out] pBufferToRead pointer to the buffer to read data into. + * @param[in] pBufferToWrite pointer to the buffer of data to send. + * @param[in] NumBytes number of bytes to read and write. + * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. + */ + status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumBytes) + { + if (dev_spi.spi_read_write(pBufferToRead, pBufferToWrite, ssel, NumBytes) != 0) { + return COMPONENT_ERROR; + } + return COMPONENT_OK; + } -/* ACTION --------------------------------------------------------------------* - * Declare here extern platform-dependent APIs you might need (e.g.: I/O and * - * interrupt related functions), and implement them in a glue-logic file on * - * the target environment, for example within the "x_nucleo_board.c" file. * - * E.g.: * - * extern Status_t COMPONENT_IO_Init (void *handle); * - * extern Status_t COMPONENT_IO_Read (handle, buf, regadd, bytes); * - * extern Status_t COMPONENT_IO_Write(handle, buf, regadd, bytes); * - * extern void COMPONENT_IO_ITConfig(void); * - *----------------------------------------------------------------------------*/ -///Delay of the requested number of milliseconds -extern void L6474_Delay(void *handle, uint32_t delay); -///Enable Irq -extern void L6474_EnableIrq(void *handle); -///Disable Irq -extern void L6474_DisableIrq(void *handle); -///Set PWM1 frequency and start it -extern void L6474_Pwm1SetFreq(void *handle, uint16_t newFreq); -///Set PWM2 frequency and start it -extern void L6474_Pwm2SetFreq(void *handle, uint16_t newFreq); -///Set PWM3 frequency and start it -extern void L6474_Pwm3SetFreq(void *handle, uint16_t newFreq); -///Init the PWM -extern void L6474_PwmInit(void *handle); -///Stop the PWM -extern void L6474_PwmStop(void *handle); -///Reset the L6474 reset pin -extern void L6474_ReleaseReset(void *handle); -///Set the L6474 reset pin -extern void L6474_Reset(void *handle); -///Set direction GPIO -extern void L6474_SetDirectionGpio(void *handle, uint8_t gpioState); -///Write bytes to the L6474s via SPI -extern uint8_t L6474_SpiWriteBytes(void *handle, uint8_t *pByteToTransmit, uint8_t *pReceivedByte); + /* ACTION 8 --------------------------------------------------------------* + * Implement here other I/O methods beyond those already implemented * + * above, which are declared extern within the component's header file. * + *------------------------------------------------------------------------*/ + /** + * @brief Making the CPU wait. + * @param None. + * @retval None. + */ + void L6474_Delay(uint32_t delay) + { + wait_ms(delay); + } + + /** + * @brief Enabling interrupts. + * @param None. + * @retval None. + */ + void L6474_EnableIrq(void) + { + __enable_irq(); + } + + /** + * @brief Disabling interrupts. + * @param None. + * @retval None. + */ + void L6474_DisableIrq(void) + { + __disable_irq(); + } + + /** + * @brief Initialising the PWM. + * @param None. + * @retval None. + */ + void L6474_PwmInit(void) {} + + /** + * @brief Setting the frequency of PWM. + * The frequency controls directly the speed of the device. + * @param frequency the frequency of PWM. + * @retval None. + */ + void L6474_PwmSetFreq(uint16_t frequency) + { + /* Computing the period of PWM. */ + double period = 1.0f / frequency; + + /* Setting the period and the duty-cycle of PWM. */ + pwm.period(period); + pwm.write(0.5f); + + /* Setting a callback with the same period of PWM's, to update the state machine. */ + ticker.attach(Callback<void()>(this, &L6474::L6474_StepClockHandler), period); + } + + /** + * @brief Stopping the PWM. + * @param None. + * @retval None. + */ + void L6474_PwmStop(void) + { + pwm.write(0.0f); + ticker.detach(); + } + + /** + * @brief Putting the device in standby mode. + * @param None. + * @retval None. + */ + void L6474_ReleaseReset(void) + { + standby_reset = 1; + } + + /** + * @brief Putting the device in reset mode. + * @param None. + * @retval None. + */ + void L6474_Reset(void) + { + standby_reset = 0; + } -#ifdef __cplusplus - } -#endif + /** + * @brief Setting the direction of rotation. + * @param gpioState direction of rotation: "1" for forward, "0" for backward. + * @retval None. + */ + void L6474_SetDirectionGpio(uint8_t gpioState) + { + direction = gpioState; + } + + /** + * @brief Writing and reading bytes to/from the component through the SPI at the same time. + * @param[in] pByteToTransmit pointer to the buffer of data to send. + * @param[out] pReceivedByte pointer to the buffer to read data into. + * @retval "0" in case of success, "1" otherwise. + */ + uint8_t L6474_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) + { + return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, number_of_devices) == COMPONENT_OK ? 0 : 1); + } + + + /*** Component's Instance Variables ***/ + + /* ACTION 9 --------------------------------------------------------------* + * Declare here interrupt related variables, if needed. * + * Note that interrupt handling is platform dependent, see * + * "Interrupt Related Methods" above. * + * * + * Example: * + * + mbed: * + * InterruptIn feature_irq; * + *------------------------------------------------------------------------*/ + /* Flag Interrupt. */ + InterruptIn flag_irq; + + /* ACTION 10 -------------------------------------------------------------* + * Declare here other pin related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut standby_reset; * + *------------------------------------------------------------------------*/ + /* Standby/reset pin. */ + DigitalOut standby_reset; + + /* Direction of rotation pin. */ + DigitalOut direction; + + /* Pulse Width Modulation pin. */ + PwmOut pwm; -#endif /* #ifndef __L6474_H */ + /* Timer to trigger the PWM callback at each PWM pulse. */ + Ticker ticker; + + /* ACTION 11 -------------------------------------------------------------* + * Declare here communication related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut ssel; * + * DevSPI &dev_spi; * + *------------------------------------------------------------------------*/ + /* Configuration. */ + DigitalOut ssel; + + /* IO Device. */ + DevSPI &dev_spi; + + /* ACTION 12 -------------------------------------------------------------* + * Declare here identity related variables, if needed. * + * Note that there should be only a unique identifier for each component, * + * which should be the "who_am_i" parameter. * + *------------------------------------------------------------------------*/ + /* Identity */ + uint8_t who_am_i; -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + /* ACTION 13 -------------------------------------------------------------* + * Declare here the component's static and non-static data, one variable * + * per line. * + * * + * Example: * + * float measure; * + * int instance_id; * + * static int number_of_instances; * + *------------------------------------------------------------------------*/ + /* Data. */ + void (*error_handler_callback)(uint16_t error); + deviceParams_t device_prm; + uint8_t device_instance; + + /* Static data. */ + static uint8_t number_of_devices; + static uint8_t spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + static uint8_t spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + + +public: + + /* Static data. */ + static bool spi_preemtion_by_isr; + static bool isr_flag; +}; + +#endif // __L6474_CLASS_H + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/