X-CUBE-SPN1-20150128 example source code for one motor compiled under mbed. Tested OK on Nucleo F401. l6474.cpp is modified from original with defines in l6474_target_config.h to select the original behaviour (motor de-energised when halted), or new mode to continue powering with a (reduced) current in the coils (braking/position hold capability). On F401 avoid using mbed's InterruptIn on pins 10-15 (any port). Beware of other conflicts! L0 & F0 are included but untested.

Dependencies:   mbed

Committer:
gregeric
Date:
Tue Oct 13 10:46:01 2015 +0000
Revision:
6:19c1b4a04c24
Parent:
0:b9444a40a999
Ensure bridge is disabled before resetting the L6474.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gregeric 0:b9444a40a999 1 /**
gregeric 0:b9444a40a999 2 ******************************************************************************
gregeric 0:b9444a40a999 3 * @file motor.h
gregeric 0:b9444a40a999 4 * @author IPC Rennes
gregeric 0:b9444a40a999 5 * @version V1.3.0
gregeric 0:b9444a40a999 6 * @date November 12, 2014
gregeric 0:b9444a40a999 7 * @brief This file contains all the functions prototypes for motor drivers.
gregeric 0:b9444a40a999 8 ******************************************************************************
gregeric 0:b9444a40a999 9 * @attention
gregeric 0:b9444a40a999 10 *
gregeric 0:b9444a40a999 11 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
gregeric 0:b9444a40a999 12 *
gregeric 0:b9444a40a999 13 * Redistribution and use in source and binary forms, with or without modification,
gregeric 0:b9444a40a999 14 * are permitted provided that the following conditions are met:
gregeric 0:b9444a40a999 15 * 1. Redistributions of source code must retain the above copyright notice,
gregeric 0:b9444a40a999 16 * this list of conditions and the following disclaimer.
gregeric 0:b9444a40a999 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
gregeric 0:b9444a40a999 18 * this list of conditions and the following disclaimer in the documentation
gregeric 0:b9444a40a999 19 * and/or other materials provided with the distribution.
gregeric 0:b9444a40a999 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
gregeric 0:b9444a40a999 21 * may be used to endorse or promote products derived from this software
gregeric 0:b9444a40a999 22 * without specific prior written permission.
gregeric 0:b9444a40a999 23 *
gregeric 0:b9444a40a999 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
gregeric 0:b9444a40a999 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
gregeric 0:b9444a40a999 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
gregeric 0:b9444a40a999 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
gregeric 0:b9444a40a999 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
gregeric 0:b9444a40a999 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
gregeric 0:b9444a40a999 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
gregeric 0:b9444a40a999 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
gregeric 0:b9444a40a999 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
gregeric 0:b9444a40a999 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
gregeric 0:b9444a40a999 34 *
gregeric 0:b9444a40a999 35 ******************************************************************************
gregeric 0:b9444a40a999 36 */
gregeric 0:b9444a40a999 37
gregeric 0:b9444a40a999 38 /* Define to prevent recursive inclusion -------------------------------------*/
gregeric 0:b9444a40a999 39 #ifndef __MOTOR_H
gregeric 0:b9444a40a999 40 #define __MOTOR_H
gregeric 0:b9444a40a999 41
gregeric 0:b9444a40a999 42 #ifdef __cplusplus
gregeric 0:b9444a40a999 43 extern "C" {
gregeric 0:b9444a40a999 44 #endif
gregeric 0:b9444a40a999 45
gregeric 0:b9444a40a999 46 /* Includes ------------------------------------------------------------------*/
gregeric 0:b9444a40a999 47 #include <stdint.h>
gregeric 0:b9444a40a999 48
gregeric 0:b9444a40a999 49 /** @addtogroup BSP
gregeric 0:b9444a40a999 50 * @{
gregeric 0:b9444a40a999 51 */
gregeric 0:b9444a40a999 52
gregeric 0:b9444a40a999 53 /** @addtogroup Components
gregeric 0:b9444a40a999 54 * @{
gregeric 0:b9444a40a999 55 */
gregeric 0:b9444a40a999 56
gregeric 0:b9444a40a999 57 /** @defgroup Motor
gregeric 0:b9444a40a999 58 * @{
gregeric 0:b9444a40a999 59 */
gregeric 0:b9444a40a999 60
gregeric 0:b9444a40a999 61 /** @defgroup Motor_Exported_Constants
gregeric 0:b9444a40a999 62 * @{
gregeric 0:b9444a40a999 63 */
gregeric 0:b9444a40a999 64
gregeric 0:b9444a40a999 65 /// boolean for false condition
gregeric 0:b9444a40a999 66 #ifndef FALSE
gregeric 0:b9444a40a999 67 #define FALSE (0)
gregeric 0:b9444a40a999 68 #endif
gregeric 0:b9444a40a999 69 /// boolean for true condition
gregeric 0:b9444a40a999 70 #ifndef TRUE
gregeric 0:b9444a40a999 71 #define TRUE (1)
gregeric 0:b9444a40a999 72 #endif
gregeric 0:b9444a40a999 73
gregeric 0:b9444a40a999 74 /**
gregeric 0:b9444a40a999 75 * @}
gregeric 0:b9444a40a999 76 */
gregeric 0:b9444a40a999 77
gregeric 0:b9444a40a999 78 /** @defgroup Motor_Exported_Types
gregeric 0:b9444a40a999 79 * @{
gregeric 0:b9444a40a999 80 */
gregeric 0:b9444a40a999 81
gregeric 0:b9444a40a999 82 /** @defgroup Motor_Boolean_Type
gregeric 0:b9444a40a999 83 * @{
gregeric 0:b9444a40a999 84 */
gregeric 0:b9444a40a999 85 ///bool Type
gregeric 0:b9444a40a999 86 //mbed typedef uint8_t bool;
gregeric 0:b9444a40a999 87 /**
gregeric 0:b9444a40a999 88 * @}
gregeric 0:b9444a40a999 89 */
gregeric 0:b9444a40a999 90
gregeric 0:b9444a40a999 91 /** @defgroup Device_Direction_Options
gregeric 0:b9444a40a999 92 * @{
gregeric 0:b9444a40a999 93 */
gregeric 0:b9444a40a999 94 /// Direction options
gregeric 0:b9444a40a999 95 typedef enum {
gregeric 0:b9444a40a999 96 BACKWARD = 0,
gregeric 0:b9444a40a999 97 FORWARD = 1
gregeric 0:b9444a40a999 98 } motorDir_t;
gregeric 0:b9444a40a999 99 /**
gregeric 0:b9444a40a999 100 * @}
gregeric 0:b9444a40a999 101 */
gregeric 0:b9444a40a999 102
gregeric 0:b9444a40a999 103 /** @defgroup Device_Action_Options
gregeric 0:b9444a40a999 104 * @{
gregeric 0:b9444a40a999 105 */
gregeric 0:b9444a40a999 106 /// Action options
gregeric 0:b9444a40a999 107 typedef enum {
gregeric 0:b9444a40a999 108 ACTION_RESET = ((uint8_t)0x00),
gregeric 0:b9444a40a999 109 ACTION_COPY = ((uint8_t)0x08)
gregeric 0:b9444a40a999 110 } motorAction_t;
gregeric 0:b9444a40a999 111 /**
gregeric 0:b9444a40a999 112 * @}
gregeric 0:b9444a40a999 113 */
gregeric 0:b9444a40a999 114
gregeric 0:b9444a40a999 115 /** @defgroup Device_States
gregeric 0:b9444a40a999 116 * @{
gregeric 0:b9444a40a999 117 */
gregeric 0:b9444a40a999 118 /// Device states
gregeric 0:b9444a40a999 119 typedef enum {
gregeric 0:b9444a40a999 120 ACCELERATING = 0,
gregeric 0:b9444a40a999 121 DECELERATING = 1,
gregeric 0:b9444a40a999 122 STEADY = 2,
gregeric 0:b9444a40a999 123 INACTIVE= 3
gregeric 0:b9444a40a999 124 } motorState_t;
gregeric 0:b9444a40a999 125 /**
gregeric 0:b9444a40a999 126 * @}
gregeric 0:b9444a40a999 127 */
gregeric 0:b9444a40a999 128
gregeric 0:b9444a40a999 129 /** @defgroup Device_Step_mode
gregeric 0:b9444a40a999 130 * @{
gregeric 0:b9444a40a999 131 */
gregeric 0:b9444a40a999 132 /// Stepping options
gregeric 0:b9444a40a999 133 typedef enum {
gregeric 0:b9444a40a999 134 STEP_MODE_FULL = ((uint8_t)0x00),
gregeric 0:b9444a40a999 135 STEP_MODE_HALF = ((uint8_t)0x01),
gregeric 0:b9444a40a999 136 STEP_MODE_1_4 = ((uint8_t)0x02),
gregeric 0:b9444a40a999 137 STEP_MODE_1_8 = ((uint8_t)0x03),
gregeric 0:b9444a40a999 138 STEP_MODE_1_16 = ((uint8_t)0x04),
gregeric 0:b9444a40a999 139 STEP_MODE_1_32 = ((uint8_t)0x05),
gregeric 0:b9444a40a999 140 STEP_MODE_1_64 = ((uint8_t)0x06),
gregeric 0:b9444a40a999 141 STEP_MODE_1_128 = ((uint8_t)0x07)
gregeric 0:b9444a40a999 142 } motorStepMode_t;
gregeric 0:b9444a40a999 143 /**
gregeric 0:b9444a40a999 144 * @}
gregeric 0:b9444a40a999 145 */
gregeric 0:b9444a40a999 146
gregeric 0:b9444a40a999 147
gregeric 0:b9444a40a999 148 /** @defgroup Motor_Driver_Structure
gregeric 0:b9444a40a999 149 * @{
gregeric 0:b9444a40a999 150 */
gregeric 0:b9444a40a999 151 /// Motor driver structure definition
gregeric 0:b9444a40a999 152 typedef struct
gregeric 0:b9444a40a999 153 {
gregeric 0:b9444a40a999 154 /// Function pointer to Init
gregeric 0:b9444a40a999 155 void (*Init)(uint8_t);
gregeric 0:b9444a40a999 156 /// Function pointer to ReadID
gregeric 0:b9444a40a999 157 uint16_t (*ReadID)(void);
gregeric 0:b9444a40a999 158 /// Function pointer to AttachErrorHandler
gregeric 0:b9444a40a999 159 void(*AttachErrorHandler)(void (*callback)(uint16_t));
gregeric 0:b9444a40a999 160 /// Function pointer to AttachFlagInterrupt
gregeric 0:b9444a40a999 161 void (*AttachFlagInterrupt)(void (*callback)(void));
gregeric 0:b9444a40a999 162 /// Function pointer to AttachBusyInterrupt
gregeric 0:b9444a40a999 163 void (*AttachBusyInterrupt)(void (*callback)(void));
gregeric 0:b9444a40a999 164 /// Function pointer to FlagInterruptHandler
gregeric 0:b9444a40a999 165 void (*FlagInterruptHandler)(void);
gregeric 0:b9444a40a999 166 /// Function pointer to GetAcceleration
gregeric 0:b9444a40a999 167 uint16_t (*GetAcceleration)(uint8_t);
gregeric 0:b9444a40a999 168 /// Function pointer to GetCurrentSpeed
gregeric 0:b9444a40a999 169 uint16_t (*GetCurrentSpeed)(uint8_t);
gregeric 0:b9444a40a999 170 /// Function pointer to GetDeceleration
gregeric 0:b9444a40a999 171 uint16_t (*GetDeceleration)(uint8_t);
gregeric 0:b9444a40a999 172 /// Function pointer to GetDeviceState
gregeric 0:b9444a40a999 173 motorState_t(*GetDeviceState)(uint8_t);
gregeric 0:b9444a40a999 174 /// Function pointer to GetFwVersion
gregeric 0:b9444a40a999 175 uint8_t (*GetFwVersion)(void);
gregeric 0:b9444a40a999 176 /// Function pointer to GetMark
gregeric 0:b9444a40a999 177 int32_t (*GetMark)(uint8_t);
gregeric 0:b9444a40a999 178 /// Function pointer to GetMaxSpeed
gregeric 0:b9444a40a999 179 uint16_t (*GetMaxSpeed)(uint8_t);
gregeric 0:b9444a40a999 180 /// Function pointer to GetMinSpeed
gregeric 0:b9444a40a999 181 uint16_t (*GetMinSpeed)(uint8_t);
gregeric 0:b9444a40a999 182 /// Function pointer to GetPosition
gregeric 0:b9444a40a999 183 int32_t (*GetPosition)(uint8_t);
gregeric 0:b9444a40a999 184 /// Function pointer to GoHome
gregeric 0:b9444a40a999 185 void (*GoHome)(uint8_t);
gregeric 0:b9444a40a999 186 /// Function pointer to GoMark
gregeric 0:b9444a40a999 187 void (*GoMark)(uint8_t);
gregeric 0:b9444a40a999 188 /// Function pointer to GoTo
gregeric 0:b9444a40a999 189 void (*GoTo)(uint8_t, int32_t);
gregeric 0:b9444a40a999 190 /// Function pointer to HardStop
gregeric 0:b9444a40a999 191 void (*HardStop)(uint8_t);
gregeric 0:b9444a40a999 192 /// Function pointer to Move
gregeric 0:b9444a40a999 193 void (*Move)(uint8_t, motorDir_t, uint32_t );
gregeric 0:b9444a40a999 194 /// Function pointer to ResetAllDevices
gregeric 0:b9444a40a999 195 void (*ResetAllDevices)(void);
gregeric 0:b9444a40a999 196 /// Function pointer to Run
gregeric 0:b9444a40a999 197 void (*Run)(uint8_t, motorDir_t);
gregeric 0:b9444a40a999 198 /// Function pointer to SetAcceleration
gregeric 0:b9444a40a999 199 bool(*SetAcceleration)(uint8_t ,uint16_t );
gregeric 0:b9444a40a999 200 /// Function pointer to SetDeceleration
gregeric 0:b9444a40a999 201 bool(*SetDeceleration)(uint8_t , uint16_t );
gregeric 0:b9444a40a999 202 /// Function pointer to SetHome
gregeric 0:b9444a40a999 203 void (*SetHome)(uint8_t);
gregeric 0:b9444a40a999 204 /// Function pointer to SetMark
gregeric 0:b9444a40a999 205 void (*SetMark)(uint8_t);
gregeric 0:b9444a40a999 206 /// Function pointer to SetMaxSpeed
gregeric 0:b9444a40a999 207 bool (*SetMaxSpeed)(uint8_t, uint16_t );
gregeric 0:b9444a40a999 208 /// Function pointer to SetMinSpeed
gregeric 0:b9444a40a999 209 bool (*SetMinSpeed)(uint8_t, uint16_t );
gregeric 0:b9444a40a999 210 /// Function pointer to SoftStop
gregeric 0:b9444a40a999 211 bool (*SoftStop)(uint8_t);
gregeric 0:b9444a40a999 212 /// Function pointer to StepClockHandler
gregeric 0:b9444a40a999 213 void (*StepClockHandler)(uint8_t deviceId);
gregeric 0:b9444a40a999 214 /// Function pointer to WaitWhileActive
gregeric 0:b9444a40a999 215 void (*WaitWhileActive)(uint8_t);
gregeric 0:b9444a40a999 216 /// Function pointer to CmdDisable
gregeric 0:b9444a40a999 217 void (*CmdDisable)(uint8_t);
gregeric 0:b9444a40a999 218 /// Function pointer to CmdEnable
gregeric 0:b9444a40a999 219 void (*CmdEnable)(uint8_t);
gregeric 0:b9444a40a999 220 /// Function pointer to CmdGetParam
gregeric 0:b9444a40a999 221 uint32_t (*CmdGetParam)(uint8_t, uint32_t);
gregeric 0:b9444a40a999 222 /// Function pointer to CmdGetStatus
gregeric 0:b9444a40a999 223 uint16_t (*CmdGetStatus)(uint8_t);
gregeric 0:b9444a40a999 224 /// Function pointer to CmdNop
gregeric 0:b9444a40a999 225 void (*CmdNop)(uint8_t);
gregeric 0:b9444a40a999 226 /// Function pointer to CmdSetParam
gregeric 0:b9444a40a999 227 void (*CmdSetParam)(uint8_t, uint32_t, uint32_t);
gregeric 0:b9444a40a999 228 /// Function pointer to ReadStatusRegister
gregeric 0:b9444a40a999 229 uint16_t (*ReadStatusRegister)(uint8_t);
gregeric 0:b9444a40a999 230 /// Function pointer to ReleaseReset
gregeric 0:b9444a40a999 231 void (*ReleaseReset)(void);
gregeric 0:b9444a40a999 232 /// Function pointer to Reset
gregeric 0:b9444a40a999 233 void (*Reset)(void);
gregeric 0:b9444a40a999 234 /// Function pointer to SelectStepMode
gregeric 0:b9444a40a999 235 void (*SelectStepMode)(uint8_t deviceId, motorStepMode_t);
gregeric 0:b9444a40a999 236 /// Function pointer to SetDirection
gregeric 0:b9444a40a999 237 void (*SetDirection)(uint8_t, motorDir_t);
gregeric 0:b9444a40a999 238 /// Function pointer to CmdGoToDir
gregeric 0:b9444a40a999 239 void (*CmdGoToDir)(uint8_t, motorDir_t, int32_t);
gregeric 0:b9444a40a999 240 /// Function pointer to CheckBusyHw
gregeric 0:b9444a40a999 241 uint8_t (*CheckBusyHw)(void);
gregeric 0:b9444a40a999 242 /// Function pointer to CheckStatusHw
gregeric 0:b9444a40a999 243 uint8_t (*CheckStatusHw)(void);
gregeric 0:b9444a40a999 244 /// Function pointer to CmdGoUntil
gregeric 0:b9444a40a999 245 void (*CmdGoUntil)(uint8_t, motorAction_t, motorDir_t, uint32_t);
gregeric 0:b9444a40a999 246 /// Function pointer to CmdHardHiZ
gregeric 0:b9444a40a999 247 void (*CmdHardHiZ)(uint8_t);
gregeric 0:b9444a40a999 248 /// Function pointer to CmdReleaseSw
gregeric 0:b9444a40a999 249 void (*CmdReleaseSw)(uint8_t, motorAction_t, motorDir_t);
gregeric 0:b9444a40a999 250 /// Function pointer to CmdResetDevice
gregeric 0:b9444a40a999 251 void (*CmdResetDevice)(uint8_t);
gregeric 0:b9444a40a999 252 /// Function pointer to CmdResetPos
gregeric 0:b9444a40a999 253 void (*CmdResetPos)(uint8_t);
gregeric 0:b9444a40a999 254 /// Function pointer to CmdRun
gregeric 0:b9444a40a999 255 void (*CmdRun)(uint8_t, motorDir_t, uint32_t);
gregeric 0:b9444a40a999 256 /// Function pointer to CmdSoftHiZ
gregeric 0:b9444a40a999 257 void (*CmdSoftHiZ)(uint8_t);
gregeric 0:b9444a40a999 258 /// Function pointer to CmdStepClock
gregeric 0:b9444a40a999 259 void (*CmdStepClock)(uint8_t, motorDir_t);
gregeric 0:b9444a40a999 260 /// Function pointer to FetchAndClearAllStatus
gregeric 0:b9444a40a999 261 void (*FetchAndClearAllStatus)(void);
gregeric 0:b9444a40a999 262 /// Function pointer to GetFetchedStatus
gregeric 0:b9444a40a999 263 uint16_t (*GetFetchedStatus)(uint8_t);
gregeric 0:b9444a40a999 264 /// Function pointer to GetNbDevices
gregeric 0:b9444a40a999 265 uint8_t (*GetNbDevices)(void);
gregeric 0:b9444a40a999 266 /// Function pointer to IsDeviceBusy
gregeric 0:b9444a40a999 267 bool (*IsDeviceBusy)(uint8_t);
gregeric 0:b9444a40a999 268 /// Function pointer to SendQueuedCommands
gregeric 0:b9444a40a999 269 void (*SendQueuedCommands)(void);
gregeric 0:b9444a40a999 270 /// Function pointer to QueueCommands
gregeric 0:b9444a40a999 271 void (*QueueCommands)(uint8_t, uint8_t, uint32_t);
gregeric 0:b9444a40a999 272 /// Function pointer to WaitForAllDevicesNotBusy
gregeric 0:b9444a40a999 273 void (*WaitForAllDevicesNotBusy)(void);
gregeric 0:b9444a40a999 274 /// Function pointer to ErrorHandler
gregeric 0:b9444a40a999 275 void (*ErrorHandler)(uint16_t);
gregeric 0:b9444a40a999 276 /// Function pointer to BusyInterruptHandler
gregeric 0:b9444a40a999 277 void (*BusyInterruptHandler)(void);
gregeric 0:b9444a40a999 278 void (*CmdSoftStop)(uint8_t);
gregeric 0:b9444a40a999 279 }motorDrv_t;
gregeric 0:b9444a40a999 280
gregeric 0:b9444a40a999 281 /**
gregeric 0:b9444a40a999 282
gregeric 0:b9444a40a999 283 * @}
gregeric 0:b9444a40a999 284 */
gregeric 0:b9444a40a999 285
gregeric 0:b9444a40a999 286 /**
gregeric 0:b9444a40a999 287 * @}
gregeric 0:b9444a40a999 288 */
gregeric 0:b9444a40a999 289
gregeric 0:b9444a40a999 290 /**
gregeric 0:b9444a40a999 291 * @}
gregeric 0:b9444a40a999 292 */
gregeric 0:b9444a40a999 293
gregeric 0:b9444a40a999 294 /**
gregeric 0:b9444a40a999 295 * @}
gregeric 0:b9444a40a999 296 */
gregeric 0:b9444a40a999 297
gregeric 0:b9444a40a999 298 /**
gregeric 0:b9444a40a999 299 * @}
gregeric 0:b9444a40a999 300 */
gregeric 0:b9444a40a999 301
gregeric 0:b9444a40a999 302 #ifdef __cplusplus
gregeric 0:b9444a40a999 303 }
gregeric 0:b9444a40a999 304 #endif
gregeric 0:b9444a40a999 305
gregeric 0:b9444a40a999 306 #endif /* __MOTOR_H */
gregeric 0:b9444a40a999 307
gregeric 0:b9444a40a999 308 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/