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.
Dependencies: ST_INTERFACES
Dependents: HelloWorld_IHM14A1
Fork of X_NUCLEO_IHM14A1 by
Revision 2:4fd08b67958c, committed 2018-09-27
- Comitter:
- Davidroid
- Date:
- Thu Sep 27 14:05:44 2018 +0000
- Parent:
- 1:bc265521eb00
- Commit message:
- Library to handle the X-NUCLEO-IHM14A1 Motor Control Expansion Board based on the STSPIN820 component.
Changed in this revision
--- a/Components/Common/motor_def.h Fri Apr 27 17:02:08 2018 +0000 +++ b/Components/Common/motor_def.h Thu Sep 27 14:05:44 2018 +0000 @@ -89,7 +89,7 @@ * @{ */ ///bool Type -typedef uint8_t bool; +//typedef uint8_t bool; /** * @} */
--- a/Components/STSPIN820/STSPIN820.cpp Fri Apr 27 17:02:08 2018 +0000 +++ b/Components/STSPIN820/STSPIN820.cpp Thu Sep 27 14:05:44 2018 +0000 @@ -1,40 +1,40 @@ /** - ****************************************************************************** - * @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. - * - ****************************************************************************** - */ + ******************************************************************************* + * @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 -----------------------------------------------*/ @@ -53,6 +53,13 @@ #include "STSPIN820.h" + +/* Variables ----------------------------------------------------------------*/ + +/* Number of devices. */ +uint8_t STSPIN820::number_of_devices = 0; + + /* Private function prototypes -----------------------------------------------*/ /** @defgroup STSPIN820_Private_Functions STSPIN820 Private Functions @@ -68,33 +75,22 @@ * @{ */ -/******************************************************//** - * @brief Return motor handle (pointer to the STSPIN820 motor driver structure) - * @retval Pointer to the MOTOR_vt_t structure +/********************************************************** + * @brief Starts the STSPIN820 library + * @param init Initialization structure. + * @retval COMPONENT_OK in case of success. **********************************************************/ -MOTOR_vt_t* STSPIN820::STSPIN820_GetMotorHandle(void) +status_t STSPIN820::STSPIN820_Init(void *init) { - 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(); + STSPIN820_GpioInit(); /* Initialise the timer used for the step clock and ------------------------*/ /* the PWM for the reference voltage generation ----------------------------*/ - STSPIN820_Board_TimStckInit(); - STSPIN820_Board_PwmRefInit(); + STSPIN820_TimStckInit(); + STSPIN820_PwmRefInit(); - if (pInit == 0) + if (init == 0) { /* Set all context variables to the predefined values */ /* from STSPIN820_config.h */ @@ -102,20 +98,25 @@ } else { - STSPIN820_SetDeviceParamsToGivenValues((STSPIN820_init_t*) pInit); + STSPIN820_SetDeviceParamsToGivenValues((STSPIN820_init_t*) init); } + + return COMPONENT_OK; } -/******************************************************//** +/********************************************************** * @brief Read id - * @retval Id of the STSPIN820 Driver Instance + * @param id pointer to the identifier to be read. + * @retval COMPONENT_OK in case of success. **********************************************************/ -uint16_t STSPIN820::STSPIN820_ReadId(void) +status_t STSPIN820::STSPIN820_ReadId(uint8_t *id) { - return (device_instance); + *id = device_instance; + + return COMPONENT_OK; } -/******************************************************//** +/********************************************************** * @brief Attach a user callback to the error Handler. * The call back will be then called each time the library * detects an error @@ -128,7 +129,7 @@ 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 @@ -143,13 +144,13 @@ 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()) + if(!STSPIN820_EN_FAULT_PIN_GetState()) { return 0x01; } @@ -159,27 +160,25 @@ } } -/******************************************************//** +/********************************************************** * @brief Disable the power bridges (leave the output bridges HiZ) - * @param[in] deviceId Unused parameter * @retval None **********************************************************/ -void STSPIN820::STSPIN820_Disable(uint8_t deviceId) +void STSPIN820::STSPIN820_Disable(void) { - STSPIN820_Board_Disable(); + STSPIN820_Disable_Power_Bridges(); } -/******************************************************//** +/********************************************************** * @brief Enable the power bridges - * @param[in] deviceId Unused parameter * @retval None **********************************************************/ -void STSPIN820::STSPIN820_Enable(uint8_t deviceId) +void STSPIN820::STSPIN820_Enable(void) { - STSPIN820_Board_Enable(); + STSPIN820_Enable_Power_Bridges(); } -/******************************************************//** +/********************************************************** * @brief Error handler which calls the user callback (if defined) * @param[in] error Number of the error * @retval None @@ -199,30 +198,29 @@ } } -/******************************************************//** +/********************************************************** * @brief Exit STSPIN820 device from standby (low power consumption) - * @param[in] deviceId Unused parameter * @retval None **********************************************************/ -void STSPIN820::STSPIN820_ExitDeviceFromStandby(uint8_t deviceId) +void STSPIN820::STSPIN820_ExitDeviceFromStandby(void) { uint32_t sequencerPosition = device_prm.sequencerPosition; /* Exit standby and set step mode */ /* Disable step clock */ - if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0) + if (STSPIN820_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); + STSPIN820_Delay(DISABLE_DELAY); /* Set reference voltage to 0 */ - STSPIN820_SetTorque(0, CURRENT_TORQUE, 0); + STSPIN820_SetTorque( CURRENT_TORQUE, 0); /* Disable power bridges */ - STSPIN820_Board_Disable(); + STSPIN820_Disable_Power_Bridges(); device_prm.commandExecuted = NO_CMD; device_prm.stepsToTake = 0; @@ -235,27 +233,27 @@ 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_SetStepMode(device_prm.stepMode); + STSPIN820_ReleaseReset(); + STSPIN820_Delay(AFTER_STANDBY_EXIT_DEAD_TIME); - STSPIN820_SetHome(deviceId); - STSPIN820_SetMark(deviceId); + STSPIN820_SetHome(); + STSPIN820_SetMark(); 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); + STSPIN820_SetDirection(FORWARD); /* Going out of standby */ device_prm.motionState = STANDBYTOINACTIVE; /* Initialize the step clock timer */ - STSPIN820_Board_TimStckInit(); + STSPIN820_TimStckInit(); /* Program the step clock */ - STSPIN820_Board_TimStckCompareInit(); - STSPIN820_Board_TimStckSetFreq(STSPIN820_MAX_STCK_FREQ); + STSPIN820_TimStckCompareInit(); + STSPIN820_TimStckSetFreq(STSPIN820_MAX_STCK_FREQ); toggle_odd = 0; - STSPIN820_Board_TimStckStart(); + STSPIN820_TimStckStart(); while (device_prm.sequencerPosition != 0); while (toggle_odd!=0); device_prm.sequencerPosition = sequencerPosition; @@ -264,35 +262,32 @@ 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) +uint16_t STSPIN820::STSPIN820_GetAcceleration(void) { 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) +uint16_t STSPIN820::STSPIN820_GetCurrentSpeed(void) { 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 STSPIN820::STSPIN820_GetDecayMode(void) { motor_decay_mode_t status; - if (STSPIN820_Board_GetDecayGpio() != 0) + if (STSPIN820_GetDecayGpio() != 0) { status = SLOW_DECAY; } @@ -304,37 +299,34 @@ 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) +uint16_t STSPIN820::STSPIN820_GetDeceleration(void) { 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) +motor_state_t STSPIN820::STSPIN820_GetDeviceState(void) { 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) +motor_direction_t STSPIN820::STSPIN820_GetDirection(void) { return device_prm.direction; } -/******************************************************//** +/********************************************************** * @brief Return the FW version of the library * @retval Stspin220_FW_VERSION **********************************************************/ @@ -343,37 +335,34 @@ 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) +int32_t STSPIN820::STSPIN820_GetMark(void) { 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) +uint16_t STSPIN820::STSPIN820_GetMaxSpeed(void) { 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) +uint16_t STSPIN820::STSPIN820_GetMinSpeed(void) { return (device_prm.minSpeed); } -/******************************************************//** +/********************************************************** * @brief Returns the number of devices * @retval number of devices **********************************************************/ @@ -382,44 +371,40 @@ 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) +int32_t STSPIN820::STSPIN820_GetPosition(void) { 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) +motor_step_mode_t STSPIN820::STSPIN820_GetStepMode(void) { 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) +motor_stop_mode_t STSPIN820::STSPIN820_GetStopMode(void) { 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 STSPIN820::STSPIN820_GetTorque(motor_torque_mode_t torqueMode) { uint8_t torqueValue = 0; switch(torqueMode) @@ -445,54 +430,46 @@ 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) +bool STSPIN820::STSPIN820_GetTorqueBoostEnable(void) { return device_prm.torqueBoostEnable; } -/******************************************************//** +/********************************************************** * @brief Get the torque boost threshold - * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES - 1) + * @param[in] (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) +uint16_t STSPIN820::STSPIN820_GetTorqueBoostThreshold(void) { 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) +void STSPIN820::STSPIN820_GoHome(void) { - deviceId = 0; - - STSPIN820_GoTo(deviceId, 0); + STSPIN820_GoTo(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) +void STSPIN820::STSPIN820_GoMark(void) { - deviceId = 0; - - STSPIN820_GoTo(deviceId, device_prm.markPosition); + STSPIN820_GoTo(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 @@ -507,20 +484,19 @@ * 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) +void STSPIN820::STSPIN820_GoTo(int32_t targetPosition) { motor_direction_t direction; - deviceId = 0; /* Exit from standby if needed */ if (device_prm.motionState == STANDBY) { - STSPIN820_ExitDeviceFromStandby(deviceId); + STSPIN820_ExitDeviceFromStandby(); } /* Deactivate motor if needed */ else if (device_prm.motionState != INACTIVE) { - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); } if (targetPosition > device_prm.currentPosition) @@ -559,18 +535,17 @@ device_prm.commandExecuted = MOVE_CMD; /* Direction setup */ - STSPIN820_SetDirection(deviceId, direction); + STSPIN820_SetDirection(direction); - STSPIN820_ComputeSpeedProfile(deviceId, device_prm.stepsToTake); + STSPIN820_ComputeSpeedProfile(device_prm.stepsToTake); /* Motor activation */ - STSPIN820_StartMovement(deviceId); + STSPIN820_StartMovement(); } } -/******************************************************//** +/********************************************************** * @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 @@ -586,19 +561,17 @@ * 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) +void STSPIN820::STSPIN820_GoToDir(motor_direction_t direction, int32_t targetPosition) { - deviceId = 0; - /* Exit from standby if needed */ if (device_prm.motionState == STANDBY) { - STSPIN820_ExitDeviceFromStandby(deviceId); + STSPIN820_ExitDeviceFromStandby(); } /* Deactivate motor if needed */ else if (device_prm.motionState != INACTIVE) { - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); } if (direction != BACKWARD) @@ -635,46 +608,45 @@ device_prm.commandExecuted = MOVE_CMD; /* Direction setup */ - STSPIN820_SetDirection(deviceId, direction); + STSPIN820_SetDirection(direction); - STSPIN820_ComputeSpeedProfile(deviceId, device_prm.stepsToTake); + STSPIN820_ComputeSpeedProfile(device_prm.stepsToTake); /* Motor activation */ - STSPIN820_StartMovement(deviceId); + STSPIN820_StartMovement(); } } -/******************************************************//** +/********************************************************** * @brief Immediatly stop the motor and disable the power bridge - * @param[in] deviceId Unused parameter * @retval None **********************************************************/ -void STSPIN820::STSPIN820_HardHiZ(uint8_t deviceId) +void STSPIN820::STSPIN820_HardHiZ(void) { /* Set inactive state */ device_prm.motionState = INACTIVE; /* Disable step clock */ - if (STSPIN820_Board_TimStckStop(handle &toggle_odd) == 0) + if (STSPIN820_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); + STSPIN820_Delay(DISABLE_DELAY); /* Set reference voltage to 0 */ - STSPIN820_SetTorque(0, CURRENT_TORQUE, 0); + STSPIN820_SetTorque(CURRENT_TORQUE, 0); /* Disable power bridges */ - STSPIN820_Board_Disable(); + STSPIN820_Disable_Power_Bridges(); /* Comeback to nominal step mode */ if (device_prm.stepModeLatched != device_prm.stepMode) { motor_step_mode_t StepMode = device_prm.stepModeLatched; - STSPIN820_SetStepMode(0, StepMode); + STSPIN820_SetStepMode(StepMode); device_prm.stepMode = device_prm.stepModeLatched; } @@ -683,37 +655,34 @@ 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) +void STSPIN820::STSPIN820_HardStop(void) { - 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) + if (STSPIN820_TimStckStop(&toggle_odd) == 0) { STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK); } /* Set holding torque */ - STSPIN820_ApplyTorque(deviceId, HOLD_TORQUE); + STSPIN820_ApplyTorque(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); + STSPIN820_SetStepMode(StepMode); device_prm.stepMode = device_prm.stepModeLatched; } @@ -723,34 +692,31 @@ } else if (device_prm.stopMode == HIZ_MODE) { - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); } else if (device_prm.stopMode == STANDBY_MODE) { - STSPIN820_PutDeviceInStandby(deviceId); + STSPIN820_PutDeviceInStandby(); } } -/******************************************************//** +/********************************************************** * @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) +void STSPIN820::STSPIN820_Move(motor_direction_t direction, uint32_t stepCount) { - deviceId = 0; - /* Exit from standby if needed */ if (device_prm.motionState == STANDBY) { - STSPIN820_ExitDeviceFromStandby(deviceId); + STSPIN820_ExitDeviceFromStandby(); } /* Deactivate motor if needed */ else if (device_prm.motionState != INACTIVE) { - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); } if (stepCount != 0) @@ -759,67 +725,64 @@ device_prm.commandExecuted = MOVE_CMD; /* Direction setup */ - STSPIN820_SetDirection(deviceId, direction); + STSPIN820_SetDirection(direction); - STSPIN820_ComputeSpeedProfile(deviceId, stepCount); + STSPIN820_ComputeSpeedProfile(stepCount); /* Motor activation */ - STSPIN820_StartMovement(deviceId); + STSPIN820_StartMovement(); } } -/******************************************************//** +/********************************************************** * @brief Put STSPIN820 device in standby (low power consumption) - * @param[in] deviceId Unused parameter * @retval None **********************************************************/ -void STSPIN820::STSPIN820_PutDeviceInStandby(uint8_t deviceId) +void STSPIN820::STSPIN820_PutDeviceInStandby(void) { /* Stop movement */ - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); /* Enter standby */ - STSPIN820_Board_Reset(); + STSPIN820_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) +void STSPIN820::STSPIN820_Run(motor_direction_t direction) { /* Exit from standby if needed */ if (device_prm.motionState == STANDBY) { - STSPIN820_ExitDeviceFromStandby(deviceId); + STSPIN820_ExitDeviceFromStandby(); } /* Deactivate motor if needed */ else if (device_prm.motionState != INACTIVE) { - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); } /* Direction setup */ - STSPIN820_SetDirection(deviceId,direction); + STSPIN820_SetDirection(direction); device_prm.commandExecuted = RUN_CMD; /* Motor activation */ - STSPIN820_StartMovement(deviceId); + STSPIN820_StartMovement(); } -/******************************************************//** +/********************************************************** * @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 STSPIN820::STSPIN820_SetAcceleration(uint16_t newAcc) { bool cmdExecuted = FALSE; if ((newAcc != 0)&& @@ -832,33 +795,31 @@ 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) +void STSPIN820::STSPIN820_SetDecayMode(motor_decay_mode_t decay) { if (decay == SLOW_DECAY) { - STSPIN820_Board_SetDecayGpio(1); + STSPIN820_SetDecayGpio(1); } else if (decay == MIXED_DECAY) { - STSPIN820_Board_SetDecayGpio(0); + STSPIN820_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 STSPIN820::STSPIN820_SetDeceleration(uint16_t newDec) { bool cmdExecuted = FALSE; if ((newDec != 0)&& @@ -871,65 +832,61 @@ 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) +void STSPIN820::STSPIN820_SetDirection(motor_direction_t dir) { if ((device_prm.motionState == INACTIVE)||\ (device_prm.motionState == STANDBY)) { device_prm.direction = dir; - STSPIN820_Board_SetDirectionGpio(dir); + STSPIN820_SetDirectionGpio(dir); } else if ((device_prm.commandExecuted&RUN_CMD)!=0) { - device_prm.commandExecuted|=STSPIN820_DIR_CHANGE_BIT_MASK; + device_prm.commandExecuted = (device_command_t) (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) +void STSPIN820::STSPIN820_SetHome(void) { 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) +void STSPIN820::STSPIN820_SetMark(void) { 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 STSPIN820::STSPIN820_SetMaxSpeed(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)))&&\ + ((newMaxSpeed>>STSPIN820_GetStepMode())<= STSPIN820_MAX_STCK_FREQ)))&&\ (device_prm.minSpeed <= newMaxSpeed) &&\ (((device_prm.motionState & INACTIVE) == INACTIVE)||\ (device_prm.commandExecuted == RUN_CMD))) @@ -940,15 +897,14 @@ 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 STSPIN820::STSPIN820_SetMinSpeed(uint16_t newMinSpeed) { bool cmdExecuted = FALSE; if ((newMinSpeed >= STSPIN820_MIN_STCK_FREQ)&& @@ -963,7 +919,7 @@ 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 @@ -982,15 +938,14 @@ } } -/******************************************************//** +/********************************************************** * @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) +bool STSPIN820::STSPIN820_SetStepMode(motor_step_mode_t stepMode) { /* Store step mode */ device_prm.stepMode = stepMode; @@ -1000,28 +955,28 @@ switch (stepMode) { case STEP_MODE_FULL: - STSPIN820_Board_SetFullStep(); + STSPIN820_SetFullStep(); break; case STEP_MODE_HALF: - STSPIN820_Board_SetModePins(1, 0, 0); + STSPIN820_SetModePins(1, 0, 0); break; case STEP_MODE_1_4: - STSPIN820_Board_SetModePins(0, 1, 0); + STSPIN820_SetModePins(0, 1, 0); break; case STEP_MODE_1_8: - STSPIN820_Board_SetModePins(1, 1, 0); + STSPIN820_SetModePins(1, 1, 0); break; case STEP_MODE_1_16: - STSPIN820_Board_SetModePins(0, 0, 1); + STSPIN820_SetModePins(0, 0, 1); break; case STEP_MODE_1_32: - STSPIN820_Board_SetModePins(1, 0, 1); + STSPIN820_SetModePins(1, 0, 1); break; case STEP_MODE_1_128: - STSPIN820_Board_SetModePins(0, 1, 1); + STSPIN820_SetModePins(0, 1, 1); break; case STEP_MODE_1_256: - STSPIN820_Board_SetModePins(1, 1, 1); + STSPIN820_SetModePins(1, 1, 1); break; default: return FALSE; @@ -1031,26 +986,24 @@ } -/******************************************************//** +/********************************************************** * @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) +void STSPIN820::STSPIN820_SetStopMode(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) +void STSPIN820::STSPIN820_SetTorque(motor_torque_mode_t torqueMode, uint8_t torqueValue) { device_prm.updateTorque = TRUE; if (torqueValue>100) @@ -1076,97 +1029,91 @@ } case CURRENT_TORQUE: device_prm.currentTorque = torqueValue; - STSPIN820_Board_PwmRefSetFreqAndDutyCycle(device_prm.refPwmFreq,torqueValue); + STSPIN820_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) +void STSPIN820::STSPIN820_SetTorqueBoostEnable(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] (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) +void STSPIN820::STSPIN820_SetTorqueBoostThreshold(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 STSPIN820::STSPIN820_SoftStop(void) { bool cmdExecuted = FALSE; if ((device_prm.motionState & INACTIVE) != INACTIVE) { - device_prm.commandExecuted |= STSPIN820_SOFT_STOP_BIT_MASK; + device_prm.commandExecuted = (device_command_t) (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) +uint32_t STSPIN820::STSPIN820_VrefPwmGetFreq(void) { 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) +void STSPIN820::STSPIN820_VrefPwmSetFreq(uint32_t newFreq) { device_prm.refPwmFreq = newFreq; - STSPIN820_Board_PwmRefSetFreqAndDutyCycle(newFreq,device_prm.currentTorque); + STSPIN820_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) +void STSPIN820::STSPIN820_WaitWhileActive(void) { /* Wait while motor is running */ - while (((STSPIN820_GetDeviceState(deviceId)&INACTIVE)!=INACTIVE)||\ - (((STSPIN820_GetDeviceState(deviceId)&INACTIVE)==INACTIVE)&&(toggle_odd!=0))); + while (((STSPIN820_GetDeviceState()&INACTIVE)!=INACTIVE)||\ + (((STSPIN820_GetDeviceState()&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) +void STSPIN820::STSPIN820_ApplySpeed(uint16_t newSpeed) { if (device_prm.torqueBoostEnable != FALSE) { @@ -1184,8 +1131,8 @@ { if ((device_prm.sequencerPosition & 0xFF) == 0x80) { - STSPIN820_Board_SetFullStep(); - device_prm.stepMode = STEP_MODE_FULL; + STSPIN820_SetFullStep(); + device_prm.stepMode = (motor_step_mode_t) STEP_MODE_FULL; device_prm.accu >>= device_prm.stepModeLatched; newSpeed >>= device_prm.stepModeLatched; } @@ -1197,7 +1144,7 @@ ((device_prm.stepsToTake-device_prm.relativePos)<=\ (1<<device_prm.stepModeLatched)))) { - STSPIN820_SetStepMode(0, device_prm.stepModeLatched); + STSPIN820_SetStepMode(device_prm.stepModeLatched); device_prm.stepMode = device_prm.stepModeLatched; device_prm.accu <<= device_prm.stepModeLatched; newSpeed <<= device_prm.stepModeLatched; @@ -1206,7 +1153,7 @@ else if (device_prm.stepMode != device_prm.stepModeLatched) { //torqueBoostEnable has just been disabled - STSPIN820_SetStepMode(0, device_prm.stepModeLatched); + STSPIN820_SetStepMode(device_prm.stepModeLatched); device_prm.stepMode = device_prm.stepModeLatched; device_prm.accu <<= device_prm.stepModeLatched; newSpeed <<= device_prm.stepModeLatched; @@ -1222,18 +1169,17 @@ } device_prm.speed = newSpeed; - STSPIN820_Board_TimStckSetFreq(newSpeed); + STSPIN820_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) +void STSPIN820::STSPIN820_ApplyTorque(motor_torque_mode_t torqueMode) { uint8_t torqueValue = 0; device_prm.updateTorque = FALSE; @@ -1257,12 +1203,11 @@ return; //ignore error } torqueValue = device_prm.currentTorque; - STSPIN820_Board_PwmRefSetFreqAndDutyCycle(device_prm.refPwmFreq,torqueValue); + STSPIN820_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, @@ -1274,7 +1219,7 @@ * 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) +void STSPIN820::STSPIN820_ComputeSpeedProfile(uint32_t nbSteps) { uint32_t reqAccSteps; uint32_t reqDecSteps; @@ -1325,7 +1270,7 @@ } } -/******************************************************//** +/********************************************************** * @brief Handlers of the flag interrupt which calls the user callback (if defined) * @retval None **********************************************************/ @@ -1337,7 +1282,7 @@ } } -/******************************************************//** +/********************************************************** * @brief Set the parameters of the device whose values are not defined in * STSPIN820_config.h * @retval None @@ -1366,7 +1311,7 @@ } } -/******************************************************//** +/********************************************************** * @brief Set the parameters of the device to values of initDevicePrm structure * @param pInitDevicePrm structure containing values to initialize the device * parameters @@ -1374,45 +1319,50 @@ **********************************************************/ void STSPIN820::STSPIN820_SetDeviceParamsToGivenValues(STSPIN820_init_t* pInitDevicePrm) { + int i = 100; + printf("%d\r\n", i++); device_prm.motionState = STANDBY;; - if (STSPIN820_SetAcceleration(0,pInitDevicePrm->acceleration)==FALSE) + if (STSPIN820_SetAcceleration(pInitDevicePrm->acceleration)==FALSE) { STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_ACCELERATION); } - if (STSPIN820_SetDeceleration(0,pInitDevicePrm->deceleration)==FALSE) + printf("%d\r\n", i++); + if (STSPIN820_SetDeceleration(pInitDevicePrm->deceleration)==FALSE) { STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_DECELERATION); } - if (STSPIN820_SetMaxSpeed(0,pInitDevicePrm->maxSpeed)==FALSE) + printf("%d\r\n", i++); + if (STSPIN820_SetMaxSpeed(pInitDevicePrm->maxSpeed)==FALSE) { STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_MAX_SPEED); } - if (STSPIN820_SetMinSpeed(0,pInitDevicePrm->minSpeed)==FALSE) + printf("%d\r\n", i++); + if (STSPIN820_SetMinSpeed(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); + printf("%d\r\n", i++); + STSPIN820_VrefPwmSetFreq(pInitDevicePrm->vrefPwmFreq); + STSPIN820_SetTorque(ACC_TORQUE,pInitDevicePrm->accelTorque); + STSPIN820_SetTorque(DEC_TORQUE,pInitDevicePrm->decelTorque); + STSPIN820_SetTorque(RUN_TORQUE,pInitDevicePrm->runTorque); + STSPIN820_SetTorque(HOLD_TORQUE,pInitDevicePrm->holdTorque); device_prm.torqueBoostEnable = pInitDevicePrm->torqueBoostEnable; device_prm.torqueBoostSpeedThreshold = pInitDevicePrm->torqueBoostSpeedThreshold; - STSPIN820_SetStopMode(0,pInitDevicePrm->stopMode); - + STSPIN820_SetStopMode(pInitDevicePrm->stopMode); + printf("%d\r\n", i++); STSPIN820_SetDeviceParamsOtherValues(); - + printf("%d\r\n", i++); /* Eventually deactivate motor */ if ((device_prm.motionState != INACTIVE)&&\ (device_prm.motionState != STANDBY)) { - STSPIN820_HardHiZ(0); + STSPIN820_HardHiZ(); } /* Enter standby */ - STSPIN820_Board_Reset(); + STSPIN820_Reset(); /* Reset the microstepping sequencer position */ device_prm.sequencerPosition = 0; @@ -1422,20 +1372,20 @@ device_prm.markPosition = 0; /* Set predefined step mode */ - STSPIN820_SetStepMode(0, pInitDevicePrm->stepMode); + STSPIN820_SetStepMode(pInitDevicePrm->stepMode); /* Wait */ - STSPIN820_Board_Delay(SELECT_STEP_MODE_DELAY); + STSPIN820_Delay(SELECT_STEP_MODE_DELAY); /* Exit standby */ - STSPIN820_Board_ReleaseReset(); + STSPIN820_ReleaseReset(); /* Let a delay after reset release*/ - STSPIN820_Board_Delay(AFTER_STANDBY_EXIT_DEAD_TIME); + STSPIN820_Delay(AFTER_STANDBY_EXIT_DEAD_TIME); } -/******************************************************//** +/********************************************************** * @brief Sets the parameters of the device to predefined values * from STSPIN820_config.h * @retval None @@ -1444,31 +1394,31 @@ { device_prm.motionState = STANDBY; - if (STSPIN820_SetAcceleration(0,STSPIN820_CONF_PARAM_ACC)==FALSE) + if (STSPIN820_SetAcceleration(STSPIN820_CONF_PARAM_ACC)==FALSE) { STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_ACCELERATION); } - if (STSPIN820_SetDeceleration(0,STSPIN820_CONF_PARAM_DEC)==FALSE) + if (STSPIN820_SetDeceleration(STSPIN820_CONF_PARAM_DEC)==FALSE) { STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_DECELERATION); } - if (STSPIN820_SetMaxSpeed(0,STSPIN820_CONF_PARAM_RUNNING_SPEED)==FALSE) + if (STSPIN820_SetMaxSpeed(STSPIN820_CONF_PARAM_RUNNING_SPEED)==FALSE) { STSPIN820_ErrorHandler(STSPIN820_ERROR_SET_MAX_SPEED); } - if (STSPIN820_SetMinSpeed(0,STSPIN820_CONF_PARAM_MIN_SPEED)==FALSE) + if (STSPIN820_SetMinSpeed(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); + STSPIN820_VrefPwmSetFreq(STSPIN820_CONF_PARAM_REF_PWM_FREQUENCY); + STSPIN820_SetTorque(ACC_TORQUE,STSPIN820_CONF_PARAM_ACC_TORQUE); + STSPIN820_SetTorque(DEC_TORQUE,STSPIN820_CONF_PARAM_DEC_TORQUE); + STSPIN820_SetTorque(RUN_TORQUE,STSPIN820_CONF_PARAM_RUNNING_TORQUE); + STSPIN820_SetTorque(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_SetStopMode(STSPIN820_CONF_PARAM_AUTO_HIZ_STOP); STSPIN820_SetDeviceParamsOtherValues(); @@ -1476,11 +1426,11 @@ if ((device_prm.motionState != INACTIVE)&&\ (device_prm.motionState != STANDBY)) { - STSPIN820_HardHiZ(0); + STSPIN820_HardHiZ(); } /* Enter standby */ - STSPIN820_Board_Reset(); + STSPIN820_Reset(); /* Reset the microstepping sequencer position */ device_prm.sequencerPosition = 0; @@ -1490,30 +1440,27 @@ device_prm.markPosition = 0; /* Set predefined step mode */ - STSPIN820_SetStepMode(0, STSPIN820_CONF_PARAM_STEP_MODE); + STSPIN820_SetStepMode((motor_step_mode_t) STSPIN820_CONF_PARAM_STEP_MODE); /* Wait */ - STSPIN820_Board_Delay(SELECT_STEP_MODE_DELAY); + STSPIN820_Delay(SELECT_STEP_MODE_DELAY); /* Exit standby */ - STSPIN820_Board_ReleaseReset(); + STSPIN820_ReleaseReset(); /* Let a delay after reset release*/ - STSPIN820_Board_Delay(AFTER_STANDBY_EXIT_DEAD_TIME); + STSPIN820_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) +void STSPIN820::STSPIN820_StartMovement(void) { - deviceId = 0; - /* Enable STSPIN820 powerstage */ - STSPIN820_Enable(deviceId); + STSPIN820_Enable(); toggle_odd = 0; device_prm.accu = 0; device_prm.relativePos = 0; @@ -1521,34 +1468,32 @@ (device_prm.commandExecuted != RUN_CMD)) { device_prm.motionState = DECELERATING; - STSPIN820_ApplyTorque(deviceId, DEC_TORQUE); + STSPIN820_ApplyTorque(DEC_TORQUE); } else { device_prm.motionState = ACCELERATING; - STSPIN820_ApplyTorque(deviceId, ACC_TORQUE); + STSPIN820_ApplyTorque(ACC_TORQUE); } - STSPIN820_Board_PwmRefStart(); + STSPIN820_PwmRefStart(); /* Initialize the step clock timer */ - STSPIN820_Board_TimStckInit(); + STSPIN820_TimStckInit(); /* Program the step clock */ - STSPIN820_Board_TimStckCompareInit(); - STSPIN820_ApplySpeed(deviceId, device_prm.minSpeed); - STSPIN820_Board_TimStckStart(); + STSPIN820_TimStckCompareInit(); + STSPIN820_ApplySpeed(device_prm.minSpeed); + STSPIN820_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) +void STSPIN820::STSPIN820_StepClockHandler(void) { uint32_t stepModeShift = device_prm.stepModeLatched - device_prm.stepMode; uint16_t tmp; - deviceId = 0; - + if (device_prm.motionState == STANDBYTOINACTIVE) { if (toggle_odd != 0) @@ -1556,7 +1501,7 @@ toggle_odd = 0; if (device_prm.sequencerPosition == 0) { - if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0) + if (STSPIN820_TimStckStop(&toggle_odd) == 0) { STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK); } @@ -1569,7 +1514,7 @@ tmp = (1 << (STEP_MODE_1_256-device_prm.stepMode)); device_prm.sequencerPosition -= tmp; } - STSPIN820_Board_TimStckSetFreq(STSPIN820_MAX_STCK_FREQ); + STSPIN820_TimStckSetFreq(STSPIN820_MAX_STCK_FREQ); return; } @@ -1620,14 +1565,14 @@ device_prm.motionState = DECELERATING; device_prm.accu = 0; /* Apply decelerating torque */ - STSPIN820_ApplyTorque(deviceId, DEC_TORQUE); + STSPIN820_ApplyTorque(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); + STSPIN820_ApplyTorque(RUN_TORQUE); } else { @@ -1657,7 +1602,7 @@ if (device_prm.updateTorque!=FALSE) { /* Apply accelerating torque */ - STSPIN820_ApplyTorque(deviceId, ACC_TORQUE); + STSPIN820_ApplyTorque(ACC_TORQUE); } } break; @@ -1669,7 +1614,7 @@ if (device_prm.updateTorque!=FALSE) { /* Apply accelerating torque */ - STSPIN820_ApplyTorque(deviceId, RUN_TORQUE); + STSPIN820_ApplyTorque(RUN_TORQUE); } if (((device_prm.commandExecuted&(STSPIN820_SOFT_STOP_BIT_MASK|STSPIN820_DIR_CHANGE_BIT_MASK))!=0)||\ ((device_prm.commandExecuted==MOVE_CMD)&&\ @@ -1680,14 +1625,14 @@ device_prm.motionState = DECELERATING; device_prm.accu = 0; /* Apply decelerating torque */ - STSPIN820_ApplyTorque(deviceId, DEC_TORQUE); + STSPIN820_ApplyTorque(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); + STSPIN820_ApplyTorque(ACC_TORQUE); } break; } @@ -1703,33 +1648,33 @@ /* Motion process complete */ if ((device_prm.commandExecuted&STSPIN820_DIR_CHANGE_BIT_MASK)!=0) { - device_prm.commandExecuted&=~STSPIN820_DIR_CHANGE_BIT_MASK; + device_prm.commandExecuted = (device_command_t) (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); + STSPIN820_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); + STSPIN820_ApplyTorque(ACC_TORQUE); break; } } if (device_prm.stopMode==HOLD_MODE) { - STSPIN820_HardStop(deviceId); + STSPIN820_HardStop(); } else if (device_prm.stopMode==STANDBY_MODE) { - STSPIN820_PutDeviceInStandby(deviceId); + STSPIN820_PutDeviceInStandby(); } else { - STSPIN820_HardHiZ(deviceId); + STSPIN820_HardHiZ(); } } else if ((device_prm.commandExecuted==RUN_CMD)&& @@ -1737,7 +1682,7 @@ { device_prm.motionState = STEADY; /* Apply running torque */ - STSPIN820_ApplyTorque(deviceId, RUN_TORQUE); + STSPIN820_ApplyTorque(RUN_TORQUE); } else { @@ -1772,7 +1717,7 @@ if (device_prm.updateTorque!=FALSE) { /* Apply decelerating torque */ - STSPIN820_ApplyTorque(deviceId, DEC_TORQUE); + STSPIN820_ApplyTorque(DEC_TORQUE); } } } @@ -1786,14 +1731,15 @@ } if ((device_prm.motionState & INACTIVE) != INACTIVE) { - STSPIN820_ApplySpeed(deviceId, device_prm.speed); + STSPIN820_ApplySpeed(device_prm.speed); } else { - if (STSPIN820_Board_TimStckStop(&toggle_odd) == 0) + if (STSPIN820_TimStckStop(&toggle_odd) == 0) { STSPIN820_ErrorHandler(STSPIN820_ERROR_STEP_CLOCK); } } } + /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- a/Components/STSPIN820/STSPIN820.h Fri Apr 27 17:02:08 2018 +0000 +++ b/Components/STSPIN820/STSPIN820.h Thu Sep 27 14:05:44 2018 +0000 @@ -1,40 +1,41 @@ /** - ****************************************************************************** - * @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. - * - ****************************************************************************** - */ + ******************************************************************************* + * @file STSPIN820.h + * @author Davide Aliprandi, STMicroelectronics + * @version V1.0.0 + * @date May 3rd, 2018 + * @brief This file contains the class definition of an STSPIN820 Motor + * Control component. + * @note (C) COPYRIGHT 2018 STMicroelectronics + ******************************************************************************* + * @attention + * + * <h2><center>© COPYRIGHT(c) 2018 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 -----------------------------------------------*/ @@ -61,7 +62,6 @@ * Include here platform specific header files. * *----------------------------------------------------------------------------*/ #include "mbed.h" -#include "DevSPI.h" /* ACTION 2 ------------------------------------------------------------------* * Include here component specific header files. * *----------------------------------------------------------------------------*/ @@ -70,18 +70,18 @@ * Include here interface specific header files. * * * * Example: * - * #include "../Interfaces/Humidity.h" * - * #include "../Interfaces/Temperature.h" * + * #include "../Interfaces/Humidity.h" * + * #include "../Interfaces/Temperature.h" * *----------------------------------------------------------------------------*/ -#include "../Interfaces/Motor.h" +#include "StepperMotor.h" /* Classes -------------------------------------------------------------------*/ /** - * @brief Class representing a STSPIN820 component. + * @brief Class representing an STSPIN820 component. */ -class STSPIN820 : public Motor +class STSPIN820 : public StepperMotor { public: @@ -89,11 +89,35 @@ /** * @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. + * @param en_fault_irq pin name of the EN_FAULT pin of the component. + * @param standby_reset pin name of the STBY\RST pin of the component. + * @param direction pin name of the DIR pin of the component. + * @param ref_pwm pin name of the REF_PWM pin of the component. + * @param decay pin name of the DECAY pin of the component. + * @param step_clock pin name of the STCK pin of the component. + * @param mode1 pin name of the MODE1 pin of the component. + * @param mode2 pin name of the MODE2 pin of the component. + * @param mode3 pin name of the MODE3 pin of the component. */ - STSPIN820(PinName ssel, DevSPI &spi) : Motor(), ssel(ssel), dev_spi(spi) + STSPIN820(PinName en_fault_irq, PinName standby_reset, PinName direction, + PinName ref_pwm, PinName decay, PinName step_clock, + PinName mode1, PinName mode2, PinName mode3) : + StepperMotor(), + en_fault_irq(en_fault_irq), + standby_reset(standby_reset), + direction(direction), + ref_pwm(ref_pwm), + decay(decay), + step_clock(step_clock), + mode1(mode1), + mode2(mode2), + mode3(mode3) { + /* Checking stackability. */ + if (!(number_of_devices < MAX_NUMBER_OF_DEVICES)) { + error("Instantiation of the STSPIN820 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES); + } + /* ACTION 4 ----------------------------------------------------------* * Initialize here the component's member variables, one variable per * * line. * @@ -105,9 +129,9 @@ flag_interrupt_callback = 0; error_handler_callback = 0; toggle_odd = 0; - device_prm = 0; - number_of_devices = 0; - device_instance = 0; + pwm_period = 1; + pwm_duty_cycle = 0; + device_instance = number_of_devices++; } /** @@ -163,24 +187,24 @@ STSPIN820_FlagInterruptHandler(); } - virtual uint16_t get_acceleration(uint8_t deviceId) + virtual unsigned int get_acceleration(void) { - return (uint16_t) STSPIN820_GetAcceleration((uint8_t) deviceId); + return (unsigned int) STSPIN820_GetAcceleration(); } - virtual uint16_t get_current_speed(uint8_t deviceId) + virtual unsigned int get_speed(void) { - return (uint16_t) STSPIN820_GetCurrentSpeed((uint8_t) deviceId); + return (unsigned int) STSPIN820_GetCurrentSpeed(); } - virtual uint16_t get_deceleration(uint8_t deviceId) + virtual unsigned int get_deceleration(void) { - return (uint16_t) STSPIN820_GetDeceleration((uint8_t) deviceId); + return (unsigned int) STSPIN820_GetDeceleration(); } - virtual motor_state_t get_device_state(uint8_t deviceId) + virtual motor_state_t get_device_state(void) { - return (motor_state_t) STSPIN820_GetDeviceState((uint8_t) deviceId); + return (motor_state_t) STSPIN820_GetDeviceState(); } virtual uint32_t get_fw_version(void) @@ -188,134 +212,156 @@ return (uint32_t) STSPIN820_GetFwVersion(); } - virtual int32_t get_mark(uint8_t deviceId) + virtual signed int get_mark(void) { - return (int32_t) STSPIN820_GetMark((uint8_t) deviceId); + return (signed int) STSPIN820_GetMark(); } - virtual uint16_t get_max_speed(uint8_t deviceId) + virtual unsigned int get_max_speed(void) { - return (uint16_t) STSPIN820_GetMaxSpeed((uint8_t) deviceId); + return (unsigned int) STSPIN820_GetMaxSpeed(); + } + + virtual unsigned int get_min_speed(void) + { + return (unsigned int) STSPIN820_GetMinSpeed(); } - virtual uint16_t get_min_speed(uint8_t deviceId) + virtual signed int get_position(void) { - return (uint16_t) STSPIN820_GetMinSpeed((uint8_t) deviceId); + return (signed int) STSPIN820_GetPosition(); } - virtual int32_t get_position(uint8_t deviceId) + virtual void go_home(void) { - return (int32_t) STSPIN820_GetPosition((uint8_t) deviceId); + STSPIN820_GoHome(); } - virtual void go_home(uint8_t deviceId) + virtual void go_mark(void) { - STSPIN820_GoHome((uint8_t) deviceId); + STSPIN820_GoMark(); + } + + virtual void go_to(signed int position) + { + STSPIN820_GoTo((signed int) position); } - virtual void go_mark(uint8_t deviceId) - { - STSPIN820_GoMark((uint8_t) deviceId); - } - - virtual void go_to(uint8_t deviceId, int32_t targetPosition) + /** + * @brief Stopping the motor through an immediate deceleration up to zero speed. + * @param None. + * @retval None. + */ + virtual void soft_stop(void) { - STSPIN820_GoTo((uint8_t) deviceId, (int32_t) targetPosition); - } - - virtual void hard_stop(uint8_t deviceId) - { - STSPIN820_HardStop((uint8_t) deviceId); + STSPIN820_SoftStop(); } - virtual void move(uint8_t deviceId, motor_direction_t direction, uint32_t stepCount) + /** + * @brief Stopping the motor through an immediate infinite deceleration. + * @param None. + * @retval None. + */ + virtual void hard_stop(void) { - STSPIN820_Move((uint8_t) deviceId, (motor_direction_t) direction, (uint32_t) stepCount); + STSPIN820_HardStop(); } - virtual void run(uint8_t deviceId, motor_direction_t direction) - { - STSPIN820_Run((uint8_t) deviceId, (motor_direction_t) direction); - } + /** + * @brief Disabling the power bridge after performing a deceleration to zero. + * @param None. + * @retval None. + */ + virtual void soft_hiz(void) {} - virtual bool set_acceleration(uint8_t deviceId, uint16_t newAcc) + /** + * @brief Disabling the power bridge immediately. + * @param None. + * @retval None. + */ + virtual void hard_hiz(void) { - 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); + STSPIN820_HardHiZ(); } - virtual void set_home(uint8_t deviceId) + virtual void move(direction_t direction, unsigned int steps) { - STSPIN820_SetHome((uint8_t) deviceId); + STSPIN820_Move((motor_direction_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (unsigned int) steps); } - virtual void set_mark(uint8_t deviceId) + virtual void run(direction_t direction) { - STSPIN820_SetMark((uint8_t) deviceId); + STSPIN820_Run((motor_direction_t) direction); + } + + virtual bool set_acceleration(unsigned int acceleration) + { + return (bool) STSPIN820_SetAcceleration((unsigned int) acceleration); } - virtual bool set_max_speed(uint8_t deviceId, uint16_t newMaxSpeed) + virtual bool set_deceleration(unsigned int deceleration) { - return (bool) STSPIN820_SetMaxSpeed((uint8_t) deviceId, (uint16_t) newMaxSpeed); + return (bool) STSPIN820_SetDeceleration((unsigned int) deceleration); } - virtual bool set_min_speed(uint8_t deviceId, uint16_t newMinSpeed) + virtual void set_home(void) { - return (bool) STSPIN820_SetMinSpeed((uint8_t) deviceId, (uint16_t) newMinSpeed); + STSPIN820_SetHome(); } - virtual bool soft_stop(uint8_t deviceId) + virtual void set_mark(void) { - return (bool) STSPIN820_SoftStop((uint8_t) deviceId); + STSPIN820_SetMark(); } - virtual void step_clock_handler(uint8_t deviceId) + virtual bool set_max_speed(unsigned int speed) { - STSPIN820_StepClockHandler((uint8_t) deviceId); + return (bool) STSPIN820_SetMaxSpeed((unsigned int) speed); } - virtual void wait_while_active(uint8_t deviceId) + virtual bool set_min_speed(unsigned int speed) { - STSPIN820_WaitWhileActive((uint8_t) deviceId); + return (bool) STSPIN820_SetMinSpeed((unsigned int) speed); } - virtual void cmd_disable(uint8_t deviceId) + virtual void wait_while_active(void) { - STSPIN820_Disable((uint8_t) deviceId); + STSPIN820_WaitWhileActive(); } - virtual void cmd_enable(uint8_t deviceId) + virtual void cmd_disable(void) { - STSPIN820_Enable((uint8_t) deviceId); + STSPIN820_Disable(); + } + + virtual void cmd_enable(void) + { + STSPIN820_Enable(); } - virtual bool select_step_mode(uint8_t deviceId, motor_step_mode_t stepMode) + virtual bool set_step_mode(step_mode_t step_mode) { - return (bool) STSPIN820_SetStepMode((uint8_t) deviceId, (motor_step_mode_t) stepMode); + return (bool) STSPIN820_SetStepMode((motor_step_mode_t) step_mode); } - virtual void set_direction(uint8_t deviceId, motor_direction_t direction) + virtual void set_direction(direction_t direction) { - STSPIN820_SetDirection((uint8_t) deviceId, (motor_direction_t) direction); + STSPIN820_SetDirection((motor_direction_t) direction); } - virtual void cmd_go_to_dir(uint8_t deviceId, motor_direction_t direction, int32_t targetPosition) + virtual void go_to_dir(direction_t direction, int32_t targetPosition) { - STSPIN820_GoToDir((uint8_t) deviceId, (motor_direction_t) direction, (int32_t) targetPosition); + STSPIN820_GoToDir((motor_direction_t) direction, (int32_t) targetPosition); } - virtual uint8_t check_status_hw(void) + virtual unsigned int get_status(void) { - return (uint8_t) STSPIN820_CheckStatusHw(); + return (unsigned int) STSPIN820_CheckStatusHw(); } - virtual void cmd_reset_device(uint8_t deviceId) + virtual void cmd_reset_device(void) { - STSPIN820_PutDeviceInStandby((uint8_t) deviceId); + STSPIN820_PutDeviceInStandby(); } virtual uint8_t get_nb_devices(void) @@ -323,89 +369,79 @@ return (uint8_t) STSPIN820_GetNbDevices(); } - virtual void error_handler(uint16_t error) + virtual uint32_t get_bridge_input_pwm_freq(void) { - STSPIN820_ErrorHandler((uint16_t) error); + return (uint32_t) STSPIN820_VrefPwmGetFreq(); } - virtual uint32_t get_bridge_input_pwm_freq(uint8_t deviceId) + virtual void set_bridge_input_pwm_freq(uint32_t newFreq) { - return (uint32_t) STSPIN820_VrefPwmGetFreq((uint8_t) deviceId); + STSPIN820_VrefPwmSetFreq((uint32_t) newFreq); } - virtual void set_bridge_input_pwm_freq(uint8_t deviceId, uint32_t newFreq) + virtual void set_stop_mode(motor_stop_mode_t stopMode) { - STSPIN820_VrefPwmSetFreq((uint8_t) deviceId, (uint32_t) newFreq); + STSPIN820_SetStopMode((motor_stop_mode_t) stopMode); } - virtual void set_stop_mode(uint8_t deviceId, motor_stop_mode_t stopMode) + virtual motor_stop_mode_t get_stop_mode(void) { - 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); + return (motor_stop_mode_t) STSPIN820_GetStopMode(); } - virtual void set_decay_mode(uint8_t deviceId, motor_decay_mode_t decayMode) + virtual void set_decay_mode(motor_decay_mode_t decayMode) { - STSPIN820_SetDecayMode((uint8_t) deviceId, (motor_decay_mode_t) decayMode); + STSPIN820_SetDecayMode((motor_decay_mode_t) decayMode); } - virtual motor_decay_mode_t get_decay_mode(uint8_t deviceId) + virtual motor_decay_mode_t get_decay_mode(void) { - return (motor_decay_mode_t) STSPIN820_GetDecayMode((uint8_t) deviceId); + return (motor_decay_mode_t) STSPIN820_GetDecayMode(); } - virtual motor_step_mode_t get_step_mode(uint8_t deviceId) + virtual motor_step_mode_t get_step_mode(void) { - return (motor_step_mode_t) STSPIN820_GetStepMode((uint8_t) deviceId); + return (motor_step_mode_t) STSPIN820_GetStepMode(); } - virtual motor_direction_t get_direction(uint8_t deviceId) + virtual direction_t get_direction(void) { - return (motor_direction_t) STSPIN820_GetDirection((uint8_t) deviceId); + return (direction_t) (STSPIN820_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD); } - virtual void exit_device_from_reset(uint8_t deviceId) + virtual void exit_device_from_reset(void) { - STSPIN820_ExitDeviceFromStandby((uint8_t) deviceId); + STSPIN820_ExitDeviceFromStandby(); } - virtual void set_torque(uint8_t deviceId, motor_torque_mode_t torqueMode, uint8_t torqueValue) + virtual void set_torque(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); + STSPIN820_SetTorque((motor_torque_mode_t) torqueMode, (uint8_t) torqueValue); } - virtual bool set_nb_devices(uint8_t deviceId) + virtual uint8_t get_torque(motor_torque_mode_t torqueMode) { - return (bool) STSPIN820_SetNbDevices((uint8_t) deviceId); + return (uint8_t) STSPIN820_GetTorque((motor_torque_mode_t) torqueMode); } - virtual void set_torque_boost_enable(uint8_t deviceId, bool enable) + virtual void set_torque_boost_enable(bool enable) { - STSPIN820_SetTorqueBoostEnable((uint8_t) deviceId, (bool) enable); + STSPIN820_SetTorqueBoostEnable((bool) enable); } - virtual bool get_torque_boost_enable(uint8_t deviceId) + virtual bool get_torque_boost_enable(void) { - return (bool) STSPIN820_GetTorqueBoostEnable((uint8_t) deviceId); + return (bool) STSPIN820_GetTorqueBoostEnable(); } - virtual void set_torque_boost_threshold(uint8_t deviceId, uint16_t speedThreshold) + virtual void set_torque_boost_threshold(uint16_t speedThreshold) { - STSPIN820_SetTorqueBoostThreshold((uint8_t) deviceId, (uint16_t) speedThreshold); + STSPIN820_SetTorqueBoostThreshold((uint16_t) speedThreshold); } - virtual uint16_t get_torque_boost_threshold(uint8_t deviceId) + virtual uint16_t get_torque_boost_threshold(void) { - return (uint16_t) STSPIN820_GetTorqueBoostThreshold((uint8_t) deviceId); + return (uint16_t) STSPIN820_GetTorqueBoostThreshold(); } @@ -440,6 +476,36 @@ * feature_irq.disable_irq(); * * } * *------------------------------------------------------------------------*/ + /** + * @brief Attaching an interrupt handler to the FLAG interrupt. + * @param fptr An interrupt handler. + * @retval None. + */ + void attach_en_fault_irq(void (*fptr)(void)) + { + en_fault_irq.mode(PullUp); + en_fault_irq.fall(fptr); + } + + /** + * @brief Enabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void enable_en_fault_irq(void) + { + en_fault_irq.enable_irq(); + } + + /** + * @brief Disabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void disable_en_fault_irq(void) + { + en_fault_irq.disable_irq(); + } protected: @@ -461,244 +527,396 @@ * 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); + //Apply speed + void STSPIN820_ApplySpeed(uint16_t newSpeed); + //Apply torque + void STSPIN820_ApplyTorque(motor_torque_mode_t torqueMode); + //Compute speed profile + void STSPIN820_ComputeSpeedProfile(uint32_t nbSteps); + //Handler for the flag interrupt void STSPIN820_FlagInterruptHandler(void); + //Set device paramenters to other values void STSPIN820_SetDeviceParamsOtherValues(void); + //Set device paramenters to given values void STSPIN820_SetDeviceParamsToGivenValues(STSPIN820_init_t* initDevicePrm); + //Set device paramenters to predefined values 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 + //Start moving + void STSPIN820_StartMovement(void); + //Handle the step clock + void STSPIN820_StepClockHandler(void); + //Start the STSPIN820 library + status_t STSPIN820_Init(void *init); + //Read Id to get driver instance + status_t STSPIN820_ReadId(uint8_t *id); + //Attach a user callback to the error handler + void STSPIN820_AttachErrorHandler(void (*callback)(uint16_t)); + //Attach a user callback to the flag Interrupt + void STSPIN820_AttachFlagInterrupt(void (*callback)(void)); + //Check if STSPIN820 has a fault by reading EN pin position + uint8_t STSPIN820_CheckStatusHw(void); + //Disable the power stage of the specified device + void STSPIN820_Disable(void); + //Enable the power stage of the specified device + void STSPIN820_Enable(void); + //Error handler which calls the user callback + void STSPIN820_ErrorHandler(uint16_t error); + //Exit STSPIN820 device from standby + void STSPIN820_ExitDeviceFromStandby(void); + //Return the acceleration in pps^2 + uint16_t STSPIN820_GetAcceleration(void); + //Return the current speed in pps + uint16_t STSPIN820_GetCurrentSpeed(void); + //Return the device decay mode + motor_decay_mode_t STSPIN820_GetDecayMode(void); + //Return the deceleration in pps^2 + uint16_t STSPIN820_GetDeceleration(void); + //Return the device state + motor_state_t STSPIN820_GetDeviceState(void); + //Get the motor current direction + motor_direction_t STSPIN820_GetDirection(void); + //Return the FW version + uint32_t STSPIN820_GetFwVersion(void); + //Return the mark position + int32_t STSPIN820_GetMark(void); + //Return the max speed in pps + uint16_t STSPIN820_GetMaxSpeed(void); + //Return the min speed in pps + uint16_t STSPIN820_GetMinSpeed(void); + //Return the nupber of devices + uint8_t STSPIN820_GetNbDevices(void); + //Return the ABS_POSITION (32b signed) + int32_t STSPIN820_GetPosition(void); + //Get the motor step mode + motor_step_mode_t STSPIN820_GetStepMode(void); + //Get the selected mode to stop the motor + motor_stop_mode_t STSPIN820_GetStopMode(void); + //Get the torque + uint8_t STSPIN820_GetTorque(motor_torque_mode_t torqueMode); + //Get the torque boost feature status + bool STSPIN820_GetTorqueBoostEnable(void); + //Get the torque boost threshold + uint16_t STSPIN820_GetTorqueBoostThreshold(void); + //Move to the home position + void STSPIN820_GoHome(void); + //Move to the Mark position + void STSPIN820_GoMark(void); + //Go to the specified position + void STSPIN820_GoTo(int32_t targetPosition); + //Go to the specified position using the specified direction + void STSPIN820_GoToDir(motor_direction_t direction, int32_t targetPosition); + //Stop the motor and keep holding torque + void STSPIN820_HardStop(void); + //Stop the motor and disable the power bridge + void STSPIN820_HardHiZ(void); + //Move the motor of the specified number of steps + void STSPIN820_Move(motor_direction_t direction, uint32_t stepCount); + //Put STSPIN820 device in standby (low power consumption) + void STSPIN820_PutDeviceInStandby(void); + //Run the motor + void STSPIN820_Run(motor_direction_t direction); + //Set the acceleration in pps^2 + bool STSPIN820_SetAcceleration(uint16_t newAcc); + //Set the deceleration in pps^2 + bool STSPIN820_SetDeceleration(uint16_t newDec); + //Set the STSPIN820 decay mode pin + void STSPIN820_SetDecayMode(motor_decay_mode_t decay); + //Set the STSPIN820 direction pin + void STSPIN820_SetDirection(motor_direction_t direction); + //Set current position to be the home position + void STSPIN820_SetHome(void); + //Set current position to be the mark position + void STSPIN820_SetMark(void); + //Set the max speed in pps + bool STSPIN820_SetMaxSpeed(uint16_t newMaxSpeed); + //Set the min speed in pps + bool STSPIN820_SetMinSpeed(uint16_t newMinSpeed); + //Set the number of devices 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 ***/ + //Step mode selection + bool STSPIN820_SetStepMode(motor_step_mode_t stepMode); + //Select the mode to stop the motor + void STSPIN820_SetStopMode(motor_stop_mode_t stopMode); + //Set the torque + void STSPIN820_SetTorque(motor_torque_mode_t torqueMode, uint8_t torqueValue); + //Enable or disable the torque boost feature + void STSPIN820_SetTorqueBoostEnable(bool enable); + //Set the torque boost threshold + void STSPIN820_SetTorqueBoostThreshold(uint16_t speedThreshold); + //Progressively stop the motor by using the device deceleration and set deceleration torque + bool STSPIN820_SoftStop(void); + //Get the frequency of REF PWM of the specified device + uint32_t STSPIN820_VrefPwmGetFreq(void); + //Set the frequency of REF PWM of the specified device + void STSPIN820_VrefPwmSetFreq(uint32_t newFreq); + //Wait for the device state becomes Inactive + void STSPIN820_WaitWhileActive(void); - /** - * @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) + /** + * @brief Making the CPU wait. + * @param None. + * @retval None. + */ + void STSPIN820_Delay(uint32_t delay) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + wait_ms(delay); } - void STSPIN820_Board_Disable(void) + /** + * @brief Enabling the power bridges (leaving the output bridges at HiZ). + * @param None. + * @retval None. + */ + void STSPIN820_Enable_Power_Bridges(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + if (en_fault_irq.read() == 0) + { + //en_fault_irq.write(1); + wait_ms(BRIDGE_TURN_ON_DELAY_ms); + enable_en_fault_irq(); + } } - void STSPIN820_Board_DisableIrq(void) + /** + * @brief Disabling the power bridges (forcing the output bridges at HiZ). + * @param None. + * @retval None. + */ + void STSPIN820_Disable_Power_Bridges(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + disable_en_fault_irq(); + //en_fault_irq.write(0); } - uint32_t STSPIN820_Board_EN_AND_FAULT_PIN_GetState(void) + /** + * @brief Enabling interrupts. + * @param None. + * @retval None. + */ + void STSPIN820_EnableIrq(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ - return (uint32_t) 0; + __enable_irq(); } - void STSPIN820_Board_Enable(void) + /** + * @brief Disabling interrupts. + * @param None. + * @retval None. + */ + void STSPIN820_DisableIrq(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + __disable_irq(); } - void STSPIN820_Board_EnableIrq(void) + /** + * @brief Getting the state of the EN_FAULT pin. + * @param None. + * @retval None. + */ + uint32_t STSPIN820_EN_FAULT_PIN_GetState(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + return (uint32_t) en_fault_irq; } - void STSPIN820_Board_GpioInit(void) + /** + * @brief Initialising the GPIOs. + * @param None. + * @retval None. + */ + void STSPIN820_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. */ + disable_en_fault_irq(); + enable_en_fault_irq(); + STSPIN820_Reset(); } - void STSPIN820_Board_PwmRefSetFreqAndDutyCycle(uint32_t newFreq, uint8_t dutyCycle) + /** + * @brief Initialising the PWM. + * @param None. + * @retval None. + */ + void STSPIN820_PwmRefInit(void) {} + + /** + * @brief Setting the frequency and the duty-cycle of PWM. + * The frequency controls directly the speed of the device. + * The duty-cycle controls the torque of the device. + * @param frequency The frequency of PWM in Hz. + * @param duty_cycle The duty-cycle of PWM in [0..100]%. + * @retval None. + */ + void STSPIN820_PwmRefSetFreqAndDutyCycle(uint32_t frequency, uint8_t duty_cycle) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + pwm_period = (double) (1.0f / frequency); + pwm_duty_cycle = (double) duty_cycle; } - void STSPIN820_Board_PwmRefStart(void) + /** + * @brief Starting the PWM. + * @param None. + * @retval None. + */ + void STSPIN820_PwmRefStart(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + /* Setting the period and the duty-cycle of PWM. */ + ref_pwm.period(pwm_period); + ref_pwm.write(pwm_duty_cycle); + + /* Setting a callback with the same period of PWM's, to update the state machine. */ + ticker.attach(Callback<void()>(this, &STSPIN820::STSPIN820_StepClockHandler), pwm_period); } - void STSPIN820_Board_PwmRefStop(void) + /** + * @brief Stopping the PWM. + * @param None. + * @retval None. + */ + void STSPIN820_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. */ + ref_pwm.write(0.0f); + ticker.detach(); } - void STSPIN820_Board_Reset(void) + /** + * @brief Putting the device in standby mode. + * @param None. + * @retval None. + */ + void STSPIN820_ReleaseReset(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + standby_reset = 1; } - void STSPIN820_Board_SetDecayGpio(uint8_t gpioState) + /** + * @brief Putting the device in reset mode. + * @param None. + * @retval None. + */ + void STSPIN820_Reset(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + standby_reset = 0; } - uint8_t STSPIN820_Board_GetDecayGpio(void) + /** + * @brief Setting the decay mode pin state. + * @param gpio_state State of the decay mode pin ("0" to reset, "1" to set). + * @retval None. + */ + void STSPIN820_SetDecayGpio(uint8_t gpio_state) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ - return (uint8_t) 0; + decay = gpio_state; } - void STSPIN820_Board_SetDirectionGpio(uint8_t gpioState) + /** + * @brief Getting the decay mode pin state. + * @param None. + * @retval None. + */ + uint8_t STSPIN820_GetDecayGpio(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + return (decay != 0 ? 1 : 0); } - void STSPIN820_Board_SetFullStep(void) + /** + * @brief Setting the direction pin state. + * @param gpio_state State of the direction pin ("0" to reset, "1" to set). + * @retval None. + */ + void STSPIN820_SetDirectionGpio(uint8_t gpio_state) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + direction = gpio_state; } - bool STSPIN820_Board_SetModePins(uint8_t modePin1Level, uint8_t modePin2Level, uint8_t modePin3Level) + /** + * @brief Setting the full step mode. + * @param None. + * @retval None. + */ + void STSPIN820_SetFullStep(void) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ - return (bool) 0; + mode1 = mode2 = mode3 = 0; } - void STSPIN820_Board_TimStckCompareInit(void) - { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ - } - - void STSPIN820_Board_TimStckDeInit(void) + /** + * @brief Selecting the STSPIN820 mode1, mode2 and mode3 pins levels. + * @param mode1_level Level of the mode1 gpio (0 low, 1+ high). + * @param mode2_level Level of the mode2 gpio (0 low, 1+ high). + * @param mode3_level Level of the mode3 gpio (0 low, 1+ high). + * @retval None. + */ + void STSPIN820_SetModePins(uint8_t mode1_level, uint8_t mode2_level, uint8_t mode3_level) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ - } - - void STSPIN820_Board_TimStckInit(void) - { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + mode1 = (mode1_level != 0 ? 1 : 0); + mode2 = (mode2_level != 0 ? 1 : 0); + mode3 = (mode3_level != 0 ? 1 : 0); } - void STSPIN820_Board_TimStckSetFreq(uint16_t newFreq) + /** + * @brief Initializing step clock compare value. + * @param None. + * @retval None. + */ + void STSPIN820_TimStckCompareInit(void) {} + + /** + * @brief Deinitializing the timer used for the step clock. + * @param None. + * @retval None. + */ + void STSPIN820_TimStckDeInit(void) {} + + /** + * @brief Initializing the timer used for the step clock. + * @param None. + * @retval None. + */ + void STSPIN820_TimStckInit(void) {} + + /** + * @brief Setting the frequency of PWM. + * The frequency controls directly the speed of the device. + * @param frequency The frequency of PWM in Hz. + * @retval None. + */ + void STSPIN820_TimStckSetFreq(uint32_t frequency) { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ - } - - void STSPIN820_Board_TimStckStart(void) - { - /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */ + pwm_period = (double) (1.0f / frequency); } - uint8_t STSPIN820_Board_TimStckStop(uint8_t *pToggleOdd) + /** + * @brief Starting the step clock. + * @param None. + * @retval None. + */ + void STSPIN820_TimStckStart(void) {} + + /** + * @brief Stopping the step clock. + * @param p_toggle_odd Pointer to the volatile toggleOdd variable. + * @retval "1" if OK, "0" if the STCK pin is High (forbidden configuration). + */ + uint8_t STSPIN820_TimStckStop(volatile uint8_t *p_toggle_odd ) { - /* 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. */ + __disable_irq(); + if (*p_toggle_odd == 1) + { + __enable_irq(); + return 1; + } + if (step_clock != 0) + { + __enable_irq(); + return 0; + } + //HAL_TIM_OC_Stop_IT(&hTimerStepClock, BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); + __enable_irq(); + STSPIN820_TimStckDeInit(); + return 1; } @@ -713,6 +931,8 @@ * + mbed: * * InterruptIn feature_irq; * *------------------------------------------------------------------------*/ + /* En Fault Interrupt. */ + InterruptIn en_fault_irq; /* ACTION 10 -------------------------------------------------------------* * Declare here other pin related variables, if needed. * @@ -721,6 +941,32 @@ * + mbed: * * DigitalOut standby_reset; * *------------------------------------------------------------------------*/ + /* Standby/reset pin. */ + DigitalOut standby_reset; + + /* Direction of rotation pin. */ + DigitalOut direction; + + /* Pulse Width Modulation pin. */ + PwmOut ref_pwm; + + /* Decay pin. */ + DigitalOut decay; + + /* Step clock pin. */ + DigitalOut step_clock; + + /* Mode1 pin. */ + DigitalOut mode1; + + /* Mode2 pin. */ + DigitalOut mode2; + + /* Mode3 pin. */ + DigitalOut mode3; + + /* Timer to trigger the PWM callback at each PWM pulse. */ + Ticker ticker; /* ACTION 11 -------------------------------------------------------------* * Declare here communication related variables, if needed. * @@ -730,11 +976,6 @@ * DigitalOut ssel; * * DevSPI &dev_spi; * *------------------------------------------------------------------------*/ - /* Configuration. */ - DigitalOut ssel; - - /* IO Device. */ - DevSPI &dev_spi; /* ACTION 12 -------------------------------------------------------------* * Declare here identity related variables, if needed. * @@ -753,12 +994,17 @@ * int instance_id; * * static int number_of_instances; * *------------------------------------------------------------------------*/ + /* Data. */ void (*flag_interrupt_callback)(void); void (*error_handler_callback)(uint16_t error); - uint8_t toggle_odd; + volatile uint8_t toggle_odd; device_params_t device_prm; - uint8_t number_of_devices; + double pwm_period; + double pwm_duty_cycle; uint8_t device_instance; + + /* Static data. */ + static uint8_t number_of_devices; }; #endif /* __STSPIN820_CLASS_H */
--- a/Components/STSPIN820/STSPIN820_config.h Fri Apr 27 17:02:08 2018 +0000 +++ b/Components/STSPIN820/STSPIN820_config.h Thu Sep 27 14:05:44 2018 +0000 @@ -1,40 +1,41 @@ -/**************************************************************************//** - * @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. - * - ****************************************************************************** - */ +/** + ******************************************************************************* + * @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 -------------------------------------*/ @@ -110,6 +111,9 @@ /// REF PWM frequency (Hz) #define STSPIN820_CONF_PARAM_REF_PWM_FREQUENCY (100000) +/// MCU wait time after power bridges are enabled (ms) +#define BRIDGE_TURN_ON_DELAY_ms (10) + /** * @} */
--- a/Components/STSPIN820/STSPIN820_def.h Fri Apr 27 17:02:08 2018 +0000 +++ b/Components/STSPIN820/STSPIN820_def.h Thu Sep 27 14:05:44 2018 +0000 @@ -1,39 +1,41 @@ -/******************************************************//** - * @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. - * - ****************************************************************************** - */ +/** + ******************************************************************************* + * @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 -------------------------------------*/ @@ -235,7 +237,6 @@ motor_step_mode_t stepModeLatched; /// current stop mode motor_stop_mode_t stopMode; - } device_params_t; /* ACTION --------------------------------------------------------------------*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Components/STSPIN820/platform_dependent.c Thu Sep 27 14:05:44 2018 +0000 @@ -0,0 +1,708 @@ +/** + ****************************************************************************** + * @file stm32f0xx_nucleo_ihm14a1.c + * @author STM + * @version V1.1.0 + * @date April 10th, 2018 + * @brief BSP driver for x-nucleo-ihm14a1 Nucleo extension board + * (based on STSPIN820) + ****************************************************************************** +* @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. + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#if 0 + +/** @addtogroup BSP + * @{ + */ + +/** @defgroup STM32F0XX_NUCLEO_IHM14A1 STM32F0XX NUCLEO IHM14A1 + * @{ + */ + +/* Private constants ---------------------------------------------------------*/ + +/** @defgroup IHM14A1_Private_Constants IHM14A1 Private Constants + * @{ + */ + +/// Timer Prescaler for step clocks +#define TIMER_PRESCALER (32) + +/// MCU wait time after power bridges are enabled +#define BRIDGE_TURN_ON_DELAY (10) + +/// RC Filtering delay on the PWM +#define PWM_FILTER_TIME_CONSTANT (5) + +/** + * @} + */ + +/* Private variables ---------------------------------------------------------*/ + +/** @defgroup IHM14A1_Board_Private_Variables IHM14A1 Board Private Variables + * @{ + */ +/// Timer handler for step clock +TIM_HandleTypeDef hTimerStepClock; + +/// Step clock compare value +volatile uint32_t ccrValue; + +/// Timer handler for REF +TIM_HandleTypeDef hTimerPwm; + +/** + * @} + */ + +/** @defgroup IHM14A1_Board_Private_Function_Prototypes IHM14A1 Board Private Function Prototypes + * @{ + */ +//Delay of the requested number of milliseconds +void Stspin820_Board_Delay(uint32_t delay); +//Disable Irq +void Stspin820_Board_DisableIrq(void); +//Enable Irq +void Stspin820_Board_EnableIrq(void); +//Read the status of the Pin DECAY +uint8_t Stspin820_Board_GetDecayGpio(void); +//Initialise GPIOs used for STSPIN820 +void Stspin820_Board_GpioInit(void); +//Start step clock +void Stspin820_Board_TimStckStart(void); +//Set step clock frequency and start it +void Stspin820_Board_TimStckSetFreq(uint16_t newFreq); +//Sets the frequency and duty cycle of PWM used for the reference voltage +//generation +void Stspin820_Board_PwmRefSetFreqAndDutyCycle(uint32_t newFreq,uint8_t dutyCycle); +//Init the timer +void Stspin820_Board_TimStckInit(void); +//DeInit the timer +void Stspin820_Board_TimStckDeInit(void); +//Stop the timer +uint8_t Stspin820_Board_TimStckStop(volatile uint8_t *pToggleOdd); +//Init the reference voltage pwm +void Stspin820_Board_PwmRefInit(void); +//Start the reference voltage pwm +void Stspin820_Board_PwmRefStart(void); +//Stop the reference voltage pwm +void Stspin820_Board_PwmRefStop(void); +//Set the STSPIN820 STBY RESET pin and leave low consumption mode +void Stspin820_Board_ReleaseReset(void); +//Reset the STSPIN820 STBY RESET pin and force low consumption mode +void Stspin820_Board_Reset(void); +//Set the state of the direction GPIO +void Stspin820_Board_SetDirectionGpio(uint8_t gpioState); +//Set the state of the decay GPIO +void Stspin820_Board_SetDecayGpio(uint8_t gpioState); +//Enable the power bridges (leave the output bridges HiZ) +void Stspin820_Board_Enable(void); +//Disable the power bridges (force the output bridges HiZ) +void Stspin820_Board_Disable(void); +//Get the EN FAULT pin state +uint32_t Stspin820_Board_EN_AND_FAULT_PIN_GetState(void); +//Select the STSPIN820 mode1, mode2 and mode3 pins levels +uint8_t Stspin820_Board_SetModePins(uint8_t modePin1Level,\ + uint8_t modePin2Level,\ + uint8_t modePin3Level); +//Select Full Step mode +void Stspin820_Board_SetFullStep(void); +//Step clock compare value initialization +void Stspin820_Board_TimStckCompareInit(void); +/** + * @} + */ + +/** @defgroup IHM14A1_Board_Private_Functions IHM14A1 Board Private Functions + * @{ + */ + +/******************************************************//** + * @brief This function provides an accurate delay in milliseconds + * @param[in] delay time length in milliseconds + * @retval None + **********************************************************/ +void Stspin820_Board_Delay(uint32_t delay) +{ + HAL_Delay(delay); +} + +/******************************************************//** + * @brief This function disable the interruptions + * @retval None + **********************************************************/ +void Stspin820_Board_DisableIrq(void) +{ + __disable_irq(); +} + +/******************************************************//** + * @brief This function enable the interruptions + * @retval None + **********************************************************/ +void Stspin820_Board_EnableIrq(void) +{ + __enable_irq(); +} + +/******************************************************//** + * @brief Read the digital level of GPIO decay pin + * @retval Value of the decay pin + **********************************************************/ +uint8_t Stspin820_Board_GetDecayGpio(void) +{ + uint8_t statusPin; + + if(HAL_GPIO_ReadPin(BSP_MOTOR_CONTROL_BOARD_PORT_DECAY,\ + BSP_MOTOR_CONTROL_BOARD_PIN_DECAY) != GPIO_PIN_RESET) + { + statusPin = 1; + } + else + { + statusPin = 0; + } + + return statusPin; + +} + +/******************************************************//** + * @brief Initiliases the GPIOs used by the STSPIN820 + * @retval None + **********************************************************/ +void Stspin820_Board_GpioInit(void) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + + /* Configure STSPIN820 - EN pin --------------------------------------------*/ + /* When this pin is set low, it is configured just before as */ + /* GPIO_MODE_OUTPUT_PP with GPIO_NOPULL */ + /* When this pin is set high, it is just after configured for FAULT */ + /* as GPIO_MODE_IT_FALLING with GPIO_PULLUP */ + Stspin820_Board_Disable(); + + /* Set Priority of External Line Interrupt used for the FAULT interrupt*/ + HAL_NVIC_SetPriority(BSP_MOTOR_CONTROL_BOARD_IRQn_EN_AND_FAULT,\ + BSP_MOTOR_CONTROL_BOARD_PRIORITY_EN_AND_FAULT,\ + 0); + + /* Enable the External Line Interrupt used for the FAULT interrupt*/ + HAL_NVIC_EnableIRQ(BSP_MOTOR_CONTROL_BOARD_IRQn_EN_AND_FAULT); + + /* Configure STSPIN820 - MODE1 pin -----------------------------------------*/ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_MODE1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_MODE1, &GPIO_InitStruct); + + /* Configure STSPIN820 - MODE2 pin -----------------------------------------*/ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_MODE2; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_MODE2, &GPIO_InitStruct); + + /* Configure STSPIN820 - MODE3 pin -----------------------------------------*/ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_MODE3; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_MODE3, &GPIO_InitStruct); + + /* Configure STSPIN820 - DIR pin -------------------------------------------*/ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_DIR; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_DIR, &GPIO_InitStruct); + + /* Configure STSPIN820 - DECAY pin -------------------------------------------*/ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_DECAY; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_DECAY, &GPIO_InitStruct); + + /* Configure STSPIN820 - STBY/RESET pin ------------------------------------*/ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_STBY_RESET; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_STBY_RESET, &GPIO_InitStruct); + + Stspin820_Board_Reset(); + +} + +/******************************************************//** + * @brief Starts the step clock + * @retval None + * @note The frequency is directly the current speed of the device + **********************************************************/ +void Stspin820_Board_TimStckStart() +{ + /* Clear pending timer interrupt */ + if (BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK == TIM_CHANNEL_1) + { + __HAL_TIM_CLEAR_IT(&hTimerStepClock, TIM_IT_CC1); + } + else if (BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK == TIM_CHANNEL_2) + { + __HAL_TIM_CLEAR_IT(&hTimerStepClock, TIM_IT_CC2); + } + else if (BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK == TIM_CHANNEL_3) + { + __HAL_TIM_CLEAR_IT(&hTimerStepClock, TIM_IT_CC3); + } + else if (BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK == TIM_CHANNEL_4) + { + __HAL_TIM_CLEAR_IT(&hTimerStepClock, TIM_IT_CC4); + } + /* Start timer interrupts */ +#if (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_MAIN_OUTPUT) + if (hTimerStepClock.Instance->CCER == 0) + { + hTimerStepClock.Instance->CCER |=\ + (TIM_OCPOLARITY_LOW<<BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); + } + else + { + hTimerStepClock.Instance->CCER = 0; + } + HAL_TIM_OC_Start_IT(&hTimerStepClock,\ + BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); +#endif /* (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_MAIN_OUTPUT) */ + +#if (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_COMPLEMENTARY_OUTPUT) + if (hTimerStepClock.Instance->CCER == 0) + { + hTimerStepClock.Instance->CCER |=\ + (TIM_OCNPOLARITY_LOW<<BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); + } + else + { + hTimerStepClock.Instance->CCER = 0; + } + HAL_TIMEx_OCN_Start_IT(&hTimerStepClock,\ + BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); +#endif /* (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_COMPLEMENTARY_OUTPUT) */ + +#if (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_NO_OUTPUT) + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_TIM_STCK,\ + BSP_MOTOR_CONTROL_BOARD_PIN_TIM_STCK,\ + GPIO_PIN_RESET); + HAL_TIM_OC_Start_IT(&hTimerStepClock,\ + BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); +#endif /* (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_NO_OUTPUT) */ +} + +/******************************************************//** + * @brief Step clock compare value initialization + * @retval None + **********************************************************/ +void Stspin820_Board_TimStckCompareInit(void) +{ + ccrValue = hTimerStepClock.Instance->CNT; +} + +/******************************************************//** + * @brief Sets the frequency of step clock + * @param[in] newFreq in Hz + * @retval None + * @note The frequency is directly the current speed of the device + **********************************************************/ +void Stspin820_Board_TimStckSetFreq(uint16_t newFreq) +{ + ccrValue += (HAL_RCC_GetSysClockFreq()/\ + (TIMER_PRESCALER * 2 * (uint32_t)newFreq)); + __HAL_TIM_SET_COMPARE(&hTimerStepClock,\ + BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK,\ + ccrValue); +} + +/******************************************************//** + * @brief Starts the PWM used for the reference voltage generation + * @retval None + **********************************************************/ +void Stspin820_Board_PwmRefStart(void) +{ + HAL_TIM_PWM_Start_IT(&hTimerPwm, BSP_MOTOR_CONTROL_BOARD_CHAN_PWM_REF); + HAL_Delay(5*PWM_FILTER_TIME_CONSTANT); +} + +/******************************************************//** + * @brief Sets the frequency and duty cycle of PWM used for the reference + * voltage generation + * @param[in] newFreq in Hz + * @param[in] dutyCycle 0 - 100% + * @retval None + **********************************************************/ +void Stspin820_Board_PwmRefSetFreqAndDutyCycle(uint32_t newFreq, uint8_t dutyCycle) +{ + uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); + uint32_t period = (sysFreq/newFreq) - 1; + uint16_t pulseLength; + TIM_HandleTypeDef *pHTim; + + pHTim = &hTimerPwm; + pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_PWM_REF; + + if (dutyCycle > 100) dutyCycle = 100; + pulseLength = (uint16_t)((period * (100-(uint32_t)dutyCycle)) / 100); + + __HAL_TIM_SET_AUTORELOAD(pHTim, period); + __HAL_TIM_SET_COMPARE(pHTim, BSP_MOTOR_CONTROL_BOARD_CHAN_PWM_REF, pulseLength); +} + +/******************************************************//** + * @brief Initialises the PWM used for the reference voltage generation + * @retval None + **********************************************************/ +void Stspin820_Board_PwmRefInit(void) +{ + TIM_OC_InitTypeDef sConfigOC; + TIM_MasterConfigTypeDef sMasterConfig; + TIM_HandleTypeDef *pHTim; + + pHTim = &hTimerPwm; + + pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_PWM_REF; + pHTim->Init.CounterMode = TIM_COUNTERMODE_UP; + pHTim->Init.Prescaler = 0; + pHTim->Init.Period = 0; + pHTim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.Pulse = 0; + + HAL_TIM_PWM_Init(pHTim); + HAL_TIM_PWM_ConfigChannel(pHTim, &sConfigOC, BSP_MOTOR_CONTROL_BOARD_CHAN_PWM_REF); + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + HAL_TIMEx_MasterConfigSynchronization(pHTim, &sMasterConfig); +} + +/******************************************************//** + * @brief Initialises the timer used for the step clock + * @retval None + **********************************************************/ +void Stspin820_Board_TimStckInit(void) +{ + TIM_OC_InitTypeDef sConfigOC; + TIM_MasterConfigTypeDef sMasterConfig; + TIM_HandleTypeDef *pHTim; + uint32_t output = BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK; + + pHTim = &hTimerStepClock; + pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIM_STCK; + pHTim->Init.CounterMode = TIM_COUNTERMODE_UP; + pHTim->Init.Prescaler = TIMER_PRESCALER -1; + pHTim->Init.Period = 0xFFFF; + pHTim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + HAL_TIM_OC_Init(pHTim); + + if (output != TIMER_COMPLEMENTARY_OUTPUT) + { + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + } + else + { + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW; + } + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.Pulse = 0; + if (pHTim->Instance != TIM15) + { + /* Setting the OCMode to TIM_OCMODE_FORCED_ACTIVE ensures that on the */ + /* first interrupt occuring in the toggle mode, a rising edge will occur */ + sConfigOC.OCMode = TIM_OCMODE_FORCED_ACTIVE; + HAL_TIM_OC_ConfigChannel(pHTim, &sConfigOC, BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); + sConfigOC.OCMode = TIM_OCMODE_TOGGLE; + } + else + { + sConfigOC.OCMode = TIM_OCMODE_TIMING; + } + HAL_TIM_OC_ConfigChannel(pHTim, &sConfigOC, BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + HAL_TIMEx_MasterConfigSynchronization(pHTim, &sMasterConfig); +} + +/******************************************************//** + * @brief DeInitialises the timer used for the step clock + * @retval None + **********************************************************/ +void Stspin820_Board_TimStckDeInit(void) +{ + HAL_TIM_OC_DeInit(&hTimerStepClock); +} + +/******************************************************//** + * @brief Stops the timer + * @param[in] pToggleOdd pointer to the volatile toggleOdd variable + * @retval 1 if OK, 0 if STCK pin is high (forbidden configuration) + **********************************************************/ +uint8_t Stspin820_Board_TimStckStop(volatile uint8_t *pToggleOdd) +{ + __disable_irq(); + if (*pToggleOdd == 1) + { + __enable_irq(); + return 1; + } + if (HAL_GPIO_ReadPin(BSP_MOTOR_CONTROL_BOARD_PORT_TIM_STCK,\ + BSP_MOTOR_CONTROL_BOARD_PIN_TIM_STCK) != 0) + { + __enable_irq(); + return 0; + } +#if (BSP_MOTOR_CONTROL_BOARD_OUTPUT_TIM_STCK == TIMER_COMPLEMENTARY_OUTPUT) + HAL_TIMEx_OCN_Stop_IT(&hTimerStepClock, BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); +#else + HAL_TIM_OC_Stop_IT(&hTimerStepClock, BSP_MOTOR_CONTROL_BOARD_CHAN_TIM_STCK); +#endif + __enable_irq(); + /* DeInitialize the step clock timer */ + Stspin820_Board_TimStckDeInit(); + return 1; +} + +/******************************************************//** + * @brief Stops the PWM used for the reference voltage generation + * @retval None + **********************************************************/ +void Stspin820_Board_PwmRefStop(void) +{ + HAL_TIM_PWM_Stop_IT(&hTimerPwm, BSP_MOTOR_CONTROL_BOARD_CHAN_PWM_REF); +} + +/******************************************************//** + * @brief Releases the STSPIN820 reset (pin set to High) + * @retval None + **********************************************************/ +void Stspin820_Board_ReleaseReset(void) +{ + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_STBY_RESET,\ + BSP_MOTOR_CONTROL_BOARD_PIN_STBY_RESET,\ + GPIO_PIN_SET); +} + +/******************************************************//** + * @brief Resets the STSPIN820 (reset pin set to low) + * @retval None + **********************************************************/ +void Stspin820_Board_Reset(void) +{ + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_STBY_RESET,\ + BSP_MOTOR_CONTROL_BOARD_PIN_STBY_RESET,\ + GPIO_PIN_RESET); +} + +/******************************************************//** + * @brief Set the GPIO used for the decay mode + * @param[in] gpioState state of the direction gpio (0 to reset, 1 to set) + * @retval None + **********************************************************/ +void Stspin820_Board_SetDecayGpio(uint8_t gpioState) +{ + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_DECAY,\ + BSP_MOTOR_CONTROL_BOARD_PIN_DECAY,\ + (GPIO_PinState)gpioState); +} + +/******************************************************//** + * @brief Set the GPIO used for the direction + * @param[in] gpioState state of the direction gpio (0 to reset, 1 to set) + * @retval None + **********************************************************/ +void Stspin820_Board_SetDirectionGpio(uint8_t gpioState) +{ + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_DIR,\ + BSP_MOTOR_CONTROL_BOARD_PIN_DIR,\ + (GPIO_PinState)gpioState); +} + +/******************************************************//** + * @brief Disable the power bridges (Force the output bridges HiZ) + * the IHM14A1 board + * @retval None + **********************************************************/ +void Stspin820_Board_Disable(void) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + /* Configure the GPIO connected to EN pin as an output */ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_EN_AND_FAULT, &GPIO_InitStruct); + __disable_irq(); + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_EN_AND_FAULT,\ + BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT,\ + GPIO_PIN_RESET); + __HAL_GPIO_EXTI_CLEAR_IT(BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT); + __enable_irq(); +} + +/******************************************************//** + * @brief Enable the power bridges (leave the output bridges HiZ) + * @retval None + **********************************************************/ +void Stspin820_Board_Enable(void) +{ + GPIO_InitTypeDef GPIO_InitStruct; + if (HAL_GPIO_ReadPin(BSP_MOTOR_CONTROL_BOARD_PORT_EN_AND_FAULT,\ + BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT) == 0) + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_EN_AND_FAULT,\ + BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT,\ + GPIO_PIN_SET); + HAL_Delay(BRIDGE_TURN_ON_DELAY); + /* Configure the GPIO connected to EN pin to take interrupt */ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT; + GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PORT_EN_AND_FAULT, &GPIO_InitStruct); + __HAL_GPIO_EXTI_CLEAR_IT(BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT); + HAL_NVIC_ClearPendingIRQ(BSP_MOTOR_CONTROL_BOARD_IRQn_EN_AND_FAULT); + HAL_NVIC_EnableIRQ(BSP_MOTOR_CONTROL_BOARD_IRQn_EN_AND_FAULT); + } +} + +/******************************************************//** + * @brief Returns the EN FAULT pin state. + * @retval The EN FAULT pin value. + **********************************************************/ +uint32_t Stspin820_Board_EN_AND_FAULT_PIN_GetState(void) +{ + return HAL_GPIO_ReadPin(BSP_MOTOR_CONTROL_BOARD_PORT_EN_AND_FAULT, BSP_MOTOR_CONTROL_BOARD_PIN_EN_AND_FAULT); +} + +/******************************************************//** + * @brief Select the STSPIN820 mode1, mode2 and mode3 pins levels. + * @param[in] modePin1Level level of the mode1 gpio (0 low, 1+ high) + * @param[in] modePin2Level level of the mode2 gpio (0 low, 1+ high) + * @param[in] modePin3Level level of the mode3 gpio (0 low, 1+ high) + * @retval 1 + **********************************************************/ +uint8_t Stspin820_Board_SetModePins(uint8_t modePin1Level,\ + uint8_t modePin2Level,\ + uint8_t modePin3Level) +{ + + + if (modePin1Level != 0) + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE1,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE1,\ + GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE1,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE1,\ + GPIO_PIN_RESET); + } + + if (modePin2Level != 0) + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE2,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE2,\ + GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE2,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE2,\ + GPIO_PIN_RESET); + } + + if (modePin3Level != 0) + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE3,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE3,\ + GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE3,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE3,\ + GPIO_PIN_RESET); + } + + return 1; +} + +/******************************************************//** + * @brief Select Full Step mode + * @retval None + **********************************************************/ +void Stspin820_Board_SetFullStep(void) +{ + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE1,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE1,\ + GPIO_PIN_RESET); + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE2,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE2,\ + GPIO_PIN_RESET); + HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_PORT_MODE3,\ + BSP_MOTOR_CONTROL_BOARD_PIN_MODE3,\ + GPIO_PIN_RESET); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +#endif