1
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of X_NUCLEO_IHM01A1 by
Components/L6474/L6474.cpp@36:c1cda832e875, 2018-09-14 (annotated)
- Committer:
- alex1403
- Date:
- Fri Sep 14 09:12:58 2018 +0000
- Revision:
- 36:c1cda832e875
- Parent:
- 35:974ca699c792
Version 1;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davide.aliprandi@st.com | 33:8daea0279301 | 1 | /** |
davide.aliprandi@st.com | 33:8daea0279301 | 2 | ****************************************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 3 | * @file L6474.cpp |
davide.aliprandi@st.com | 33:8daea0279301 | 4 | * @author IPC Rennes |
davide.aliprandi@st.com | 33:8daea0279301 | 5 | * @version V1.5.0 |
davide.aliprandi@st.com | 33:8daea0279301 | 6 | * @date November 12, 2014 |
davide.aliprandi@st.com | 33:8daea0279301 | 7 | * @brief L6474 driver (fully integrated microstepping motor driver) |
davide.aliprandi@st.com | 33:8daea0279301 | 8 | * @note (C) COPYRIGHT 2014 STMicroelectronics |
davide.aliprandi@st.com | 33:8daea0279301 | 9 | ****************************************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 10 | * @attention |
davide.aliprandi@st.com | 33:8daea0279301 | 11 | * |
davide.aliprandi@st.com | 33:8daea0279301 | 12 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
davide.aliprandi@st.com | 33:8daea0279301 | 13 | * |
davide.aliprandi@st.com | 33:8daea0279301 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
davide.aliprandi@st.com | 33:8daea0279301 | 15 | * are permitted provided that the following conditions are met: |
davide.aliprandi@st.com | 33:8daea0279301 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
davide.aliprandi@st.com | 33:8daea0279301 | 17 | * this list of conditions and the following disclaimer. |
davide.aliprandi@st.com | 33:8daea0279301 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
davide.aliprandi@st.com | 33:8daea0279301 | 19 | * this list of conditions and the following disclaimer in the documentation |
davide.aliprandi@st.com | 33:8daea0279301 | 20 | * and/or other materials provided with the distribution. |
davide.aliprandi@st.com | 33:8daea0279301 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
davide.aliprandi@st.com | 33:8daea0279301 | 22 | * may be used to endorse or promote products derived from this software |
davide.aliprandi@st.com | 33:8daea0279301 | 23 | * without specific prior written permission. |
davide.aliprandi@st.com | 33:8daea0279301 | 24 | * |
davide.aliprandi@st.com | 33:8daea0279301 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
davide.aliprandi@st.com | 33:8daea0279301 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
davide.aliprandi@st.com | 33:8daea0279301 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
davide.aliprandi@st.com | 33:8daea0279301 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
davide.aliprandi@st.com | 33:8daea0279301 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
davide.aliprandi@st.com | 33:8daea0279301 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
davide.aliprandi@st.com | 33:8daea0279301 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
davide.aliprandi@st.com | 33:8daea0279301 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
davide.aliprandi@st.com | 33:8daea0279301 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
davide.aliprandi@st.com | 33:8daea0279301 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
davide.aliprandi@st.com | 33:8daea0279301 | 35 | * |
davide.aliprandi@st.com | 33:8daea0279301 | 36 | ****************************************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 37 | */ |
davide.aliprandi@st.com | 33:8daea0279301 | 38 | |
davide.aliprandi@st.com | 33:8daea0279301 | 39 | |
davide.aliprandi@st.com | 33:8daea0279301 | 40 | /* Generated with STM32CubeTOO -----------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 41 | |
davide.aliprandi@st.com | 33:8daea0279301 | 42 | |
davide.aliprandi@st.com | 33:8daea0279301 | 43 | /* Revision ------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 44 | /* |
davide.aliprandi@st.com | 33:8daea0279301 | 45 | Repository: http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev |
davide.aliprandi@st.com | 33:8daea0279301 | 46 | Branch/Trunk/Tag: trunk |
davide.aliprandi@st.com | 33:8daea0279301 | 47 | Based on: X-CUBE-SPN1/trunk/Drivers/BSP/Components/l6474/l6474.c |
davide.aliprandi@st.com | 33:8daea0279301 | 48 | Revision: 0 |
davide.aliprandi@st.com | 33:8daea0279301 | 49 | */ |
davide.aliprandi@st.com | 33:8daea0279301 | 50 | |
davide.aliprandi@st.com | 33:8daea0279301 | 51 | |
davide.aliprandi@st.com | 33:8daea0279301 | 52 | /* Includes ------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 53 | |
davide.aliprandi@st.com | 33:8daea0279301 | 54 | #include "L6474.h" |
davide.aliprandi@st.com | 33:8daea0279301 | 55 | |
davide.aliprandi@st.com | 33:8daea0279301 | 56 | |
davide.aliprandi@st.com | 33:8daea0279301 | 57 | /* Definitions ---------------------------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 58 | |
davide.aliprandi@st.com | 33:8daea0279301 | 59 | /* Error while initialising the SPI. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 60 | #define L6474_ERROR_0 (0x8000) |
davide.aliprandi@st.com | 33:8daea0279301 | 61 | |
davide.aliprandi@st.com | 33:8daea0279301 | 62 | /* Error of bad SPI transaction. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 63 | #define L6474_ERROR_1 (0x8001) |
davide.aliprandi@st.com | 33:8daea0279301 | 64 | |
davide.aliprandi@st.com | 33:8daea0279301 | 65 | /* Maximum number of steps. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 66 | #define MAX_STEPS (0x7FFFFFFF) |
davide.aliprandi@st.com | 33:8daea0279301 | 67 | |
davide.aliprandi@st.com | 33:8daea0279301 | 68 | /* Maximum frequency of the PWMs in Hz. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 69 | #define L6474_MAX_PWM_FREQ (10000) |
davide.aliprandi@st.com | 33:8daea0279301 | 70 | |
davide.aliprandi@st.com | 33:8daea0279301 | 71 | /* Minimum frequency of the PWMs in Hz. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 72 | #define L6474_MIN_PWM_FREQ (2) |
davide.aliprandi@st.com | 33:8daea0279301 | 73 | |
davide.aliprandi@st.com | 33:8daea0279301 | 74 | |
davide.aliprandi@st.com | 33:8daea0279301 | 75 | /* Variables ----------------------------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 76 | |
davide.aliprandi@st.com | 33:8daea0279301 | 77 | /* Number of devices. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 78 | uint8_t L6474::number_of_devices = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 79 | |
davide.aliprandi@st.com | 33:8daea0279301 | 80 | /* ISR flags used to restart an interrupted SPI transfer when an error is reported. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 81 | bool L6474::spi_preemtion_by_isr = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 82 | bool L6474::isr_flag = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 83 | |
davide.aliprandi@st.com | 33:8daea0279301 | 84 | /* SPI Transmission for Daisy-Chain Configuration. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 85 | uint8_t L6474::spi_tx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; |
davide.aliprandi@st.com | 33:8daea0279301 | 86 | uint8_t L6474::spi_rx_bursts[L6474_CMD_ARG_MAX_NB_BYTES][MAX_NUMBER_OF_DEVICES]; |
davide.aliprandi@st.com | 33:8daea0279301 | 87 | |
davide.aliprandi@st.com | 33:8daea0279301 | 88 | |
davide.aliprandi@st.com | 33:8daea0279301 | 89 | /* Methods -------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 90 | |
davide.aliprandi@st.com | 33:8daea0279301 | 91 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 92 | * @brief Attaches a user callback to the error Handler. |
davide.aliprandi@st.com | 33:8daea0279301 | 93 | * The call back will be then called each time the library |
davide.aliprandi@st.com | 33:8daea0279301 | 94 | * detects an error |
davide.aliprandi@st.com | 33:8daea0279301 | 95 | * @param[in] callback Name of the callback to attach |
davide.aliprandi@st.com | 33:8daea0279301 | 96 | * to the error Hanlder |
davide.aliprandi@st.com | 33:8daea0279301 | 97 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 98 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 99 | void L6474::L6474_AttachErrorHandler(void (*callback)(uint16_t error)) |
davide.aliprandi@st.com | 33:8daea0279301 | 100 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 101 | error_handler_callback = (void (*)(uint16_t error)) callback; |
davide.aliprandi@st.com | 33:8daea0279301 | 102 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 103 | |
davide.aliprandi@st.com | 33:8daea0279301 | 104 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 105 | * @brief Starts the L6474 library |
davide.aliprandi@st.com | 33:8daea0279301 | 106 | * @param init Initialization structure. |
davide.aliprandi@st.com | 33:8daea0279301 | 107 | * @retval COMPONENT_OK in case of success. |
davide.aliprandi@st.com | 33:8daea0279301 | 108 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 109 | status_t L6474::L6474_Init(void *init) |
davide.aliprandi@st.com | 33:8daea0279301 | 110 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 111 | /* Initialise the PWMs used for the Step clocks ----------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 112 | L6474_PwmInit(); |
davide.aliprandi@st.com | 33:8daea0279301 | 113 | |
davide.aliprandi@st.com | 33:8daea0279301 | 114 | /* Initialise the L6474s ------------------------------------------------*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 115 | |
davide.aliprandi@st.com | 33:8daea0279301 | 116 | /* Standby-reset deactivation */ |
davide.aliprandi@st.com | 33:8daea0279301 | 117 | L6474_ReleaseReset(); |
davide.aliprandi@st.com | 33:8daea0279301 | 118 | |
davide.aliprandi@st.com | 33:8daea0279301 | 119 | /* Let a delay after reset */ |
davide.aliprandi@st.com | 33:8daea0279301 | 120 | L6474_Delay(1); |
davide.aliprandi@st.com | 33:8daea0279301 | 121 | |
davide.aliprandi@st.com | 33:8daea0279301 | 122 | /* Set device parameters to the predefined values from "l6474_target_config.h". */ |
davide.aliprandi@st.com | 33:8daea0279301 | 123 | L6474_SetDeviceParamsToPredefinedValues(); |
davide.aliprandi@st.com | 33:8daea0279301 | 124 | |
davide.aliprandi@st.com | 33:8daea0279301 | 125 | if (init == NULL) |
davide.aliprandi@st.com | 33:8daea0279301 | 126 | /* Set device registers to the predefined values from "l6474_target_config.h". */ |
davide.aliprandi@st.com | 33:8daea0279301 | 127 | L6474_SetRegisterToPredefinedValues(); |
davide.aliprandi@st.com | 33:8daea0279301 | 128 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 129 | /* Set device registers to the passed initialization values. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 130 | L6474_SetRegisterToInitializationValues((L6474_init_t *) init); |
davide.aliprandi@st.com | 33:8daea0279301 | 131 | |
davide.aliprandi@st.com | 33:8daea0279301 | 132 | /* Disable L6474 powerstage */ |
davide.aliprandi@st.com | 33:8daea0279301 | 133 | L6474_CmdDisable(); |
davide.aliprandi@st.com | 33:8daea0279301 | 134 | |
davide.aliprandi@st.com | 33:8daea0279301 | 135 | /* Get Status to clear flags after start up */ |
davide.aliprandi@st.com | 33:8daea0279301 | 136 | L6474_CmdGetStatus(); |
davide.aliprandi@st.com | 33:8daea0279301 | 137 | |
davide.aliprandi@st.com | 33:8daea0279301 | 138 | return COMPONENT_OK; |
davide.aliprandi@st.com | 33:8daea0279301 | 139 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 140 | |
davide.aliprandi@st.com | 33:8daea0279301 | 141 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 142 | * @brief Read id |
davide.aliprandi@st.com | 33:8daea0279301 | 143 | * @param id pointer to the identifier to be read. |
davide.aliprandi@st.com | 33:8daea0279301 | 144 | * @retval COMPONENT_OK in case of success. |
davide.aliprandi@st.com | 33:8daea0279301 | 145 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 146 | status_t L6474::L6474_ReadID(uint8_t *id) |
davide.aliprandi@st.com | 33:8daea0279301 | 147 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 148 | *id = device_instance; |
davide.aliprandi@st.com | 33:8daea0279301 | 149 | |
davide.aliprandi@st.com | 33:8daea0279301 | 150 | return COMPONENT_OK; |
davide.aliprandi@st.com | 33:8daea0279301 | 151 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 152 | |
davide.aliprandi@st.com | 33:8daea0279301 | 153 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 154 | * @brief Returns the acceleration of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 155 | * @retval Acceleration in pps^2 |
davide.aliprandi@st.com | 33:8daea0279301 | 156 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 157 | uint16_t L6474::L6474_GetAcceleration(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 158 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 159 | return (device_prm.acceleration); |
davide.aliprandi@st.com | 33:8daea0279301 | 160 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 161 | |
davide.aliprandi@st.com | 33:8daea0279301 | 162 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 163 | * @brief Returns the current speed of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 164 | * @retval Speed in pps |
davide.aliprandi@st.com | 33:8daea0279301 | 165 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 166 | uint16_t L6474::L6474_GetCurrentSpeed(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 167 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 168 | return device_prm.speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 169 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 170 | |
davide.aliprandi@st.com | 33:8daea0279301 | 171 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 172 | * @brief Returns the deceleration of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 173 | * @retval Deceleration in pps^2 |
davide.aliprandi@st.com | 33:8daea0279301 | 174 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 175 | uint16_t L6474::L6474_GetDeceleration(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 176 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 177 | return (device_prm.deceleration); |
davide.aliprandi@st.com | 33:8daea0279301 | 178 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 179 | |
davide.aliprandi@st.com | 33:8daea0279301 | 180 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 181 | * @brief Returns the device state |
davide.aliprandi@st.com | 33:8daea0279301 | 182 | * @retval State (ACCELERATING, DECELERATING, STEADY or INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 183 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 184 | motorState_t L6474::L6474_GetDeviceState(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 185 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 186 | return device_prm.motionState; |
davide.aliprandi@st.com | 33:8daea0279301 | 187 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 188 | |
davide.aliprandi@st.com | 33:8daea0279301 | 189 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 190 | * @brief Returns the FW version of the library |
davide.aliprandi@st.com | 33:8daea0279301 | 191 | * @param None |
davide.aliprandi@st.com | 33:8daea0279301 | 192 | * @retval L6474_FW_VERSION |
davide.aliprandi@st.com | 33:8daea0279301 | 193 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 194 | uint8_t L6474::L6474_GetFwVersion(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 195 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 196 | return (L6474_FW_VERSION); |
davide.aliprandi@st.com | 33:8daea0279301 | 197 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 198 | |
davide.aliprandi@st.com | 33:8daea0279301 | 199 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 200 | * @brief Returns the mark position of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 201 | * @retval Mark register value converted in a 32b signed integer |
davide.aliprandi@st.com | 33:8daea0279301 | 202 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 203 | int32_t L6474::L6474_GetMark(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 204 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 205 | return L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); |
davide.aliprandi@st.com | 33:8daea0279301 | 206 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 207 | |
davide.aliprandi@st.com | 33:8daea0279301 | 208 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 209 | * @brief Returns the max speed of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 210 | * @retval maxSpeed in pps |
davide.aliprandi@st.com | 33:8daea0279301 | 211 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 212 | uint16_t L6474::L6474_GetMaxSpeed(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 213 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 214 | return (device_prm.maxSpeed); |
davide.aliprandi@st.com | 33:8daea0279301 | 215 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 216 | |
davide.aliprandi@st.com | 33:8daea0279301 | 217 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 218 | * @brief Returns the min speed of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 219 | * @retval minSpeed in pps |
davide.aliprandi@st.com | 33:8daea0279301 | 220 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 221 | uint16_t L6474::L6474_GetMinSpeed(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 222 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 223 | return (device_prm.minSpeed); |
davide.aliprandi@st.com | 33:8daea0279301 | 224 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 225 | |
davide.aliprandi@st.com | 33:8daea0279301 | 226 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 227 | * @brief Returns the ABS_POSITION of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 228 | * @retval ABS_POSITION register value converted in a 32b signed integer |
davide.aliprandi@st.com | 33:8daea0279301 | 229 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 230 | int32_t L6474::L6474_GetPosition(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 231 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 232 | return L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); |
davide.aliprandi@st.com | 33:8daea0279301 | 233 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 234 | |
davide.aliprandi@st.com | 33:8daea0279301 | 235 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 236 | * @brief Requests the motor to move to the home position (ABS_POSITION = 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 237 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 238 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 239 | void L6474::L6474_GoHome(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 240 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 241 | L6474_GoTo(0); |
davide.aliprandi@st.com | 33:8daea0279301 | 242 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 243 | |
davide.aliprandi@st.com | 33:8daea0279301 | 244 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 245 | * @brief Requests the motor to move to the mark position |
davide.aliprandi@st.com | 33:8daea0279301 | 246 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 247 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 248 | void L6474::L6474_GoMark(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 249 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 250 | uint32_t mark; |
davide.aliprandi@st.com | 33:8daea0279301 | 251 | |
davide.aliprandi@st.com | 33:8daea0279301 | 252 | mark = L6474_ConvertPosition(L6474_CmdGetParam(L6474_MARK)); |
davide.aliprandi@st.com | 33:8daea0279301 | 253 | L6474_GoTo(mark); |
davide.aliprandi@st.com | 33:8daea0279301 | 254 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 255 | |
davide.aliprandi@st.com | 33:8daea0279301 | 256 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 257 | * @brief Requests the motor to move to the specified position |
davide.aliprandi@st.com | 33:8daea0279301 | 258 | * @param[in] targetPosition absolute position in steps |
davide.aliprandi@st.com | 33:8daea0279301 | 259 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 260 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 261 | void L6474::L6474_GoTo(int32_t targetPosition) |
davide.aliprandi@st.com | 33:8daea0279301 | 262 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 263 | motorDir_t direction; |
davide.aliprandi@st.com | 33:8daea0279301 | 264 | int32_t steps; |
davide.aliprandi@st.com | 33:8daea0279301 | 265 | |
davide.aliprandi@st.com | 33:8daea0279301 | 266 | /* Eventually deactivate motor */ |
davide.aliprandi@st.com | 33:8daea0279301 | 267 | if (device_prm.motionState != INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 268 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 269 | L6474_HardStop(); |
davide.aliprandi@st.com | 33:8daea0279301 | 270 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 271 | |
davide.aliprandi@st.com | 33:8daea0279301 | 272 | /* Get current position */ |
davide.aliprandi@st.com | 33:8daea0279301 | 273 | device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); |
davide.aliprandi@st.com | 33:8daea0279301 | 274 | |
davide.aliprandi@st.com | 33:8daea0279301 | 275 | /* Compute the number of steps to perform */ |
davide.aliprandi@st.com | 33:8daea0279301 | 276 | steps = targetPosition - device_prm.currentPosition; |
davide.aliprandi@st.com | 33:8daea0279301 | 277 | |
davide.aliprandi@st.com | 33:8daea0279301 | 278 | if (steps >= 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 279 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 280 | device_prm.stepsToTake = steps; |
davide.aliprandi@st.com | 33:8daea0279301 | 281 | direction = FORWARD; |
davide.aliprandi@st.com | 33:8daea0279301 | 282 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 283 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 284 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 285 | device_prm.stepsToTake = -steps; |
davide.aliprandi@st.com | 33:8daea0279301 | 286 | direction = BACKWARD; |
davide.aliprandi@st.com | 33:8daea0279301 | 287 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 288 | |
davide.aliprandi@st.com | 33:8daea0279301 | 289 | if (steps != 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 290 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 291 | device_prm.commandExecuted = MOVE_CMD; |
davide.aliprandi@st.com | 33:8daea0279301 | 292 | |
davide.aliprandi@st.com | 33:8daea0279301 | 293 | /* Direction setup */ |
davide.aliprandi@st.com | 33:8daea0279301 | 294 | L6474_SetDirection(direction); |
davide.aliprandi@st.com | 33:8daea0279301 | 295 | |
davide.aliprandi@st.com | 33:8daea0279301 | 296 | L6474_ComputeSpeedProfile(device_prm.stepsToTake); |
davide.aliprandi@st.com | 33:8daea0279301 | 297 | |
davide.aliprandi@st.com | 33:8daea0279301 | 298 | /* Motor activation */ |
davide.aliprandi@st.com | 33:8daea0279301 | 299 | L6474_StartMovement(); |
davide.aliprandi@st.com | 33:8daea0279301 | 300 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 301 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 302 | |
davide.aliprandi@st.com | 33:8daea0279301 | 303 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 304 | * @brief Immediatly stops the motor and disable the power bridge |
davide.aliprandi@st.com | 33:8daea0279301 | 305 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 306 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 307 | void L6474::L6474_HardStop(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 308 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 309 | /* Disable corresponding PWM */ |
davide.aliprandi@st.com | 33:8daea0279301 | 310 | L6474_PwmStop(); |
davide.aliprandi@st.com | 33:8daea0279301 | 311 | |
davide.aliprandi@st.com | 33:8daea0279301 | 312 | /* Set inactive state */ |
davide.aliprandi@st.com | 33:8daea0279301 | 313 | device_prm.motionState = INACTIVE; |
davide.aliprandi@st.com | 33:8daea0279301 | 314 | device_prm.commandExecuted = NO_CMD; |
davide.aliprandi@st.com | 33:8daea0279301 | 315 | device_prm.stepsToTake = MAX_STEPS; |
davide.aliprandi@st.com | 33:8daea0279301 | 316 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 317 | |
davide.aliprandi@st.com | 33:8daea0279301 | 318 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 319 | * @brief Moves the motor of the specified number of steps |
davide.aliprandi@st.com | 33:8daea0279301 | 320 | * @param[in] direction FORWARD or BACKWARD |
davide.aliprandi@st.com | 33:8daea0279301 | 321 | * @param[in] stepCount Number of steps to perform |
davide.aliprandi@st.com | 33:8daea0279301 | 322 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 323 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 324 | void L6474::L6474_Move(motorDir_t direction, uint32_t stepCount) |
davide.aliprandi@st.com | 33:8daea0279301 | 325 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 326 | /* Eventually deactivate motor */ |
davide.aliprandi@st.com | 33:8daea0279301 | 327 | if (device_prm.motionState != INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 328 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 329 | L6474_HardStop(); |
davide.aliprandi@st.com | 33:8daea0279301 | 330 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 331 | |
davide.aliprandi@st.com | 33:8daea0279301 | 332 | if (stepCount != 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 333 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 334 | device_prm.stepsToTake = stepCount; |
davide.aliprandi@st.com | 33:8daea0279301 | 335 | |
davide.aliprandi@st.com | 33:8daea0279301 | 336 | device_prm.commandExecuted = MOVE_CMD; |
davide.aliprandi@st.com | 33:8daea0279301 | 337 | |
davide.aliprandi@st.com | 33:8daea0279301 | 338 | device_prm.currentPosition = L6474_ConvertPosition(L6474_CmdGetParam(L6474_ABS_POS)); |
davide.aliprandi@st.com | 33:8daea0279301 | 339 | |
davide.aliprandi@st.com | 33:8daea0279301 | 340 | /* Direction setup */ |
davide.aliprandi@st.com | 33:8daea0279301 | 341 | L6474_SetDirection(direction); |
davide.aliprandi@st.com | 33:8daea0279301 | 342 | |
davide.aliprandi@st.com | 33:8daea0279301 | 343 | L6474_ComputeSpeedProfile(stepCount); |
davide.aliprandi@st.com | 33:8daea0279301 | 344 | |
davide.aliprandi@st.com | 33:8daea0279301 | 345 | /* Motor activation */ |
davide.aliprandi@st.com | 33:8daea0279301 | 346 | L6474_StartMovement(); |
davide.aliprandi@st.com | 33:8daea0279301 | 347 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 348 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 349 | |
davide.aliprandi@st.com | 33:8daea0279301 | 350 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 351 | * @brief Runs the motor. It will accelerate from the min |
davide.aliprandi@st.com | 33:8daea0279301 | 352 | * speed up to the max speed by using the device acceleration. |
davide.aliprandi@st.com | 33:8daea0279301 | 353 | * @param[in] direction FORWARD or BACKWARD |
davide.aliprandi@st.com | 33:8daea0279301 | 354 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 355 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 356 | void L6474::L6474_Run(motorDir_t direction) |
davide.aliprandi@st.com | 33:8daea0279301 | 357 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 358 | /* Eventually deactivate motor */ |
davide.aliprandi@st.com | 33:8daea0279301 | 359 | if (device_prm.motionState != INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 360 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 361 | L6474_HardStop(); |
davide.aliprandi@st.com | 33:8daea0279301 | 362 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 363 | |
davide.aliprandi@st.com | 33:8daea0279301 | 364 | /* Direction setup */ |
davide.aliprandi@st.com | 33:8daea0279301 | 365 | L6474_SetDirection(direction); |
davide.aliprandi@st.com | 33:8daea0279301 | 366 | |
davide.aliprandi@st.com | 33:8daea0279301 | 367 | device_prm.commandExecuted = RUN_CMD; |
davide.aliprandi@st.com | 33:8daea0279301 | 368 | |
davide.aliprandi@st.com | 33:8daea0279301 | 369 | /* Motor activation */ |
davide.aliprandi@st.com | 33:8daea0279301 | 370 | L6474_StartMovement(); |
davide.aliprandi@st.com | 33:8daea0279301 | 371 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 372 | |
davide.aliprandi@st.com | 33:8daea0279301 | 373 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 374 | * @brief Changes the acceleration of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 375 | * @param[in] newAcc New acceleration to apply in pps^2 |
davide.aliprandi@st.com | 33:8daea0279301 | 376 | * @retval true if the command is successfully executed, else false |
davide.aliprandi@st.com | 33:8daea0279301 | 377 | * @note The command is not performed is the device is executing |
davide.aliprandi@st.com | 33:8daea0279301 | 378 | * a MOVE or GOTO command (but it can be used during a RUN command) |
davide.aliprandi@st.com | 33:8daea0279301 | 379 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 380 | bool L6474::L6474_SetAcceleration(uint16_t newAcc) |
davide.aliprandi@st.com | 33:8daea0279301 | 381 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 382 | bool cmdExecuted = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 383 | if ((newAcc != 0)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 384 | ((device_prm.motionState == INACTIVE)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 385 | (device_prm.commandExecuted == RUN_CMD))) |
davide.aliprandi@st.com | 33:8daea0279301 | 386 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 387 | device_prm.acceleration = newAcc; |
davide.aliprandi@st.com | 33:8daea0279301 | 388 | cmdExecuted = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 389 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 390 | return cmdExecuted; |
davide.aliprandi@st.com | 33:8daea0279301 | 391 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 392 | |
davide.aliprandi@st.com | 33:8daea0279301 | 393 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 394 | * @brief Changes the deceleration of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 395 | * @param[in] newDec New deceleration to apply in pps^2 |
davide.aliprandi@st.com | 33:8daea0279301 | 396 | * @retval true if the command is successfully executed, else false |
davide.aliprandi@st.com | 33:8daea0279301 | 397 | * @note The command is not performed is the device is executing |
davide.aliprandi@st.com | 33:8daea0279301 | 398 | * a MOVE or GOTO command (but it can be used during a RUN command) |
davide.aliprandi@st.com | 33:8daea0279301 | 399 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 400 | bool L6474::L6474_SetDeceleration(uint16_t newDec) |
davide.aliprandi@st.com | 33:8daea0279301 | 401 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 402 | bool cmdExecuted = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 403 | if ((newDec != 0)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 404 | ((device_prm.motionState == INACTIVE)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 405 | (device_prm.commandExecuted == RUN_CMD))) |
davide.aliprandi@st.com | 33:8daea0279301 | 406 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 407 | device_prm.deceleration = newDec; |
davide.aliprandi@st.com | 33:8daea0279301 | 408 | cmdExecuted = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 409 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 410 | return cmdExecuted; |
davide.aliprandi@st.com | 33:8daea0279301 | 411 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 412 | |
davide.aliprandi@st.com | 33:8daea0279301 | 413 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 414 | * @brief Set current position to be the Home position (ABS pos set to 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 415 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 416 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 417 | void L6474::L6474_SetHome(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 418 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 419 | L6474_CmdSetParam(L6474_ABS_POS, 0); |
davide.aliprandi@st.com | 33:8daea0279301 | 420 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 421 | |
davide.aliprandi@st.com | 33:8daea0279301 | 422 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 423 | * @brief Sets current position to be the Mark position |
davide.aliprandi@st.com | 33:8daea0279301 | 424 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 425 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 426 | void L6474::L6474_SetMark(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 427 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 428 | uint32_t mark = L6474_CmdGetParam(L6474_ABS_POS); |
davide.aliprandi@st.com | 33:8daea0279301 | 429 | L6474_CmdSetParam(L6474_MARK, mark); |
davide.aliprandi@st.com | 33:8daea0279301 | 430 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 431 | |
davide.aliprandi@st.com | 33:8daea0279301 | 432 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 433 | * @brief Changes the max speed of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 434 | * @param[in] newMaxSpeed New max speed to apply in pps |
davide.aliprandi@st.com | 33:8daea0279301 | 435 | * @retval true if the command is successfully executed, else false |
davide.aliprandi@st.com | 33:8daea0279301 | 436 | * @note The command is not performed is the device is executing |
davide.aliprandi@st.com | 33:8daea0279301 | 437 | * a MOVE or GOTO command (but it can be used during a RUN command). |
davide.aliprandi@st.com | 33:8daea0279301 | 438 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 439 | bool L6474::L6474_SetMaxSpeed(uint16_t newMaxSpeed) |
davide.aliprandi@st.com | 33:8daea0279301 | 440 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 441 | bool cmdExecuted = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 442 | if ((newMaxSpeed >= L6474_MIN_PWM_FREQ)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 443 | (newMaxSpeed <= L6474_MAX_PWM_FREQ) && |
davide.aliprandi@st.com | 33:8daea0279301 | 444 | (device_prm.minSpeed <= newMaxSpeed) && |
davide.aliprandi@st.com | 33:8daea0279301 | 445 | ((device_prm.motionState == INACTIVE)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 446 | (device_prm.commandExecuted == RUN_CMD))) |
davide.aliprandi@st.com | 33:8daea0279301 | 447 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 448 | device_prm.maxSpeed = newMaxSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 449 | cmdExecuted = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 450 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 451 | return cmdExecuted; |
davide.aliprandi@st.com | 33:8daea0279301 | 452 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 453 | |
davide.aliprandi@st.com | 33:8daea0279301 | 454 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 455 | * @brief Changes the min speed of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 456 | * @param[in] newMinSpeed New min speed to apply in pps |
davide.aliprandi@st.com | 33:8daea0279301 | 457 | * @retval true if the command is successfully executed, else false |
davide.aliprandi@st.com | 33:8daea0279301 | 458 | * @note The command is not performed is the device is executing |
davide.aliprandi@st.com | 33:8daea0279301 | 459 | * a MOVE or GOTO command (but it can be used during a RUN command). |
davide.aliprandi@st.com | 33:8daea0279301 | 460 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 461 | bool L6474::L6474_SetMinSpeed(uint16_t newMinSpeed) |
davide.aliprandi@st.com | 33:8daea0279301 | 462 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 463 | bool cmdExecuted = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 464 | if ((newMinSpeed >= L6474_MIN_PWM_FREQ)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 465 | (newMinSpeed <= L6474_MAX_PWM_FREQ) && |
davide.aliprandi@st.com | 33:8daea0279301 | 466 | (newMinSpeed <= device_prm.maxSpeed) && |
davide.aliprandi@st.com | 33:8daea0279301 | 467 | ((device_prm.motionState == INACTIVE)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 468 | (device_prm.commandExecuted == RUN_CMD))) |
davide.aliprandi@st.com | 33:8daea0279301 | 469 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 470 | device_prm.minSpeed = newMinSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 471 | cmdExecuted = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 472 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 473 | return cmdExecuted; |
davide.aliprandi@st.com | 33:8daea0279301 | 474 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 475 | |
davide.aliprandi@st.com | 33:8daea0279301 | 476 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 477 | * @brief Stops the motor by using the device deceleration |
davide.aliprandi@st.com | 33:8daea0279301 | 478 | * @retval true if the command is successfully executed, else false |
davide.aliprandi@st.com | 33:8daea0279301 | 479 | * @note The command is not performed is the device is in INACTIVE state. |
davide.aliprandi@st.com | 33:8daea0279301 | 480 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 481 | bool L6474::L6474_SoftStop(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 482 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 483 | bool cmdExecuted = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 484 | if (device_prm.motionState != INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 485 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 486 | device_prm.commandExecuted = SOFT_STOP_CMD; |
davide.aliprandi@st.com | 33:8daea0279301 | 487 | cmdExecuted = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 488 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 489 | return (cmdExecuted); |
davide.aliprandi@st.com | 33:8daea0279301 | 490 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 491 | |
davide.aliprandi@st.com | 33:8daea0279301 | 492 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 493 | * @brief Locks until the device state becomes Inactive |
davide.aliprandi@st.com | 33:8daea0279301 | 494 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 495 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 496 | void L6474::L6474_WaitWhileActive(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 497 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 498 | /* Wait while motor is running */ |
davide.aliprandi@st.com | 33:8daea0279301 | 499 | while (L6474_GetDeviceState() != INACTIVE); |
davide.aliprandi@st.com | 33:8daea0279301 | 500 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 501 | |
davide.aliprandi@st.com | 33:8daea0279301 | 502 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 503 | * @brief Issue the Disable command to the L6474 of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 504 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 505 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 506 | void L6474::L6474_CmdDisable(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 507 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 508 | L6474_SendCommand(L6474_DISABLE); |
davide.aliprandi@st.com | 33:8daea0279301 | 509 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 510 | |
davide.aliprandi@st.com | 33:8daea0279301 | 511 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 512 | * @brief Issues the Enable command to the L6474 of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 513 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 514 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 515 | void L6474::L6474_CmdEnable(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 516 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 517 | L6474_SendCommand(L6474_ENABLE); |
davide.aliprandi@st.com | 33:8daea0279301 | 518 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 519 | |
davide.aliprandi@st.com | 33:8daea0279301 | 520 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 521 | * @brief Issues the GetParam command to the L6474 of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 522 | * @param[in] parameter Register adress (L6474_ABS_POS, L6474_MARK,...) |
davide.aliprandi@st.com | 33:8daea0279301 | 523 | * @retval Register value |
davide.aliprandi@st.com | 33:8daea0279301 | 524 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 525 | uint32_t L6474::L6474_CmdGetParam(L6474_Registers_t parameter) |
davide.aliprandi@st.com | 33:8daea0279301 | 526 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 527 | uint32_t i; |
davide.aliprandi@st.com | 33:8daea0279301 | 528 | uint32_t spiRxData; |
davide.aliprandi@st.com | 33:8daea0279301 | 529 | uint8_t maxArgumentNbBytes = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 530 | uint8_t spiIndex = number_of_devices - device_instance - 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 531 | bool itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 532 | |
davide.aliprandi@st.com | 33:8daea0279301 | 533 | do |
davide.aliprandi@st.com | 33:8daea0279301 | 534 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 535 | spi_preemtion_by_isr = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 536 | if (itDisable) |
davide.aliprandi@st.com | 33:8daea0279301 | 537 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 538 | /* re-enable L6474_EnableIrq if disable in previous iteration */ |
davide.aliprandi@st.com | 33:8daea0279301 | 539 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 540 | itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 541 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 542 | |
davide.aliprandi@st.com | 33:8daea0279301 | 543 | for (i = 0; i < number_of_devices; i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 544 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 545 | spi_tx_bursts[0][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 546 | spi_tx_bursts[1][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 547 | spi_tx_bursts[2][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 548 | spi_tx_bursts[3][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 549 | spi_rx_bursts[1][i] = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 550 | spi_rx_bursts[2][i] = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 551 | spi_rx_bursts[3][i] = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 552 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 553 | |
davide.aliprandi@st.com | 33:8daea0279301 | 554 | switch (parameter) |
davide.aliprandi@st.com | 33:8daea0279301 | 555 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 556 | case L6474_ABS_POS: ; |
davide.aliprandi@st.com | 33:8daea0279301 | 557 | case L6474_MARK: |
davide.aliprandi@st.com | 33:8daea0279301 | 558 | spi_tx_bursts[0][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); |
davide.aliprandi@st.com | 33:8daea0279301 | 559 | maxArgumentNbBytes = 3; |
davide.aliprandi@st.com | 33:8daea0279301 | 560 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 561 | case L6474_EL_POS: ; |
davide.aliprandi@st.com | 33:8daea0279301 | 562 | case L6474_CONFIG: ; |
davide.aliprandi@st.com | 33:8daea0279301 | 563 | case L6474_STATUS: |
davide.aliprandi@st.com | 33:8daea0279301 | 564 | spi_tx_bursts[1][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); |
davide.aliprandi@st.com | 33:8daea0279301 | 565 | maxArgumentNbBytes = 2; |
davide.aliprandi@st.com | 33:8daea0279301 | 566 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 567 | default: |
davide.aliprandi@st.com | 33:8daea0279301 | 568 | spi_tx_bursts[2][spiIndex] = ((uint8_t)L6474_GET_PARAM )| (parameter); |
davide.aliprandi@st.com | 33:8daea0279301 | 569 | maxArgumentNbBytes = 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 570 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 571 | |
davide.aliprandi@st.com | 33:8daea0279301 | 572 | /* Disable interruption before checking */ |
davide.aliprandi@st.com | 33:8daea0279301 | 573 | /* pre-emption by ISR and SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 574 | L6474_DisableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 575 | itDisable = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 576 | } while (spi_preemtion_by_isr); // check pre-emption by ISR |
davide.aliprandi@st.com | 33:8daea0279301 | 577 | |
davide.aliprandi@st.com | 33:8daea0279301 | 578 | for (i = L6474_CMD_ARG_MAX_NB_BYTES-1-maxArgumentNbBytes; |
davide.aliprandi@st.com | 33:8daea0279301 | 579 | i < L6474_CMD_ARG_MAX_NB_BYTES; |
davide.aliprandi@st.com | 33:8daea0279301 | 580 | i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 581 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 582 | L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); |
davide.aliprandi@st.com | 33:8daea0279301 | 583 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 584 | |
davide.aliprandi@st.com | 33:8daea0279301 | 585 | spiRxData = ((uint32_t)spi_rx_bursts[1][spiIndex] << 16) | |
davide.aliprandi@st.com | 33:8daea0279301 | 586 | (spi_rx_bursts[2][spiIndex] << 8) | |
davide.aliprandi@st.com | 33:8daea0279301 | 587 | (spi_rx_bursts[3][spiIndex]); |
davide.aliprandi@st.com | 33:8daea0279301 | 588 | |
davide.aliprandi@st.com | 33:8daea0279301 | 589 | /* re-enable L6474_EnableIrq after SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 590 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 591 | |
davide.aliprandi@st.com | 33:8daea0279301 | 592 | return (spiRxData); |
davide.aliprandi@st.com | 33:8daea0279301 | 593 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 594 | |
davide.aliprandi@st.com | 33:8daea0279301 | 595 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 596 | * @brief Issues the GetStatus command to the L6474 of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 597 | * @retval Status Register value |
davide.aliprandi@st.com | 33:8daea0279301 | 598 | * @note Once the GetStatus command is performed, the flags of the status register |
davide.aliprandi@st.com | 33:8daea0279301 | 599 | * are reset. This is not the case when the status register is read with the |
davide.aliprandi@st.com | 33:8daea0279301 | 600 | * GetParam command (via the functions L6474ReadStatusRegister or L6474_CmdGetParam). |
davide.aliprandi@st.com | 33:8daea0279301 | 601 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 602 | uint16_t L6474::L6474_CmdGetStatus(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 603 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 604 | uint32_t i; |
davide.aliprandi@st.com | 33:8daea0279301 | 605 | uint16_t status; |
davide.aliprandi@st.com | 33:8daea0279301 | 606 | uint8_t spiIndex = number_of_devices - device_instance - 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 607 | bool itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 608 | |
davide.aliprandi@st.com | 33:8daea0279301 | 609 | do |
davide.aliprandi@st.com | 33:8daea0279301 | 610 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 611 | spi_preemtion_by_isr = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 612 | if (itDisable) |
davide.aliprandi@st.com | 33:8daea0279301 | 613 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 614 | /* re-enable L6474_EnableIrq if disable in previous iteration */ |
davide.aliprandi@st.com | 33:8daea0279301 | 615 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 616 | itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 617 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 618 | |
davide.aliprandi@st.com | 33:8daea0279301 | 619 | for (i = 0; i < number_of_devices; i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 620 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 621 | spi_tx_bursts[0][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 622 | spi_tx_bursts[1][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 623 | spi_tx_bursts[2][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 624 | spi_rx_bursts[1][i] = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 625 | spi_rx_bursts[2][i] = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 626 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 627 | spi_tx_bursts[0][spiIndex] = L6474_GET_STATUS; |
davide.aliprandi@st.com | 33:8daea0279301 | 628 | |
davide.aliprandi@st.com | 33:8daea0279301 | 629 | /* Disable interruption before checking */ |
davide.aliprandi@st.com | 33:8daea0279301 | 630 | /* pre-emption by ISR and SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 631 | L6474_DisableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 632 | itDisable = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 633 | } while (spi_preemtion_by_isr); // check pre-emption by ISR |
davide.aliprandi@st.com | 33:8daea0279301 | 634 | |
davide.aliprandi@st.com | 33:8daea0279301 | 635 | for (i = 0; i < L6474_CMD_ARG_NB_BYTES_GET_STATUS + L6474_RSP_NB_BYTES_GET_STATUS; i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 636 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 637 | L6474_WriteBytes(&spi_tx_bursts[i][0], &spi_rx_bursts[i][0]); |
davide.aliprandi@st.com | 33:8daea0279301 | 638 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 639 | status = (spi_rx_bursts[1][spiIndex] << 8) | (spi_rx_bursts[2][spiIndex]); |
davide.aliprandi@st.com | 33:8daea0279301 | 640 | |
davide.aliprandi@st.com | 33:8daea0279301 | 641 | /* re-enable L6474_EnableIrq after SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 642 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 643 | |
davide.aliprandi@st.com | 33:8daea0279301 | 644 | return (status); |
davide.aliprandi@st.com | 33:8daea0279301 | 645 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 646 | |
davide.aliprandi@st.com | 33:8daea0279301 | 647 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 648 | * @brief Issues the Nop command to the L6474 of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 649 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 650 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 651 | void L6474::L6474_CmdNop(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 652 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 653 | L6474_SendCommand(L6474_NOP); |
davide.aliprandi@st.com | 33:8daea0279301 | 654 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 655 | |
davide.aliprandi@st.com | 33:8daea0279301 | 656 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 657 | * @brief Issues the SetParam command to the L6474 of the specified device |
davide.aliprandi@st.com | 33:8daea0279301 | 658 | * @param[in] parameter Register adress (L6474_ABS_POS, L6474_MARK,...) |
davide.aliprandi@st.com | 33:8daea0279301 | 659 | * @param[in] value Value to set in the register |
davide.aliprandi@st.com | 33:8daea0279301 | 660 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 661 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 662 | void L6474::L6474_CmdSetParam(L6474_Registers_t parameter, uint32_t value) |
davide.aliprandi@st.com | 33:8daea0279301 | 663 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 664 | uint32_t i; |
davide.aliprandi@st.com | 33:8daea0279301 | 665 | uint8_t maxArgumentNbBytes = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 666 | uint8_t spiIndex = number_of_devices - device_instance - 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 667 | bool itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 668 | do |
davide.aliprandi@st.com | 33:8daea0279301 | 669 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 670 | spi_preemtion_by_isr = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 671 | if (itDisable) |
davide.aliprandi@st.com | 33:8daea0279301 | 672 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 673 | /* re-enable L6474_EnableIrq if disable in previous iteration */ |
davide.aliprandi@st.com | 33:8daea0279301 | 674 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 675 | itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 676 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 677 | |
davide.aliprandi@st.com | 33:8daea0279301 | 678 | for (i = 0; i < number_of_devices; i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 679 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 680 | spi_tx_bursts[0][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 681 | spi_tx_bursts[1][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 682 | spi_tx_bursts[2][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 683 | spi_tx_bursts[3][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 684 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 685 | |
davide.aliprandi@st.com | 33:8daea0279301 | 686 | switch (parameter) |
davide.aliprandi@st.com | 33:8daea0279301 | 687 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 688 | case L6474_ABS_POS: ; |
davide.aliprandi@st.com | 33:8daea0279301 | 689 | case L6474_MARK: |
davide.aliprandi@st.com | 33:8daea0279301 | 690 | spi_tx_bursts[0][spiIndex] = parameter; |
davide.aliprandi@st.com | 33:8daea0279301 | 691 | spi_tx_bursts[1][spiIndex] = (uint8_t)(value >> 16); |
davide.aliprandi@st.com | 33:8daea0279301 | 692 | spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); |
davide.aliprandi@st.com | 33:8daea0279301 | 693 | maxArgumentNbBytes = 3; |
davide.aliprandi@st.com | 33:8daea0279301 | 694 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 695 | case L6474_EL_POS: ; |
davide.aliprandi@st.com | 33:8daea0279301 | 696 | case L6474_CONFIG: |
davide.aliprandi@st.com | 33:8daea0279301 | 697 | spi_tx_bursts[1][spiIndex] = parameter; |
davide.aliprandi@st.com | 33:8daea0279301 | 698 | spi_tx_bursts[2][spiIndex] = (uint8_t)(value >> 8); |
davide.aliprandi@st.com | 33:8daea0279301 | 699 | maxArgumentNbBytes = 2; |
davide.aliprandi@st.com | 33:8daea0279301 | 700 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 701 | default: |
davide.aliprandi@st.com | 33:8daea0279301 | 702 | spi_tx_bursts[2][spiIndex] = parameter; |
davide.aliprandi@st.com | 33:8daea0279301 | 703 | maxArgumentNbBytes = 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 704 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 705 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 706 | spi_tx_bursts[3][spiIndex] = (uint8_t)(value); |
davide.aliprandi@st.com | 33:8daea0279301 | 707 | |
davide.aliprandi@st.com | 33:8daea0279301 | 708 | /* Disable interruption before checking */ |
davide.aliprandi@st.com | 33:8daea0279301 | 709 | /* pre-emption by ISR and SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 710 | L6474_DisableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 711 | itDisable = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 712 | } while (spi_preemtion_by_isr); // check pre-emption by ISR |
davide.aliprandi@st.com | 33:8daea0279301 | 713 | |
davide.aliprandi@st.com | 33:8daea0279301 | 714 | /* SPI transfer */ |
davide.aliprandi@st.com | 33:8daea0279301 | 715 | for (i = L6474_CMD_ARG_MAX_NB_BYTES-1-maxArgumentNbBytes; |
davide.aliprandi@st.com | 33:8daea0279301 | 716 | i < L6474_CMD_ARG_MAX_NB_BYTES; |
davide.aliprandi@st.com | 33:8daea0279301 | 717 | i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 718 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 719 | L6474_WriteBytes(&spi_tx_bursts[i][0],&spi_rx_bursts[i][0]); |
davide.aliprandi@st.com | 33:8daea0279301 | 720 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 721 | /* re-enable L6474_EnableIrq after SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 722 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 723 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 724 | |
davide.aliprandi@st.com | 33:8daea0279301 | 725 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 726 | * @brief Reads the Status Register value |
davide.aliprandi@st.com | 33:8daea0279301 | 727 | * @retval Status register valued |
davide.aliprandi@st.com | 33:8daea0279301 | 728 | * @note The status register flags are not cleared |
davide.aliprandi@st.com | 33:8daea0279301 | 729 | * at the difference with L6474CmdGetStatus() |
davide.aliprandi@st.com | 33:8daea0279301 | 730 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 731 | uint16_t L6474::L6474_ReadStatusRegister(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 732 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 733 | return (L6474_CmdGetParam(L6474_STATUS)); |
davide.aliprandi@st.com | 33:8daea0279301 | 734 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 735 | |
davide.aliprandi@st.com | 33:8daea0279301 | 736 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 737 | * @brief Set the stepping mode |
davide.aliprandi@st.com | 33:8daea0279301 | 738 | * @param[in] stepMod from full step to 1/16 microstep as specified in enum motorStepMode_t |
davide.aliprandi@st.com | 33:8daea0279301 | 739 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 740 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 741 | void L6474::L6474_SelectStepMode(motorStepMode_t stepMod) |
davide.aliprandi@st.com | 33:8daea0279301 | 742 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 743 | uint8_t stepModeRegister; |
davide.aliprandi@st.com | 33:8daea0279301 | 744 | L6474_STEP_SEL_t l6474StepMod; |
davide.aliprandi@st.com | 33:8daea0279301 | 745 | |
davide.aliprandi@st.com | 33:8daea0279301 | 746 | switch (stepMod) |
davide.aliprandi@st.com | 33:8daea0279301 | 747 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 748 | case STEP_MODE_FULL: |
davide.aliprandi@st.com | 33:8daea0279301 | 749 | l6474StepMod = L6474_STEP_SEL_1; |
davide.aliprandi@st.com | 33:8daea0279301 | 750 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 751 | case STEP_MODE_HALF: |
davide.aliprandi@st.com | 33:8daea0279301 | 752 | l6474StepMod = L6474_STEP_SEL_1_2; |
davide.aliprandi@st.com | 33:8daea0279301 | 753 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 754 | case STEP_MODE_1_4: |
davide.aliprandi@st.com | 33:8daea0279301 | 755 | l6474StepMod = L6474_STEP_SEL_1_4; |
davide.aliprandi@st.com | 33:8daea0279301 | 756 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 757 | case STEP_MODE_1_8: |
davide.aliprandi@st.com | 33:8daea0279301 | 758 | l6474StepMod = L6474_STEP_SEL_1_8; |
davide.aliprandi@st.com | 33:8daea0279301 | 759 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 760 | case STEP_MODE_1_16: |
davide.aliprandi@st.com | 33:8daea0279301 | 761 | default: |
davide.aliprandi@st.com | 33:8daea0279301 | 762 | l6474StepMod = L6474_STEP_SEL_1_16; |
davide.aliprandi@st.com | 33:8daea0279301 | 763 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 764 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 765 | |
davide.aliprandi@st.com | 33:8daea0279301 | 766 | /* Eventually deactivate motor */ |
davide.aliprandi@st.com | 33:8daea0279301 | 767 | if (device_prm.motionState != INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 768 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 769 | L6474_HardStop(); |
davide.aliprandi@st.com | 33:8daea0279301 | 770 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 771 | |
davide.aliprandi@st.com | 33:8daea0279301 | 772 | /* Read Step mode register and clear STEP_SEL field */ |
davide.aliprandi@st.com | 33:8daea0279301 | 773 | stepModeRegister = (uint8_t)(0xF8 & L6474_CmdGetParam(L6474_STEP_MODE)) ; |
davide.aliprandi@st.com | 33:8daea0279301 | 774 | |
davide.aliprandi@st.com | 33:8daea0279301 | 775 | /* Apply new step mode */ |
davide.aliprandi@st.com | 33:8daea0279301 | 776 | L6474_CmdSetParam(L6474_STEP_MODE, stepModeRegister | (uint8_t)l6474StepMod); |
davide.aliprandi@st.com | 33:8daea0279301 | 777 | |
davide.aliprandi@st.com | 33:8daea0279301 | 778 | /* Reset abs pos register */ |
davide.aliprandi@st.com | 33:8daea0279301 | 779 | L6474_SetHome(); |
davide.aliprandi@st.com | 33:8daea0279301 | 780 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 781 | |
davide.aliprandi@st.com | 33:8daea0279301 | 782 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 783 | * @brief Get the direction |
davide.aliprandi@st.com | 33:8daea0279301 | 784 | * @param None |
davide.aliprandi@st.com | 33:8daea0279301 | 785 | * @retval direction FORWARD or BACKWARD |
davide.aliprandi@st.com | 33:8daea0279301 | 786 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 787 | motorDir_t L6474::L6474_GetDirection(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 788 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 789 | return device_prm.direction; |
davide.aliprandi@st.com | 33:8daea0279301 | 790 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 791 | |
davide.aliprandi@st.com | 33:8daea0279301 | 792 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 793 | * @brief Specifies the direction |
davide.aliprandi@st.com | 33:8daea0279301 | 794 | * @param[in] dir FORWARD or BACKWARD |
davide.aliprandi@st.com | 33:8daea0279301 | 795 | * @note The direction change is only applied if the device |
davide.aliprandi@st.com | 33:8daea0279301 | 796 | * is in INACTIVE state |
davide.aliprandi@st.com | 33:8daea0279301 | 797 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 798 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 799 | void L6474::L6474_SetDirection(motorDir_t direction) |
davide.aliprandi@st.com | 33:8daea0279301 | 800 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 801 | if (device_prm.motionState == INACTIVE) |
davide.aliprandi@st.com | 33:8daea0279301 | 802 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 803 | device_prm.direction = direction; |
davide.aliprandi@st.com | 33:8daea0279301 | 804 | L6474_SetDirectionGpio(direction); |
davide.aliprandi@st.com | 33:8daea0279301 | 805 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 806 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 807 | |
davide.aliprandi@st.com | 33:8daea0279301 | 808 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 809 | * @brief Updates the current speed of the device |
davide.aliprandi@st.com | 33:8daea0279301 | 810 | * @param[in] newSpeed in pps |
davide.aliprandi@st.com | 33:8daea0279301 | 811 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 812 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 813 | void L6474::L6474_ApplySpeed(uint16_t newSpeed) |
davide.aliprandi@st.com | 33:8daea0279301 | 814 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 815 | if (newSpeed < L6474_MIN_PWM_FREQ) |
davide.aliprandi@st.com | 33:8daea0279301 | 816 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 817 | newSpeed = L6474_MIN_PWM_FREQ; |
davide.aliprandi@st.com | 33:8daea0279301 | 818 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 819 | if (newSpeed > L6474_MAX_PWM_FREQ) |
davide.aliprandi@st.com | 33:8daea0279301 | 820 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 821 | newSpeed = L6474_MAX_PWM_FREQ; |
davide.aliprandi@st.com | 33:8daea0279301 | 822 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 823 | |
davide.aliprandi@st.com | 33:8daea0279301 | 824 | device_prm.speed = newSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 825 | |
davide.aliprandi@st.com | 33:8daea0279301 | 826 | L6474_PwmSetFreq(newSpeed); |
davide.aliprandi@st.com | 33:8daea0279301 | 827 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 828 | |
davide.aliprandi@st.com | 33:8daea0279301 | 829 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 830 | * @brief Computes the speed profile according to the number of steps to move |
davide.aliprandi@st.com | 33:8daea0279301 | 831 | * @param[in] nbSteps number of steps to perform |
davide.aliprandi@st.com | 33:8daea0279301 | 832 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 833 | * @note Using the acceleration and deceleration of the device, |
davide.aliprandi@st.com | 33:8daea0279301 | 834 | * this function determines the duration in steps of the acceleration, |
davide.aliprandi@st.com | 33:8daea0279301 | 835 | * steady and deceleration phases. |
davide.aliprandi@st.com | 33:8daea0279301 | 836 | * If the total number of steps to perform is big enough, a trapezoidal move |
davide.aliprandi@st.com | 33:8daea0279301 | 837 | * is performed (i.e. there is a steady phase where the motor runs at the maximum |
davide.aliprandi@st.com | 33:8daea0279301 | 838 | * speed. |
davide.aliprandi@st.com | 33:8daea0279301 | 839 | * Else, a triangular move is performed (no steady phase: the maximum speed is never |
davide.aliprandi@st.com | 33:8daea0279301 | 840 | * reached. |
davide.aliprandi@st.com | 33:8daea0279301 | 841 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 842 | void L6474::L6474_ComputeSpeedProfile(uint32_t nbSteps) |
davide.aliprandi@st.com | 33:8daea0279301 | 843 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 844 | uint32_t reqAccSteps; |
davide.aliprandi@st.com | 33:8daea0279301 | 845 | uint32_t reqDecSteps; |
davide.aliprandi@st.com | 33:8daea0279301 | 846 | |
davide.aliprandi@st.com | 33:8daea0279301 | 847 | /* compute the number of steps to get the targeted speed */ |
davide.aliprandi@st.com | 33:8daea0279301 | 848 | uint16_t minSpeed = device_prm.minSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 849 | reqAccSteps = (device_prm.maxSpeed - minSpeed); |
davide.aliprandi@st.com | 33:8daea0279301 | 850 | reqAccSteps *= (device_prm.maxSpeed + minSpeed); |
davide.aliprandi@st.com | 33:8daea0279301 | 851 | reqDecSteps = reqAccSteps; |
davide.aliprandi@st.com | 33:8daea0279301 | 852 | reqAccSteps /= (uint32_t)device_prm.acceleration; |
davide.aliprandi@st.com | 33:8daea0279301 | 853 | reqAccSteps /= 2; |
davide.aliprandi@st.com | 33:8daea0279301 | 854 | |
davide.aliprandi@st.com | 33:8daea0279301 | 855 | /* compute the number of steps to stop */ |
davide.aliprandi@st.com | 33:8daea0279301 | 856 | reqDecSteps /= (uint32_t)device_prm.deceleration; |
davide.aliprandi@st.com | 33:8daea0279301 | 857 | reqDecSteps /= 2; |
davide.aliprandi@st.com | 33:8daea0279301 | 858 | |
davide.aliprandi@st.com | 33:8daea0279301 | 859 | if(( reqAccSteps + reqDecSteps ) > nbSteps) |
davide.aliprandi@st.com | 33:8daea0279301 | 860 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 861 | /* Triangular move */ |
davide.aliprandi@st.com | 33:8daea0279301 | 862 | /* reqDecSteps = (Pos * Dec) /(Dec+Acc) */ |
davide.aliprandi@st.com | 33:8daea0279301 | 863 | uint32_t dec = device_prm.deceleration; |
davide.aliprandi@st.com | 33:8daea0279301 | 864 | uint32_t acc = device_prm.acceleration; |
davide.aliprandi@st.com | 33:8daea0279301 | 865 | |
davide.aliprandi@st.com | 33:8daea0279301 | 866 | reqDecSteps = ((uint32_t) dec * nbSteps) / (acc + dec); |
davide.aliprandi@st.com | 33:8daea0279301 | 867 | if (reqDecSteps > 1) |
davide.aliprandi@st.com | 33:8daea0279301 | 868 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 869 | reqAccSteps = reqDecSteps - 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 870 | if(reqAccSteps == 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 871 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 872 | reqAccSteps = 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 873 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 874 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 875 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 876 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 877 | reqAccSteps = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 878 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 879 | device_prm.endAccPos = reqAccSteps; |
davide.aliprandi@st.com | 33:8daea0279301 | 880 | device_prm.startDecPos = reqDecSteps; |
davide.aliprandi@st.com | 33:8daea0279301 | 881 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 882 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 883 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 884 | /* Trapezoidal move */ |
davide.aliprandi@st.com | 33:8daea0279301 | 885 | /* accelerating phase to endAccPos */ |
davide.aliprandi@st.com | 33:8daea0279301 | 886 | /* steady phase from endAccPos to startDecPos */ |
davide.aliprandi@st.com | 33:8daea0279301 | 887 | /* decelerating from startDecPos to stepsToTake*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 888 | device_prm.endAccPos = reqAccSteps; |
davide.aliprandi@st.com | 33:8daea0279301 | 889 | device_prm.startDecPos = nbSteps - reqDecSteps - 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 890 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 891 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 892 | |
davide.aliprandi@st.com | 33:8daea0279301 | 893 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 894 | * @brief Converts the ABS_POSITION register value to a 32b signed integer |
davide.aliprandi@st.com | 33:8daea0279301 | 895 | * @param[in] abs_position_reg value of the ABS_POSITION register |
davide.aliprandi@st.com | 33:8daea0279301 | 896 | * @retval operation_result 32b signed integer corresponding to the absolute position |
davide.aliprandi@st.com | 33:8daea0279301 | 897 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 898 | int32_t L6474::L6474_ConvertPosition(uint32_t abs_position_reg) |
davide.aliprandi@st.com | 33:8daea0279301 | 899 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 900 | int32_t operation_result; |
davide.aliprandi@st.com | 33:8daea0279301 | 901 | |
davide.aliprandi@st.com | 33:8daea0279301 | 902 | if (abs_position_reg & L6474_ABS_POS_SIGN_BIT_MASK) |
davide.aliprandi@st.com | 33:8daea0279301 | 903 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 904 | /* Negative register value */ |
davide.aliprandi@st.com | 33:8daea0279301 | 905 | abs_position_reg = ~abs_position_reg; |
davide.aliprandi@st.com | 33:8daea0279301 | 906 | abs_position_reg += 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 907 | |
davide.aliprandi@st.com | 33:8daea0279301 | 908 | operation_result = (int32_t) (abs_position_reg & L6474_ABS_POS_VALUE_MASK); |
davide.aliprandi@st.com | 33:8daea0279301 | 909 | operation_result = -operation_result; |
davide.aliprandi@st.com | 33:8daea0279301 | 910 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 911 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 912 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 913 | operation_result = (int32_t) abs_position_reg; |
davide.aliprandi@st.com | 33:8daea0279301 | 914 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 915 | |
davide.aliprandi@st.com | 33:8daea0279301 | 916 | return operation_result; |
davide.aliprandi@st.com | 33:8daea0279301 | 917 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 918 | |
davide.aliprandi@st.com | 33:8daea0279301 | 919 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 920 | * @brief Error handler which calls the user callback (if defined) |
davide.aliprandi@st.com | 33:8daea0279301 | 921 | * @param[in] error Number of the error |
davide.aliprandi@st.com | 33:8daea0279301 | 922 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 923 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 924 | void L6474::L6474_ErrorHandler(uint16_t error) |
davide.aliprandi@st.com | 33:8daea0279301 | 925 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 926 | if (error_handler_callback != 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 927 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 928 | (void) error_handler_callback(error); |
davide.aliprandi@st.com | 33:8daea0279301 | 929 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 930 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 931 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 932 | /* Aborting the program. */ |
davide.aliprandi@st.com | 33:8daea0279301 | 933 | exit(EXIT_FAILURE); |
davide.aliprandi@st.com | 33:8daea0279301 | 934 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 935 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 936 | |
davide.aliprandi@st.com | 33:8daea0279301 | 937 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 938 | * @brief Sends a command without arguments to the L6474 via the SPI |
davide.aliprandi@st.com | 33:8daea0279301 | 939 | * @param[in] param Command to send |
davide.aliprandi@st.com | 33:8daea0279301 | 940 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 941 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 942 | void L6474::L6474_SendCommand(uint8_t param) |
davide.aliprandi@st.com | 33:8daea0279301 | 943 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 944 | uint32_t i; |
davide.aliprandi@st.com | 33:8daea0279301 | 945 | bool itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 946 | uint8_t spiIndex = number_of_devices - device_instance - 1; |
davide.aliprandi@st.com | 33:8daea0279301 | 947 | |
davide.aliprandi@st.com | 33:8daea0279301 | 948 | do |
davide.aliprandi@st.com | 33:8daea0279301 | 949 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 950 | spi_preemtion_by_isr = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 951 | if (itDisable) |
davide.aliprandi@st.com | 33:8daea0279301 | 952 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 953 | /* re-enable L6474_EnableIrq if disable in previous iteration */ |
davide.aliprandi@st.com | 33:8daea0279301 | 954 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 955 | itDisable = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 956 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 957 | |
davide.aliprandi@st.com | 33:8daea0279301 | 958 | for (i = 0; i < number_of_devices; i++) |
davide.aliprandi@st.com | 33:8daea0279301 | 959 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 960 | spi_tx_bursts[3][i] = L6474_NOP; |
davide.aliprandi@st.com | 33:8daea0279301 | 961 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 962 | spi_tx_bursts[3][spiIndex] = param; |
davide.aliprandi@st.com | 33:8daea0279301 | 963 | |
davide.aliprandi@st.com | 33:8daea0279301 | 964 | /* Disable interruption before checking */ |
davide.aliprandi@st.com | 33:8daea0279301 | 965 | /* pre-emption by ISR and SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 966 | L6474_DisableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 967 | itDisable = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 968 | } while (spi_preemtion_by_isr); // check pre-emption by ISR |
davide.aliprandi@st.com | 33:8daea0279301 | 969 | |
davide.aliprandi@st.com | 33:8daea0279301 | 970 | L6474_WriteBytes(&spi_tx_bursts[3][0], &spi_rx_bursts[3][0]); |
davide.aliprandi@st.com | 33:8daea0279301 | 971 | |
davide.aliprandi@st.com | 33:8daea0279301 | 972 | /* re-enable L6474_EnableIrq after SPI transfers*/ |
davide.aliprandi@st.com | 33:8daea0279301 | 973 | L6474_EnableIrq(); |
davide.aliprandi@st.com | 33:8daea0279301 | 974 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 975 | |
davide.aliprandi@st.com | 33:8daea0279301 | 976 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 977 | * @brief Sets the registers of the L6474 to their predefined values |
davide.aliprandi@st.com | 33:8daea0279301 | 978 | * from l6474_target_config.h |
davide.aliprandi@st.com | 33:8daea0279301 | 979 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 980 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 981 | void L6474::L6474_SetRegisterToPredefinedValues(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 982 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 983 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 984 | L6474_ABS_POS, |
davide.aliprandi@st.com | 33:8daea0279301 | 985 | 0); |
davide.aliprandi@st.com | 33:8daea0279301 | 986 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 987 | L6474_EL_POS, |
davide.aliprandi@st.com | 33:8daea0279301 | 988 | 0); |
davide.aliprandi@st.com | 33:8daea0279301 | 989 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 990 | L6474_MARK, |
davide.aliprandi@st.com | 33:8daea0279301 | 991 | 0); |
davide.aliprandi@st.com | 33:8daea0279301 | 992 | switch (device_instance) |
davide.aliprandi@st.com | 33:8daea0279301 | 993 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 994 | case 0: |
davide.aliprandi@st.com | 33:8daea0279301 | 995 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 996 | L6474_TVAL, |
davide.aliprandi@st.com | 33:8daea0279301 | 997 | L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_0)); |
davide.aliprandi@st.com | 33:8daea0279301 | 998 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 999 | L6474_T_FAST, |
davide.aliprandi@st.com | 33:8daea0279301 | 1000 | (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_0 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1001 | (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_0); |
davide.aliprandi@st.com | 33:8daea0279301 | 1002 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1003 | L6474_TON_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1004 | L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_0) |
davide.aliprandi@st.com | 33:8daea0279301 | 1005 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1006 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1007 | L6474_TOFF_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1008 | L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_0)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1009 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1010 | L6474_OCD_TH, |
davide.aliprandi@st.com | 33:8daea0279301 | 1011 | L6474_CONF_PARAM_OCD_TH_DEVICE_0); |
davide.aliprandi@st.com | 33:8daea0279301 | 1012 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1013 | L6474_STEP_MODE, |
davide.aliprandi@st.com | 33:8daea0279301 | 1014 | (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_0 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1015 | (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_0); |
davide.aliprandi@st.com | 33:8daea0279301 | 1016 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1017 | L6474_ALARM_EN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1018 | L6474_CONF_PARAM_ALARM_EN_DEVICE_0); |
davide.aliprandi@st.com | 33:8daea0279301 | 1019 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1020 | L6474_CONFIG, |
davide.aliprandi@st.com | 33:8daea0279301 | 1021 | (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_0 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1022 | (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_0 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1023 | (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_0 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1024 | (uint16_t)L6474_CONF_PARAM_SR_DEVICE_0 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1025 | (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_0); |
davide.aliprandi@st.com | 33:8daea0279301 | 1026 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1027 | case 1: |
davide.aliprandi@st.com | 33:8daea0279301 | 1028 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1029 | L6474_TVAL, |
davide.aliprandi@st.com | 33:8daea0279301 | 1030 | L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_1)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1031 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1032 | L6474_T_FAST, |
davide.aliprandi@st.com | 33:8daea0279301 | 1033 | (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_1 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1034 | (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_1); |
davide.aliprandi@st.com | 33:8daea0279301 | 1035 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1036 | L6474_TON_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1037 | L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_1)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1038 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1039 | L6474_TOFF_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1040 | L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_1)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1041 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1042 | L6474_OCD_TH, |
davide.aliprandi@st.com | 33:8daea0279301 | 1043 | L6474_CONF_PARAM_OCD_TH_DEVICE_1); |
davide.aliprandi@st.com | 33:8daea0279301 | 1044 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1045 | L6474_STEP_MODE, |
davide.aliprandi@st.com | 33:8daea0279301 | 1046 | (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_1 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1047 | (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_1); |
davide.aliprandi@st.com | 33:8daea0279301 | 1048 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1049 | L6474_ALARM_EN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1050 | L6474_CONF_PARAM_ALARM_EN_DEVICE_1); |
davide.aliprandi@st.com | 33:8daea0279301 | 1051 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1052 | L6474_CONFIG, |
davide.aliprandi@st.com | 33:8daea0279301 | 1053 | (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_1 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1054 | (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_1 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1055 | (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_1 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1056 | (uint16_t)L6474_CONF_PARAM_SR_DEVICE_1 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1057 | (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_1); |
davide.aliprandi@st.com | 33:8daea0279301 | 1058 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1059 | case 2: |
davide.aliprandi@st.com | 33:8daea0279301 | 1060 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1061 | L6474_TVAL, |
davide.aliprandi@st.com | 33:8daea0279301 | 1062 | L6474_Tval_Current_to_Par(L6474_CONF_PARAM_TVAL_DEVICE_2)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1063 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1064 | L6474_T_FAST, |
davide.aliprandi@st.com | 33:8daea0279301 | 1065 | (uint8_t)L6474_CONF_PARAM_TOFF_FAST_DEVICE_2 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1066 | (uint8_t)L6474_CONF_PARAM_FAST_STEP_DEVICE_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1067 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1068 | L6474_TON_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1069 | L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TON_MIN_DEVICE_2)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1070 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1071 | L6474_TOFF_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1072 | L6474_Tmin_Time_to_Par(L6474_CONF_PARAM_TOFF_MIN_DEVICE_2)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1073 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1074 | L6474_OCD_TH, |
davide.aliprandi@st.com | 33:8daea0279301 | 1075 | L6474_CONF_PARAM_OCD_TH_DEVICE_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1076 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1077 | L6474_STEP_MODE, |
davide.aliprandi@st.com | 33:8daea0279301 | 1078 | (uint8_t)L6474_CONF_PARAM_STEP_SEL_DEVICE_2 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1079 | (uint8_t)L6474_CONF_PARAM_SYNC_SEL_DEVICE_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1080 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1081 | L6474_ALARM_EN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1082 | L6474_CONF_PARAM_ALARM_EN_DEVICE_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1083 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1084 | L6474_CONFIG, |
davide.aliprandi@st.com | 33:8daea0279301 | 1085 | (uint16_t)L6474_CONF_PARAM_CLOCK_SETTING_DEVICE_2 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1086 | (uint16_t)L6474_CONF_PARAM_TQ_REG_DEVICE_2 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1087 | (uint16_t)L6474_CONF_PARAM_OC_SD_DEVICE_2 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1088 | (uint16_t)L6474_CONF_PARAM_SR_DEVICE_2 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1089 | (uint16_t)L6474_CONF_PARAM_TOFF_DEVICE_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1090 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1091 | default: ; |
davide.aliprandi@st.com | 33:8daea0279301 | 1092 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1093 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1094 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1095 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1096 | * @brief Sets the registers of the L6474 to initialization values. |
davide.aliprandi@st.com | 33:8daea0279301 | 1097 | * @param init Initialization structure. |
davide.aliprandi@st.com | 33:8daea0279301 | 1098 | * @retval None. |
davide.aliprandi@st.com | 33:8daea0279301 | 1099 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1100 | void L6474::L6474_SetRegisterToInitializationValues(L6474_init_t *init) |
davide.aliprandi@st.com | 33:8daea0279301 | 1101 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1102 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1103 | L6474_ABS_POS, |
davide.aliprandi@st.com | 33:8daea0279301 | 1104 | 0 |
davide.aliprandi@st.com | 33:8daea0279301 | 1105 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1106 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1107 | L6474_EL_POS, |
davide.aliprandi@st.com | 33:8daea0279301 | 1108 | 0 |
davide.aliprandi@st.com | 33:8daea0279301 | 1109 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1110 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1111 | L6474_MARK, |
davide.aliprandi@st.com | 33:8daea0279301 | 1112 | 0 |
davide.aliprandi@st.com | 33:8daea0279301 | 1113 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1114 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1115 | L6474_TVAL, |
davide.aliprandi@st.com | 33:8daea0279301 | 1116 | L6474_Tval_Current_to_Par(init->torque_regulation_current_mA) |
davide.aliprandi@st.com | 33:8daea0279301 | 1117 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1118 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1119 | L6474_T_FAST, |
davide.aliprandi@st.com | 33:8daea0279301 | 1120 | (uint8_t) init->maximum_fast_decay_time | |
davide.aliprandi@st.com | 33:8daea0279301 | 1121 | (uint8_t) init->fall_time |
davide.aliprandi@st.com | 33:8daea0279301 | 1122 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1123 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1124 | L6474_TON_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1125 | L6474_Tmin_Time_to_Par(init->minimum_ON_time_us) |
davide.aliprandi@st.com | 33:8daea0279301 | 1126 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1127 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1128 | L6474_TOFF_MIN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1129 | L6474_Tmin_Time_to_Par(init->minimum_OFF_time_us) |
davide.aliprandi@st.com | 33:8daea0279301 | 1130 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1131 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1132 | L6474_OCD_TH, |
davide.aliprandi@st.com | 33:8daea0279301 | 1133 | init->overcurrent_threshold |
davide.aliprandi@st.com | 33:8daea0279301 | 1134 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1135 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1136 | L6474_STEP_MODE, |
davide.aliprandi@st.com | 33:8daea0279301 | 1137 | (uint8_t) init->step_selection | |
davide.aliprandi@st.com | 33:8daea0279301 | 1138 | (uint8_t) init->sync_selection |
davide.aliprandi@st.com | 33:8daea0279301 | 1139 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1140 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1141 | L6474_ALARM_EN, |
davide.aliprandi@st.com | 33:8daea0279301 | 1142 | init->alarm |
davide.aliprandi@st.com | 33:8daea0279301 | 1143 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1144 | L6474_CmdSetParam( |
davide.aliprandi@st.com | 33:8daea0279301 | 1145 | L6474_CONFIG, |
davide.aliprandi@st.com | 33:8daea0279301 | 1146 | (uint16_t) init->clock | |
davide.aliprandi@st.com | 33:8daea0279301 | 1147 | (uint16_t) init->torque_regulation_method | |
davide.aliprandi@st.com | 33:8daea0279301 | 1148 | (uint16_t) init->overcurrent_shutwdown | |
davide.aliprandi@st.com | 33:8daea0279301 | 1149 | (uint16_t) init->slew_rate | |
davide.aliprandi@st.com | 33:8daea0279301 | 1150 | (uint16_t) init->target_swicthing_period |
davide.aliprandi@st.com | 33:8daea0279301 | 1151 | ); |
davide.aliprandi@st.com | 33:8daea0279301 | 1152 | L6474_SetAcceleration((uint16_t) init->acceleration_pps_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1153 | L6474_SetDeceleration((uint16_t) init->deceleration_pps_2); |
davide.aliprandi@st.com | 33:8daea0279301 | 1154 | L6474_SetMaxSpeed((uint16_t) init->maximum_speed_pps); |
davide.aliprandi@st.com | 33:8daea0279301 | 1155 | L6474_SetMinSpeed((uint16_t) init->minimum_speed_pps); |
davide.aliprandi@st.com | 33:8daea0279301 | 1156 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1157 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1158 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1159 | * @brief Sets the parameters of the device to predefined values |
davide.aliprandi@st.com | 33:8daea0279301 | 1160 | * from l6474_target_config.h |
davide.aliprandi@st.com | 33:8daea0279301 | 1161 | * @param None |
davide.aliprandi@st.com | 33:8daea0279301 | 1162 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 1163 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1164 | void L6474::L6474_SetDeviceParamsToPredefinedValues(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 1165 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1166 | switch (device_instance) |
davide.aliprandi@st.com | 33:8daea0279301 | 1167 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1168 | case 0: |
davide.aliprandi@st.com | 33:8daea0279301 | 1169 | device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1170 | device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1171 | device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1172 | device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1173 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1174 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1175 | case 1: |
davide.aliprandi@st.com | 33:8daea0279301 | 1176 | device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1177 | device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1178 | device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1179 | device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1180 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1181 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1182 | case 2: |
davide.aliprandi@st.com | 33:8daea0279301 | 1183 | device_prm.acceleration = L6474_CONF_PARAM_ACC_DEVICE_2; |
davide.aliprandi@st.com | 33:8daea0279301 | 1184 | device_prm.deceleration = L6474_CONF_PARAM_DEC_DEVICE_2; |
davide.aliprandi@st.com | 33:8daea0279301 | 1185 | device_prm.maxSpeed = L6474_CONF_PARAM_MAX_SPEED_DEVICE_2; |
davide.aliprandi@st.com | 33:8daea0279301 | 1186 | device_prm.minSpeed = L6474_CONF_PARAM_MIN_SPEED_DEVICE_2; |
davide.aliprandi@st.com | 33:8daea0279301 | 1187 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1188 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1189 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1190 | device_prm.accu = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1191 | device_prm.currentPosition = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1192 | device_prm.endAccPos = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1193 | device_prm.relativePos = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1194 | device_prm.startDecPos = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1195 | device_prm.stepsToTake = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1196 | device_prm.speed = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1197 | device_prm.commandExecuted = NO_CMD; |
davide.aliprandi@st.com | 33:8daea0279301 | 1198 | device_prm.direction = FORWARD; |
davide.aliprandi@st.com | 33:8daea0279301 | 1199 | device_prm.motionState = INACTIVE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1200 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1201 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1202 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1203 | * @brief Initialises the bridge parameters to start the movement |
davide.aliprandi@st.com | 33:8daea0279301 | 1204 | * and enable the power bridge |
davide.aliprandi@st.com | 33:8daea0279301 | 1205 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 1206 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1207 | void L6474::L6474_StartMovement(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 1208 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1209 | /* Enable L6474 powerstage */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1210 | L6474_CmdEnable(); |
davide.aliprandi@st.com | 33:8daea0279301 | 1211 | if (device_prm.endAccPos != 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 1212 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1213 | device_prm.motionState = ACCELERATING; |
davide.aliprandi@st.com | 33:8daea0279301 | 1214 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1215 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 1216 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1217 | device_prm.motionState = DECELERATING; |
davide.aliprandi@st.com | 33:8daea0279301 | 1218 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1219 | device_prm.accu = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1220 | device_prm.relativePos = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1221 | L6474_ApplySpeed(device_prm.minSpeed); |
davide.aliprandi@st.com | 33:8daea0279301 | 1222 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1223 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1224 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1225 | * @brief Handles the device state machine at each ste |
davide.aliprandi@st.com | 33:8daea0279301 | 1226 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 1227 | * @note Must only be called by the timer ISR |
davide.aliprandi@st.com | 33:8daea0279301 | 1228 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1229 | void L6474::L6474_StepClockHandler(void) |
davide.aliprandi@st.com | 33:8daea0279301 | 1230 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1231 | /* Set isr flag */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1232 | isr_flag = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1233 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1234 | /* Incrementation of the relative position */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1235 | device_prm.relativePos++; |
davide.aliprandi@st.com | 33:8daea0279301 | 1236 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1237 | switch (device_prm.motionState) |
davide.aliprandi@st.com | 33:8daea0279301 | 1238 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1239 | case ACCELERATING: |
davide.aliprandi@st.com | 33:8daea0279301 | 1240 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1241 | uint32_t relPos = device_prm.relativePos; |
davide.aliprandi@st.com | 33:8daea0279301 | 1242 | uint32_t endAccPos = device_prm.endAccPos; |
davide.aliprandi@st.com | 33:8daea0279301 | 1243 | uint16_t speed = device_prm.speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1244 | uint32_t acc = ((uint32_t)device_prm.acceleration << 16); |
davide.aliprandi@st.com | 33:8daea0279301 | 1245 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1246 | if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 1247 | ((device_prm.commandExecuted != RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1248 | (relPos == device_prm.startDecPos))) |
davide.aliprandi@st.com | 33:8daea0279301 | 1249 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1250 | device_prm.motionState = DECELERATING; |
davide.aliprandi@st.com | 33:8daea0279301 | 1251 | device_prm.accu = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1252 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1253 | else if ((speed >= device_prm.maxSpeed)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 1254 | ((device_prm.commandExecuted != RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1255 | (relPos == endAccPos))) |
davide.aliprandi@st.com | 33:8daea0279301 | 1256 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1257 | device_prm.motionState = STEADY; |
davide.aliprandi@st.com | 33:8daea0279301 | 1258 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1259 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 1260 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1261 | bool speedUpdated = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1262 | /* Go on accelerating */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1263 | if (speed == 0) speed =1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1264 | device_prm.accu += acc / speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1265 | while (device_prm.accu >= (0X10000L)) |
davide.aliprandi@st.com | 33:8daea0279301 | 1266 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1267 | device_prm.accu -= (0X10000L); |
davide.aliprandi@st.com | 33:8daea0279301 | 1268 | speed +=1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1269 | speedUpdated = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1270 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1271 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1272 | if (speedUpdated) |
davide.aliprandi@st.com | 33:8daea0279301 | 1273 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1274 | if (speed > device_prm.maxSpeed) |
davide.aliprandi@st.com | 33:8daea0279301 | 1275 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1276 | speed = device_prm.maxSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1277 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1278 | device_prm.speed = speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1279 | L6474_ApplySpeed(device_prm.speed); |
davide.aliprandi@st.com | 33:8daea0279301 | 1280 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1281 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1282 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1283 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1284 | case STEADY: |
davide.aliprandi@st.com | 33:8daea0279301 | 1285 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1286 | uint16_t maxSpeed = device_prm.maxSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1287 | uint32_t relativePos = device_prm.relativePos; |
davide.aliprandi@st.com | 33:8daea0279301 | 1288 | if ((device_prm.commandExecuted == SOFT_STOP_CMD)|| |
davide.aliprandi@st.com | 33:8daea0279301 | 1289 | ((device_prm.commandExecuted != RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1290 | (relativePos >= (device_prm.startDecPos))) || |
davide.aliprandi@st.com | 33:8daea0279301 | 1291 | ((device_prm.commandExecuted == RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1292 | (device_prm.speed > maxSpeed))) |
davide.aliprandi@st.com | 33:8daea0279301 | 1293 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1294 | device_prm.motionState = DECELERATING; |
davide.aliprandi@st.com | 33:8daea0279301 | 1295 | device_prm.accu = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1296 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1297 | else if ((device_prm.commandExecuted == RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1298 | (device_prm.speed < maxSpeed)) |
davide.aliprandi@st.com | 33:8daea0279301 | 1299 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1300 | device_prm.motionState = ACCELERATING; |
davide.aliprandi@st.com | 33:8daea0279301 | 1301 | device_prm.accu = 0; |
davide.aliprandi@st.com | 33:8daea0279301 | 1302 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1303 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1304 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1305 | case DECELERATING: |
davide.aliprandi@st.com | 33:8daea0279301 | 1306 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1307 | uint32_t relativePos = device_prm.relativePos; |
davide.aliprandi@st.com | 33:8daea0279301 | 1308 | uint16_t speed = device_prm.speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1309 | uint32_t deceleration = ((uint32_t)device_prm.deceleration << 16); |
davide.aliprandi@st.com | 33:8daea0279301 | 1310 | if (((device_prm.commandExecuted == SOFT_STOP_CMD)&&(speed <= device_prm.minSpeed))|| |
davide.aliprandi@st.com | 33:8daea0279301 | 1311 | ((device_prm.commandExecuted != RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1312 | (relativePos >= device_prm.stepsToTake))) |
davide.aliprandi@st.com | 33:8daea0279301 | 1313 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1314 | /* Motion process complete */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1315 | L6474_HardStop(); |
davide.aliprandi@st.com | 33:8daea0279301 | 1316 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1317 | else if ((device_prm.commandExecuted == RUN_CMD)&& |
davide.aliprandi@st.com | 33:8daea0279301 | 1318 | (speed <= device_prm.maxSpeed)) |
davide.aliprandi@st.com | 33:8daea0279301 | 1319 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1320 | device_prm.motionState = STEADY; |
davide.aliprandi@st.com | 33:8daea0279301 | 1321 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1322 | else |
davide.aliprandi@st.com | 33:8daea0279301 | 1323 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1324 | /* Go on decelerating */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1325 | if (speed > device_prm.minSpeed) |
davide.aliprandi@st.com | 33:8daea0279301 | 1326 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1327 | bool speedUpdated = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1328 | if (speed == 0) speed =1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1329 | device_prm.accu += deceleration / speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1330 | while (device_prm.accu >= (0X10000L)) |
davide.aliprandi@st.com | 33:8daea0279301 | 1331 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1332 | device_prm.accu -= (0X10000L); |
davide.aliprandi@st.com | 33:8daea0279301 | 1333 | if (speed > 1) |
davide.aliprandi@st.com | 33:8daea0279301 | 1334 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1335 | speed -=1; |
davide.aliprandi@st.com | 33:8daea0279301 | 1336 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1337 | speedUpdated = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1338 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1339 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1340 | if (speedUpdated) |
davide.aliprandi@st.com | 33:8daea0279301 | 1341 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1342 | if (speed < device_prm.minSpeed) |
davide.aliprandi@st.com | 33:8daea0279301 | 1343 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1344 | speed = device_prm.minSpeed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1345 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1346 | device_prm.speed = speed; |
davide.aliprandi@st.com | 33:8daea0279301 | 1347 | L6474_ApplySpeed(device_prm.speed); |
davide.aliprandi@st.com | 33:8daea0279301 | 1348 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1349 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1350 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1351 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1352 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1353 | default: |
davide.aliprandi@st.com | 33:8daea0279301 | 1354 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1355 | break; |
davide.aliprandi@st.com | 33:8daea0279301 | 1356 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1357 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1358 | /* Set isr flag */ |
davide.aliprandi@st.com | 33:8daea0279301 | 1359 | isr_flag = FALSE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1360 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1361 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1362 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1363 | * @brief Converts current in mA to values for TVAL register |
davide.aliprandi@st.com | 33:8daea0279301 | 1364 | * @param[in] current_mA current in mA |
davide.aliprandi@st.com | 33:8daea0279301 | 1365 | * @retval value for TVAL register |
davide.aliprandi@st.com | 33:8daea0279301 | 1366 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1367 | float L6474::L6474_Tval_Current_to_Par(float current_mA) |
davide.aliprandi@st.com | 33:8daea0279301 | 1368 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1369 | return ((float)(((current_mA - 31.25f) / 31.25f) + 0.5f)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1370 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1371 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1372 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1373 | * @brief Converts values from TVAL register to mA |
davide.aliprandi@st.com | 33:8daea0279301 | 1374 | * @param[in] Tval value from TVAL register |
davide.aliprandi@st.com | 33:8daea0279301 | 1375 | * @retval current in mA |
davide.aliprandi@st.com | 33:8daea0279301 | 1376 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1377 | float L6474::L6474_Par_to_Tval_Current(float Tval) |
davide.aliprandi@st.com | 33:8daea0279301 | 1378 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1379 | return ((float)((Tval - 0.5f) * 31.25f + 31.25f)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1380 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1381 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1382 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1383 | * @brief Convert time in us to values for TON_MIN register |
davide.aliprandi@st.com | 33:8daea0279301 | 1384 | * @param[in] ton_min_us time in us |
davide.aliprandi@st.com | 33:8daea0279301 | 1385 | * @retval value for TON_MIN register |
davide.aliprandi@st.com | 33:8daea0279301 | 1386 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1387 | float L6474::L6474_Tmin_Time_to_Par(float ton_min_us) |
davide.aliprandi@st.com | 33:8daea0279301 | 1388 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1389 | return ((float)(((ton_min_us - 0.5f) * 2.0f) + 0.5f)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1390 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1391 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1392 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1393 | * @brief Convert values for TON_MIN register to time in us |
davide.aliprandi@st.com | 33:8daea0279301 | 1394 | * @param[in] Tmin value from TON_MIN register |
davide.aliprandi@st.com | 33:8daea0279301 | 1395 | * @retval time in us |
davide.aliprandi@st.com | 33:8daea0279301 | 1396 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1397 | float L6474::L6474_Par_to_Tmin_Time(float Tmin) |
davide.aliprandi@st.com | 33:8daea0279301 | 1398 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1399 | return ((float)(((Tmin - 0.5f) / 2.0f) + 0.5f)); |
davide.aliprandi@st.com | 33:8daea0279301 | 1400 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1401 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1402 | /********************************************************** |
davide.aliprandi@st.com | 33:8daea0279301 | 1403 | * @brief Write and receive a byte via SPI |
davide.aliprandi@st.com | 33:8daea0279301 | 1404 | * @param[in] pByteToTransmit pointer to the byte to transmit |
davide.aliprandi@st.com | 33:8daea0279301 | 1405 | * @param[in] pReceivedByte pointer to the received byte |
davide.aliprandi@st.com | 33:8daea0279301 | 1406 | * @retval None |
davide.aliprandi@st.com | 33:8daea0279301 | 1407 | **********************************************************/ |
davide.aliprandi@st.com | 33:8daea0279301 | 1408 | void L6474::L6474_WriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte) |
davide.aliprandi@st.com | 33:8daea0279301 | 1409 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1410 | if (L6474_SpiWriteBytes(pByteToTransmit, pReceivedByte) != 0) |
davide.aliprandi@st.com | 33:8daea0279301 | 1411 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1412 | L6474_ErrorHandler(L6474_ERROR_1); |
davide.aliprandi@st.com | 33:8daea0279301 | 1413 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1414 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1415 | if (isr_flag) |
davide.aliprandi@st.com | 33:8daea0279301 | 1416 | { |
davide.aliprandi@st.com | 33:8daea0279301 | 1417 | spi_preemtion_by_isr = TRUE; |
davide.aliprandi@st.com | 33:8daea0279301 | 1418 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1419 | } |
davide.aliprandi@st.com | 33:8daea0279301 | 1420 | |
davide.aliprandi@st.com | 33:8daea0279301 | 1421 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |