Expansion SW library to control high power stepper motor(s) using IHM03A1 expansion board(s) with Powerstep01 driver.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: IHM03A1_ExampleFor1Motor HelloWorld_IHM03A1 IHM03A1_ExampleFor3Motors KYPHOS_Stepper_Motor_Control
Fork of X_NUCLEO_IHM03A1 by
PowerStep01.h
00001 /** 00002 ****************************************************************************** 00003 * @file PowerStep01.h 00004 * @author IPC Rennes 00005 * @version V1.0.1 00006 * @date September 13th, 2016 00007 * @brief This file contains the class of a Powerstep01 Motor Control component. 00008 * @note (C) COPYRIGHT 2016 STMicroelectronics 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 00040 /* Define to prevent recursive inclusion -------------------------------------*/ 00041 00042 #ifndef __POWERSTEP01_CLASS_H 00043 #define __POWERSTEP01_CLASS_H 00044 00045 00046 /* Includes ------------------------------------------------------------------*/ 00047 00048 /* ACTION 1 ------------------------------------------------------------------* 00049 * Include here platform specific header files. * 00050 *----------------------------------------------------------------------------*/ 00051 #include "mbed.h" 00052 #include "DevSPI.h" 00053 /* ACTION 2 ------------------------------------------------------------------* 00054 * Include here component specific header files. * 00055 *----------------------------------------------------------------------------*/ 00056 #include "PowerStep01_def.h" 00057 /* ACTION 3 ------------------------------------------------------------------* 00058 * Include here interface specific header files. * 00059 * * 00060 * Example: * 00061 * #include "HumiditySensor.h" * 00062 * #include "TemperatureSensor.h" * 00063 *----------------------------------------------------------------------------*/ 00064 #include "StepperMotor.h" 00065 00066 00067 /* Classes -------------------------------------------------------------------*/ 00068 00069 /** 00070 * @brief Class representing a Powerstep01 component. 00071 */ 00072 class PowerStep01 : public StepperMotor 00073 { 00074 public: 00075 00076 /*** Constructor and Destructor Methods ***/ 00077 00078 /** 00079 * @brief Constructor. 00080 * @param flag_irq pin name of the FLAG pin of the component. 00081 * @param busy_irq pin name of the BUSY pin of the component. 00082 * @param standby_reset pin name of the STBY\RST pin of the component. 00083 * @param pwm pin name of the PWM pin of the component. 00084 * @param ssel pin name of the SSEL pin of the SPI device to be used for communication. 00085 * @param spi SPI device to be used for communication. 00086 */ 00087 PowerStep01(PinName flag_irq, PinName busy_irq, PinName standby_reset, PinName pwm, PinName ssel, DevSPI &spi) : StepperMotor(), flag_irq(flag_irq), busy_irq(busy_irq), standby_reset(standby_reset), pwm(pwm), ssel(ssel), dev_spi(spi) 00088 { 00089 /* Checking stackability. */ 00090 if (!(numberOfDevices < MAX_NUMBER_OF_DEVICES)) { 00091 error("Instantiation of the PowerStep01 component failed: it can be stacked up to %d times.\r\n", MAX_NUMBER_OF_DEVICES); 00092 } 00093 00094 /* ACTION 4 ----------------------------------------------------------* 00095 * Initialize here the component's member variables, one variable per * 00096 * line. * 00097 * * 00098 * Example: * 00099 * measure = 0; * 00100 * instance_id = number_of_instances++; * 00101 *--------------------------------------------------------------------*/ 00102 errorHandlerCallback = 0; 00103 deviceInstance = numberOfDevices++; 00104 memset(spiTxBursts, 0, POWERSTEP01_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); 00105 memset(spiRxBursts, 0, POWERSTEP01_CMD_ARG_MAX_NB_BYTES * MAX_NUMBER_OF_DEVICES * sizeof(uint8_t)); 00106 } 00107 00108 /** 00109 * @brief Destructor. 00110 */ 00111 virtual ~PowerStep01(void) {} 00112 00113 00114 /*** Public Component Related Methods ***/ 00115 00116 /* ACTION 5 --------------------------------------------------------------* 00117 * Implement here the component's public methods, as wrappers of the C * 00118 * component's functions. * 00119 * They should be: * 00120 * + Methods with the same name of the C component's virtual table's * 00121 * functions (1); * 00122 * + Methods with the same name of the C component's extended virtual * 00123 * table's functions, if any (2). * 00124 * * 00125 * Example: * 00126 * virtual int get_value(float *pData) //(1) * 00127 * { * 00128 * return COMPONENT_get_value(float *pfData); * 00129 * } * 00130 * * 00131 * virtual int enable_feature(void) //(2) * 00132 * { * 00133 * return COMPONENT_enable_feature(); * 00134 * } * 00135 *------------------------------------------------------------------------*/ 00136 00137 /** 00138 * @brief Public functions inherited from the Component Class 00139 */ 00140 00141 /** 00142 * @brief Initialize the component. 00143 * @param init Pointer to device specific initalization structure. 00144 * @retval "0" in case of success, an error code otherwise. 00145 */ 00146 virtual int init(void *init = NULL) 00147 { 00148 return (int) Powerstep01_Init((void *) init); 00149 } 00150 00151 /** 00152 * @brief Getting the ID of the component. 00153 * @param id Pointer to an allocated variable to store the ID into. 00154 * @retval "0" in case of success, an error code otherwise. 00155 */ 00156 virtual int read_id(uint8_t *id = NULL) 00157 { 00158 return (int) Powerstep01_ReadID((uint8_t *) id); 00159 } 00160 00161 /** 00162 * @brief Public functions inherited from the StepperMotor Class 00163 */ 00164 00165 /** 00166 * @brief Getting the value of the Status Register. 00167 * @param None. 00168 * @retval None. 00169 * @note The Status Register's flags are cleared, contrary to read_status_register(). 00170 */ 00171 virtual unsigned int get_status(void) 00172 { 00173 return (unsigned int) Powerstep01_CmdGetStatus(); 00174 } 00175 00176 /** 00177 * @brief Getting the position. 00178 * @param None. 00179 * @retval The position. 00180 */ 00181 virtual signed int get_position(void) 00182 { 00183 return (signed int)Powerstep01_GetPosition(); 00184 } 00185 00186 /** 00187 * @brief Getting the marked position. 00188 * @param None. 00189 * @retval The marked position. 00190 */ 00191 virtual signed int get_mark(void) 00192 { 00193 return (signed int)Powerstep01_GetMark(); 00194 } 00195 00196 /** 00197 * @brief Getting the current speed in step/s. 00198 * @param None. 00199 * @retval The current speed in step/s. 00200 */ 00201 virtual unsigned int get_speed(void) 00202 { 00203 return (unsigned int)round(Powerstep01_GetAnalogValue(POWERSTEP01_SPEED)); 00204 } 00205 00206 /** 00207 * @brief Getting the maximum speed in step/s. 00208 * @param None. 00209 * @retval The maximum speed in step/s. 00210 */ 00211 virtual unsigned int get_max_speed(void) 00212 { 00213 return (unsigned int)round(Powerstep01_GetAnalogValue(POWERSTEP01_MAX_SPEED)); 00214 } 00215 00216 /** 00217 * @brief Getting the minimum speed in step/s. 00218 * @param None. 00219 * @retval The minimum speed in step/s. 00220 */ 00221 virtual unsigned int get_min_speed(void) 00222 { 00223 return (unsigned int)round(Powerstep01_GetAnalogValue(POWERSTEP01_MIN_SPEED)); 00224 } 00225 00226 /** 00227 * @brief Getting the acceleration in step/s^2. 00228 * @param None. 00229 * @retval The acceleration in step/s^2. 00230 */ 00231 virtual unsigned int get_acceleration(void) 00232 { 00233 return (unsigned int)round(Powerstep01_GetAnalogValue(POWERSTEP01_ACC)); 00234 } 00235 00236 /** 00237 * @brief Getting the deceleration in step/s^2. 00238 * @param None. 00239 * @retval The deceleration in step/s^2. 00240 */ 00241 virtual unsigned int get_deceleration(void) 00242 { 00243 return (unsigned int)round(Powerstep01_GetAnalogValue(POWERSTEP01_DEC)); 00244 } 00245 00246 /** 00247 * @brief Getting the direction of rotation. 00248 * @param None. 00249 * @retval The direction of rotation. 00250 */ 00251 virtual direction_t get_direction(void) 00252 { 00253 if ((POWERSTEP01_STATUS_DIR&Powerstep01_ReadStatusRegister())!=0) 00254 { 00255 return FWD; 00256 } 00257 else 00258 { 00259 return BWD; 00260 } 00261 } 00262 00263 /** 00264 * @brief Setting the current position to be the home position. 00265 * @param None. 00266 * @retval None. 00267 */ 00268 virtual void set_home(void) 00269 { 00270 Powerstep01_SetHome(); 00271 } 00272 00273 /** 00274 * @brief Setting the current position to be the marked position. 00275 * @param None. 00276 * @retval None. 00277 */ 00278 virtual void set_mark(void) 00279 { 00280 Powerstep01_SetMark(); 00281 } 00282 00283 /** 00284 * @brief Setting the maximum speed in steps/s. 00285 * @param speed The maximum speed in steps/s. 00286 * @retval TRUE if value is valid, FALSE otherwise. 00287 */ 00288 virtual bool set_max_speed(unsigned int speed) 00289 { 00290 return Powerstep01_SetAnalogValue(POWERSTEP01_MAX_SPEED, (float)speed); 00291 } 00292 00293 /** 00294 * @brief Setting the minimum speed in steps/s. 00295 * @param speed The minimum speed in steps/s. 00296 * @retval TRUE if value is valid, FALSE otherwise. 00297 */ 00298 virtual bool set_min_speed(unsigned int speed) 00299 { 00300 return Powerstep01_SetAnalogValue(POWERSTEP01_MIN_SPEED, (float)speed); 00301 } 00302 00303 /** 00304 * @brief Setting the acceleration in steps/s^2. 00305 * @param acceleration The acceleration in steps/s^2. 00306 * @retval None. 00307 */ 00308 virtual bool set_acceleration(unsigned int acceleration) 00309 { 00310 return Powerstep01_SetAnalogValue(POWERSTEP01_ACC, (float)acceleration); 00311 } 00312 00313 /** 00314 * @brief Setting the deceleration in steps/s^2. 00315 * @param deceleration The deceleration in steps/s^2. 00316 * @retval None. 00317 */ 00318 virtual bool set_deceleration(unsigned int deceleration) 00319 { 00320 return Powerstep01_SetAnalogValue(POWERSTEP01_DEC, (float)deceleration); 00321 } 00322 00323 /** 00324 * @brief Setting the Step Mode. 00325 * @param step_mode The Step Mode. 00326 * @retval None. 00327 * @note step_mode can be one of the following: 00328 * + STEP_MODE_FULL 00329 * + STEP_MODE_HALF 00330 * + STEP_MODE_1_4 00331 * + STEP_MODE_1_8 00332 * + STEP_MODE_1_16 00333 * + STEP_MODE_1_32 00334 * + STEP_MODE_1_64 00335 * + STEP_MODE_1_128 00336 */ 00337 virtual bool set_step_mode(step_mode_t step_mode) 00338 { 00339 return Powerstep01_SelectStepMode((motorStepMode_t) step_mode); 00340 } 00341 00342 /** 00343 * @brief Going to a specified position. 00344 * @param position The desired position. 00345 * @retval None. 00346 */ 00347 virtual void go_to(signed int position) 00348 { 00349 Powerstep01_CmdGoTo((int32_t)position); 00350 } 00351 00352 virtual void go_to(direction_t direction, signed int position) 00353 { 00354 Powerstep01_CmdGoToDir((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD),(int32_t)position); 00355 } 00356 00357 /** 00358 * @brief Going to the home position. 00359 * @param None. 00360 * @retval None. 00361 */ 00362 virtual void go_home(void) 00363 { 00364 Powerstep01_CmdGoHome(); 00365 } 00366 00367 /** 00368 * @brief Going to the marked position. 00369 * @param None. 00370 * @retval None. 00371 */ 00372 virtual void go_mark(void) 00373 { 00374 Powerstep01_CmdGoMark(); 00375 } 00376 00377 /** 00378 * @brief Running the motor towards a specified direction. 00379 * @param direction The direction of rotation. 00380 * @retval None. 00381 */ 00382 virtual void run(direction_t direction) 00383 { 00384 Powerstep01_CmdRun((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), Powerstep01_CmdGetParam((powerstep01_Registers_t) POWERSTEP01_MAX_SPEED)); 00385 } 00386 00387 /** 00388 * @brief Moving the motor towards a specified direction for a certain number of steps. 00389 * @param direction The direction of rotation. 00390 * @param steps The desired number of steps. 00391 * @retval None. 00392 */ 00393 virtual void move(direction_t direction, unsigned int steps) 00394 { 00395 Powerstep01_CmdMove((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), (uint32_t)steps); 00396 } 00397 00398 /** 00399 * @brief Stopping the motor through an immediate deceleration up to zero speed. 00400 * @param None. 00401 * @retval None. 00402 */ 00403 virtual void soft_stop(void) 00404 { 00405 Powerstep01_CmdSoftStop(); 00406 } 00407 00408 /** 00409 * @brief Stopping the motor through an immediate infinite deceleration. 00410 * @param None. 00411 * @retval None. 00412 */ 00413 virtual void hard_stop(void) 00414 { 00415 Powerstep01_CmdHardStop(); 00416 } 00417 00418 /** 00419 * @brief Disabling the power bridge after performing a deceleration to zero. 00420 * @param None. 00421 * @retval None. 00422 */ 00423 virtual void soft_hiz(void) 00424 { 00425 Powerstep01_CmdSoftHiZ(); 00426 } 00427 00428 /** 00429 * @brief Disabling the power bridge immediately. 00430 * @param None. 00431 * @retval None. 00432 */ 00433 virtual void hard_hiz(void) 00434 { 00435 Powerstep01_CmdHardHiZ(); 00436 } 00437 00438 /** 00439 * @brief Waiting while the motor is active. 00440 * @param None. 00441 * @retval None. 00442 */ 00443 virtual void wait_while_active(void) 00444 { 00445 Powerstep01_WaitWhileActive(); 00446 } 00447 00448 /** 00449 * @brief Public functions NOT inherited 00450 */ 00451 00452 /** 00453 * @brief Attaching an error handler. 00454 * @param fptr An error handler. 00455 * @retval None. 00456 */ 00457 virtual void attach_error_handler(void (*fptr)(uint16_t error)) 00458 { 00459 Powerstep01_AttachErrorHandler((void (*)(uint16_t error)) fptr); 00460 } 00461 00462 /** 00463 * @brief Checks if the device is busy by reading the busy pin position. 00464 * @param None. 00465 * @retval One if the device his busy (low logic level on busy output), 00466 * otherwise zero 00467 */ 00468 virtual int check_busy_hw(void) 00469 { 00470 if (busy_irq!=0) { 00471 return 0x01; 00472 } else { 00473 return 0x00; 00474 } 00475 } 00476 00477 /** 00478 * @brief Checks if the device has an alarm flag set by reading the flag pin position. 00479 * @param None. 00480 * @retval One if the device has an alarm flag set (low logic level on flag output), 00481 * otherwise zero 00482 */ 00483 virtual unsigned int check_status_hw(void) 00484 { 00485 if (flag_irq!=0) { 00486 return 0x01; 00487 } else { 00488 return 0x00; 00489 } 00490 } 00491 00492 /** 00493 * @brief Fetch and clear status flags of all devices 00494 * by issuing a GET_STATUS command simultaneously 00495 * to all devices. 00496 * Then, the fetched status of each device can be retrieved 00497 * by using the Powerstep01_GetFetchedStatus function 00498 * provided there is no other calls to functions which 00499 * use the SPI in between. 00500 * @retval None 00501 */ 00502 virtual void fetch_and_clear_all_status(void) 00503 { 00504 Powerstep01_FetchAndClearAllStatus(); 00505 } 00506 00507 /** 00508 * @brief Getting a parameter float value. 00509 * @param parameter A parameter's register adress. 00510 * @retval The parameter's float value. 00511 * parameter can be one of the following: 00512 * + POWERSTEP01_ABS_POS 00513 * + POWERSTEP01_MARK 00514 * + POWERSTEP01_ACC 00515 * + POWERSTEP01_DEC 00516 * + POWERSTEP01_SPEED 00517 * + POWERSTEP01_MAX_SPEED 00518 * + POWERSTEP01_MIN_SPEED 00519 * + POWERSTEP01_FS_SPD 00520 * (voltage mode) + POWERSTEP01_INT_SPD 00521 * (voltage mode) + POWERSTEP01_K_THERM 00522 * + POWERSTEP01_OCD_TH 00523 * (voltage mode) + POWERSTEP01_STALL_TH 00524 * (voltage mode) + POWERSTEP01_KVAL_HOLD : value in % 00525 * (current mode) + POWERSTEP01_TVAL_HOLD : value in mV 00526 * (voltage mode) + POWERSTEP01_KVAL_RUN : value in % 00527 * (current mode) + POWERSTEP01_TVAL_RUN : value in mV 00528 * (voltage mode) + POWERSTEP01_KVAL_ACC : value in % 00529 * (current mode) + POWERSTEP01_TVAL_ACC : value in mV 00530 * (voltage mode) + POWERSTEP01_KVAL_DEC : value in % 00531 * (current mode) + POWERSTEP01_TVAL_DEC : value in mV 00532 * (voltage mode) + POWERSTEP01_ST_SLP 00533 * (voltage mode) + POWERSTEP01_FN_SLP_ACC 00534 * (voltage mode) + POWERSTEP01_FN_SLP_DEC 00535 */ 00536 virtual float get_analog_value(unsigned int parameter) 00537 { 00538 return Powerstep01_GetAnalogValue((powerstep01_Registers_t)parameter); 00539 } 00540 00541 /** 00542 * @brief Get the value of the STATUS register which was 00543 * fetched by using Powerstep01_FetchAndClearAllStatus. 00544 * The fetched values are available as long as there 00545 * no other calls to functions which use the SPI. 00546 * @retval Last fetched value of the STATUS register. 00547 */ 00548 virtual uint16_t get_fetched_status(void) 00549 { 00550 return Powerstep01_GetFetchedStatus(); 00551 } 00552 00553 /** 00554 * @brief Getting the version of the firmware. 00555 * @param None. 00556 * @retval The version of the firmware. 00557 */ 00558 virtual unsigned int get_fw_version(void) 00559 { 00560 return (unsigned int) Powerstep01_GetFwVersion(); 00561 } 00562 00563 /** 00564 * @brief Getting a parameter register value. 00565 * @param parameter A parameter's register adress. 00566 * @retval The parameter's register value. 00567 * parameter can be one of the following: 00568 * + POWERSTEP01_ABS_POS 00569 * + POWERSTEP01_EL_POS 00570 * + POWERSTEP01_MARK 00571 * + POWERSTEP01_SPEED 00572 * + POWERSTEP01_ACC 00573 * + POWERSTEP01_DEC 00574 * + POWERSTEP01_MAX_SPEED 00575 * + POWERSTEP01_MIN_SPEED 00576 * (voltage mode) + POWERSTEP01_KVAL_HOLD : value in % 00577 * (current mode) + POWERSTEP01_TVAL_HOLD : value in mV 00578 * (voltage mode) + POWERSTEP01_KVAL_RUN : value in % 00579 * (current mode) + POWERSTEP01_TVAL_RUN : value in mV 00580 * (voltage mode) + POWERSTEP01_KVAL_ACC : value in % 00581 * (current mode) + POWERSTEP01_TVAL_ACC : value in mV 00582 * (voltage mode) + POWERSTEP01_KVAL_DEC : value in % 00583 * (current mode) + POWERSTEP01_TVAL_DEC : value in mV 00584 * (voltage mode) + POWERSTEP01_INT_SPD 00585 * (voltage mode) + POWERSTEP01_ST_SLP 00586 * (current mode) + POWERSTEP01_T_FAST 00587 * (voltage mode) + POWERSTEP01_FN_SLP_ACC 00588 * (current mode) + POWERSTEP01_TON_MIN 00589 * (voltage mode) + POWERSTEP01_FN_SLP_DEC 00590 * (current mode) + POWERSTEP01_TOFF_MIN 00591 * (voltage mode) + POWERSTEP01_K_THERM 00592 * + POWERSTEP01_ADC_OUT 00593 * + POWERSTEP01_OCD_TH 00594 * (voltage mode) + POWERSTEP01_STALL_TH 00595 * + POWERSTEP01_FS_SPD 00596 * + POWERSTEP01_STEP_MODE 00597 * + POWERSTEP01_ALARM_EN 00598 * + POWERSTEP01_GATECFG1 00599 * + POWERSTEP01_GATECFG2 00600 * + POWERSTEP01_CONFIG 00601 * + POWERSTEP01_STATUS 00602 */ 00603 virtual unsigned int get_raw_parameter(unsigned int parameter) 00604 { 00605 return (unsigned int) Powerstep01_CmdGetParam((powerstep01_Registers_t)parameter); 00606 } 00607 00608 /** 00609 * @brief Issues PowerStep01 Go Until command. 00610 * @param action type of action to undertake when the SW 00611 * input is forced high (ACTION_RESET or ACTION_COPY). 00612 * @param direction The direction of rotation. 00613 * @param speed in steps/s. 00614 * @retval One if the device has an alarm flag set (low logic level on flag output), 00615 * otherwise zero 00616 */ 00617 virtual void go_until(motorAction_t action, direction_t direction, float speed) 00618 { 00619 Powerstep01_CmdGoUntil(action, 00620 (motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), 00621 speed_steps_s_to_reg_val(speed)); 00622 } 00623 00624 /** 00625 * @brief Checks if the device is busy 00626 * by reading the Busy flag bit ot its status Register 00627 * This operation clears the status register 00628 * @retval true if device is busy, false zero 00629 */ 00630 virtual bool is_device_busy(void) 00631 { 00632 return Powerstep01_IsDeviceBusy(); 00633 } 00634 00635 /** 00636 * @brief Put commands in queue before synchronous sending 00637 * done by calling send_queued_commands. 00638 * Any call to functions that use the SPI between the calls of 00639 * queue_commands and send_queued_commands 00640 * will corrupt the queue. 00641 * A command for each device of the daisy chain must be 00642 * specified before calling send_queued_commands. 00643 * @param command Command to queue (all Powerstep01 commmands 00644 * except POWERSTEP01_SET_PARAM, POWERSTEP01_GET_PARAM, 00645 * POWERSTEP01_GET_STATUS). 00646 * @param value argument of the command to queue. 00647 * @retval None. 00648 */ 00649 virtual void queue_commands(uint8_t command, int32_t value) 00650 { 00651 Powerstep01_QueueCommands(command, value); 00652 } 00653 00654 /** 00655 * @brief Reading the Status Register. 00656 * @param None. 00657 * @retval None. 00658 * @note The Status Register's flags are not cleared, contrary to get_status(). 00659 */ 00660 virtual uint16_t read_status_register(void) 00661 { 00662 return Powerstep01_ReadStatusRegister(); 00663 } 00664 00665 /** 00666 * @brief Issues PowerStep01 Release SW command. 00667 * @param action type of action to undertake when the SW 00668 * input is forced high (ACTION_RESET or ACTION_COPY). 00669 * @param direction The direction of rotation. 00670 * @param speed in steps/s. 00671 * @retval One if the device has an alarm flag set (low logic level on flag output), 00672 * otherwise zero 00673 */ 00674 virtual void release_sw(motorAction_t action, direction_t direction) 00675 { 00676 Powerstep01_CmdReleaseSw(action, 00677 (motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); 00678 } 00679 00680 /** 00681 * @brief Issues PowerStep01 Reset Device command. 00682 * @param None. 00683 * @retval None. 00684 */ 00685 virtual void reset_command(void) 00686 { 00687 Powerstep01_CmdResetDevice(); 00688 } 00689 00690 /** 00691 * @brief Issues PowerStep01 ResetPos command. 00692 * @param None. 00693 * @retval None. 00694 * @note Same effect as set_home(). 00695 */ 00696 virtual void reset_position(void) 00697 { 00698 Powerstep01_CmdResetPos(); 00699 } 00700 00701 /** 00702 * @brief Running the motor towards a specified direction. 00703 * @param direction The direction of rotation. 00704 * @param speed in steps/s. 00705 * @retval None. 00706 */ 00707 virtual void run(direction_t direction, float speed) 00708 { 00709 Powerstep01_CmdRun((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD), speed_steps_s_to_reg_val(speed)); 00710 } 00711 00712 /** 00713 * @brief Sends commands stored previously in the queue by queue_commands. 00714 * @param None. 00715 * @retval None. 00716 */ 00717 virtual void send_queued_commands(void) 00718 { 00719 Powerstep01_SendQueuedCommands(); 00720 } 00721 00722 /** 00723 * @brief Setting a parameter with an input float value. 00724 * @param param Register adress. 00725 * @param value Float value to convert and set into the register. 00726 * @retval TRUE if param and value are valid, FALSE otherwise 00727 * @note parameter can be one of the following: 00728 * + POWERSTEP01_EL_POS 00729 * + POWERSTEP01_ABS_POS 00730 * + POWERSTEP01_MARK 00731 * + POWERSTEP01_ACC 00732 * + POWERSTEP01_DEC 00733 * + POWERSTEP01_MAX_SPEED 00734 * + POWERSTEP01_MIN_SPEED 00735 * + POWERSTEP01_FS_SPD 00736 * + POWERSTEP01_INT_SPD 00737 * + POWERSTEP01_K_THERM 00738 * + POWERSTEP01_OCD_TH 00739 * + POWERSTEP01_STALL_TH 00740 * + POWERSTEP01_KVAL_HOLD 00741 * + POWERSTEP01_KVAL_RUN 00742 * + POWERSTEP01_KVAL_ACC 00743 * + POWERSTEP01_KVAL_DEC 00744 * + POWERSTEP01_ST_SLP 00745 * + POWERSTEP01_FN_SLP_ACC 00746 * + POWERSTEP01_FN_SLP_DEC 00747 * + POWERSTEP01_TVAL_HOLD 00748 * + POWERSTEP01_TVAL_RUN 00749 * + POWERSTEP01_TVAL_ACC 00750 * + POWERSTEP01_TVAL_DEC 00751 * + POWERSTEP01_TON_MIN 00752 * + POWERSTEP01_TOFF_MIN 00753 */ 00754 virtual bool set_analog_value(unsigned int param, float value) 00755 { 00756 return Powerstep01_SetAnalogValue((powerstep01_Registers_t)param, value); 00757 } 00758 00759 /** 00760 * @brief Setting a parameter. 00761 * @param parameter A parameter's register adress. 00762 * @param value The parameter's value. 00763 * @retval None. 00764 * parameter can be one of the following: 00765 * + POWERSTEP01_ABS_POS 00766 * + POWERSTEP01_EL_POS 00767 * + POWERSTEP01_MARK 00768 * + POWERSTEP01_ACC 00769 * + POWERSTEP01_DEC 00770 * + POWERSTEP01_MAX_SPEED 00771 * + POWERSTEP01_MIN_SPEED 00772 * (voltage mode) + POWERSTEP01_KVAL_HOLD : value in % 00773 * (current mode) + POWERSTEP01_TVAL_HOLD : value in mV 00774 * (voltage mode) + POWERSTEP01_KVAL_RUN : value in % 00775 * (current mode) + POWERSTEP01_TVAL_RUN : value in mV 00776 * (voltage mode) + POWERSTEP01_KVAL_ACC : value in % 00777 * (current mode) + POWERSTEP01_TVAL_ACC : value in mV 00778 * (voltage mode) + POWERSTEP01_KVAL_DEC : value in % 00779 * (current mode) + POWERSTEP01_TVAL_DEC : value in mV 00780 * (voltage mode) + POWERSTEP01_INT_SPD 00781 * (voltage mode) + POWERSTEP01_ST_SLP 00782 * (current mode) + POWERSTEP01_T_FAST 00783 * (voltage mode) + POWERSTEP01_FN_SLP_ACC 00784 * (current mode) + POWERSTEP01_TON_MIN 00785 * (voltage mode) + POWERSTEP01_FN_SLP_DEC 00786 * (current mode) + POWERSTEP01_TOFF_MIN 00787 * (voltage mode) + POWERSTEP01_K_THERM 00788 * + POWERSTEP01_ADC_OUT 00789 * + POWERSTEP01_OCD_TH 00790 * (voltage mode) + POWERSTEP01_STALL_TH 00791 * + POWERSTEP01_FS_SPD 00792 * + POWERSTEP01_STEP_MODE 00793 * + POWERSTEP01_ALARM_EN 00794 * + POWERSTEP01_GATECFG1 00795 * + POWERSTEP01_GATECFG2 00796 * + POWERSTEP01_CONFIG 00797 */ 00798 virtual void set_raw_parameter(unsigned int parameter, unsigned int value) 00799 { 00800 Powerstep01_CmdSetParam((powerstep01_Registers_t)parameter, (uint32_t)value); 00801 } 00802 00803 /** 00804 * @brief Enable the step clock mode. 00805 * @param frequency the frequency of PWM. 00806 * @retval None. 00807 */ 00808 virtual void step_clock_mode_enable(direction_t direction) 00809 { 00810 Powerstep01_CmdStepClock((motorDir_t) (direction == StepperMotor::FWD ? FORWARD : BACKWARD)); 00811 } 00812 00813 /** 00814 * @brief Setting the frequency of PWM. 00815 * The frequency controls directly the speed of the device. 00816 * @param frequency the frequency of PWM. 00817 * @retval None. 00818 */ 00819 virtual void step_clock_start(uint16_t frequency) 00820 { 00821 /* Computing the period of PWM. */ 00822 double period = 1.0f / frequency; 00823 00824 /* Setting the period and the duty-cycle of PWM. */ 00825 pwm.period(period); 00826 pwm.write(0.5f); 00827 } 00828 00829 /** 00830 * @brief Stopping the PWM. 00831 * @param None. 00832 * @retval None. 00833 */ 00834 virtual void step_clock_stop(void) 00835 { 00836 pwm.write(0.0f); 00837 } 00838 00839 /** 00840 * @brief Public static functions 00841 */ 00842 00843 static uint8_t get_nb_devices(void) 00844 { 00845 return numberOfDevices; 00846 } 00847 00848 /** 00849 * @brief To and from register parameter conversion functions 00850 */ 00851 00852 /********************************************************** 00853 * @brief Convert the float formatted acceleration or 00854 * deceleration into respectively an ACC or DEC register value 00855 * @param[in] steps_s2 the acceleration or deceleration as 00856 * steps/s^2, range 14.55 to 59590 steps/s^2 00857 * @retval The acceleration or deceleration as steps/tick^2 00858 **********************************************************/ 00859 static uint16_t acc_dec_steps_s2_to_reg_val(float steps_s2) 00860 { 00861 return ((uint16_t)(((float)(steps_s2)*0.068719476736f)+0.5f)); 00862 } 00863 00864 /********************************************************** 00865 * @brief Convert the ACC or DEC register value into step/s^2 00866 * @param[in] regVal The ACC or DEC register value 00867 * @retval The speed as steps/s 00868 **********************************************************/ 00869 static float acc_dec_reg_val_to_steps_s2(uint32_t regVal) 00870 { 00871 return (((float)(regVal))*14.5519152283f); 00872 } 00873 00874 /********************************************************** 00875 * @brief Converts BEMF compensation slope to values for ST_SLP, 00876 * FN_SLP_ACC or FN_SLP_DEC register 00877 * @param[in] percentage BEMF compensation slope percentage, 00878 * range 0 to 0.4% (0.004) s/step 00879 * @retval value for ST_SLP, FN_SLP_ACC or FN_SLP_DEC register 00880 **********************************************************/ 00881 static uint8_t bemf_slope_perc_to_reg_val(float percentage) 00882 { 00883 return ((uint8_t)(((float)(percentage)*637.5f)+0.5f)); 00884 } 00885 00886 /********************************************************** 00887 * @brief Converts values from ST_SLP, FN_SLP_ACC or 00888 * FN_SLP_DEC register to BEMF compensation slope percentage 00889 * @param[in] regVal The ST_SLP, FN_SLP_ACC or FN_SLP_DEC 00890 * register value 00891 * @retval BEMF compensation slope percentage 00892 **********************************************************/ 00893 static float bemf_slope_reg_val_to_perc(uint32_t regVal) 00894 { 00895 return (((float)(regVal))*0.00156862745098f); 00896 } 00897 00898 /********************************************************** 00899 * @brief Convert the float formatted speed into a FS_SPD 00900 * register value 00901 * @param[in] steps_s the speed as steps/s, range 15.25 to 15610 steps/s 00902 * @retval The speed as steps/tick 00903 **********************************************************/ 00904 static uint16_t fs_spd_steps_s_to_reg_val(float steps_s) 00905 { 00906 return ((uint16_t)((float)(steps_s)*0.065536f)); 00907 } 00908 00909 /********************************************************** 00910 * @brief Convert the FS_SPD register value into step/s 00911 * @param[in] regVal The FS_SPD register value 00912 * @retval The full Step speed as steps/s 00913 **********************************************************/ 00914 static float fs_spd_reg_val_to_steps_s(uint32_t regVal) 00915 { 00916 return (((float)regVal+0.999f)*15.258789f); 00917 } 00918 00919 /********************************************************** 00920 * @brief Convert the float formatted speed into a INT_SPEED 00921 * register value 00922 * @param[in] steps_s the speed as steps/s, range 0 to 976.5 steps/s 00923 * @retval The intersect speed as steps/tick 00924 **********************************************************/ 00925 static uint16_t int_spd_steps_s_to_reg_val(float steps_s) 00926 { 00927 return ((uint16_t)(((float)(steps_s)*16.777216f)+0.5f)); 00928 } 00929 00930 /********************************************************** 00931 * @brief Convert the INT_SPEED register value into step/s 00932 * @param[in] regVal The INT_SPEED register value 00933 * @retval The speed as steps/s 00934 **********************************************************/ 00935 static float int_spd_reg_val_to_steps_s(uint32_t regVal) 00936 { 00937 return (((float)(regVal))*0.0596045f); 00938 } 00939 00940 /********************************************************** 00941 * @brief Convert the float formatted thermal compensation 00942 * factor into a K_THERM register value 00943 * @param[in] compFactor the float formatted thermal 00944 * compensation factor, range 1 to 1.46875 00945 * @retval value for K_THERM register 00946 **********************************************************/ 00947 static uint8_t k_therm_comp_to_reg_val(float compFactor) 00948 { 00949 return ((uint8_t)((((float)(compFactor)-1.0f)*32.0f)+0.5f)); 00950 } 00951 00952 /********************************************************** 00953 * @brief Convert the K_THERM register value into a float 00954 * formatted thermal compensation factor 00955 * @param[in] regVal The K_THERM register value 00956 * @retval The float formatted thermal compensation factor 00957 **********************************************************/ 00958 static float k_therm_reg_val_to_comp(uint32_t regVal) 00959 { 00960 return (((float)(regVal))*0.03125f+1); 00961 } 00962 00963 /********************************************************** 00964 * @brief Converts voltage in percentage to values for KVAL_RUN, 00965 * KVAL_HOLD, KVAL_ACC or KVAL_DEC register 00966 * @param[in] percentage percentage of the power supply voltage 00967 * applied to the motor windings, range 0.4% to 99.6% 00968 * @retval value for KVAL_RUN, KVAL_HOLD, KVAL_ACC or 00969 * KVAL_DEC register 00970 * @note The voltage applied is sinusoidal 00971 **********************************************************/ 00972 static uint8_t k_val_perc_to_reg_val(float percentage) 00973 { 00974 return ((uint8_t)(((float)(percentage)*2.56f)+0.5f)); 00975 } 00976 00977 /********************************************************** 00978 * @brief Converts values from KVAL_RUN, KVAL_HOLD, KVAL_ACC 00979 * or KVAL_DEC register to percentage 00980 * @param[in] regVal The KVAL_RUN, KVAL_HOLD, KVAL_ACC 00981 * or KVAL_DEC register value 00982 * @retval percentage of the power supply voltage applied to 00983 * the motor windings 00984 * @note The voltage applied is sinusoidal 00985 **********************************************************/ 00986 static float k_val_reg_val_to_perc(uint32_t regVal) 00987 { 00988 return (((float)(regVal))*0.390625f); 00989 } 00990 00991 /********************************************************** 00992 * @brief Convert the float formatted speed into a MAX_SPEED 00993 * register value 00994 * @param[in] steps_s the speed as steps/s, range 15.25 to 15610 steps/s 00995 * @retval The speed as steps/tick 00996 **********************************************************/ 00997 static uint16_t max_spd_steps_s_to_reg_val(float steps_s) 00998 { 00999 return ((uint16_t)(((float)(steps_s)*0.065536f)+0.5f)); 01000 } 01001 01002 /********************************************************** 01003 * @brief Convert the MAX_SPEED register value into step/s 01004 * @param[in] regVal The MAX_SPEED register value 01005 * @retval The speed as steps/s 01006 **********************************************************/ 01007 static float max_spd_reg_val_to_steps_s(uint32_t regVal) 01008 { 01009 return (((float)(regVal))*15.258789f); 01010 } 01011 01012 /********************************************************** 01013 * @brief Convert the float formatted speed into a MIN_SPEED 01014 * register value 01015 * @param[in] steps_s the speed as steps/s, range 0 to 976.3 steps/s 01016 * @retval The speed as steps/tick 01017 **********************************************************/ 01018 static uint16_t min_spd_steps_s_to_reg_val(float steps_s) 01019 { 01020 return ((uint16_t)(((float)(steps_s)*4.194304f)+0.5f)); 01021 } 01022 01023 /********************************************************** 01024 * @brief Convert the MIN_SPEED register value into step/s 01025 * @param[in] regVal The MIN_SPEED register value 01026 * @retval The speed as steps/s 01027 **********************************************************/ 01028 static float min_spd_reg_val_to_steps_s(uint32_t regVal) 01029 { 01030 return (((float)(regVal))*0.238418579f); 01031 } 01032 01033 /********************************************************** 01034 * @brief Convert the float formatted speed into a SPEED 01035 * register value 01036 * @param[in] steps_s the speed as steps/s, range 0 to 15625 steps/s 01037 * @retval The speed as steps/tick 01038 **********************************************************/ 01039 static uint32_t speed_steps_s_to_reg_val(float steps_s) 01040 { 01041 return ((uint32_t)(((float)(steps_s)*67.108864f)+0.5f)); 01042 } 01043 01044 /********************************************************** 01045 * @brief Convert the SPEED register value into step/s 01046 * @param[in] regVal The SPEED register value 01047 * @retval The speed as steps/s 01048 **********************************************************/ 01049 static float speed_reg_val_to_steps_s(uint32_t regVal) 01050 { 01051 return (((float)(regVal))*0.01490116119f); 01052 } 01053 01054 /********************************************************** 01055 * @brief Converts STALL or OCD Threshold voltage in mV to 01056 * values for STALL_TH or OCD_TH register 01057 * @param[in] mV voltage in mV, range 31.25mV to 1000mV 01058 * @retval value for STALL_TH or OCD_TH register 01059 **********************************************************/ 01060 static uint8_t stall_ocd_th_to_reg_val(float mV) 01061 { 01062 return ((uint8_t)((((float)(mV)-31.25f)*0.032f)+0.5f)); 01063 } 01064 01065 /********************************************************** 01066 * @brief Converts values from STALL_TH or OCD_TH register 01067 * to mV 01068 * @param[in] regVal The STALL_TH or OCD_TH register value 01069 * @retval voltage in mV 01070 **********************************************************/ 01071 static float stall_ocd_reg_val_to_th(uint32_t regVal) 01072 { 01073 return (((float)(regVal+1))*31.25f); 01074 } 01075 01076 /********************************************************** 01077 * @brief Converts voltage in mV to values for TVAL_RUN, 01078 * TVAL_HOLD, TVAL_ACC or TVAL_DEC register 01079 * @param[in] voltage_mV voltage in mV, range 7.8mV to 1000mV 01080 * @retval value for TVAL_RUN, TVAL_HOLD, TVAL_ACC or 01081 * TVAL_DEC register 01082 * @note The voltage corresponds to a peak output current 01083 * accross the external sense power resistor 01084 **********************************************************/ 01085 static uint8_t t_val_ref_voltage_to_reg_val(float voltage_mV) 01086 { 01087 return ((uint8_t)((((float)(voltage_mV)-7.8125f)*0.128f)+0.5f)); 01088 } 01089 01090 /********************************************************** 01091 * @brief Converts values from TVAL_RUN, TVAL_HOLD, TVAL_ACC 01092 * or TVAL_DEC register to mV 01093 * @param[in] regVal The TVAL_RUN, TVAL_HOLD, TVAL_ACC 01094 * or TVAL_DEC register value 01095 * @retval voltage in mV 01096 * @note The voltage corresponds to a peak output current 01097 * accross the external sense power resistor 01098 **********************************************************/ 01099 static float t_val_reg_val_to_ref_voltage(uint32_t regVal) 01100 { 01101 return (((float)(regVal+1))*7.8125f); 01102 } 01103 01104 /********************************************************** 01105 * @brief Convert time in us to values for TON_MIN register 01106 * @param[in] tmin_us time in us, range 0.5us to 64us 01107 * @retval value for TON_MIN register 01108 **********************************************************/ 01109 static uint8_t t_min_time_to_reg_val(float tmin_us) 01110 { 01111 return ((uint8_t)((((float)(tmin_us)-0.5f)*2.0f)+0.5f)); 01112 } 01113 01114 /********************************************************** 01115 * @brief Convert values for TON_MIN or TOFF_MIN register to time in us 01116 * @param[in] regVal The TON_MIN or TOFF_MIN register value 01117 * @retval time in us 01118 **********************************************************/ 01119 static float t_min_reg_val_to_time(uint32_t regVal) 01120 { 01121 return (((float)(regVal+1))*0.5f); 01122 } 01123 01124 /*** Public Interrupt Related Methods ***/ 01125 01126 /* ACTION 6 --------------------------------------------------------------* 01127 * Implement here interrupt related methods, if any. * 01128 * Note that interrupt handling is platform dependent, e.g.: * 01129 * + mbed: * 01130 * InterruptIn feature_irq(pin); //Interrupt object. * 01131 * feature_irq.rise(callback); //Attach a callback. * 01132 * feature_irq.mode(PullNone); //Set interrupt mode. * 01133 * feature_irq.enable_irq(); //Enable interrupt. * 01134 * feature_irq.disable_irq(); //Disable interrupt. * 01135 * + Arduino: * 01136 * attachInterrupt(pin, callback, RISING); //Attach a callback. * 01137 * detachInterrupt(pin); //Detach a callback. * 01138 * * 01139 * Example (mbed): * 01140 * void attach_feature_irq(void (*fptr) (void)) * 01141 * { * 01142 * feature_irq.rise(fptr); * 01143 * } * 01144 * * 01145 * void enable_feature_irq(void) * 01146 * { * 01147 * feature_irq.enable_irq(); * 01148 * } * 01149 * * 01150 * void disable_feature_irq(void) * 01151 * { * 01152 * feature_irq.disable_irq(); * 01153 * } * 01154 *------------------------------------------------------------------------*/ 01155 /** 01156 * @brief Attaching an interrupt handler to the FLAG interrupt. 01157 * @param fptr An interrupt handler. 01158 * @retval None. 01159 */ 01160 void attach_flag_irq(void (*fptr)(void)) 01161 { 01162 flag_irq.fall(fptr); 01163 } 01164 01165 /** 01166 * @brief Enabling the FLAG interrupt handling. 01167 * @param None. 01168 * @retval None. 01169 */ 01170 void enable_flag_irq(void) 01171 { 01172 flag_irq.enable_irq(); 01173 } 01174 01175 /** 01176 * @brief Disabling the FLAG interrupt handling. 01177 * @param None. 01178 * @retval None. 01179 */ 01180 void disable_flag_irq(void) 01181 { 01182 flag_irq.disable_irq(); 01183 } 01184 01185 /** 01186 * @brief Attaching an interrupt handler to the BUSY interrupt. 01187 * @param fptr An interrupt handler. 01188 * @retval None. 01189 */ 01190 void attach_busy_irq(void (*fptr)(void)) 01191 { 01192 busy_irq.fall(fptr); 01193 } 01194 01195 /** 01196 * @brief Enabling the BUSY interrupt handling. 01197 * @param None. 01198 * @retval None. 01199 */ 01200 void enable_busy_irq(void) 01201 { 01202 busy_irq.enable_irq(); 01203 } 01204 01205 /** 01206 * @brief Disabling the BUSY interrupt handling. 01207 * @param None. 01208 * @retval None. 01209 */ 01210 void disable_busy_irq(void) 01211 { 01212 busy_irq.disable_irq(); 01213 } 01214 01215 protected: 01216 01217 /*** Protected Component Related Methods ***/ 01218 01219 /* ACTION 7 --------------------------------------------------------------* 01220 * Declare here the component's specific methods. * 01221 * They should be: * 01222 * + Methods with the same name of the C component's virtual table's * 01223 * functions (1); * 01224 * + Methods with the same name of the C component's extended virtual * 01225 * table's functions, if any (2); * 01226 * + Helper methods, if any, like functions declared in the component's * 01227 * source files but not pointed by the component's virtual table (3). * 01228 * * 01229 * Example: * 01230 * status_t COMPONENT_get_value(float *f); //(1) * 01231 * status_t COMPONENT_enable_feature(void); //(2) * 01232 * status_t COMPONENT_compute_average(void); //(3) * 01233 *------------------------------------------------------------------------*/ 01234 status_t Powerstep01_Init(void *init); 01235 status_t Powerstep01_ReadID(uint8_t *id); 01236 void Powerstep01_AttachErrorHandler(void (*callback)(uint16_t error)); 01237 uint8_t Powerstep01_CheckBusyHw(void); 01238 uint8_t Powerstep01_CheckStatusHw(void); 01239 uint16_t Powerstep01_CmdGetStatus(void); 01240 void Powerstep01_CmdGoHome(void); 01241 void Powerstep01_CmdGoMark(void); 01242 void Powerstep01_CmdGoTo(int32_t targetPosition); 01243 void Powerstep01_CmdGoToDir(motorDir_t direction, int32_t targetPosition); 01244 void Powerstep01_CmdGoUntil(motorAction_t action, motorDir_t direction, uint32_t speed); 01245 void Powerstep01_CmdHardHiZ(void); 01246 void Powerstep01_CmdHardStop(void); 01247 void Powerstep01_CmdMove(motorDir_t direction, uint32_t stepCount); 01248 void Powerstep01_CmdNop(void); 01249 void Powerstep01_CmdReleaseSw(motorAction_t action, motorDir_t direction); 01250 void Powerstep01_CmdResetDevice(void); 01251 void Powerstep01_CmdResetPos(void); 01252 void Powerstep01_CmdRun(motorDir_t direction, uint32_t speed); 01253 void Powerstep01_CmdSoftHiZ(void); 01254 void Powerstep01_CmdSoftStop(void); 01255 void Powerstep01_CmdStepClock(motorDir_t direction); 01256 void Powerstep01_ErrorHandler(uint16_t error); 01257 void Powerstep01_FetchAndClearAllStatus(void); 01258 uint16_t Powerstep01_GetFetchedStatus(void); 01259 uint32_t Powerstep01_GetFwVersion(void); 01260 int32_t Powerstep01_GetMark(void); 01261 int32_t Powerstep01_GetPosition(void); 01262 bool Powerstep01_IsDeviceBusy(void); 01263 uint16_t Powerstep01_ReadStatusRegister(void); 01264 bool Powerstep01_SelectStepMode(motorStepMode_t stepMode); 01265 void Powerstep01_SetHome(void); 01266 void Powerstep01_SetMark(void); 01267 void Powerstep01_WaitForAllDevicesNotBusy(void); 01268 void Powerstep01_WaitWhileActive(void); 01269 01270 /** 01271 * @brief To and from register parameter conversion functions 01272 */ 01273 int32_t Powerstep01_ConvertPosition(uint32_t abs_position_reg); 01274 01275 /** 01276 * @brief Functions to initialize the registers 01277 */ 01278 void Powerstep01_SetDeviceParamsToGivenValues(powerstep01_init_u_t *initPrm); 01279 void Powerstep01_SetRegisterToPredefinedValues(void); 01280 01281 /** 01282 * @brief Functions to get and set parameters using digital or analog values 01283 */ 01284 uint32_t Powerstep01_CmdGetParam(powerstep01_Registers_t param); 01285 void Powerstep01_CmdSetParam(powerstep01_Registers_t param, uint32_t value); 01286 float Powerstep01_GetAnalogValue(powerstep01_Registers_t param); 01287 void Powerstep01_QueueCommands(uint8_t command, int32_t value); 01288 void Powerstep01_SendCommand(uint8_t command, uint32_t value); 01289 void Powerstep01_SendQueuedCommands(void); 01290 bool Powerstep01_SetAnalogValue(powerstep01_Registers_t param, float value); 01291 void Powerstep01_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte); 01292 01293 /** 01294 * @brief Rounding a floating point number to the nearest unsigned integer number. 01295 * @param f The floating point number. 01296 * @retval The nearest unsigned integer number. 01297 */ 01298 int round(float f) 01299 { 01300 if (f >= 0) { 01301 return (int) f + (f - (int) f < 0.5f ? 0 : 1); 01302 } else { 01303 return (int) f - (f - (int) f < -0.5f ? 1 : 0); 01304 } 01305 } 01306 01307 /*** Component's I/O Methods ***/ 01308 01309 /** 01310 * @brief Utility function to read data from Powerstep01. 01311 * @param[out] pBuffer pointer to the buffer to read data into. 01312 * @param NumBytesToRead number of bytes to read. 01313 * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. 01314 */ 01315 status_t Read(uint8_t* pBuffer, uint16_t NumBytesToRead) 01316 { 01317 if (dev_spi.spi_read(pBuffer, ssel, NumBytesToRead) != 0) { 01318 return COMPONENT_ERROR; 01319 } 01320 return COMPONENT_OK; 01321 } 01322 01323 /** 01324 * @brief Utility function to write data to Powerstep01. 01325 * @param pBuffer pointer to the buffer of data to send. 01326 * @param NumBytesToWrite number of bytes to write. 01327 * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. 01328 */ 01329 status_t Write(uint8_t* pBuffer, uint16_t NumBytesToWrite) 01330 { 01331 if (dev_spi.spi_write(pBuffer, ssel, NumBytesToWrite) != 0) { 01332 return COMPONENT_ERROR; 01333 } 01334 return COMPONENT_OK; 01335 } 01336 01337 /** 01338 * @brief Utility function to read and write data from/to Powerstep01 at the same time. 01339 * @param[out] pBufferToRead pointer to the buffer to read data into. 01340 * @param pBufferToWrite pointer to the buffer of data to send. 01341 * @param NumBytes number of bytes to read and write. 01342 * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise. 01343 */ 01344 status_t ReadWrite(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, uint16_t NumBytes) 01345 { 01346 if (dev_spi.spi_read_write(pBufferToRead, pBufferToWrite, ssel, NumBytes) != 0) { 01347 return COMPONENT_ERROR; 01348 } 01349 return COMPONENT_OK; 01350 } 01351 01352 /* ACTION 8 --------------------------------------------------------------* 01353 * Implement here other I/O methods beyond those already implemented * 01354 * above, which are declared extern within the component's header file. * 01355 *------------------------------------------------------------------------*/ 01356 /** 01357 * @brief Making the CPU wait. 01358 * @param None. 01359 * @retval None. 01360 */ 01361 void Powerstep01_Board_Delay(uint32_t delay) 01362 { 01363 wait_ms(delay); 01364 } 01365 01366 /** 01367 * @brief Enabling interrupts. 01368 * @param None. 01369 * @retval None. 01370 */ 01371 void Powerstep01_Board_EnableIrq(void) 01372 { 01373 __enable_irq(); 01374 } 01375 01376 /** 01377 * @brief Disabling interrupts. 01378 * @param None. 01379 * @retval None. 01380 */ 01381 void Powerstep01_Board_DisableIrq(void) 01382 { 01383 __disable_irq(); 01384 } 01385 01386 /** 01387 * @brief Initialising the PWM. 01388 * @param None. 01389 * @retval None. 01390 */ 01391 void Powerstep01_Board_StepClockInit(void) {} 01392 01393 /** 01394 * @brief Exit the device from reset mode. 01395 * @param None. 01396 * @retval None. 01397 */ 01398 void Powerstep01_Board_ReleaseReset(void) 01399 { 01400 standby_reset = 1; 01401 } 01402 01403 /** 01404 * @brief Put the device in reset mode. 01405 * @param None. 01406 * @retval None. 01407 */ 01408 void Powerstep01_Board_reset(void) 01409 { 01410 standby_reset = 0; 01411 } 01412 01413 /** 01414 * @brief Writing and reading bytes to/from the component through the SPI at the same time. 01415 * @param pByteToTransmit pointer to the buffer of data to send. 01416 * @param[out] pReceivedByte pointer to the buffer to read data into. 01417 * @retval "0" in case of success, "1" otherwise. 01418 */ 01419 uint8_t Powerstep01_Board_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) 01420 { 01421 return (uint8_t) (ReadWrite(pReceivedByte, pByteToTransmit, numberOfDevices) == COMPONENT_OK ? 0 : 1); 01422 } 01423 01424 01425 /*** Component's Instance Variables ***/ 01426 01427 /* ACTION 9 --------------------------------------------------------------* 01428 * Declare here interrupt related variables, if needed. * 01429 * Note that interrupt handling is platform dependent, see * 01430 * "Interrupt Related Methods" above. * 01431 * * 01432 * Example: * 01433 * + mbed: * 01434 * InterruptIn feature_irq; * 01435 *------------------------------------------------------------------------*/ 01436 /* Flag Interrupt. */ 01437 InterruptIn flag_irq; 01438 01439 /* Busy Interrupt. */ 01440 InterruptIn busy_irq; 01441 01442 /* ACTION 10 -------------------------------------------------------------* 01443 * Declare here other pin related variables, if needed. * 01444 * * 01445 * Example: * 01446 * + mbed: * 01447 * DigitalOut standby_reset; * 01448 *------------------------------------------------------------------------*/ 01449 /* Standby/reset pin. */ 01450 DigitalOut standby_reset; 01451 01452 /* Pulse Width Modulation pin. */ 01453 PwmOut pwm; 01454 01455 /* ACTION 11 -------------------------------------------------------------* 01456 * Declare here communication related variables, if needed. * 01457 * * 01458 * Example: * 01459 * + mbed: * 01460 * DigitalOut ssel; * 01461 * DevSPI &dev_spi; * 01462 *------------------------------------------------------------------------*/ 01463 /* Configuration. */ 01464 DigitalOut ssel; 01465 01466 /* IO Device. */ 01467 DevSPI &dev_spi; 01468 01469 /* ACTION 12 -------------------------------------------------------------* 01470 * Declare here identity related variables, if needed. * 01471 * Note that there should be only a unique identifier for each component, * 01472 * which should be the "who_am_i" parameter. * 01473 *------------------------------------------------------------------------*/ 01474 /* Identity */ 01475 uint8_t who_am_i; 01476 01477 /* ACTION 13 -------------------------------------------------------------* 01478 * Declare here the component's static and non-static data, one variable * 01479 * per line. * 01480 * * 01481 * Example: * 01482 * float measure; * 01483 * int instance_id; * 01484 * static int number_of_instances; * 01485 *------------------------------------------------------------------------*/ 01486 /* Data. */ 01487 void (*errorHandlerCallback)(uint16_t error); 01488 uint8_t deviceInstance; 01489 01490 /* Static data. */ 01491 static uint8_t numberOfDevices; 01492 static uint8_t spiTxBursts[POWERSTEP01_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; 01493 static uint8_t spiRxBursts[POWERSTEP01_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; 01494 01495 01496 public: 01497 01498 /* Static data. */ 01499 static bool spiPreemptionByIsr; 01500 static bool isrFlag; 01501 01502 }; 01503 01504 #endif // __POWERSTEP01_CLASS_H 01505 01506 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Thu Jul 14 2022 15:33:05 by
