Library to handle the X-NUCLEO-IHM01A1 Motor Control Expansion Board based on the L6474 component.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: Stepper_Matlab_Control SunTracker_BLE Stepper_Matlab_Control MemsMotorControl ... more
Fork of X_NUCLEO_IHM01A1 by
Motor Control Library
Introduction
Library to handle the X-NUCLEO-IHM01A1 Motor Control Expansion Board based on the the L6474 component.
Daisy-Chain Configuration
This board can be stacked up to three times so that the L6474 components will be connected in daisy-chain configuration. For this purpose, some resistors must be correctly connected on the boards as depicted here below:
Platform compatibility
- NUCLEO boards have been tested with the default configuration provided by the HelloWorld_IHM01A1 example.
- LPCXpresso11U68 board has been tested with the following patch:
- to connect with a wire from the LPCX’s
D4
pin to the IHM01A1’sD9
pin; - to initialize the pwm PinName variable with
D4
rather thanD9
.
- to connect with a wire from the LPCX’s
- FRDM-K64F board has been tested with the following patch:
- to connect with a wire from the FRDM’s
D4
pin to the IHM01A1’sD8
pin; - to initialize the standby_reset PinName variable with
D4
rather thanD8
.
- to connect with a wire from the FRDM’s
Example Applications
- HelloWorld_IHM01A1
- HelloWorld_IHM01A1_2Motors
- MotorControl_IHM01A1
- MemsMotorControl
- MemsMotorControl_IHM01A1_IKS01A2
Revision 35:974ca699c792, committed 2017-03-13
- Comitter:
- Davidroid
- Date:
- Mon Mar 13 17:45:14 2017 +0000
- Parent:
- 34:27da297c361c
- Commit message:
- Typo corrected.
Changed in this revision
diff -r 27da297c361c -r 974ca699c792 Components/L6474/L6474.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Components/L6474/L6474.cpp Mon Mar 13 17:45:14 2017 +0000 @@ -0,0 +1,1421 @@ +/** + ****************************************************************************** + * @file L6474.cpp + * @author IPC Rennes + * @version V1.5.0 + * @date November 12, 2014 + * @brief L6474 driver (fully integrated microstepping motor driver) + * @note (C) COPYRIGHT 2014 STMicroelectronics + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +/* Generated with STM32CubeTOO -----------------------------------------------*/ + + +/* Revision ------------------------------------------------------------------*/ +/* + Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev + Branch/Trunk/Tag: trunk + Based on: X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.c + Revision: 0 +*/ + + +/* Includes ------------------------------------------------------------------*/ + +#include "L6474.h" + + +/* Definitions ---------------------------------------------------------------*/ + +/* Error while initialising the SPI. */ +#define L6474_ERROR_0 (0x8000) + +/* Error of bad SPI transaction. */ +#define L6474_ERROR_1 (0x8001) + +/* Maximum number of steps. */ +#define MAX_STEPS (0x7FFFFFFF) + +/* Maximum frequency of the PWMs in Hz. */ +#define L6474_MAX_PWM_FREQ (10000) + +/* Minimum frequency of the PWMs in Hz. */ +#define L6474_MIN_PWM_FREQ (2) + + +/* Variables ----------------------------------------------------------------*/ + +/* Number of devices. */ +uint8_t L6474::number_of_devices = 0; + +/* ISR flags used to restart an interrupted SPI transfer when an error is reported. */ +bool L6474::spi_preemtion_by_isr = FALSE; +bool L6474::isr_flag = FALSE; + +/* SPI Transmission for Daisy-Chain Configuration. */ +uint8_t L6474::spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; +uint8_t L6474::spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + + +/* Methods -------------------------------------------------------------------*/ + +/********************************************************** + * @brief Attaches a user callback to the error Handler. + * The call back will be then called each time the library + * detects an error + * @param[in] callback Name of the callback to attach + * to the error Hanlder + * @retval None + **********************************************************/ +void L6474::L6474_AttachErrorHandler(void (*callback)(uint16_t error)) +{ + error_handler_callback = (void (*)(uint16_t error)) callback; +} + +/********************************************************** + * @brief Starts the L6474 library + * @param init Initialization structure. + * @retval COMPONENT_OK in case of success. + **********************************************************/ +status_t L6474::L6474_Init(void *init) +{ + /* Initialise the PWMs used for the Step clocks ----------------------------*/ + L6474_PwmInit(); + + /* Initialise the L6474s ------------------------------------------------*/ + + /* Standby-reset deactivation */ + L6474_ReleaseReset(); + + /* Let a delay after reset */ + L6474_Delay(1); + + /* Set device parameters to the predefined values from "l6474_target_config.h". */ + L6474_SetDeviceParamsToPredefinedValues(); + + if (init == NULL) + /* Set device registers to the predefined values from "l6474_target_config.h". */ + L6474_SetRegisterToPredefinedValues(); + else + /* Set device registers to the passed initialization values. */ + L6474_SetRegisterToInitializationValues((L6474_init_t *) init); + + /* Disable L6474 powerstage */ + L6474_CmdDisable(); + + /* Get Status to clear flags after start up */ + L6474_CmdGetStatus(); + + return COMPONENT_OK; +} + +/********************************************************** + * @brief Read id + * @param id pointer to the identifier to be read. + * @retval COMPONENT_OK in case of success. + **********************************************************/ +status_t L6474::L6474_ReadID(uint8_t *id) +{ + *id = device_instance; + + return COMPONENT_OK; +} + +/********************************************************** + * @brief Returns the acceleration of the specified device + * @retval Acceleration in pps^2 + **********************************************************/ +uint16_t L6474::L6474_GetAcceleration(void) +{ + return (device_prm.acceleration); +} + +/********************************************************** + * @brief Returns the current speed of the specified device + * @retval Speed in pps + **********************************************************/ +uint16_t L6474::L6474_GetCurrentSpeed(void) +{ + return device_prm.speed; +} + +/********************************************************** + * @brief Returns the deceleration of the specified device + * @retval Deceleration in pps^2 + **********************************************************/ +uint16_t L6474::L6474_GetDeceleration(void) +{ + return (device_prm.deceleration); +} + +/********************************************************** + * @brief Returns the device state + * @retval State (ACCELERATING, DECELERATING, STEADY or INACTIVE) + **********************************************************/ +motorState_t L6474::L6474_GetDeviceState(void) +{ + return device_prm.motionState; +} + +/********************************************************** + * @brief Returns the FW version of the library + * @param None + * @retval L6474_FW_VERSION + **********************************************************/ +uint8_t L6474::L6474_GetFwVersion(void) +{ + return (L6474_FW_VERSION); +} + +/********************************************************** + * @brief Returns the mark position of the specified device + * @retval Mark register value converted in a 32b signed integer + **********************************************************/ +int32_t L6474::L6474_GetMark(void) +{ + return L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); +} + +/********************************************************** + * @brief Returns the max speed of the specified device + * @retval maxSpeed in pps + **********************************************************/ +uint16_t L6474::L6474_GetMaxSpeed(void) +{ + return (device_prm.maxSpeed); +} + +/********************************************************** + * @brief Returns the min speed of the specified device + * @retval minSpeed in pps + **********************************************************/ +uint16_t L6474::L6474_GetMinSpeed(void) +{ + return (device_prm.minSpeed); +} + +/********************************************************** + * @brief Returns the ABS_POSITION of the specified device + * @retval ABS_POSITION register value converted in a 32b signed integer + **********************************************************/ +int32_t L6474::L6474_GetPosition(void) +{ + return L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); +} + +/********************************************************** + * @brief Requests the motor to move to the home position (ABS_POSITION = 0) + * @retval None + **********************************************************/ +void L6474::L6474_GoHome(void) +{ + L6474_GoTo(0); +} + +/********************************************************** + * @brief Requests the motor to move to the mark position + * @retval None + **********************************************************/ +void L6474::L6474_GoMark(void) +{ + uint32_t mark; + + mark = L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); + L6474_GoTo(mark); +} + +/********************************************************** + * @brief Requests the motor to move to the specified position + * @param[in] targetPosition absolute position in steps + * @retval None + **********************************************************/ +void L6474::L6474_GoTo(int32_t targetPosition) +{ + motorDir_t direction; + int32_t steps; + + /* Eventually deactivate motor */ + if (device_prm.motionState != INACTIVE) + { + L6474_HardStop(); + } + + /* Get current position */ + device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); + + /* Compute the number of steps to perform */ + steps = targetPosition - device_prm.currentPosition; + + if (steps >= 0) + { + device_prm.stepsToTake = steps; + direction = FORWARD; + } + else + { + device_prm.stepsToTake = -steps; + direction = BACKWARD; + } + + if (steps != 0) + { + device_prm.commandExecuted = MOVE_CMD; + + /* Direction setup */ + L6474_SetDirection(direction); + + L6474_ComputeSpeedProfile(device_prm.stepsToTake); + + /* Motor activation */ + L6474_StartMovement(); + } +} + +/********************************************************** + * @brief Immediatly stops the motor and disable the power bridge + * @retval None + **********************************************************/ +void L6474::L6474_HardStop(void) +{ + /* Disable corresponding PWM */ + L6474_PwmStop(); + + /* Set inactive state */ + device_prm.motionState = INACTIVE; + device_prm.commandExecuted = NO_CMD; + device_prm.stepsToTake = MAX_STEPS; +} + +/********************************************************** + * @brief Moves the motor of the specified number of steps + * @param[in] direction FORWARD or BACKWARD + * @param[in] stepCount Number of steps to perform + * @retval None + **********************************************************/ +void L6474::L6474_Move(motorDir_t direction, uint32_t stepCount) +{ + /* Eventually deactivate motor */ + if (device_prm.motionState != INACTIVE) + { + L6474_HardStop(); + } + + if (stepCount != 0) + { + device_prm.stepsToTake = stepCount; + + device_prm.commandExecuted = MOVE_CMD; + + device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); + + /* Direction setup */ + L6474_SetDirection(direction); + + L6474_ComputeSpeedProfile(stepCount); + + /* Motor activation */ + L6474_StartMovement(); + } +} + +/********************************************************** + * @brief Runs the motor. It will accelerate from the min + * speed up to the max speed by using the device acceleration. + * @param[in] direction FORWARD or BACKWARD + * @retval None + **********************************************************/ +void L6474::L6474_Run(motorDir_t direction) +{ + /* Eventually deactivate motor */ + if (device_prm.motionState != INACTIVE) + { + L6474_HardStop(); + } + + /* Direction setup */ + L6474_SetDirection(direction); + + device_prm.commandExecuted = RUN_CMD; + + /* Motor activation */ + L6474_StartMovement(); +} + +/********************************************************** + * @brief Changes the acceleration of the specified device + * @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 is the device is executing + * a MOVE or GOTO command (but it can be used during a RUN command) + **********************************************************/ +bool L6474::L6474_SetAcceleration(uint16_t newAcc) +{ + bool cmdExecuted = FALSE; + if ((newAcc != 0)&& + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) + { + device_prm.acceleration = newAcc; + cmdExecuted = TRUE; + } + return cmdExecuted; +} + +/********************************************************** + * @brief Changes the deceleration of the specified device + * @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 is the device is executing + * a MOVE or GOTO command (but it can be used during a RUN command) + **********************************************************/ +bool L6474::L6474_SetDeceleration(uint16_t newDec) +{ + bool cmdExecuted = FALSE; + if ((newDec != 0)&& + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) + { + device_prm.deceleration = newDec; + cmdExecuted = TRUE; + } + return cmdExecuted; +} + +/********************************************************** + * @brief Set current position to be the Home position (ABS pos set to 0) + * @retval None + **********************************************************/ +void L6474::L6474_SetHome(void) +{ + L6474_CmdSetParam(L6474_ABS_POS, 0); +} + +/********************************************************** + * @brief Sets current position to be the Mark position + * @retval None + **********************************************************/ +void L6474::L6474_SetMark(void) +{ + uint32_t mark = L6474_CmdGetParam(L6474_ABS_POS); + L6474_CmdSetParam(L6474_MARK, mark); +} + +/********************************************************** + * @brief Changes the max speed of the specified device + * @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 L6474::L6474_SetMaxSpeed(uint16_t newMaxSpeed) +{ + bool cmdExecuted = FALSE; + if ((newMaxSpeed >= L6474_MIN_PWM_FREQ)&& + (newMaxSpeed <= L6474_MAX_PWM_FREQ) && + (device_prm.minSpeed <= newMaxSpeed) && + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) + { + device_prm.maxSpeed = newMaxSpeed; + cmdExecuted = TRUE; + } + return cmdExecuted; +} + +/********************************************************** + * @brief Changes the min speed of the specified device + * @param[in] 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 L6474::L6474_SetMinSpeed(uint16_t newMinSpeed) +{ + bool cmdExecuted = FALSE; + if ((newMinSpeed >= L6474_MIN_PWM_FREQ)&& + (newMinSpeed <= L6474_MAX_PWM_FREQ) && + (newMinSpeed <= device_prm.maxSpeed) && + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) + { + device_prm.minSpeed = newMinSpeed; + cmdExecuted = TRUE; + } + return cmdExecuted; +} + +/********************************************************** + * @brief Stops the motor by using the device deceleration + * @retval true if the command is successfully executed, else false + * @note The command is not performed is the device is in INACTIVE state. + **********************************************************/ +bool L6474::L6474_SoftStop(void) +{ + bool cmdExecuted = FALSE; + if (device_prm.motionState != INACTIVE) + { + device_prm.commandExecuted = SOFT_STOP_CMD; + cmdExecuted = TRUE; + } + return (cmdExecuted); +} + +/********************************************************** + * @brief Locks until the device state becomes Inactive + * @retval None + **********************************************************/ +void L6474::L6474_WaitWhileActive(void) +{ + /* Wait while motor is running */ + while (L6474_GetDeviceState() != INACTIVE); +} + +/********************************************************** + * @brief Issue the Disable command to the L6474 of the specified device + * @retval None + **********************************************************/ +void L6474::L6474_CmdDisable(void) +{ + L6474_SendCommand(L6474_DISABLE); +} + +/********************************************************** + * @brief Issues the Enable command to the L6474 of the specified device + * @retval None + **********************************************************/ +void L6474::L6474_CmdEnable(void) +{ + L6474_SendCommand(L6474_ENABLE); +} + +/********************************************************** + * @brief Issues the GetParam command to the L6474 of the specified device + * @param[in] parameter Register adress (L6474_ABS_POS, L6474_MARK,...) + * @retval Register value + **********************************************************/ +uint32_t L6474::L6474_CmdGetParam(L6474_Registers_t parameter) +{ + uint32_t i; + uint32_t spiRxData; + uint8_t maxArgumentNbBytes = 0; + uint8_t spiIndex = number_of_devices - device_instance - 1; + bool itDisable = FALSE; + + do + { + spi_preemtion_by_isr = FALSE; + if (itDisable) + { + /* re-enable L6474_EnableIrq if disable in previous iteration */ + L6474_EnableIrq(); + itDisable = FALSE; + } + + for (i = 0; i < number_of_devices; i++) + { + spi_tx_bursts[0][i] = L6474_NOP; + spi_tx_bursts[1][i] = L6474_NOP; + spi_tx_bursts[2][i] = L6474_NOP; + spi_tx_bursts[3][i] = L6474_NOP; + spi_rx_bursts[1][i] = 0; + spi_rx_bursts[2][i] = 0; + spi_rx_bursts[3][i] = 0; + } + + switch (parameter) + { + case L6474_ABS_POS: ; + case L6474_MARK: + spi_tx_bursts[0][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); + maxArgumentNbBytes = 3; + break; + case L6474_EL_POS: ; + case L6474_CONFIG: ; + case L6474_STATUS: + spi_tx_bursts[1][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); + maxArgumentNbBytes = 2; + break; + default: + spi_tx_bursts[2][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); + maxArgumentNbBytes = 1; + } + + /* Disable interruption before checking */ + /* pre-emption by ISR and SPI transfers*/ + L6474_DisableIrq(); + itDisable = TRUE; + } while (spi_preemtion_by_isr); // check pre-emption by ISR + + for (i = L6474_CMD_ARG_MAX_NB_BYTES-1-maxArgumentNbBytes; + i < L6474_CMD_ARG_MAX_NB_BYTES; + i++) + { + L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); + } + + spiRxData = ((uint32_t)spi_rx_bursts[1][spiIndex] << 16) | + (spi_rx_bursts[2][spiIndex] << 8) | + (spi_rx_bursts[3][spiIndex]); + + /* re-enable L6474_EnableIrq after SPI transfers*/ + L6474_EnableIrq(); + + return (spiRxData); +} + +/********************************************************** + * @brief Issues the GetStatus command to the L6474 of the specified device + * @retval Status Register value + * @note Once the GetStatus command is performed, the flags of the status register + * are reset. This is not the case when the status register is read with the + * GetParam command (via the functions L6474ReadStatusRegister or L6474_CmdGetParam). + **********************************************************/ +uint16_t L6474::L6474_CmdGetStatus(void) +{ + uint32_t i; + uint16_t status; + uint8_t spiIndex = number_of_devices - device_instance - 1; + bool itDisable = FALSE; + + do + { + spi_preemtion_by_isr = FALSE; + if (itDisable) + { + /* re-enable L6474_EnableIrq if disable in previous iteration */ + L6474_EnableIrq(); + itDisable = FALSE; + } + + for (i = 0; i < number_of_devices; i++) + { + spi_tx_bursts[0][i] = L6474_NOP; + spi_tx_bursts[1][i] = L6474_NOP; + spi_tx_bursts[2][i] = L6474_NOP; + spi_rx_bursts[1][i] = 0; + spi_rx_bursts[2][i] = 0; + } + spi_tx_bursts[0][spiIndex] = L6474_GET_STATUS; + + /* Disable interruption before checking */ + /* pre-emption by ISR and SPI transfers*/ + L6474_DisableIrq(); + itDisable = TRUE; + } while (spi_preemtion_by_isr); // check pre-emption by ISR + + for (i = 0; i < L6474_CMD_ARG_NB_BYTES_GET_STATUS + L6474_RSP_NB_BYTES_GET_STATUS; i++) + { + L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); + } + status = (spi_rx_bursts[1][spiIndex] << 8) | (spi_rx_bursts[2][spiIndex]); + + /* re-enable L6474_EnableIrq after SPI transfers*/ + L6474_EnableIrq(); + + return (status); +} + +/********************************************************** + * @brief Issues the Nop command to the L6474 of the specified device + * @retval None + **********************************************************/ +void L6474::L6474_CmdNop(void) +{ + L6474_SendCommand(L6474_NOP); +} + +/********************************************************** + * @brief Issues the SetParam command to the L6474 of the specified device + * @param[in] parameter Register adress (L6474_ABS_POS, L6474_MARK,...) + * @param[in] value Value to set in the register + * @retval None + **********************************************************/ +void L6474::L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value) +{ + uint32_t i; + uint8_t maxArgumentNbBytes = 0; + uint8_t spiIndex = number_of_devices - device_instance - 1; + bool itDisable = FALSE; + do + { + spi_preemtion_by_isr = FALSE; + if (itDisable) + { + /* re-enable L6474_EnableIrq if disable in previous iteration */ + L6474_EnableIrq(); + itDisable = FALSE; + } + + for (i = 0; i < number_of_devices; i++) + { + spi_tx_bursts[0][i] = L6474_NOP; + spi_tx_bursts[1][i] = L6474_NOP; + spi_tx_bursts[2][i] = L6474_NOP; + spi_tx_bursts[3][i] = L6474_NOP; + } + + switch (parameter) + { + case L6474_ABS_POS: ; + case L6474_MARK: + spi_tx_bursts[0][spiIndex] = parameter; + spi_tx_bursts[1][spiIndex] = (uint8_t)(value >> 16); + spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); + maxArgumentNbBytes = 3; + break; + case L6474_EL_POS: ; + case L6474_CONFIG: + spi_tx_bursts[1][spiIndex] = parameter; + spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); + maxArgumentNbBytes = 2; + break; + default: + spi_tx_bursts[2][spiIndex] = parameter; + maxArgumentNbBytes = 1; + break; + } + spi_tx_bursts[3][spiIndex] = (uint8_t)(value); + + /* Disable interruption before checking */ + /* pre-emption by ISR and SPI transfers*/ + L6474_DisableIrq(); + itDisable = TRUE; + } while (spi_preemtion_by_isr); // check pre-emption by ISR + + /* SPI transfer */ + for (i = L6474_CMD_ARG_MAX_NB_BYTES-1-maxArgumentNbBytes; + i < L6474_CMD_ARG_MAX_NB_BYTES; + i++) + { + L6474_WriteBytes(&spi_tx_bursts[i][0],&spi_rx_bursts[i][0]); + } + /* re-enable L6474_EnableIrq after SPI transfers*/ + L6474_EnableIrq(); +} + +/********************************************************** + * @brief Reads the Status Register value + * @retval Status register valued + * @note The status register flags are not cleared + * at the difference with L6474CmdGetStatus() + **********************************************************/ +uint16_t L6474::L6474_ReadStatusRegister(void) +{ + return (L6474_CmdGetParam(L6474_STATUS)); +} + +/********************************************************** + * @brief Set the stepping mode + * @param[in] stepMod from full step to 1/16 microstep as specified in enum motorStepMode_t + * @retval None + **********************************************************/ +void L6474::L6474_SelectStepMode(motorStepMode_t stepMod) +{ + uint8_t stepModeRegister; + L6474_STEP_SEL_t l6474StepMod; + + switch (stepMod) + { + case STEP_MODE_FULL: + l6474StepMod = L6474_STEP_SEL_1; + break; + case STEP_MODE_HALF: + l6474StepMod = L6474_STEP_SEL_1_2; + break; + case STEP_MODE_1_4: + l6474StepMod = L6474_STEP_SEL_1_4; + break; + case STEP_MODE_1_8: + l6474StepMod = L6474_STEP_SEL_1_8; + break; + case STEP_MODE_1_16: + default: + l6474StepMod = L6474_STEP_SEL_1_16; + break; + } + + /* Eventually deactivate motor */ + if (device_prm.motionState != INACTIVE) + { + L6474_HardStop(); + } + + /* Read Step mode register and clear STEP_SEL field */ + stepModeRegister = (uint8_t)(0xF8 & L6474_CmdGetParam(L6474_STEP_MODE)) ; + + /* Apply new step mode */ + L6474_CmdSetParam(L6474_STEP_MODE, stepModeRegister | (uint8_t)l6474StepMod); + + /* Reset abs pos register */ + L6474_SetHome(); +} + +/********************************************************** + * @brief Get the direction + * @param None + * @retval direction FORWARD or BACKWARD + **********************************************************/ +motorDir_t L6474::L6474_GetDirection(void) +{ + return device_prm.direction; +} + +/********************************************************** + * @brief Specifies the direction + * @param[in] dir FORWARD or BACKWARD + * @note The direction change is only applied if the device + * is in INACTIVE state + * @retval None + **********************************************************/ +void L6474::L6474_SetDirection(motorDir_t direction) +{ + if (device_prm.motionState == INACTIVE) + { + device_prm.direction = direction; + L6474_SetDirectionGpio(direction); + } +} + +/********************************************************** + * @brief Updates the current speed of the device + * @param[in] newSpeed in pps + * @retval None + **********************************************************/ +void L6474::L6474_ApplySpeed(uint16_t newSpeed) +{ + if (newSpeed < L6474_MIN_PWM_FREQ) + { + newSpeed = L6474_MIN_PWM_FREQ; + } + if (newSpeed > L6474_MAX_PWM_FREQ) + { + newSpeed = L6474_MAX_PWM_FREQ; + } + + device_prm.speed = newSpeed; + + L6474_PwmSetFreq(newSpeed); +} + +/********************************************************** + * @brief Computes the speed profile according to the number of steps to move + * @param[in] nbSteps number of steps to perform + * @retval None + * @note Using the acceleration and deceleration of the device, + * this function determines the duration in steps of the acceleration, + * steady and deceleration phases. + * If the total number of steps to perform is big enough, a trapezoidal move + * is performed (i.e. there is a steady phase where the motor runs at the maximum + * speed. + * Else, a triangular move is performed (no steady phase: the maximum speed is never + * reached. + **********************************************************/ +void L6474::L6474_ComputeSpeedProfile(uint32_t nbSteps) +{ + uint32_t reqAccSteps; + uint32_t reqDecSteps; + + /* compute the number of steps to get the targeted speed */ + uint16_t minSpeed = device_prm.minSpeed; + reqAccSteps = (device_prm.maxSpeed - minSpeed); + reqAccSteps *= (device_prm.maxSpeed + minSpeed); + reqDecSteps = reqAccSteps; + reqAccSteps /= (uint32_t)device_prm.acceleration; + reqAccSteps /= 2; + + /* compute the number of steps to stop */ + reqDecSteps /= (uint32_t)device_prm.deceleration; + reqDecSteps /= 2; + + if(( reqAccSteps + reqDecSteps ) > nbSteps) + { + /* Triangular move */ + /* reqDecSteps = (Pos * Dec) /(Dec+Acc) */ + uint32_t dec = device_prm.deceleration; + uint32_t acc = device_prm.acceleration; + + reqDecSteps = ((uint32_t) dec * nbSteps) / (acc + dec); + if (reqDecSteps > 1) + { + reqAccSteps = reqDecSteps - 1; + if(reqAccSteps == 0) + { + reqAccSteps = 1; + } + } + else + { + reqAccSteps = 0; + } + device_prm.endAccPos = reqAccSteps; + device_prm.startDecPos = reqDecSteps; + } + else + { + /* Trapezoidal move */ + /* accelerating phase to endAccPos */ + /* steady phase from endAccPos to startDecPos */ + /* decelerating from startDecPos to stepsToTake*/ + device_prm.endAccPos = reqAccSteps; + device_prm.startDecPos = nbSteps - reqDecSteps - 1; + } +} + +/********************************************************** + * @brief Converts the ABS_POSITION register value to a 32b signed integer + * @param[in] abs_position_reg value of the ABS_POSITION register + * @retval operation_result 32b signed integer corresponding to the absolute position + **********************************************************/ +int32_t L6474::L6474_ConvertPosition(uint32_t abs_position_reg) +{ + int32_t operation_result; + + if (abs_position_reg & L6474_ABS_POS_SIGN_BIT_MASK) + { + /* Negative register value */ + abs_position_reg = ~abs_position_reg; + abs_position_reg += 1; + + operation_result = (int32_t) (abs_position_reg & L6474_ABS_POS_VALUE_MASK); + operation_result = -operation_result; + } + else + { + operation_result = (int32_t) abs_position_reg; + } + + return operation_result; +} + +/********************************************************** + * @brief Error handler which calls the user callback (if defined) + * @param[in] error Number of the error + * @retval None + **********************************************************/ +void L6474::L6474_ErrorHandler(uint16_t error) +{ + if (error_handler_callback != 0) + { + (void) error_handler_callback(error); + } + else + { + /* Aborting the program. */ + exit(EXIT_FAILURE); + } +} + +/********************************************************** + * @brief Sends a command without arguments to the L6474 via the SPI + * @param[in] param Command to send + * @retval None + **********************************************************/ +void L6474::L6474_SendCommand(uint8_t param) +{ + uint32_t i; + bool itDisable = FALSE; + uint8_t spiIndex = number_of_devices - device_instance - 1; + + do + { + spi_preemtion_by_isr = FALSE; + if (itDisable) + { + /* re-enable L6474_EnableIrq if disable in previous iteration */ + L6474_EnableIrq(); + itDisable = FALSE; + } + + for (i = 0; i < number_of_devices; i++) + { + spi_tx_bursts[3][i] = L6474_NOP; + } + spi_tx_bursts[3][spiIndex] = param; + + /* Disable interruption before checking */ + /* pre-emption by ISR and SPI transfers*/ + L6474_DisableIrq(); + itDisable = TRUE; + } while (spi_preemtion_by_isr); // check pre-emption by ISR + + L6474_WriteBytes(&spi_tx_bursts[3][0], &spi_rx_bursts[3][0]); + + /* re-enable L6474_EnableIrq after SPI transfers*/ + L6474_EnableIrq(); +} + +/********************************************************** + * @brief Sets the registers of the L6474 to their predefined values + * from l6474_target_config.h + * @retval None + **********************************************************/ +void L6474::L6474_SetRegisterToPredefinedValues(void) +{ + L6474_CmdSetParam( + L6474_ABS_POS, + 0); + L6474_CmdSetParam( + L6474_EL_POS, + 0); + L6474_CmdSetParam( + L6474_MARK, + 0); + switch (device_instance) + { + case 0: + L6474_CmdSetParam( + L6474_TVAL, + L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_0)); + L6474_CmdSetParam( + L6474_T_FAST, + (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_0 | + (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_0); + L6474_CmdSetParam( + L6474_TON_MIN, + L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_0) + ); + L6474_CmdSetParam( + L6474_TOFF_MIN, + L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_0)); + L6474_CmdSetParam( + L6474_OCD_TH, + L6474_CONF_PARAM_OCD_TH_DEVICE_0); + L6474_CmdSetParam( + L6474_STEP_MODE, + (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_0 | + (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_0); + L6474_CmdSetParam( + L6474_ALARM_EN, + L6474_CONF_PARAM_ALARM_EN_DEVICE_0); + L6474_CmdSetParam( + L6474_CONFIG, + (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_0 | + (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_0 | + (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_0 | + (uint16_t)L6474_CONF_PARAM_SR_DEVICE_0 | + (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_0); + break; + case 1: + L6474_CmdSetParam( + L6474_TVAL, + L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_1)); + L6474_CmdSetParam( + L6474_T_FAST, + (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_1 | + (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_1); + L6474_CmdSetParam( + L6474_TON_MIN, + L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_1)); + L6474_CmdSetParam( + L6474_TOFF_MIN, + L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_1)); + L6474_CmdSetParam( + L6474_OCD_TH, + L6474_CONF_PARAM_OCD_TH_DEVICE_1); + L6474_CmdSetParam( + L6474_STEP_MODE, + (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_1 | + (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_1); + L6474_CmdSetParam( + L6474_ALARM_EN, + L6474_CONF_PARAM_ALARM_EN_DEVICE_1); + L6474_CmdSetParam( + L6474_CONFIG, + (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_1 | + (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_1 | + (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_1 | + (uint16_t)L6474_CONF_PARAM_SR_DEVICE_1 | + (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_1); + break; + case 2: + L6474_CmdSetParam( + L6474_TVAL, + L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_2)); + L6474_CmdSetParam( + L6474_T_FAST, + (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_2 | + (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_2); + L6474_CmdSetParam( + L6474_TON_MIN, + L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_2)); + L6474_CmdSetParam( + L6474_TOFF_MIN, + L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_2)); + L6474_CmdSetParam( + L6474_OCD_TH, + L6474_CONF_PARAM_OCD_TH_DEVICE_2); + L6474_CmdSetParam( + L6474_STEP_MODE, + (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_2 | + (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_2); + L6474_CmdSetParam( + L6474_ALARM_EN, + L6474_CONF_PARAM_ALARM_EN_DEVICE_2); + L6474_CmdSetParam( + L6474_CONFIG, + (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_2 | + (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_2 | + (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_2 | + (uint16_t)L6474_CONF_PARAM_SR_DEVICE_2 | + (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_2); + break; + default: ; + } +} + +/********************************************************** + * @brief Sets the registers of the L6474 to initialization values. + * @param init Initialization structure. + * @retval None. + **********************************************************/ +void L6474::L6474_SetRegisterToInitializationValues(L6474_init_t *init) +{ + L6474_CmdSetParam( + L6474_ABS_POS, + 0 + ); + L6474_CmdSetParam( + L6474_EL_POS, + 0 + ); + L6474_CmdSetParam( + L6474_MARK, + 0 + ); + L6474_CmdSetParam( + L6474_TVAL, + L6474_Tval_Current_to_Par(init->torque_regulation_current_mA) + ); + L6474_CmdSetParam( + L6474_T_FAST, + (uint8_t) init->maximum_fast_decay_time | + (uint8_t) init->fall_time + ); + L6474_CmdSetParam( + L6474_TON_MIN, + L6474_Tmin_Time_to_Par(init->minimum_ON_time_us) + ); + L6474_CmdSetParam( + L6474_TOFF_MIN, + L6474_Tmin_Time_to_Par(init->minimum_OFF_time_us) + ); + L6474_CmdSetParam( + L6474_OCD_TH, + init->overcurrent_threshold + ); + L6474_CmdSetParam( + L6474_STEP_MODE, + (uint8_t) init->step_selection | + (uint8_t) init->sync_selection + ); + L6474_CmdSetParam( + L6474_ALARM_EN, + init->alarm + ); + L6474_CmdSetParam( + L6474_CONFIG, + (uint16_t) init->clock | + (uint16_t) init->torque_regulation_method | + (uint16_t) init->overcurrent_shutwdown | + (uint16_t) init->slew_rate | + (uint16_t) init->target_swicthing_period + ); + L6474_SetAcceleration((uint16_t) init->acceleration_pps_2); + L6474_SetDeceleration((uint16_t) init->deceleration_pps_2); + L6474_SetMaxSpeed((uint16_t) init->maximum_speed_pps); + L6474_SetMinSpeed((uint16_t) init->minimum_speed_pps); +} + +/********************************************************** + * @brief Sets the parameters of the device to predefined values + * from l6474_target_config.h + * @param None + * @retval None + **********************************************************/ +void L6474::L6474_SetDeviceParamsToPredefinedValues(void) +{ + switch (device_instance) + { + case 0: + device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_0; + device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_0; + device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_0; + device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_0; + break; + + case 1: + device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_1; + device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_1; + device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_1; + device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_1; + break; + + case 2: + device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_2; + device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_2; + device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_2; + device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_2; + break; + } + + device_prm.accu = 0; + device_prm.currentPosition = 0; + device_prm.endAccPos = 0; + device_prm.relativePos = 0; + device_prm.startDecPos = 0; + device_prm.stepsToTake = 0; + device_prm.speed = 0; + device_prm.commandExecuted = NO_CMD; + device_prm.direction = FORWARD; + device_prm.motionState = INACTIVE; +} + +/********************************************************** + * @brief Initialises the bridge parameters to start the movement + * and enable the power bridge + * @retval None + **********************************************************/ +void L6474::L6474_StartMovement(void) +{ + /* Enable L6474 powerstage */ + L6474_CmdEnable(); + if (device_prm.endAccPos != 0) + { + device_prm.motionState = ACCELERATING; + } + else + { + device_prm.motionState = DECELERATING; + } + device_prm.accu = 0; + device_prm.relativePos = 0; + L6474_ApplySpeed(device_prm.minSpeed); +} + +/********************************************************** + * @brief Handles the device state machine at each ste + * @retval None + * @note Must only be called by the timer ISR + **********************************************************/ +void L6474::L6474_StepClockHandler(void) +{ + /* Set isr flag */ + isr_flag = TRUE; + + /* Incrementation of the relative position */ + device_prm.relativePos++; + + switch (device_prm.motionState) + { + case ACCELERATING: + { + uint32_t relPos = device_prm.relativePos; + uint32_t endAccPos = device_prm.endAccPos; + uint16_t speed = device_prm.speed; + uint32_t acc = ((uint32_t)device_prm.acceleration << 16); + + if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| + ((device_prm.commandExecuted != RUN_CMD)&& + (relPos == device_prm.startDecPos))) + { + device_prm.motionState = DECELERATING; + device_prm.accu = 0; + } + else if ((speed >= device_prm.maxSpeed)|| + ((device_prm.commandExecuted != RUN_CMD)&& + (relPos == endAccPos))) + { + device_prm.motionState = STEADY; + } + else + { + bool speedUpdated = FALSE; + /* Go on accelerating */ + if (speed == 0) speed =1; + device_prm.accu += acc / speed; + while (device_prm.accu >= (0X10000L)) + { + device_prm.accu -= (0X10000L); + speed +=1; + speedUpdated = TRUE; + } + + if (speedUpdated) + { + if (speed > device_prm.maxSpeed) + { + speed = device_prm.maxSpeed; + } + device_prm.speed = speed; + L6474_ApplySpeed(device_prm.speed); + } + } + break; + } + case STEADY: + { + uint16_t maxSpeed = device_prm.maxSpeed; + uint32_t relativePos = device_prm.relativePos; + if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| + ((device_prm.commandExecuted != RUN_CMD)&& + (relativePos >= (device_prm.startDecPos))) || + ((device_prm.commandExecuted == RUN_CMD)&& + (device_prm.speed > maxSpeed))) + { + device_prm.motionState = DECELERATING; + device_prm.accu = 0; + } + else if ((device_prm.commandExecuted == RUN_CMD)&& + (device_prm.speed < maxSpeed)) + { + device_prm.motionState = ACCELERATING; + device_prm.accu = 0; + } + break; + } + case DECELERATING: + { + uint32_t relativePos = device_prm.relativePos; + uint16_t speed = device_prm.speed; + uint32_t deceleration = ((uint32_t)device_prm.deceleration << 16); + if (((device_prm.commandExecuted == SOFT_STOP_CMD)&&(speed <= device_prm.minSpeed))|| + ((device_prm.commandExecuted != RUN_CMD)&& + (relativePos >= device_prm.stepsToTake))) + { + /* Motion process complete */ + L6474_HardStop(); + } + else if ((device_prm.commandExecuted == RUN_CMD)&& + (speed <= device_prm.maxSpeed)) + { + device_prm.motionState = STEADY; + } + else + { + /* Go on decelerating */ + if (speed > device_prm.minSpeed) + { + bool speedUpdated = FALSE; + if (speed == 0) speed =1; + device_prm.accu += deceleration / speed; + while (device_prm.accu >= (0X10000L)) + { + device_prm.accu -= (0X10000L); + if (speed > 1) + { + speed -=1; + } + speedUpdated = TRUE; + } + + if (speedUpdated) + { + if (speed < device_prm.minSpeed) + { + speed = device_prm.minSpeed; + } + device_prm.speed = speed; + L6474_ApplySpeed(device_prm.speed); + } + } + } + break; + } + default: + { + break; + } + } + /* Set isr flag */ + isr_flag = FALSE; +} + +/********************************************************** + * @brief Converts current in mA to values for TVAL register + * @param[in] current_mA current in mA + * @retval value for TVAL register + **********************************************************/ +float L6474::L6474_Tval_Current_to_Par(float current_mA) +{ + return ((float)(((current_mA - 31.25f) / 31.25f) + 0.5f)); +} + +/********************************************************** + * @brief Converts values from TVAL register to mA + * @param[in] Tval value from TVAL register + * @retval current in mA + **********************************************************/ +float L6474::L6474_Par_to_Tval_Current(float Tval) +{ + return ((float)((Tval - 0.5f) * 31.25f + 31.25f)); +} + +/********************************************************** + * @brief Convert time in us to values for TON_MIN register + * @param[in] ton_min_us time in us + * @retval value for TON_MIN register + **********************************************************/ +float L6474::L6474_Tmin_Time_to_Par(float ton_min_us) +{ + return ((float)(((ton_min_us - 0.5f) * 2.0f) + 0.5f)); +} + +/********************************************************** + * @brief Convert values for TON_MIN register to time in us + * @param[in] Tmin value from TON_MIN register + * @retval time in us + **********************************************************/ +float L6474::L6474_Par_to_Tmin_Time(float Tmin) +{ + return ((float)(((Tmin - 0.5f) / 2.0f) + 0.5f)); +} + +/********************************************************** + * @brief Write and receive a byte via SPI + * @param[in] pByteToTransmit pointer to the byte to transmit + * @param[in] pReceivedByte pointer to the received byte + * @retval None + **********************************************************/ +void L6474::L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) +{ + if (L6474_SpiWriteBytes(pByteToTransmit, pReceivedByte) != 0) + { + L6474_ErrorHandler(L6474_ERROR_1); + } + + if (isr_flag) + { + spi_preemtion_by_isr = TRUE; + } +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/L6474/L6474.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Components/L6474/L6474.h Mon Mar 13 17:45:14 2017 +0000 @@ -0,0 +1,1018 @@ +/** + ****************************************************************************** + * @file L6474.h + * @author Davide Aliprandi, STMicroelectronics + * @version V1.0.0 + * @date October 14th, 2015 + * @brief This file contains the class of an L6474 Motor Control component. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +/* Generated with STM32CubeTOO -----------------------------------------------*/ + + +/* Revision ------------------------------------------------------------------*/ +/* + Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev + Branch/Trunk/Tag: trunk + Based on: X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.h + Revision: 0 +*/ + + +/* Define to prevent recursive inclusion -------------------------------------*/ + +#ifndef __L6474_CLASS_H +#define __L6474_CLASS_H + + +/* Includes ------------------------------------------------------------------*/ + +/* ACTION 1 ------------------------------------------------------------------* + * Include here platform specific header files. * + *----------------------------------------------------------------------------*/ +#include "mbed.h" +#include "DevSPI.h" +/* ACTION 2 ------------------------------------------------------------------* + * Include here component specific header files. * + *----------------------------------------------------------------------------*/ +#include "L6474_def.h" +/* ACTION 3 ------------------------------------------------------------------* + * Include here interface specific header files. * + * * + * Example: * + * #include "HumiditySensor.h" * + * #include "TemperatureSensor.h" * + *----------------------------------------------------------------------------*/ +#include "StepperMotor.h" + + +/* Classes -------------------------------------------------------------------*/ + +/** + * @brief Class representing an L6474 component. + */ +class L6474 : public StepperMotor +{ +public: + + /*** Constructor and Destructor Methods ***/ + + /** + * @brief Constructor. + * @param flag_irq pin name of the FLAG pin of the component. + * @param standby_reset pin name of the STBY\RST pin of the component. + * @param direction pin name of the DIR pin of the component. + * @param pwm pin name of the PWM pin of the component. + * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. + * @param spi SPI device to be used for communication. + */ + L6474(PinName flag_irq, PinName standby_reset, PinName direction, PinName pwm, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), standby_reset(standby_reset), direction(direction), pwm(pwm), ssel(ssel), dev_spi(spi) + { + /* Checking stackability. */ + if (!(number_of_devices < MAX_NUMBER_OF_DEVICES)) { + error("Instantiation of the L6474 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES); + } + + /* ACTION 4 ----------------------------------------------------------* + * Initialize here the component's member variables, one variable per * + * line. * + * * + * Example: * + * measure = 0; * + * instance_id = number_of_instances++; * + *--------------------------------------------------------------------*/ + error_handler_callback = 0; + device_instance = number_of_devices++; + memset(spi_tx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); + memset(spi_rx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); + } + + /** + * @brief Destructor. + */ + virtual ~L6474(void) {} + + + /*** Public Component Related Methods ***/ + + /* ACTION 5 --------------------------------------------------------------* + * Implement here the component's public methods, as wrappers of the C * + * component's functions. * + * They should be: * + * + Methods with the same name of the C component's virtual table's * + * functions (1); * + * + Methods with the same name of the C component's extended virtual * + * table's functions, if any (2). * + * * + * Example: * + * virtual int get_value(float *p_data) //(1) * + * { * + * return COMPONENT_get_value(float *pf_data); * + * } * + * * + * virtual int enable_feature(void) //(2) * + * { * + * return COMPONENT_enable_feature(); * + * } * + *------------------------------------------------------------------------*/ + /** + * @brief Initializing the component in 1/16 Microstepping mode. + * @param init Pointer to device specific initalization structure. + * @retval "0" in case of success, an error code otherwise. + */ + virtual int init(void *init = NULL) + { + return (int) L6474_Init((void *) init); + } + + /** + * @brief Getting the ID of the component. + * @param id Pointer to an allocated variable to store the ID into. + * @retval "0" in case of success, an error code otherwise. + */ + virtual int read_id(uint8_t *id = NULL) + { + return (int) L6474_ReadID((uint8_t *) id); + } + + /** + * @brief Getting the value of the Status Register. + * @param None. + * @retval None. + * @note The Status Register's flags are cleared, contrary to the + * read_status_register() method. + */ + virtual unsigned int get_status(void) + { + return (unsigned int) L6474_CmdGetStatus(); + } + + /** + * @brief Getting a parameter. + * @param parameter A parameter's register address. + * @retval The parameter's value. + * @note The Status Register's flags are cleared, contrary to the + * read_status_register() method. + * The parameter can be one of the following: + * + L6474_ABS_POS + * + L6474_EL_POS + * + L6474_MARK + * + L6474_RESERVED_REG01 + * + L6474_RESERVED_REG02 + * + L6474_RESERVED_REG03 + * + L6474_RESERVED_REG04 + * + L6474_RESERVED_REG05 + * + L6474_RESERVED_REG06 + * + L6474_TVAL : value in mA + * + L6474_RESERVED_REG07 + * + L6474_RESERVED_REG08 + * + L6474_RESERVED_REG09 + * + L6474_RESERVED_REG10 + * + L6474_T_FAST + * + L6474_TON_MIN : value in us + * + L6474_TOFF_MIN : value in us + * + L6474_RESERVED_REG11 + * + L6474_ADC_OUT + * + L6474_OCD_TH + * + L6474_RESERVED_REG12 + * + L6474_STEP_MODE + * + L6474_ALARM_EN + * + L6474_CONFIG + * + L6474_STATUS + * + L6474_RESERVED_REG13 + * + L6474_RESERVED_REG14 + * + L6474_INEXISTENT_REG + */ + virtual float get_parameter(unsigned int parameter) + { + unsigned int register_value = (unsigned int) L6474_CmdGetParam((L6474_Registers_t) parameter); + float value; + + switch ((L6474_Registers_t) parameter) { + case L6474_TVAL: + value = L6474_Par_to_Tval_Current((float) register_value); + break; + case L6474_TON_MIN: + case L6474_TOFF_MIN: + value = L6474_Par_to_Tmin_Time((float) register_value); + break; + default: + value = (float) register_value; + break; + } + + return value; + } + + /** + * @brief Getting the position. + * @param None. + * @retval The position. + */ + virtual signed int get_position(void) + { + return (signed int) L6474_GetPosition(); + } + + /** + * @brief Getting the marked position. + * @param None. + * @retval The marked position. + */ + virtual signed int get_mark(void) + { + return (signed int) L6474_GetMark(); + } + + /** + * @brief Getting the current speed in pps. + * @param None. + * @retval The current speed in pps. + */ + virtual unsigned int get_speed(void) + { + return (unsigned int) L6474_GetCurrentSpeed(); + } + + /** + * @brief Getting the maximum speed in pps. + * @param None. + * @retval The maximum speed in pps. + */ + virtual unsigned int get_max_speed(void) + { + return (unsigned int) L6474_GetMaxSpeed(); + } + + /** + * @brief Getting the minimum speed in pps. + * @param None. + * @retval The minimum speed in pps. + */ + virtual unsigned int get_min_speed(void) + { + return (unsigned int) L6474_GetMinSpeed(); + } + + /** + * @brief Getting the acceleration in pps^2. + * @param None. + * @retval The acceleration in pps^2. + */ + virtual unsigned int get_acceleration(void) + { + return (unsigned int) L6474_GetAcceleration(); + } + + /** + * @brief Getting the deceleration in pps^2. + * @param None. + * @retval The deceleration in pps^2. + */ + virtual unsigned int get_deceleration(void) + { + return (unsigned int) L6474_GetDeceleration(); + } + + /** + * @brief Getting the direction of rotation. + * @param None. + * @retval The direction of rotation. + */ + virtual direction_t get_direction(void) + { + return (direction_t) (L6474_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD); + } + + /** + * @brief Setting a parameter. + * @param parameter A parameter's register address. + * @param value The parameter's value. + * @retval None. + * @note The parameter can be one of the following: + * + L6474_ABS_POS + * + L6474_EL_POS + * + L6474_MARK + * + L6474_RESERVED_REG01 + * + L6474_RESERVED_REG02 + * + L6474_RESERVED_REG03 + * + L6474_RESERVED_REG04 + * + L6474_RESERVED_REG05 + * + L6474_RESERVED_REG06 + * + L6474_TVAL : value in mA + * + L6474_RESERVED_REG07 + * + L6474_RESERVED_REG08 + * + L6474_RESERVED_REG09 + * + L6474_RESERVED_REG10 + * + L6474_T_FAST + * + L6474_TON_MIN : value in us + * + L6474_TOFF_MIN : value in us + * + L6474_RESERVED_REG11 + * + L6474_ADC_OUT + * + L6474_OCD_TH + * + L6474_RESERVED_REG12 + * + L6474_STEP_MODE + * + L6474_ALARM_EN + * + L6474_CONFIG + * + L6474_STATUS + * + L6474_RESERVED_REG13 + * + L6474_RESERVED_REG14 + * + L6474_INEXISTENT_REG + * @warning Some registers can only be written in particular conditions (see L6474's datasheet). + * Any attempt to write one of those registers when the conditions are not satisfied + * causes the command to be ignored and the NOTPERF_CMD flag to rise at the end of the + * last argument byte. Any attempt to set an inexistent register (wrong address value) + * causes the command to be ignored and the WRONG_CMD flag to rise. + * For example, setting some parameters requires first to disable the power bridge; + * this can be done through the soft_hiz() method. + * They are the following: + * + L6474_EL_POS + * + L6474_T_FAST + * + L6474_TON_MIN : value in us + * + L6474_TOFF_MIN : value in us + * + L6474_ADC_OUT + * + L6474_STEP_MODE + * + L6474_CONFIG + * + L6474_STATUS + */ + virtual void set_parameter(unsigned int parameter, float value) + { + float register_value; + + switch ((L6474_Registers_t) parameter) { + case L6474_TVAL: + register_value = L6474_Tval_Current_to_Par(value); + break; + case L6474_TON_MIN: + case L6474_TOFF_MIN: + register_value = L6474_Tmin_Time_to_Par(value); + break; + default: + register_value = value; + break; + } + + L6474_CmdSetParam((L6474_Registers_t) parameter, (unsigned int) register_value); + } + + /** + * @brief Setting the current position to be the home position. + * @param None. + * @retval None. + */ + virtual void set_home(void) + { + L6474_SetHome(); + } + + /** + * @brief Setting the current position to be the marked position. + * @param None. + * @retval None. + */ + virtual void set_mark(void) + { + L6474_SetMark(); + } + + /** + * @brief Setting the maximum speed in pps. + * @param speed The maximum speed in pps. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_max_speed(unsigned int speed) + { + L6474_SetMaxSpeed((unsigned int) speed); + return true; + } + + /** + * @brief Setting the minimum speed in pps. + * @param speed The minimum speed in pps. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_min_speed(unsigned int speed) + { + L6474_SetMinSpeed((unsigned int) speed); + return true; + } + + /** + * @brief Setting the acceleration in pps^2. + * @param acceleration The acceleration in pps^2. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_acceleration(unsigned int acceleration) + { + L6474_SetAcceleration((unsigned int) acceleration); + return true; + } + + /** + * @brief Setting the deceleration in pps^2. + * @param deceleration The deceleration in pps^2. + * @retval "true" in case of success, "false" otherwise. + */ + virtual bool set_deceleration(unsigned int deceleration) + { + L6474_SetDeceleration((unsigned int) deceleration); + return true; + } + + /** + * @brief Going to a specified position. + * @param position The desired position. + * @retval None. + */ + virtual void go_to(signed int position) + { + L6474_GoTo((signed int) position); + } + + /** + * @brief Going to the home position. + * @param None. + * @retval None. + */ + virtual void go_home(void) + { + L6474_GoHome(); + } + + /** + * @brief Going to the marked position. + * @param None. + * @retval None. + */ + virtual void go_mark(void) + { + L6474_GoMark(); + } + + /** + * @brief Running the motor towards a specified direction. + * @param direction The direction of rotation. + * @retval None. + */ + virtual void run(direction_t direction) + { + L6474_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); + } + + /** + * @brief Moving the motor towards a specified direction for a certain number of steps. + * @param direction The direction of rotation. + * @param steps The desired number of steps. + * @retval None. + */ + virtual void move(direction_t direction, unsigned int steps) + { + L6474_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (unsigned int) steps); + } + + /** + * @brief Stopping the motor through an immediate deceleration up to zero speed. + * @param None. + * @retval None. + */ + virtual void soft_stop(void) + { + L6474_SoftStop(); + } + + /** + * @brief Stopping the motor through an immediate infinite deceleration. + * @param None. + * @retval None. + */ + virtual void hard_stop(void) + { + L6474_HardStop(); + } + + /** + * @brief Disabling the power bridge after performing a deceleration to zero. + * @param None. + * @retval None. + */ + virtual void soft_hiz(void) + { + L6474_SoftStop(); + L6474_CmdDisable(); + } + + /** + * @brief Disabling the power bridge immediately. + * @param None. + * @retval None. + */ + virtual void hard_hiz(void) + { + L6474_HardStop(); + L6474_CmdDisable(); + } + + /** + * @brief Waiting while the motor is active. + * @param None. + * @retval None. + */ + virtual void wait_while_active(void) + { + L6474_WaitWhileActive(); + } + + /** + * @brief Getting the device state. + * @param None. + * @retval The device state. + * @note The device state can be one of the following: + * + ACCELERATING + * + DECELERATING + * + STEADY + * + INACTIVE + */ + virtual motorState_t get_device_state(void) + { + return (motorState_t) L6474_GetDeviceState(); + } + + /** + * @brief Reading the Status Register. + * @param None. + * @retval None. + * @note The Status Register's flags are not cleared, contrary to the + * GetStatus() method. + */ + virtual uint16_t read_status_register(void) + { + return (uint16_t) L6474_ReadStatusRegister(); + } + + /** + * @brief Setting the Step Mode. + * @param step_mode The Step Mode. + * @retval "true" in case of success, "false" otherwise. + * @warning Setting the step mode implies first disabling the power bridge through + * the soft_hiz() method. + * @warning Every time step mode is changed, the values of the home + * and mark positions lose meaning and are reset. + */ + virtual bool set_step_mode(step_mode_t step_mode) + { + if ((motorStepMode_t) step_mode > STEP_MODE_1_16) { + return false; + } + + soft_hiz(); + L6474_SelectStepMode((motorStepMode_t) step_mode); + return true; + } + + /** + * @brief Attaching an error handler. + * @param fptr An error handler. + * @retval None. + */ + virtual void attach_error_handler(void (*fptr)(uint16_t error)) + { + L6474_AttachErrorHandler((void (*)(uint16_t error)) fptr); + } + + /** + * @brief Enabling the device. + * @param None. + * @retval None. + */ + virtual void enable(void) + { + L6474_CmdEnable(); + } + + /** + * @brief Disabling the device. + * @param None. + * @retval None. + */ + virtual void disable(void) + { + L6474_CmdDisable(); + } + + /** + * @brief Getting the version of the firmware. + * @param None. + * @retval The version of the firmware. + */ + virtual uint8_t get_fw_version(void) + { + return (uint8_t) L6474_GetFwVersion(); + } + + + /*** Public Interrupt Related Methods ***/ + + /* ACTION 6 --------------------------------------------------------------* + * Implement here interrupt related methods, if any. * + * Note that interrupt handling is platform dependent, e.g.: * + * + mbed: * + * InterruptIn feature_irq(pin); //Interrupt object. * + * feature_irq.rise(callback); //Attach a callback. * + * feature_irq.mode(PullNone); //Set interrupt mode. * + * feature_irq.enable_irq(); //Enable interrupt. * + * feature_irq.disable_irq(); //Disable interrupt. * + * + Arduino: * + * attachInterrupt(pin, callback, RISING); //Attach a callback. * + * detachInterrupt(pin); //Detach a callback. * + * * + * Example (mbed): * + * void attach_feature_irq(void (*fptr) (void)) * + * { * + * feature_irq.rise(fptr); * + * } * + * * + * void enable_feature_irq(void) * + * { * + * feature_irq.enable_irq(); * + * } * + * * + * void disable_feature_irq(void) * + * { * + * feature_irq.disable_irq(); * + * } * + *------------------------------------------------------------------------*/ + /** + * @brief Attaching an interrupt handler to the FLAG interrupt. + * @param fptr An interrupt handler. + * @retval None. + */ + void attach_flag_irq(void (*fptr)(void)) + { + flag_irq.fall(fptr); + } + + /** + * @brief Enabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void enable_flag_irq(void) + { + flag_irq.enable_irq(); + } + + /** + * @brief Disabling the FLAG interrupt handling. + * @param None. + * @retval None. + */ + void disable_flag_irq(void) + { + flag_irq.disable_irq(); + } + + +protected: + + /*** Protected Component Related Methods ***/ + + /* ACTION 7 --------------------------------------------------------------* + * Declare here the component's specific methods. * + * They should be: * + * + Methods with the same name of the C component's virtual table's * + * functions (1); * + * + Methods with the same name of the C component's extended virtual * + * table's functions, if any (2); * + * + Helper methods, if any, like functions declared in the component's * + * source files but not pointed by the component's virtual table (3). * + * * + * Example: * + * status_t COMPONENT_get_value(float *f); //(1) * + * status_t COMPONENT_enable_feature(void); //(2) * + * status_t COMPONENT_compute_average(void); //(3) * + *------------------------------------------------------------------------*/ + void L6474_AttachErrorHandler(void (*callback)(uint16_t error)); + status_t L6474_Init(void *init); + status_t L6474_ReadID(uint8_t *id); + uint16_t L6474_GetAcceleration(void); + uint16_t L6474_GetCurrentSpeed(void); + uint16_t L6474_GetDeceleration(void); + motorState_t L6474_GetDeviceState(void); + uint8_t L6474_GetFwVersion(void); + int32_t L6474_GetMark(void); + uint16_t L6474_GetMaxSpeed(void); + uint16_t L6474_GetMinSpeed(void); + int32_t L6474_GetPosition(void); + void L6474_GoHome(void); + void L6474_GoMark(void); + void L6474_GoTo(int32_t targetPosition); + void L6474_HardStop(void); + void L6474_Move(motorDir_t direction, uint32_t stepCount); + void L6474_Run(motorDir_t direction); + bool L6474_SetAcceleration(uint16_t newAcc); + bool L6474_SetDeceleration(uint16_t newDec); + void L6474_SetHome(void); + void L6474_SetMark(void); + bool L6474_SetMaxSpeed(uint16_t newMaxSpeed); + bool L6474_SetMinSpeed(uint16_t newMinSpeed); + bool L6474_SoftStop(void); + void L6474_WaitWhileActive(void); + void L6474_CmdDisable(void); + void L6474_CmdEnable(void); + uint32_t L6474_CmdGetParam(L6474_Registers_t parameter); + uint16_t L6474_CmdGetStatus(void); + void L6474_CmdNop(void); + void L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value); + uint16_t L6474_ReadStatusRegister(void); + void L6474_SelectStepMode(motorStepMode_t stepMod); + motorDir_t L6474_GetDirection(void); + void L6474_SetDirection(motorDir_t direction); + void L6474_ApplySpeed(uint16_t newSpeed); + void L6474_ComputeSpeedProfile(uint32_t nbSteps); + int32_t L6474_ConvertPosition(uint32_t abs_position_reg); + void L6474_ErrorHandler(uint16_t error); + void L6474_SendCommand(uint8_t param); + void L6474_SetRegisterToPredefinedValues(void); + void L6474_SetRegisterToInitializationValues(L6474_init_t *init); + void L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte); + void L6474_SetDeviceParamsToPredefinedValues(void); + void L6474_StartMovement(void); + void L6474_StepClockHandler(void); + float L6474_Tval_Current_to_Par(float current_mA); + float L6474_Par_to_Tval_Current(float Tval); + float L6474_Tmin_Time_to_Par(float ton_min_us); + float L6474_Par_to_Tmin_Time(float Tmin); + + + /*** Component's I/O Methods ***/ + + /** + * @brief Utility function to read data from L6474. + * @param[out] pBuffer pointer to the buffer to read data into. + * @param[in] NumBytesToRead number of bytes to read. + * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. + */ + status_t Read(uint8_t* pBuffer, uint16_t NumBytesToRead) + { + if (dev_spi.spi_read(pBuffer, ssel, NumBytesToRead) != 0) { + return COMPONENT_ERROR; + } + return COMPONENT_OK; + } + + /** + * @brief Utility function to write data to L6474. + * @param[in] pBuffer pointer to the buffer of data to send. + * @param[in] NumBytesToWrite number of bytes to write. + * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. + */ + status_t Write(uint8_t* pBuffer, uint16_t NumBytesToWrite) + { + if (dev_spi.spi_write(pBuffer, ssel, NumBytesToWrite) != 0) { + return COMPONENT_ERROR; + } + return COMPONENT_OK; + } + + /** + * @brief Utility function to read and write data from/to L6474 at the same time. + * @param[out] pBufferToRead pointer to the buffer to read data into. + * @param[in] pBufferToWrite pointer to the buffer of data to send. + * @param[in] NumBytes number of bytes to read and write. + * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. + */ + status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumBytes) + { + if (dev_spi.spi_read_write(pBufferToRead, pBufferToWrite, ssel, NumBytes) != 0) { + return COMPONENT_ERROR; + } + return COMPONENT_OK; + } + + /* ACTION 8 --------------------------------------------------------------* + * Implement here other I/O methods beyond those already implemented * + * above, which are declared extern within the component's header file. * + *------------------------------------------------------------------------*/ + /** + * @brief Making the CPU wait. + * @param None. + * @retval None. + */ + void L6474_Delay(uint32_t delay) + { + wait_ms(delay); + } + + /** + * @brief Enabling interrupts. + * @param None. + * @retval None. + */ + void L6474_EnableIrq(void) + { + __enable_irq(); + } + + /** + * @brief Disabling interrupts. + * @param None. + * @retval None. + */ + void L6474_DisableIrq(void) + { + __disable_irq(); + } + + /** + * @brief Initialising the PWM. + * @param None. + * @retval None. + */ + void L6474_PwmInit(void) {} + + /** + * @brief Setting the frequency of PWM. + * The frequency controls directly the speed of the device. + * @param frequency the frequency of PWM. + * @retval None. + */ + void L6474_PwmSetFreq(uint16_t frequency) + { + /* Computing the period of PWM. */ + double period = 1.0f / frequency; + + /* Setting the period and the duty-cycle of PWM. */ + pwm.period(period); + pwm.write(0.5f); + + /* Setting a callback with the same period of PWM's, to update the state machine. */ + ticker.attach(Callback<void()>(this, &L6474::L6474_StepClockHandler), period); + } + + /** + * @brief Stopping the PWM. + * @param None. + * @retval None. + */ + void L6474_PwmStop(void) + { + pwm.write(0.0f); + ticker.detach(); + } + + /** + * @brief Putting the device in standby mode. + * @param None. + * @retval None. + */ + void L6474_ReleaseReset(void) + { + standby_reset = 1; + } + + /** + * @brief Putting the device in reset mode. + * @param None. + * @retval None. + */ + void L6474_Reset(void) + { + standby_reset = 0; + } + + /** + * @brief Setting the direction of rotation. + * @param gpioState direction of rotation: "1" for forward, "0" for backward. + * @retval None. + */ + void L6474_SetDirectionGpio(uint8_t gpioState) + { + direction = gpioState; + } + + /** + * @brief Writing and reading bytes to/from the component through the SPI at the same time. + * @param[in] pByteToTransmit pointer to the buffer of data to send. + * @param[out] pReceivedByte pointer to the buffer to read data into. + * @retval "0" in case of success, "1" otherwise. + */ + uint8_t L6474_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) + { + return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, number_of_devices) == COMPONENT_OK ? 0 : 1); + } + + + /*** Component's Instance Variables ***/ + + /* ACTION 9 --------------------------------------------------------------* + * Declare here interrupt related variables, if needed. * + * Note that interrupt handling is platform dependent, see * + * "Interrupt Related Methods" above. * + * * + * Example: * + * + mbed: * + * InterruptIn feature_irq; * + *------------------------------------------------------------------------*/ + /* Flag Interrupt. */ + InterruptIn flag_irq; + + /* ACTION 10 -------------------------------------------------------------* + * Declare here other pin related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut standby_reset; * + *------------------------------------------------------------------------*/ + /* Standby/reset pin. */ + DigitalOut standby_reset; + + /* Direction of rotation pin. */ + DigitalOut direction; + + /* Pulse Width Modulation pin. */ + PwmOut pwm; + + /* Timer to trigger the PWM callback at each PWM pulse. */ + Ticker ticker; + + /* ACTION 11 -------------------------------------------------------------* + * Declare here communication related variables, if needed. * + * * + * Example: * + * + mbed: * + * DigitalOut ssel; * + * DevSPI &dev_spi; * + *------------------------------------------------------------------------*/ + /* Configuration. */ + DigitalOut ssel; + + /* IO Device. */ + DevSPI &dev_spi; + + /* ACTION 12 -------------------------------------------------------------* + * Declare here identity related variables, if needed. * + * Note that there should be only a unique identifier for each component, * + * which should be the "who_am_i" parameter. * + *------------------------------------------------------------------------*/ + /* Identity */ + uint8_t who_am_i; + + /* ACTION 13 -------------------------------------------------------------* + * Declare here the component's static and non-static data, one variable * + * per line. * + * * + * Example: * + * float measure; * + * int instance_id; * + * static int number_of_instances; * + *------------------------------------------------------------------------*/ + /* Data. */ + void (*error_handler_callback)(uint16_t error); + deviceParams_t device_prm; + uint8_t device_instance; + + /* Static data. */ + static uint8_t number_of_devices; + static uint8_t spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + static uint8_t spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + + +public: + + /* Static data. */ + static bool spi_preemtion_by_isr; + static bool isr_flag; +}; + +#endif // __L6474_CLASS_H + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/L6474/L6474_config.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Components/L6474/L6474_config.h Mon Mar 13 17:45:14 2017 +0000 @@ -0,0 +1,229 @@ +/** + ****************************************************************************** + * @file L6474_target_config.h + * @author IPC Rennes + * @version V1.5.0 + * @date November 12, 2014 + * @brief Predefines values for the L6474 registers + * and for the devices parameters + * @note (C) COPYRIGHT 2014 STMicroelectronics + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +/* Define to prevent recursive inclusion -------------------------------------*/ + +#ifndef __L6474_TARGET_CONFIG_H +#define __L6474_TARGET_CONFIG_H + +#ifdef __cplusplus + extern "C" { +#endif + + +/* Definitions ---------------------------------------------------------------*/ + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup L6474 + * @{ + */ + +/** @addtogroup L6474_Exported_Constants + * @{ + */ + +/** @defgroup Predefined_L6474_Registers_Values + * @{ + */ + +/// The maximum number of devices in the daisy chain +#define MAX_NUMBER_OF_DEVICES (3) + +/************************ Speed Profile *******************************/ + +/// Acceleration rate in step/s2 for device 0 (must be greater than 0) +#define L6474_CONF_PARAM_ACC_DEVICE_0 (160) +/// Acceleration rate in step/s2 for device 1 (must be greater than 0) +#define L6474_CONF_PARAM_ACC_DEVICE_1 (160) +/// Acceleration rate in step/s2 for device 2 (must be greater than 0) +#define L6474_CONF_PARAM_ACC_DEVICE_2 (160) + +/// Deceleration rate in step/s2 for device 0 (must be greater than 0) +#define L6474_CONF_PARAM_DEC_DEVICE_0 (160) +/// Deceleration rate in step/s2 for device 1 (must be greater than 0) +#define L6474_CONF_PARAM_DEC_DEVICE_1 (160) +/// Deceleration rate in step/s2 for device 2 (must be greater than 0) +#define L6474_CONF_PARAM_DEC_DEVICE_2 (160) + +/// Maximum speed in step/s for device 0 (30 step/s < Maximum speed <= 10 000 step/s ) +#define L6474_CONF_PARAM_MAX_SPEED_DEVICE_0 (1600) +/// Maximum speed in step/s for device 1 (30 step/s < Maximum speed <= 10 000 step/s ) +#define L6474_CONF_PARAM_MAX_SPEED_DEVICE_1 (1600) +/// Maximum speed in step/s for device 2 (30 step/s < Maximum speed <= 10 000 step/s ) +#define L6474_CONF_PARAM_MAX_SPEED_DEVICE_2 (1600) + +/// Minimum speed in step/s for device 0 (30 step/s <= Minimum speed < 10 000 step/s) +#define L6474_CONF_PARAM_MIN_SPEED_DEVICE_0 (800) +/// Minimum speed in step/s for device 1 (30 step/s <= Minimum speed < 10 000 step/s) +#define L6474_CONF_PARAM_MIN_SPEED_DEVICE_1 (800) +/// Minimum speed in step/s for device 2 (30 step/s <= Minimum speed < 10 000 step/s) +#define L6474_CONF_PARAM_MIN_SPEED_DEVICE_2 (800) + + +/************************ Phase Current Control *******************************/ + +// Current value that is assigned to the torque regulation DAC +/// TVAL register value for device 0 (range 31.25mA to 4000mA) +#define L6474_CONF_PARAM_TVAL_DEVICE_0 (250) +/// TVAL register value for device 1 (range 31.25mA to 4000mA) +#define L6474_CONF_PARAM_TVAL_DEVICE_1 (250) +/// TVAL register value for device 2 (range 31.25mA to 4000mA) +#define L6474_CONF_PARAM_TVAL_DEVICE_2 (250) + +/// Fall time value (T_FAST field of T_FAST register) for device 0 (range 2us to 32us) +#define L6474_CONF_PARAM_FAST_STEP_DEVICE_0 (L6474_FAST_STEP_12us) +/// Fall time value (T_FAST field of T_FAST register) for device 1 (range 2us to 32us) +#define L6474_CONF_PARAM_FAST_STEP_DEVICE_1 (L6474_FAST_STEP_12us) +/// Fall time value (T_FAST field of T_FAST register) for device 2 (range 2us to 32us) +#define L6474_CONF_PARAM_FAST_STEP_DEVICE_2 (L6474_FAST_STEP_12us) + +/// Maximum fast decay time (T_OFF field of T_FAST register) for device 0 (range 2us to 32us) +#define L6474_CONF_PARAM_TOFF_FAST_DEVICE_0 (L6474_TOFF_FAST_8us) +/// Maximum fast decay time (T_OFF field of T_FAST register) for device 1 (range 2us to 32us) +#define L6474_CONF_PARAM_TOFF_FAST_DEVICE_1 (L6474_TOFF_FAST_8us) +/// Maximum fast decay time (T_OFF field of T_FAST register) for device 2 (range 2us to 32us) +#define L6474_CONF_PARAM_TOFF_FAST_DEVICE_2 (L6474_TOFF_FAST_8us) + +/// Minimum ON time (TON_MIN register) for device 0 (range 0.5us to 64us) +#define L6474_CONF_PARAM_TON_MIN_DEVICE_0 (3) +/// Minimum ON time (TON_MIN register) for device 1 (range 0.5us to 64us) +#define L6474_CONF_PARAM_TON_MIN_DEVICE_1 (3) +/// Minimum ON time (TON_MIN register) for device 2 (range 0.5us to 64us) +#define L6474_CONF_PARAM_TON_MIN_DEVICE_2 (3) + +/// Minimum OFF time (TOFF_MIN register) for device 0 (range 0.5us to 64us) +#define L6474_CONF_PARAM_TOFF_MIN_DEVICE_0 (21) +/// Minimum OFF time (TOFF_MIN register) for device 1 (range 0.5us to 64us) +#define L6474_CONF_PARAM_TOFF_MIN_DEVICE_1 (21) +/// Minimum OFF time (TOFF_MIN register) for device 2 (range 0.5us to 64us) +#define L6474_CONF_PARAM_TOFF_MIN_DEVICE_2 (21) + +/******************************* Others ***************************************/ + +/// Overcurrent threshold settings for device 0 (OCD_TH register) +#define L6474_CONF_PARAM_OCD_TH_DEVICE_0 (L6474_OCD_TH_750mA) +/// Overcurrent threshold settings for device 1 (OCD_TH register) +#define L6474_CONF_PARAM_OCD_TH_DEVICE_1 (L6474_OCD_TH_750mA) +/// Overcurrent threshold settings for device 2 (OCD_TH register) +#define L6474_CONF_PARAM_OCD_TH_DEVICE_2 (L6474_OCD_TH_750mA) + +/// Alarm settings for device 0 (ALARM_EN register) +#define L6474_CONF_PARAM_ALARM_EN_DEVICE_0 (L6474_ALARM_EN_OVERCURRENT |\ + L6474_ALARM_EN_THERMAL_SHUTDOWN |\ + L6474_ALARM_EN_THERMAL_WARNING |\ + L6474_ALARM_EN_UNDERVOLTAGE |\ + L6474_ALARM_EN_SW_TURN_ON |\ + L6474_ALARM_EN_WRONG_NPERF_CMD) + +///Alarm settings for device 1 (ALARM_EN register) +#define L6474_CONF_PARAM_ALARM_EN_DEVICE_1 (L6474_ALARM_EN_OVERCURRENT |\ + L6474_ALARM_EN_THERMAL_SHUTDOWN |\ + L6474_ALARM_EN_THERMAL_WARNING |\ + L6474_ALARM_EN_UNDERVOLTAGE |\ + L6474_ALARM_EN_SW_TURN_ON |\ + L6474_ALARM_EN_WRONG_NPERF_CMD) + +/// Alarm settings for device 2 (ALARM_EN register) +#define L6474_CONF_PARAM_ALARM_EN_DEVICE_2 (L6474_ALARM_EN_OVERCURRENT |\ + L6474_ALARM_EN_THERMAL_SHUTDOWN |\ + L6474_ALARM_EN_THERMAL_WARNING |\ + L6474_ALARM_EN_UNDERVOLTAGE |\ + L6474_ALARM_EN_SW_TURN_ON |\ + L6474_ALARM_EN_WRONG_NPERF_CMD) + +/// Step selection settings for device 0 (STEP_SEL field of STEP_MODE register) +#define L6474_CONF_PARAM_STEP_SEL_DEVICE_0 (L6474_STEP_SEL_1_16) +/// Step selection settings for device 1 (STEP_SEL field of STEP_MODE register) +#define L6474_CONF_PARAM_STEP_SEL_DEVICE_1 (L6474_STEP_SEL_1_16) +/// Step selection settings for device 2 (STEP_SEL field of STEP_MODE register) +#define L6474_CONF_PARAM_STEP_SEL_DEVICE_2 (L6474_STEP_SEL_1_16) + +/// Synch. selection settings for device 0 (SYNC_SEL field of STEP_MODE register) +#define L6474_CONF_PARAM_SYNC_SEL_DEVICE_0 (L6474_SYNC_SEL_1_2) +/// Synch. selection settings for device 1 (SYNC_SEL field of STEP_MODE register) +#define L6474_CONF_PARAM_SYNC_SEL_DEVICE_1 (L6474_SYNC_SEL_1_2) +/// Synch. selection settings for device 2 (SYNC_SEL field of STEP_MODE register) +#define L6474_CONF_PARAM_SYNC_SEL_DEVICE_2 (L6474_SYNC_SEL_1_2) + +/// Target Swicthing Period for device 0 (field TOFF of CONFIG register) +#define L6474_CONF_PARAM_TOFF_DEVICE_0 (L6474_CONFIG_TOFF_044us) +/// Target Swicthing Period for device 1 (field TOFF of CONFIG register) +#define L6474_CONF_PARAM_TOFF_DEVICE_1 (L6474_CONFIG_TOFF_044us) +/// Target Swicthing Period for device 2 (field TOFF of CONFIG register) +#define L6474_CONF_PARAM_TOFF_DEVICE_2 (L6474_CONFIG_TOFF_044us) + +/// Slew rate for device 0 (POW_SR field of CONFIG register) +#define L6474_CONF_PARAM_SR_DEVICE_0 (L6474_CONFIG_SR_320V_us) +/// Slew rate for device 1 (POW_SR field of CONFIG register) +#define L6474_CONF_PARAM_SR_DEVICE_1 (L6474_CONFIG_SR_320V_us) +/// Slew rate for device 2 (POW_SR field of CONFIG register) +#define L6474_CONF_PARAM_SR_DEVICE_2 (L6474_CONFIG_SR_320V_us) + +/// Over current shutwdown enabling for device 0 (OC_SD field of CONFIG register) +#define L6474_CONF_PARAM_OC_SD_DEVICE_0 (L6474_CONFIG_OC_SD_ENABLE) +/// Over current shutwdown enabling for device 1 (OC_SD field of CONFIG register) +#define L6474_CONF_PARAM_OC_SD_DEVICE_1 (L6474_CONFIG_OC_SD_ENABLE) +/// Over current shutwdown enabling for device 2 (OC_SD field of CONFIG register) +#define L6474_CONF_PARAM_OC_SD_DEVICE_2 (L6474_CONFIG_OC_SD_ENABLE) + +/// Torque regulation method for device 0 (EN_TQREG field of CONFIG register) +#define L6474_CONF_PARAM_TQ_REG_DEVICE_0 (L6474_CONFIG_EN_TQREG_TVAL_USED) +///Torque regulation method for device 1 (EN_TQREG field of CONFIG register) +#define L6474_CONF_PARAM_TQ_REG_DEVICE_1 (L6474_CONFIG_EN_TQREG_TVAL_USED) +/// Torque regulation method for device 2 (EN_TQREG field of CONFIG register) +#define L6474_CONF_PARAM_TQ_REG_DEVICE_2 (L6474_CONFIG_EN_TQREG_TVAL_USED) + +/// Clock setting for device 0 (OSC_CLK_SEL field of CONFIG register) +#define L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_0 (L6474_CONFIG_INT_16MHZ) +/// Clock setting for device 1 (OSC_CLK_SEL field of CONFIG register) +#define L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_1 (L6474_CONFIG_INT_16MHZ) +/// Clock setting for device 2 (OSC_CLK_SEL field of CONFIG register) +#define L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_2 (L6474_CONFIG_INT_16MHZ) + +#ifdef __cplusplus + } +#endif + +#endif /* __L6474_TARGET_CONFIG_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/L6474/L6474_def.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Components/L6474/L6474_def.h Mon Mar 13 17:45:14 2017 +0000 @@ -0,0 +1,600 @@ +/** + ****************************************************************************** + * @file L6474_def.h + * @author IPC Rennes + * @version V1.5.0 + * @date November 12, 2014 + * @brief Header for L6474 driver (fully integrated microstepping motor driver) + * @note (C) COPYRIGHT 2014 STMicroelectronics + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +/* Define to prevent recursive inclusion -------------------------------------*/ + +#ifndef __L6474_H +#define __L6474_H + +#ifdef __cplusplus + extern "C" { +#endif + + +/* Includes ------------------------------------------------------------------*/ + +#include "L6474_config.h" +#include "../Common/motor_def.h" + + +/* Definitions ---------------------------------------------------------------*/ + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup Components + * @{ + */ + +/** @addtogroup L6474 + * @{ + */ + +/** @defgroup L6474_Exported_Defines L6474_Exported_Defines + * @{ + */ + +/// Current FW version +#define L6474_FW_VERSION (5) + +/// L6474 max number of bytes of command & arguments to set a parameter +#define L6474_CMD_ARG_MAX_NB_BYTES (4) + +/// L6474 command + argument bytes number for GET_STATUS command +#define L6474_CMD_ARG_NB_BYTES_GET_STATUS (1) + +/// L6474 response bytes number +#define L6474_RSP_NB_BYTES_GET_STATUS (2) + +/// L6474 value mask for ABS_POS register +#define L6474_ABS_POS_VALUE_MASK ((uint32_t) 0x003FFFFF) + +/// L6474 sign bit mask for ABS_POS register +#define L6474_ABS_POS_SIGN_BIT_MASK ((uint32_t) 0x00200000) + + +/* Types ---------------------------------------------------------------------*/ + +/** @defgroup L6474_Exported_Types + * @{ + */ + +/** @defgroup L6474_Fast_Decay_Time_Options + * @{ + */ +///TOFF_FAST values for T_FAST register +typedef enum { + L6474_TOFF_FAST_2us = ((uint8_t) 0x00 << 4), + L6474_TOFF_FAST_4us = ((uint8_t) 0x01 << 4), + L6474_TOFF_FAST_6us = ((uint8_t) 0x02 << 4), + L6474_TOFF_FAST_8us = ((uint8_t) 0x03 << 4), + L6474_TOFF_FAST_10us = ((uint8_t) 0x04 << 4), + L6474_TOFF_FAST_12us = ((uint8_t) 0x05 << 4), + L6474_TOFF_FAST_14us = ((uint8_t) 0x06 << 4), + L6474_TOFF_FAST_16us = ((uint8_t) 0x07 << 4), + L6474_TOFF_FAST_18us = ((uint8_t) 0x08 << 4), + L6474_TOFF_FAST_20us = ((uint8_t) 0x09 << 4), + L6474_TOFF_FAST_22us = ((uint8_t) 0x0A << 4), + L6474_TOFF_FAST_24us = ((uint8_t) 0x0B << 4), + L6474_TOFF_FAST_26us = ((uint8_t) 0x0C << 4), + L6474_TOFF_FAST_28us = ((uint8_t) 0x0D << 4), + L6474_TOFF_FAST_30us = ((uint8_t) 0x0E << 4), + L6474_TOFF_FAST_32us = ((uint8_t) 0x0F << 4) +} L6474_TOFF_FAST_t; +/** + * @} + */ + +/** @defgroup L6474_Fall_Step_Time_Options + * @{ + */ +///FAST_STEP values for T_FAST register +typedef enum { + L6474_FAST_STEP_2us = ((uint8_t) 0x00), + L6474_FAST_STEP_4us = ((uint8_t) 0x01), + L6474_FAST_STEP_6us = ((uint8_t) 0x02), + L6474_FAST_STEP_8us = ((uint8_t) 0x03), + L6474_FAST_STEP_10us = ((uint8_t) 0x04), + L6474_FAST_STEP_12us = ((uint8_t) 0x05), + L6474_FAST_STEP_14us = ((uint8_t) 0x06), + L6474_FAST_STEP_16us = ((uint8_t) 0x07), + L6474_FAST_STEP_18us = ((uint8_t) 0x08), + L6474_FAST_STEP_20us = ((uint8_t) 0x09), + L6474_FAST_STEP_22us = ((uint8_t) 0x0A), + L6474_FAST_STEP_24us = ((uint8_t) 0x0B), + L6474_FAST_STEP_26us = ((uint8_t) 0x0C), + L6474_FAST_STEP_28us = ((uint8_t) 0x0D), + L6474_FAST_STEP_30us = ((uint8_t) 0x0E), + L6474_FAST_STEP_32us = ((uint8_t) 0x0F) +} L6474_FAST_STEP_t; +/** + * @} + */ + +/** @defgroup L6474_Overcurrent_Threshold_options + * @{ + */ +///OCD_TH register +typedef enum { + L6474_OCD_TH_375mA = ((uint8_t) 0x00), + L6474_OCD_TH_750mA = ((uint8_t) 0x01), + L6474_OCD_TH_1125mA = ((uint8_t) 0x02), + L6474_OCD_TH_1500mA = ((uint8_t) 0x03), + L6474_OCD_TH_1875mA = ((uint8_t) 0x04), + L6474_OCD_TH_2250mA = ((uint8_t) 0x05), + L6474_OCD_TH_2625mA = ((uint8_t) 0x06), + L6474_OCD_TH_3000mA = ((uint8_t) 0x07), + L6474_OCD_TH_3375mA = ((uint8_t) 0x08), + L6474_OCD_TH_3750mA = ((uint8_t) 0x09), + L6474_OCD_TH_4125mA = ((uint8_t) 0x0A), + L6474_OCD_TH_4500mA = ((uint8_t) 0x0B), + L6474_OCD_TH_4875mA = ((uint8_t) 0x0C), + L6474_OCD_TH_5250mA = ((uint8_t) 0x0D), + L6474_OCD_TH_5625mA = ((uint8_t) 0x0E), + L6474_OCD_TH_6000mA = ((uint8_t) 0x0F) +} L6474_OCD_TH_t; +/** + * @} + */ + +/** @defgroup L6474_STEP_MODE_Register_Masks + * @{ + */ +///STEP_MODE register +typedef enum { + L6474_STEP_MODE_STEP_SEL = ((uint8_t) 0x07), + L6474_STEP_MODE_SYNC_SEL = ((uint8_t) 0x70) +} L6474_STEP_MODE_Masks_t; +/** + * @} + */ + +/** @defgroup L6474_STEP_SEL_Options_For_STEP_MODE_Register + * @{ + */ +///STEP_SEL field of STEP_MODE register +typedef enum { + L6474_STEP_SEL_1 = ((uint8_t) 0x08), //full step + L6474_STEP_SEL_1_2 = ((uint8_t) 0x09), //half step + L6474_STEP_SEL_1_4 = ((uint8_t) 0x0A), //1/4 microstep + L6474_STEP_SEL_1_8 = ((uint8_t) 0x0B), //1/8 microstep + L6474_STEP_SEL_1_16 = ((uint8_t) 0x0C) //1/16 microstep +} L6474_STEP_SEL_t; +/** + * @} + */ + +/** @defgroup L6474_SYNC_SEL_Options_For_STEP_MODE_Register + * @{ + */ +///SYNC_SEL field of STEP_MODE register +typedef enum { + L6474_SYNC_SEL_1_2 = ((uint8_t) 0x80), + L6474_SYNC_SEL_1 = ((uint8_t) 0x90), + L6474_SYNC_SEL_2 = ((uint8_t) 0xA0), + L6474_SYNC_SEL_4 = ((uint8_t) 0xB0), + L6474_SYNC_SEL_8 = ((uint8_t) 0xC0), + L6474_SYNC_SEL_UNUSED = ((uint8_t) 0xD0) +} L6474_SYNC_SEL_t; +/** + * @} + */ + +/** @defgroup L6474_ALARM_EN_Register_Options + * @{ + */ +///ALARM_EN register +typedef enum { + L6474_ALARM_EN_OVERCURRENT = ((uint8_t) 0x01), + L6474_ALARM_EN_THERMAL_SHUTDOWN = ((uint8_t) 0x02), + L6474_ALARM_EN_THERMAL_WARNING = ((uint8_t) 0x04), + L6474_ALARM_EN_UNDERVOLTAGE = ((uint8_t) 0x08), + L6474_ALARM_EN_SW_TURN_ON = ((uint8_t) 0x40), + L6474_ALARM_EN_WRONG_NPERF_CMD = ((uint8_t) 0x80) +} L6474_ALARM_EN_t; +/** + * @} + */ + +/** @defgroup L6474_CONFIG_Register_Masks + * @{ + */ +///CONFIG register +typedef enum { + L6474_CONFIG_OSC_SEL = ((uint16_t) 0x0007), + L6474_CONFIG_EXT_CLK = ((uint16_t) 0x0008), + L6474_CONFIG_EN_TQREG = ((uint16_t) 0x0020), + L6474_CONFIG_OC_SD = ((uint16_t) 0x0080), + L6474_CONFIG_POW_SR = ((uint16_t) 0x0300), + L6474_CONFIG_TOFF = ((uint16_t) 0x7C00) +} L6474_CONFIG_Masks_t; +/** + * @} + */ + +/** @defgroup L6474_Clock_Source_Options_For_CONFIG_Register + * @{ + */ +///Clock source option for CONFIG register +typedef enum { + L6474_CONFIG_INT_16MHZ = ((uint16_t) 0x0000), + L6474_CONFIG_INT_16MHZ_OSCOUT_2MHZ = ((uint16_t) 0x0008), + L6474_CONFIG_INT_16MHZ_OSCOUT_4MHZ = ((uint16_t) 0x0009), + L6474_CONFIG_INT_16MHZ_OSCOUT_8MHZ = ((uint16_t) 0x000A), + L6474_CONFIG_INT_16MHZ_OSCOUT_16MHZ = ((uint16_t) 0x000B), + L6474_CONFIG_EXT_8MHZ_XTAL_DRIVE = ((uint16_t) 0x0004), + L6474_CONFIG_EXT_16MHZ_XTAL_DRIVE = ((uint16_t) 0x0005), + L6474_CONFIG_EXT_24MHZ_XTAL_DRIVE = ((uint16_t) 0x0006), + L6474_CONFIG_EXT_32MHZ_XTAL_DRIVE = ((uint16_t) 0x0007), + L6474_CONFIG_EXT_8MHZ_OSCOUT_INVERT = ((uint16_t) 0x000C), + L6474_CONFIG_EXT_16MHZ_OSCOUT_INVERT = ((uint16_t) 0x000D), + L6474_CONFIG_EXT_24MHZ_OSCOUT_INVERT = ((uint16_t) 0x000E), + L6474_CONFIG_EXT_32MHZ_OSCOUT_INVERT = ((uint16_t) 0x000F) +} L6474_CONFIG_OSC_MGMT_t; +/** + * @} + */ + +/** @defgroup L6474_External_Torque_Regulation_Options_For_CONFIG_Register + * @{ + */ +///External Torque regulation options for CONFIG register +typedef enum { + L6474_CONFIG_EN_TQREG_TVAL_USED = ((uint16_t) 0x0000), + L6474_CONFIG_EN_TQREG_ADC_OUT = ((uint16_t) 0x0020) +} L6474_CONFIG_EN_TQREG_t; +/** + * @} + */ + +/** @defgroup L6474_Over_Current_Shutdown_Options_For_CONFIG_Register + * @{ + */ +///Over Current Shutdown options for CONFIG register +typedef enum { + L6474_CONFIG_OC_SD_DISABLE = ((uint16_t) 0x0000), + L6474_CONFIG_OC_SD_ENABLE = ((uint16_t) 0x0080) +} L6474_CONFIG_OC_SD_t; +/** + * @} + */ + +/** @defgroup L6474_Power_Bridge_Output_Slew_Rate_Options + * @{ + */ +/// POW_SR values for CONFIG register +typedef enum { + L6474_CONFIG_SR_320V_us =((uint16_t)0x0000), + L6474_CONFIG_SR_075V_us =((uint16_t)0x0100), + L6474_CONFIG_SR_110V_us =((uint16_t)0x0200), + L6474_CONFIG_SR_260V_us =((uint16_t)0x0300) +} L6474_CONFIG_POW_SR_t; +/** + * @} + */ + +/** @defgroup L6474_Off_Time_Options + * @{ + */ +/// TOFF values for CONFIG register +typedef enum { + L6474_CONFIG_TOFF_004us = (((uint16_t) 0x01) << 10), + L6474_CONFIG_TOFF_008us = (((uint16_t) 0x02) << 10), + L6474_CONFIG_TOFF_012us = (((uint16_t) 0x03) << 10), + L6474_CONFIG_TOFF_016us = (((uint16_t) 0x04) << 10), + L6474_CONFIG_TOFF_020us = (((uint16_t) 0x05) << 10), + L6474_CONFIG_TOFF_024us = (((uint16_t) 0x06) << 10), + L6474_CONFIG_TOFF_028us = (((uint16_t) 0x07) << 10), + L6474_CONFIG_TOFF_032us = (((uint16_t) 0x08) << 10), + L6474_CONFIG_TOFF_036us = (((uint16_t) 0x09) << 10), + L6474_CONFIG_TOFF_040us = (((uint16_t) 0x0A) << 10), + L6474_CONFIG_TOFF_044us = (((uint16_t) 0x0B) << 10), + L6474_CONFIG_TOFF_048us = (((uint16_t) 0x0C) << 10), + L6474_CONFIG_TOFF_052us = (((uint16_t) 0x0D) << 10), + L6474_CONFIG_TOFF_056us = (((uint16_t) 0x0E) << 10), + L6474_CONFIG_TOFF_060us = (((uint16_t) 0x0F) << 10), + L6474_CONFIG_TOFF_064us = (((uint16_t) 0x10) << 10), + L6474_CONFIG_TOFF_068us = (((uint16_t) 0x11) << 10), + L6474_CONFIG_TOFF_072us = (((uint16_t) 0x12) << 10), + L6474_CONFIG_TOFF_076us = (((uint16_t) 0x13) << 10), + L6474_CONFIG_TOFF_080us = (((uint16_t) 0x14) << 10), + L6474_CONFIG_TOFF_084us = (((uint16_t) 0x15) << 10), + L6474_CONFIG_TOFF_088us = (((uint16_t) 0x16) << 10), + L6474_CONFIG_TOFF_092us = (((uint16_t) 0x17) << 10), + L6474_CONFIG_TOFF_096us = (((uint16_t) 0x18) << 10), + L6474_CONFIG_TOFF_100us = (((uint16_t) 0x19) << 10), + L6474_CONFIG_TOFF_104us = (((uint16_t) 0x1A) << 10), + L6474_CONFIG_TOFF_108us = (((uint16_t) 0x1B) << 10), + L6474_CONFIG_TOFF_112us = (((uint16_t) 0x1C) << 10), + L6474_CONFIG_TOFF_116us = (((uint16_t) 0x1D) << 10), + L6474_CONFIG_TOFF_120us = (((uint16_t) 0x1E) << 10), + L6474_CONFIG_TOFF_124us = (((uint16_t) 0x1F) << 10) +} L6474_CONFIG_TOFF_t; +/** + * @} + */ + +/** @defgroup L6474_STATUS_Register_Bit_Masks + * @{ + */ +///STATUS Register Bit Masks +typedef enum { + L6474_STATUS_HIZ = (((uint16_t) 0x0001)), + L6474_STATUS_DIR = (((uint16_t) 0x0010)), + L6474_STATUS_NOTPERF_CMD = (((uint16_t) 0x0080)), + L6474_STATUS_WRONG_CMD = (((uint16_t) 0x0100)), + L6474_STATUS_UVLO = (((uint16_t) 0x0200)), + L6474_STATUS_TH_WRN = (((uint16_t) 0x0400)), + L6474_STATUS_TH_SD = (((uint16_t) 0x0800)), + L6474_STATUS_OCD = (((uint16_t) 0x1000)) +} L6474_STATUS_Masks_t; +/** + * @} + */ + +/** @defgroup L6474_Direction_Field_Of_STATUS_Register + * @{ + */ +///Diretion field of STATUS register +typedef enum { + L6474_STATUS_DIR_FORWARD = (((uint16_t) 0x0001) << 4), + L6474_STATUS_DIR_REVERSE = (((uint16_t) 0x0000) << 4) +} L6474_STATUS_DIR_t; +/** + * @} + */ + +/** @defgroup L6474_Internal_Register_Addresses + * @{ + */ +/// Internal L6474 register addresses +typedef enum { + L6474_ABS_POS = ((uint8_t) 0x01), + L6474_EL_POS = ((uint8_t) 0x02), + L6474_MARK = ((uint8_t) 0x03), + L6474_RESERVED_REG01 = ((uint8_t) 0x04), + L6474_RESERVED_REG02 = ((uint8_t) 0x05), + L6474_RESERVED_REG03 = ((uint8_t) 0x06), + L6474_RESERVED_REG04 = ((uint8_t) 0x07), + L6474_RESERVED_REG05 = ((uint8_t) 0x08), + L6474_RESERVED_REG06 = ((uint8_t) 0x15), + L6474_TVAL = ((uint8_t) 0x09), + L6474_RESERVED_REG07 = ((uint8_t) 0x0A), + L6474_RESERVED_REG08 = ((uint8_t) 0x0B), + L6474_RESERVED_REG09 = ((uint8_t) 0x0C), + L6474_RESERVED_REG10 = ((uint8_t) 0x0D), + L6474_T_FAST = ((uint8_t) 0x0E), + L6474_TON_MIN = ((uint8_t) 0x0F), + L6474_TOFF_MIN = ((uint8_t) 0x10), + L6474_RESERVED_REG11 = ((uint8_t) 0x11), + L6474_ADC_OUT = ((uint8_t) 0x12), + L6474_OCD_TH = ((uint8_t) 0x13), + L6474_RESERVED_REG12 = ((uint8_t) 0x14), + L6474_STEP_MODE = ((uint8_t) 0x16), + L6474_ALARM_EN = ((uint8_t) 0x17), + L6474_CONFIG = ((uint8_t) 0x18), + L6474_STATUS = ((uint8_t) 0x19), + L6474_RESERVED_REG13 = ((uint8_t) 0x1A), + L6474_RESERVED_REG14 = ((uint8_t) 0x1B), + L6474_INEXISTENT_REG = ((uint8_t) 0x1F) +} L6474_Registers_t; +/** + * @} + */ + +/** @defgroup L6474_Command_Set + * @{ + */ +/// L6474 command set +typedef enum { + L6474_NOP = ((uint8_t) 0x00), + L6474_SET_PARAM = ((uint8_t) 0x00), + L6474_GET_PARAM = ((uint8_t) 0x20), + L6474_ENABLE = ((uint8_t) 0xB8), + L6474_DISABLE = ((uint8_t) 0xA8), + L6474_GET_STATUS = ((uint8_t) 0xD0), + L6474_RESERVED_CMD1 = ((uint8_t) 0xEB), + L6474_RESERVED_CMD2 = ((uint8_t) 0xF8) +} L6474_Commands_t; + +/** + * @brief L6474 driver initialization structure definition. + */ +/* ACTION --------------------------------------------------------------------* + * Declare here the component's initialization structure, if any, one * + * variable per line without initialization. * + * * + * Example: * + * typedef struct * + * { * + * int frequency; * + * int update_mode; * + * } COMPONENT_Init_t; * + *----------------------------------------------------------------------------*/ +typedef struct +{ + /* Acceleration rate in pps^2. Range: (0..+inf). */ + int acceleration_pps_2; + + /* Deceleration rate in pps^2. Range: (0..+inf). */ + int deceleration_pps_2; + + /* Maximum speed in pps. Range: (30..10000]. */ + int maximum_speed_pps; + + /* Minimum speed in pps. Range: [30..10000). */ + int minimum_speed_pps; + + /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ + float torque_regulation_current_mA; + + /* Overcurrent threshold (OCD_TH register). */ + L6474_OCD_TH_t overcurrent_threshold; + + /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ + L6474_CONFIG_OC_SD_t overcurrent_shutwdown; + + /* Torque regulation method (EN_TQREG field of CONFIG register). */ + L6474_CONFIG_EN_TQREG_t torque_regulation_method; + + /* Step selection (STEP_SEL field of STEP_MODE register). */ + L6474_STEP_SEL_t step_selection; + + /* Sync selection (SYNC_SEL field of STEP_MODE register). */ + L6474_SYNC_SEL_t sync_selection; + + /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ + L6474_FAST_STEP_t fall_time; + + /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ + L6474_TOFF_FAST_t maximum_fast_decay_time; + + /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ + float minimum_ON_time_us; + + /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ + float minimum_OFF_time_us; + + /* Target Swicthing Period (field TOFF of CONFIG register). */ + L6474_CONFIG_TOFF_t target_swicthing_period; + + /* Slew rate (POW_SR field of CONFIG register). */ + L6474_CONFIG_POW_SR_t slew_rate; + + /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ + L6474_CONFIG_OSC_MGMT_t clock; + + /* Alarm (ALARM_EN register). */ + int alarm; +} L6474_init_t; + +/** + * @brief L6474 driver data structure definition. + */ +/* ACTION --------------------------------------------------------------------* + * Declare here the structure of component's data, if any, one variable per * + * line without initialization. * + * * + * Example: * + * typedef struct * + * { * + * int T0_out; * + * int T1_out; * + * float T0_degC; * + * float T1_degC; * + * } COMPONENT_Data_t; * + *----------------------------------------------------------------------------*/ +typedef struct +{ + /// Function pointer to flag interrupt call back + void (*flagInterruptCallback)(void); + /// Function pointer to error handler call back + void (*errorHandlerCallback)(uint16_t error); + bool spiPreemtionByIsr; // = FALSE; + bool isrFlag; // = FALSE; + /// L6474 Device Paramaters structure + deviceParams_t devicePrm; //[MAX_NUMBER_OF_DEVICES]; + uint8_t number_of_devices; + uint8_t device_instance; + uint8_t spiTxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; + uint8_t spiRxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; +} L6474_Data_t; + + +/* Functions -----------------------------------------------------------------*/ + +/** @addtogroup BSP + * @{ + */ + +/** @addtogroup Components + * @{ + */ + +/** @addtogroup L6474 + * @{ + */ + +/** @defgroup L6474_Imported_Functions L6474_Imported_Functions + * @{ + */ + +/* ACTION --------------------------------------------------------------------* + * Declare here extern platform-dependent APIs you might need (e.g.: I/O and * + * interrupt related functions), and implement them in a glue-logic file on * + * the target environment, for example within the "x_nucleo_board.c" file. * + * E.g.: * + * extern status_t COMPONENT_IO_Init (void *handle); * + * extern status_t COMPONENT_IO_Read (handle, buf, regadd, bytes); * + * extern status_t COMPONENT_IO_Write(handle, buf, regadd, bytes); * + * extern void COMPONENT_IO_ITConfig(void); * + *----------------------------------------------------------------------------*/ +///Delay of the requested number of milliseconds +extern void L6474_Delay(void *handle, uint32_t delay); +///Enable Irq +extern void L6474_EnableIrq(void *handle); +///Disable Irq +extern void L6474_DisableIrq(void *handle); +///Set PWM1 frequency and start it +extern void L6474_Pwm1SetFreq(void *handle, uint16_t newFreq); +///Set PWM2 frequency and start it +extern void L6474_Pwm2SetFreq(void *handle, uint16_t newFreq); +///Set PWM3 frequency and start it +extern void L6474_Pwm3SetFreq(void *handle, uint16_t newFreq); +///Init the PWM +extern void L6474_PwmInit(void *handle); +///Stop the PWM +extern void L6474_PwmStop(void *handle); +///Reset the L6474 reset pin +extern void L6474_ReleaseReset(void *handle); +///Set the L6474 reset pin +extern void L6474_Reset(void *handle); +///Set direction GPIO +extern void L6474_SetDirectionGpio(void *handle, uint8_t gpioState); +///Write bytes to the L6474s via SPI +extern uint8_t L6474_SpiWriteBytes(void *handle, uint8_t *pByteToTransmit, uint8_t *pReceivedByte); + +#ifdef __cplusplus + } +#endif + +#endif /* #ifndef __L6474_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/l6474/L6474.cpp --- a/Components/l6474/L6474.cpp Fri Mar 10 14:07:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1421 +0,0 @@ -/** - ****************************************************************************** - * @file L6474.cpp - * @author IPC Rennes - * @version V1.5.0 - * @date November 12, 2014 - * @brief L6474 driver (fully integrated microstepping motor driver) - * @note (C) COPYRIGHT 2014 STMicroelectronics - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - - -/* Generated with STM32CubeTOO -----------------------------------------------*/ - - -/* Revision ------------------------------------------------------------------*/ -/* - Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev - Branch/Trunk/Tag: trunk - Based on: X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.c - Revision: 0 -*/ - - -/* Includes ------------------------------------------------------------------*/ - -#include "L6474.h" - - -/* Definitions ---------------------------------------------------------------*/ - -/* Error while initialising the SPI. */ -#define L6474_ERROR_0 (0x8000) - -/* Error of bad SPI transaction. */ -#define L6474_ERROR_1 (0x8001) - -/* Maximum number of steps. */ -#define MAX_STEPS (0x7FFFFFFF) - -/* Maximum frequency of the PWMs in Hz. */ -#define L6474_MAX_PWM_FREQ (10000) - -/* Minimum frequency of the PWMs in Hz. */ -#define L6474_MIN_PWM_FREQ (2) - - -/* Variables ----------------------------------------------------------------*/ - -/* Number of devices. */ -uint8_t L6474::number_of_devices = 0; - -/* ISR flags used to restart an interrupted SPI transfer when an error is reported. */ -bool L6474::spi_preemtion_by_isr = FALSE; -bool L6474::isr_flag = FALSE; - -/* SPI Transmission for Daisy-Chain Configuration. */ -uint8_t L6474::spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; -uint8_t L6474::spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; - - -/* Methods -------------------------------------------------------------------*/ - -/********************************************************** - * @brief Attaches a user callback to the error Handler. - * The call back will be then called each time the library - * detects an error - * @param[in] callback Name of the callback to attach - * to the error Hanlder - * @retval None - **********************************************************/ -void L6474::L6474_AttachErrorHandler(void (*callback)(uint16_t error)) -{ - error_handler_callback = (void (*)(uint16_t error)) callback; -} - -/********************************************************** - * @brief Starts the L6474 library - * @param init Initialization structure. - * @retval COMPONENT_OK in case of success. - **********************************************************/ -status_t L6474::L6474_Init(void *init) -{ - /* Initialise the PWMs used for the Step clocks ----------------------------*/ - L6474_PwmInit(); - - /* Initialise the L6474s ------------------------------------------------*/ - - /* Standby-reset deactivation */ - L6474_ReleaseReset(); - - /* Let a delay after reset */ - L6474_Delay(1); - - /* Set device parameters to the predefined values from "l6474_target_config.h". */ - L6474_SetDeviceParamsToPredefinedValues(); - - if (init == NULL) - /* Set device registers to the predefined values from "l6474_target_config.h". */ - L6474_SetRegisterToPredefinedValues(); - else - /* Set device registers to the passed initialization values. */ - L6474_SetRegisterToInitializationValues((L6474_init_t *) init); - - /* Disable L6474 powerstage */ - L6474_CmdDisable(); - - /* Get Status to clear flags after start up */ - L6474_CmdGetStatus(); - - return COMPONENT_OK; -} - -/********************************************************** - * @brief Read id - * @param id pointer to the identifier to be read. - * @retval COMPONENT_OK in case of success. - **********************************************************/ -status_t L6474::L6474_ReadID(uint8_t *id) -{ - *id = device_instance; - - return COMPONENT_OK; -} - -/********************************************************** - * @brief Returns the acceleration of the specified device - * @retval Acceleration in pps^2 - **********************************************************/ -uint16_t L6474::L6474_GetAcceleration(void) -{ - return (device_prm.acceleration); -} - -/********************************************************** - * @brief Returns the current speed of the specified device - * @retval Speed in pps - **********************************************************/ -uint16_t L6474::L6474_GetCurrentSpeed(void) -{ - return device_prm.speed; -} - -/********************************************************** - * @brief Returns the deceleration of the specified device - * @retval Deceleration in pps^2 - **********************************************************/ -uint16_t L6474::L6474_GetDeceleration(void) -{ - return (device_prm.deceleration); -} - -/********************************************************** - * @brief Returns the device state - * @retval State (ACCELERATING, DECELERATING, STEADY or INACTIVE) - **********************************************************/ -motorState_t L6474::L6474_GetDeviceState(void) -{ - return device_prm.motionState; -} - -/********************************************************** - * @brief Returns the FW version of the library - * @param None - * @retval L6474_FW_VERSION - **********************************************************/ -uint8_t L6474::L6474_GetFwVersion(void) -{ - return (L6474_FW_VERSION); -} - -/********************************************************** - * @brief Returns the mark position of the specified device - * @retval Mark register value converted in a 32b signed integer - **********************************************************/ -int32_t L6474::L6474_GetMark(void) -{ - return L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); -} - -/********************************************************** - * @brief Returns the max speed of the specified device - * @retval maxSpeed in pps - **********************************************************/ -uint16_t L6474::L6474_GetMaxSpeed(void) -{ - return (device_prm.maxSpeed); -} - -/********************************************************** - * @brief Returns the min speed of the specified device - * @retval minSpeed in pps - **********************************************************/ -uint16_t L6474::L6474_GetMinSpeed(void) -{ - return (device_prm.minSpeed); -} - -/********************************************************** - * @brief Returns the ABS_POSITION of the specified device - * @retval ABS_POSITION register value converted in a 32b signed integer - **********************************************************/ -int32_t L6474::L6474_GetPosition(void) -{ - return L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); -} - -/********************************************************** - * @brief Requests the motor to move to the home position (ABS_POSITION = 0) - * @retval None - **********************************************************/ -void L6474::L6474_GoHome(void) -{ - L6474_GoTo(0); -} - -/********************************************************** - * @brief Requests the motor to move to the mark position - * @retval None - **********************************************************/ -void L6474::L6474_GoMark(void) -{ - uint32_t mark; - - mark = L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); - L6474_GoTo(mark); -} - -/********************************************************** - * @brief Requests the motor to move to the specified position - * @param[in] targetPosition absolute position in steps - * @retval None - **********************************************************/ -void L6474::L6474_GoTo(int32_t targetPosition) -{ - motorDir_t direction; - int32_t steps; - - /* Eventually deactivate motor */ - if (device_prm.motionState != INACTIVE) - { - L6474_HardStop(); - } - - /* Get current position */ - device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); - - /* Compute the number of steps to perform */ - steps = targetPosition - device_prm.currentPosition; - - if (steps >= 0) - { - device_prm.stepsToTake = steps; - direction = FORWARD; - } - else - { - device_prm.stepsToTake = -steps; - direction = BACKWARD; - } - - if (steps != 0) - { - device_prm.commandExecuted = MOVE_CMD; - - /* Direction setup */ - L6474_SetDirection(direction); - - L6474_ComputeSpeedProfile(device_prm.stepsToTake); - - /* Motor activation */ - L6474_StartMovement(); - } -} - -/********************************************************** - * @brief Immediatly stops the motor and disable the power bridge - * @retval None - **********************************************************/ -void L6474::L6474_HardStop(void) -{ - /* Disable corresponding PWM */ - L6474_PwmStop(); - - /* Set inactive state */ - device_prm.motionState = INACTIVE; - device_prm.commandExecuted = NO_CMD; - device_prm.stepsToTake = MAX_STEPS; -} - -/********************************************************** - * @brief Moves the motor of the specified number of steps - * @param[in] direction FORWARD or BACKWARD - * @param[in] stepCount Number of steps to perform - * @retval None - **********************************************************/ -void L6474::L6474_Move(motorDir_t direction, uint32_t stepCount) -{ - /* Eventually deactivate motor */ - if (device_prm.motionState != INACTIVE) - { - L6474_HardStop(); - } - - if (stepCount != 0) - { - device_prm.stepsToTake = stepCount; - - device_prm.commandExecuted = MOVE_CMD; - - device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); - - /* Direction setup */ - L6474_SetDirection(direction); - - L6474_ComputeSpeedProfile(stepCount); - - /* Motor activation */ - L6474_StartMovement(); - } -} - -/********************************************************** - * @brief Runs the motor. It will accelerate from the min - * speed up to the max speed by using the device acceleration. - * @param[in] direction FORWARD or BACKWARD - * @retval None - **********************************************************/ -void L6474::L6474_Run(motorDir_t direction) -{ - /* Eventually deactivate motor */ - if (device_prm.motionState != INACTIVE) - { - L6474_HardStop(); - } - - /* Direction setup */ - L6474_SetDirection(direction); - - device_prm.commandExecuted = RUN_CMD; - - /* Motor activation */ - L6474_StartMovement(); -} - -/********************************************************** - * @brief Changes the acceleration of the specified device - * @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 is the device is executing - * a MOVE or GOTO command (but it can be used during a RUN command) - **********************************************************/ -bool L6474::L6474_SetAcceleration(uint16_t newAcc) -{ - bool cmdExecuted = FALSE; - if ((newAcc != 0)&& - ((device_prm.motionState == INACTIVE)|| - (device_prm.commandExecuted == RUN_CMD))) - { - device_prm.acceleration = newAcc; - cmdExecuted = TRUE; - } - return cmdExecuted; -} - -/********************************************************** - * @brief Changes the deceleration of the specified device - * @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 is the device is executing - * a MOVE or GOTO command (but it can be used during a RUN command) - **********************************************************/ -bool L6474::L6474_SetDeceleration(uint16_t newDec) -{ - bool cmdExecuted = FALSE; - if ((newDec != 0)&& - ((device_prm.motionState == INACTIVE)|| - (device_prm.commandExecuted == RUN_CMD))) - { - device_prm.deceleration = newDec; - cmdExecuted = TRUE; - } - return cmdExecuted; -} - -/********************************************************** - * @brief Set current position to be the Home position (ABS pos set to 0) - * @retval None - **********************************************************/ -void L6474::L6474_SetHome(void) -{ - L6474_CmdSetParam(L6474_ABS_POS, 0); -} - -/********************************************************** - * @brief Sets current position to be the Mark position - * @retval None - **********************************************************/ -void L6474::L6474_SetMark(void) -{ - uint32_t mark = L6474_CmdGetParam(L6474_ABS_POS); - L6474_CmdSetParam(L6474_MARK, mark); -} - -/********************************************************** - * @brief Changes the max speed of the specified device - * @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 L6474::L6474_SetMaxSpeed(uint16_t newMaxSpeed) -{ - bool cmdExecuted = FALSE; - if ((newMaxSpeed >= L6474_MIN_PWM_FREQ)&& - (newMaxSpeed <= L6474_MAX_PWM_FREQ) && - (device_prm.minSpeed <= newMaxSpeed) && - ((device_prm.motionState == INACTIVE)|| - (device_prm.commandExecuted == RUN_CMD))) - { - device_prm.maxSpeed = newMaxSpeed; - cmdExecuted = TRUE; - } - return cmdExecuted; -} - -/********************************************************** - * @brief Changes the min speed of the specified device - * @param[in] 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 L6474::L6474_SetMinSpeed(uint16_t newMinSpeed) -{ - bool cmdExecuted = FALSE; - if ((newMinSpeed >= L6474_MIN_PWM_FREQ)&& - (newMinSpeed <= L6474_MAX_PWM_FREQ) && - (newMinSpeed <= device_prm.maxSpeed) && - ((device_prm.motionState == INACTIVE)|| - (device_prm.commandExecuted == RUN_CMD))) - { - device_prm.minSpeed = newMinSpeed; - cmdExecuted = TRUE; - } - return cmdExecuted; -} - -/********************************************************** - * @brief Stops the motor by using the device deceleration - * @retval true if the command is successfully executed, else false - * @note The command is not performed is the device is in INACTIVE state. - **********************************************************/ -bool L6474::L6474_SoftStop(void) -{ - bool cmdExecuted = FALSE; - if (device_prm.motionState != INACTIVE) - { - device_prm.commandExecuted = SOFT_STOP_CMD; - cmdExecuted = TRUE; - } - return (cmdExecuted); -} - -/********************************************************** - * @brief Locks until the device state becomes Inactive - * @retval None - **********************************************************/ -void L6474::L6474_WaitWhileActive(void) -{ - /* Wait while motor is running */ - while (L6474_GetDeviceState() != INACTIVE); -} - -/********************************************************** - * @brief Issue the Disable command to the L6474 of the specified device - * @retval None - **********************************************************/ -void L6474::L6474_CmdDisable(void) -{ - L6474_SendCommand(L6474_DISABLE); -} - -/********************************************************** - * @brief Issues the Enable command to the L6474 of the specified device - * @retval None - **********************************************************/ -void L6474::L6474_CmdEnable(void) -{ - L6474_SendCommand(L6474_ENABLE); -} - -/********************************************************** - * @brief Issues the GetParam command to the L6474 of the specified device - * @param[in] parameter Register adress (L6474_ABS_POS, L6474_MARK,...) - * @retval Register value - **********************************************************/ -uint32_t L6474::L6474_CmdGetParam(L6474_Registers_t parameter) -{ - uint32_t i; - uint32_t spiRxData; - uint8_t maxArgumentNbBytes = 0; - uint8_t spiIndex = number_of_devices - device_instance - 1; - bool itDisable = FALSE; - - do - { - spi_preemtion_by_isr = FALSE; - if (itDisable) - { - /* re-enable L6474_EnableIrq if disable in previous iteration */ - L6474_EnableIrq(); - itDisable = FALSE; - } - - for (i = 0; i < number_of_devices; i++) - { - spi_tx_bursts[0][i] = L6474_NOP; - spi_tx_bursts[1][i] = L6474_NOP; - spi_tx_bursts[2][i] = L6474_NOP; - spi_tx_bursts[3][i] = L6474_NOP; - spi_rx_bursts[1][i] = 0; - spi_rx_bursts[2][i] = 0; - spi_rx_bursts[3][i] = 0; - } - - switch (parameter) - { - case L6474_ABS_POS: ; - case L6474_MARK: - spi_tx_bursts[0][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); - maxArgumentNbBytes = 3; - break; - case L6474_EL_POS: ; - case L6474_CONFIG: ; - case L6474_STATUS: - spi_tx_bursts[1][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); - maxArgumentNbBytes = 2; - break; - default: - spi_tx_bursts[2][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); - maxArgumentNbBytes = 1; - } - - /* Disable interruption before checking */ - /* pre-emption by ISR and SPI transfers*/ - L6474_DisableIrq(); - itDisable = TRUE; - } while (spi_preemtion_by_isr); // check pre-emption by ISR - - for (i = L6474_CMD_ARG_MAX_NB_BYTES-1-maxArgumentNbBytes; - i < L6474_CMD_ARG_MAX_NB_BYTES; - i++) - { - L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); - } - - spiRxData = ((uint32_t)spi_rx_bursts[1][spiIndex] << 16) | - (spi_rx_bursts[2][spiIndex] << 8) | - (spi_rx_bursts[3][spiIndex]); - - /* re-enable L6474_EnableIrq after SPI transfers*/ - L6474_EnableIrq(); - - return (spiRxData); -} - -/********************************************************** - * @brief Issues the GetStatus command to the L6474 of the specified device - * @retval Status Register value - * @note Once the GetStatus command is performed, the flags of the status register - * are reset. This is not the case when the status register is read with the - * GetParam command (via the functions L6474ReadStatusRegister or L6474_CmdGetParam). - **********************************************************/ -uint16_t L6474::L6474_CmdGetStatus(void) -{ - uint32_t i; - uint16_t status; - uint8_t spiIndex = number_of_devices - device_instance - 1; - bool itDisable = FALSE; - - do - { - spi_preemtion_by_isr = FALSE; - if (itDisable) - { - /* re-enable L6474_EnableIrq if disable in previous iteration */ - L6474_EnableIrq(); - itDisable = FALSE; - } - - for (i = 0; i < number_of_devices; i++) - { - spi_tx_bursts[0][i] = L6474_NOP; - spi_tx_bursts[1][i] = L6474_NOP; - spi_tx_bursts[2][i] = L6474_NOP; - spi_rx_bursts[1][i] = 0; - spi_rx_bursts[2][i] = 0; - } - spi_tx_bursts[0][spiIndex] = L6474_GET_STATUS; - - /* Disable interruption before checking */ - /* pre-emption by ISR and SPI transfers*/ - L6474_DisableIrq(); - itDisable = TRUE; - } while (spi_preemtion_by_isr); // check pre-emption by ISR - - for (i = 0; i < L6474_CMD_ARG_NB_BYTES_GET_STATUS + L6474_RSP_NB_BYTES_GET_STATUS; i++) - { - L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); - } - status = (spi_rx_bursts[1][spiIndex] << 8) | (spi_rx_bursts[2][spiIndex]); - - /* re-enable L6474_EnableIrq after SPI transfers*/ - L6474_EnableIrq(); - - return (status); -} - -/********************************************************** - * @brief Issues the Nop command to the L6474 of the specified device - * @retval None - **********************************************************/ -void L6474::L6474_CmdNop(void) -{ - L6474_SendCommand(L6474_NOP); -} - -/********************************************************** - * @brief Issues the SetParam command to the L6474 of the specified device - * @param[in] parameter Register adress (L6474_ABS_POS, L6474_MARK,...) - * @param[in] value Value to set in the register - * @retval None - **********************************************************/ -void L6474::L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value) -{ - uint32_t i; - uint8_t maxArgumentNbBytes = 0; - uint8_t spiIndex = number_of_devices - device_instance - 1; - bool itDisable = FALSE; - do - { - spi_preemtion_by_isr = FALSE; - if (itDisable) - { - /* re-enable L6474_EnableIrq if disable in previous iteration */ - L6474_EnableIrq(); - itDisable = FALSE; - } - - for (i = 0; i < number_of_devices; i++) - { - spi_tx_bursts[0][i] = L6474_NOP; - spi_tx_bursts[1][i] = L6474_NOP; - spi_tx_bursts[2][i] = L6474_NOP; - spi_tx_bursts[3][i] = L6474_NOP; - } - - switch (parameter) - { - case L6474_ABS_POS: ; - case L6474_MARK: - spi_tx_bursts[0][spiIndex] = parameter; - spi_tx_bursts[1][spiIndex] = (uint8_t)(value >> 16); - spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); - maxArgumentNbBytes = 3; - break; - case L6474_EL_POS: ; - case L6474_CONFIG: - spi_tx_bursts[1][spiIndex] = parameter; - spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); - maxArgumentNbBytes = 2; - break; - default: - spi_tx_bursts[2][spiIndex] = parameter; - maxArgumentNbBytes = 1; - break; - } - spi_tx_bursts[3][spiIndex] = (uint8_t)(value); - - /* Disable interruption before checking */ - /* pre-emption by ISR and SPI transfers*/ - L6474_DisableIrq(); - itDisable = TRUE; - } while (spi_preemtion_by_isr); // check pre-emption by ISR - - /* SPI transfer */ - for (i = L6474_CMD_ARG_MAX_NB_BYTES-1-maxArgumentNbBytes; - i < L6474_CMD_ARG_MAX_NB_BYTES; - i++) - { - L6474_WriteBytes(&spi_tx_bursts[i][0],&spi_rx_bursts[i][0]); - } - /* re-enable L6474_EnableIrq after SPI transfers*/ - L6474_EnableIrq(); -} - -/********************************************************** - * @brief Reads the Status Register value - * @retval Status register valued - * @note The status register flags are not cleared - * at the difference with L6474CmdGetStatus() - **********************************************************/ -uint16_t L6474::L6474_ReadStatusRegister(void) -{ - return (L6474_CmdGetParam(L6474_STATUS)); -} - -/********************************************************** - * @brief Set the stepping mode - * @param[in] stepMod from full step to 1/16 microstep as specified in enum motorStepMode_t - * @retval None - **********************************************************/ -void L6474::L6474_SelectStepMode(motorStepMode_t stepMod) -{ - uint8_t stepModeRegister; - L6474_STEP_SEL_t l6474StepMod; - - switch (stepMod) - { - case STEP_MODE_FULL: - l6474StepMod = L6474_STEP_SEL_1; - break; - case STEP_MODE_HALF: - l6474StepMod = L6474_STEP_SEL_1_2; - break; - case STEP_MODE_1_4: - l6474StepMod = L6474_STEP_SEL_1_4; - break; - case STEP_MODE_1_8: - l6474StepMod = L6474_STEP_SEL_1_8; - break; - case STEP_MODE_1_16: - default: - l6474StepMod = L6474_STEP_SEL_1_16; - break; - } - - /* Eventually deactivate motor */ - if (device_prm.motionState != INACTIVE) - { - L6474_HardStop(); - } - - /* Read Step mode register and clear STEP_SEL field */ - stepModeRegister = (uint8_t)(0xF8 & L6474_CmdGetParam(L6474_STEP_MODE)) ; - - /* Apply new step mode */ - L6474_CmdSetParam(L6474_STEP_MODE, stepModeRegister | (uint8_t)l6474StepMod); - - /* Reset abs pos register */ - L6474_SetHome(); -} - -/********************************************************** - * @brief Get the direction - * @param None - * @retval direction FORWARD or BACKWARD - **********************************************************/ -motorDir_t L6474::L6474_GetDirection(void) -{ - return device_prm.direction; -} - -/********************************************************** - * @brief Specifies the direction - * @param[in] dir FORWARD or BACKWARD - * @note The direction change is only applied if the device - * is in INACTIVE state - * @retval None - **********************************************************/ -void L6474::L6474_SetDirection(motorDir_t direction) -{ - if (device_prm.motionState == INACTIVE) - { - device_prm.direction = direction; - L6474_SetDirectionGpio(direction); - } -} - -/********************************************************** - * @brief Updates the current speed of the device - * @param[in] newSpeed in pps - * @retval None - **********************************************************/ -void L6474::L6474_ApplySpeed(uint16_t newSpeed) -{ - if (newSpeed < L6474_MIN_PWM_FREQ) - { - newSpeed = L6474_MIN_PWM_FREQ; - } - if (newSpeed > L6474_MAX_PWM_FREQ) - { - newSpeed = L6474_MAX_PWM_FREQ; - } - - device_prm.speed = newSpeed; - - L6474_PwmSetFreq(newSpeed); -} - -/********************************************************** - * @brief Computes the speed profile according to the number of steps to move - * @param[in] nbSteps number of steps to perform - * @retval None - * @note Using the acceleration and deceleration of the device, - * this function determines the duration in steps of the acceleration, - * steady and deceleration phases. - * If the total number of steps to perform is big enough, a trapezoidal move - * is performed (i.e. there is a steady phase where the motor runs at the maximum - * speed. - * Else, a triangular move is performed (no steady phase: the maximum speed is never - * reached. - **********************************************************/ -void L6474::L6474_ComputeSpeedProfile(uint32_t nbSteps) -{ - uint32_t reqAccSteps; - uint32_t reqDecSteps; - - /* compute the number of steps to get the targeted speed */ - uint16_t minSpeed = device_prm.minSpeed; - reqAccSteps = (device_prm.maxSpeed - minSpeed); - reqAccSteps *= (device_prm.maxSpeed + minSpeed); - reqDecSteps = reqAccSteps; - reqAccSteps /= (uint32_t)device_prm.acceleration; - reqAccSteps /= 2; - - /* compute the number of steps to stop */ - reqDecSteps /= (uint32_t)device_prm.deceleration; - reqDecSteps /= 2; - - if(( reqAccSteps + reqDecSteps ) > nbSteps) - { - /* Triangular move */ - /* reqDecSteps = (Pos * Dec) /(Dec+Acc) */ - uint32_t dec = device_prm.deceleration; - uint32_t acc = device_prm.acceleration; - - reqDecSteps = ((uint32_t) dec * nbSteps) / (acc + dec); - if (reqDecSteps > 1) - { - reqAccSteps = reqDecSteps - 1; - if(reqAccSteps == 0) - { - reqAccSteps = 1; - } - } - else - { - reqAccSteps = 0; - } - device_prm.endAccPos = reqAccSteps; - device_prm.startDecPos = reqDecSteps; - } - else - { - /* Trapezoidal move */ - /* accelerating phase to endAccPos */ - /* steady phase from endAccPos to startDecPos */ - /* decelerating from startDecPos to stepsToTake*/ - device_prm.endAccPos = reqAccSteps; - device_prm.startDecPos = nbSteps - reqDecSteps - 1; - } -} - -/********************************************************** - * @brief Converts the ABS_POSITION register value to a 32b signed integer - * @param[in] abs_position_reg value of the ABS_POSITION register - * @retval operation_result 32b signed integer corresponding to the absolute position - **********************************************************/ -int32_t L6474::L6474_ConvertPosition(uint32_t abs_position_reg) -{ - int32_t operation_result; - - if (abs_position_reg & L6474_ABS_POS_SIGN_BIT_MASK) - { - /* Negative register value */ - abs_position_reg = ~abs_position_reg; - abs_position_reg += 1; - - operation_result = (int32_t) (abs_position_reg & L6474_ABS_POS_VALUE_MASK); - operation_result = -operation_result; - } - else - { - operation_result = (int32_t) abs_position_reg; - } - - return operation_result; -} - -/********************************************************** - * @brief Error handler which calls the user callback (if defined) - * @param[in] error Number of the error - * @retval None - **********************************************************/ -void L6474::L6474_ErrorHandler(uint16_t error) -{ - if (error_handler_callback != 0) - { - (void) error_handler_callback(error); - } - else - { - /* Aborting the program. */ - exit(EXIT_FAILURE); - } -} - -/********************************************************** - * @brief Sends a command without arguments to the L6474 via the SPI - * @param[in] param Command to send - * @retval None - **********************************************************/ -void L6474::L6474_SendCommand(uint8_t param) -{ - uint32_t i; - bool itDisable = FALSE; - uint8_t spiIndex = number_of_devices - device_instance - 1; - - do - { - spi_preemtion_by_isr = FALSE; - if (itDisable) - { - /* re-enable L6474_EnableIrq if disable in previous iteration */ - L6474_EnableIrq(); - itDisable = FALSE; - } - - for (i = 0; i < number_of_devices; i++) - { - spi_tx_bursts[3][i] = L6474_NOP; - } - spi_tx_bursts[3][spiIndex] = param; - - /* Disable interruption before checking */ - /* pre-emption by ISR and SPI transfers*/ - L6474_DisableIrq(); - itDisable = TRUE; - } while (spi_preemtion_by_isr); // check pre-emption by ISR - - L6474_WriteBytes(&spi_tx_bursts[3][0], &spi_rx_bursts[3][0]); - - /* re-enable L6474_EnableIrq after SPI transfers*/ - L6474_EnableIrq(); -} - -/********************************************************** - * @brief Sets the registers of the L6474 to their predefined values - * from l6474_target_config.h - * @retval None - **********************************************************/ -void L6474::L6474_SetRegisterToPredefinedValues(void) -{ - L6474_CmdSetParam( - L6474_ABS_POS, - 0); - L6474_CmdSetParam( - L6474_EL_POS, - 0); - L6474_CmdSetParam( - L6474_MARK, - 0); - switch (device_instance) - { - case 0: - L6474_CmdSetParam( - L6474_TVAL, - L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_0)); - L6474_CmdSetParam( - L6474_T_FAST, - (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_0 | - (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_0); - L6474_CmdSetParam( - L6474_TON_MIN, - L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_0) - ); - L6474_CmdSetParam( - L6474_TOFF_MIN, - L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_0)); - L6474_CmdSetParam( - L6474_OCD_TH, - L6474_CONF_PARAM_OCD_TH_DEVICE_0); - L6474_CmdSetParam( - L6474_STEP_MODE, - (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_0 | - (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_0); - L6474_CmdSetParam( - L6474_ALARM_EN, - L6474_CONF_PARAM_ALARM_EN_DEVICE_0); - L6474_CmdSetParam( - L6474_CONFIG, - (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_0 | - (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_0 | - (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_0 | - (uint16_t)L6474_CONF_PARAM_SR_DEVICE_0 | - (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_0); - break; - case 1: - L6474_CmdSetParam( - L6474_TVAL, - L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_1)); - L6474_CmdSetParam( - L6474_T_FAST, - (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_1 | - (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_1); - L6474_CmdSetParam( - L6474_TON_MIN, - L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_1)); - L6474_CmdSetParam( - L6474_TOFF_MIN, - L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_1)); - L6474_CmdSetParam( - L6474_OCD_TH, - L6474_CONF_PARAM_OCD_TH_DEVICE_1); - L6474_CmdSetParam( - L6474_STEP_MODE, - (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_1 | - (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_1); - L6474_CmdSetParam( - L6474_ALARM_EN, - L6474_CONF_PARAM_ALARM_EN_DEVICE_1); - L6474_CmdSetParam( - L6474_CONFIG, - (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_1 | - (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_1 | - (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_1 | - (uint16_t)L6474_CONF_PARAM_SR_DEVICE_1 | - (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_1); - break; - case 2: - L6474_CmdSetParam( - L6474_TVAL, - L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_2)); - L6474_CmdSetParam( - L6474_T_FAST, - (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_2 | - (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_2); - L6474_CmdSetParam( - L6474_TON_MIN, - L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_2)); - L6474_CmdSetParam( - L6474_TOFF_MIN, - L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_2)); - L6474_CmdSetParam( - L6474_OCD_TH, - L6474_CONF_PARAM_OCD_TH_DEVICE_2); - L6474_CmdSetParam( - L6474_STEP_MODE, - (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_2 | - (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_2); - L6474_CmdSetParam( - L6474_ALARM_EN, - L6474_CONF_PARAM_ALARM_EN_DEVICE_2); - L6474_CmdSetParam( - L6474_CONFIG, - (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_2 | - (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_2 | - (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_2 | - (uint16_t)L6474_CONF_PARAM_SR_DEVICE_2 | - (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_2); - break; - default: ; - } -} - -/********************************************************** - * @brief Sets the registers of the L6474 to initialization values. - * @param init Initialization structure. - * @retval None. - **********************************************************/ -void L6474::L6474_SetRegisterToInitializationValues(L6474_init_t *init) -{ - L6474_CmdSetParam( - L6474_ABS_POS, - 0 - ); - L6474_CmdSetParam( - L6474_EL_POS, - 0 - ); - L6474_CmdSetParam( - L6474_MARK, - 0 - ); - L6474_CmdSetParam( - L6474_TVAL, - L6474_Tval_Current_to_Par(init->torque_regulation_current_mA) - ); - L6474_CmdSetParam( - L6474_T_FAST, - (uint8_t) init->maximum_fast_decay_time | - (uint8_t) init->fall_time - ); - L6474_CmdSetParam( - L6474_TON_MIN, - L6474_Tmin_Time_to_Par(init->minimum_ON_time_us) - ); - L6474_CmdSetParam( - L6474_TOFF_MIN, - L6474_Tmin_Time_to_Par(init->minimum_OFF_time_us) - ); - L6474_CmdSetParam( - L6474_OCD_TH, - init->overcurrent_threshold - ); - L6474_CmdSetParam( - L6474_STEP_MODE, - (uint8_t) init->step_selection | - (uint8_t) init->sync_selection - ); - L6474_CmdSetParam( - L6474_ALARM_EN, - init->alarm - ); - L6474_CmdSetParam( - L6474_CONFIG, - (uint16_t) init->clock | - (uint16_t) init->torque_regulation_method | - (uint16_t) init->overcurrent_shutwdown | - (uint16_t) init->slew_rate | - (uint16_t) init->target_swicthing_period - ); - L6474_SetAcceleration((uint16_t) init->acceleration_pps_2); - L6474_SetDeceleration((uint16_t) init->deceleration_pps_2); - L6474_SetMaxSpeed((uint16_t) init->maximum_speed_pps); - L6474_SetMinSpeed((uint16_t) init->minimum_speed_pps); -} - -/********************************************************** - * @brief Sets the parameters of the device to predefined values - * from l6474_target_config.h - * @param None - * @retval None - **********************************************************/ -void L6474::L6474_SetDeviceParamsToPredefinedValues(void) -{ - switch (device_instance) - { - case 0: - device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_0; - device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_0; - device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_0; - device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_0; - break; - - case 1: - device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_1; - device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_1; - device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_1; - device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_1; - break; - - case 2: - device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_2; - device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_2; - device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_2; - device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_2; - break; - } - - device_prm.accu = 0; - device_prm.currentPosition = 0; - device_prm.endAccPos = 0; - device_prm.relativePos = 0; - device_prm.startDecPos = 0; - device_prm.stepsToTake = 0; - device_prm.speed = 0; - device_prm.commandExecuted = NO_CMD; - device_prm.direction = FORWARD; - device_prm.motionState = INACTIVE; -} - -/********************************************************** - * @brief Initialises the bridge parameters to start the movement - * and enable the power bridge - * @retval None - **********************************************************/ -void L6474::L6474_StartMovement(void) -{ - /* Enable L6474 powerstage */ - L6474_CmdEnable(); - if (device_prm.endAccPos != 0) - { - device_prm.motionState = ACCELERATING; - } - else - { - device_prm.motionState = DECELERATING; - } - device_prm.accu = 0; - device_prm.relativePos = 0; - L6474_ApplySpeed(device_prm.minSpeed); -} - -/********************************************************** - * @brief Handles the device state machine at each ste - * @retval None - * @note Must only be called by the timer ISR - **********************************************************/ -void L6474::L6474_StepClockHandler(void) -{ - /* Set isr flag */ - isr_flag = TRUE; - - /* Incrementation of the relative position */ - device_prm.relativePos++; - - switch (device_prm.motionState) - { - case ACCELERATING: - { - uint32_t relPos = device_prm.relativePos; - uint32_t endAccPos = device_prm.endAccPos; - uint16_t speed = device_prm.speed; - uint32_t acc = ((uint32_t)device_prm.acceleration << 16); - - if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| - ((device_prm.commandExecuted != RUN_CMD)&& - (relPos == device_prm.startDecPos))) - { - device_prm.motionState = DECELERATING; - device_prm.accu = 0; - } - else if ((speed >= device_prm.maxSpeed)|| - ((device_prm.commandExecuted != RUN_CMD)&& - (relPos == endAccPos))) - { - device_prm.motionState = STEADY; - } - else - { - bool speedUpdated = FALSE; - /* Go on accelerating */ - if (speed == 0) speed =1; - device_prm.accu += acc / speed; - while (device_prm.accu >= (0X10000L)) - { - device_prm.accu -= (0X10000L); - speed +=1; - speedUpdated = TRUE; - } - - if (speedUpdated) - { - if (speed > device_prm.maxSpeed) - { - speed = device_prm.maxSpeed; - } - device_prm.speed = speed; - L6474_ApplySpeed(device_prm.speed); - } - } - break; - } - case STEADY: - { - uint16_t maxSpeed = device_prm.maxSpeed; - uint32_t relativePos = device_prm.relativePos; - if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| - ((device_prm.commandExecuted != RUN_CMD)&& - (relativePos >= (device_prm.startDecPos))) || - ((device_prm.commandExecuted == RUN_CMD)&& - (device_prm.speed > maxSpeed))) - { - device_prm.motionState = DECELERATING; - device_prm.accu = 0; - } - else if ((device_prm.commandExecuted == RUN_CMD)&& - (device_prm.speed < maxSpeed)) - { - device_prm.motionState = ACCELERATING; - device_prm.accu = 0; - } - break; - } - case DECELERATING: - { - uint32_t relativePos = device_prm.relativePos; - uint16_t speed = device_prm.speed; - uint32_t deceleration = ((uint32_t)device_prm.deceleration << 16); - if (((device_prm.commandExecuted == SOFT_STOP_CMD)&&(speed <= device_prm.minSpeed))|| - ((device_prm.commandExecuted != RUN_CMD)&& - (relativePos >= device_prm.stepsToTake))) - { - /* Motion process complete */ - L6474_HardStop(); - } - else if ((device_prm.commandExecuted == RUN_CMD)&& - (speed <= device_prm.maxSpeed)) - { - device_prm.motionState = STEADY; - } - else - { - /* Go on decelerating */ - if (speed > device_prm.minSpeed) - { - bool speedUpdated = FALSE; - if (speed == 0) speed =1; - device_prm.accu += deceleration / speed; - while (device_prm.accu >= (0X10000L)) - { - device_prm.accu -= (0X10000L); - if (speed > 1) - { - speed -=1; - } - speedUpdated = TRUE; - } - - if (speedUpdated) - { - if (speed < device_prm.minSpeed) - { - speed = device_prm.minSpeed; - } - device_prm.speed = speed; - L6474_ApplySpeed(device_prm.speed); - } - } - } - break; - } - default: - { - break; - } - } - /* Set isr flag */ - isr_flag = FALSE; -} - -/********************************************************** - * @brief Converts current in mA to values for TVAL register - * @param[in] current_mA current in mA - * @retval value for TVAL register - **********************************************************/ -float L6474::L6474_Tval_Current_to_Par(float current_mA) -{ - return ((float)(((current_mA - 31.25f) / 31.25f) + 0.5f)); -} - -/********************************************************** - * @brief Converts values from TVAL register to mA - * @param[in] Tval value from TVAL register - * @retval current in mA - **********************************************************/ -float L6474::L6474_Par_to_Tval_Current(float Tval) -{ - return ((float)((Tval - 0.5f) * 31.25f + 31.25f)); -} - -/********************************************************** - * @brief Convert time in us to values for TON_MIN register - * @param[in] ton_min_us time in us - * @retval value for TON_MIN register - **********************************************************/ -float L6474::L6474_Tmin_Time_to_Par(float ton_min_us) -{ - return ((float)(((ton_min_us - 0.5f) * 2.0f) + 0.5f)); -} - -/********************************************************** - * @brief Convert values for TON_MIN register to time in us - * @param[in] Tmin value from TON_MIN register - * @retval time in us - **********************************************************/ -float L6474::L6474_Par_to_Tmin_Time(float Tmin) -{ - return ((float)(((Tmin - 0.5f) / 2.0f) + 0.5f)); -} - -/********************************************************** - * @brief Write and receive a byte via SPI - * @param[in] pByteToTransmit pointer to the byte to transmit - * @param[in] pReceivedByte pointer to the received byte - * @retval None - **********************************************************/ -void L6474::L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) -{ - if (L6474_SpiWriteBytes(pByteToTransmit, pReceivedByte) != 0) - { - L6474_ErrorHandler(L6474_ERROR_1); - } - - if (isr_flag) - { - spi_preemtion_by_isr = TRUE; - } -} - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/l6474/L6474.h --- a/Components/l6474/L6474.h Fri Mar 10 14:07:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1018 +0,0 @@ -/** - ****************************************************************************** - * @file L6474.h - * @author Davide Aliprandi, STMicroelectronics - * @version V1.0.0 - * @date October 14th, 2015 - * @brief This file contains the class of an L6474 Motor Control component. - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - - -/* Generated with STM32CubeTOO -----------------------------------------------*/ - - -/* Revision ------------------------------------------------------------------*/ -/* - Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev - Branch/Trunk/Tag: trunk - Based on: X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.h - Revision: 0 -*/ - - -/* Define to prevent recursive inclusion -------------------------------------*/ - -#ifndef __L6474_CLASS_H -#define __L6474_CLASS_H - - -/* Includes ------------------------------------------------------------------*/ - -/* ACTION 1 ------------------------------------------------------------------* - * Include here platform specific header files. * - *----------------------------------------------------------------------------*/ -#include "mbed.h" -#include "DevSPI.h" -/* ACTION 2 ------------------------------------------------------------------* - * Include here component specific header files. * - *----------------------------------------------------------------------------*/ -#include "L6474_def.h" -/* ACTION 3 ------------------------------------------------------------------* - * Include here interface specific header files. * - * * - * Example: * - * #include "HumiditySensor.h" * - * #include "TemperatureSensor.h" * - *----------------------------------------------------------------------------*/ -#include "StepperMotor.h" - - -/* Classes -------------------------------------------------------------------*/ - -/** - * @brief Class representing an L6474 component. - */ -class L6474 : public StepperMotor -{ -public: - - /*** Constructor and Destructor Methods ***/ - - /** - * @brief Constructor. - * @param flag_irq pin name of the FLAG pin of the component. - * @param standby_reset pin name of the STBY\RST pin of the component. - * @param direction pin name of the DIR pin of the component. - * @param pwm pin name of the PWM pin of the component. - * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. - * @param spi SPI device to be used for communication. - */ - L6474(PinName flag_irq, PinName standby_reset, PinName direction, PinName pwm, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), standby_reset(standby_reset), direction(direction), pwm(pwm), ssel(ssel), dev_spi(spi) - { - /* Checking stackability. */ - if (!(number_of_devices < MAX_NUMBER_OF_DEVICES)) { - error("Instantiation of the L6474 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES); - } - - /* ACTION 4 ----------------------------------------------------------* - * Initialize here the component's member variables, one variable per * - * line. * - * * - * Example: * - * measure = 0; * - * instance_id = number_of_instances++; * - *--------------------------------------------------------------------*/ - error_handler_callback = 0; - device_instance = number_of_devices++; - memset(spi_tx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); - memset(spi_rx_bursts, 0, L6474_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); - } - - /** - * @brief Destructor. - */ - virtual ~L6474(void) {} - - - /*** Public Component Related Methods ***/ - - /* ACTION 5 --------------------------------------------------------------* - * Implement here the component's public methods, as wrappers of the C * - * component's functions. * - * They should be: * - * + Methods with the same name of the C component's virtual table's * - * functions (1); * - * + Methods with the same name of the C component's extended virtual * - * table's functions, if any (2). * - * * - * Example: * - * virtual int get_value(float *p_data) //(1) * - * { * - * return COMPONENT_get_value(float *pf_data); * - * } * - * * - * virtual int enable_feature(void) //(2) * - * { * - * return COMPONENT_enable_feature(); * - * } * - *------------------------------------------------------------------------*/ - /** - * @brief Initializing the component in 1/16 Microstepping mode. - * @param init Pointer to device specific initalization structure. - * @retval "0" in case of success, an error code otherwise. - */ - virtual int init(void *init = NULL) - { - return (int) L6474_Init((void *) init); - } - - /** - * @brief Getting the ID of the component. - * @param id Pointer to an allocated variable to store the ID into. - * @retval "0" in case of success, an error code otherwise. - */ - virtual int read_id(uint8_t *id = NULL) - { - return (int) L6474_ReadID((uint8_t *) id); - } - - /** - * @brief Getting the value of the Status Register. - * @param None. - * @retval None. - * @note The Status Register's flags are cleared, contrary to the - * read_status_register() method. - */ - virtual unsigned int get_status(void) - { - return (unsigned int) L6474_CmdGetStatus(); - } - - /** - * @brief Getting a parameter. - * @param parameter A parameter's register address. - * @retval The parameter's value. - * @note The Status Register's flags are cleared, contrary to the - * read_status_register() method. - * The parameter can be one of the following: - * + L6474_ABS_POS - * + L6474_EL_POS - * + L6474_MARK - * + L6474_RESERVED_REG01 - * + L6474_RESERVED_REG02 - * + L6474_RESERVED_REG03 - * + L6474_RESERVED_REG04 - * + L6474_RESERVED_REG05 - * + L6474_RESERVED_REG06 - * + L6474_TVAL : value in mA - * + L6474_RESERVED_REG07 - * + L6474_RESERVED_REG08 - * + L6474_RESERVED_REG09 - * + L6474_RESERVED_REG10 - * + L6474_T_FAST - * + L6474_TON_MIN : value in us - * + L6474_TOFF_MIN : value in us - * + L6474_RESERVED_REG11 - * + L6474_ADC_OUT - * + L6474_OCD_TH - * + L6474_RESERVED_REG12 - * + L6474_STEP_MODE - * + L6474_ALARM_EN - * + L6474_CONFIG - * + L6474_STATUS - * + L6474_RESERVED_REG13 - * + L6474_RESERVED_REG14 - * + L6474_INEXISTENT_REG - */ - virtual float get_parameter(unsigned int parameter) - { - unsigned int register_value = (unsigned int) L6474_CmdGetParam((L6474_Registers_t) parameter); - float value; - - switch ((L6474_Registers_t) parameter) { - case L6474_TVAL: - value = L6474_Par_to_Tval_Current((float) register_value); - break; - case L6474_TON_MIN: - case L6474_TOFF_MIN: - value = L6474_Par_to_Tmin_Time((float) register_value); - break; - default: - value = (float) register_value; - break; - } - - return value; - } - - /** - * @brief Getting the position. - * @param None. - * @retval The position. - */ - virtual signed int get_position(void) - { - return (signed int) L6474_GetPosition(); - } - - /** - * @brief Getting the marked position. - * @param None. - * @retval The marked position. - */ - virtual signed int get_mark(void) - { - return (signed int) L6474_GetMark(); - } - - /** - * @brief Getting the current speed in pps. - * @param None. - * @retval The current speed in pps. - */ - virtual unsigned int get_speed(void) - { - return (unsigned int) L6474_GetCurrentSpeed(); - } - - /** - * @brief Getting the maximum speed in pps. - * @param None. - * @retval The maximum speed in pps. - */ - virtual unsigned int get_max_speed(void) - { - return (unsigned int) L6474_GetMaxSpeed(); - } - - /** - * @brief Getting the minimum speed in pps. - * @param None. - * @retval The minimum speed in pps. - */ - virtual unsigned int get_min_speed(void) - { - return (unsigned int) L6474_GetMinSpeed(); - } - - /** - * @brief Getting the acceleration in pps^2. - * @param None. - * @retval The acceleration in pps^2. - */ - virtual unsigned int get_acceleration(void) - { - return (unsigned int) L6474_GetAcceleration(); - } - - /** - * @brief Getting the deceleration in pps^2. - * @param None. - * @retval The deceleration in pps^2. - */ - virtual unsigned int get_deceleration(void) - { - return (unsigned int) L6474_GetDeceleration(); - } - - /** - * @brief Getting the direction of rotation. - * @param None. - * @retval The direction of rotation. - */ - virtual direction_t get_direction(void) - { - return (direction_t) (L6474_GetDirection() == FORWARD ? StepperMotor::FWD : StepperMotor::BWD); - } - - /** - * @brief Setting a parameter. - * @param parameter A parameter's register address. - * @param value The parameter's value. - * @retval None. - * @note The parameter can be one of the following: - * + L6474_ABS_POS - * + L6474_EL_POS - * + L6474_MARK - * + L6474_RESERVED_REG01 - * + L6474_RESERVED_REG02 - * + L6474_RESERVED_REG03 - * + L6474_RESERVED_REG04 - * + L6474_RESERVED_REG05 - * + L6474_RESERVED_REG06 - * + L6474_TVAL : value in mA - * + L6474_RESERVED_REG07 - * + L6474_RESERVED_REG08 - * + L6474_RESERVED_REG09 - * + L6474_RESERVED_REG10 - * + L6474_T_FAST - * + L6474_TON_MIN : value in us - * + L6474_TOFF_MIN : value in us - * + L6474_RESERVED_REG11 - * + L6474_ADC_OUT - * + L6474_OCD_TH - * + L6474_RESERVED_REG12 - * + L6474_STEP_MODE - * + L6474_ALARM_EN - * + L6474_CONFIG - * + L6474_STATUS - * + L6474_RESERVED_REG13 - * + L6474_RESERVED_REG14 - * + L6474_INEXISTENT_REG - * @warning Some registers can only be written in particular conditions (see L6474's datasheet). - * Any attempt to write one of those registers when the conditions are not satisfied - * causes the command to be ignored and the NOTPERF_CMD flag to rise at the end of the - * last argument byte. Any attempt to set an inexistent register (wrong address value) - * causes the command to be ignored and the WRONG_CMD flag to rise. - * For example, setting some parameters requires first to disable the power bridge; - * this can be done through the soft_hiz() method. - * They are the following: - * + L6474_EL_POS - * + L6474_T_FAST - * + L6474_TON_MIN : value in us - * + L6474_TOFF_MIN : value in us - * + L6474_ADC_OUT - * + L6474_STEP_MODE - * + L6474_CONFIG - * + L6474_STATUS - */ - virtual void set_parameter(unsigned int parameter, float value) - { - float register_value; - - switch ((L6474_Registers_t) parameter) { - case L6474_TVAL: - register_value = L6474_Tval_Current_to_Par(value); - break; - case L6474_TON_MIN: - case L6474_TOFF_MIN: - register_value = L6474_Tmin_Time_to_Par(value); - break; - default: - register_value = value; - break; - } - - L6474_CmdSetParam((L6474_Registers_t) parameter, (unsigned int) register_value); - } - - /** - * @brief Setting the current position to be the home position. - * @param None. - * @retval None. - */ - virtual void set_home(void) - { - L6474_SetHome(); - } - - /** - * @brief Setting the current position to be the marked position. - * @param None. - * @retval None. - */ - virtual void set_mark(void) - { - L6474_SetMark(); - } - - /** - * @brief Setting the maximum speed in pps. - * @param speed The maximum speed in pps. - * @retval "true" in case of success, "false" otherwise. - */ - virtual bool set_max_speed(unsigned int speed) - { - L6474_SetMaxSpeed((unsigned int) speed); - return true; - } - - /** - * @brief Setting the minimum speed in pps. - * @param speed The minimum speed in pps. - * @retval "true" in case of success, "false" otherwise. - */ - virtual bool set_min_speed(unsigned int speed) - { - L6474_SetMinSpeed((unsigned int) speed); - return true; - } - - /** - * @brief Setting the acceleration in pps^2. - * @param acceleration The acceleration in pps^2. - * @retval "true" in case of success, "false" otherwise. - */ - virtual bool set_acceleration(unsigned int acceleration) - { - L6474_SetAcceleration((unsigned int) acceleration); - return true; - } - - /** - * @brief Setting the deceleration in pps^2. - * @param deceleration The deceleration in pps^2. - * @retval "true" in case of success, "false" otherwise. - */ - virtual bool set_deceleration(unsigned int deceleration) - { - L6474_SetDeceleration((unsigned int) deceleration); - return true; - } - - /** - * @brief Going to a specified position. - * @param position The desired position. - * @retval None. - */ - virtual void go_to(signed int position) - { - L6474_GoTo((signed int) position); - } - - /** - * @brief Going to the home position. - * @param None. - * @retval None. - */ - virtual void go_home(void) - { - L6474_GoHome(); - } - - /** - * @brief Going to the marked position. - * @param None. - * @retval None. - */ - virtual void go_mark(void) - { - L6474_GoMark(); - } - - /** - * @brief Running the motor towards a specified direction. - * @param direction The direction of rotation. - * @retval None. - */ - virtual void run(direction_t direction) - { - L6474_Run((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); - } - - /** - * @brief Moving the motor towards a specified direction for a certain number of steps. - * @param direction The direction of rotation. - * @param steps The desired number of steps. - * @retval None. - */ - virtual void move(direction_t direction, unsigned int steps) - { - L6474_Move((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (unsigned int) steps); - } - - /** - * @brief Stopping the motor through an immediate deceleration up to zero speed. - * @param None. - * @retval None. - */ - virtual void soft_stop(void) - { - L6474_SoftStop(); - } - - /** - * @brief Stopping the motor through an immediate infinite deceleration. - * @param None. - * @retval None. - */ - virtual void hard_stop(void) - { - L6474_HardStop(); - } - - /** - * @brief Disabling the power bridge after performing a deceleration to zero. - * @param None. - * @retval None. - */ - virtual void soft_hiz(void) - { - L6474_SoftStop(); - L6474_CmdDisable(); - } - - /** - * @brief Disabling the power bridge immediately. - * @param None. - * @retval None. - */ - virtual void hard_hiz(void) - { - L6474_HardStop(); - L6474_CmdDisable(); - } - - /** - * @brief Waiting while the motor is active. - * @param None. - * @retval None. - */ - virtual void wait_while_active(void) - { - L6474_WaitWhileActive(); - } - - /** - * @brief Getting the device state. - * @param None. - * @retval The device state. - * @note The device state can be one of the following: - * + ACCELERATING - * + DECELERATING - * + STEADY - * + INACTIVE - */ - virtual motorState_t get_device_state(void) - { - return (motorState_t) L6474_GetDeviceState(); - } - - /** - * @brief Reading the Status Register. - * @param None. - * @retval None. - * @note The Status Register's flags are not cleared, contrary to the - * GetStatus() method. - */ - virtual uint16_t read_status_register(void) - { - return (uint16_t) L6474_ReadStatusRegister(); - } - - /** - * @brief Setting the Step Mode. - * @param step_mode The Step Mode. - * @retval "true" in case of success, "false" otherwise. - * @warning Setting the step mode implies first disabling the power bridge through - * the soft_hiz() method. - * @warning Every time step mode is changed, the values of the home - * and mark positions lose meaning and are reset. - */ - virtual bool set_step_mode(step_mode_t step_mode) - { - if ((motorStepMode_t) step_mode > STEP_MODE_1_16) { - return false; - } - - soft_hiz(); - L6474_SelectStepMode((motorStepMode_t) step_mode); - return true; - } - - /** - * @brief Attaching an error handler. - * @param fptr An error handler. - * @retval None. - */ - virtual void attach_error_handler(void (*fptr)(uint16_t error)) - { - L6474_AttachErrorHandler((void (*)(uint16_t error)) fptr); - } - - /** - * @brief Enabling the device. - * @param None. - * @retval None. - */ - virtual void enable(void) - { - L6474_CmdEnable(); - } - - /** - * @brief Disabling the device. - * @param None. - * @retval None. - */ - virtual void disable(void) - { - L6474_CmdDisable(); - } - - /** - * @brief Getting the version of the firmware. - * @param None. - * @retval The version of the firmware. - */ - virtual uint8_t get_fw_version(void) - { - return (uint8_t) L6474_GetFwVersion(); - } - - - /*** Public Interrupt Related Methods ***/ - - /* ACTION 6 --------------------------------------------------------------* - * Implement here interrupt related methods, if any. * - * Note that interrupt handling is platform dependent, e.g.: * - * + mbed: * - * InterruptIn feature_irq(pin); //Interrupt object. * - * feature_irq.rise(callback); //Attach a callback. * - * feature_irq.mode(PullNone); //Set interrupt mode. * - * feature_irq.enable_irq(); //Enable interrupt. * - * feature_irq.disable_irq(); //Disable interrupt. * - * + Arduino: * - * attachInterrupt(pin, callback, RISING); //Attach a callback. * - * detachInterrupt(pin); //Detach a callback. * - * * - * Example (mbed): * - * void attach_feature_irq(void (*fptr) (void)) * - * { * - * feature_irq.rise(fptr); * - * } * - * * - * void enable_feature_irq(void) * - * { * - * feature_irq.enable_irq(); * - * } * - * * - * void disable_feature_irq(void) * - * { * - * feature_irq.disable_irq(); * - * } * - *------------------------------------------------------------------------*/ - /** - * @brief Attaching an interrupt handler to the FLAG interrupt. - * @param fptr An interrupt handler. - * @retval None. - */ - void attach_flag_irq(void (*fptr)(void)) - { - flag_irq.fall(fptr); - } - - /** - * @brief Enabling the FLAG interrupt handling. - * @param None. - * @retval None. - */ - void enable_flag_irq(void) - { - flag_irq.enable_irq(); - } - - /** - * @brief Disabling the FLAG interrupt handling. - * @param None. - * @retval None. - */ - void disable_flag_irq(void) - { - flag_irq.disable_irq(); - } - - -protected: - - /*** Protected Component Related Methods ***/ - - /* ACTION 7 --------------------------------------------------------------* - * Declare here the component's specific methods. * - * They should be: * - * + Methods with the same name of the C component's virtual table's * - * functions (1); * - * + Methods with the same name of the C component's extended virtual * - * table's functions, if any (2); * - * + Helper methods, if any, like functions declared in the component's * - * source files but not pointed by the component's virtual table (3). * - * * - * Example: * - * status_t COMPONENT_get_value(float *f); //(1) * - * status_t COMPONENT_enable_feature(void); //(2) * - * status_t COMPONENT_compute_average(void); //(3) * - *------------------------------------------------------------------------*/ - void L6474_AttachErrorHandler(void (*callback)(uint16_t error)); - status_t L6474_Init(void *init); - status_t L6474_ReadID(uint8_t *id); - uint16_t L6474_GetAcceleration(void); - uint16_t L6474_GetCurrentSpeed(void); - uint16_t L6474_GetDeceleration(void); - motorState_t L6474_GetDeviceState(void); - uint8_t L6474_GetFwVersion(void); - int32_t L6474_GetMark(void); - uint16_t L6474_GetMaxSpeed(void); - uint16_t L6474_GetMinSpeed(void); - int32_t L6474_GetPosition(void); - void L6474_GoHome(void); - void L6474_GoMark(void); - void L6474_GoTo(int32_t targetPosition); - void L6474_HardStop(void); - void L6474_Move(motorDir_t direction, uint32_t stepCount); - void L6474_Run(motorDir_t direction); - bool L6474_SetAcceleration(uint16_t newAcc); - bool L6474_SetDeceleration(uint16_t newDec); - void L6474_SetHome(void); - void L6474_SetMark(void); - bool L6474_SetMaxSpeed(uint16_t newMaxSpeed); - bool L6474_SetMinSpeed(uint16_t newMinSpeed); - bool L6474_SoftStop(void); - void L6474_WaitWhileActive(void); - void L6474_CmdDisable(void); - void L6474_CmdEnable(void); - uint32_t L6474_CmdGetParam(L6474_Registers_t parameter); - uint16_t L6474_CmdGetStatus(void); - void L6474_CmdNop(void); - void L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value); - uint16_t L6474_ReadStatusRegister(void); - void L6474_SelectStepMode(motorStepMode_t stepMod); - motorDir_t L6474_GetDirection(void); - void L6474_SetDirection(motorDir_t direction); - void L6474_ApplySpeed(uint16_t newSpeed); - void L6474_ComputeSpeedProfile(uint32_t nbSteps); - int32_t L6474_ConvertPosition(uint32_t abs_position_reg); - void L6474_ErrorHandler(uint16_t error); - void L6474_SendCommand(uint8_t param); - void L6474_SetRegisterToPredefinedValues(void); - void L6474_SetRegisterToInitializationValues(L6474_init_t *init); - void L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte); - void L6474_SetDeviceParamsToPredefinedValues(void); - void L6474_StartMovement(void); - void L6474_StepClockHandler(void); - float L6474_Tval_Current_to_Par(float current_mA); - float L6474_Par_to_Tval_Current(float Tval); - float L6474_Tmin_Time_to_Par(float ton_min_us); - float L6474_Par_to_Tmin_Time(float Tmin); - - - /*** Component's I/O Methods ***/ - - /** - * @brief Utility function to read data from L6474. - * @param[out] pBuffer pointer to the buffer to read data into. - * @param[in] NumBytesToRead number of bytes to read. - * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. - */ - status_t Read(uint8_t* pBuffer, uint16_t NumBytesToRead) - { - if (dev_spi.spi_read(pBuffer, ssel, NumBytesToRead) != 0) { - return COMPONENT_ERROR; - } - return COMPONENT_OK; - } - - /** - * @brief Utility function to write data to L6474. - * @param[in] pBuffer pointer to the buffer of data to send. - * @param[in] NumBytesToWrite number of bytes to write. - * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. - */ - status_t Write(uint8_t* pBuffer, uint16_t NumBytesToWrite) - { - if (dev_spi.spi_write(pBuffer, ssel, NumBytesToWrite) != 0) { - return COMPONENT_ERROR; - } - return COMPONENT_OK; - } - - /** - * @brief Utility function to read and write data from/to L6474 at the same time. - * @param[out] pBufferToRead pointer to the buffer to read data into. - * @param[in] pBufferToWrite pointer to the buffer of data to send. - * @param[in] NumBytes number of bytes to read and write. - * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. - */ - status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumBytes) - { - if (dev_spi.spi_read_write(pBufferToRead, pBufferToWrite, ssel, NumBytes) != 0) { - return COMPONENT_ERROR; - } - return COMPONENT_OK; - } - - /* ACTION 8 --------------------------------------------------------------* - * Implement here other I/O methods beyond those already implemented * - * above, which are declared extern within the component's header file. * - *------------------------------------------------------------------------*/ - /** - * @brief Making the CPU wait. - * @param None. - * @retval None. - */ - void L6474_Delay(uint32_t delay) - { - wait_ms(delay); - } - - /** - * @brief Enabling interrupts. - * @param None. - * @retval None. - */ - void L6474_EnableIrq(void) - { - __enable_irq(); - } - - /** - * @brief Disabling interrupts. - * @param None. - * @retval None. - */ - void L6474_DisableIrq(void) - { - __disable_irq(); - } - - /** - * @brief Initialising the PWM. - * @param None. - * @retval None. - */ - void L6474_PwmInit(void) {} - - /** - * @brief Setting the frequency of PWM. - * The frequency controls directly the speed of the device. - * @param frequency the frequency of PWM. - * @retval None. - */ - void L6474_PwmSetFreq(uint16_t frequency) - { - /* Computing the period of PWM. */ - double period = 1.0f / frequency; - - /* Setting the period and the duty-cycle of PWM. */ - pwm.period(period); - pwm.write(0.5f); - - /* Setting a callback with the same period of PWM's, to update the state machine. */ - ticker.attach(Callback<void()>(this, &L6474::L6474_StepClockHandler), period); - } - - /** - * @brief Stopping the PWM. - * @param None. - * @retval None. - */ - void L6474_PwmStop(void) - { - pwm.write(0.0f); - ticker.detach(); - } - - /** - * @brief Putting the device in standby mode. - * @param None. - * @retval None. - */ - void L6474_ReleaseReset(void) - { - standby_reset = 1; - } - - /** - * @brief Putting the device in reset mode. - * @param None. - * @retval None. - */ - void L6474_Reset(void) - { - standby_reset = 0; - } - - /** - * @brief Setting the direction of rotation. - * @param gpioState direction of rotation: "1" for forward, "0" for backward. - * @retval None. - */ - void L6474_SetDirectionGpio(uint8_t gpioState) - { - direction = gpioState; - } - - /** - * @brief Writing and reading bytes to/from the component through the SPI at the same time. - * @param[in] pByteToTransmit pointer to the buffer of data to send. - * @param[out] pReceivedByte pointer to the buffer to read data into. - * @retval "0" in case of success, "1" otherwise. - */ - uint8_t L6474_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) - { - return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, number_of_devices) == COMPONENT_OK ? 0 : 1); - } - - - /*** Component's Instance Variables ***/ - - /* ACTION 9 --------------------------------------------------------------* - * Declare here interrupt related variables, if needed. * - * Note that interrupt handling is platform dependent, see * - * "Interrupt Related Methods" above. * - * * - * Example: * - * + mbed: * - * InterruptIn feature_irq; * - *------------------------------------------------------------------------*/ - /* Flag Interrupt. */ - InterruptIn flag_irq; - - /* ACTION 10 -------------------------------------------------------------* - * Declare here other pin related variables, if needed. * - * * - * Example: * - * + mbed: * - * DigitalOut standby_reset; * - *------------------------------------------------------------------------*/ - /* Standby/reset pin. */ - DigitalOut standby_reset; - - /* Direction of rotation pin. */ - DigitalOut direction; - - /* Pulse Width Modulation pin. */ - PwmOut pwm; - - /* Timer to trigger the PWM callback at each PWM pulse. */ - Ticker ticker; - - /* ACTION 11 -------------------------------------------------------------* - * Declare here communication related variables, if needed. * - * * - * Example: * - * + mbed: * - * DigitalOut ssel; * - * DevSPI &dev_spi; * - *------------------------------------------------------------------------*/ - /* Configuration. */ - DigitalOut ssel; - - /* IO Device. */ - DevSPI &dev_spi; - - /* ACTION 12 -------------------------------------------------------------* - * Declare here identity related variables, if needed. * - * Note that there should be only a unique identifier for each component, * - * which should be the "who_am_i" parameter. * - *------------------------------------------------------------------------*/ - /* Identity */ - uint8_t who_am_i; - - /* ACTION 13 -------------------------------------------------------------* - * Declare here the component's static and non-static data, one variable * - * per line. * - * * - * Example: * - * float measure; * - * int instance_id; * - * static int number_of_instances; * - *------------------------------------------------------------------------*/ - /* Data. */ - void (*error_handler_callback)(uint16_t error); - deviceParams_t device_prm; - uint8_t device_instance; - - /* Static data. */ - static uint8_t number_of_devices; - static uint8_t spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; - static uint8_t spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; - - -public: - - /* Static data. */ - static bool spi_preemtion_by_isr; - static bool isr_flag; -}; - -#endif // __L6474_CLASS_H - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/l6474/L6474_config.h --- a/Components/l6474/L6474_config.h Fri Mar 10 14:07:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,229 +0,0 @@ -/** - ****************************************************************************** - * @file L6474_target_config.h - * @author IPC Rennes - * @version V1.5.0 - * @date November 12, 2014 - * @brief Predefines values for the L6474 registers - * and for the devices parameters - * @note (C) COPYRIGHT 2014 STMicroelectronics - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - - -/* Define to prevent recursive inclusion -------------------------------------*/ - -#ifndef __L6474_TARGET_CONFIG_H -#define __L6474_TARGET_CONFIG_H - -#ifdef __cplusplus - extern "C" { -#endif - - -/* Definitions ---------------------------------------------------------------*/ - -/** @addtogroup BSP - * @{ - */ - -/** @addtogroup L6474 - * @{ - */ - -/** @addtogroup L6474_Exported_Constants - * @{ - */ - -/** @defgroup Predefined_L6474_Registers_Values - * @{ - */ - -/// The maximum number of devices in the daisy chain -#define MAX_NUMBER_OF_DEVICES (3) - -/************************ Speed Profile *******************************/ - -/// Acceleration rate in step/s2 for device 0 (must be greater than 0) -#define L6474_CONF_PARAM_ACC_DEVICE_0 (160) -/// Acceleration rate in step/s2 for device 1 (must be greater than 0) -#define L6474_CONF_PARAM_ACC_DEVICE_1 (160) -/// Acceleration rate in step/s2 for device 2 (must be greater than 0) -#define L6474_CONF_PARAM_ACC_DEVICE_2 (160) - -/// Deceleration rate in step/s2 for device 0 (must be greater than 0) -#define L6474_CONF_PARAM_DEC_DEVICE_0 (160) -/// Deceleration rate in step/s2 for device 1 (must be greater than 0) -#define L6474_CONF_PARAM_DEC_DEVICE_1 (160) -/// Deceleration rate in step/s2 for device 2 (must be greater than 0) -#define L6474_CONF_PARAM_DEC_DEVICE_2 (160) - -/// Maximum speed in step/s for device 0 (30 step/s < Maximum speed <= 10 000 step/s ) -#define L6474_CONF_PARAM_MAX_SPEED_DEVICE_0 (1600) -/// Maximum speed in step/s for device 1 (30 step/s < Maximum speed <= 10 000 step/s ) -#define L6474_CONF_PARAM_MAX_SPEED_DEVICE_1 (1600) -/// Maximum speed in step/s for device 2 (30 step/s < Maximum speed <= 10 000 step/s ) -#define L6474_CONF_PARAM_MAX_SPEED_DEVICE_2 (1600) - -/// Minimum speed in step/s for device 0 (30 step/s <= Minimum speed < 10 000 step/s) -#define L6474_CONF_PARAM_MIN_SPEED_DEVICE_0 (800) -/// Minimum speed in step/s for device 1 (30 step/s <= Minimum speed < 10 000 step/s) -#define L6474_CONF_PARAM_MIN_SPEED_DEVICE_1 (800) -/// Minimum speed in step/s for device 2 (30 step/s <= Minimum speed < 10 000 step/s) -#define L6474_CONF_PARAM_MIN_SPEED_DEVICE_2 (800) - - -/************************ Phase Current Control *******************************/ - -// Current value that is assigned to the torque regulation DAC -/// TVAL register value for device 0 (range 31.25mA to 4000mA) -#define L6474_CONF_PARAM_TVAL_DEVICE_0 (250) -/// TVAL register value for device 1 (range 31.25mA to 4000mA) -#define L6474_CONF_PARAM_TVAL_DEVICE_1 (250) -/// TVAL register value for device 2 (range 31.25mA to 4000mA) -#define L6474_CONF_PARAM_TVAL_DEVICE_2 (250) - -/// Fall time value (T_FAST field of T_FAST register) for device 0 (range 2us to 32us) -#define L6474_CONF_PARAM_FAST_STEP_DEVICE_0 (L6474_FAST_STEP_12us) -/// Fall time value (T_FAST field of T_FAST register) for device 1 (range 2us to 32us) -#define L6474_CONF_PARAM_FAST_STEP_DEVICE_1 (L6474_FAST_STEP_12us) -/// Fall time value (T_FAST field of T_FAST register) for device 2 (range 2us to 32us) -#define L6474_CONF_PARAM_FAST_STEP_DEVICE_2 (L6474_FAST_STEP_12us) - -/// Maximum fast decay time (T_OFF field of T_FAST register) for device 0 (range 2us to 32us) -#define L6474_CONF_PARAM_TOFF_FAST_DEVICE_0 (L6474_TOFF_FAST_8us) -/// Maximum fast decay time (T_OFF field of T_FAST register) for device 1 (range 2us to 32us) -#define L6474_CONF_PARAM_TOFF_FAST_DEVICE_1 (L6474_TOFF_FAST_8us) -/// Maximum fast decay time (T_OFF field of T_FAST register) for device 2 (range 2us to 32us) -#define L6474_CONF_PARAM_TOFF_FAST_DEVICE_2 (L6474_TOFF_FAST_8us) - -/// Minimum ON time (TON_MIN register) for device 0 (range 0.5us to 64us) -#define L6474_CONF_PARAM_TON_MIN_DEVICE_0 (3) -/// Minimum ON time (TON_MIN register) for device 1 (range 0.5us to 64us) -#define L6474_CONF_PARAM_TON_MIN_DEVICE_1 (3) -/// Minimum ON time (TON_MIN register) for device 2 (range 0.5us to 64us) -#define L6474_CONF_PARAM_TON_MIN_DEVICE_2 (3) - -/// Minimum OFF time (TOFF_MIN register) for device 0 (range 0.5us to 64us) -#define L6474_CONF_PARAM_TOFF_MIN_DEVICE_0 (21) -/// Minimum OFF time (TOFF_MIN register) for device 1 (range 0.5us to 64us) -#define L6474_CONF_PARAM_TOFF_MIN_DEVICE_1 (21) -/// Minimum OFF time (TOFF_MIN register) for device 2 (range 0.5us to 64us) -#define L6474_CONF_PARAM_TOFF_MIN_DEVICE_2 (21) - -/******************************* Others ***************************************/ - -/// Overcurrent threshold settings for device 0 (OCD_TH register) -#define L6474_CONF_PARAM_OCD_TH_DEVICE_0 (L6474_OCD_TH_750mA) -/// Overcurrent threshold settings for device 1 (OCD_TH register) -#define L6474_CONF_PARAM_OCD_TH_DEVICE_1 (L6474_OCD_TH_750mA) -/// Overcurrent threshold settings for device 2 (OCD_TH register) -#define L6474_CONF_PARAM_OCD_TH_DEVICE_2 (L6474_OCD_TH_750mA) - -/// Alarm settings for device 0 (ALARM_EN register) -#define L6474_CONF_PARAM_ALARM_EN_DEVICE_0 (L6474_ALARM_EN_OVERCURRENT |\ - L6474_ALARM_EN_THERMAL_SHUTDOWN |\ - L6474_ALARM_EN_THERMAL_WARNING |\ - L6474_ALARM_EN_UNDERVOLTAGE |\ - L6474_ALARM_EN_SW_TURN_ON |\ - L6474_ALARM_EN_WRONG_NPERF_CMD) - -///Alarm settings for device 1 (ALARM_EN register) -#define L6474_CONF_PARAM_ALARM_EN_DEVICE_1 (L6474_ALARM_EN_OVERCURRENT |\ - L6474_ALARM_EN_THERMAL_SHUTDOWN |\ - L6474_ALARM_EN_THERMAL_WARNING |\ - L6474_ALARM_EN_UNDERVOLTAGE |\ - L6474_ALARM_EN_SW_TURN_ON |\ - L6474_ALARM_EN_WRONG_NPERF_CMD) - -/// Alarm settings for device 2 (ALARM_EN register) -#define L6474_CONF_PARAM_ALARM_EN_DEVICE_2 (L6474_ALARM_EN_OVERCURRENT |\ - L6474_ALARM_EN_THERMAL_SHUTDOWN |\ - L6474_ALARM_EN_THERMAL_WARNING |\ - L6474_ALARM_EN_UNDERVOLTAGE |\ - L6474_ALARM_EN_SW_TURN_ON |\ - L6474_ALARM_EN_WRONG_NPERF_CMD) - -/// Step selection settings for device 0 (STEP_SEL field of STEP_MODE register) -#define L6474_CONF_PARAM_STEP_SEL_DEVICE_0 (L6474_STEP_SEL_1_16) -/// Step selection settings for device 1 (STEP_SEL field of STEP_MODE register) -#define L6474_CONF_PARAM_STEP_SEL_DEVICE_1 (L6474_STEP_SEL_1_16) -/// Step selection settings for device 2 (STEP_SEL field of STEP_MODE register) -#define L6474_CONF_PARAM_STEP_SEL_DEVICE_2 (L6474_STEP_SEL_1_16) - -/// Synch. selection settings for device 0 (SYNC_SEL field of STEP_MODE register) -#define L6474_CONF_PARAM_SYNC_SEL_DEVICE_0 (L6474_SYNC_SEL_1_2) -/// Synch. selection settings for device 1 (SYNC_SEL field of STEP_MODE register) -#define L6474_CONF_PARAM_SYNC_SEL_DEVICE_1 (L6474_SYNC_SEL_1_2) -/// Synch. selection settings for device 2 (SYNC_SEL field of STEP_MODE register) -#define L6474_CONF_PARAM_SYNC_SEL_DEVICE_2 (L6474_SYNC_SEL_1_2) - -/// Target Swicthing Period for device 0 (field TOFF of CONFIG register) -#define L6474_CONF_PARAM_TOFF_DEVICE_0 (L6474_CONFIG_TOFF_044us) -/// Target Swicthing Period for device 1 (field TOFF of CONFIG register) -#define L6474_CONF_PARAM_TOFF_DEVICE_1 (L6474_CONFIG_TOFF_044us) -/// Target Swicthing Period for device 2 (field TOFF of CONFIG register) -#define L6474_CONF_PARAM_TOFF_DEVICE_2 (L6474_CONFIG_TOFF_044us) - -/// Slew rate for device 0 (POW_SR field of CONFIG register) -#define L6474_CONF_PARAM_SR_DEVICE_0 (L6474_CONFIG_SR_320V_us) -/// Slew rate for device 1 (POW_SR field of CONFIG register) -#define L6474_CONF_PARAM_SR_DEVICE_1 (L6474_CONFIG_SR_320V_us) -/// Slew rate for device 2 (POW_SR field of CONFIG register) -#define L6474_CONF_PARAM_SR_DEVICE_2 (L6474_CONFIG_SR_320V_us) - -/// Over current shutwdown enabling for device 0 (OC_SD field of CONFIG register) -#define L6474_CONF_PARAM_OC_SD_DEVICE_0 (L6474_CONFIG_OC_SD_ENABLE) -/// Over current shutwdown enabling for device 1 (OC_SD field of CONFIG register) -#define L6474_CONF_PARAM_OC_SD_DEVICE_1 (L6474_CONFIG_OC_SD_ENABLE) -/// Over current shutwdown enabling for device 2 (OC_SD field of CONFIG register) -#define L6474_CONF_PARAM_OC_SD_DEVICE_2 (L6474_CONFIG_OC_SD_ENABLE) - -/// Torque regulation method for device 0 (EN_TQREG field of CONFIG register) -#define L6474_CONF_PARAM_TQ_REG_DEVICE_0 (L6474_CONFIG_EN_TQREG_TVAL_USED) -///Torque regulation method for device 1 (EN_TQREG field of CONFIG register) -#define L6474_CONF_PARAM_TQ_REG_DEVICE_1 (L6474_CONFIG_EN_TQREG_TVAL_USED) -/// Torque regulation method for device 2 (EN_TQREG field of CONFIG register) -#define L6474_CONF_PARAM_TQ_REG_DEVICE_2 (L6474_CONFIG_EN_TQREG_TVAL_USED) - -/// Clock setting for device 0 (OSC_CLK_SEL field of CONFIG register) -#define L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_0 (L6474_CONFIG_INT_16MHZ) -/// Clock setting for device 1 (OSC_CLK_SEL field of CONFIG register) -#define L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_1 (L6474_CONFIG_INT_16MHZ) -/// Clock setting for device 2 (OSC_CLK_SEL field of CONFIG register) -#define L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_2 (L6474_CONFIG_INT_16MHZ) - -#ifdef __cplusplus - } -#endif - -#endif /* __L6474_TARGET_CONFIG_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 27da297c361c -r 974ca699c792 Components/l6474/L6474_def.h --- a/Components/l6474/L6474_def.h Fri Mar 10 14:07:49 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,600 +0,0 @@ -/** - ****************************************************************************** - * @file L6474_def.h - * @author IPC Rennes - * @version V1.5.0 - * @date November 12, 2014 - * @brief Header for L6474 driver (fully integrated microstepping motor driver) - * @note (C) COPYRIGHT 2014 STMicroelectronics - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ - - -/* Define to prevent recursive inclusion -------------------------------------*/ - -#ifndef __L6474_H -#define __L6474_H - -#ifdef __cplusplus - extern "C" { -#endif - - -/* Includes ------------------------------------------------------------------*/ - -#include "L6474_config.h" -#include "../Common/motor_def.h" - - -/* Definitions ---------------------------------------------------------------*/ - -/** @addtogroup BSP - * @{ - */ - -/** @addtogroup Components - * @{ - */ - -/** @addtogroup L6474 - * @{ - */ - -/** @defgroup L6474_Exported_Defines L6474_Exported_Defines - * @{ - */ - -/// Current FW version -#define L6474_FW_VERSION (5) - -/// L6474 max number of bytes of command & arguments to set a parameter -#define L6474_CMD_ARG_MAX_NB_BYTES (4) - -/// L6474 command + argument bytes number for GET_STATUS command -#define L6474_CMD_ARG_NB_BYTES_GET_STATUS (1) - -/// L6474 response bytes number -#define L6474_RSP_NB_BYTES_GET_STATUS (2) - -/// L6474 value mask for ABS_POS register -#define L6474_ABS_POS_VALUE_MASK ((uint32_t) 0x003FFFFF) - -/// L6474 sign bit mask for ABS_POS register -#define L6474_ABS_POS_SIGN_BIT_MASK ((uint32_t) 0x00200000) - - -/* Types ---------------------------------------------------------------------*/ - -/** @defgroup L6474_Exported_Types - * @{ - */ - -/** @defgroup L6474_Fast_Decay_Time_Options - * @{ - */ -///TOFF_FAST values for T_FAST register -typedef enum { - L6474_TOFF_FAST_2us = ((uint8_t) 0x00 << 4), - L6474_TOFF_FAST_4us = ((uint8_t) 0x01 << 4), - L6474_TOFF_FAST_6us = ((uint8_t) 0x02 << 4), - L6474_TOFF_FAST_8us = ((uint8_t) 0x03 << 4), - L6474_TOFF_FAST_10us = ((uint8_t) 0x04 << 4), - L6474_TOFF_FAST_12us = ((uint8_t) 0x05 << 4), - L6474_TOFF_FAST_14us = ((uint8_t) 0x06 << 4), - L6474_TOFF_FAST_16us = ((uint8_t) 0x07 << 4), - L6474_TOFF_FAST_18us = ((uint8_t) 0x08 << 4), - L6474_TOFF_FAST_20us = ((uint8_t) 0x09 << 4), - L6474_TOFF_FAST_22us = ((uint8_t) 0x0A << 4), - L6474_TOFF_FAST_24us = ((uint8_t) 0x0B << 4), - L6474_TOFF_FAST_26us = ((uint8_t) 0x0C << 4), - L6474_TOFF_FAST_28us = ((uint8_t) 0x0D << 4), - L6474_TOFF_FAST_30us = ((uint8_t) 0x0E << 4), - L6474_TOFF_FAST_32us = ((uint8_t) 0x0F << 4) -} L6474_TOFF_FAST_t; -/** - * @} - */ - -/** @defgroup L6474_Fall_Step_Time_Options - * @{ - */ -///FAST_STEP values for T_FAST register -typedef enum { - L6474_FAST_STEP_2us = ((uint8_t) 0x00), - L6474_FAST_STEP_4us = ((uint8_t) 0x01), - L6474_FAST_STEP_6us = ((uint8_t) 0x02), - L6474_FAST_STEP_8us = ((uint8_t) 0x03), - L6474_FAST_STEP_10us = ((uint8_t) 0x04), - L6474_FAST_STEP_12us = ((uint8_t) 0x05), - L6474_FAST_STEP_14us = ((uint8_t) 0x06), - L6474_FAST_STEP_16us = ((uint8_t) 0x07), - L6474_FAST_STEP_18us = ((uint8_t) 0x08), - L6474_FAST_STEP_20us = ((uint8_t) 0x09), - L6474_FAST_STEP_22us = ((uint8_t) 0x0A), - L6474_FAST_STEP_24us = ((uint8_t) 0x0B), - L6474_FAST_STEP_26us = ((uint8_t) 0x0C), - L6474_FAST_STEP_28us = ((uint8_t) 0x0D), - L6474_FAST_STEP_30us = ((uint8_t) 0x0E), - L6474_FAST_STEP_32us = ((uint8_t) 0x0F) -} L6474_FAST_STEP_t; -/** - * @} - */ - -/** @defgroup L6474_Overcurrent_Threshold_options - * @{ - */ -///OCD_TH register -typedef enum { - L6474_OCD_TH_375mA = ((uint8_t) 0x00), - L6474_OCD_TH_750mA = ((uint8_t) 0x01), - L6474_OCD_TH_1125mA = ((uint8_t) 0x02), - L6474_OCD_TH_1500mA = ((uint8_t) 0x03), - L6474_OCD_TH_1875mA = ((uint8_t) 0x04), - L6474_OCD_TH_2250mA = ((uint8_t) 0x05), - L6474_OCD_TH_2625mA = ((uint8_t) 0x06), - L6474_OCD_TH_3000mA = ((uint8_t) 0x07), - L6474_OCD_TH_3375mA = ((uint8_t) 0x08), - L6474_OCD_TH_3750mA = ((uint8_t) 0x09), - L6474_OCD_TH_4125mA = ((uint8_t) 0x0A), - L6474_OCD_TH_4500mA = ((uint8_t) 0x0B), - L6474_OCD_TH_4875mA = ((uint8_t) 0x0C), - L6474_OCD_TH_5250mA = ((uint8_t) 0x0D), - L6474_OCD_TH_5625mA = ((uint8_t) 0x0E), - L6474_OCD_TH_6000mA = ((uint8_t) 0x0F) -} L6474_OCD_TH_t; -/** - * @} - */ - -/** @defgroup L6474_STEP_MODE_Register_Masks - * @{ - */ -///STEP_MODE register -typedef enum { - L6474_STEP_MODE_STEP_SEL = ((uint8_t) 0x07), - L6474_STEP_MODE_SYNC_SEL = ((uint8_t) 0x70) -} L6474_STEP_MODE_Masks_t; -/** - * @} - */ - -/** @defgroup L6474_STEP_SEL_Options_For_STEP_MODE_Register - * @{ - */ -///STEP_SEL field of STEP_MODE register -typedef enum { - L6474_STEP_SEL_1 = ((uint8_t) 0x08), //full step - L6474_STEP_SEL_1_2 = ((uint8_t) 0x09), //half step - L6474_STEP_SEL_1_4 = ((uint8_t) 0x0A), //1/4 microstep - L6474_STEP_SEL_1_8 = ((uint8_t) 0x0B), //1/8 microstep - L6474_STEP_SEL_1_16 = ((uint8_t) 0x0C) //1/16 microstep -} L6474_STEP_SEL_t; -/** - * @} - */ - -/** @defgroup L6474_SYNC_SEL_Options_For_STEP_MODE_Register - * @{ - */ -///SYNC_SEL field of STEP_MODE register -typedef enum { - L6474_SYNC_SEL_1_2 = ((uint8_t) 0x80), - L6474_SYNC_SEL_1 = ((uint8_t) 0x90), - L6474_SYNC_SEL_2 = ((uint8_t) 0xA0), - L6474_SYNC_SEL_4 = ((uint8_t) 0xB0), - L6474_SYNC_SEL_8 = ((uint8_t) 0xC0), - L6474_SYNC_SEL_UNUSED = ((uint8_t) 0xD0) -} L6474_SYNC_SEL_t; -/** - * @} - */ - -/** @defgroup L6474_ALARM_EN_Register_Options - * @{ - */ -///ALARM_EN register -typedef enum { - L6474_ALARM_EN_OVERCURRENT = ((uint8_t) 0x01), - L6474_ALARM_EN_THERMAL_SHUTDOWN = ((uint8_t) 0x02), - L6474_ALARM_EN_THERMAL_WARNING = ((uint8_t) 0x04), - L6474_ALARM_EN_UNDERVOLTAGE = ((uint8_t) 0x08), - L6474_ALARM_EN_SW_TURN_ON = ((uint8_t) 0x40), - L6474_ALARM_EN_WRONG_NPERF_CMD = ((uint8_t) 0x80) -} L6474_ALARM_EN_t; -/** - * @} - */ - -/** @defgroup L6474_CONFIG_Register_Masks - * @{ - */ -///CONFIG register -typedef enum { - L6474_CONFIG_OSC_SEL = ((uint16_t) 0x0007), - L6474_CONFIG_EXT_CLK = ((uint16_t) 0x0008), - L6474_CONFIG_EN_TQREG = ((uint16_t) 0x0020), - L6474_CONFIG_OC_SD = ((uint16_t) 0x0080), - L6474_CONFIG_POW_SR = ((uint16_t) 0x0300), - L6474_CONFIG_TOFF = ((uint16_t) 0x7C00) -} L6474_CONFIG_Masks_t; -/** - * @} - */ - -/** @defgroup L6474_Clock_Source_Options_For_CONFIG_Register - * @{ - */ -///Clock source option for CONFIG register -typedef enum { - L6474_CONFIG_INT_16MHZ = ((uint16_t) 0x0000), - L6474_CONFIG_INT_16MHZ_OSCOUT_2MHZ = ((uint16_t) 0x0008), - L6474_CONFIG_INT_16MHZ_OSCOUT_4MHZ = ((uint16_t) 0x0009), - L6474_CONFIG_INT_16MHZ_OSCOUT_8MHZ = ((uint16_t) 0x000A), - L6474_CONFIG_INT_16MHZ_OSCOUT_16MHZ = ((uint16_t) 0x000B), - L6474_CONFIG_EXT_8MHZ_XTAL_DRIVE = ((uint16_t) 0x0004), - L6474_CONFIG_EXT_16MHZ_XTAL_DRIVE = ((uint16_t) 0x0005), - L6474_CONFIG_EXT_24MHZ_XTAL_DRIVE = ((uint16_t) 0x0006), - L6474_CONFIG_EXT_32MHZ_XTAL_DRIVE = ((uint16_t) 0x0007), - L6474_CONFIG_EXT_8MHZ_OSCOUT_INVERT = ((uint16_t) 0x000C), - L6474_CONFIG_EXT_16MHZ_OSCOUT_INVERT = ((uint16_t) 0x000D), - L6474_CONFIG_EXT_24MHZ_OSCOUT_INVERT = ((uint16_t) 0x000E), - L6474_CONFIG_EXT_32MHZ_OSCOUT_INVERT = ((uint16_t) 0x000F) -} L6474_CONFIG_OSC_MGMT_t; -/** - * @} - */ - -/** @defgroup L6474_External_Torque_Regulation_Options_For_CONFIG_Register - * @{ - */ -///External Torque regulation options for CONFIG register -typedef enum { - L6474_CONFIG_EN_TQREG_TVAL_USED = ((uint16_t) 0x0000), - L6474_CONFIG_EN_TQREG_ADC_OUT = ((uint16_t) 0x0020) -} L6474_CONFIG_EN_TQREG_t; -/** - * @} - */ - -/** @defgroup L6474_Over_Current_Shutdown_Options_For_CONFIG_Register - * @{ - */ -///Over Current Shutdown options for CONFIG register -typedef enum { - L6474_CONFIG_OC_SD_DISABLE = ((uint16_t) 0x0000), - L6474_CONFIG_OC_SD_ENABLE = ((uint16_t) 0x0080) -} L6474_CONFIG_OC_SD_t; -/** - * @} - */ - -/** @defgroup L6474_Power_Bridge_Output_Slew_Rate_Options - * @{ - */ -/// POW_SR values for CONFIG register -typedef enum { - L6474_CONFIG_SR_320V_us =((uint16_t)0x0000), - L6474_CONFIG_SR_075V_us =((uint16_t)0x0100), - L6474_CONFIG_SR_110V_us =((uint16_t)0x0200), - L6474_CONFIG_SR_260V_us =((uint16_t)0x0300) -} L6474_CONFIG_POW_SR_t; -/** - * @} - */ - -/** @defgroup L6474_Off_Time_Options - * @{ - */ -/// TOFF values for CONFIG register -typedef enum { - L6474_CONFIG_TOFF_004us = (((uint16_t) 0x01) << 10), - L6474_CONFIG_TOFF_008us = (((uint16_t) 0x02) << 10), - L6474_CONFIG_TOFF_012us = (((uint16_t) 0x03) << 10), - L6474_CONFIG_TOFF_016us = (((uint16_t) 0x04) << 10), - L6474_CONFIG_TOFF_020us = (((uint16_t) 0x05) << 10), - L6474_CONFIG_TOFF_024us = (((uint16_t) 0x06) << 10), - L6474_CONFIG_TOFF_028us = (((uint16_t) 0x07) << 10), - L6474_CONFIG_TOFF_032us = (((uint16_t) 0x08) << 10), - L6474_CONFIG_TOFF_036us = (((uint16_t) 0x09) << 10), - L6474_CONFIG_TOFF_040us = (((uint16_t) 0x0A) << 10), - L6474_CONFIG_TOFF_044us = (((uint16_t) 0x0B) << 10), - L6474_CONFIG_TOFF_048us = (((uint16_t) 0x0C) << 10), - L6474_CONFIG_TOFF_052us = (((uint16_t) 0x0D) << 10), - L6474_CONFIG_TOFF_056us = (((uint16_t) 0x0E) << 10), - L6474_CONFIG_TOFF_060us = (((uint16_t) 0x0F) << 10), - L6474_CONFIG_TOFF_064us = (((uint16_t) 0x10) << 10), - L6474_CONFIG_TOFF_068us = (((uint16_t) 0x11) << 10), - L6474_CONFIG_TOFF_072us = (((uint16_t) 0x12) << 10), - L6474_CONFIG_TOFF_076us = (((uint16_t) 0x13) << 10), - L6474_CONFIG_TOFF_080us = (((uint16_t) 0x14) << 10), - L6474_CONFIG_TOFF_084us = (((uint16_t) 0x15) << 10), - L6474_CONFIG_TOFF_088us = (((uint16_t) 0x16) << 10), - L6474_CONFIG_TOFF_092us = (((uint16_t) 0x17) << 10), - L6474_CONFIG_TOFF_096us = (((uint16_t) 0x18) << 10), - L6474_CONFIG_TOFF_100us = (((uint16_t) 0x19) << 10), - L6474_CONFIG_TOFF_104us = (((uint16_t) 0x1A) << 10), - L6474_CONFIG_TOFF_108us = (((uint16_t) 0x1B) << 10), - L6474_CONFIG_TOFF_112us = (((uint16_t) 0x1C) << 10), - L6474_CONFIG_TOFF_116us = (((uint16_t) 0x1D) << 10), - L6474_CONFIG_TOFF_120us = (((uint16_t) 0x1E) << 10), - L6474_CONFIG_TOFF_124us = (((uint16_t) 0x1F) << 10) -} L6474_CONFIG_TOFF_t; -/** - * @} - */ - -/** @defgroup L6474_STATUS_Register_Bit_Masks - * @{ - */ -///STATUS Register Bit Masks -typedef enum { - L6474_STATUS_HIZ = (((uint16_t) 0x0001)), - L6474_STATUS_DIR = (((uint16_t) 0x0010)), - L6474_STATUS_NOTPERF_CMD = (((uint16_t) 0x0080)), - L6474_STATUS_WRONG_CMD = (((uint16_t) 0x0100)), - L6474_STATUS_UVLO = (((uint16_t) 0x0200)), - L6474_STATUS_TH_WRN = (((uint16_t) 0x0400)), - L6474_STATUS_TH_SD = (((uint16_t) 0x0800)), - L6474_STATUS_OCD = (((uint16_t) 0x1000)) -} L6474_STATUS_Masks_t; -/** - * @} - */ - -/** @defgroup L6474_Direction_Field_Of_STATUS_Register - * @{ - */ -///Diretion field of STATUS register -typedef enum { - L6474_STATUS_DIR_FORWARD = (((uint16_t) 0x0001) << 4), - L6474_STATUS_DIR_REVERSE = (((uint16_t) 0x0000) << 4) -} L6474_STATUS_DIR_t; -/** - * @} - */ - -/** @defgroup L6474_Internal_Register_Addresses - * @{ - */ -/// Internal L6474 register addresses -typedef enum { - L6474_ABS_POS = ((uint8_t) 0x01), - L6474_EL_POS = ((uint8_t) 0x02), - L6474_MARK = ((uint8_t) 0x03), - L6474_RESERVED_REG01 = ((uint8_t) 0x04), - L6474_RESERVED_REG02 = ((uint8_t) 0x05), - L6474_RESERVED_REG03 = ((uint8_t) 0x06), - L6474_RESERVED_REG04 = ((uint8_t) 0x07), - L6474_RESERVED_REG05 = ((uint8_t) 0x08), - L6474_RESERVED_REG06 = ((uint8_t) 0x15), - L6474_TVAL = ((uint8_t) 0x09), - L6474_RESERVED_REG07 = ((uint8_t) 0x0A), - L6474_RESERVED_REG08 = ((uint8_t) 0x0B), - L6474_RESERVED_REG09 = ((uint8_t) 0x0C), - L6474_RESERVED_REG10 = ((uint8_t) 0x0D), - L6474_T_FAST = ((uint8_t) 0x0E), - L6474_TON_MIN = ((uint8_t) 0x0F), - L6474_TOFF_MIN = ((uint8_t) 0x10), - L6474_RESERVED_REG11 = ((uint8_t) 0x11), - L6474_ADC_OUT = ((uint8_t) 0x12), - L6474_OCD_TH = ((uint8_t) 0x13), - L6474_RESERVED_REG12 = ((uint8_t) 0x14), - L6474_STEP_MODE = ((uint8_t) 0x16), - L6474_ALARM_EN = ((uint8_t) 0x17), - L6474_CONFIG = ((uint8_t) 0x18), - L6474_STATUS = ((uint8_t) 0x19), - L6474_RESERVED_REG13 = ((uint8_t) 0x1A), - L6474_RESERVED_REG14 = ((uint8_t) 0x1B), - L6474_INEXISTENT_REG = ((uint8_t) 0x1F) -} L6474_Registers_t; -/** - * @} - */ - -/** @defgroup L6474_Command_Set - * @{ - */ -/// L6474 command set -typedef enum { - L6474_NOP = ((uint8_t) 0x00), - L6474_SET_PARAM = ((uint8_t) 0x00), - L6474_GET_PARAM = ((uint8_t) 0x20), - L6474_ENABLE = ((uint8_t) 0xB8), - L6474_DISABLE = ((uint8_t) 0xA8), - L6474_GET_STATUS = ((uint8_t) 0xD0), - L6474_RESERVED_CMD1 = ((uint8_t) 0xEB), - L6474_RESERVED_CMD2 = ((uint8_t) 0xF8) -} L6474_Commands_t; - -/** - * @brief L6474 driver initialization structure definition. - */ -/* ACTION --------------------------------------------------------------------* - * Declare here the component's initialization structure, if any, one * - * variable per line without initialization. * - * * - * Example: * - * typedef struct * - * { * - * int frequency; * - * int update_mode; * - * } COMPONENT_Init_t; * - *----------------------------------------------------------------------------*/ -typedef struct -{ - /* Acceleration rate in pps^2. Range: (0..+inf). */ - int acceleration_pps_2; - - /* Deceleration rate in pps^2. Range: (0..+inf). */ - int deceleration_pps_2; - - /* Maximum speed in pps. Range: (30..10000]. */ - int maximum_speed_pps; - - /* Minimum speed in pps. Range: [30..10000). */ - int minimum_speed_pps; - - /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ - float torque_regulation_current_mA; - - /* Overcurrent threshold (OCD_TH register). */ - L6474_OCD_TH_t overcurrent_threshold; - - /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ - L6474_CONFIG_OC_SD_t overcurrent_shutwdown; - - /* Torque regulation method (EN_TQREG field of CONFIG register). */ - L6474_CONFIG_EN_TQREG_t torque_regulation_method; - - /* Step selection (STEP_SEL field of STEP_MODE register). */ - L6474_STEP_SEL_t step_selection; - - /* Sync selection (SYNC_SEL field of STEP_MODE register). */ - L6474_SYNC_SEL_t sync_selection; - - /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ - L6474_FAST_STEP_t fall_time; - - /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ - L6474_TOFF_FAST_t maximum_fast_decay_time; - - /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ - float minimum_ON_time_us; - - /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ - float minimum_OFF_time_us; - - /* Target Swicthing Period (field TOFF of CONFIG register). */ - L6474_CONFIG_TOFF_t target_swicthing_period; - - /* Slew rate (POW_SR field of CONFIG register). */ - L6474_CONFIG_POW_SR_t slew_rate; - - /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ - L6474_CONFIG_OSC_MGMT_t clock; - - /* Alarm (ALARM_EN register). */ - int alarm; -} L6474_init_t; - -/** - * @brief L6474 driver data structure definition. - */ -/* ACTION --------------------------------------------------------------------* - * Declare here the structure of component's data, if any, one variable per * - * line without initialization. * - * * - * Example: * - * typedef struct * - * { * - * int T0_out; * - * int T1_out; * - * float T0_degC; * - * float T1_degC; * - * } COMPONENT_Data_t; * - *----------------------------------------------------------------------------*/ -typedef struct -{ - /// Function pointer to flag interrupt call back - void (*flagInterruptCallback)(void); - /// Function pointer to error handler call back - void (*errorHandlerCallback)(uint16_t error); - bool spiPreemtionByIsr; // = FALSE; - bool isrFlag; // = FALSE; - /// L6474 Device Paramaters structure - deviceParams_t devicePrm; //[MAX_NUMBER_OF_DEVICES]; - uint8_t number_of_devices; - uint8_t device_instance; - uint8_t spiTxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; - uint8_t spiRxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; -} L6474_Data_t; - - -/* Functions -----------------------------------------------------------------*/ - -/** @addtogroup BSP - * @{ - */ - -/** @addtogroup Components - * @{ - */ - -/** @addtogroup L6474 - * @{ - */ - -/** @defgroup L6474_Imported_Functions L6474_Imported_Functions - * @{ - */ - -/* ACTION --------------------------------------------------------------------* - * Declare here extern platform-dependent APIs you might need (e.g.: I/O and * - * interrupt related functions), and implement them in a glue-logic file on * - * the target environment, for example within the "x_nucleo_board.c" file. * - * E.g.: * - * extern status_t COMPONENT_IO_Init (void *handle); * - * extern status_t COMPONENT_IO_Read (handle, buf, regadd, bytes); * - * extern status_t COMPONENT_IO_Write(handle, buf, regadd, bytes); * - * extern void COMPONENT_IO_ITConfig(void); * - *----------------------------------------------------------------------------*/ -///Delay of the requested number of milliseconds -extern void L6474_Delay(void *handle, uint32_t delay); -///Enable Irq -extern void L6474_EnableIrq(void *handle); -///Disable Irq -extern void L6474_DisableIrq(void *handle); -///Set PWM1 frequency and start it -extern void L6474_Pwm1SetFreq(void *handle, uint16_t newFreq); -///Set PWM2 frequency and start it -extern void L6474_Pwm2SetFreq(void *handle, uint16_t newFreq); -///Set PWM3 frequency and start it -extern void L6474_Pwm3SetFreq(void *handle, uint16_t newFreq); -///Init the PWM -extern void L6474_PwmInit(void *handle); -///Stop the PWM -extern void L6474_PwmStop(void *handle); -///Reset the L6474 reset pin -extern void L6474_ReleaseReset(void *handle); -///Set the L6474 reset pin -extern void L6474_Reset(void *handle); -///Set direction GPIO -extern void L6474_SetDirectionGpio(void *handle, uint8_t gpioState); -///Write bytes to the L6474s via SPI -extern uint8_t L6474_SpiWriteBytes(void *handle, uint8_t *pByteToTransmit, uint8_t *pReceivedByte); - -#ifdef __cplusplus - } -#endif - -#endif /* #ifndef __L6474_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/