Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:bc265521eb00, committed 2018-04-27
- Comitter:
- Davidroid
- Date:
- Fri Apr 27 17:02:08 2018 +0000
- Parent:
- 0:77ad55a7f6d2
- Commit message:
- Populate
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Components/Common/motor_def.h Fri Apr 27 17:02:08 2018 +0000
@@ -0,0 +1,479 @@
+/**
+ ******************************************************************************
+ * @file motor_def.h
+ * @author STM
+ * @version V1.7.0
+ * @date August 7th, 2017
+ * @brief This file contains all the functions prototypes for motor drivers.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+
+#ifndef __MOTOR_H
+#define __MOTOR_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+
+/* Includes ------------------------------------------------------------------*/
+
+#include <stdint.h>
+#include "component_def.h"
+
+/** @addtogroup BSP
+ * @{
+ */
+
+/** @addtogroup Components
+ * @{
+ */
+
+/** @defgroup Motor Motor
+ * @{
+ */
+
+/** @defgroup Motor_Exported_Constants Motor Exported Constants
+ * @{
+ */
+
+
+/// boolean for false condition
+#ifndef FALSE
+#define FALSE (0)
+#endif
+/// boolean for true condition
+#ifndef TRUE
+#define TRUE (1)
+#endif
+
+/**
+ * @}
+ */
+
+/** @defgroup Motor_Exported_Types Motor Exported Types
+ * @{
+ */
+
+/** @defgroup Motor_Boolean_Type Motor Boolean Type
+ * @{
+ */
+///bool Type
+typedef uint8_t bool;
+/**
+ * @}
+ */
+
+/** @defgroup Device_Direction_Options Device Direction Options
+ * @{
+ */
+/// Direction options
+typedef enum {
+ BACKWARD = 0,
+ FORWARD = 1,
+ UNKNOW_DIR = ((uint8_t)0xFF)
+} motor_direction_t;
+/**
+ * @}
+ */
+
+/** @defgroup Device_Action_Options Device Action Options
+ * @{
+ */
+/// Action options
+typedef enum {
+ ACTION_RESET = ((uint8_t)0x00),
+ ACTION_COPY = ((uint8_t)0x08)
+} motor_action_t;
+/**
+ * @}
+ */
+
+/** @defgroup Device_States Device States
+ * @{
+ */
+/// Device states
+typedef enum {
+ ACCELERATING = 0,
+ DECELERATINGTOSTOP = 1,
+ DECELERATING = 2,
+ STEADY = 3,
+ INDEX_ACCEL = 4,
+ INDEX_RUN = 5,
+ INDEX_DECEL = 6,
+ INDEX_DWELL = 7,
+ INACTIVE = 8,
+ STANDBY = 9,
+ STANDBYTOINACTIVE = 10
+} motor_state_t;
+/**
+ * @}
+ */
+
+/** @defgroup Device_Step_mode Device Step mode
+ * @{
+ */
+ /// Stepping options
+typedef enum {
+ STEP_MODE_FULL = ((uint8_t)0x00),
+ STEP_MODE_HALF = ((uint8_t)0x01),
+ STEP_MODE_1_4 = ((uint8_t)0x02),
+ STEP_MODE_1_8 = ((uint8_t)0x03),
+ STEP_MODE_1_16 = ((uint8_t)0x04),
+ STEP_MODE_1_32 = ((uint8_t)0x05),
+ STEP_MODE_1_64 = ((uint8_t)0x06),
+ STEP_MODE_1_128 = ((uint8_t)0x07),
+ STEP_MODE_1_256 = ((uint8_t)0x08),
+ STEP_MODE_UNKNOW = ((uint8_t)0xFE),
+ STEP_MODE_WAVE = ((uint8_t)0xFF)
+} motor_step_mode_t;
+
+/**
+ * @}
+ */
+
+/** @defgroup Decay_mode Decay mode
+ * @{
+ */
+/// Decay Mode
+typedef enum {
+ SLOW_DECAY = 0,
+ FAST_DECAY = 1,
+ MIXED_DECAY = 2,
+ UNKNOW_DECAY = ((uint8_t)0xFF)
+} motor_decay_mode_t;
+/**
+ * @}
+ */
+
+/** @defgroup Stop_mode Stop mode
+ * @{
+ */
+/// Stop mode
+typedef enum
+{
+ HOLD_MODE = 0,
+ HIZ_MODE = 1,
+ STANDBY_MODE = 2,
+ UNKNOW_STOP_MODE = ((uint8_t)0xFF)
+} motor_stop_mode_t;
+/**
+ * @}
+ */
+
+/** @defgroup Torque_mode Torque mode
+ * @{
+ */
+/// Torque mode
+typedef enum
+{
+ ACC_TORQUE = 0,
+ DEC_TORQUE = 1,
+ RUN_TORQUE = 2,
+ HOLD_TORQUE = 3,
+ CURRENT_TORQUE = 4,
+ UNKNOW_TORQUE = ((uint8_t)0xFF)
+} motor_torque_mode_t;
+/**
+ * @}
+ */
+
+/** @defgroup Dual_Full_Bridge_Configuration Dual Full Bridge Configuration
+ * @{
+ */
+///Dual full bridge configurations for brush DC motors
+typedef enum {
+ PARALLELING_NONE___1_BIDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B = 0,
+ PARALLELING_NONE___1_BIDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B = 1,
+ PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B = 2,
+ PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B = 3,
+ PARALLELING_IN1A_IN2A__1_UNDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B = 4,
+ PARALLELING_IN1A_IN2A__1_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B = 5,
+ PARALLELING_IN1B_IN2B__1_BIDIR_MOTOR_BRIDGE_A__1_UNDIR_MOTOR_BRIDGE_B = 6,
+ PARALLELING_IN1B_IN2B__2_UNDIR_MOTOR_BRIDGE_A__1_UNDIR_MOTOR_BRIDGE_B = 7,
+ PARALLELING_IN1A_IN2A__IN1B_IN2B__1_UNDIR_MOTOR_BRIDGE_A__1_UNDIR_MOTOR_BRIDGE_B = 8,
+ PARALLELING_IN1A_IN2A__IN1B_IN2B__1_BIDIR_MOTOR = 9,
+ PARALLELING_IN1A_IN1B__IN2A_IN2B__1_UNDIR_MOTOR_BRIDGE_1A__1_UNDIR_MOTOR_BRIDGE_2A = 10,
+ PARALLELING_IN1A_IN1B__IN2A_IN2B__1_BIDIR_MOTOR = 11,
+ PARALLELING_ALL_WITH_IN1A___1_UNDIR_MOTOR = 12,
+ PARALLELING_END_ENUM = 13
+} dual_full_bridge_config_t;
+/**
+ * @}
+ */
+
+/** @defgroup Motor_Driver_Structure Motor Driver Structure
+ * @{
+ */
+/// Motor driver structure definition
+typedef struct
+{
+ /* ACTION ----------------------------------------------------------------*
+ * Declare here the component's generic functions. *
+ * Tag this group of functions with the " Generic " C-style comment. *
+ * A component's interface has to define at least the two generic *
+ * functions provided here below within the "Example" section, as the *
+ * first and second functions of its Virtual Table. They have to be *
+ * specified exactly in the given way. *
+ * *
+ * Example: *
+ * status_t (*Init) (void *handle, void *init); *
+ * status_t (*ReadID) (void *handle, uint8_t *id); *
+ *------------------------------------------------------------------------*/
+ /* Generic */
+ /// Function pointer to Init
+ status_t (*Init)(void *handle, void *init);
+ /// Function pointer to ReadID
+ status_t (*ReadID)(void *handle, uint8_t *id);
+
+ /* ACTION ----------------------------------------------------------------*
+ * Declare here the component's interrupts related functions. *
+ * Tag this group of functions with the " Interrupts " C-style comment. *
+ * Do not specify any function if not required. *
+ * *
+ * Example: *
+ * void (*ConfigIT) (void *handle, int a); *
+ *------------------------------------------------------------------------*/
+ /* Interrupts */
+ /// Function pointer to AttachErrorHandler
+ void(*AttachErrorHandler)(void *handle, void (*callback)(void *handle, uint16_t error));
+ /// Function pointer to AttachFlagInterrupt
+ void (*AttachFlagInterrupt)(void *handle, void (*callback)(void *handle));
+ /// Function pointer to AttachBusyInterrupt
+ void (*AttachBusyInterrupt)(void *handle, void (*callback)(void *handle));
+ /// Function pointer to FlagInterruptHandler
+ void (*FlagInterruptHandler)(void *handle);
+
+ /* ACTION ----------------------------------------------------------------*
+ * Declare here the component's specific functions. *
+ * Tag this group of functions with the " Specific " C-style comment. *
+ * Do not specify any function if not required. *
+ * *
+ * Example: *
+ * status_t (*GetValue) (void *handle, float *f); *
+ *------------------------------------------------------------------------*/
+ /* Specific */
+ /// Function pointer to GetAcceleration
+ uint16_t (*GetAcceleration)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetCurrentSpeed
+ uint16_t (*GetCurrentSpeed)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetDeceleration
+ uint16_t (*GetDeceleration)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetDeviceState
+ motor_state_t(*GetDeviceState)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetFwVersion
+ uint32_t (*GetFwVersion)(void *handle);
+ /// Function pointer to GetMark
+ int32_t (*GetMark)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetMaxSpeed
+ uint16_t (*GetMaxSpeed)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetMinSpeed
+ uint16_t (*GetMinSpeed)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetPosition
+ int32_t (*GetPosition)(void *handle, uint8_t deviceId);
+ /// Function pointer to GoHome
+ void (*GoHome)(void *handle, uint8_t deviceId);
+ /// Function pointer to GoMark
+ void (*GoMark)(void *handle, uint8_t deviceId);
+ /// Function pointer to GoTo
+ void (*GoTo)(void *handle, uint8_t deviceId, int32_t targetPosition);
+ /// Function pointer to HardStop
+ void (*HardStop)(void *handle, uint8_t deviceId);
+ /// Function pointer to Move
+ void (*Move)(void *handle, uint8_t deviceId, motor_direction_t direction, uint32_t stepCount);
+ /// Function pointer to ResetAllDevices
+ void (*ResetAllDevices)(void *handle);
+ /// Function pointer to Run
+ void (*Run)(void *handle, uint8_t deviceId, motor_direction_t direction);
+ /// Function pointer to SetAcceleration
+ bool(*SetAcceleration)(void *handle, uint8_t deviceId, uint16_t newAcc);
+ /// Function pointer to SetDeceleration
+ bool(*SetDeceleration)(void *handle, uint8_t deviceId, uint16_t newDec);
+ /// Function pointer to SetHome
+ void (*SetHome)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetMark
+ void (*SetMark)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetMaxSpeed
+ bool (*SetMaxSpeed)(void *handle, uint8_t deviceId, uint16_t newMaxSpeed);
+ /// Function pointer to SetMinSpeed
+ bool (*SetMinSpeed)(void *handle, uint8_t deviceId, uint16_t newMinSpeed);
+ /// Function pointer to SoftStop
+ bool (*SoftStop)(void *handle, uint8_t deviceId);
+ /// Function pointer to StepClockHandler
+ void (*StepClockHandler)(void *handle, uint8_t deviceId);
+ /// Function pointer to WaitWhileActive
+ void (*WaitWhileActive)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdDisable
+ void (*CmdDisable)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdEnable
+ void (*CmdEnable)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdGetParam
+ uint32_t (*CmdGetParam)(void *handle, uint8_t deviceId, uint32_t cmd);
+ /// Function pointer to CmdGetStatus
+ uint16_t (*CmdGetStatus)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdNop
+ void (*CmdNop)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdSetParam
+ void (*CmdSetParam)(void *handle, uint8_t deviceId, uint32_t cmd, uint32_t param);
+ /// Function pointer to ReadStatusRegister
+ uint16_t (*ReadStatusRegister)(void *handle, uint8_t deviceId);
+ /// Function pointer to ReleaseReset
+ void (*ReleaseReset)(void *handle, uint8_t deviceId);
+ /// Function pointer to Reset
+ void (*Reset)(void *handle, uint8_t deviceId);
+ /// Function pointer to SelectStepMode
+ bool (*SelectStepMode)(void *handle, uint8_t deviceId, motor_step_mode_t stepMode);
+ /// Function pointer to SetDirection
+ void (*SetDirection)(void *handle, uint8_t deviceId, motor_direction_t direction);
+ /// Function pointer to CmdGoToDir
+ void (*CmdGoToDir)(void *handle, uint8_t deviceId, motor_direction_t direction, int32_t targetPosition);
+ /// Function pointer to CheckBusyHw
+ uint8_t (*CheckBusyHw)(void *handle);
+ /// Function pointer to CheckStatusHw
+ uint8_t (*CheckStatusHw)(void *handle);
+ /// Function pointer to CmdGoUntil
+ void (*CmdGoUntil)(void *handle, uint8_t deviceId, motor_action_t action, motor_direction_t direction, uint32_t position);
+ /// Function pointer to CmdHardHiZ
+ void (*CmdHardHiZ)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdReleaseSw
+ void (*CmdReleaseSw)(void *handle, uint8_t deviceId, motor_action_t action, motor_direction_t direction);
+ /// Function pointer to CmdResetDevice
+ void (*CmdResetDevice)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdResetPos
+ void (*CmdResetPos)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdRun
+ void (*CmdRun)(void *handle, uint8_t deviceId, motor_direction_t position, uint32_t p);
+ /// Function pointer to CmdSoftHiZ
+ void (*CmdSoftHiZ)(void *handle, uint8_t deviceId);
+ /// Function pointer to CmdStepClock
+ void (*CmdStepClock)(void *handle, uint8_t deviceId, motor_direction_t direction);
+ /// Function pointer to FetchAndClearAllStatus
+ void (*FetchAndClearAllStatus)(void *handle);
+ /// Function pointer to GetFetchedStatus
+ uint16_t (*GetFetchedStatus)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetNbDevices
+ uint8_t (*GetNbDevices)(void *handle);
+ /// Function pointer to IsDeviceBusy
+ bool (*IsDeviceBusy)(void *handle, uint8_t deviceId);
+ /// Function pointer to SendQueuedCommands
+ void (*SendQueuedCommands)(void *handle);
+ /// Function pointer to QueueCommands
+ void (*QueueCommands)(void *handle, uint8_t deviceId, uint8_t p1, int32_t p2);
+ /// Function pointer to WaitForAllDevicesNotBusy
+ void (*WaitForAllDevicesNotBusy)(void *handle);
+ /// Function pointer to ErrorHandler
+ void (*ErrorHandler)(void *handle, uint16_t error);
+ /// Function pointer to BusyInterruptHandler
+ void (*BusyInterruptHandler)(void *handle);
+ /// Function pointer to CmdSoftStop
+ void (*CmdSoftStop)(void *handle, uint8_t deviceId);
+ /// Function pointer to StartStepClock
+ void (*StartStepClock)(void *handle, uint16_t p);
+ /// Function pointer to StopStepClock
+ void (*StopStepClock)(void *handle);
+ /// Function pointer to SetDualFullBridgeConfig
+ void (*SetDualFullBridgeConfig)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetBridgeInputPwmFreq
+ uint32_t (*GetBridgeInputPwmFreq)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetBridgeInputPwmFreq
+ void (*SetBridgeInputPwmFreq)(void *handle, uint8_t deviceId, uint32_t newFreq);
+ /// Function pointer to SetStopMode
+ void (*SetStopMode)(void *handle, uint8_t deviceId, motor_stop_mode_t stopMode);
+ /// Function pointer to GetStopMode
+ motor_stop_mode_t (*GetStopMode)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetDecayMode
+ void (*SetDecayMode)(void *handle, uint8_t deviceId, motor_decay_mode_t decayMode);
+ /// Function pointer to GetDecayMode
+ motor_decay_mode_t (*GetDecayMode)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetStepMode
+ motor_step_mode_t (*GetStepMode)(void *handle, uint8_t deviceId);
+ /// Function pointer to GetDirection
+ motor_direction_t (*GetDirection)(void *handle, uint8_t deviceId);
+ /// Function pointer to ExitDeviceFromReset
+ void (*ExitDeviceFromReset)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetTorque
+ void (*SetTorque)(void *handle, uint8_t deviceId, motor_torque_mode_t torqueMode, uint8_t torqueValue);
+ /// Function pointer to GetTorque
+ uint8_t (*GetTorque)(void *handle, uint8_t deviceId, motor_torque_mode_t torqueMode);
+ /// Function pointer to SetVRefFreq
+ void (*SetRefFreq)(void *handle, uint8_t deviceId, uint32_t refFreq);
+ /// Function pointer to GetVRefFreq
+ uint32_t (*GetRefFreq)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetVRefDc
+ void (*SetRefDc)(void *handle, uint8_t deviceId, uint8_t refDc);
+ /// Function pointer to GetVRefDc
+ uint8_t (*GetRefDc)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetNbDevices
+ bool (*SetNbDevices)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetAnalogValue
+ bool (*SetAnalogValue)(void *handle, uint8_t deviceId, uint32_t param, float value);
+ /// Function pointer to GetAnalogValue
+ float (*GetAnalogValue)(void *handle, uint8_t deviceId, uint32_t param);
+ /// Function pointer to SetTorqueBoostEnable
+ void (*SetTorqueBoostEnable)(void *handle, uint8_t deviceId, bool enable);
+ /// Function pointer to GetTorqueBoostEnable
+ bool (*GetTorqueBoostEnable)(void *handle, uint8_t deviceId);
+ /// Function pointer to SetTorqueBoostThreshold
+ void (*SetTorqueBoostThreshold)(void *handle, uint8_t deviceId, uint16_t speedThreshold);
+ /// Function pointer to GetTorqueBoostThreshold
+ uint16_t (*GetTorqueBoostThreshold)(void *handle, uint8_t deviceId);
+} MOTOR_vt_t;
+
+/**
+
+* @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MOTOR_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Components/STSPIN820/STSPIN820.cpp Fri Apr 27 17:02:08 2018 +0000
@@ -0,0 +1,1799 @@
+/**
+ ******************************************************************************
+ * @file STSPIN820.cpp
+ * @author STM
+ * @version V1.0.0
+ * @date August 7th, 2017
+ * @brief STSPIN820 driver (fully integrated microstepping motor driver)
+ * @note (C) COPYRIGHT 2017 STMicroelectronics
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Generated with STM32CubeTOO -----------------------------------------------*/
+
+
+/* Revision ------------------------------------------------------------------*/
+/*
+ Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev
+ Branch/Trunk/Tag: trunk
+ Based on: X-CUBE-SPN14/trunk/Drivers/BSP/Components/STSPIN820/STSPIN820.c
+ Revision: 0
+*/
+
+
+/* Includes ------------------------------------------------------------------*/
+
+#include "STSPIN820.h"
+
+/* Private function prototypes -----------------------------------------------*/
+
+/** @defgroup STSPIN820_Private_Functions STSPIN820 Private Functions
+ * @{
+ */
+/* Methods -------------------------------------------------------------------*/
+
+/** @defgroup STSPIN820_Exported_Variables STSPIN820 Exported Variables
+ * @{
+ */
+
+/** @defgroup STSPIN820_Library_Functions STSPIN820 Library Functions
+ * @{
+ */
+
+/******************************************************//**
+ * @brief Return motor handle (pointer to the STSPIN820 motor driver structure)
+ * @retval Pointer to the MOTOR_vt_t structure
+ **********************************************************/
+MOTOR_vt_t* STSPIN820::STSPIN820_GetMotorHandle(void)
+{
+ return (&STSPIN820_drv);
+}
+
+/******************************************************//**
+ * @brief Start the STSPIN820 library
+ * @param[in] pInit pointer to the initialization data
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_Init(void* pInit)
+{
+ device_instance++;
+
+ /* Initialise the GPIOs */
+ STSPIN820_Board_GpioInit();
+
+ /* Initialise the timer used for the step clock and ------------------------*/
+ /* the PWM for the reference voltage generation ----------------------------*/
+ STSPIN820_Board_TimStckInit();
+ STSPIN820_Board_PwmRefInit();
+
+ if (pInit == 0)
+ {
+ /* Set all context variables to the predefined values */
+ /* from STSPIN820_config.h */
+ STSPIN820_SetDeviceParamsToPredefinedValues();
+ }
+ else
+ {
+ STSPIN820_SetDeviceParamsToGivenValues((STSPIN820_init_t*) pInit);
+ }
+}
+
+/******************************************************//**
+ * @brief Read id
+ * @retval Id of the STSPIN820 Driver Instance
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_ReadId(void)
+{
+ return (device_instance);
+}
+
+/******************************************************//**
+ * @brief Attach a user callback to the error Handler.
+ * The call back will be then called each time the library
+ * detects an error
+ * @param[in] callback Name of the callback to attach
+ * to the error Hanlder
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_AttachErrorHandler(void (*callback)(uint16_t error))
+{
+ error_handler_callback = (void (*)(uint16_t error))callback;
+}
+
+/******************************************************//**
+ * @brief Attach a user callback to the flag Interrupt
+ * The call back will be then called each time the status
+ * flag pin will be pulled down due to the occurrence of
+ * a programmed alarms ( OCD, thermal pre-warning or
+ * shutdown, UVLO, wrong command, non-performable command)
+ * @param[in] callback Name of the callback to attach
+ * to the Flag Interrupt
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_AttachFlagInterrupt(void (*callback)(void))
+{
+ flag_interrupt_callback = (void (*)(void))callback;
+}
+
+/******************************************************//**
+ * @brief Check if STSPIN820 has a fault by reading EN pin position.
+ * @retval One if STSPIN820 has EN pin down, otherwise zero
+ **********************************************************/
+uint8_t STSPIN820::STSPIN820_CheckStatusHw(void)
+{
+ if(!STSPIN820_Board_EN_AND_FAULT_PIN_GetState())
+ {
+ return 0x01;
+ }
+ else
+ {
+ return 0x00;
+ }
+}
+
+/******************************************************//**
+ * @brief Disable the power bridges (leave the output bridges HiZ)
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_Disable(uint8_t deviceId)
+{
+ STSPIN820_Board_Disable();
+}
+
+/******************************************************//**
+ * @brief Enable the power bridges
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_Enable(uint8_t deviceId)
+{
+ STSPIN820_Board_Enable();
+}
+
+/******************************************************//**
+ * @brief Error handler which calls the user callback (if defined)
+ * @param[in] error Number of the error
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_ErrorHandler(uint16_t error)
+{
+ if (error_handler_callback != 0)
+ {
+ (void) error_handler_callback(error);
+ }
+ else
+ {
+ while(1)
+ {
+ /* Infinite loop */
+ }
+ }
+}
+
+/******************************************************//**
+ * @brief Exit STSPIN820 device from standby (low power consumption)
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_ExitDeviceFromStandby(uint8_t deviceId)
+{
+ uint32_t sequencerPosition = device_prm.sequencerPosition;
+
+ /* Exit standby and set step mode */
+ /* Disable step clock */
+ if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK);
+ }
+
+ /* Let the PWM REF and bridges enabled at least for DISABLE_DELAY time */
+ /* after the last step clock rising edge triggering the last step */
+ STSPIN820_Board_Delay(DISABLE_DELAY);
+ /* Set reference voltage to 0 */
+ STSPIN820_SetTorque(0, CURRENT_TORQUE, 0);
+
+ /* Disable power bridges */
+ STSPIN820_Board_Disable();
+
+ device_prm.commandExecuted = NO_CMD;
+ device_prm.stepsToTake = 0;
+ device_prm.speed = 0;
+
+ /* Reset the microstepping sequencer position */
+ device_prm.sequencerPosition = 0;
+
+ /* Reset current and mark positions */
+ device_prm.currentPosition = 0;
+ device_prm.markPosition = 0;
+
+ STSPIN820_SetStepMode(deviceId, device_prm.stepMode);
+ STSPIN820_Board_ReleaseReset();
+ STSPIN820_Board_Delay(AFTER_STANDBY_EXIT_DEAD_TIME);
+
+ STSPIN820_SetHome(deviceId);
+ STSPIN820_SetMark(deviceId);
+
+ if (device_prm.sequencerPosition != 0)
+ {
+ /* Set direction to FORWARD to ensure the HW sequencer is increased at */
+ /* each step clock rising edge */
+ STSPIN820_SetDirection(0, FORWARD);
+ /* Going out of standby */
+ device_prm.motionState = STANDBYTOINACTIVE;
+ /* Initialize the step clock timer */
+ STSPIN820_Board_TimStckInit();
+ /* Program the step clock */
+ STSPIN820_Board_TimStckCompareInit();
+ STSPIN820_Board_TimStckSetFreq(STSPIN820_MAX_STCK_FREQ);
+ toggle_odd = 0;
+ STSPIN820_Board_TimStckStart();
+ while (device_prm.sequencerPosition != 0);
+ while (toggle_odd!=0);
+ device_prm.sequencerPosition = sequencerPosition;
+ }
+
+ device_prm.motionState = INACTIVE;
+}
+
+/******************************************************//**
+ * @brief Return the acceleration of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval Acceleration in pps^2
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_GetAcceleration(int8_t deviceId)
+{
+ return (device_prm.acceleration);
+}
+
+/******************************************************//**
+ * @brief Return the current speed of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval Speed in pps
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_GetCurrentSpeed(int8_t deviceId)
+{
+ return device_prm.speed;
+}
+
+/******************************************************//**
+ * @brief Return the decay mode of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval Decay Mode State (SLOW or MIXED)
+ **********************************************************/
+motor_decay_mode_t STSPIN820::STSPIN820_GetDecayMode(uint8_t deviceId)
+{
+ motor_decay_mode_t status;
+ if (STSPIN820_Board_GetDecayGpio() != 0)
+ {
+ status = SLOW_DECAY;
+ }
+ else
+ {
+ status = MIXED_DECAY;
+ }
+
+ return status;
+}
+
+/******************************************************//**
+ * @brief Return the deceleration of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval Deceleration in pps^2
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_GetDeceleration(uint8_t deviceId)
+{
+ return (device_prm.deceleration);
+}
+
+/******************************************************//**
+ * @brief Return the device state
+ * @param[in] deviceId Unused parameter
+ * @retval State (ACCELERATING, DECELERATING, STEADY or INACTIVE)
+ **********************************************************/
+motor_state_t STSPIN820::STSPIN820_GetDeviceState(uint8_t deviceId)
+{
+ return device_prm.motionState;
+}
+
+/******************************************************//**
+ * @brief Get the motor current direction
+ * @param[in] deviceId Unused parameter
+ * @retval direction
+ **********************************************************/
+motor_direction_t STSPIN820::STSPIN820_GetDirection(uint8_t deviceId)
+{
+ return device_prm.direction;
+}
+
+/******************************************************//**
+ * @brief Return the FW version of the library
+ * @retval Stspin220_FW_VERSION
+ **********************************************************/
+uint32_t STSPIN820::STSPIN820_GetFwVersion(void)
+{
+ return (STSPIN820_FW_VERSION);
+}
+
+/******************************************************//**
+ * @brief Return the mark position of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval mark position value
+ **********************************************************/
+int32_t STSPIN820::STSPIN820_GetMark(uint8_t deviceId)
+{
+ return device_prm.markPosition;
+}
+
+/******************************************************//**
+ * @brief Return the max speed of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval maxSpeed in pps
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_GetMaxSpeed(uint8_t deviceId)
+{
+ return (device_prm.maxSpeed);
+}
+
+/******************************************************//**
+ * @brief Return the min speed of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval minSpeed in pps
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_GetMinSpeed(uint8_t deviceId)
+{
+ return (device_prm.minSpeed);
+}
+
+/******************************************************//**
+ * @brief Returns the number of devices
+ * @retval number of devices
+ **********************************************************/
+uint8_t STSPIN820::STSPIN820_GetNbDevices(void)
+{
+ return (number_of_devices);
+}
+
+/******************************************************//**
+ * @brief Return the current position value of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval current position value
+ **********************************************************/
+int32_t STSPIN820::STSPIN820_GetPosition(uint8_t deviceId)
+{
+ return device_prm.currentPosition;
+}
+
+/******************************************************//**
+ * @brief Get the motor step mode
+ * @param[in] deviceId Unused parameter
+ * @retval step mode
+ **********************************************************/
+motor_step_mode_t STSPIN820::STSPIN820_GetStepMode(uint8_t deviceId)
+{
+ return device_prm.stepMode;
+}
+
+/******************************************************//**
+ * @brief Get the selected stop mode
+ * @param[in] deviceId Unused parameter
+ * @retval the selected stop mode
+ **********************************************************/
+motor_stop_mode_t STSPIN820::STSPIN820_GetStopMode(uint8_t deviceId)
+{
+ return device_prm.stopMode;
+}
+
+/******************************************************//**
+ * @brief Get the torque of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] torqueMode torque mode
+ * @retval the torqueValue in % (from 0 to 100)
+ * @note
+ **********************************************************/
+uint8_t STSPIN820::STSPIN820_GetTorque(uint8_t deviceId, motor_torque_mode_t torqueMode)
+{
+ uint8_t torqueValue = 0;
+ switch(torqueMode)
+ {
+ case ACC_TORQUE:
+ torqueValue = device_prm.accelTorque;
+ break;
+ case DEC_TORQUE:
+ torqueValue = device_prm.decelTorque;
+ break;
+ case RUN_TORQUE:
+ torqueValue = device_prm.runTorque;
+ break;
+ case HOLD_TORQUE:
+ torqueValue = device_prm.holdTorque;
+ break;
+ case CURRENT_TORQUE:
+ torqueValue = device_prm.currentTorque;
+ break;
+ default:
+ break;
+ }
+ return torqueValue;
+}
+
+/******************************************************//**
+ * @brief Get the torque boost feature status
+ * @param[in] deviceId Unused parameter
+ * @retval TRUE if enabled, FALSE if disabled
+ **********************************************************/
+bool STSPIN820::STSPIN820_GetTorqueBoostEnable(uint8_t deviceId)
+{
+ return device_prm.torqueBoostEnable;
+}
+
+/******************************************************//**
+ * @brief Get the torque boost threshold
+ * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES - 1)
+ * @retval the torque boost threshold above which the step mode is
+ * changed to full step
+ **********************************************************/
+uint16_t STSPIN820::STSPIN820_GetTorqueBoostThreshold(uint8_t deviceId)
+{
+ return device_prm.torqueBoostSpeedThreshold;
+}
+
+/******************************************************//**
+ * @brief Request the motor to move to the home position (ABS_POSITION = 0)
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_GoHome(uint8_t deviceId)
+{
+ deviceId = 0;
+
+ STSPIN820_GoTo(deviceId, 0);
+}
+
+/******************************************************//**
+ * @brief Request the motor to move to the mark position
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_GoMark(uint8_t deviceId)
+{
+ deviceId = 0;
+
+ STSPIN820_GoTo(deviceId, device_prm.markPosition);
+}
+
+/******************************************************//**
+ * @brief Request the motor to move to the specified position
+ * @param[in] deviceId Unused parameter
+ * @param[in] targetPosition absolute position in steps
+ * @retval None
+ * @note The position is at the resolution corresponding to the
+ * selected step mode.
+ * STEP_MODE_FULL : Full step
+ * STEP_MODE_HALF : 1/2 step
+ * STEP_MODE_1_4 : 1/4 step
+ * STEP_MODE_1_8 : 1/8 step
+ * STEP_MODE_1_16 : 1/16 step
+ * STEP_MODE_1_32 : 1/32 step
+ * STEP_MODE_1_128 : 1/128 step
+ * STEP_MODE_1_256 : 1/256 step
+ * @note The 1/64 step mode is not allowed
+ **********************************************************/
+void STSPIN820::STSPIN820_GoTo(uint8_t deviceId, int32_t targetPosition)
+{
+ motor_direction_t direction;
+ deviceId = 0;
+
+ /* Exit from standby if needed */
+ if (device_prm.motionState == STANDBY)
+ {
+ STSPIN820_ExitDeviceFromStandby(deviceId);
+ }
+ /* Deactivate motor if needed */
+ else if (device_prm.motionState != INACTIVE)
+ {
+ STSPIN820_HardHiZ(deviceId);
+ }
+
+ if (targetPosition > device_prm.currentPosition)
+ {
+ device_prm.stepsToTake = targetPosition -\
+ device_prm.currentPosition;
+ if (device_prm.stepsToTake < (STSPIN820_POSITION_RANGE>>1))
+ {
+ direction = FORWARD;
+ }
+ else
+ {
+ direction = BACKWARD;
+ device_prm.stepsToTake = STSPIN820_POSITION_RANGE -\
+ device_prm.stepsToTake;
+ }
+ }
+ else
+ {
+ device_prm.stepsToTake = device_prm.currentPosition -\
+ targetPosition;
+ if (device_prm.stepsToTake < (STSPIN820_POSITION_RANGE>>1))
+ {
+ direction = BACKWARD;
+ }
+ else
+ {
+ direction = FORWARD;
+ device_prm.stepsToTake = STSPIN820_POSITION_RANGE -\
+ device_prm.stepsToTake;
+ }
+ }
+
+ if (device_prm.stepsToTake != 0)
+ {
+ device_prm.commandExecuted = MOVE_CMD;
+
+ /* Direction setup */
+ STSPIN820_SetDirection(deviceId, direction);
+
+ STSPIN820_ComputeSpeedProfile(deviceId, device_prm.stepsToTake);
+
+ /* Motor activation */
+ STSPIN820_StartMovement(deviceId);
+ }
+}
+
+/******************************************************//**
+ * @brief Move the motor to the absolute position
+ * @param[in] deviceId Unused parameter
+ * @param[in] direction FORWARD or BACKWARD
+ * @param[in] targetPosition 32 bit signed value position
+ * @retval None
+ * @note The position is at the resolution corresponding to the
+ * selected step mode.
+ * STEP_MODE_FULL : step
+ * STEP_MODE_HALF : 1/2 step
+ * STEP_MODE_1_4 : 1/4 step
+ * STEP_MODE_1_8 : 1/8 step
+ * STEP_MODE_1_16 : 1/16 step
+ * STEP_MODE_1_32 : 1/32 step
+ * STEP_MODE_1_128 : 1/128 step
+ * STEP_MODE_1_256 : 1/256 step
+ * @note The 1/64 step mode is not allowed
+ **********************************************************/
+void STSPIN820::STSPIN820_GoToDir(uint8_t deviceId, motor_direction_t direction, int32_t targetPosition)
+{
+ deviceId = 0;
+
+ /* Exit from standby if needed */
+ if (device_prm.motionState == STANDBY)
+ {
+ STSPIN820_ExitDeviceFromStandby(deviceId);
+ }
+ /* Deactivate motor if needed */
+ else if (device_prm.motionState != INACTIVE)
+ {
+ STSPIN820_HardHiZ(deviceId);
+ }
+
+ if (direction != BACKWARD)
+ {
+ if (targetPosition > device_prm.currentPosition)
+ {
+ device_prm.stepsToTake = targetPosition -\
+ device_prm.currentPosition;
+ }
+ else
+ {
+ device_prm.stepsToTake = STSPIN820_POSITION_RANGE +\
+ (targetPosition -\
+ device_prm.currentPosition);
+ }
+ }
+ else
+ {
+ if (targetPosition > device_prm.currentPosition)
+ {
+ device_prm.stepsToTake = STSPIN820_POSITION_RANGE +\
+ (device_prm.currentPosition -\
+ targetPosition);
+ }
+ else
+ {
+ device_prm.stepsToTake = device_prm.currentPosition -\
+ targetPosition;
+ }
+ }
+
+ if (device_prm.stepsToTake != 0)
+ {
+ device_prm.commandExecuted = MOVE_CMD;
+
+ /* Direction setup */
+ STSPIN820_SetDirection(deviceId, direction);
+
+ STSPIN820_ComputeSpeedProfile(deviceId, device_prm.stepsToTake);
+
+ /* Motor activation */
+ STSPIN820_StartMovement(deviceId);
+ }
+}
+
+/******************************************************//**
+ * @brief Immediatly stop the motor and disable the power bridge
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_HardHiZ(uint8_t deviceId)
+{
+ /* Set inactive state */
+ device_prm.motionState = INACTIVE;
+
+ /* Disable step clock */
+ if (STSPIN820_Board_TimStckStop(handle &toggle_odd) == 0)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK);
+ }
+
+ /* Let the PWM REF and bridges enabled at least for DISABLE_DELAY time */
+ /* after the last step clock rising edge triggering the last step */
+ STSPIN820_Board_Delay(DISABLE_DELAY);
+
+ /* Set reference voltage to 0 */
+ STSPIN820_SetTorque(0, CURRENT_TORQUE, 0);
+
+ /* Disable power bridges */
+ STSPIN820_Board_Disable();
+
+ /* Comeback to nominal step mode */
+ if (device_prm.stepModeLatched != device_prm.stepMode)
+ {
+ motor_step_mode_t StepMode = device_prm.stepModeLatched;
+ STSPIN820_SetStepMode(0, StepMode);
+ device_prm.stepMode = device_prm.stepModeLatched;
+ }
+
+ device_prm.commandExecuted = NO_CMD;
+ device_prm.stepsToTake = 0;
+ device_prm.speed = 0;
+}
+
+/******************************************************//**
+ * @brief Immediatly stop the motor
+ * and either set holding torque when stop mode is HOLD_MODE,
+ * or call STSPIN820_HardHiz function when stop mode is HIZ_MODE,
+ * or call STSPIN820_PutDeviceInStandby function when stop mode is STANDBY_MODE
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_HardStop(uint8_t deviceId)
+{
+ deviceId = 0;
+
+ if (device_prm.stopMode == HOLD_MODE)
+ {
+ /* Set inactive state */
+ device_prm.motionState = INACTIVE;
+
+ /* Disable step clock */
+ if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK);
+ }
+
+ /* Set holding torque */
+ STSPIN820_ApplyTorque(deviceId, HOLD_TORQUE);
+
+ /* Comeback to nominal step mode */
+ if (device_prm.stepModeLatched != device_prm.stepMode)
+ {
+ motor_step_mode_t StepMode = device_prm.stepModeLatched;
+ STSPIN820_SetStepMode(0, StepMode);
+ device_prm.stepMode = device_prm.stepModeLatched;
+ }
+
+ device_prm.commandExecuted = NO_CMD;
+ device_prm.stepsToTake = 0;
+ device_prm.speed = 0;
+ }
+ else if (device_prm.stopMode == HIZ_MODE)
+ {
+ STSPIN820_HardHiZ(deviceId);
+ }
+ else if (device_prm.stopMode == STANDBY_MODE)
+ {
+ STSPIN820_PutDeviceInStandby(deviceId);
+ }
+}
+
+/******************************************************//**
+ * @brief Moves the motor of the specified number of steps
+ * @param[in] deviceId Unused parameter
+ * @param[in] direction FORWARD or BACKWARD
+ * @param[in] stepCount Number of steps to perform
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_Move(uint8_t deviceId, motor_direction_t direction, uint32_t stepCount)
+{
+ deviceId = 0;
+
+ /* Exit from standby if needed */
+ if (device_prm.motionState == STANDBY)
+ {
+ STSPIN820_ExitDeviceFromStandby(deviceId);
+ }
+ /* Deactivate motor if needed */
+ else if (device_prm.motionState != INACTIVE)
+ {
+ STSPIN820_HardHiZ(deviceId);
+ }
+
+ if (stepCount != 0)
+ {
+ device_prm.stepsToTake = stepCount;
+ device_prm.commandExecuted = MOVE_CMD;
+
+ /* Direction setup */
+ STSPIN820_SetDirection(deviceId, direction);
+
+ STSPIN820_ComputeSpeedProfile(deviceId, stepCount);
+
+ /* Motor activation */
+ STSPIN820_StartMovement(deviceId);
+ }
+}
+
+/******************************************************//**
+ * @brief Put STSPIN820 device in standby (low power consumption)
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_PutDeviceInStandby(uint8_t deviceId)
+{
+ /* Stop movement */
+ STSPIN820_HardHiZ(deviceId);
+
+ /* Enter standby */
+ STSPIN820_Board_Reset();
+
+ device_prm.motionState = STANDBY;
+}
+
+/******************************************************//**
+ * @brief Runs the motor. It will accelerate from the min
+ * speed up to the max speed by using the device acceleration.
+ * @param[in] deviceId Unused parameter
+ * @param[in] direction FORWARD or BACKWARD
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_Run(uint8_t deviceId, motor_direction_t direction)
+{
+ /* Exit from standby if needed */
+ if (device_prm.motionState == STANDBY)
+ {
+ STSPIN820_ExitDeviceFromStandby(deviceId);
+ }
+ /* Deactivate motor if needed */
+ else if (device_prm.motionState != INACTIVE)
+ {
+ STSPIN820_HardHiZ(deviceId);
+ }
+
+ /* Direction setup */
+ STSPIN820_SetDirection(deviceId,direction);
+ device_prm.commandExecuted = RUN_CMD;
+ /* Motor activation */
+ STSPIN820_StartMovement(deviceId);
+}
+
+/******************************************************//**
+ * @brief Changes the acceleration of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] newAcc New acceleration to apply in pps^2
+ * @retval true if the command is successfully executed, else false
+ * @note The command is not performed if the device is executing
+ * a MOVE or GOTO command (but it can be used during a RUN command)
+ **********************************************************/
+bool STSPIN820::STSPIN820_SetAcceleration(uint8_t deviceId, uint16_t newAcc)
+{
+ bool cmdExecuted = FALSE;
+ if ((newAcc != 0)&&
+ (((device_prm.motionState & INACTIVE) == INACTIVE)||
+ (device_prm.commandExecuted == RUN_CMD)))
+ {
+ device_prm.acceleration = newAcc;
+ cmdExecuted = TRUE;
+ }
+ return cmdExecuted;
+}
+
+/******************************************************//**
+ * @brief Specifies the decay mode
+ * @param[in] deviceId Unused parameter
+ * @param[in] decay SLOW_DECAY or MIXED_DECAY
+ * @retval true if the command is successfully executed, else false
+ **********************************************************/
+void STSPIN820::STSPIN820_SetDecayMode(uint8_t deviceId, motor_decay_mode_t decay)
+{
+ if (decay == SLOW_DECAY)
+ {
+ STSPIN820_Board_SetDecayGpio(1);
+ }
+ else if (decay == MIXED_DECAY)
+ {
+ STSPIN820_Board_SetDecayGpio(0);
+ }
+}
+
+/******************************************************//**
+ * @brief Changes the deceleration of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] newDec New deceleration to apply in pps^2
+ * @retval true if the command is successfully executed, else false
+ * @note The command is not performed if the device is executing
+ * a MOVE or GOTO command (but it can be used during a RUN command)
+ **********************************************************/
+bool STSPIN820::STSPIN820_SetDeceleration(uint8_t deviceId, uint16_t newDec)
+{
+ bool cmdExecuted = FALSE;
+ if ((newDec != 0)&&
+ (((device_prm.motionState & INACTIVE) == INACTIVE)||
+ (device_prm.commandExecuted == RUN_CMD)))
+ {
+ device_prm.deceleration = newDec;
+ cmdExecuted = TRUE;
+ }
+ return cmdExecuted;
+}
+
+/******************************************************//**
+ * @brief Specifies the direction
+ * @param[in] deviceId Unused parameter
+ * @param[in] dir FORWARD or BACKWARD
+ * @note The direction change is applied if the device
+ * is in INACTIVE or STANDBY state or if the device is
+ * executing a run command
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetDirection(uint8_t deviceId, motor_direction_t dir)
+{
+ if ((device_prm.motionState == INACTIVE)||\
+ (device_prm.motionState == STANDBY))
+ {
+ device_prm.direction = dir;
+ STSPIN820_Board_SetDirectionGpio(dir);
+ }
+ else if ((device_prm.commandExecuted&RUN_CMD)!=0)
+ {
+ device_prm.commandExecuted|=STSPIN820_DIR_CHANGE_BIT_MASK;
+ }
+}
+
+/******************************************************//**
+ * @brief Set current position to be the Home position
+ * (current position set to 0)
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetHome(uint8_t deviceId)
+{
+ device_prm.currentPosition = 0;
+}
+
+/******************************************************//**
+ * @brief Set current position to be the Mark position
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetMark(uint8_t deviceId)
+{
+ device_prm.markPosition = device_prm.currentPosition;
+}
+
+/******************************************************//**
+ * @brief Changes the max speed of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] newMaxSpeed New max speed to apply in pps
+ * @retval true if the command is successfully executed, else false
+ * @note The command is not performed is the device is executing
+ * a MOVE or GOTO command (but it can be used during a RUN command).
+ **********************************************************/
+bool STSPIN820::STSPIN820_SetMaxSpeed(uint8_t deviceId, uint16_t newMaxSpeed)
+{
+ bool cmdExecuted = FALSE;
+ if ((newMaxSpeed >= STSPIN820_MIN_STCK_FREQ)&&\
+ ((newMaxSpeed <= STSPIN820_MAX_STCK_FREQ)||\
+ ((device_prm.torqueBoostEnable != FALSE)&&\
+ ((newMaxSpeed>>STSPIN820_GetStepMode(deviceId))<= STSPIN820_MAX_STCK_FREQ)))&&\
+ (device_prm.minSpeed <= newMaxSpeed) &&\
+ (((device_prm.motionState & INACTIVE) == INACTIVE)||\
+ (device_prm.commandExecuted == RUN_CMD)))
+ {
+ device_prm.maxSpeed = newMaxSpeed;
+ cmdExecuted = TRUE;
+ }
+ return cmdExecuted;
+}
+
+/******************************************************//**
+ * @brief Changes the min speed of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] newMinSpeed New min speed to apply in pps
+ * @retval true if the command is successfully executed, else false
+ * @note The command is not performed is the device is executing
+ * a MOVE or GOTO command (but it can be used during a RUN command).
+ **********************************************************/
+bool STSPIN820::STSPIN820_SetMinSpeed(uint8_t deviceId, uint16_t newMinSpeed)
+{
+ bool cmdExecuted = FALSE;
+ if ((newMinSpeed >= STSPIN820_MIN_STCK_FREQ)&&
+ (newMinSpeed <= STSPIN820_MAX_STCK_FREQ) &&
+ (newMinSpeed <= device_prm.maxSpeed) &&
+ (((device_prm.motionState & INACTIVE) == INACTIVE)||
+ (device_prm.commandExecuted == RUN_CMD)))
+ {
+ device_prm.minSpeed = newMinSpeed;
+ cmdExecuted = TRUE;
+ }
+ return cmdExecuted;
+}
+
+/******************************************************//**
+ * @brief Sets the number of devices to be used
+ * @param[in] nbDevices (from 1 to MAX_NUMBER_OF_DEVICES)
+ * @retval TRUE if successfull, FALSE if failure, attempt to set a number of
+ * devices greater than MAX_NUMBER_OF_DEVICES
+ **********************************************************/
+bool STSPIN820::STSPIN820_SetNbDevices(uint8_t nbDevices)
+{
+ if (nbDevices <= MAX_NUMBER_OF_DEVICES)
+ {
+ number_of_devices = nbDevices;
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+/******************************************************//**
+ * @brief Set the stepping mode
+ * @param[in] deviceId Unused parameter
+ * @param[in] stepMode from full step to 1/256 microstep
+ * as specified in enum motor_step_mode_t
+ * 1/64 microstep mode not supported by STSPIN820
+ * @retval true if the command is successfully executed, else false
+ **********************************************************/
+bool STSPIN820::STSPIN820_SetStepMode(uint8_t deviceId, motor_step_mode_t stepMode)
+{
+ /* Store step mode */
+ device_prm.stepMode = stepMode;
+ device_prm.stepModeLatched = stepMode;
+
+ /* Set the mode pins to the levels corresponding to the selected step mode */
+ switch (stepMode)
+ {
+ case STEP_MODE_FULL:
+ STSPIN820_Board_SetFullStep();
+ break;
+ case STEP_MODE_HALF:
+ STSPIN820_Board_SetModePins(1, 0, 0);
+ break;
+ case STEP_MODE_1_4:
+ STSPIN820_Board_SetModePins(0, 1, 0);
+ break;
+ case STEP_MODE_1_8:
+ STSPIN820_Board_SetModePins(1, 1, 0);
+ break;
+ case STEP_MODE_1_16:
+ STSPIN820_Board_SetModePins(0, 0, 1);
+ break;
+ case STEP_MODE_1_32:
+ STSPIN820_Board_SetModePins(1, 0, 1);
+ break;
+ case STEP_MODE_1_128:
+ STSPIN820_Board_SetModePins(0, 1, 1);
+ break;
+ case STEP_MODE_1_256:
+ STSPIN820_Board_SetModePins(1, 1, 1);
+ break;
+ default:
+ return FALSE;
+ }
+
+ return TRUE;
+
+}
+
+/******************************************************//**
+ * @brief Select the mode to stop the motor.
+ * @param[in] deviceId Unused parameter
+ * @param[in] stopMode HOLD_MODE to let power bridge enabled
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetStopMode(uint8_t deviceId, motor_stop_mode_t stopMode)
+{
+ device_prm.stopMode = stopMode;
+}
+
+/******************************************************//**
+ * @brief Set the torque of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] torqueMode Torque mode as specified in enum motor_torque_mode_t
+ * @param[in] torqueValue in % (from 0 to 100)
+ * @retval None
+ * @note
+ **********************************************************/
+void STSPIN820::STSPIN820_SetTorque(uint8_t deviceId, motor_torque_mode_t torqueMode, uint8_t torqueValue)
+{
+ device_prm.updateTorque = TRUE;
+ if (torqueValue>100)
+ {
+ torqueValue = 100;
+ }
+ switch(torqueMode)
+ {
+ case ACC_TORQUE:
+ device_prm.accelTorque = torqueValue;
+ break;
+ case DEC_TORQUE:
+ device_prm.decelTorque = torqueValue;
+ break;
+ case RUN_TORQUE:
+ device_prm.runTorque = torqueValue;
+ break;
+ case HOLD_TORQUE:
+ device_prm.holdTorque = torqueValue;
+ if (device_prm.motionState != INACTIVE)
+ {
+ break;
+ }
+ case CURRENT_TORQUE:
+ device_prm.currentTorque = torqueValue;
+ STSPIN820_Board_PwmRefSetFreqAndDutyCycle(device_prm.refPwmFreq,torqueValue);
+ default:
+ device_prm.updateTorque = FALSE;
+ break; //ignore error
+ }
+}
+
+/******************************************************//**
+ * @brief Enable or disable the torque boost feature
+ * @param[in] deviceId Unused parameter
+ * @param[in] enable true to enable torque boost, false to disable
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetTorqueBoostEnable(uint8_t deviceId, bool enable)
+{
+ device_prm.torqueBoostEnable = enable;
+}
+
+/******************************************************//**
+ * @brief Set the torque boost threshold
+ * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES - 1)
+ * @param[in] speedThreshold speed threshold above which the step mode is
+ * changed to full step
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetTorqueBoostThreshold(uint8_t deviceId, uint16_t speedThreshold)
+{
+ device_prm.torqueBoostSpeedThreshold = speedThreshold;
+}
+
+/******************************************************//**
+ * @brief Stops the motor by using the device deceleration
+ * @param[in] deviceId Unused parameter
+ * @retval true if the command is successfully executed, else false
+ * @note The command is not performed if the device is in INACTIVE,
+ * STANDBYTOINACTIVE or STANDBY state.
+ **********************************************************/
+bool STSPIN820::STSPIN820_SoftStop(uint8_t deviceId)
+{
+ bool cmdExecuted = FALSE;
+ if ((device_prm.motionState & INACTIVE) != INACTIVE)
+ {
+ device_prm.commandExecuted |= STSPIN820_SOFT_STOP_BIT_MASK;
+ cmdExecuted = TRUE;
+ }
+ return (cmdExecuted);
+}
+
+/******************************************************//**
+ * @brief Get the frequency of REF PWM of the specified device
+ * @param[in] deviceId Unused parameter
+ * @retval the frequency of REF PWM in Hz
+ * @note
+ **********************************************************/
+uint32_t STSPIN820::STSPIN820_VrefPwmGetFreq(uint8_t deviceId)
+{
+ return device_prm.refPwmFreq;
+}
+
+/******************************************************//**
+ * @brief Set the frequency of REF PWM of the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] newFreq in Hz
+ * @retval None
+ * @note
+ **********************************************************/
+void STSPIN820::STSPIN820_VrefPwmSetFreq(uint8_t deviceId, uint32_t newFreq)
+{
+ device_prm.refPwmFreq = newFreq;
+ STSPIN820_Board_PwmRefSetFreqAndDutyCycle(newFreq,device_prm.currentTorque);
+}
+
+/******************************************************//**
+ * @brief Locks until the device state becomes Inactive
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_WaitWhileActive(uint8_t deviceId)
+{
+ /* Wait while motor is running */
+ while (((STSPIN820_GetDeviceState(deviceId)&INACTIVE)!=INACTIVE)||\
+ (((STSPIN820_GetDeviceState(deviceId)&INACTIVE)==INACTIVE)&&(toggle_odd!=0)));
+}
+
+/******************************************************//**
+ * @brief Updates the current speed of the device
+ * @param[in] deviceId Unused parameter
+ * @param[in] newSpeed in pps
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_ApplySpeed(uint8_t deviceId, uint16_t newSpeed)
+{
+ if (device_prm.torqueBoostEnable != FALSE)
+ {
+ if (device_prm.stepMode > STEP_MODE_1_256)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_APPLY_SPEED);
+ }
+ if (device_prm.stepMode != STEP_MODE_FULL)
+ {
+ if (((newSpeed>>device_prm.stepModeLatched)>\
+ device_prm.torqueBoostSpeedThreshold)&&\
+ (((device_prm.commandExecuted & STSPIN820_MOVE_BIT_MASK) != MOVE_CMD) ||\
+ ((device_prm.stepsToTake-device_prm.relativePos)>=\
+ (1<<device_prm.stepModeLatched))))
+ {
+ if ((device_prm.sequencerPosition & 0xFF) == 0x80)
+ {
+ STSPIN820_Board_SetFullStep();
+ device_prm.stepMode = STEP_MODE_FULL;
+ device_prm.accu >>= device_prm.stepModeLatched;
+ newSpeed >>= device_prm.stepModeLatched;
+ }
+ }
+ }
+ else if (((newSpeed <= device_prm.torqueBoostSpeedThreshold) &&\
+ (device_prm.stepModeLatched != STEP_MODE_FULL))||\
+ (((device_prm.commandExecuted & STSPIN820_MOVE_BIT_MASK) == MOVE_CMD)&&\
+ ((device_prm.stepsToTake-device_prm.relativePos)<=\
+ (1<<device_prm.stepModeLatched))))
+ {
+ STSPIN820_SetStepMode(0, device_prm.stepModeLatched);
+ device_prm.stepMode = device_prm.stepModeLatched;
+ device_prm.accu <<= device_prm.stepModeLatched;
+ newSpeed <<= device_prm.stepModeLatched;
+ }
+ }
+ else if (device_prm.stepMode != device_prm.stepModeLatched)
+ {
+ //torqueBoostEnable has just been disabled
+ STSPIN820_SetStepMode(0, device_prm.stepModeLatched);
+ device_prm.stepMode = device_prm.stepModeLatched;
+ device_prm.accu <<= device_prm.stepModeLatched;
+ newSpeed <<= device_prm.stepModeLatched;
+ }
+
+ if (newSpeed < STSPIN820_MIN_STCK_FREQ)
+ {
+ newSpeed = STSPIN820_MIN_STCK_FREQ;
+ }
+ if (newSpeed > STSPIN820_MAX_STCK_FREQ)
+ {
+ newSpeed = STSPIN820_MAX_STCK_FREQ;
+ }
+
+ device_prm.speed = newSpeed;
+ STSPIN820_Board_TimStckSetFreq(newSpeed);
+
+}
+
+/******************************************************//**
+ * @brief Apply the set torque to the specified device
+ * @param[in] deviceId Unused parameter
+ * @param[in] torqueMode torque mode
+ * @retval None
+ * @note
+ **********************************************************/
+void STSPIN820::STSPIN820_ApplyTorque(uint8_t deviceId, motor_torque_mode_t torqueMode)
+{
+ uint8_t torqueValue = 0;
+ device_prm.updateTorque = FALSE;
+ switch(torqueMode)
+ {
+ case ACC_TORQUE:
+ device_prm.currentTorque = device_prm.accelTorque;
+ break;
+ case DEC_TORQUE:
+ device_prm.currentTorque = device_prm.decelTorque;
+ break;
+ case RUN_TORQUE:
+ device_prm.currentTorque = device_prm.runTorque;
+ break;
+ case HOLD_TORQUE:
+ device_prm.currentTorque = device_prm.holdTorque;
+ break;
+ case CURRENT_TORQUE:
+ break;
+ default:
+ return; //ignore error
+ }
+ torqueValue = device_prm.currentTorque;
+ STSPIN820_Board_PwmRefSetFreqAndDutyCycle(device_prm.refPwmFreq,torqueValue);
+}
+
+/******************************************************//**
+ * @brief Computes the speed profile according to the number of steps to move
+ * @param[in] deviceId Unused parameter
+ * @param[in] nbSteps number of steps to perform
+ * @retval None
+ * @note Using the acceleration and deceleration of the device,
+ * this function determines the duration in steps of the acceleration,
+ * steady and deceleration phases.
+ * If the total number of steps to perform is big enough, a trapezoidal move
+ * is performed (i.e. there is a steady phase where the motor runs at the maximum
+ * speed.
+ * Else, a triangular move is performed (no steady phase: the maximum speed is never
+ * reached.
+ **********************************************************/
+void STSPIN820::STSPIN820_ComputeSpeedProfile(uint8_t deviceId, uint32_t nbSteps)
+{
+ uint32_t reqAccSteps;
+ uint32_t reqDecSteps;
+
+ /* compute the number of steps to get the targeted speed */
+ uint16_t minSpeed = device_prm.minSpeed;
+ reqAccSteps = (device_prm.maxSpeed - minSpeed);
+ reqAccSteps *= (device_prm.maxSpeed + minSpeed);
+ reqDecSteps = reqAccSteps;
+ reqAccSteps /= (uint32_t)device_prm.acceleration;
+ reqAccSteps /= 2;
+
+ /* compute the number of steps to stop */
+ reqDecSteps /= (uint32_t)device_prm.deceleration;
+ reqDecSteps /= 2;
+
+ if(( reqAccSteps + reqDecSteps ) > nbSteps)
+ {
+ /* Triangular move */
+ /* reqDecSteps = (Pos * Dec) /(Dec+Acc) */
+ uint32_t dec = device_prm.deceleration;
+ uint32_t acc = device_prm.acceleration;
+
+ reqDecSteps = ((uint32_t) dec * nbSteps) / (acc + dec);
+ if (reqDecSteps > 1)
+ {
+ reqAccSteps = reqDecSteps - 1;
+ if(reqAccSteps == 0)
+ {
+ reqAccSteps = 1;
+ }
+ }
+ else
+ {
+ reqAccSteps = 0;
+ }
+ device_prm.endAccPos = reqAccSteps;
+ device_prm.startDecPos = reqDecSteps;
+ }
+ else
+ {
+ /* Trapezoidal move */
+ /* accelerating phase to endAccPos */
+ /* steady phase from endAccPos to startDecPos */
+ /* decelerating from startDecPos to stepsToTake*/
+ device_prm.endAccPos = reqAccSteps;
+ device_prm.startDecPos = nbSteps - reqDecSteps - 1;
+ }
+}
+
+/******************************************************//**
+ * @brief Handlers of the flag interrupt which calls the user callback (if defined)
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_FlagInterruptHandler(void)
+{
+ if (flag_interrupt_callback != 0)
+ {
+ flag_interrupt_callback();
+ }
+}
+
+/******************************************************//**
+ * @brief Set the parameters of the device whose values are not defined in
+ * STSPIN820_config.h
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetDeviceParamsOtherValues(void)
+{
+ uint16_t tmp;
+
+ device_prm.accu = 0;
+ device_prm.currentPosition = 0;
+ device_prm.sequencerPosition = 0;
+ device_prm.endAccPos = 0;
+ device_prm.relativePos = 0;
+ device_prm.startDecPos = 0;
+ device_prm.stepsToTake = 0;
+ device_prm.updateTorque = FALSE;
+ device_prm.speed = 0;
+ device_prm.commandExecuted = NO_CMD;
+ device_prm.direction = FORWARD;
+ tmp = device_prm.minSpeed;
+ if (((device_prm.torqueBoostEnable != FALSE)&&\
+ (device_prm.torqueBoostSpeedThreshold>STSPIN820_MAX_STCK_FREQ))||\
+ (tmp>device_prm.maxSpeed))
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_INIT);
+ }
+}
+
+/******************************************************//**
+ * @brief Set the parameters of the device to values of initDevicePrm structure
+ * @param pInitDevicePrm structure containing values to initialize the device
+ * parameters
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetDeviceParamsToGivenValues(STSPIN820_init_t* pInitDevicePrm)
+{
+ device_prm.motionState = STANDBY;;
+
+ if (STSPIN820_SetAcceleration(0,pInitDevicePrm->acceleration)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_ACCELERATION);
+ }
+ if (STSPIN820_SetDeceleration(0,pInitDevicePrm->deceleration)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_DECELERATION);
+ }
+ if (STSPIN820_SetMaxSpeed(0,pInitDevicePrm->maxSpeed)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_MAX_SPEED);
+ }
+ if (STSPIN820_SetMinSpeed(0,pInitDevicePrm->minSpeed)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_MIN_SPEED);
+ }
+
+ STSPIN820_VrefPwmSetFreq(0,pInitDevicePrm->vrefPwmFreq);
+ STSPIN820_SetTorque(0,ACC_TORQUE,pInitDevicePrm->accelTorque);
+ STSPIN820_SetTorque(0,DEC_TORQUE,pInitDevicePrm->decelTorque);
+ STSPIN820_SetTorque(0,RUN_TORQUE,pInitDevicePrm->runTorque);
+ STSPIN820_SetTorque(0,HOLD_TORQUE,pInitDevicePrm->holdTorque);
+ device_prm.torqueBoostEnable = pInitDevicePrm->torqueBoostEnable;
+ device_prm.torqueBoostSpeedThreshold = pInitDevicePrm->torqueBoostSpeedThreshold;
+ STSPIN820_SetStopMode(0,pInitDevicePrm->stopMode);
+
+ STSPIN820_SetDeviceParamsOtherValues();
+
+ /* Eventually deactivate motor */
+ if ((device_prm.motionState != INACTIVE)&&\
+ (device_prm.motionState != STANDBY))
+ {
+ STSPIN820_HardHiZ(0);
+ }
+
+ /* Enter standby */
+ STSPIN820_Board_Reset();
+
+ /* Reset the microstepping sequencer position */
+ device_prm.sequencerPosition = 0;
+
+ /* Reset current and mark positions */
+ device_prm.currentPosition = 0;
+ device_prm.markPosition = 0;
+
+ /* Set predefined step mode */
+ STSPIN820_SetStepMode(0, pInitDevicePrm->stepMode);
+
+ /* Wait */
+ STSPIN820_Board_Delay(SELECT_STEP_MODE_DELAY);
+
+ /* Exit standby */
+ STSPIN820_Board_ReleaseReset();
+
+ /* Let a delay after reset release*/
+ STSPIN820_Board_Delay(AFTER_STANDBY_EXIT_DEAD_TIME);
+
+}
+
+/******************************************************//**
+ * @brief Sets the parameters of the device to predefined values
+ * from STSPIN820_config.h
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_SetDeviceParamsToPredefinedValues(void)
+{
+ device_prm.motionState = STANDBY;
+
+ if (STSPIN820_SetAcceleration(0,STSPIN820_CONF_PARAM_ACC)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_ACCELERATION);
+ }
+ if (STSPIN820_SetDeceleration(0,STSPIN820_CONF_PARAM_DEC)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_DECELERATION);
+ }
+ if (STSPIN820_SetMaxSpeed(0,STSPIN820_CONF_PARAM_RUNNING_SPEED)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_MAX_SPEED);
+ }
+ if (STSPIN820_SetMinSpeed(0,STSPIN820_CONF_PARAM_MIN_SPEED)==FALSE)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_MIN_SPEED);
+ }
+
+ STSPIN820_VrefPwmSetFreq(0,STSPIN820_CONF_PARAM_REF_PWM_FREQUENCY);
+ STSPIN820_SetTorque(0,ACC_TORQUE,STSPIN820_CONF_PARAM_ACC_TORQUE);
+ STSPIN820_SetTorque(0,DEC_TORQUE,STSPIN820_CONF_PARAM_DEC_TORQUE);
+ STSPIN820_SetTorque(0,RUN_TORQUE,STSPIN820_CONF_PARAM_RUNNING_TORQUE);
+ STSPIN820_SetTorque(0,HOLD_TORQUE,STSPIN820_CONF_PARAM_HOLDING_TORQUE);
+ device_prm.torqueBoostEnable = STSPIN820_CONF_PARAM_TORQUE_BOOST_EN;
+ device_prm.torqueBoostSpeedThreshold = STSPIN820_CONF_PARAM_TORQUE_BOOST_TH;
+ STSPIN820_SetStopMode(0,STSPIN820_CONF_PARAM_AUTO_HIZ_STOP);
+
+ STSPIN820_SetDeviceParamsOtherValues();
+
+ /* Eventually deactivate motor */
+ if ((device_prm.motionState != INACTIVE)&&\
+ (device_prm.motionState != STANDBY))
+ {
+ STSPIN820_HardHiZ(0);
+ }
+
+ /* Enter standby */
+ STSPIN820_Board_Reset();
+
+ /* Reset the microstepping sequencer position */
+ device_prm.sequencerPosition = 0;
+
+ /* Reset current and mark positions */
+ device_prm.currentPosition = 0;
+ device_prm.markPosition = 0;
+
+ /* Set predefined step mode */
+ STSPIN820_SetStepMode(0, STSPIN820_CONF_PARAM_STEP_MODE);
+
+ /* Wait */
+ STSPIN820_Board_Delay(SELECT_STEP_MODE_DELAY);
+
+ /* Exit standby */
+ STSPIN820_Board_ReleaseReset();
+
+ /* Let a delay after reset release*/
+ STSPIN820_Board_Delay(AFTER_STANDBY_EXIT_DEAD_TIME);
+}
+
+/******************************************************//**
+ * @brief Initialises the bridge parameters to start the movement
+ * and enable the power bridge
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ **********************************************************/
+void STSPIN820::STSPIN820_StartMovement(uint8_t deviceId)
+{
+ deviceId = 0;
+
+ /* Enable STSPIN820 powerstage */
+ STSPIN820_Enable(deviceId);
+ toggle_odd = 0;
+ device_prm.accu = 0;
+ device_prm.relativePos = 0;
+ if ((device_prm.endAccPos == 0)&&\
+ (device_prm.commandExecuted != RUN_CMD))
+ {
+ device_prm.motionState = DECELERATING;
+ STSPIN820_ApplyTorque(deviceId, DEC_TORQUE);
+ }
+ else
+ {
+ device_prm.motionState = ACCELERATING;
+ STSPIN820_ApplyTorque(deviceId, ACC_TORQUE);
+ }
+ STSPIN820_Board_PwmRefStart();
+ /* Initialize the step clock timer */
+ STSPIN820_Board_TimStckInit();
+ /* Program the step clock */
+ STSPIN820_Board_TimStckCompareInit();
+ STSPIN820_ApplySpeed(deviceId, device_prm.minSpeed);
+ STSPIN820_Board_TimStckStart();
+}
+
+/******************************************************//**
+ * @brief Handles the device state machine at each pulse
+ * @param[in] deviceId Unused parameter
+ * @retval None
+ * @note Must only be called by the timer ISR
+ **********************************************************/
+void STSPIN820::STSPIN820_StepClockHandler(uint8_t deviceId)
+{
+ uint32_t stepModeShift = device_prm.stepModeLatched - device_prm.stepMode;
+ uint16_t tmp;
+ deviceId = 0;
+
+ if (device_prm.motionState == STANDBYTOINACTIVE)
+ {
+ if (toggle_odd != 0)
+ {
+ toggle_odd = 0;
+ if (device_prm.sequencerPosition == 0)
+ {
+ if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK);
+ }
+ return;
+ }
+ }
+ else
+ {
+ toggle_odd = 1;
+ tmp = (1 << (STEP_MODE_1_256-device_prm.stepMode));
+ device_prm.sequencerPosition -= tmp;
+ }
+ STSPIN820_Board_TimStckSetFreq(STSPIN820_MAX_STCK_FREQ);
+ return;
+ }
+
+ if (toggle_odd == 0)
+ {
+ toggle_odd = 1;
+ }
+ else
+ {
+ toggle_odd = 0;
+ /* Incrementation of the relative position */
+ device_prm.relativePos += (1 << stepModeShift);
+
+ /* Incrementation of the current position */
+ if (device_prm.direction != BACKWARD)
+ {
+ device_prm.currentPosition += (1 << stepModeShift);
+ tmp = (1 << (STEP_MODE_1_256-device_prm.stepMode));
+ device_prm.sequencerPosition += tmp;
+ if (device_prm.sequencerPosition >= (SEQUENCER_MAX_VALUE+1))
+ {
+ device_prm.sequencerPosition -= (SEQUENCER_MAX_VALUE+1);
+ }
+ }
+ else
+ {
+ device_prm.currentPosition -= (1 << stepModeShift);
+ tmp = (1 << (STEP_MODE_1_256-device_prm.stepMode));
+ device_prm.sequencerPosition -= tmp;
+ if (device_prm.sequencerPosition < 0)
+ {
+ device_prm.sequencerPosition += (SEQUENCER_MAX_VALUE+1);
+ }
+ }
+
+ switch (device_prm.motionState)
+ {
+ case ACCELERATING:
+ {
+ uint32_t relPos = device_prm.relativePos;
+ uint32_t endAccPos = device_prm.endAccPos;
+ uint16_t speed = device_prm.speed;
+ uint32_t acc = ((uint32_t)device_prm.acceleration << 16)>>stepModeShift;
+
+ if (((device_prm.commandExecuted&(STSPIN820_SOFT_STOP_BIT_MASK|STSPIN820_DIR_CHANGE_BIT_MASK))!=0)||\
+ ((device_prm.commandExecuted==MOVE_CMD)&&(relPos>=device_prm.startDecPos)))
+ {
+ device_prm.motionState = DECELERATING;
+ device_prm.accu = 0;
+ /* Apply decelerating torque */
+ STSPIN820_ApplyTorque(deviceId, DEC_TORQUE);
+ }
+ else if ((speed>=(device_prm.maxSpeed>>stepModeShift))||\
+ ((device_prm.commandExecuted==MOVE_CMD)&&(relPos >= endAccPos)))
+ {
+ device_prm.motionState = STEADY;
+ /* Apply running torque */
+ STSPIN820_ApplyTorque(deviceId, RUN_TORQUE);
+ }
+ else
+ {
+ bool speedUpdated = FALSE;
+ /* Go on accelerating */
+ if (speed==0)
+ {
+ speed =1;
+ }
+ device_prm.accu += acc / speed;
+ while (device_prm.accu>=(0X10000L))
+ {
+ device_prm.accu -= (0X10000L);
+ speed +=1;
+ speedUpdated = TRUE;
+ }
+
+ if (speedUpdated)
+ {
+ if (speed>(device_prm.maxSpeed>>stepModeShift))
+ {
+ speed = device_prm.maxSpeed>>stepModeShift;
+ }
+ device_prm.speed = speed;
+ }
+
+ if (device_prm.updateTorque!=FALSE)
+ {
+ /* Apply accelerating torque */
+ STSPIN820_ApplyTorque(deviceId, ACC_TORQUE);
+ }
+ }
+ break;
+ }
+ case STEADY:
+ {
+ uint16_t maxSpeed = device_prm.maxSpeed>>stepModeShift;
+ uint32_t relativePos = device_prm.relativePos;
+ if (device_prm.updateTorque!=FALSE)
+ {
+ /* Apply accelerating torque */
+ STSPIN820_ApplyTorque(deviceId, RUN_TORQUE);
+ }
+ if (((device_prm.commandExecuted&(STSPIN820_SOFT_STOP_BIT_MASK|STSPIN820_DIR_CHANGE_BIT_MASK))!=0)||\
+ ((device_prm.commandExecuted==MOVE_CMD)&&\
+ (relativePos>=(device_prm.startDecPos)))||\
+ ((device_prm.commandExecuted==RUN_CMD)&&\
+ (device_prm.speed>maxSpeed)))
+ {
+ device_prm.motionState = DECELERATING;
+ device_prm.accu = 0;
+ /* Apply decelerating torque */
+ STSPIN820_ApplyTorque(deviceId, DEC_TORQUE);
+ }
+ else if ((device_prm.commandExecuted==RUN_CMD)&&(device_prm.speed<maxSpeed))
+ {
+ device_prm.motionState = ACCELERATING;
+ device_prm.accu = 0;
+ /* Apply accelerating torque */
+ STSPIN820_ApplyTorque(deviceId, ACC_TORQUE);
+ }
+ break;
+ }
+ case DECELERATING:
+ {
+ uint32_t relativePos = device_prm.relativePos;
+ uint16_t speed = device_prm.speed;
+ uint32_t dec = ((uint32_t)device_prm.deceleration << 16)>>stepModeShift;
+ if ((((device_prm.commandExecuted&(STSPIN820_SOFT_STOP_BIT_MASK|STSPIN820_DIR_CHANGE_BIT_MASK))!=0)&&\
+ (speed<=(device_prm.minSpeed>>stepModeShift)))||\
+ ((device_prm.commandExecuted==MOVE_CMD)&&(relativePos>=device_prm.stepsToTake)))
+ {
+ /* Motion process complete */
+ if ((device_prm.commandExecuted&STSPIN820_DIR_CHANGE_BIT_MASK)!=0)
+ {
+ device_prm.commandExecuted&=~STSPIN820_DIR_CHANGE_BIT_MASK;
+ if (device_prm.direction==BACKWARD)
+ {
+ device_prm.direction=FORWARD;
+ }
+ else device_prm.direction=BACKWARD;
+ STSPIN820_Board_SetDirectionGpio(device_prm.direction);
+ if ((device_prm.commandExecuted&STSPIN820_SOFT_STOP_BIT_MASK)==0)
+ {
+ device_prm.motionState = ACCELERATING;
+ device_prm.accu = 0;
+ /* Apply accelerating torque */
+ STSPIN820_ApplyTorque(deviceId, ACC_TORQUE);
+ break;
+ }
+ }
+ if (device_prm.stopMode==HOLD_MODE)
+ {
+ STSPIN820_HardStop(deviceId);
+ }
+ else if (device_prm.stopMode==STANDBY_MODE)
+ {
+ STSPIN820_PutDeviceInStandby(deviceId);
+ }
+ else
+ {
+ STSPIN820_HardHiZ(deviceId);
+ }
+ }
+ else if ((device_prm.commandExecuted==RUN_CMD)&&
+ (speed<=(device_prm.maxSpeed>>stepModeShift)))
+ {
+ device_prm.motionState = STEADY;
+ /* Apply running torque */
+ STSPIN820_ApplyTorque(deviceId, RUN_TORQUE);
+ }
+ else
+ {
+ /* Go on decelerating */
+ if (speed>(device_prm.minSpeed>>stepModeShift))
+ {
+ bool speedUpdated = FALSE;
+ if (speed==0)
+ {
+ speed = 1;
+ }
+ device_prm.accu += dec / speed;
+ while (device_prm.accu>=(0X10000L))
+ {
+ device_prm.accu -= (0X10000L);
+ if (speed>1)
+ {
+ speed -=1;
+ }
+ speedUpdated = TRUE;
+ }
+
+ if (speedUpdated)
+ {
+ if (speed<(device_prm.minSpeed>>stepModeShift))
+ {
+ speed = device_prm.minSpeed>>stepModeShift;
+ }
+ device_prm.speed = speed;
+ }
+
+ if (device_prm.updateTorque!=FALSE)
+ {
+ /* Apply decelerating torque */
+ STSPIN820_ApplyTorque(deviceId, DEC_TORQUE);
+ }
+ }
+ }
+ break;
+ }
+ default:
+ {
+ break;
+ }
+ }
+ }
+ if ((device_prm.motionState & INACTIVE) != INACTIVE)
+ {
+ STSPIN820_ApplySpeed(deviceId, device_prm.speed);
+ }
+ else
+ {
+ if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0)
+ {
+ STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK);
+ }
+ }
+}
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Components/STSPIN820/STSPIN820.h Fri Apr 27 17:02:08 2018 +0000
@@ -0,0 +1,766 @@
+/**
+ ******************************************************************************
+ * @file STSPIN820.h
+ * @author STM
+ * @version V1.0.0
+ * @date August 7th, 2017
+ * @brief STSPIN820 driver (fully integrated microstepping motor driver)
+ * @note (C) COPYRIGHT 2017 STMicroelectronics
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Generated with STM32CubeTOO -----------------------------------------------*/
+
+
+/* Revision ------------------------------------------------------------------*/
+/*
+ Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev
+ Branch/Trunk/Tag: trunk
+ Based on: X-CUBE-SPN14/trunk/Drivers/BSP/Components/STSPIN820/STSPIN820.h
+ Revision: 0
+*/
+
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+
+#ifndef __STSPIN820_CLASS_H
+#define __STSPIN820_CLASS_H
+
+
+/* Includes ------------------------------------------------------------------*/
+
+/* ACTION 1 ------------------------------------------------------------------*
+ * Include here platform specific header files. *
+ *----------------------------------------------------------------------------*/
+#include "mbed.h"
+#include "DevSPI.h"
+/* ACTION 2 ------------------------------------------------------------------*
+ * Include here component specific header files. *
+ *----------------------------------------------------------------------------*/
+#include "STSPIN820_def.h"
+/* ACTION 3 ------------------------------------------------------------------*
+ * Include here interface specific header files. *
+ * *
+ * Example: *
+ * #include "../Interfaces/Humidity.h" *
+ * #include "../Interfaces/Temperature.h" *
+ *----------------------------------------------------------------------------*/
+#include "../Interfaces/Motor.h"
+
+
+/* Classes -------------------------------------------------------------------*/
+
+/**
+ * @brief Class representing a STSPIN820 component.
+ */
+class STSPIN820 : public Motor
+{
+public:
+
+ /*** Constructor and Destructor Methods ***/
+
+ /**
+ * @brief Constructor.
+ * @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.
+ */
+ STSPIN820(PinName ssel, DevSPI &spi) : Motor(), ssel(ssel), dev_spi(spi)
+ {
+ /* ACTION 4 ----------------------------------------------------------*
+ * Initialize here the component's member variables, one variable per *
+ * line. *
+ * *
+ * Example: *
+ * measure = 0; *
+ * instance_id = number_of_instances++; *
+ *--------------------------------------------------------------------*/
+ flag_interrupt_callback = 0;
+ error_handler_callback = 0;
+ toggle_odd = 0;
+ device_prm = 0;
+ number_of_devices = 0;
+ device_instance = 0;
+ }
+
+ /**
+ * @brief Destructor.
+ */
+ virtual ~STSPIN820(void) {}
+
+
+ /*** Public Component Related Methods ***/
+
+ /* 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(); *
+ * } *
+ *------------------------------------------------------------------------*/
+ virtual int init(void *init = NULL)
+ {
+ return (int) STSPIN820_Init((void *) init);
+ }
+
+ virtual int read_id(uint8_t *id = NULL)
+ {
+ return (int) STSPIN820_ReadId((uint8_t *) id);
+ }
+
+ virtual void attach_error_handler(void (*callback)(uint16_t error))
+ {
+ STSPIN820_AttachErrorHandler((void (*)(uint16_t error)) callback);
+ }
+
+ virtual void attach_flag_interrupt(void (*callback)(void))
+ {
+ STSPIN820_AttachFlagInterrupt((void (*)(void)) callback);
+ }
+
+ virtual void flag_interrupt_handler(void)
+ {
+ STSPIN820_FlagInterruptHandler();
+ }
+
+ virtual uint16_t get_acceleration(uint8_t deviceId)
+ {
+ return (uint16_t) STSPIN820_GetAcceleration((uint8_t) deviceId);
+ }
+
+ virtual uint16_t get_current_speed(uint8_t deviceId)
+ {
+ return (uint16_t) STSPIN820_GetCurrentSpeed((uint8_t) deviceId);
+ }
+
+ virtual uint16_t get_deceleration(uint8_t deviceId)
+ {
+ return (uint16_t) STSPIN820_GetDeceleration((uint8_t) deviceId);
+ }
+
+ virtual motor_state_t get_device_state(uint8_t deviceId)
+ {
+ return (motor_state_t) STSPIN820_GetDeviceState((uint8_t) deviceId);
+ }
+
+ virtual uint32_t get_fw_version(void)
+ {
+ return (uint32_t) STSPIN820_GetFwVersion();
+ }
+
+ virtual int32_t get_mark(uint8_t deviceId)
+ {
+ return (int32_t) STSPIN820_GetMark((uint8_t) deviceId);
+ }
+
+ virtual uint16_t get_max_speed(uint8_t deviceId)
+ {
+ return (uint16_t) STSPIN820_GetMaxSpeed((uint8_t) deviceId);
+ }
+
+ virtual uint16_t get_min_speed(uint8_t deviceId)
+ {
+ return (uint16_t) STSPIN820_GetMinSpeed((uint8_t) deviceId);
+ }
+
+ virtual int32_t get_position(uint8_t deviceId)
+ {
+ return (int32_t) STSPIN820_GetPosition((uint8_t) deviceId);
+ }
+
+ virtual void go_home(uint8_t deviceId)
+ {
+ STSPIN820_GoHome((uint8_t) deviceId);
+ }
+
+ virtual void go_mark(uint8_t deviceId)
+ {
+ STSPIN820_GoMark((uint8_t) deviceId);
+ }
+
+ virtual void go_to(uint8_t deviceId, int32_t targetPosition)
+ {
+ STSPIN820_GoTo((uint8_t) deviceId, (int32_t) targetPosition);
+ }
+
+ virtual void hard_stop(uint8_t deviceId)
+ {
+ STSPIN820_HardStop((uint8_t) deviceId);
+ }
+
+ virtual void move(uint8_t deviceId, motor_direction_t direction, uint32_t stepCount)
+ {
+ STSPIN820_Move((uint8_t) deviceId, (motor_direction_t) direction, (uint32_t) stepCount);
+ }
+
+ virtual void run(uint8_t deviceId, motor_direction_t direction)
+ {
+ STSPIN820_Run((uint8_t) deviceId, (motor_direction_t) direction);
+ }
+
+ virtual bool set_acceleration(uint8_t deviceId, uint16_t newAcc)
+ {
+ return (bool) STSPIN820_SetAcceleration((uint8_t) deviceId, (uint16_t) newAcc);
+ }
+
+ virtual bool set_deceleration(uint8_t deviceId, uint16_t newDec)
+ {
+ return (bool) STSPIN820_SetDeceleration((uint8_t) deviceId, (uint16_t) newDec);
+ }
+
+ virtual void set_home(uint8_t deviceId)
+ {
+ STSPIN820_SetHome((uint8_t) deviceId);
+ }
+
+ virtual void set_mark(uint8_t deviceId)
+ {
+ STSPIN820_SetMark((uint8_t) deviceId);
+ }
+
+ virtual bool set_max_speed(uint8_t deviceId, uint16_t newMaxSpeed)
+ {
+ return (bool) STSPIN820_SetMaxSpeed((uint8_t) deviceId, (uint16_t) newMaxSpeed);
+ }
+
+ virtual bool set_min_speed(uint8_t deviceId, uint16_t newMinSpeed)
+ {
+ return (bool) STSPIN820_SetMinSpeed((uint8_t) deviceId, (uint16_t) newMinSpeed);
+ }
+
+ virtual bool soft_stop(uint8_t deviceId)
+ {
+ return (bool) STSPIN820_SoftStop((uint8_t) deviceId);
+ }
+
+ virtual void step_clock_handler(uint8_t deviceId)
+ {
+ STSPIN820_StepClockHandler((uint8_t) deviceId);
+ }
+
+ virtual void wait_while_active(uint8_t deviceId)
+ {
+ STSPIN820_WaitWhileActive((uint8_t) deviceId);
+ }
+
+ virtual void cmd_disable(uint8_t deviceId)
+ {
+ STSPIN820_Disable((uint8_t) deviceId);
+ }
+
+ virtual void cmd_enable(uint8_t deviceId)
+ {
+ STSPIN820_Enable((uint8_t) deviceId);
+ }
+
+ virtual bool select_step_mode(uint8_t deviceId, motor_step_mode_t stepMode)
+ {
+ return (bool) STSPIN820_SetStepMode((uint8_t) deviceId, (motor_step_mode_t) stepMode);
+ }
+
+ virtual void set_direction(uint8_t deviceId, motor_direction_t direction)
+ {
+ STSPIN820_SetDirection((uint8_t) deviceId, (motor_direction_t) direction);
+ }
+
+ virtual void cmd_go_to_dir(uint8_t deviceId, motor_direction_t direction, int32_t targetPosition)
+ {
+ STSPIN820_GoToDir((uint8_t) deviceId, (motor_direction_t) direction, (int32_t) targetPosition);
+ }
+
+ virtual uint8_t check_status_hw(void)
+ {
+ return (uint8_t) STSPIN820_CheckStatusHw();
+ }
+
+ virtual void cmd_reset_device(uint8_t deviceId)
+ {
+ STSPIN820_PutDeviceInStandby((uint8_t) deviceId);
+ }
+
+ virtual uint8_t get_nb_devices(void)
+ {
+ return (uint8_t) STSPIN820_GetNbDevices();
+ }
+
+ virtual void error_handler(uint16_t error)
+ {
+ STSPIN820_ErrorHandler((uint16_t) error);
+ }
+
+ virtual uint32_t get_bridge_input_pwm_freq(uint8_t deviceId)
+ {
+ return (uint32_t) STSPIN820_VrefPwmGetFreq((uint8_t) deviceId);
+ }
+
+ virtual void set_bridge_input_pwm_freq(uint8_t deviceId, uint32_t newFreq)
+ {
+ STSPIN820_VrefPwmSetFreq((uint8_t) deviceId, (uint32_t) newFreq);
+ }
+
+ virtual void set_stop_mode(uint8_t deviceId, motor_stop_mode_t stopMode)
+ {
+ STSPIN820_SetStopMode((uint8_t) deviceId, (motor_stop_mode_t) stopMode);
+ }
+
+ virtual motor_stop_mode_t get_stop_mode(uint8_t deviceId)
+ {
+ return (motor_stop_mode_t) STSPIN820_GetStopMode((uint8_t) deviceId);
+ }
+
+ virtual void set_decay_mode(uint8_t deviceId, motor_decay_mode_t decayMode)
+ {
+ STSPIN820_SetDecayMode((uint8_t) deviceId, (motor_decay_mode_t) decayMode);
+ }
+
+ virtual motor_decay_mode_t get_decay_mode(uint8_t deviceId)
+ {
+ return (motor_decay_mode_t) STSPIN820_GetDecayMode((uint8_t) deviceId);
+ }
+
+ virtual motor_step_mode_t get_step_mode(uint8_t deviceId)
+ {
+ return (motor_step_mode_t) STSPIN820_GetStepMode((uint8_t) deviceId);
+ }
+
+ virtual motor_direction_t get_direction(uint8_t deviceId)
+ {
+ return (motor_direction_t) STSPIN820_GetDirection((uint8_t) deviceId);
+ }
+
+ virtual void exit_device_from_reset(uint8_t deviceId)
+ {
+ STSPIN820_ExitDeviceFromStandby((uint8_t) deviceId);
+ }
+
+ virtual void set_torque(uint8_t deviceId, motor_torque_mode_t torqueMode, uint8_t torqueValue)
+ {
+ STSPIN820_SetTorque((uint8_t) deviceId, (motor_torque_mode_t) torqueMode, (uint8_t) torqueValue);
+ }
+
+ virtual uint8_t get_torque(uint8_t deviceId, motor_torque_mode_t torqueMode)
+ {
+ return (uint8_t) STSPIN820_GetTorque((uint8_t) deviceId, (motor_torque_mode_t) torqueMode);
+ }
+
+ virtual bool set_nb_devices(uint8_t deviceId)
+ {
+ return (bool) STSPIN820_SetNbDevices((uint8_t) deviceId);
+ }
+
+ virtual void set_torque_boost_enable(uint8_t deviceId, bool enable)
+ {
+ STSPIN820_SetTorqueBoostEnable((uint8_t) deviceId, (bool) enable);
+ }
+
+ virtual bool get_torque_boost_enable(uint8_t deviceId)
+ {
+ return (bool) STSPIN820_GetTorqueBoostEnable((uint8_t) deviceId);
+ }
+
+ virtual void set_torque_boost_threshold(uint8_t deviceId, uint16_t speedThreshold)
+ {
+ STSPIN820_SetTorqueBoostThreshold((uint8_t) deviceId, (uint16_t) speedThreshold);
+ }
+
+ virtual uint16_t get_torque_boost_threshold(uint8_t deviceId)
+ {
+ return (uint16_t) STSPIN820_GetTorqueBoostThreshold((uint8_t) deviceId);
+ }
+
+
+ /*** Public Interrupt Related Methods ***/
+
+ /* 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(); *
+ * } *
+ *------------------------------------------------------------------------*/
+
+
+protected:
+
+ /*** Protected Component Related Methods ***/
+
+ /* 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 STSPIN820_ApplySpeed(uint8_t pwmId, uint16_t newSpeed);
+ void STSPIN820_ApplyTorque(uint8_t deviceId, motor_torque_mode_t torqueMode);
+ void STSPIN820_ComputeSpeedProfile(uint8_t deviceId, uint32_t nbSteps);
+ void STSPIN820_FlagInterruptHandler(void);
+ void STSPIN820_SetDeviceParamsOtherValues(void);
+ void STSPIN820_SetDeviceParamsToGivenValues(STSPIN820_init_t* initDevicePrm);
+ void STSPIN820_SetDeviceParamsToPredefinedValues(void);
+ void STSPIN820_StartMovement(uint8_t deviceId);
+ void STSPIN820_StepClockHandler(uint8_t deviceId);
+ MOTOR_vt_t* STSPIN820_GetMotorHandle(void); //Return handle of the motor driver handle
+ void STSPIN820_Init(void* pInit); //Start the STSPIN820 library
+ uint16_t STSPIN820_ReadId(void); //Read Id to get driver instance
+ void STSPIN820_AttachErrorHandler(void (*callback)(uint16_t)); //Attach a user callback to the error handler
+ void STSPIN820_AttachFlagInterrupt(void (*callback)(void)); //Attach a user callback to the flag Interrupt
+ uint8_t STSPIN820_CheckStatusHw(void); //Check if STSPIN820 has a fault by reading EN pin position
+ void STSPIN820_Disable(uint8_t deviceId); //Disable the power stage of the specified device
+ void STSPIN820_Enable(uint8_t deviceId); //Enable the power stage of the specified device
+ void STSPIN820_ErrorHandler(uint16_t error); //Error handler which calls the user callback
+ void STSPIN820_ExitDeviceFromStandby(uint8_t deviceId); //Exit STSPIN820 device from standby
+ uint16_t STSPIN820_GetAcceleration(uint8_t deviceId); //Return the acceleration in pps^2
+ uint16_t STSPIN820_GetCurrentSpeed(uint8_t deviceId); //Return the current speed in pps
+ motor_decay_mode_t STSPIN820_GetDecayMode(uint8_t deviceId); //Return the device decay mode
+ uint16_t STSPIN820_GetDeceleration(uint8_t deviceId); //Return the deceleration in pps^2
+ motor_state_t STSPIN820_GetDeviceState(uint8_t deviceId); //Return the device state
+ motor_direction_t STSPIN820_GetDirection(uint8_t deviceId); //Get the motor current direction
+ uint32_t STSPIN820_GetFwVersion(void); //Return the FW version
+ int32_t STSPIN820_GetMark(uint8_t deviceId); //Return the mark position
+ uint16_t STSPIN820_GetMaxSpeed(uint8_t deviceId); //Return the max speed in pps
+ uint16_t STSPIN820_GetMinSpeed(uint8_t deviceId); //Return the min speed in pps
+ uint8_t STSPIN820_GetNbDevices(void); //Return the nupber of devices
+ int32_t STSPIN820_GetPosition(uint8_t deviceId); //Return the ABS_POSITION (32b signed)
+ motor_step_mode_t STSPIN820_GetStepMode(uint8_t deviceId); //Get the motor step mode
+ motor_stop_mode_t STSPIN820_GetStopMode(uint8_t deviceId); //Get the selected mode to stop the motor
+ uint8_t STSPIN820_GetTorque(uint8_t deviceId, motor_torque_mode_t torqueMode);
+ bool STSPIN820_GetTorqueBoostEnable(uint8_t deviceId); //Get the torque boost feature status
+ uint16_t STSPIN820_GetTorqueBoostThreshold(uint8_t deviceId); //Get the torque boost threshold
+ void STSPIN820_GoHome(uint8_t deviceId); //Move to the home position
+ void STSPIN820_GoMark(uint8_t deviceId); //Move to the Mark position
+ void STSPIN820_GoTo(uint8_t deviceId, int32_t targetPosition); //Go to the specified position
+ void STSPIN820_GoToDir(uint8_t deviceId, motor_direction_t direction, int32_t targetPosition); //Go to the specified position using the specified direction
+ void STSPIN820_HardStop(uint8_t deviceId); //Stop the motor and keep holding torque
+ void STSPIN820_HardHiZ(uint8_t deviceId); //Stop the motor and disable the power bridge
+ void STSPIN820_Move(uint8_t deviceId, motor_direction_t direction, uint32_t stepCount); //Move the motor of the specified number of steps
+ void STSPIN820_PutDeviceInStandby(uint8_t deviceId); //Put STSPIN820 device in standby (low power consumption)
+ void STSPIN820_Run(uint8_t deviceId, motor_direction_t direction); //Run the motor
+ bool STSPIN820_SetAcceleration(uint8_t deviceId,uint16_t newAcc); //Set the acceleration in pps^2
+ bool STSPIN820_SetDeceleration(uint8_t deviceId,uint16_t newDec); //Set the deceleration in pps^2
+ void STSPIN820_SetDecayMode(uint8_t deviceId, motor_decay_mode_t decay); //Set the STSPIN820 decay mode pin
+ void STSPIN820_SetDirection(uint8_t deviceId, motor_direction_t direction); //Set the STSPIN820 direction pin
+ void STSPIN820_SetHome(uint8_t deviceId); //Set current position to be the home position
+ void STSPIN820_SetMark(uint8_t deviceId); //Set current position to be the Markposition
+ bool STSPIN820_SetMaxSpeed(uint8_t deviceId,uint16_t newMaxSpeed);//Set the max speed in pps
+ bool STSPIN820_SetMinSpeed(uint8_t deviceId,uint16_t newMinSpeed);//Set the min speed in pps
+ bool STSPIN820_SetNbDevices(uint8_t nbDevices);
+ bool STSPIN820_SetStepMode(uint8_t deviceId, motor_step_mode_t stepMode); // Step mode selection
+ void STSPIN820_SetStopMode(uint8_t deviceId, motor_stop_mode_t stopMode); //Select the mode to stop the motor
+ void STSPIN820_SetTorque(uint8_t deviceId, motor_torque_mode_t torqueMode, uint8_t torqueValue);
+ void STSPIN820_SetTorqueBoostEnable(uint8_t deviceId, bool enable); // Enable or disable the torque boost feature
+ void STSPIN820_SetTorqueBoostThreshold(uint8_t deviceId, uint16_t speedThreshold); //Set the torque boost threshold
+ bool STSPIN820_SoftStop(uint8_t deviceId); //Progressively stop the motor by using the device deceleration and set deceleration torque
+ uint32_t STSPIN820_VrefPwmGetFreq(uint8_t deviceId); //Get the frequency of REF PWM of the specified device
+ void STSPIN820_VrefPwmSetFreq(uint8_t deviceId, uint32_t newFreq);//Set the frequency of REF PWM of the specified device
+ void STSPIN820_WaitWhileActive(uint8_t deviceId); //Wait for the device state becomes Inactive
+
+
+ /*** Component's I/O Methods ***/
+
+ /**
+ * @brief Utility function to read data from STSPIN820.
+ * @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 STSPIN820.
+ * @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 STSPIN820 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 read_write(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 8 --------------------------------------------------------------*
+ * Implement here other I/O methods beyond those already implemented *
+ * above, which are declared extern within the component's header file. *
+ *------------------------------------------------------------------------*/
+ void STSPIN820_Board_Delay(uint32_t delay)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_Disable(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_DisableIrq(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ uint32_t STSPIN820_Board_EN_AND_FAULT_PIN_GetState(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ return (uint32_t) 0;
+ }
+
+ void STSPIN820_Board_Enable(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_EnableIrq(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_GpioInit(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_PwmRefInit(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_PwmRefSetFreqAndDutyCycle(uint32_t newFreq, uint8_t dutyCycle)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_PwmRefStart(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_PwmRefStop(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_ReleaseReset(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_Reset(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_SetDecayGpio(uint8_t gpioState)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ uint8_t STSPIN820_Board_GetDecayGpio(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ return (uint8_t) 0;
+ }
+
+ void STSPIN820_Board_SetDirectionGpio(uint8_t gpioState)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_SetFullStep(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ bool STSPIN820_Board_SetModePins(uint8_t modePin1Level, uint8_t modePin2Level, uint8_t modePin3Level)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ return (bool) 0;
+ }
+
+ void STSPIN820_Board_TimStckCompareInit(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_TimStckDeInit(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_TimStckInit(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_TimStckSetFreq(uint16_t newFreq)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ void STSPIN820_Board_TimStckStart(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+ uint8_t STSPIN820_Board_TimStckStop(uint8_t *pToggleOdd)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ return (uint8_t) 0;
+ }
+
+ void STSPIN820_Board_UnsetFullStep(void)
+ {
+ /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
+ }
+
+
+ /*** 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; *
+ *------------------------------------------------------------------------*/
+
+ /* ACTION 10 -------------------------------------------------------------*
+ * Declare here other pin related variables, if needed. *
+ * *
+ * Example: *
+ * + mbed: *
+ * DigitalOut standby_reset; *
+ *------------------------------------------------------------------------*/
+
+ /* 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;
+
+ /* 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; *
+ *------------------------------------------------------------------------*/
+ void (*flag_interrupt_callback)(void);
+ void (*error_handler_callback)(uint16_t error);
+ uint8_t toggle_odd;
+ device_params_t device_prm;
+ uint8_t number_of_devices;
+ uint8_t device_instance;
+};
+
+#endif /* __STSPIN820_CLASS_H */
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Components/STSPIN820/STSPIN820_config.h Fri Apr 27 17:02:08 2018 +0000
@@ -0,0 +1,133 @@
+/**************************************************************************//**
+ * @file STSPIN820_config.h
+ * @author STM
+ * @version V1.0.1
+ * @date August 7th, 2017
+ * @brief Predefines values for the STSPIN820 registers
+ * and for the devices parameters
+ * @note (C) COPYRIGHT 2017 STMicroelectronics
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+
+#ifndef __STSPIN820_TARGET_CONFIG_H
+#define __STSPIN820_TARGET_CONFIG_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/** @addtogroup BSP
+ * @{
+ */
+
+/** @addtogroup STSPIN820
+ * @{
+ */
+
+/** @addtogroup STSPIN820_Exported_Constants
+ * @{
+ */
+
+/** @defgroup Predefined_STSPIN820_Registers_Values Predefined STSPIN820 Registers Values
+ * @{
+ */
+
+/// The maximum number of devices
+#define MAX_NUMBER_OF_DEVICES (2)
+
+/************************ Speed Profile *******************************/
+
+/// Acceleration rate in pulse/s2 (must be greater than 0)
+#define STSPIN820_CONF_PARAM_ACC (480)
+
+/// Deceleration rate in pulse/s2 (must be greater than 0)
+#define STSPIN820_CONF_PARAM_DEC (480)
+
+/// Running speed in pulse/s (8 pulse/s < Maximum speed <= 10 000 pulse/s )
+#define STSPIN820_CONF_PARAM_RUNNING_SPEED (1600)
+
+/// Minimum speed in pulse/s (8 pulse/s <= Minimum speed < 10 000 pulse/s)
+#define STSPIN820_CONF_PARAM_MIN_SPEED (400)
+
+/************************ Torque *******************************/
+
+/// Acceleration torque in % (from 0 to 100)
+#define STSPIN820_CONF_PARAM_ACC_TORQUE (25)
+
+/// Deceleration torque in % (from 0 to 100)
+#define STSPIN820_CONF_PARAM_DEC_TORQUE (20)
+
+/// Running torque in % (from 0 to 100)
+#define STSPIN820_CONF_PARAM_RUNNING_TORQUE (15)
+
+/// Holding torque in % (from 0 to 100)
+#define STSPIN820_CONF_PARAM_HOLDING_TORQUE (30)
+
+/// Torque boost speed enable
+#define STSPIN820_CONF_PARAM_TORQUE_BOOST_EN (TRUE)
+
+/// Torque boost speed threshold in fullstep/s
+#define STSPIN820_CONF_PARAM_TORQUE_BOOST_TH (200)
+
+/******************************* Others ***************************************/
+
+/// Step mode selection settings
+#define STSPIN820_CONF_PARAM_STEP_MODE (STEP_MODE_1_32)
+
+/// Automatic HIZ STOP
+#define STSPIN820_CONF_PARAM_AUTO_HIZ_STOP (HOLD_MODE)
+
+/// REF PWM frequency (Hz)
+#define STSPIN820_CONF_PARAM_REF_PWM_FREQUENCY (100000)
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* __STSPIN820_TARGET_CONFIG_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Components/STSPIN820/STSPIN820_def.h Fri Apr 27 17:02:08 2018 +0000
@@ -0,0 +1,402 @@
+/******************************************************//**
+ * @file STSPIN820_def.h
+ * @author STM
+ * @version V1.0.0
+ * @date August 7th, 2017
+ * @brief Header for STSPIN820 driver (fully integrated microstepping motor driver)
+ * @note (C) COPYRIGHT 2017 STMicroelectronics
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of STMicroelectronics nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+
+#ifndef __STSPIN820_H
+#define __STSPIN820_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+
+/* Includes ------------------------------------------------------------------*/
+
+#include "STSPIN820_config.h"
+#include "motor_def.h"
+
+/** @addtogroup BSP
+ * @{
+ */
+
+/** @addtogroup STSPIN820
+ * @{
+ */
+
+
+/* Definitions ---------------------------------------------------------------*/
+
+/** @defgroup STSPIN820_Exported_Constants STSPIN820 Exported Constants
+ * @{
+ */
+/// Current FW major version
+#define STSPIN820_FW_MAJOR_VERSION (uint8_t)(1)
+/// Current FW minor version
+#define STSPIN820_FW_MINOR_VERSION (uint8_t)(0)
+/// Current FW patch version
+#define STSPIN820_FW_PATCH_VERSION (uint8_t)(0)
+/// Current FW version
+#define STSPIN820_FW_VERSION (uint32_t)((STSPIN820_FW_MAJOR_VERSION<<16)|\
+ (STSPIN820_FW_MINOR_VERSION<<8)|\
+ (STSPIN820_FW_PATCH_VERSION))
+
+/// Max position
+#define STSPIN820_MAX_POSITION (0x7FFFFFFF)
+
+/// Min position
+#define STSPIN820_MIN_POSITION (0x80000000)
+
+/// Position range
+#define STSPIN820_POSITION_RANGE ((uint32_t)(STSPIN820_MAX_POSITION -\
+ STSPIN820_MIN_POSITION))
+/// STSPIN820 error base number
+#define STSPIN820_ERROR_BASE (0xA000)
+
+/// run bit mask
+#define STSPIN820_RUN_BIT_MASK (0x01)
+
+/// move bit mask
+#define STSPIN820_MOVE_BIT_MASK (0x02)
+
+/// soft stop bit mask
+#define STSPIN820_SOFT_STOP_BIT_MASK (0x04)
+
+/// direction change bit mask
+#define STSPIN820_DIR_CHANGE_BIT_MASK (0x08)
+
+/// Maximum frequency of the step clock frequency in Hz
+#define STSPIN820_MAX_STCK_FREQ (10000)
+
+/// Minimum frequency of the step clock frequency in Hz
+#define STSPIN820_MIN_STCK_FREQ (8)
+
+/// Minimum duration of standby
+#define STANDBY_MIN_DURATION (1)
+
+/// Dead time after standby exit
+#define AFTER_STANDBY_EXIT_DEAD_TIME (1)
+
+/// Reset delay to select step mode
+#define SELECT_STEP_MODE_DELAY (1)
+
+/// PWM REF and bridges disable delay
+#define DISABLE_DELAY (1)
+
+/// Microstepping sequencer maximum value
+#define SEQUENCER_MAX_VALUE (uint16_t)(0x3FF)
+
+
+/* Types ---------------------------------------------------------------------*/
+
+/** @defgroup STSPIN820_Exported_Types STSPIN820 Exported Types
+ * @{
+ */
+
+/** @defgroup Error_Types Error Types
+ * @{
+ */
+/// Errors
+typedef enum
+{
+ STSPIN820_ERROR_SET_HOME = STSPIN820_ERROR_BASE, /// Error while setting home position
+ STSPIN820_ERROR_SET_MAX_SPEED = STSPIN820_ERROR_BASE + 1, /// Error while setting max speed
+ STSPIN820_ERROR_SET_MIN_SPEED = STSPIN820_ERROR_BASE + 2, /// Error while setting min speed
+ STSPIN820_ERROR_SET_ACCELERATION = STSPIN820_ERROR_BASE + 3, /// Error while setting acceleration
+ STSPIN820_ERROR_SET_DECELERATION = STSPIN820_ERROR_BASE + 4, /// Error while setting decelaration
+ STSPIN820_ERROR_MCU_OSC_CONFIG = STSPIN820_ERROR_BASE + 5, /// Error while configuring mcu oscillator
+ STSPIN820_ERROR_MCU_CLOCK_CONFIG = STSPIN820_ERROR_BASE + 6, /// Error while configuring mcu clock
+ STSPIN820_ERROR_POSITION = STSPIN820_ERROR_BASE + 7, /// Unexpected current position (wrong number of steps)
+ STSPIN820_ERROR_SPEED = STSPIN820_ERROR_BASE + 8, /// Unexpected current speed
+ STSPIN820_ERROR_INIT = STSPIN820_ERROR_BASE + 9, /// Unexpected number of devices or unexpected value for predefined parameter
+ STSPIN820_ERROR_SET_DIRECTION = STSPIN820_ERROR_BASE + 10, /// Error while setting direction
+ STSPIN820_ERROR_SET_STEP_MODE = STSPIN820_ERROR_BASE + 11, /// Attempt to set an unsupported step mode
+ STSPIN820_ERROR_APPLY_SPEED = STSPIN820_ERROR_BASE + 12, /// Error while applying speed
+ STSPIN820_ERROR_SET_TORQUE = STSPIN820_ERROR_BASE + 13, /// Error while setting torque
+ STSPIN820_ERROR_STEP_CLOCK = STSPIN820_ERROR_BASE + 14 /// Error related to step clock
+} error_types_t;
+/**
+ * @}
+ */
+
+/** @defgroup Device_Commands Device Commands
+ * @{
+ */
+/// Device commands
+typedef enum
+{
+ NO_CMD = 0x00,
+ RUN_CMD = (STSPIN820_RUN_BIT_MASK),
+ MOVE_CMD = (STSPIN820_MOVE_BIT_MASK),
+} device_command_t;
+/**
+ * @}
+ */
+
+
+/** @defgroup Device_Parameters Device Parameters
+ * @{
+ */
+
+/// Device Parameters Structure Type
+typedef struct
+{
+ /// accumulator used to store speed increase smaller than 1 pps
+ volatile uint32_t accu;
+ /// Position in microstep according to current step mode
+ volatile int32_t currentPosition;
+ /// Position of sequencer
+ volatile int16_t sequencerPosition;
+ /// mark position in microstep (motor position control mode)
+ volatile int32_t markPosition;
+ /// position in microstep at the end of the accelerating phase
+ volatile uint32_t endAccPos;
+ /// nb of in microstep performed from the beggining of the goto or the move command
+ volatile uint32_t relativePos;
+ /// position in microstep step at the start of the decelerating phase
+ volatile uint32_t startDecPos;
+ /// nb of microstep steps to perform for the goto or move commands
+ uint32_t stepsToTake;
+
+ /// constant speed phase torque value (%)
+ volatile uint8_t runTorque;
+ /// acceleration phase torque value (%)
+ volatile uint8_t accelTorque;
+ /// deceleration phase torque value (%)
+ volatile uint8_t decelTorque;
+ /// holding phase torque value (%)
+ volatile uint8_t holdTorque;
+ /// current selected torque value
+ volatile uint8_t currentTorque;
+ /// torque update
+ volatile bool updateTorque;
+ /// PWM frequency used to generate REF voltage
+ volatile uint32_t refPwmFreq;
+ /// torque boost enable
+ volatile bool torqueBoostEnable;
+ /// torque boost speed threshold
+ volatile uint16_t torqueBoostSpeedThreshold;
+
+ /// acceleration in pps^2
+ volatile uint16_t acceleration;
+ /// deceleration in pps^2
+ volatile uint16_t deceleration;
+ /// max speed in pps (speed use for goto or move command)
+ volatile uint16_t maxSpeed;
+ /// min speed in pps
+ volatile uint16_t minSpeed;
+ /// current speed in pps
+ volatile uint16_t speed;
+
+ /// command under execution
+ volatile device_command_t commandExecuted;
+ /// FORWARD or BACKWARD direction
+ volatile motor_direction_t direction;
+ /// current state of the device
+ volatile motor_state_t motionState;
+ /// current step mode
+ volatile motor_step_mode_t stepMode;
+ /// latched step mode
+ motor_step_mode_t stepModeLatched;
+ /// current stop mode
+ motor_stop_mode_t stopMode;
+
+} device_params_t;
+
+/* 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; *
+ *----------------------------------------------------------------------------*/
+/// Motor driver initialization structure definition
+typedef struct
+{
+ /// acceleration in pps^2
+ uint16_t acceleration;
+ /// deceleration in pps^2
+ uint16_t deceleration;
+ /// max speed in pps (speed use for goto or move command)
+ uint16_t maxSpeed;
+ /// min speed in pps
+ uint16_t minSpeed;
+ /// acceleration phase torque value (%)
+ uint8_t accelTorque;
+ /// deceleration phase torque value (%)
+ uint8_t decelTorque;
+ /// constant speed phase torque value (%)
+ uint8_t runTorque;
+ /// holding phase torque value (%)
+ uint8_t holdTorque;
+ /// torque boost enable
+ bool torqueBoostEnable;
+ /// torque boost speed threshold
+ uint16_t torqueBoostSpeedThreshold;
+ /// step mode
+ motor_step_mode_t stepMode;
+ /// stop mode
+ motor_stop_mode_t stopMode;
+ /// PWM frequency used to generate REF voltage
+ uint32_t vrefPwmFreq;
+} STSPIN820_init_t;
+
+/**
+ * @brief STSPIN820 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 (*flag_interrupt_callback)(void);
+ /// Function pointer to error handler call back
+ void (*error_handler_callback)(uint16_t error);
+ uint8_t toggle_odd;
+
+ /// STSPIN820 Device Paramaters structure
+ device_params_t device_prm;
+ uint8_t number_of_devices;
+ uint8_t device_instance;
+} STSPIN820_data_t;
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup MotorControl_Board_Linked_Functions MotorControl Board Linked Functions
+ * @{
+ */
+/* ACTION --------------------------------------------------------------------*
+ * Declare here extern I/O and interrupt related functions you might need, *
+ * and implemented then 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 STSPIN820_Board_Delay(void *handle, uint32_t delay);
+///Disable the power bridges (leave the output bridges HiZ)
+extern void STSPIN820_Board_Disable(void *handle);
+///Disable Irq
+extern void STSPIN820_Board_DisableIrq(void *handle);
+//Get the EN FAULT pin state
+extern uint32_t STSPIN820_Board_EN_AND_FAULT_PIN_GetState(void *handle);
+///Enable the power bridges (leave the output bridges HiZ)
+extern void STSPIN820_Board_Enable(void *handle);
+///Enable Irq
+extern void STSPIN820_Board_EnableIrq(void *handle);
+///Initialise GPIOs used for STSPIN820
+extern void STSPIN820_Board_GpioInit(void *handle);
+///Init the reference voltage pwm
+extern void STSPIN820_Board_PwmRefInit(void *handle);
+///Set the frequency and duty cycle of PWM used for the reference voltage generation
+extern void STSPIN820_Board_PwmRefSetFreqAndDutyCycle(void *handle, uint32_t newFreq, uint8_t dutyCycle);
+///Start the reference voltage pwm
+extern void STSPIN820_Board_PwmRefStart(void *handle);
+///Stop the reference voltage pwm
+extern void STSPIN820_Board_PwmRefStop(void *handle);
+///Reset the STSPIN820 reset pin
+extern void STSPIN820_Board_ReleaseReset(void *handle);
+///Set the STSPIN820 reset pin
+extern void STSPIN820_Board_Reset(void *handle);
+///Set decay GPIO
+extern void STSPIN820_Board_SetDecayGpio(void *handle, uint8_t gpioState);
+///Get decay GPIO
+extern uint8_t STSPIN820_Board_GetDecayGpio(void *handle);
+///Set direction GPIO
+extern void STSPIN820_Board_SetDirectionGpio(void *handle, uint8_t gpioState);
+///Select Full Step mode
+extern void STSPIN820_Board_SetFullStep(void *handle);
+///Select the STSPIN820 mode1, mode2, and mode3 pins levels
+extern bool STSPIN820_Board_SetModePins(void *handle, uint8_t modePin1Level, uint8_t modePin2Level, uint8_t modePin3Level);
+///Step clock compare value initialization
+extern void STSPIN820_Board_TimStckCompareInit(void *handle);
+///DeInit the timer
+extern void STSPIN820_Board_TimStckDeInit(void *handle);
+///Init the timer
+extern void STSPIN820_Board_TimStckInit(void *handle);
+///Set step clock frequency
+extern void STSPIN820_Board_TimStckSetFreq(void *handle, uint16_t newFreq);
+///Start step clock
+extern void STSPIN820_Board_TimStckStart(void *handle);
+///Stop the timer
+extern uint8_t STSPIN820_Board_TimStckStop(void *handle, uint8_t *pToggleOdd);
+///Unselect Full Step mode
+extern void STSPIN820_Board_UnsetFullStep(void *handle);
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* #ifndef __STSPIN820_H */
+
+/******************* (C) COPYRIGHT 2017 STMicroelectronics *****END OF FILE****/