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 ST Expansion SW Team

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 the PWMB expansion board pin.


Board configuration for HelloWorld_IHM12A1 example

/media/uploads/Manu_L/x_nucleo_ihm12a1.jpg

Committer:
davide.aliprandi@st.com
Date:
Fri Mar 24 10:59:30 2017 +0100
Revision:
3:b0128df93d26
Aligning to ARM mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew 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
davide.aliprandi@st.com 3:b0128df93d26 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>&copy; 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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 */
davide.aliprandi@st.com 3:b0128df93d26 85 Stspin240_250_Board_ReleaseReset();
davide.aliprandi@st.com 3:b0128df93d26 86
davide.aliprandi@st.com 3:b0128df93d26 87 /* Let a delay after reset */
davide.aliprandi@st.com 3:b0128df93d26 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
davide.aliprandi@st.com 3:b0128df93d26 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 {
davide.aliprandi@st.com 3:b0128df93d26 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*/
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 141 void STSpin240_250::Stspin240_250_DisableBridge(uint8_t bridgeId)
davide.aliprandi@st.com 3:b0128df93d26 142 {
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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;
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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 {
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 201 uint16_t STSpin240_250::Stspin240_250_GetBridgeStatus(void)
davide.aliprandi@st.com 3:b0128df93d26 202 {
davide.aliprandi@st.com 3:b0128df93d26 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 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 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 {
davide.aliprandi@st.com 3:b0128df93d26 219 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 220 }
davide.aliprandi@st.com 3:b0128df93d26 221 else if (device_prm.motionState[motorId] != INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 222 {
davide.aliprandi@st.com 3:b0128df93d26 223 speed = device_prm.speed[motorId];
davide.aliprandi@st.com 3:b0128df93d26 224 }
davide.aliprandi@st.com 3:b0128df93d26 225 return (speed);
davide.aliprandi@st.com 3:b0128df93d26 226 }
davide.aliprandi@st.com 3:b0128df93d26 227
davide.aliprandi@st.com 3:b0128df93d26 228 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 229 * @brief Returns the device state
davide.aliprandi@st.com 3:b0128df93d26 230 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 231 * @retval State (STEADY or INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 232 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 233 motorState_t STSpin240_250::Stspin240_250_get_device_state(uint8_t motorId)
davide.aliprandi@st.com 3:b0128df93d26 234 {
davide.aliprandi@st.com 3:b0128df93d26 235 motorState_t state = INACTIVE;
davide.aliprandi@st.com 3:b0128df93d26 236
davide.aliprandi@st.com 3:b0128df93d26 237 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 238 {
davide.aliprandi@st.com 3:b0128df93d26 239 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 240 }
davide.aliprandi@st.com 3:b0128df93d26 241 else
davide.aliprandi@st.com 3:b0128df93d26 242 {
davide.aliprandi@st.com 3:b0128df93d26 243 state = device_prm.motionState[motorId];
davide.aliprandi@st.com 3:b0128df93d26 244 }
davide.aliprandi@st.com 3:b0128df93d26 245 return (state);
davide.aliprandi@st.com 3:b0128df93d26 246 }
davide.aliprandi@st.com 3:b0128df93d26 247
davide.aliprandi@st.com 3:b0128df93d26 248 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 249 * @brief Get the motor current direction
davide.aliprandi@st.com 3:b0128df93d26 250 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 251 * @retval direction
davide.aliprandi@st.com 3:b0128df93d26 252 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 253 motorDir_t STSpin240_250::Stspin240_250_GetDirection(uint8_t motorId)
davide.aliprandi@st.com 3:b0128df93d26 254 {
davide.aliprandi@st.com 3:b0128df93d26 255 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 256 {
davide.aliprandi@st.com 3:b0128df93d26 257 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 258 }
davide.aliprandi@st.com 3:b0128df93d26 259
davide.aliprandi@st.com 3:b0128df93d26 260 return device_prm.direction[motorId];
davide.aliprandi@st.com 3:b0128df93d26 261 }
davide.aliprandi@st.com 3:b0128df93d26 262
davide.aliprandi@st.com 3:b0128df93d26 263 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 264 * @brief Returns the FW version of the library
davide.aliprandi@st.com 3:b0128df93d26 265 * @retval STSPIN240_250_FW_VERSION
davide.aliprandi@st.com 3:b0128df93d26 266 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 267 uint32_t STSpin240_250::Stspin240_250_GetFwVersion(void)
davide.aliprandi@st.com 3:b0128df93d26 268 {
davide.aliprandi@st.com 3:b0128df93d26 269 return (STSPIN240_250_FW_VERSION);
davide.aliprandi@st.com 3:b0128df93d26 270 }
davide.aliprandi@st.com 3:b0128df93d26 271
davide.aliprandi@st.com 3:b0128df93d26 272
davide.aliprandi@st.com 3:b0128df93d26 273 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 274 * @brief Return the duty cycle of PWM used for REF
davide.aliprandi@st.com 3:b0128df93d26 275 * @param[in] refId 0 is the only supported id for Stspin240 or
davide.aliprandi@st.com 3:b0128df93d26 276 * Stspin250
davide.aliprandi@st.com 3:b0128df93d26 277 * @retval duty cycle in % (from 0 to 100)
davide.aliprandi@st.com 3:b0128df93d26 278 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 279 uint8_t STSpin240_250::Stspin240_250_GetRefPwmDc(uint8_t refId)
davide.aliprandi@st.com 3:b0128df93d26 280 {
davide.aliprandi@st.com 3:b0128df93d26 281 uint32_t duty = 0;
davide.aliprandi@st.com 3:b0128df93d26 282
davide.aliprandi@st.com 3:b0128df93d26 283 if (duty == 0)
davide.aliprandi@st.com 3:b0128df93d26 284 {
davide.aliprandi@st.com 3:b0128df93d26 285 duty = device_prm.refPwmDc;
davide.aliprandi@st.com 3:b0128df93d26 286 }
davide.aliprandi@st.com 3:b0128df93d26 287 return (duty);
davide.aliprandi@st.com 3:b0128df93d26 288 }
davide.aliprandi@st.com 3:b0128df93d26 289
davide.aliprandi@st.com 3:b0128df93d26 290 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 291 * @brief Return the frequency of PWM used for REF
davide.aliprandi@st.com 3:b0128df93d26 292 * @param[in] refId 0 is the only supported id for Stspin240 or
davide.aliprandi@st.com 3:b0128df93d26 293 * Stspin250
davide.aliprandi@st.com 3:b0128df93d26 294 * @retval Frequency in Hz
davide.aliprandi@st.com 3:b0128df93d26 295 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 296 uint32_t STSpin240_250::Stspin240_250_GetRefPwmFreq(uint8_t refId)
davide.aliprandi@st.com 3:b0128df93d26 297 {
davide.aliprandi@st.com 3:b0128df93d26 298 uint32_t freq = 0;
davide.aliprandi@st.com 3:b0128df93d26 299
davide.aliprandi@st.com 3:b0128df93d26 300 if (refId == 0)
davide.aliprandi@st.com 3:b0128df93d26 301 {
davide.aliprandi@st.com 3:b0128df93d26 302 freq = device_prm.refPwmFreq;
davide.aliprandi@st.com 3:b0128df93d26 303 }
davide.aliprandi@st.com 3:b0128df93d26 304 return (freq);
davide.aliprandi@st.com 3:b0128df93d26 305 }
davide.aliprandi@st.com 3:b0128df93d26 306
davide.aliprandi@st.com 3:b0128df93d26 307 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 308 * @brief Immediatly stops the motor and disable the power bridge
davide.aliprandi@st.com 3:b0128df93d26 309 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 310 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 311 * @note As all motors uses the same power bridge, the
davide.aliprandi@st.com 3:b0128df93d26 312 * power bridge will be disable only if all motors are
davide.aliprandi@st.com 3:b0128df93d26 313 * stopped
davide.aliprandi@st.com 3:b0128df93d26 314 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 315 void STSpin240_250::Stspin240_250_HardHiz(uint8_t motorId)
davide.aliprandi@st.com 3:b0128df93d26 316 {
davide.aliprandi@st.com 3:b0128df93d26 317 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 318 {
davide.aliprandi@st.com 3:b0128df93d26 319 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 320 }
davide.aliprandi@st.com 3:b0128df93d26 321 else
davide.aliprandi@st.com 3:b0128df93d26 322 {
davide.aliprandi@st.com 3:b0128df93d26 323 if (device_prm.bridgeEnabled[motorId] != FALSE)
davide.aliprandi@st.com 3:b0128df93d26 324 {
davide.aliprandi@st.com 3:b0128df93d26 325 /* Only disable bridges if dual bridge mode is disabled or */
davide.aliprandi@st.com 3:b0128df93d26 326 /* if all motors are inactive as there are sharing the same power stage */
davide.aliprandi@st.com 3:b0128df93d26 327 if ((device_prm.dualBridgeEnabled == 0)||
davide.aliprandi@st.com 3:b0128df93d26 328 ((motorId == 0)&&(device_prm.motionState[1] == INACTIVE))||
davide.aliprandi@st.com 3:b0128df93d26 329 ((motorId == 1)&&(device_prm.motionState[0] == INACTIVE)))
davide.aliprandi@st.com 3:b0128df93d26 330 {
davide.aliprandi@st.com 3:b0128df93d26 331 /* Disable the bridge */
davide.aliprandi@st.com 3:b0128df93d26 332 Stspin240_250_DisableBridge(motorId);
davide.aliprandi@st.com 3:b0128df93d26 333 }
davide.aliprandi@st.com 3:b0128df93d26 334 }
davide.aliprandi@st.com 3:b0128df93d26 335 /* Disable the PWM */
davide.aliprandi@st.com 3:b0128df93d26 336 Stspin240_250_HardStop(motorId);
davide.aliprandi@st.com 3:b0128df93d26 337 }
davide.aliprandi@st.com 3:b0128df93d26 338 }
davide.aliprandi@st.com 3:b0128df93d26 339
davide.aliprandi@st.com 3:b0128df93d26 340 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 341 * @brief Stops the motor without disabling the bridge
davide.aliprandi@st.com 3:b0128df93d26 342 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 343 * @retval none
davide.aliprandi@st.com 3:b0128df93d26 344 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 345 void STSpin240_250::Stspin240_250_HardStop(uint8_t motorId)
davide.aliprandi@st.com 3:b0128df93d26 346 {
davide.aliprandi@st.com 3:b0128df93d26 347 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 348 {
davide.aliprandi@st.com 3:b0128df93d26 349 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 350 }
davide.aliprandi@st.com 3:b0128df93d26 351 else if (device_prm.motionState[motorId] != INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 352 {
davide.aliprandi@st.com 3:b0128df93d26 353 /* Disable corresponding PWM */
davide.aliprandi@st.com 3:b0128df93d26 354 Stspin240_250_Board_PwmStop(motorId);
davide.aliprandi@st.com 3:b0128df93d26 355
davide.aliprandi@st.com 3:b0128df93d26 356 /* Set inactive state */
davide.aliprandi@st.com 3:b0128df93d26 357 device_prm.motionState[motorId] = INACTIVE;
davide.aliprandi@st.com 3:b0128df93d26 358 }
davide.aliprandi@st.com 3:b0128df93d26 359 }
davide.aliprandi@st.com 3:b0128df93d26 360
davide.aliprandi@st.com 3:b0128df93d26 361 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 362 * @brief Release reset (exit standby mode)
davide.aliprandi@st.com 3:b0128df93d26 363 * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES -1)
davide.aliprandi@st.com 3:b0128df93d26 364 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 365 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 366 void STSpin240_250::Stspin240_250_Releasereset(void)
davide.aliprandi@st.com 3:b0128df93d26 367 {
davide.aliprandi@st.com 3:b0128df93d26 368 Stspin240_250_Board_ReleaseReset();
davide.aliprandi@st.com 3:b0128df93d26 369
davide.aliprandi@st.com 3:b0128df93d26 370 /* Start PWM used for REF pin */
davide.aliprandi@st.com 3:b0128df93d26 371 Stspin240_250_Board_PwmSetFreq(PWM_REF, device_prm.refPwmFreq,device_prm.refPwmDc);
davide.aliprandi@st.com 3:b0128df93d26 372 }
davide.aliprandi@st.com 3:b0128df93d26 373
davide.aliprandi@st.com 3:b0128df93d26 374 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 375 * @brief Reset (enter standby mode)
davide.aliprandi@st.com 3:b0128df93d26 376 * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES -1)
davide.aliprandi@st.com 3:b0128df93d26 377 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 378 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 379 void STSpin240_250::Stspin240_250_reset(void)
davide.aliprandi@st.com 3:b0128df93d26 380 {
davide.aliprandi@st.com 3:b0128df93d26 381 uint8_t loop;
davide.aliprandi@st.com 3:b0128df93d26 382 for (loop = 0; loop < STSPIN240_250_NB_MAX_MOTORS; loop++)
davide.aliprandi@st.com 3:b0128df93d26 383 {
davide.aliprandi@st.com 3:b0128df93d26 384 /* Stop motor if needed*/
davide.aliprandi@st.com 3:b0128df93d26 385 if (device_prm.motionState[loop] != INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 386 {
davide.aliprandi@st.com 3:b0128df93d26 387 Stspin240_250_HardStop(loop);
davide.aliprandi@st.com 3:b0128df93d26 388 }
davide.aliprandi@st.com 3:b0128df93d26 389 /* Disable bridge if needed */
davide.aliprandi@st.com 3:b0128df93d26 390 if (device_prm.bridgeEnabled[loop] != FALSE)
davide.aliprandi@st.com 3:b0128df93d26 391 {
davide.aliprandi@st.com 3:b0128df93d26 392 Stspin240_250_DisableBridge(loop);
davide.aliprandi@st.com 3:b0128df93d26 393 }
davide.aliprandi@st.com 3:b0128df93d26 394 }
davide.aliprandi@st.com 3:b0128df93d26 395
davide.aliprandi@st.com 3:b0128df93d26 396 /* Stop PWM used for REF pin */
davide.aliprandi@st.com 3:b0128df93d26 397 Stspin240_250_Board_PwmStop(PWM_REF);
davide.aliprandi@st.com 3:b0128df93d26 398
davide.aliprandi@st.com 3:b0128df93d26 399 /* Reset the STBY/RESET pin */
davide.aliprandi@st.com 3:b0128df93d26 400 Stspin240_250_Board_Reset();
davide.aliprandi@st.com 3:b0128df93d26 401 }
davide.aliprandi@st.com 3:b0128df93d26 402
davide.aliprandi@st.com 3:b0128df93d26 403 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 404 * @brief Runs the motor
davide.aliprandi@st.com 3:b0128df93d26 405 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 406 * @param[in] direction FORWARD or BACKWARD
davide.aliprandi@st.com 3:b0128df93d26 407 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 408 * @note For unidirectionnal motor, direction parameter has
davide.aliprandi@st.com 3:b0128df93d26 409 * no effect
davide.aliprandi@st.com 3:b0128df93d26 410 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 411 void STSpin240_250::Stspin240_250_Run(uint8_t motorId, motorDir_t direction)
davide.aliprandi@st.com 3:b0128df93d26 412 {
davide.aliprandi@st.com 3:b0128df93d26 413 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 414 {
davide.aliprandi@st.com 3:b0128df93d26 415 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 416 }
davide.aliprandi@st.com 3:b0128df93d26 417 else if ((device_prm.motionState[motorId] == INACTIVE) ||
davide.aliprandi@st.com 3:b0128df93d26 418 (device_prm.direction[motorId] != direction))
davide.aliprandi@st.com 3:b0128df93d26 419 {
davide.aliprandi@st.com 3:b0128df93d26 420
davide.aliprandi@st.com 3:b0128df93d26 421 /* Release reset if required */
davide.aliprandi@st.com 3:b0128df93d26 422 if (Stspin240_250_GetResetState() == 0)
davide.aliprandi@st.com 3:b0128df93d26 423 {
davide.aliprandi@st.com 3:b0128df93d26 424 Stspin240_250_ReleaseReset();
davide.aliprandi@st.com 3:b0128df93d26 425 }
davide.aliprandi@st.com 3:b0128df93d26 426
davide.aliprandi@st.com 3:b0128df93d26 427 /* Eventually deactivate motor */
davide.aliprandi@st.com 3:b0128df93d26 428 if (device_prm.motionState[motorId] != INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 429 {
davide.aliprandi@st.com 3:b0128df93d26 430 Stspin240_250_HardStop(motorId);
davide.aliprandi@st.com 3:b0128df93d26 431 }
davide.aliprandi@st.com 3:b0128df93d26 432
davide.aliprandi@st.com 3:b0128df93d26 433 /* Set direction */
davide.aliprandi@st.com 3:b0128df93d26 434 Stspin240_250_SetDirection(motorId, direction);
davide.aliprandi@st.com 3:b0128df93d26 435
davide.aliprandi@st.com 3:b0128df93d26 436 /* Switch to steady state */
davide.aliprandi@st.com 3:b0128df93d26 437 device_prm.motionState[motorId] = STEADY;
davide.aliprandi@st.com 3:b0128df93d26 438
davide.aliprandi@st.com 3:b0128df93d26 439 /* Enable bridge */
davide.aliprandi@st.com 3:b0128df93d26 440 if (device_prm.bridgeEnabled[motorId] == FALSE)
davide.aliprandi@st.com 3:b0128df93d26 441 {
davide.aliprandi@st.com 3:b0128df93d26 442 Stspin240_250_EnableBridge(motorId);
davide.aliprandi@st.com 3:b0128df93d26 443 }
davide.aliprandi@st.com 3:b0128df93d26 444 /* Set PWM */
davide.aliprandi@st.com 3:b0128df93d26 445 Stspin240_250_Board_PwmSetFreq(motorId, device_prm.bridgePwmFreq[motorId],device_prm.speed[motorId]);
davide.aliprandi@st.com 3:b0128df93d26 446 }
davide.aliprandi@st.com 3:b0128df93d26 447 }
davide.aliprandi@st.com 3:b0128df93d26 448
davide.aliprandi@st.com 3:b0128df93d26 449 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 450 * @brief Changes the PWM frequency of the bridge input
davide.aliprandi@st.com 3:b0128df93d26 451 * @param[in] bridgeId 0 for bridge A, 1 for bridge B
davide.aliprandi@st.com 3:b0128df93d26 452 * @param[in] newFreq in Hz
davide.aliprandi@st.com 3:b0128df93d26 453 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 454 * @note 1)The PWM is only enabled when the motor is requested
davide.aliprandi@st.com 3:b0128df93d26 455 * to run.
davide.aliprandi@st.com 3:b0128df93d26 456 * 2) If the two bridges share the same timer, their frequency
davide.aliprandi@st.com 3:b0128df93d26 457 * has to be the same
davide.aliprandi@st.com 3:b0128df93d26 458 * 3) If the two bridges share the same timer, the frequency
davide.aliprandi@st.com 3:b0128df93d26 459 * is updated on the fly is there is only one motor running
davide.aliprandi@st.com 3:b0128df93d26 460 * on the targeted bridge.
davide.aliprandi@st.com 3:b0128df93d26 461 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 462 void STSpin240_250::Stspin240_250_SetBridgeInputPwmFreq(uint8_t bridgeId, uint32_t newFreq)
davide.aliprandi@st.com 3:b0128df93d26 463 {
davide.aliprandi@st.com 3:b0128df93d26 464 if (bridgeId > BRIDGE_B)
davide.aliprandi@st.com 3:b0128df93d26 465 {
davide.aliprandi@st.com 3:b0128df93d26 466 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_2);
davide.aliprandi@st.com 3:b0128df93d26 467 }
davide.aliprandi@st.com 3:b0128df93d26 468
davide.aliprandi@st.com 3:b0128df93d26 469 if (newFreq > STSPIN240_250_MAX_PWM_FREQ)
davide.aliprandi@st.com 3:b0128df93d26 470 {
davide.aliprandi@st.com 3:b0128df93d26 471 newFreq = STSPIN240_250_MAX_PWM_FREQ;
davide.aliprandi@st.com 3:b0128df93d26 472 }
davide.aliprandi@st.com 3:b0128df93d26 473
davide.aliprandi@st.com 3:b0128df93d26 474 device_prm.bridgePwmFreq[bridgeId] = newFreq;
davide.aliprandi@st.com 3:b0128df93d26 475
davide.aliprandi@st.com 3:b0128df93d26 476 if (device_prm.motionState[bridgeId] != INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 477 {
davide.aliprandi@st.com 3:b0128df93d26 478 Stspin240_250_Board_PwmSetFreq(bridgeId, device_prm.bridgePwmFreq[bridgeId],device_prm.speed[bridgeId]);
davide.aliprandi@st.com 3:b0128df93d26 479 }
davide.aliprandi@st.com 3:b0128df93d26 480 }
davide.aliprandi@st.com 3:b0128df93d26 481
davide.aliprandi@st.com 3:b0128df93d26 482 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 483 * @brief Specifies the direction
davide.aliprandi@st.com 3:b0128df93d26 484 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 485 * @param[in] dir FORWARD or BACKWARD
davide.aliprandi@st.com 3:b0128df93d26 486 * @note The direction change is only applied if the device
davide.aliprandi@st.com 3:b0128df93d26 487 * is in INACTIVE state. To change direction while motor is
davide.aliprandi@st.com 3:b0128df93d26 488 * running, use the run function
davide.aliprandi@st.com 3:b0128df93d26 489 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 490 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 491 void STSpin240_250::Stspin240_250_SetDirection(uint8_t motorId, motorDir_t dir)
davide.aliprandi@st.com 3:b0128df93d26 492 {
davide.aliprandi@st.com 3:b0128df93d26 493 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 494 {
davide.aliprandi@st.com 3:b0128df93d26 495 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 496 }
davide.aliprandi@st.com 3:b0128df93d26 497 else if (device_prm.motionState[motorId] == INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 498 {
davide.aliprandi@st.com 3:b0128df93d26 499 Stspin240_250_Board_SetDirectionGpio(motorId, dir);
davide.aliprandi@st.com 3:b0128df93d26 500 device_prm.direction[motorId] = dir;
davide.aliprandi@st.com 3:b0128df93d26 501 }
davide.aliprandi@st.com 3:b0128df93d26 502 }
davide.aliprandi@st.com 3:b0128df93d26 503
davide.aliprandi@st.com 3:b0128df93d26 504 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 505 * @brief Set the dual bridge configuration mode
davide.aliprandi@st.com 3:b0128df93d26 506 * @param[in] enable 0 to disable,
davide.aliprandi@st.com 3:b0128df93d26 507 * 1 to enable (not supported by STSPIN250)
davide.aliprandi@st.com 3:b0128df93d26 508 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 509 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 510 void STSpin240_250::Stspin240_250_SetDualFullBridgeconfig(uint8_t enable)
davide.aliprandi@st.com 3:b0128df93d26 511 {
davide.aliprandi@st.com 3:b0128df93d26 512 device_prm.dualBridgeEnabled = enable;
davide.aliprandi@st.com 3:b0128df93d26 513
davide.aliprandi@st.com 3:b0128df93d26 514 /* Check reset pin state*/
davide.aliprandi@st.com 3:b0128df93d26 515 if (Stspin240_250_GetResetState() != 0)
davide.aliprandi@st.com 3:b0128df93d26 516 {
davide.aliprandi@st.com 3:b0128df93d26 517 Stspin240_250_Reset();
davide.aliprandi@st.com 3:b0128df93d26 518 Stspin240_250_ReleaseReset();
davide.aliprandi@st.com 3:b0128df93d26 519 }
davide.aliprandi@st.com 3:b0128df93d26 520 }
davide.aliprandi@st.com 3:b0128df93d26 521
davide.aliprandi@st.com 3:b0128df93d26 522 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 523 * @brief Changes the max speed of the specified device
davide.aliprandi@st.com 3:b0128df93d26 524 * @param[in] motorId from 0 to (MAX_NUMBER_OF_BRUSH_DC_MOTORS - 1)
davide.aliprandi@st.com 3:b0128df93d26 525 * @param[in] newMaxSpeed in % from 0 to 100
davide.aliprandi@st.com 3:b0128df93d26 526 * @retval true if the command is successfully executed, else false
davide.aliprandi@st.com 3:b0128df93d26 527 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 528 bool STSpin240_250::Stspin240_250_SetMaxSpeed(uint8_t motorId, uint16_t newMaxSpeed)
davide.aliprandi@st.com 3:b0128df93d26 529 {
davide.aliprandi@st.com 3:b0128df93d26 530 bool cmdExecuted = FALSE;
davide.aliprandi@st.com 3:b0128df93d26 531
davide.aliprandi@st.com 3:b0128df93d26 532 if (motorId >= arrayNbMaxMotorsByConfig[device_prm.dualBridgeEnabled])
davide.aliprandi@st.com 3:b0128df93d26 533 {
davide.aliprandi@st.com 3:b0128df93d26 534 Stspin240_250_ErrorHandler(STSPIN240_250_ERROR_1);
davide.aliprandi@st.com 3:b0128df93d26 535 }
davide.aliprandi@st.com 3:b0128df93d26 536 else
davide.aliprandi@st.com 3:b0128df93d26 537 {
davide.aliprandi@st.com 3:b0128df93d26 538 device_prm.speed[motorId] = newMaxSpeed;
davide.aliprandi@st.com 3:b0128df93d26 539 if (device_prm.motionState[motorId] != INACTIVE)
davide.aliprandi@st.com 3:b0128df93d26 540 {
davide.aliprandi@st.com 3:b0128df93d26 541 /* Set PWM frequency*/
davide.aliprandi@st.com 3:b0128df93d26 542 Stspin240_250_Board_PwmSetFreq(motorId, device_prm.bridgePwmFreq[motorId],device_prm.speed[motorId]);
davide.aliprandi@st.com 3:b0128df93d26 543 }
davide.aliprandi@st.com 3:b0128df93d26 544 cmdExecuted = TRUE;
davide.aliprandi@st.com 3:b0128df93d26 545 }
davide.aliprandi@st.com 3:b0128df93d26 546 return cmdExecuted;
davide.aliprandi@st.com 3:b0128df93d26 547 }
davide.aliprandi@st.com 3:b0128df93d26 548
davide.aliprandi@st.com 3:b0128df93d26 549 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 550 * @brief Changes the duty cycle of the PWM used for REF
davide.aliprandi@st.com 3:b0128df93d26 551 * @param[in] refId 0 is the only supported id for Stspin240 or
davide.aliprandi@st.com 3:b0128df93d26 552 * Stspin250
davide.aliprandi@st.com 3:b0128df93d26 553 * @param[in] newDc new duty cycle from 0 to 100
davide.aliprandi@st.com 3:b0128df93d26 554 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 555 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 556 void STSpin240_250::Stspin240_250_SetRefPwmDc(uint8_t refId, uint8_t newDc)
davide.aliprandi@st.com 3:b0128df93d26 557 {
davide.aliprandi@st.com 3:b0128df93d26 558 if (newDc > 100)
davide.aliprandi@st.com 3:b0128df93d26 559 {
davide.aliprandi@st.com 3:b0128df93d26 560 newDc = 100;
davide.aliprandi@st.com 3:b0128df93d26 561 }
davide.aliprandi@st.com 3:b0128df93d26 562
davide.aliprandi@st.com 3:b0128df93d26 563 device_prm.refPwmDc = newDc;
davide.aliprandi@st.com 3:b0128df93d26 564
davide.aliprandi@st.com 3:b0128df93d26 565 if (Stspin240_250_GetResetState() != 0)
davide.aliprandi@st.com 3:b0128df93d26 566 {
davide.aliprandi@st.com 3:b0128df93d26 567 /* Immediatly set the PWM frequency for ref if chip is not in reset */
davide.aliprandi@st.com 3:b0128df93d26 568 Stspin240_250_Board_PwmSetFreq(PWM_REF, device_prm.refPwmFreq,device_prm.refPwmDc);
davide.aliprandi@st.com 3:b0128df93d26 569 }
davide.aliprandi@st.com 3:b0128df93d26 570 }
davide.aliprandi@st.com 3:b0128df93d26 571
davide.aliprandi@st.com 3:b0128df93d26 572 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 573 * @brief Changes the frequency of PWM used for REF
davide.aliprandi@st.com 3:b0128df93d26 574 * @param[in] refId 0 is the only supported id for Stspin240 or
davide.aliprandi@st.com 3:b0128df93d26 575 * Stspin250
davide.aliprandi@st.com 3:b0128df93d26 576 * @param[in] newFreq in Hz
davide.aliprandi@st.com 3:b0128df93d26 577 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 578 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 579 void STSpin240_250::Stspin240_250_SetRefPwmFreq(uint8_t refId, uint32_t newFreq)
davide.aliprandi@st.com 3:b0128df93d26 580 {
davide.aliprandi@st.com 3:b0128df93d26 581 if (newFreq > STSPIN240_250_MAX_PWM_FREQ)
davide.aliprandi@st.com 3:b0128df93d26 582 {
davide.aliprandi@st.com 3:b0128df93d26 583 newFreq = STSPIN240_250_MAX_PWM_FREQ;
davide.aliprandi@st.com 3:b0128df93d26 584 }
davide.aliprandi@st.com 3:b0128df93d26 585
davide.aliprandi@st.com 3:b0128df93d26 586 device_prm.refPwmFreq = newFreq;
davide.aliprandi@st.com 3:b0128df93d26 587
davide.aliprandi@st.com 3:b0128df93d26 588 if (Stspin240_250_GetResetState() != 0)
davide.aliprandi@st.com 3:b0128df93d26 589 {
davide.aliprandi@st.com 3:b0128df93d26 590 /* Immediatly set the PWM frequency for ref if chip is not in reset */
davide.aliprandi@st.com 3:b0128df93d26 591 Stspin240_250_Board_PwmSetFreq(PWM_REF, device_prm.refPwmFreq,device_prm.refPwmDc);
davide.aliprandi@st.com 3:b0128df93d26 592 }
davide.aliprandi@st.com 3:b0128df93d26 593 }
davide.aliprandi@st.com 3:b0128df93d26 594
davide.aliprandi@st.com 3:b0128df93d26 595
davide.aliprandi@st.com 3:b0128df93d26 596 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 597 * @brief Get the status of the bridge enabling of the corresponding bridge
davide.aliprandi@st.com 3:b0128df93d26 598 * @retval State of the Enable&Fault pin (shared for bridge A and B)
davide.aliprandi@st.com 3:b0128df93d26 599 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 600 uint8_t STSpin240_250::Stspin240_250_GetResetState(void)
davide.aliprandi@st.com 3:b0128df93d26 601 {
davide.aliprandi@st.com 3:b0128df93d26 602 uint8_t status = Stspin240_250_Board_GetResetPinState();
davide.aliprandi@st.com 3:b0128df93d26 603
davide.aliprandi@st.com 3:b0128df93d26 604 return (status);
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 * @brief Set the parameters of the device to values of pInitPrm structure
davide.aliprandi@st.com 3:b0128df93d26 609 * @param[in] deviceId (from 0 to MAX_NUMBER_OF_DEVICES -1)
davide.aliprandi@st.com 3:b0128df93d26 610 * @param pInitPrm pointer to a structure containing the initial device parameters
davide.aliprandi@st.com 3:b0128df93d26 611 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 612 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 613 void STSpin240_250::Stspin240_250_SetDeviceParamsToGivenValues(Stspin240_250_init_t *pInitPrm)
davide.aliprandi@st.com 3:b0128df93d26 614 {
davide.aliprandi@st.com 3:b0128df93d26 615 uint32_t i;
davide.aliprandi@st.com 3:b0128df93d26 616
davide.aliprandi@st.com 3:b0128df93d26 617 device_prm.dualBridgeEnabled = pInitPrm->dualBridgeEnabled;
davide.aliprandi@st.com 3:b0128df93d26 618
davide.aliprandi@st.com 3:b0128df93d26 619 device_prm.bridgePwmFreq[BRIDGE_A] = pInitPrm->bridgePwmFreq[BRIDGE_A];
davide.aliprandi@st.com 3:b0128df93d26 620 device_prm.bridgePwmFreq[BRIDGE_B] = pInitPrm->bridgePwmFreq[BRIDGE_B];;
davide.aliprandi@st.com 3:b0128df93d26 621
davide.aliprandi@st.com 3:b0128df93d26 622 device_prm.refPwmFreq = pInitPrm->refPwmFreq;
davide.aliprandi@st.com 3:b0128df93d26 623 device_prm.refPwmDc = pInitPrm->refPwmDc;
davide.aliprandi@st.com 3:b0128df93d26 624
davide.aliprandi@st.com 3:b0128df93d26 625 for (i = 0; i < MAX_NUMBER_OF_BRUSH_DC_MOTORS; i++)
davide.aliprandi@st.com 3:b0128df93d26 626 {
davide.aliprandi@st.com 3:b0128df93d26 627 device_prm.speed[i] = 100;
davide.aliprandi@st.com 3:b0128df93d26 628 device_prm.direction[i] = FORWARD;
davide.aliprandi@st.com 3:b0128df93d26 629 device_prm.motionState[i] = INACTIVE;
davide.aliprandi@st.com 3:b0128df93d26 630 }
davide.aliprandi@st.com 3:b0128df93d26 631 for (i = 0; i < STSPIN240_250_NB_BRIDGES; i++)
davide.aliprandi@st.com 3:b0128df93d26 632 {
davide.aliprandi@st.com 3:b0128df93d26 633 device_prm.bridgeEnabled[i] = FALSE;
davide.aliprandi@st.com 3:b0128df93d26 634 }
davide.aliprandi@st.com 3:b0128df93d26 635 }
davide.aliprandi@st.com 3:b0128df93d26 636 /******************************************************//**
davide.aliprandi@st.com 3:b0128df93d26 637 * @brief Sets the parameters of the device to predefined values
davide.aliprandi@st.com 3:b0128df93d26 638 * from stspin240_250_target_config.h
davide.aliprandi@st.com 3:b0128df93d26 639 * @retval None
davide.aliprandi@st.com 3:b0128df93d26 640 **********************************************************/
davide.aliprandi@st.com 3:b0128df93d26 641 void STSpin240_250::Stspin240_250_SetDeviceParamsToPredefinedValues(void)
davide.aliprandi@st.com 3:b0128df93d26 642 {
davide.aliprandi@st.com 3:b0128df93d26 643 uint32_t i;
davide.aliprandi@st.com 3:b0128df93d26 644
davide.aliprandi@st.com 3:b0128df93d26 645 device_prm.dualBridgeEnabled = STSPIN240_250_CONF_PARAM_DUAL_BRIDGE_ENABLING;
davide.aliprandi@st.com 3:b0128df93d26 646
davide.aliprandi@st.com 3:b0128df93d26 647 device_prm.bridgePwmFreq[BRIDGE_A] = STSPIN240_250_CONF_PARAM_FREQ_PWM_A;
davide.aliprandi@st.com 3:b0128df93d26 648 device_prm.bridgePwmFreq[BRIDGE_B] = STSPIN240_250_CONF_PARAM_FREQ_PWM_B;
davide.aliprandi@st.com 3:b0128df93d26 649
davide.aliprandi@st.com 3:b0128df93d26 650 device_prm.refPwmFreq = STSPIN240_250_CONF_PARAM_FREQ_PWM_REF;
davide.aliprandi@st.com 3:b0128df93d26 651 device_prm.refPwmDc = STSPIN240_250_CONF_PARAM_DC_PWM_REF;
davide.aliprandi@st.com 3:b0128df93d26 652
davide.aliprandi@st.com 3:b0128df93d26 653 for (i = 0; i < MAX_NUMBER_OF_BRUSH_DC_MOTORS; i++)
davide.aliprandi@st.com 3:b0128df93d26 654 {
davide.aliprandi@st.com 3:b0128df93d26 655 device_prm.speed[i] = 100;
davide.aliprandi@st.com 3:b0128df93d26 656 device_prm.direction[i] = FORWARD;
davide.aliprandi@st.com 3:b0128df93d26 657 device_prm.motionState[i] = INACTIVE;
davide.aliprandi@st.com 3:b0128df93d26 658 }
davide.aliprandi@st.com 3:b0128df93d26 659 for (i = 0; i < STSPIN240_250_NB_BRIDGES; i++)
davide.aliprandi@st.com 3:b0128df93d26 660 {
davide.aliprandi@st.com 3:b0128df93d26 661 device_prm.bridgeEnabled[i] = FALSE;
davide.aliprandi@st.com 3:b0128df93d26 662 }
davide.aliprandi@st.com 3:b0128df93d26 663 }
davide.aliprandi@st.com 3:b0128df93d26 664 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/