Personal fork of the library
Fork of X_NUCLEO_IHM01A1 by
Diff: Components/l6474/l6474_class.cpp
- Revision:
- 5:d3c78f12a78d
- Parent:
- 4:83a1eb397a65
- Child:
- 6:a47569fc7534
--- a/Components/l6474/l6474_class.cpp Fri Nov 13 12:56:06 2015 +0000 +++ b/Components/l6474/l6474_class.cpp Wed Nov 18 18:41:55 2015 +0000 @@ -58,12 +58,12 @@ /* Private constants ---------------------------------------------------------*/ /// Error while initialising the SPI -#define L6474_ERROR_0 (0x8000) +#define L6474_ERROR_0 (0x8000) /// Error: Bad SPI transaction -#define L6474_ERROR_1 (0x8001) +#define L6474_ERROR_1 (0x8001) /// Maximum number of steps -#define MAX_STEPS (0x7FFFFFFF) +#define MAX_STEPS (0x7FFFFFFF) /// Maximum frequency of the PWMs in Hz #define L6474_MAX_PWM_FREQ (10000) @@ -77,10 +77,14 @@ /* Number of devices. */ uint8_t L6474::numberOfDevices = 0; +/* SPI Transmission for Daisy-Chain Configuration. */ +uint8_t L6474::spiTxBursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; +uint8_t L6474::spiRxBursts[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 @@ -93,7 +97,7 @@ errorHandlerCallback = (void (*)(uint16_t error)) callback; } -/******************************************************//** +/********************************************************** * @brief Attaches a user callback to the flag Interrupt * The call back will be then called each time the status * flag pin will be pulled down due to the occurrence of @@ -108,7 +112,7 @@ flagInterruptCallback = (void (*)(void))callback; } -/******************************************************//** +/********************************************************** * @brief Starts the L6474 library * @retval COMPONENT_OK in case of success **********************************************************/ @@ -137,7 +141,7 @@ return COMPONENT_OK; } -/******************************************************//** +/********************************************************** * @brief Read id * @param id pointer to the identifier to be read. * @retval COMPONENT_OK in case of success @@ -149,7 +153,7 @@ return COMPONENT_OK; } -/******************************************************//** +/********************************************************** * @brief Returns the acceleration of the specified device * @retval Acceleration in pps^2 **********************************************************/ @@ -158,7 +162,7 @@ return (devicePrm.acceleration); } -/******************************************************//** +/********************************************************** * @brief Returns the current speed of the specified device * @retval Speed in pps **********************************************************/ @@ -167,7 +171,7 @@ return devicePrm.speed; } -/******************************************************//** +/********************************************************** * @brief Returns the deceleration of the specified device * @retval Deceleration in pps^2 **********************************************************/ @@ -176,7 +180,7 @@ return (devicePrm.deceleration); } -/******************************************************//** +/********************************************************** * @brief Returns the device state * @retval State (ACCELERATING, DECELERATING, STEADY or INACTIVE) **********************************************************/ @@ -185,7 +189,7 @@ return devicePrm.motionState; } -/******************************************************//** +/********************************************************** * @brief Returns the FW version of the library * @param None * @retval L6474_FW_VERSION @@ -195,7 +199,7 @@ return (L6474_FW_VERSION); } -/******************************************************//** +/********************************************************** * @brief Returns the mark position of the specified device * @retval Mark register value converted in a 32b signed integer **********************************************************/ @@ -204,7 +208,7 @@ return L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); } -/******************************************************//** +/********************************************************** * @brief Returns the max speed of the specified device * @retval maxSpeed in pps **********************************************************/ @@ -213,7 +217,7 @@ return (devicePrm.maxSpeed); } -/******************************************************//** +/********************************************************** * @brief Returns the min speed of the specified device * @retval minSpeed in pps **********************************************************/ @@ -222,7 +226,7 @@ return (devicePrm.minSpeed); } -/******************************************************//** +/********************************************************** * @brief Returns the ABS_POSITION of the specified device * @retval ABS_POSITION register value converted in a 32b signed integer **********************************************************/ @@ -231,7 +235,7 @@ return L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); } -/******************************************************//** +/********************************************************** * @brief Requests the motor to move to the home position (ABS_POSITION = 0) * @retval None **********************************************************/ @@ -240,7 +244,7 @@ L6474_GoTo(0); } -/******************************************************//** +/********************************************************** * @brief Requests the motor to move to the mark position * @retval None **********************************************************/ @@ -252,14 +256,14 @@ 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) { - direction_t direction; + motorDir_t direction; int32_t steps; /* Eventually deactivate motor */ @@ -277,12 +281,12 @@ if (steps >= 0) { devicePrm.stepsToTake = steps; - direction = CW; + direction = FORWARD; } else { devicePrm.stepsToTake = -steps; - direction = CCW; + direction = BACKWARD; } if (steps != 0) @@ -299,7 +303,7 @@ } } -/******************************************************//** +/********************************************************** * @brief Immediatly stops the motor and disable the power bridge * @retval None **********************************************************/ @@ -317,13 +321,13 @@ devicePrm.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(direction_t direction, uint32_t stepCount) +void L6474::L6474_Move(motorDir_t direction, uint32_t stepCount) { /* Eventually deactivate motor */ if (devicePrm.motionState != INACTIVE) @@ -350,7 +354,7 @@ } #if 0 -/******************************************************//** +/********************************************************** * @brief Resets all L6474 devices * @param None * @retval None @@ -371,13 +375,13 @@ } #endif -/******************************************************//** +/********************************************************** * @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(direction_t direction) +void L6474::L6474_Run(motorDir_t direction) { /* Eventually deactivate motor */ if (devicePrm.motionState != INACTIVE) @@ -394,7 +398,7 @@ 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 @@ -414,7 +418,7 @@ 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 @@ -434,7 +438,7 @@ return cmdExecuted; } -/******************************************************//** +/********************************************************** * @brief Set current position to be the Home position (ABS pos set to 0) * @retval None **********************************************************/ @@ -443,7 +447,7 @@ L6474_CmdSetParam(L6474_ABS_POS, 0); } -/******************************************************//** +/********************************************************** * @brief Sets current position to be the Mark position * @retval None **********************************************************/ @@ -453,7 +457,7 @@ 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 @@ -475,7 +479,7 @@ 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 @@ -497,7 +501,7 @@ 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. @@ -513,7 +517,7 @@ return (cmdExecuted); } -/******************************************************//** +/********************************************************** * @brief Locks until the device state becomes Inactive * @retval None **********************************************************/ @@ -523,7 +527,7 @@ while (L6474_GetDeviceState() != INACTIVE); } -/******************************************************//** +/********************************************************** * @brief Issue the Disable command to the L6474 of the specified device * @retval None **********************************************************/ @@ -532,7 +536,7 @@ L6474_SendCommand(L6474_DISABLE); } -/******************************************************//** +/********************************************************** * @brief Issues the Enable command to the L6474 of the specified device * @retval None **********************************************************/ @@ -541,7 +545,7 @@ L6474_SendCommand(L6474_ENABLE); } -/******************************************************//** +/********************************************************** * @brief Issues the GetParam command to the L6474 of the specified device * @param[in] param Register adress (L6474_ABS_POS, L6474_MARK,...) * @retval Register value @@ -551,6 +555,7 @@ uint32_t i; uint32_t spiRxData; uint8_t maxArgumentNbBytes = 0; + uint8_t spiIndex = numberOfDevices - deviceInstance - 1; bool itDisable = FALSE; do @@ -563,29 +568,32 @@ itDisable = FALSE; } - spiTxBursts[0] = L6474_NOP; - spiTxBursts[1] = L6474_NOP; - spiTxBursts[2] = L6474_NOP; - spiTxBursts[3] = L6474_NOP; - spiRxBursts[1] = 0; - spiRxBursts[2] = 0; - spiRxBursts[3] = 0; + for (i = 0; i < numberOfDevices; i++) + { + spiTxBursts[0][i] = L6474_NOP; + spiTxBursts[1][i] = L6474_NOP; + spiTxBursts[2][i] = L6474_NOP; + spiTxBursts[3][i] = L6474_NOP; + spiRxBursts[1][i] = 0; + spiRxBursts[2][i] = 0; + spiRxBursts[3][i] = 0; + } switch (param) { case L6474_ABS_POS: ; case L6474_MARK: - spiTxBursts[0] = ((uint8_t)L6474_GET_PARAM )| (param); + spiTxBursts[0][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (param); maxArgumentNbBytes = 3; break; case L6474_EL_POS: ; case L6474_CONFIG: ; case L6474_STATUS: - spiTxBursts[1] = ((uint8_t)L6474_GET_PARAM )| (param); + spiTxBursts[1][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (param); maxArgumentNbBytes = 2; break; default: - spiTxBursts[2] = ((uint8_t)L6474_GET_PARAM )| (param); + spiTxBursts[2][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (param); maxArgumentNbBytes = 1; } @@ -599,12 +607,12 @@ i < L6474_CMD_ARG_MAX_NB_BYTES; i++) { - L6474_WriteBytes(&spiTxBursts[i], &spiRxBursts[i]); + L6474_WriteBytes(&spiTxBursts[i][0], &spiRxBursts[i][0]); } - spiRxData = ((uint32_t)spiRxBursts[1] << 16) | - (spiRxBursts[2] << 8) | - (spiRxBursts[3]); + spiRxData = ((uint32_t)spiRxBursts[1][spiIndex] << 16) | + (spiRxBursts[2][spiIndex] << 8) | + (spiRxBursts[3][spiIndex]); /* re-enable L6474_EnableIrq after SPI transfers*/ L6474_EnableIrq(); @@ -612,7 +620,7 @@ 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 @@ -623,6 +631,7 @@ { uint32_t i; uint16_t status; + uint8_t spiIndex = numberOfDevices - deviceInstance - 1; bool itDisable = FALSE; do @@ -635,12 +644,15 @@ itDisable = FALSE; } - spiTxBursts[0] = L6474_NOP; - spiTxBursts[1] = L6474_NOP; - spiTxBursts[2] = L6474_NOP; - spiRxBursts[1] = 0; - spiRxBursts[2] = 0; - spiTxBursts[0] = L6474_GET_STATUS; + for (i = 0; i < numberOfDevices; i++) + { + spiTxBursts[0][i] = L6474_NOP; + spiTxBursts[1][i] = L6474_NOP; + spiTxBursts[2][i] = L6474_NOP; + spiRxBursts[1][i] = 0; + spiRxBursts[2][i] = 0; + } + spiTxBursts[0][spiIndex] = L6474_GET_STATUS; /* Disable interruption before checking */ /* pre-emption by ISR and SPI transfers*/ @@ -650,9 +662,9 @@ for (i = 0; i < L6474_CMD_ARG_NB_BYTES_GET_STATUS + L6474_RSP_NB_BYTES_GET_STATUS; i++) { - L6474_WriteBytes(&spiTxBursts[i], &spiRxBursts[i]); + L6474_WriteBytes(&spiTxBursts[i][0], &spiRxBursts[i][0]); } - status = (spiRxBursts[1] << 8) | (spiRxBursts[2]); + status = (spiRxBursts[1][spiIndex] << 8) | (spiRxBursts[2][spiIndex]); /* re-enable L6474_EnableIrq after SPI transfers*/ L6474_EnableIrq(); @@ -660,7 +672,7 @@ return (status); } -/******************************************************//** +/********************************************************** * @brief Issues the Nop command to the L6474 of the specified device * @retval None **********************************************************/ @@ -669,7 +681,7 @@ L6474_SendCommand(L6474_NOP); } -/******************************************************//** +/********************************************************** * @brief Issues the SetParam command to the L6474 of the specified device * @param[in] param Register adress (L6474_ABS_POS, L6474_MARK,...) * @param[in] value Value to set in the register @@ -679,6 +691,7 @@ { uint32_t i; uint8_t maxArgumentNbBytes = 0; + uint8_t spiIndex = numberOfDevices - deviceInstance - 1; bool itDisable = FALSE; do { @@ -690,32 +703,35 @@ itDisable = FALSE; } - spiTxBursts[0] = L6474_NOP; - spiTxBursts[1] = L6474_NOP; - spiTxBursts[2] = L6474_NOP; - spiTxBursts[3] = L6474_NOP; + for (i = 0; i < numberOfDevices; i++) + { + spiTxBursts[0][i] = L6474_NOP; + spiTxBursts[1][i] = L6474_NOP; + spiTxBursts[2][i] = L6474_NOP; + spiTxBursts[3][i] = L6474_NOP; + } switch (param) { case L6474_ABS_POS: ; case L6474_MARK: - spiTxBursts[0] = param; - spiTxBursts[1] = (uint8_t)(value >> 16); - spiTxBursts[2] = (uint8_t)(value >> 8); + spiTxBursts[0][spiIndex] = param; + spiTxBursts[1][spiIndex] = (uint8_t)(value >> 16); + spiTxBursts[2][spiIndex] = (uint8_t)(value >> 8); maxArgumentNbBytes = 3; break; case L6474_EL_POS: ; case L6474_CONFIG: - spiTxBursts[1] = param; - spiTxBursts[2] = (uint8_t)(value >> 8); + spiTxBursts[1][spiIndex] = param; + spiTxBursts[2][spiIndex] = (uint8_t)(value >> 8); maxArgumentNbBytes = 2; break; default: - spiTxBursts[2] = param; + spiTxBursts[2][spiIndex] = param; maxArgumentNbBytes = 1; break; } - spiTxBursts[3] = (uint8_t)(value); + spiTxBursts[3][spiIndex] = (uint8_t)(value); /* Disable interruption before checking */ /* pre-emption by ISR and SPI transfers*/ @@ -728,13 +744,13 @@ i < L6474_CMD_ARG_MAX_NB_BYTES; i++) { - L6474_WriteBytes(&spiTxBursts[i],&spiRxBursts[i]); + L6474_WriteBytes(&spiTxBursts[i][0],&spiRxBursts[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 @@ -745,7 +761,7 @@ 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 @@ -791,32 +807,33 @@ L6474_SetHome(); } -/******************************************************//** - * @brief Get the direction - * @retval direction clockwise or counter-clockwise +/********************************************************** + * @brief Get the direction + * @param None + * @retval direction FORWARD or BACKWARD **********************************************************/ -StepperMotor::direction_t L6474::L6474_GetDirection(void) +motorDir_t L6474::L6474_GetDirection(void) { - return (devicePrm.direction == FORWARD ? CW : CCW); + return devicePrm.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(direction_t direction) +void L6474::L6474_SetDirection(motorDir_t direction) { if (devicePrm.motionState == INACTIVE) { - devicePrm.direction = (direction == CW ? FORWARD : BACKWARD); - L6474_SetDirectionGpio(direction == CW ? 1 : 0); + devicePrm.direction = direction; + L6474_SetDirectionGpio(direction); } } -/******************************************************//** +/********************************************************** * @brief Updates the current speed of the device * @param[in] newSpeed in pps * @retval None @@ -837,7 +854,7 @@ 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 @@ -901,7 +918,7 @@ } } -/******************************************************//** +/********************************************************** * @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 @@ -923,10 +940,11 @@ { 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 @@ -946,7 +964,7 @@ } } -/******************************************************//** +/********************************************************** * @brief Handlers of the flag interrupt which calls the user callback (if defined) * @param None * @retval None @@ -965,14 +983,16 @@ } } -/******************************************************//** +/********************************************************** * @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) { - bool itDisable = FALSE; + uint32_t i; + bool itDisable = FALSE; + uint8_t spiIndex = numberOfDevices - deviceInstance - 1; do { @@ -984,8 +1004,11 @@ itDisable = FALSE; } - spiTxBursts[3] = L6474_NOP; - spiTxBursts[3] = param; + for (i = 0; i < numberOfDevices; i++) + { + spiTxBursts[3][i] = L6474_NOP; + } + spiTxBursts[3][spiIndex] = param; /* Disable interruption before checking */ /* pre-emption by ISR and SPI transfers*/ @@ -993,13 +1016,13 @@ itDisable = TRUE; } while (spiPreemtionByIsr); // check pre-emption by ISR - L6474_WriteBytes(&spiTxBursts[3], &spiRxBursts[3]); + L6474_WriteBytes(&spiTxBursts[3][0], &spiRxBursts[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 @@ -1118,7 +1141,7 @@ } } -/******************************************************//** +/********************************************************** * @brief Sets the parameters of the device to predefined values * from l6474_target_config.h * @param None @@ -1164,7 +1187,7 @@ L6474_SetRegisterToPredefinedValues(); } -/******************************************************//** +/********************************************************** * @brief Initialises the bridge parameters to start the movement * and enable the power bridge * @retval None @@ -1186,7 +1209,7 @@ L6474_ApplySpeed(devicePrm.minSpeed); } -/******************************************************//** +/********************************************************** * @brief Handles the device state machine at each ste * @retval None * @note Must only be called by the timer ISR @@ -1324,7 +1347,7 @@ isrFlag = FALSE; } -/******************************************************//** +/********************************************************** * @brief Converts mA in compatible values for TVAL register * @param[in] Tval * @retval TVAL values @@ -1334,7 +1357,7 @@ return ((uint8_t)(((Tval - 31.25)/31.25)+0.5)); } -/******************************************************//** +/********************************************************** * @brief Convert time in us in compatible values * for TON_MIN register * @param[in] Tmin @@ -1345,7 +1368,7 @@ return ((uint8_t)(((Tmin - 0.5)*2)+0.5)); } -/******************************************************//** +/********************************************************** * @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