Personal fork of the library
Fork of X_NUCLEO_IHM01A1 by
Diff: Components/l6474/l6474_class.cpp
- Revision:
- 18:2d6ab2b93685
- Parent:
- 15:40470df81d9a
- Child:
- 21:83138e702683
--- a/Components/l6474/l6474_class.cpp Mon Jan 04 15:54:07 2016 +0000 +++ b/Components/l6474/l6474_class.cpp Wed Jan 13 14:35:59 2016 +0000 @@ -76,15 +76,15 @@ /* Variables ----------------------------------------------------------------*/ /* Number of devices. */ -uint8_t L6474::numberOfDevices = 0; +uint8_t L6474::number_of_devices = 0; /* ISR flags used to restart an interrupted SPI transfer when an error is reported. */ -bool L6474::spiPreemtionByIsr = FALSE; -bool L6474::isrFlag = FALSE; +bool L6474::spi_preemtion_by_isr = FALSE; +bool L6474::isr_flag = FALSE; /* 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]; +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 -------------------------------------------------------------------*/ @@ -99,14 +99,15 @@ **********************************************************/ void L6474::L6474_AttachErrorHandler(void (*callback)(uint16_t error)) { - errorHandlerCallback = (void (*)(uint16_t error)) callback; + error_handler_callback = (void (*)(uint16_t error)) callback; } /********************************************************** * @brief Starts the L6474 library - * @retval COMPONENT_OK in case of success + * @param init Initialization structure. + * @retval COMPONENT_OK in case of success. **********************************************************/ -DrvStatusTypeDef L6474::L6474_Init(MOTOR_InitTypeDef *L6474_Init) +DrvStatusTypeDef L6474::L6474_Init(L6474_InitTypeDef *init) { /* Initialise the PWMs used for the Step clocks ----------------------------*/ L6474_PwmInit(); @@ -119,9 +120,16 @@ /* Let a delay after reset */ L6474_Delay(1); - /* Set all registers and context variables to the predefined values from l6474_target_config.h */ + /* 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(init); + /* Disable L6474 powerstage */ L6474_CmdDisable(); @@ -134,11 +142,11 @@ /********************************************************** * @brief Read id * @param id pointer to the identifier to be read. - * @retval COMPONENT_OK in case of success + * @retval COMPONENT_OK in case of success. **********************************************************/ DrvStatusTypeDef L6474::L6474_ReadID(uint8_t *id) { - *id = deviceInstance; + *id = device_instance; return COMPONENT_OK; } @@ -149,7 +157,7 @@ **********************************************************/ uint16_t L6474::L6474_GetAcceleration(void) { - return (devicePrm.acceleration); + return (device_prm.acceleration); } /********************************************************** @@ -158,7 +166,7 @@ **********************************************************/ uint16_t L6474::L6474_GetCurrentSpeed(void) { - return devicePrm.speed; + return device_prm.speed; } /********************************************************** @@ -167,7 +175,7 @@ **********************************************************/ uint16_t L6474::L6474_GetDeceleration(void) { - return (devicePrm.deceleration); + return (device_prm.deceleration); } /********************************************************** @@ -176,7 +184,7 @@ **********************************************************/ motorState_t L6474::L6474_GetDeviceState(void) { - return devicePrm.motionState; + return device_prm.motionState; } /********************************************************** @@ -204,7 +212,7 @@ **********************************************************/ uint16_t L6474::L6474_GetMaxSpeed(void) { - return (devicePrm.maxSpeed); + return (device_prm.maxSpeed); } /********************************************************** @@ -213,7 +221,7 @@ **********************************************************/ uint16_t L6474::L6474_GetMinSpeed(void) { - return (devicePrm.minSpeed); + return (device_prm.minSpeed); } /********************************************************** @@ -257,36 +265,36 @@ int32_t steps; /* Eventually deactivate motor */ - if (devicePrm.motionState != INACTIVE) + if (device_prm.motionState != INACTIVE) { L6474_HardStop(); } /* Get current position */ - devicePrm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); + device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); /* Compute the number of steps to perform */ - steps = targetPosition - devicePrm.currentPosition; + steps = targetPosition - device_prm.currentPosition; if (steps >= 0) { - devicePrm.stepsToTake = steps; + device_prm.stepsToTake = steps; direction = FORWARD; } else { - devicePrm.stepsToTake = -steps; + device_prm.stepsToTake = -steps; direction = BACKWARD; } if (steps != 0) { - devicePrm.commandExecuted = MOVE_CMD; + device_prm.commandExecuted = MOVE_CMD; /* Direction setup */ L6474_SetDirection(direction); - L6474_ComputeSpeedProfile(devicePrm.stepsToTake); + L6474_ComputeSpeedProfile(device_prm.stepsToTake); /* Motor activation */ L6474_StartMovement(); @@ -303,9 +311,9 @@ L6474_PwmStop(); /* Set inactive state */ - devicePrm.motionState = INACTIVE; - devicePrm.commandExecuted = NO_CMD; - devicePrm.stepsToTake = MAX_STEPS; + device_prm.motionState = INACTIVE; + device_prm.commandExecuted = NO_CMD; + device_prm.stepsToTake = MAX_STEPS; } /********************************************************** @@ -317,18 +325,18 @@ void L6474::L6474_Move(motorDir_t direction, uint32_t stepCount) { /* Eventually deactivate motor */ - if (devicePrm.motionState != INACTIVE) + if (device_prm.motionState != INACTIVE) { L6474_HardStop(); } if (stepCount != 0) { - devicePrm.stepsToTake = stepCount; + device_prm.stepsToTake = stepCount; - devicePrm.commandExecuted = MOVE_CMD; + device_prm.commandExecuted = MOVE_CMD; - devicePrm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); + device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); /* Direction setup */ L6474_SetDirection(direction); @@ -349,7 +357,7 @@ void L6474::L6474_Run(motorDir_t direction) { /* Eventually deactivate motor */ - if (devicePrm.motionState != INACTIVE) + if (device_prm.motionState != INACTIVE) { L6474_HardStop(); } @@ -357,7 +365,7 @@ /* Direction setup */ L6474_SetDirection(direction); - devicePrm.commandExecuted = RUN_CMD; + device_prm.commandExecuted = RUN_CMD; /* Motor activation */ L6474_StartMovement(); @@ -374,10 +382,10 @@ { bool cmdExecuted = FALSE; if ((newAcc != 0)&& - ((devicePrm.motionState == INACTIVE)|| - (devicePrm.commandExecuted == RUN_CMD))) + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) { - devicePrm.acceleration = newAcc; + device_prm.acceleration = newAcc; cmdExecuted = TRUE; } return cmdExecuted; @@ -394,10 +402,10 @@ { bool cmdExecuted = FALSE; if ((newDec != 0)&& - ((devicePrm.motionState == INACTIVE)|| - (devicePrm.commandExecuted == RUN_CMD))) + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) { - devicePrm.deceleration = newDec; + device_prm.deceleration = newDec; cmdExecuted = TRUE; } return cmdExecuted; @@ -434,11 +442,11 @@ bool cmdExecuted = FALSE; if ((newMaxSpeed >= L6474_MIN_PWM_FREQ)&& (newMaxSpeed <= L6474_MAX_PWM_FREQ) && - (devicePrm.minSpeed <= newMaxSpeed) && - ((devicePrm.motionState == INACTIVE)|| - (devicePrm.commandExecuted == RUN_CMD))) + (device_prm.minSpeed <= newMaxSpeed) && + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) { - devicePrm.maxSpeed = newMaxSpeed; + device_prm.maxSpeed = newMaxSpeed; cmdExecuted = TRUE; } return cmdExecuted; @@ -456,11 +464,11 @@ bool cmdExecuted = FALSE; if ((newMinSpeed >= L6474_MIN_PWM_FREQ)&& (newMinSpeed <= L6474_MAX_PWM_FREQ) && - (newMinSpeed <= devicePrm.maxSpeed) && - ((devicePrm.motionState == INACTIVE)|| - (devicePrm.commandExecuted == RUN_CMD))) + (newMinSpeed <= device_prm.maxSpeed) && + ((device_prm.motionState == INACTIVE)|| + (device_prm.commandExecuted == RUN_CMD))) { - devicePrm.minSpeed = newMinSpeed; + device_prm.minSpeed = newMinSpeed; cmdExecuted = TRUE; } return cmdExecuted; @@ -474,9 +482,9 @@ bool L6474::L6474_SoftStop(void) { bool cmdExecuted = FALSE; - if (devicePrm.motionState != INACTIVE) + if (device_prm.motionState != INACTIVE) { - devicePrm.commandExecuted = SOFT_STOP_CMD; + device_prm.commandExecuted = SOFT_STOP_CMD; cmdExecuted = TRUE; } return (cmdExecuted); @@ -520,12 +528,12 @@ uint32_t i; uint32_t spiRxData; uint8_t maxArgumentNbBytes = 0; - uint8_t spiIndex = numberOfDevices - deviceInstance - 1; + uint8_t spiIndex = number_of_devices - device_instance - 1; bool itDisable = FALSE; do { - spiPreemtionByIsr = FALSE; + spi_preemtion_by_isr = FALSE; if (itDisable) { /* re-enable L6474_EnableIrq if disable in previous iteration */ @@ -533,32 +541,32 @@ itDisable = FALSE; } - for (i = 0; i < numberOfDevices; i++) + for (i = 0; i < number_of_devices; 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; + 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: - spiTxBursts[0][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); + spi_tx_bursts[0][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); maxArgumentNbBytes = 3; break; case L6474_EL_POS: ; case L6474_CONFIG: ; case L6474_STATUS: - spiTxBursts[1][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); + spi_tx_bursts[1][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); maxArgumentNbBytes = 2; break; default: - spiTxBursts[2][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); + spi_tx_bursts[2][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); maxArgumentNbBytes = 1; } @@ -566,18 +574,18 @@ /* pre-emption by ISR and SPI transfers*/ L6474_DisableIrq(); itDisable = TRUE; - } while (spiPreemtionByIsr); // check pre-emption by ISR + } 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(&spiTxBursts[i][0], &spiRxBursts[i][0]); + L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); } - spiRxData = ((uint32_t)spiRxBursts[1][spiIndex] << 16) | - (spiRxBursts[2][spiIndex] << 8) | - (spiRxBursts[3][spiIndex]); + 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(); @@ -596,12 +604,12 @@ { uint32_t i; uint16_t status; - uint8_t spiIndex = numberOfDevices - deviceInstance - 1; + uint8_t spiIndex = number_of_devices - device_instance - 1; bool itDisable = FALSE; do { - spiPreemtionByIsr = FALSE; + spi_preemtion_by_isr = FALSE; if (itDisable) { /* re-enable L6474_EnableIrq if disable in previous iteration */ @@ -609,27 +617,27 @@ itDisable = FALSE; } - for (i = 0; i < numberOfDevices; i++) + for (i = 0; i < number_of_devices; i++) { - spiTxBursts[0][i] = L6474_NOP; - spiTxBursts[1][i] = L6474_NOP; - spiTxBursts[2][i] = L6474_NOP; - spiRxBursts[1][i] = 0; - spiRxBursts[2][i] = 0; + 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; } - spiTxBursts[0][spiIndex] = L6474_GET_STATUS; + spi_tx_bursts[0][spiIndex] = L6474_GET_STATUS; /* Disable interruption before checking */ /* pre-emption by ISR and SPI transfers*/ L6474_DisableIrq(); itDisable = TRUE; - } while (spiPreemtionByIsr); // check pre-emption by ISR + } 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(&spiTxBursts[i][0], &spiRxBursts[i][0]); + L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); } - status = (spiRxBursts[1][spiIndex] << 8) | (spiRxBursts[2][spiIndex]); + status = (spi_rx_bursts[1][spiIndex] << 8) | (spi_rx_bursts[2][spiIndex]); /* re-enable L6474_EnableIrq after SPI transfers*/ L6474_EnableIrq(); @@ -656,11 +664,11 @@ { uint32_t i; uint8_t maxArgumentNbBytes = 0; - uint8_t spiIndex = numberOfDevices - deviceInstance - 1; + uint8_t spiIndex = number_of_devices - device_instance - 1; bool itDisable = FALSE; do { - spiPreemtionByIsr = FALSE; + spi_preemtion_by_isr = FALSE; if (itDisable) { /* re-enable L6474_EnableIrq if disable in previous iteration */ @@ -668,48 +676,48 @@ itDisable = FALSE; } - for (i = 0; i < numberOfDevices; i++) + for (i = 0; i < number_of_devices; i++) { - spiTxBursts[0][i] = L6474_NOP; - spiTxBursts[1][i] = L6474_NOP; - spiTxBursts[2][i] = L6474_NOP; - spiTxBursts[3][i] = L6474_NOP; + 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: - spiTxBursts[0][spiIndex] = parameter; - spiTxBursts[1][spiIndex] = (uint8_t)(value >> 16); - spiTxBursts[2][spiIndex] = (uint8_t)(value >> 8); + 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: - spiTxBursts[1][spiIndex] = parameter; - spiTxBursts[2][spiIndex] = (uint8_t)(value >> 8); + spi_tx_bursts[1][spiIndex] = parameter; + spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); maxArgumentNbBytes = 2; break; default: - spiTxBursts[2][spiIndex] = parameter; + spi_tx_bursts[2][spiIndex] = parameter; maxArgumentNbBytes = 1; break; } - spiTxBursts[3][spiIndex] = (uint8_t)(value); + spi_tx_bursts[3][spiIndex] = (uint8_t)(value); /* Disable interruption before checking */ /* pre-emption by ISR and SPI transfers*/ L6474_DisableIrq(); itDisable = TRUE; - } while (spiPreemtionByIsr); // check pre-emption by ISR + } 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(&spiTxBursts[i][0],&spiRxBursts[i][0]); + L6474_WriteBytes(&spi_tx_bursts[i][0],&spi_rx_bursts[i][0]); } /* re-enable L6474_EnableIrq after SPI transfers*/ L6474_EnableIrq(); @@ -757,7 +765,7 @@ } /* Eventually deactivate motor */ - if (devicePrm.motionState != INACTIVE) + if (device_prm.motionState != INACTIVE) { L6474_HardStop(); } @@ -779,7 +787,7 @@ **********************************************************/ motorDir_t L6474::L6474_GetDirection(void) { - return devicePrm.direction; + return device_prm.direction; } /********************************************************** @@ -791,9 +799,9 @@ **********************************************************/ void L6474::L6474_SetDirection(motorDir_t direction) { - if (devicePrm.motionState == INACTIVE) + if (device_prm.motionState == INACTIVE) { - devicePrm.direction = direction; + device_prm.direction = direction; L6474_SetDirectionGpio(direction); } } @@ -814,7 +822,7 @@ newSpeed = L6474_MAX_PWM_FREQ; } - devicePrm.speed = newSpeed; + device_prm.speed = newSpeed; L6474_PwmSetFreq(newSpeed); } @@ -838,23 +846,23 @@ uint32_t reqDecSteps; /* compute the number of steps to get the targeted speed */ - uint16_t minSpeed = devicePrm.minSpeed; - reqAccSteps = (devicePrm.maxSpeed - minSpeed); - reqAccSteps *= (devicePrm.maxSpeed + minSpeed); + uint16_t minSpeed = device_prm.minSpeed; + reqAccSteps = (device_prm.maxSpeed - minSpeed); + reqAccSteps *= (device_prm.maxSpeed + minSpeed); reqDecSteps = reqAccSteps; - reqAccSteps /= (uint32_t)devicePrm.acceleration; + reqAccSteps /= (uint32_t)device_prm.acceleration; reqAccSteps /= 2; /* compute the number of steps to stop */ - reqDecSteps /= (uint32_t)devicePrm.deceleration; + reqDecSteps /= (uint32_t)device_prm.deceleration; reqDecSteps /= 2; if(( reqAccSteps + reqDecSteps ) > nbSteps) { /* Triangular move */ /* reqDecSteps = (Pos * Dec) /(Dec+Acc) */ - uint32_t dec = devicePrm.deceleration; - uint32_t acc = devicePrm.acceleration; + uint32_t dec = device_prm.deceleration; + uint32_t acc = device_prm.acceleration; reqDecSteps = ((uint32_t) dec * nbSteps) / (acc + dec); if (reqDecSteps > 1) @@ -869,8 +877,8 @@ { reqAccSteps = 0; } - devicePrm.endAccPos = reqAccSteps; - devicePrm.startDecPos = reqDecSteps; + device_prm.endAccPos = reqAccSteps; + device_prm.startDecPos = reqDecSteps; } else { @@ -878,8 +886,8 @@ /* accelerating phase to endAccPos */ /* steady phase from endAccPos to startDecPos */ /* decelerating from startDecPos to stepsToTake*/ - devicePrm.endAccPos = reqAccSteps; - devicePrm.startDecPos = nbSteps - reqDecSteps - 1; + device_prm.endAccPos = reqAccSteps; + device_prm.startDecPos = nbSteps - reqDecSteps - 1; } } @@ -916,9 +924,9 @@ **********************************************************/ void L6474::L6474_ErrorHandler(uint16_t error) { - if (errorHandlerCallback != 0) + if (error_handler_callback != 0) { - (void) errorHandlerCallback(error); + (void) error_handler_callback(error); } else { @@ -936,11 +944,11 @@ { uint32_t i; bool itDisable = FALSE; - uint8_t spiIndex = numberOfDevices - deviceInstance - 1; + uint8_t spiIndex = number_of_devices - device_instance - 1; do { - spiPreemtionByIsr = FALSE; + spi_preemtion_by_isr = FALSE; if (itDisable) { /* re-enable L6474_EnableIrq if disable in previous iteration */ @@ -948,19 +956,19 @@ itDisable = FALSE; } - for (i = 0; i < numberOfDevices; i++) + for (i = 0; i < number_of_devices; i++) { - spiTxBursts[3][i] = L6474_NOP; + spi_tx_bursts[3][i] = L6474_NOP; } - spiTxBursts[3][spiIndex] = param; + spi_tx_bursts[3][spiIndex] = param; /* Disable interruption before checking */ /* pre-emption by ISR and SPI transfers*/ L6474_DisableIrq(); itDisable = TRUE; - } while (spiPreemtionByIsr); // check pre-emption by ISR + } while (spi_preemtion_by_isr); // check pre-emption by ISR - L6474_WriteBytes(&spiTxBursts[3][0], &spiRxBursts[3][0]); + L6474_WriteBytes(&spi_tx_bursts[3][0], &spi_rx_bursts[3][0]); /* re-enable L6474_EnableIrq after SPI transfers*/ L6474_EnableIrq(); @@ -982,7 +990,7 @@ L6474_CmdSetParam( L6474_MARK, 0); - switch (deviceInstance) + switch (device_instance) { case 0: L6474_CmdSetParam( @@ -1086,6 +1094,65 @@ } /********************************************************** + * @brief Sets the registers of the L6474 to initialization values. + * @param init Initialization structure. + * @retval None. + **********************************************************/ +void L6474::L6474_SetRegisterToInitializationValues(L6474_InitTypeDef *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 + ); +} + +/********************************************************** * @brief Sets the parameters of the device to predefined values * from l6474_target_config.h * @param None @@ -1093,42 +1160,40 @@ **********************************************************/ void L6474::L6474_SetDeviceParamsToPredefinedValues(void) { - switch (deviceInstance) + switch (device_instance) { case 0: - devicePrm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_0; - devicePrm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_0; - devicePrm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_0; - devicePrm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_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: - devicePrm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_1; - devicePrm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_1; - devicePrm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_1; - devicePrm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_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: - devicePrm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_2; - devicePrm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_2; - devicePrm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_2; - devicePrm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_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; } - devicePrm.accu = 0; - devicePrm.currentPosition = 0; - devicePrm.endAccPos = 0; - devicePrm.relativePos = 0; - devicePrm.startDecPos = 0; - devicePrm.stepsToTake = 0; - devicePrm.speed = 0; - devicePrm.commandExecuted = NO_CMD; - devicePrm.direction = FORWARD; - devicePrm.motionState = INACTIVE; - - L6474_SetRegisterToPredefinedValues(); + 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; } /********************************************************** @@ -1140,17 +1205,17 @@ { /* Enable L6474 powerstage */ L6474_CmdEnable(); - if (devicePrm.endAccPos != 0) + if (device_prm.endAccPos != 0) { - devicePrm.motionState = ACCELERATING; + device_prm.motionState = ACCELERATING; } else { - devicePrm.motionState = DECELERATING; + device_prm.motionState = DECELERATING; } - devicePrm.accu = 0; - devicePrm.relativePos = 0; - L6474_ApplySpeed(devicePrm.minSpeed); + device_prm.accu = 0; + device_prm.relativePos = 0; + L6474_ApplySpeed(device_prm.minSpeed); } /********************************************************** @@ -1161,107 +1226,107 @@ void L6474::L6474_StepClockHandler(void) { /* Set isr flag */ - isrFlag = TRUE; + isr_flag = TRUE; /* Incrementation of the relative position */ - devicePrm.relativePos++; + device_prm.relativePos++; - switch (devicePrm.motionState) + switch (device_prm.motionState) { case ACCELERATING: { - uint32_t relPos = devicePrm.relativePos; - uint32_t endAccPos = devicePrm.endAccPos; - uint16_t speed = devicePrm.speed; - uint32_t acc = ((uint32_t)devicePrm.acceleration << 16); + 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 ((devicePrm.commandExecuted == SOFT_STOP_CMD)|| - ((devicePrm.commandExecuted != RUN_CMD)&& - (relPos == devicePrm.startDecPos))) + if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| + ((device_prm.commandExecuted != RUN_CMD)&& + (relPos == device_prm.startDecPos))) { - devicePrm.motionState = DECELERATING; - devicePrm.accu = 0; + device_prm.motionState = DECELERATING; + device_prm.accu = 0; } - else if ((speed >= devicePrm.maxSpeed)|| - ((devicePrm.commandExecuted != RUN_CMD)&& + else if ((speed >= device_prm.maxSpeed)|| + ((device_prm.commandExecuted != RUN_CMD)&& (relPos == endAccPos))) { - devicePrm.motionState = STEADY; + device_prm.motionState = STEADY; } else { bool speedUpdated = FALSE; /* Go on accelerating */ if (speed == 0) speed =1; - devicePrm.accu += acc / speed; - while (devicePrm.accu >= (0X10000L)) + device_prm.accu += acc / speed; + while (device_prm.accu >= (0X10000L)) { - devicePrm.accu -= (0X10000L); + device_prm.accu -= (0X10000L); speed +=1; speedUpdated = TRUE; } if (speedUpdated) { - if (speed > devicePrm.maxSpeed) + if (speed > device_prm.maxSpeed) { - speed = devicePrm.maxSpeed; + speed = device_prm.maxSpeed; } - devicePrm.speed = speed; - L6474_ApplySpeed(devicePrm.speed); + device_prm.speed = speed; + L6474_ApplySpeed(device_prm.speed); } } break; } case STEADY: { - uint16_t maxSpeed = devicePrm.maxSpeed; - uint32_t relativePos = devicePrm.relativePos; - if ((devicePrm.commandExecuted == SOFT_STOP_CMD)|| - ((devicePrm.commandExecuted != RUN_CMD)&& - (relativePos >= (devicePrm.startDecPos))) || - ((devicePrm.commandExecuted == RUN_CMD)&& - (devicePrm.speed > maxSpeed))) + 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))) { - devicePrm.motionState = DECELERATING; - devicePrm.accu = 0; + device_prm.motionState = DECELERATING; + device_prm.accu = 0; } - else if ((devicePrm.commandExecuted == RUN_CMD)&& - (devicePrm.speed < maxSpeed)) + else if ((device_prm.commandExecuted == RUN_CMD)&& + (device_prm.speed < maxSpeed)) { - devicePrm.motionState = ACCELERATING; - devicePrm.accu = 0; + device_prm.motionState = ACCELERATING; + device_prm.accu = 0; } break; } case DECELERATING: { - uint32_t relativePos = devicePrm.relativePos; - uint16_t speed = devicePrm.speed; - uint32_t deceleration = ((uint32_t)devicePrm.deceleration << 16); - if (((devicePrm.commandExecuted == SOFT_STOP_CMD)&&(speed <= devicePrm.minSpeed))|| - ((devicePrm.commandExecuted != RUN_CMD)&& - (relativePos >= devicePrm.stepsToTake))) + 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 ((devicePrm.commandExecuted == RUN_CMD)&& - (speed <= devicePrm.maxSpeed)) + else if ((device_prm.commandExecuted == RUN_CMD)&& + (speed <= device_prm.maxSpeed)) { - devicePrm.motionState = STEADY; + device_prm.motionState = STEADY; } else { /* Go on decelerating */ - if (speed > devicePrm.minSpeed) + if (speed > device_prm.minSpeed) { bool speedUpdated = FALSE; if (speed == 0) speed =1; - devicePrm.accu += deceleration / speed; - while (devicePrm.accu >= (0X10000L)) + device_prm.accu += deceleration / speed; + while (device_prm.accu >= (0X10000L)) { - devicePrm.accu -= (0X10000L); + device_prm.accu -= (0X10000L); if (speed > 1) { speed -=1; @@ -1271,12 +1336,12 @@ if (speedUpdated) { - if (speed < devicePrm.minSpeed) + if (speed < device_prm.minSpeed) { - speed = devicePrm.minSpeed; + speed = device_prm.minSpeed; } - devicePrm.speed = speed; - L6474_ApplySpeed(devicePrm.speed); + device_prm.speed = speed; + L6474_ApplySpeed(device_prm.speed); } } } @@ -1288,28 +1353,47 @@ } } /* Set isr flag */ - isrFlag = FALSE; + 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 mA in compatible values for TVAL register - * @param[in] Tval - * @retval TVAL values + * @brief Converts values from TVAL register to mA + * @param[in] Tval value from TVAL register + * @retval current in mA **********************************************************/ -uint8_t L6474::L6474_Tval_Current_to_Par(double Tval) +float L6474::L6474_Par_to_Tval_Current(float Tval) { - return ((uint8_t)(((Tval - 31.25)/31.25)+0.5)); + return ((float)((Tval - 0.5f) * 31.25f + 31.25f)); } /********************************************************** - * @brief Convert time in us in compatible values - * for TON_MIN register - * @param[in] Tmin - * @retval TON_MIN values + * @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 **********************************************************/ -uint8_t L6474::L6474_Tmin_Time_to_Par(double Tmin) +float L6474::L6474_Tmin_Time_to_Par(float ton_min_us) { - return ((uint8_t)(((Tmin - 0.5)*2)+0.5)); + 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)); } /********************************************************** @@ -1325,9 +1409,9 @@ L6474_ErrorHandler(L6474_ERROR_1); } - if (isrFlag) + if (isr_flag) { - spiPreemtionByIsr = TRUE; + spi_preemtion_by_isr = TRUE; } }