Library used to control ST Nucleo Evaluation board IHM12A1, based on STSPIN240 low voltage dual brush DC motor control driver.
Dependencies: ST_INTERFACES
Dependents: motori prova_motore SchedamotoriIHM12A1 prova_motore_duck ... more
Fork of X-NUCLEO-IHM12A1 by
Brush DC Motor Control Library
Library to handle the X-NUCLEO-IHM12A1 Motor Control Expansion Board based on the STSPIN240 component.
It allows a complete management of the STSPIN240, a low voltage dual brush DC driver, by providing a complete APIs.
The key features of the library are :
- Configuration of the STSPIN240 (bridges input and enabling signals)
- Flag interrupt handling (overcurrent and thermal alarms reporting)
- Handling of up to two bidirectional Brush DC motors
- Nucleo and expansion board configuration (GPIOs, PWMs, IRQs…)
To use the STSPIN240 driver library, the user first has to call its initialization method which:
- Setups the required GPIOs to handle the bridges enable pins, the FLAG interrupt which reports overcurrent detection or thermal protection.
- Loads the driver parameters with initial values configured by the user or with their predefined values from “stspin240_250_target_config.h”, in order to program the PWMs frequency of the bridges inputs, the number of brush DC motors.
Once the initialization is done, the user can modify the driver parameters by calling specific functions to change the numbers of motors or the PWMs frequency.
The user can also write callback functions and attach them to:
- The flag interrupt handler depending on the actions he wants to perform when an overcurrent or a thermal alarm is reported.
- The Error handler which is called by the library when it reports an error.
Then, the user can drive the different brush DC motors by requesting to run in a specified direction and by changing the maximal speed. When a motor is requested to run, the corresponding bridge is automatically enabled.
A motion command can be stopped at any moment:
- Either by a hard stop which immediately stops the motor.
- Or by a hardHiz command which immediately stops the motor and disables the bridge which is used by the motor.
The library also provides functions to disable or enable the bridges independently from the run or stop commands.
Arduino Connector Compatibility Warning
Using the X-NUCLEO-IHM12A1 expansion board with the NUCLEO-F429ZI
requires adopting the following patch:
- to connect with a wire the
PB_3
Nucleo pin to thePWMB
expansion board pin.
Board configuration for HelloWorld_IHM12A1 example
Components/STSpin240_250/STSpin240_250.cpp@6:123d44b9fb9d, 2017-08-03 (annotated)
- Committer:
- Davidroid
- Date:
- Thu Aug 03 16:47:02 2017 +0000
- Revision:
- 6:123d44b9fb9d
- Parent:
- 4:07acd6934359
Aligning to ARM mbed coding style.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davide.aliprandi@st.com | 3:b0128df93d26 | 1 | /** |
davide.aliprandi@st.com | 3:b0128df93d26 | 2 | ****************************************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 3 | * @file STSpin240_250.cpp |
davide.aliprandi@st.com | 3:b0128df93d26 | 4 | * @author IPC Rennes |
davide.aliprandi@st.com | 3:b0128df93d26 | 5 | * @version V1.0.0 |
davide.aliprandi@st.com | 3:b0128df93d26 | 6 | * @date April 25th, 2016 |
Davidroid | 4:07acd6934359 | 7 | * @brief STSpin240 motor driver (Dual brush DC motors) |
davide.aliprandi@st.com | 3:b0128df93d26 | 8 | * @note (C) COPYRIGHT 2016 STMicroelectronics |
davide.aliprandi@st.com | 3:b0128df93d26 | 9 | ****************************************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 10 | * @attention |
davide.aliprandi@st.com | 3:b0128df93d26 | 11 | * |
davide.aliprandi@st.com | 3:b0128df93d26 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
davide.aliprandi@st.com | 3:b0128df93d26 | 13 | * |
davide.aliprandi@st.com | 3:b0128df93d26 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
davide.aliprandi@st.com | 3:b0128df93d26 | 15 | * are permitted provided that the following conditions are met: |
davide.aliprandi@st.com | 3:b0128df93d26 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
davide.aliprandi@st.com | 3:b0128df93d26 | 17 | * this list of conditions and the following disclaimer. |
davide.aliprandi@st.com | 3:b0128df93d26 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
davide.aliprandi@st.com | 3:b0128df93d26 | 19 | * this list of conditions and the following disclaimer in the documentation |
davide.aliprandi@st.com | 3:b0128df93d26 | 20 | * and/or other materials provided with the distribution. |
davide.aliprandi@st.com | 3:b0128df93d26 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
davide.aliprandi@st.com | 3:b0128df93d26 | 22 | * may be used to endorse or promote products derived from this software |
davide.aliprandi@st.com | 3:b0128df93d26 | 23 | * without specific prior written permission. |
davide.aliprandi@st.com | 3:b0128df93d26 | 24 | * |
davide.aliprandi@st.com | 3:b0128df93d26 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
davide.aliprandi@st.com | 3:b0128df93d26 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
davide.aliprandi@st.com | 3:b0128df93d26 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
davide.aliprandi@st.com | 3:b0128df93d26 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
davide.aliprandi@st.com | 3:b0128df93d26 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
davide.aliprandi@st.com | 3:b0128df93d26 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
davide.aliprandi@st.com | 3:b0128df93d26 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
davide.aliprandi@st.com | 3:b0128df93d26 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
davide.aliprandi@st.com | 3:b0128df93d26 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
davide.aliprandi@st.com | 3:b0128df93d26 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
davide.aliprandi@st.com | 3:b0128df93d26 | 35 | * |
davide.aliprandi@st.com | 3:b0128df93d26 | 36 | ****************************************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 37 | */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 38 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 39 | /* Includes ------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 3:b0128df93d26 | 40 | #include "STSpin240_250.h" |
davide.aliprandi@st.com | 3:b0128df93d26 | 41 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 42 | /* Definitions ---------------------------------------------------------------*/ |
davide.aliprandi@st.com | 3:b0128df93d26 | 43 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 44 | /* Error: Access a motor index greater than the one of the current brigde configuration */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 45 | #define STSPIN240_250_ERROR_1 (0xC001) |
davide.aliprandi@st.com | 3:b0128df93d26 | 46 | /* Error: Use of a bridgeId greater than BRIDGE_B */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 47 | #define STSPIN240_250_ERROR_2 (0xC002) |
davide.aliprandi@st.com | 3:b0128df93d26 | 48 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 49 | /* Maximum frequency of the PWMs in Hz */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 50 | #define STSPIN240_250_MAX_PWM_FREQ (100000) |
davide.aliprandi@st.com | 3:b0128df93d26 | 51 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 52 | /* Minimum frequency of the PWMs in Hz */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 53 | #define STSPIN240_250_MIN_PWM_FREQ (2) |
davide.aliprandi@st.com | 3:b0128df93d26 | 54 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 55 | /* Bridge A */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 56 | #define BRIDGE_A (0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 57 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 58 | /* Bridge B */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 59 | #define BRIDGE_B (1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 60 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 61 | /* PWM id for PWM_A */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 62 | #define PWM_A (0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 63 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 64 | /* PWM id for PWM_B */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 65 | #define PWM_B (1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 66 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 67 | /* PWM id for PWM_REF */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 68 | #define PWM_REF (2) |
davide.aliprandi@st.com | 3:b0128df93d26 | 69 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 70 | /* Variables ----------------------------------------------------------------*/ |
davide.aliprandi@st.com | 3:b0128df93d26 | 71 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 72 | /* Number of devices. */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 73 | uint8_t STSpin240_250::numberOfDevices = 0; |
davide.aliprandi@st.com | 3:b0128df93d26 | 74 | uint8_t STSpin240_250::arrayNbMaxMotorsByConfig[2] = {1,2}; |
davide.aliprandi@st.com | 3:b0128df93d26 | 75 | /* Methods -------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 3:b0128df93d26 | 76 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 77 | /********************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 78 | * @brief Starts the STSpin240_250 library |
davide.aliprandi@st.com | 3:b0128df93d26 | 79 | * @param[in] pInit pointer to the initialization data |
davide.aliprandi@st.com | 3:b0128df93d26 | 80 | * @retval COMPONENT_OK in case of success. |
davide.aliprandi@st.com | 3:b0128df93d26 | 81 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 82 | status_t STSpin240_250::STSpin240_250_Init(void* pInit) |
davide.aliprandi@st.com | 3:b0128df93d26 | 83 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 84 | /* Standby-reset deactivation */ |
Davidroid | 4:07acd6934359 | 85 | STSpin240_250_Board_ReleaseReset(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 86 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 87 | /* Let a delay after reset */ |
Davidroid | 4:07acd6934359 | 88 | STSpin240_250_Board_Delay(1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 89 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 90 | if (pInit == 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 91 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 92 | // Set all registers to their predefined values |
davide.aliprandi@st.com | 3:b0128df93d26 | 93 | // from powerstep01_target_config.h |
Davidroid | 4:07acd6934359 | 94 | STSpin240_250_SetDeviceParamsToPredefinedValues(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 95 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 96 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 97 | { |
Davidroid | 4:07acd6934359 | 98 | STSpin240_250_SetDeviceParamsToGivenValues((STSpin240_250_init_t *)pInit); |
davide.aliprandi@st.com | 3:b0128df93d26 | 99 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 100 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 101 | /* Initialise input PWM of bridges*/ |
Davidroid | 4:07acd6934359 | 102 | STSpin240_250_SetDualFullBridgeconfig(device_prm.dualBridgeEnabled); |
davide.aliprandi@st.com | 3:b0128df93d26 | 103 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 104 | return COMPONENT_OK; |
davide.aliprandi@st.com | 3:b0128df93d26 | 105 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 106 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 107 | /********************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 108 | * @brief Read id |
davide.aliprandi@st.com | 3:b0128df93d26 | 109 | * @param[in] id pointer to the identifier to be read. |
davide.aliprandi@st.com | 3:b0128df93d26 | 110 | * @retval COMPONENT_OK in case of success. |
davide.aliprandi@st.com | 3:b0128df93d26 | 111 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 112 | status_t STSpin240_250::STSpin240_250_ReadId(uint8_t *id) |
davide.aliprandi@st.com | 3:b0128df93d26 | 113 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 114 | *id = deviceInstance; |
davide.aliprandi@st.com | 3:b0128df93d26 | 115 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 116 | return COMPONENT_OK; |
davide.aliprandi@st.com | 3:b0128df93d26 | 117 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 118 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 119 | /********************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 120 | * @brief Attaches a user callback to the error Handler. |
davide.aliprandi@st.com | 3:b0128df93d26 | 121 | * The call back will be then called each time the library |
davide.aliprandi@st.com | 3:b0128df93d26 | 122 | * detects an error |
davide.aliprandi@st.com | 3:b0128df93d26 | 123 | * @param[in] callback Name of the callback to attach |
davide.aliprandi@st.com | 3:b0128df93d26 | 124 | * to the error Hanlder |
davide.aliprandi@st.com | 3:b0128df93d26 | 125 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 126 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 127 | void STSpin240_250::STSpin240_250_AttachErrorHandler(void (*callback)(uint16_t error)) |
davide.aliprandi@st.com | 3:b0128df93d26 | 128 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 129 | errorHandlerCallback = (void (*)(uint16_t error)) callback; |
davide.aliprandi@st.com | 3:b0128df93d26 | 130 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 131 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 132 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 133 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 134 | * @brief Disable the specified bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 135 | * @param[in] bridgeId (from 0 for bridge A to 1 for bridge B) |
davide.aliprandi@st.com | 3:b0128df93d26 | 136 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 137 | * @note Bridge A and bridge B share the same enable pin. |
davide.aliprandi@st.com | 3:b0128df93d26 | 138 | * When bridge A is disabled, bridge B is disabled and |
davide.aliprandi@st.com | 3:b0128df93d26 | 139 | * reversely |
davide.aliprandi@st.com | 3:b0128df93d26 | 140 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 141 | void STSpin240_250::STSpin240_250_DisableBridge(uint8_t bridgeId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 142 | { |
Davidroid | 4:07acd6934359 | 143 | STSpin240_250_Board_DisableBridge(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 144 | device_prm.bridgeEnabled[BRIDGE_A] = FALSE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 145 | device_prm.bridgeEnabled[BRIDGE_B] = FALSE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 146 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 147 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 148 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 149 | * @brief Enable the specified bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 150 | * @param[in] bridgeId (from 0 for bridge A to 1 for bridge B) |
davide.aliprandi@st.com | 3:b0128df93d26 | 151 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 152 | * @note Bridge A and bridge B share the same enable pin. |
davide.aliprandi@st.com | 3:b0128df93d26 | 153 | * When bridge A is enabled, bridge B is enabled and |
davide.aliprandi@st.com | 3:b0128df93d26 | 154 | * reversely |
davide.aliprandi@st.com | 3:b0128df93d26 | 155 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 156 | void STSpin240_250::STSpin240_250_EnableBridge(uint8_t bridgeId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 157 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 158 | device_prm.bridgeEnabled[BRIDGE_A] = TRUE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 159 | device_prm.bridgeEnabled[BRIDGE_B] = TRUE; |
Davidroid | 4:07acd6934359 | 160 | STSpin240_250_Board_EnableBridge(1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 161 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 162 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 163 | /********************************************************** |
davide.aliprandi@st.com | 3:b0128df93d26 | 164 | * @brief Error handler which calls the user callback (if defined) |
davide.aliprandi@st.com | 3:b0128df93d26 | 165 | * @param[in] error Number of the error |
davide.aliprandi@st.com | 3:b0128df93d26 | 166 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 167 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 168 | void STSpin240_250::STSpin240_250_ErrorHandler(uint16_t error) |
davide.aliprandi@st.com | 3:b0128df93d26 | 169 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 170 | if (errorHandlerCallback != 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 171 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 172 | (void) errorHandlerCallback(error); |
davide.aliprandi@st.com | 3:b0128df93d26 | 173 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 174 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 175 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 176 | /* Aborting the program. */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 177 | exit(EXIT_FAILURE); |
davide.aliprandi@st.com | 3:b0128df93d26 | 178 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 179 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 180 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 181 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 182 | * @brief Get the PWM frequency of the specified bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 183 | * @param[in] bridgeId 0 for bridge A, 1 for bridge B |
davide.aliprandi@st.com | 3:b0128df93d26 | 184 | * @retval Freq in Hz |
davide.aliprandi@st.com | 3:b0128df93d26 | 185 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 186 | uint32_t STSpin240_250::STSpin240_250_GetBridgeInputPwmFreq(uint8_t bridgeId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 187 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 188 | if (bridgeId > BRIDGE_B) |
davide.aliprandi@st.com | 3:b0128df93d26 | 189 | { |
Davidroid | 4:07acd6934359 | 190 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_2); |
davide.aliprandi@st.com | 3:b0128df93d26 | 191 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 192 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 193 | return (device_prm.bridgePwmFreq[(bridgeId << 1)]); |
davide.aliprandi@st.com | 3:b0128df93d26 | 194 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 195 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 196 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 197 | * @brief Get the status of the bridge enabling of the corresponding bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 198 | * @param[in] bridgeId from 0 for bridge A to 1 for bridge B |
davide.aliprandi@st.com | 3:b0128df93d26 | 199 | * @retval State of the Enable&Fault pin (shared for bridge A and B) |
davide.aliprandi@st.com | 3:b0128df93d26 | 200 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 201 | uint16_t STSpin240_250::STSpin240_250_GetBridgeStatus(void) |
davide.aliprandi@st.com | 3:b0128df93d26 | 202 | { |
Davidroid | 4:07acd6934359 | 203 | uint16_t status = STSpin240_250_Board_GetFaultPinState(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 204 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 205 | return (status); |
davide.aliprandi@st.com | 3:b0128df93d26 | 206 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 207 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 208 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 209 | * @brief Returns the current speed of the specified motor |
davide.aliprandi@st.com | 3:b0128df93d26 | 210 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 211 | * @retval current speed in % from 0 to 100 |
davide.aliprandi@st.com | 3:b0128df93d26 | 212 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 213 | uint16_t STSpin240_250::STSpin240_250_GetCurrentSpeed(uint8_t motorId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 214 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 215 | uint16_t speed = 0; |
davide.aliprandi@st.com | 3:b0128df93d26 | 216 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 217 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 218 | { |
Davidroid | 4:07acd6934359 | 219 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 220 | } |
Davidroid | 6:123d44b9fb9d | 221 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 222 | { |
Davidroid | 6:123d44b9fb9d | 223 | if (device_prm.motionState[motorId] != INACTIVE) |
Davidroid | 6:123d44b9fb9d | 224 | { |
Davidroid | 6:123d44b9fb9d | 225 | speed = device_prm.speed[motorId]; |
Davidroid | 6:123d44b9fb9d | 226 | } |
Davidroid | 6:123d44b9fb9d | 227 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 228 | return (speed); |
davide.aliprandi@st.com | 3:b0128df93d26 | 229 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 230 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 231 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 232 | * @brief Returns the device state |
davide.aliprandi@st.com | 3:b0128df93d26 | 233 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 234 | * @retval State (STEADY or INACTIVE) |
davide.aliprandi@st.com | 3:b0128df93d26 | 235 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 236 | motorState_t STSpin240_250::STSpin240_250_GetDeviceState(uint8_t motorId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 237 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 238 | motorState_t state = INACTIVE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 239 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 240 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 241 | { |
Davidroid | 4:07acd6934359 | 242 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 243 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 244 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 245 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 246 | state = device_prm.motionState[motorId]; |
davide.aliprandi@st.com | 3:b0128df93d26 | 247 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 248 | return (state); |
davide.aliprandi@st.com | 3:b0128df93d26 | 249 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 250 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 251 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 252 | * @brief Get the motor current direction |
davide.aliprandi@st.com | 3:b0128df93d26 | 253 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 254 | * @retval direction |
davide.aliprandi@st.com | 3:b0128df93d26 | 255 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 256 | motorDir_t STSpin240_250::STSpin240_250_GetDirection(uint8_t motorId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 257 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 258 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 259 | { |
Davidroid | 4:07acd6934359 | 260 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 261 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 262 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 263 | return device_prm.direction[motorId]; |
davide.aliprandi@st.com | 3:b0128df93d26 | 264 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 265 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 266 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 267 | * @brief Returns the FW version of the library |
davide.aliprandi@st.com | 3:b0128df93d26 | 268 | * @retval STSPIN240_250_FW_VERSION |
davide.aliprandi@st.com | 3:b0128df93d26 | 269 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 270 | uint32_t STSpin240_250::STSpin240_250_GetFwVersion(void) |
davide.aliprandi@st.com | 3:b0128df93d26 | 271 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 272 | return (STSPIN240_250_FW_VERSION); |
davide.aliprandi@st.com | 3:b0128df93d26 | 273 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 274 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 275 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 276 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 277 | * @brief Return the duty cycle of PWM used for REF |
Davidroid | 4:07acd6934359 | 278 | * @param[in] refId 0 is the only supported id for STSpin240 or |
Davidroid | 4:07acd6934359 | 279 | * STSpin250 |
davide.aliprandi@st.com | 3:b0128df93d26 | 280 | * @retval duty cycle in % (from 0 to 100) |
davide.aliprandi@st.com | 3:b0128df93d26 | 281 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 282 | uint8_t STSpin240_250::STSpin240_250_GetRefPwmDc(uint8_t refId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 283 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 284 | uint32_t duty = 0; |
davide.aliprandi@st.com | 3:b0128df93d26 | 285 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 286 | if (duty == 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 287 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 288 | duty = device_prm.refPwmDc; |
davide.aliprandi@st.com | 3:b0128df93d26 | 289 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 290 | return (duty); |
davide.aliprandi@st.com | 3:b0128df93d26 | 291 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 292 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 293 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 294 | * @brief Return the frequency of PWM used for REF |
Davidroid | 4:07acd6934359 | 295 | * @param[in] refId 0 is the only supported id for STSpin240 or |
Davidroid | 4:07acd6934359 | 296 | * STSpin250 |
davide.aliprandi@st.com | 3:b0128df93d26 | 297 | * @retval Frequency in Hz |
davide.aliprandi@st.com | 3:b0128df93d26 | 298 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 299 | uint32_t STSpin240_250::STSpin240_250_GetRefPwmFreq(uint8_t refId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 300 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 301 | uint32_t freq = 0; |
davide.aliprandi@st.com | 3:b0128df93d26 | 302 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 303 | if (refId == 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 304 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 305 | freq = device_prm.refPwmFreq; |
davide.aliprandi@st.com | 3:b0128df93d26 | 306 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 307 | return (freq); |
davide.aliprandi@st.com | 3:b0128df93d26 | 308 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 309 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 310 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 311 | * @brief Immediatly stops the motor and disable the power bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 312 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 313 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 314 | * @note As all motors uses the same power bridge, the |
davide.aliprandi@st.com | 3:b0128df93d26 | 315 | * power bridge will be disable only if all motors are |
davide.aliprandi@st.com | 3:b0128df93d26 | 316 | * stopped |
davide.aliprandi@st.com | 3:b0128df93d26 | 317 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 318 | void STSpin240_250::STSpin240_250_HardHiz(uint8_t motorId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 319 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 320 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 321 | { |
Davidroid | 4:07acd6934359 | 322 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 323 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 324 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 325 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 326 | if (device_prm.bridgeEnabled[motorId] != FALSE) |
davide.aliprandi@st.com | 3:b0128df93d26 | 327 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 328 | /* Only disable bridges if dual bridge mode is disabled or */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 329 | /* if all motors are inactive as there are sharing the same power stage */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 330 | if ((device_prm.dualBridgeEnabled == 0)|| |
davide.aliprandi@st.com | 3:b0128df93d26 | 331 | ((motorId == 0)&&(device_prm.motionState[1] == INACTIVE))|| |
davide.aliprandi@st.com | 3:b0128df93d26 | 332 | ((motorId == 1)&&(device_prm.motionState[0] == INACTIVE))) |
davide.aliprandi@st.com | 3:b0128df93d26 | 333 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 334 | /* Disable the bridge */ |
Davidroid | 4:07acd6934359 | 335 | STSpin240_250_DisableBridge(motorId); |
davide.aliprandi@st.com | 3:b0128df93d26 | 336 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 337 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 338 | /* Disable the PWM */ |
Davidroid | 4:07acd6934359 | 339 | STSpin240_250_HardStop(motorId); |
davide.aliprandi@st.com | 3:b0128df93d26 | 340 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 341 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 342 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 343 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 344 | * @brief Stops the motor without disabling the bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 345 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 346 | * @retval none |
davide.aliprandi@st.com | 3:b0128df93d26 | 347 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 348 | void STSpin240_250::STSpin240_250_HardStop(uint8_t motorId) |
davide.aliprandi@st.com | 3:b0128df93d26 | 349 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 350 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 351 | { |
Davidroid | 4:07acd6934359 | 352 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 353 | } |
Davidroid | 6:123d44b9fb9d | 354 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 355 | { |
Davidroid | 6:123d44b9fb9d | 356 | if (device_prm.motionState[motorId] != INACTIVE) |
Davidroid | 6:123d44b9fb9d | 357 | { |
Davidroid | 6:123d44b9fb9d | 358 | /* Disable corresponding PWM */ |
Davidroid | 6:123d44b9fb9d | 359 | STSpin240_250_Board_PwmStop(motorId); |
Davidroid | 6:123d44b9fb9d | 360 | |
Davidroid | 6:123d44b9fb9d | 361 | /* Set inactive state */ |
Davidroid | 6:123d44b9fb9d | 362 | device_prm.motionState[motorId] = INACTIVE; |
Davidroid | 6:123d44b9fb9d | 363 | } |
Davidroid | 6:123d44b9fb9d | 364 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 365 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 366 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 367 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 368 | * @brief Release reset (exit standby mode) |
davide.aliprandi@st.com | 3:b0128df93d26 | 369 | * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES -1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 370 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 371 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 372 | void STSpin240_250::STSpin240_250_ReleaseReset(void) |
davide.aliprandi@st.com | 3:b0128df93d26 | 373 | { |
Davidroid | 4:07acd6934359 | 374 | STSpin240_250_Board_ReleaseReset(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 375 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 376 | /* Start PWM used for REF pin */ |
Davidroid | 4:07acd6934359 | 377 | STSpin240_250_Board_PwmSetFreq(PWM_REF, device_prm.refPwmFreq,device_prm.refPwmDc); |
davide.aliprandi@st.com | 3:b0128df93d26 | 378 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 379 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 380 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 381 | * @brief Reset (enter standby mode) |
davide.aliprandi@st.com | 3:b0128df93d26 | 382 | * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES -1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 383 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 384 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 385 | void STSpin240_250::STSpin240_250_Reset(void) |
davide.aliprandi@st.com | 3:b0128df93d26 | 386 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 387 | uint8_t loop; |
davide.aliprandi@st.com | 3:b0128df93d26 | 388 | for (loop = 0; loop < STSPIN240_250_NB_MAX_MOTORS; loop++) |
davide.aliprandi@st.com | 3:b0128df93d26 | 389 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 390 | /* Stop motor if needed*/ |
davide.aliprandi@st.com | 3:b0128df93d26 | 391 | if (device_prm.motionState[loop] != INACTIVE) |
davide.aliprandi@st.com | 3:b0128df93d26 | 392 | { |
Davidroid | 4:07acd6934359 | 393 | STSpin240_250_HardStop(loop); |
davide.aliprandi@st.com | 3:b0128df93d26 | 394 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 395 | /* Disable bridge if needed */ |
davide.aliprandi@st.com | 3:b0128df93d26 | 396 | if (device_prm.bridgeEnabled[loop] != FALSE) |
davide.aliprandi@st.com | 3:b0128df93d26 | 397 | { |
Davidroid | 4:07acd6934359 | 398 | STSpin240_250_DisableBridge(loop); |
davide.aliprandi@st.com | 3:b0128df93d26 | 399 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 400 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 401 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 402 | /* Stop PWM used for REF pin */ |
Davidroid | 4:07acd6934359 | 403 | STSpin240_250_Board_PwmStop(PWM_REF); |
davide.aliprandi@st.com | 3:b0128df93d26 | 404 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 405 | /* Reset the STBY/RESET pin */ |
Davidroid | 4:07acd6934359 | 406 | STSpin240_250_Board_Reset(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 407 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 408 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 409 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 410 | * @brief Runs the motor |
davide.aliprandi@st.com | 3:b0128df93d26 | 411 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 412 | * @param[in] direction FORWARD or BACKWARD |
davide.aliprandi@st.com | 3:b0128df93d26 | 413 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 414 | * @note For unidirectionnal motor, direction parameter has |
davide.aliprandi@st.com | 3:b0128df93d26 | 415 | * no effect |
davide.aliprandi@st.com | 3:b0128df93d26 | 416 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 417 | void STSpin240_250::STSpin240_250_Run(uint8_t motorId, motorDir_t direction) |
davide.aliprandi@st.com | 3:b0128df93d26 | 418 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 419 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 420 | { |
Davidroid | 4:07acd6934359 | 421 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 422 | } |
Davidroid | 6:123d44b9fb9d | 423 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 424 | { |
Davidroid | 6:123d44b9fb9d | 425 | if ((device_prm.motionState[motorId] == INACTIVE) || |
Davidroid | 6:123d44b9fb9d | 426 | (device_prm.direction[motorId] != direction)) |
Davidroid | 6:123d44b9fb9d | 427 | { |
Davidroid | 6:123d44b9fb9d | 428 | |
Davidroid | 6:123d44b9fb9d | 429 | /* Release reset if required */ |
Davidroid | 6:123d44b9fb9d | 430 | if (STSpin240_250_GetResetState() == 0) |
Davidroid | 6:123d44b9fb9d | 431 | { |
Davidroid | 6:123d44b9fb9d | 432 | STSpin240_250_ReleaseReset(); |
Davidroid | 6:123d44b9fb9d | 433 | } |
Davidroid | 6:123d44b9fb9d | 434 | |
Davidroid | 6:123d44b9fb9d | 435 | /* Eventually deactivate motor */ |
Davidroid | 6:123d44b9fb9d | 436 | if (device_prm.motionState[motorId] != INACTIVE) |
Davidroid | 6:123d44b9fb9d | 437 | { |
Davidroid | 6:123d44b9fb9d | 438 | STSpin240_250_HardStop(motorId); |
Davidroid | 6:123d44b9fb9d | 439 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 440 | |
Davidroid | 6:123d44b9fb9d | 441 | /* Set direction */ |
Davidroid | 6:123d44b9fb9d | 442 | STSpin240_250_SetDirection(motorId, direction); |
Davidroid | 6:123d44b9fb9d | 443 | |
Davidroid | 6:123d44b9fb9d | 444 | /* Switch to steady state */ |
Davidroid | 6:123d44b9fb9d | 445 | device_prm.motionState[motorId] = STEADY; |
Davidroid | 6:123d44b9fb9d | 446 | |
Davidroid | 6:123d44b9fb9d | 447 | /* Enable bridge */ |
Davidroid | 6:123d44b9fb9d | 448 | if (device_prm.bridgeEnabled[motorId] == FALSE) |
Davidroid | 6:123d44b9fb9d | 449 | { |
Davidroid | 6:123d44b9fb9d | 450 | STSpin240_250_EnableBridge(motorId); |
Davidroid | 6:123d44b9fb9d | 451 | } |
Davidroid | 6:123d44b9fb9d | 452 | /* Set PWM */ |
Davidroid | 6:123d44b9fb9d | 453 | STSpin240_250_Board_PwmSetFreq(motorId, device_prm.bridgePwmFreq[motorId],device_prm.speed[motorId]); |
Davidroid | 6:123d44b9fb9d | 454 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 455 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 456 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 457 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 458 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 459 | * @brief Changes the PWM frequency of the bridge input |
davide.aliprandi@st.com | 3:b0128df93d26 | 460 | * @param[in] bridgeId 0 for bridge A, 1 for bridge B |
davide.aliprandi@st.com | 3:b0128df93d26 | 461 | * @param[in] newFreq in Hz |
davide.aliprandi@st.com | 3:b0128df93d26 | 462 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 463 | * @note 1)The PWM is only enabled when the motor is requested |
davide.aliprandi@st.com | 3:b0128df93d26 | 464 | * to run. |
davide.aliprandi@st.com | 3:b0128df93d26 | 465 | * 2) If the two bridges share the same timer, their frequency |
davide.aliprandi@st.com | 3:b0128df93d26 | 466 | * has to be the same |
davide.aliprandi@st.com | 3:b0128df93d26 | 467 | * 3) If the two bridges share the same timer, the frequency |
davide.aliprandi@st.com | 3:b0128df93d26 | 468 | * is updated on the fly is there is only one motor running |
davide.aliprandi@st.com | 3:b0128df93d26 | 469 | * on the targeted bridge. |
davide.aliprandi@st.com | 3:b0128df93d26 | 470 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 471 | void STSpin240_250::STSpin240_250_SetBridgeInputPwmFreq(uint8_t bridgeId, uint32_t newFreq) |
davide.aliprandi@st.com | 3:b0128df93d26 | 472 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 473 | if (bridgeId > BRIDGE_B) |
davide.aliprandi@st.com | 3:b0128df93d26 | 474 | { |
Davidroid | 4:07acd6934359 | 475 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_2); |
davide.aliprandi@st.com | 3:b0128df93d26 | 476 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 477 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 478 | if (newFreq > STSPIN240_250_MAX_PWM_FREQ) |
davide.aliprandi@st.com | 3:b0128df93d26 | 479 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 480 | newFreq = STSPIN240_250_MAX_PWM_FREQ; |
davide.aliprandi@st.com | 3:b0128df93d26 | 481 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 482 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 483 | device_prm.bridgePwmFreq[bridgeId] = newFreq; |
davide.aliprandi@st.com | 3:b0128df93d26 | 484 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 485 | if (device_prm.motionState[bridgeId] != INACTIVE) |
davide.aliprandi@st.com | 3:b0128df93d26 | 486 | { |
Davidroid | 4:07acd6934359 | 487 | STSpin240_250_Board_PwmSetFreq(bridgeId, device_prm.bridgePwmFreq[bridgeId],device_prm.speed[bridgeId]); |
davide.aliprandi@st.com | 3:b0128df93d26 | 488 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 489 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 490 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 491 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 492 | * @brief Specifies the direction |
davide.aliprandi@st.com | 3:b0128df93d26 | 493 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 494 | * @param[in] dir FORWARD or BACKWARD |
davide.aliprandi@st.com | 3:b0128df93d26 | 495 | * @note The direction change is only applied if the device |
davide.aliprandi@st.com | 3:b0128df93d26 | 496 | * is in INACTIVE state. To change direction while motor is |
davide.aliprandi@st.com | 3:b0128df93d26 | 497 | * running, use the run function |
davide.aliprandi@st.com | 3:b0128df93d26 | 498 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 499 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 500 | void STSpin240_250::STSpin240_250_SetDirection(uint8_t motorId, motorDir_t dir) |
davide.aliprandi@st.com | 3:b0128df93d26 | 501 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 502 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 503 | { |
Davidroid | 4:07acd6934359 | 504 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 505 | } |
Davidroid | 6:123d44b9fb9d | 506 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 507 | { |
Davidroid | 6:123d44b9fb9d | 508 | if (device_prm.motionState[motorId] == INACTIVE) |
Davidroid | 6:123d44b9fb9d | 509 | { |
Davidroid | 6:123d44b9fb9d | 510 | STSpin240_250_Board_SetDirectionGpio(motorId, dir); |
Davidroid | 6:123d44b9fb9d | 511 | device_prm.direction[motorId] = dir; |
Davidroid | 6:123d44b9fb9d | 512 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 513 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 514 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 515 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 516 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 517 | * @brief Set the dual bridge configuration mode |
davide.aliprandi@st.com | 3:b0128df93d26 | 518 | * @param[in] enable 0 to disable, |
davide.aliprandi@st.com | 3:b0128df93d26 | 519 | * 1 to enable (not supported by STSPIN250) |
davide.aliprandi@st.com | 3:b0128df93d26 | 520 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 521 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 522 | void STSpin240_250::STSpin240_250_SetDualFullBridgeconfig(uint8_t enable) |
davide.aliprandi@st.com | 3:b0128df93d26 | 523 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 524 | device_prm.dualBridgeEnabled = enable; |
davide.aliprandi@st.com | 3:b0128df93d26 | 525 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 526 | /* Check reset pin state*/ |
Davidroid | 4:07acd6934359 | 527 | if (STSpin240_250_GetResetState() != 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 528 | { |
Davidroid | 4:07acd6934359 | 529 | STSpin240_250_Reset(); |
Davidroid | 4:07acd6934359 | 530 | STSpin240_250_ReleaseReset(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 531 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 532 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 533 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 534 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 535 | * @brief Changes the max speed of the specified device |
davide.aliprandi@st.com | 3:b0128df93d26 | 536 | * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 537 | * @param[in] newMaxSpeed in % from 0 to 100 |
davide.aliprandi@st.com | 3:b0128df93d26 | 538 | * @retval true if the command is successfully executed, else false |
davide.aliprandi@st.com | 3:b0128df93d26 | 539 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 540 | bool STSpin240_250::STSpin240_250_SetMaxSpeed(uint8_t motorId, uint16_t newMaxSpeed) |
davide.aliprandi@st.com | 3:b0128df93d26 | 541 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 542 | bool cmdExecuted = FALSE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 543 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 544 | if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled]) |
davide.aliprandi@st.com | 3:b0128df93d26 | 545 | { |
Davidroid | 4:07acd6934359 | 546 | STSpin240_250_ErrorHandler(STSPIN240_250_ERROR_1); |
davide.aliprandi@st.com | 3:b0128df93d26 | 547 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 548 | else |
davide.aliprandi@st.com | 3:b0128df93d26 | 549 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 550 | device_prm.speed[motorId] = newMaxSpeed; |
davide.aliprandi@st.com | 3:b0128df93d26 | 551 | if (device_prm.motionState[motorId] != INACTIVE) |
davide.aliprandi@st.com | 3:b0128df93d26 | 552 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 553 | /* Set PWM frequency*/ |
Davidroid | 4:07acd6934359 | 554 | STSpin240_250_Board_PwmSetFreq(motorId, device_prm.bridgePwmFreq[motorId],device_prm.speed[motorId]); |
davide.aliprandi@st.com | 3:b0128df93d26 | 555 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 556 | cmdExecuted = TRUE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 557 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 558 | return cmdExecuted; |
davide.aliprandi@st.com | 3:b0128df93d26 | 559 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 560 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 561 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 562 | * @brief Changes the duty cycle of the PWM used for REF |
Davidroid | 4:07acd6934359 | 563 | * @param[in] refId 0 is the only supported id for STSpin240 or |
Davidroid | 4:07acd6934359 | 564 | * STSpin250 |
davide.aliprandi@st.com | 3:b0128df93d26 | 565 | * @param[in] newDc new duty cycle from 0 to 100 |
davide.aliprandi@st.com | 3:b0128df93d26 | 566 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 567 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 568 | void STSpin240_250::STSpin240_250_SetRefPwmDc(uint8_t refId, uint8_t newDc) |
davide.aliprandi@st.com | 3:b0128df93d26 | 569 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 570 | if (newDc > 100) |
davide.aliprandi@st.com | 3:b0128df93d26 | 571 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 572 | newDc = 100; |
davide.aliprandi@st.com | 3:b0128df93d26 | 573 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 574 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 575 | device_prm.refPwmDc = newDc; |
davide.aliprandi@st.com | 3:b0128df93d26 | 576 | |
Davidroid | 4:07acd6934359 | 577 | if (STSpin240_250_GetResetState() != 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 578 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 579 | /* Immediatly set the PWM frequency for ref if chip is not in reset */ |
Davidroid | 4:07acd6934359 | 580 | STSpin240_250_Board_PwmSetFreq(PWM_REF, device_prm.refPwmFreq,device_prm.refPwmDc); |
davide.aliprandi@st.com | 3:b0128df93d26 | 581 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 582 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 583 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 584 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 585 | * @brief Changes the frequency of PWM used for REF |
Davidroid | 4:07acd6934359 | 586 | * @param[in] refId 0 is the only supported id for STSpin240 or |
Davidroid | 4:07acd6934359 | 587 | * STSpin250 |
davide.aliprandi@st.com | 3:b0128df93d26 | 588 | * @param[in] newFreq in Hz |
davide.aliprandi@st.com | 3:b0128df93d26 | 589 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 590 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 591 | void STSpin240_250::STSpin240_250_SetRefPwmFreq(uint8_t refId, uint32_t newFreq) |
davide.aliprandi@st.com | 3:b0128df93d26 | 592 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 593 | if (newFreq > STSPIN240_250_MAX_PWM_FREQ) |
davide.aliprandi@st.com | 3:b0128df93d26 | 594 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 595 | newFreq = STSPIN240_250_MAX_PWM_FREQ; |
davide.aliprandi@st.com | 3:b0128df93d26 | 596 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 597 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 598 | device_prm.refPwmFreq = newFreq; |
davide.aliprandi@st.com | 3:b0128df93d26 | 599 | |
Davidroid | 4:07acd6934359 | 600 | if (STSpin240_250_GetResetState() != 0) |
davide.aliprandi@st.com | 3:b0128df93d26 | 601 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 602 | /* Immediatly set the PWM frequency for ref if chip is not in reset */ |
Davidroid | 4:07acd6934359 | 603 | STSpin240_250_Board_PwmSetFreq(PWM_REF, device_prm.refPwmFreq,device_prm.refPwmDc); |
davide.aliprandi@st.com | 3:b0128df93d26 | 604 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 605 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 606 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 607 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 608 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 609 | * @brief Get the status of the bridge enabling of the corresponding bridge |
davide.aliprandi@st.com | 3:b0128df93d26 | 610 | * @retval State of the Enable&Fault pin (shared for bridge A and B) |
davide.aliprandi@st.com | 3:b0128df93d26 | 611 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 612 | uint8_t STSpin240_250::STSpin240_250_GetResetState(void) |
davide.aliprandi@st.com | 3:b0128df93d26 | 613 | { |
Davidroid | 4:07acd6934359 | 614 | uint8_t status = STSpin240_250_Board_GetResetPinState(); |
davide.aliprandi@st.com | 3:b0128df93d26 | 615 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 616 | return (status); |
davide.aliprandi@st.com | 3:b0128df93d26 | 617 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 618 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 619 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 620 | * @brief Set the parameters of the device to values of pInitPrm structure |
davide.aliprandi@st.com | 3:b0128df93d26 | 621 | * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES -1) |
davide.aliprandi@st.com | 3:b0128df93d26 | 622 | * @param pInitPrm pointer to a structure containing the initial device parameters |
davide.aliprandi@st.com | 3:b0128df93d26 | 623 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 624 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 625 | void STSpin240_250::STSpin240_250_SetDeviceParamsToGivenValues(STSpin240_250_init_t *pInitPrm) |
davide.aliprandi@st.com | 3:b0128df93d26 | 626 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 627 | uint32_t i; |
davide.aliprandi@st.com | 3:b0128df93d26 | 628 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 629 | device_prm.dualBridgeEnabled = pInitPrm->dualBridgeEnabled; |
davide.aliprandi@st.com | 3:b0128df93d26 | 630 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 631 | device_prm.bridgePwmFreq[BRIDGE_A] = pInitPrm->bridgePwmFreq[BRIDGE_A]; |
davide.aliprandi@st.com | 3:b0128df93d26 | 632 | device_prm.bridgePwmFreq[BRIDGE_B] = pInitPrm->bridgePwmFreq[BRIDGE_B];; |
davide.aliprandi@st.com | 3:b0128df93d26 | 633 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 634 | device_prm.refPwmFreq = pInitPrm->refPwmFreq; |
davide.aliprandi@st.com | 3:b0128df93d26 | 635 | device_prm.refPwmDc = pInitPrm->refPwmDc; |
davide.aliprandi@st.com | 3:b0128df93d26 | 636 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 637 | for (i = 0; i < MAX_NUMBER_OF_BRUSH_DC_MOTORS; i++) |
davide.aliprandi@st.com | 3:b0128df93d26 | 638 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 639 | device_prm.speed[i] = 100; |
davide.aliprandi@st.com | 3:b0128df93d26 | 640 | device_prm.direction[i] = FORWARD; |
davide.aliprandi@st.com | 3:b0128df93d26 | 641 | device_prm.motionState[i] = INACTIVE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 642 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 643 | for (i = 0; i < STSPIN240_250_NB_BRIDGES; i++) |
davide.aliprandi@st.com | 3:b0128df93d26 | 644 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 645 | device_prm.bridgeEnabled[i] = FALSE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 646 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 647 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 648 | /******************************************************//** |
davide.aliprandi@st.com | 3:b0128df93d26 | 649 | * @brief Sets the parameters of the device to predefined values |
Davidroid | 4:07acd6934359 | 650 | * from STSpin240_250_target_config.h |
davide.aliprandi@st.com | 3:b0128df93d26 | 651 | * @retval None |
davide.aliprandi@st.com | 3:b0128df93d26 | 652 | **********************************************************/ |
Davidroid | 4:07acd6934359 | 653 | void STSpin240_250::STSpin240_250_SetDeviceParamsToPredefinedValues(void) |
davide.aliprandi@st.com | 3:b0128df93d26 | 654 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 655 | uint32_t i; |
davide.aliprandi@st.com | 3:b0128df93d26 | 656 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 657 | device_prm.dualBridgeEnabled = STSPIN240_250_CONF_PARAM_DUAL_BRIDGE_ENABLING; |
davide.aliprandi@st.com | 3:b0128df93d26 | 658 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 659 | device_prm.bridgePwmFreq[BRIDGE_A] = STSPIN240_250_CONF_PARAM_FREQ_PWM_A; |
davide.aliprandi@st.com | 3:b0128df93d26 | 660 | device_prm.bridgePwmFreq[BRIDGE_B] = STSPIN240_250_CONF_PARAM_FREQ_PWM_B; |
davide.aliprandi@st.com | 3:b0128df93d26 | 661 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 662 | device_prm.refPwmFreq = STSPIN240_250_CONF_PARAM_FREQ_PWM_REF; |
davide.aliprandi@st.com | 3:b0128df93d26 | 663 | device_prm.refPwmDc = STSPIN240_250_CONF_PARAM_DC_PWM_REF; |
davide.aliprandi@st.com | 3:b0128df93d26 | 664 | |
davide.aliprandi@st.com | 3:b0128df93d26 | 665 | for (i = 0; i < MAX_NUMBER_OF_BRUSH_DC_MOTORS; i++) |
davide.aliprandi@st.com | 3:b0128df93d26 | 666 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 667 | device_prm.speed[i] = 100; |
davide.aliprandi@st.com | 3:b0128df93d26 | 668 | device_prm.direction[i] = FORWARD; |
davide.aliprandi@st.com | 3:b0128df93d26 | 669 | device_prm.motionState[i] = INACTIVE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 670 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 671 | for (i = 0; i < STSPIN240_250_NB_BRIDGES; i++) |
davide.aliprandi@st.com | 3:b0128df93d26 | 672 | { |
davide.aliprandi@st.com | 3:b0128df93d26 | 673 | device_prm.bridgeEnabled[i] = FALSE; |
davide.aliprandi@st.com | 3:b0128df93d26 | 674 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 675 | } |
davide.aliprandi@st.com | 3:b0128df93d26 | 676 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |