You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

l6208.h 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /******************************************************//**
  2. * @file l6208.h
  3. * @author IPC Rennes
  4. * @version V1.5.0
  5. * @date June 1st, 2018
  6. * @brief Header for l6208.c module
  7. * @note (C) COPYRIGHT 2016 STMicroelectronics
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef _L6208_H_INCLUDED
  39. #define _L6208_H_INCLUDED
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "l6208_target_config.h"
  45. #include "motor.h"
  46. /** @addtogroup BSP
  47. * @{
  48. */
  49. /** @addtogroup L6208
  50. * @{
  51. */
  52. /* Exported Constants --------------------------------------------------------*/
  53. /** @addtogroup L6208_Exported_Constants
  54. * @{
  55. */
  56. /// Current FW major version
  57. #define L6208_FW_MAJOR_VERSION (uint8_t)(1)
  58. /// Current FW minor version
  59. #define L6208_FW_MINOR_VERSION (uint8_t)(4)
  60. /// Current FW patch version
  61. #define L6208_FW_PATCH_VERSION (uint8_t)(0)
  62. /// Current FW version
  63. #define L6208_FW_VERSION (uint32_t)((L6208_FW_MAJOR_VERSION<<16)|\
  64. (L6208_FW_MINOR_VERSION<<8)|\
  65. (L6208_FW_PATCH_VERSION))
  66. /// Max position
  67. #define L6208_MAX_POSITION (0x7FFFFFFF)
  68. /// Min position
  69. #define L6208_MIN_POSITION (0x80000000)
  70. /// Position range
  71. #define L6208_POSITION_RANGE ((uint32_t)(L6208_MAX_POSITION -\
  72. L6208_MIN_POSITION))
  73. /// micro step samples per period/4
  74. #define L6208_USTEPS_PER_QUARTER_PERIOD (16)
  75. /// minimum speed
  76. #define L6208_MIN_SPEED (16)
  77. /// minimum acceleration and deceleration rate
  78. #define L6208_MIN_ACC_DEC_RATE (24)
  79. /// Mask for HiZ bit in motorDecayMode_t enum
  80. #define L6208_FAST_DECAY_MODE_MASK (0x1)
  81. /// L6208 error base number
  82. #define L6208_ERROR_BASE (0x9000)
  83. /**
  84. * @}
  85. */
  86. /* Exported types ------------------------------------------------------------*/
  87. /** @defgroup Error_Types Error Types
  88. * @{
  89. */
  90. /// Errors
  91. typedef enum {
  92. L6208_ERROR_SET_HOME = L6208_ERROR_BASE, /// Error while setting home position
  93. L6208_ERROR_SET_MAX_SPEED = L6208_ERROR_BASE + 1, /// Error while setting max speed
  94. L6208_ERROR_SET_MIN_SPEED = L6208_ERROR_BASE + 2, /// Error while setting min speed
  95. L6208_ERROR_SET_ACCELERATION = L6208_ERROR_BASE + 3, /// Error while setting acceleration
  96. L6208_ERROR_SET_DECELERATION = L6208_ERROR_BASE + 4, /// Error while setting decelaration
  97. L6208_ERROR_MCU_OSC_CONFIG = L6208_ERROR_BASE + 5, /// Error while configuring mcu oscillator
  98. L6208_ERROR_MCU_CLOCK_CONFIG = L6208_ERROR_BASE + 6, /// Error while configuring mcu clock
  99. L6208_ERROR_POSITION = L6208_ERROR_BASE + 7, /// Unexpected current position (wrong number of steps)
  100. L6208_ERROR_SPEED = L6208_ERROR_BASE + 8, /// Unexpected current speed
  101. L6208_ERROR_INIT = L6208_ERROR_BASE + 9, /// Unexpected number of devices
  102. L6208_ERROR_SET_DIRECTION = L6208_ERROR_BASE + 10,/// Error while setting direction
  103. L6208_ERROR_SET_STEP_MODE = L6208_ERROR_BASE + 11,/// Attempt to set an unsupported step mode
  104. L6208_ERROR_SET_PWM = L6208_ERROR_BASE + 12,/// Error while setting a PWM parameter
  105. }errorTypes_t;
  106. /**
  107. * @}
  108. */
  109. /** @defgroup Device_Parameters Device Parameters
  110. * @{
  111. */
  112. /// Device Parameters Structure Type
  113. typedef struct
  114. {
  115. /// dwelling waiting time counter (tick)
  116. volatile uint32_t dwellCounter;
  117. /// motor position indicator (tick)
  118. uint32_t ticks;
  119. /// LSByte copy of the previous position (tick)
  120. uint8_t lsbOldTicks;
  121. /// LSByte copy of the previous position (tick) ( micro stepping )
  122. uint8_t lsbOldUSteppingTicks;
  123. /// LSByte copy of the current position (tick)
  124. uint8_t lsbTicks;
  125. /// P1 = acceleration phase steps number (motor position control mode)
  126. uint32_t positionTarget1;
  127. /// P2 = constant speed steps number (motor position control mode)
  128. uint32_t positionTarget2;
  129. /// P3 = deceleration phase steps number (motor position control mode)
  130. uint32_t positionTarget3;
  131. /// P = total move distance in steps (motor position control mode)
  132. uint32_t positionTarget;
  133. /// absolute motor position in microsteps (motor position control mode)
  134. volatile int32_t absolutePos;
  135. /// mark position in microsteps (motor position control mode)
  136. volatile int32_t markPos;
  137. /// motor position in microsteps (motor position control mode)
  138. volatile uint32_t step;
  139. /// dwelling time after position got (ms)
  140. volatile uint16_t moveDwellTime;
  141. /// number of micro stepping waveform samples to be rescaled according to selected torque value
  142. volatile uint8_t uStepsample2scale;
  143. /// number of micro stepping waveform samples to be updated into the waveform scanning table
  144. volatile uint8_t uStepsample2update;
  145. /// microstepping waveform sample index
  146. volatile uint8_t uStepSample;
  147. /// system status flags
  148. volatile uint32_t flags;
  149. /// current stepper state machine index
  150. volatile motorState_t motionState;
  151. /// current step mode
  152. volatile motorStepMode_t stepMode;
  153. /// micro stepping waveform scanning sample index increment
  154. uint8_t uStepInc;
  155. /// frequency of the VREFA and VREFB PWM
  156. uint32_t vrefPwmFreq;
  157. /// current selected torque value
  158. volatile uint8_t curTorqueScaler;
  159. /// constant speed phase torque value (%)
  160. volatile uint8_t runTorque;
  161. /// acceleration phase torque value (%)
  162. volatile uint8_t accelTorque;
  163. /// deceleration phase torque value (%)
  164. volatile uint8_t decelTorque;
  165. /// holding phase torque value (%)
  166. volatile uint8_t holdTorque;
  167. /// acceleration (steps/s^2)
  168. volatile uint16_t accelerationSps2;
  169. /// deceleration (steps/s^2)
  170. volatile uint16_t decelerationSps2;
  171. /// acceleration (steps/tick^2)
  172. volatile uint16_t accelerationSpt2;
  173. /// deceleration (steps/tick^2)
  174. volatile uint16_t decelerationSpt2;
  175. /// maximum speed (steps/s)
  176. volatile uint16_t maxSpeedSps;
  177. /// minimum speed (steps/s)
  178. volatile uint16_t minSpeedSps;
  179. /// current speed (steps/s)
  180. volatile uint16_t speedSps;
  181. /// maximum speed (steps/tick)
  182. volatile uint32_t maxSpeedSpt;
  183. /// minimum speed (steps/tick)
  184. volatile uint32_t minSpeedSpt;
  185. /// current speed (steps/tick)
  186. volatile uint32_t speedSpt;
  187. }deviceParams_t;
  188. /**
  189. * @}
  190. */
  191. /// Motor driver initialization structure definition
  192. typedef struct
  193. {
  194. /// acceleration (steps/s^2)
  195. uint16_t accelerationSps2;
  196. /// acceleration phase torque value (%)
  197. uint8_t accelTorque;
  198. /// deceleration (steps/s^2)
  199. uint16_t decelerationSps2;
  200. /// deceleration phase torque value (%)
  201. uint8_t decelTorque;
  202. /// maximum speed (steps/s)
  203. uint16_t maxSpeedSps;
  204. /// constant speed phase torque value (%)
  205. uint8_t runTorque;
  206. /// holding phase torque value (%)
  207. uint8_t holdTorque;
  208. /// current step mode
  209. motorStepMode_t stepMode;
  210. /// current decay mode (SLOW_DECAY or FAST_DECAY)
  211. motorDecayMode_t decayMode;
  212. /// dwelling time after position got (ms)
  213. uint16_t moveDwellTime;
  214. /// automatic HiZ on stop
  215. bool autoHiZstop;
  216. /// frequency of the VREFA and VREFB PWM
  217. uint32_t vrefPwmFreq;
  218. } l6208_Init_t;
  219. /* Exported functions --------------------------------------------------------*/
  220. /** @defgroup L6208_Exported_Functions L6208 Exported Functions
  221. * @{
  222. */
  223. motorDrv_t* L6208_GetMotorHandle(void); //Return handle of the motor driver handle
  224. void L6208_Init(void* pInit); //Start the L6208 library
  225. uint16_t L6208_ReadId(void); //Read Id to get driver instance
  226. void L6208_AttachErrorHandler(void (*callback)(uint16_t error)); //Attach a user callback to the error Handler
  227. void L6208_AttachFlagInterrupt(void (*callback)(void)); //Attach a user callback to the flag Interrupt
  228. uint8_t L6208_CheckStatusHw(void); //Check if L6208 has a fault by reading EN pin position
  229. void L6208_Disable(uint8_t deviceId); //Disable the power bridge
  230. void L6208_ErrorHandler(uint16_t error); //Error handler which calls the user callback
  231. void L6208_Enable(uint8_t deviceId); //Enable the power bridge
  232. void L6208_FlagInterruptHandler(void); //Handler of the flag interrupt which calls the user callback (if defined)
  233. uint16_t L6208_GetAcceleration(uint8_t deviceId); //Return the acceleration in pps^2
  234. uint16_t L6208_GetCurrentSpeed(uint8_t deviceId); //Return the current speed in pps
  235. motorDecayMode_t L6208_GetDecayMode(uint8_t deviceId); //Get the motor decay mode
  236. uint16_t L6208_GetDeceleration(uint8_t deviceId); //Return the deceleration in pps^2
  237. motorDir_t L6208_GetDirection(uint8_t deviceId); //Get the motor current direction
  238. uint32_t L6208_GetFwVersion(void); //Return the FW version
  239. int32_t L6208_GetMark(uint8_t deviceId); //Get the mark position (32b signed)
  240. uint16_t L6208_GetMaxSpeed(uint8_t deviceId); //Return the max speed in pps
  241. uint16_t L6208_GetMinSpeed(uint8_t deviceId); //Return the min speed in pps
  242. motorState_t L6208_GetMotionState(uint8_t deviceId); //Return the motion state
  243. int32_t L6208_GetPosition(uint8_t deviceId); //Get the current position (32b signed)
  244. motorStepMode_t L6208_GetStepMode(uint8_t deviceId); //Get the motor step mode
  245. motorStopMode_t L6208_GetStopMode(uint8_t deviceId); //Get the selected mode to stop the motor
  246. uint8_t L6208_GetTorque(uint8_t deviceId, motorTorqueMode_t torqueMode); //Get the torque value according to the torque mode
  247. void L6208_GoHome(uint8_t deviceId); //Go to the home position
  248. void L6208_GoMark(uint8_t deviceId); //Go to the Mark position
  249. void L6208_GoTo(uint8_t deviceId, int32_t abs_pos); //Go to the specified position
  250. void L6208_GoToDir(uint8_t deviceId, motorDir_t direction, int32_t abs_pos); //Go to the specified position using the specified direction
  251. void L6208_HardHiZ(uint8_t deviceId); //Stop the motor by using the device deceleration and disables the power bridges
  252. void L6208_HardStop(uint8_t deviceId); //Stop the motor and keeps holding torque
  253. void L6208_Move(uint8_t deviceId, motorDir_t direction, uint32_t stepCount); //Move the motor by the specified number of steps in the specified direction
  254. void L6208_ReleaseReset(uint8_t deviceId); //Release the reset pin
  255. void L6208_Reset(uint8_t deviceId); //Set the reset pin
  256. void L6208_ResetDevice(void); //Reset L6208 device
  257. void L6208_Run(uint8_t deviceId, motorDir_t direction); //Run the motor in the specified direction
  258. bool L6208_SetAcceleration(uint8_t deviceId, uint16_t newAcc); //Set the acceleration in pps^2
  259. void L6208_SetDecayMode(uint8_t deviceId, motorDecayMode_t decayMode); //Set the motor decay mode
  260. bool L6208_SetDeceleration(uint8_t deviceId, uint16_t newDec); //Set the deceleration in pps^2
  261. void L6208_SetDirection(uint8_t deviceId, motorDir_t direction); //Set the motor direction
  262. void L6208_SetHome(uint8_t deviceId, int32_t homePos); //Set the home position
  263. void L6208_SetMark(uint8_t deviceId, int32_t markPos); //Set the mark position
  264. bool L6208_SetMaxSpeed(uint8_t deviceId, uint16_t volatile newSpeed); //Set the max speed value in pps
  265. bool L6208_SetMinSpeed(uint8_t deviceId, uint16_t volatile newSpeed); //Set the max speed value in pps
  266. bool L6208_SetNbDevices(uint8_t nbDevices); //Set the number of devices
  267. bool L6208_SetStepMode(uint8_t deviceId, motorStepMode_t stepMode); //Set the motor step mode
  268. void L6208_SetStopMode(uint8_t deviceId, motorStopMode_t stopMode); //Select the mode to stop the motor
  269. void L6208_SetTorque(uint8_t deviceId, motorTorqueMode_t torqueMode, uint8_t torqueValue); //Set the torque value according to the torque mode
  270. bool L6208_SoftStop(uint8_t deviceId); //Progressively stop the motor by using the device deceleration and set deceleration torque
  271. void L6208_TickHandler(uint8_t deviceId); //Handle the device state machine at each tick timer pulse end
  272. uint32_t L6208_VrefPwmGetFreq(uint8_t deviceId); //Get the frequency of VREFA and VREFB PWM
  273. void L6208_VrefPwmSetFreq(uint8_t deviceId, uint32_t newFreq); //Set the frequency of the VREFA and VREFB PWM
  274. void L6208_WaitWhileActive(uint8_t deviceId); //Wait for the device state becomes Inactive
  275. /**
  276. * @}
  277. */
  278. /** @defgroup MotorControl_Board_Linked_Functions MotorControl Board Linked Functions
  279. * @{
  280. */
  281. ///Delay of the requested number of milliseconds
  282. extern void L6208_Board_Delay(uint32_t delay);
  283. ///Enable Irq
  284. extern void L6208_Board_EnableIrq(void);
  285. ///Disable Irq
  286. extern void L6208_Board_DisableIrq(void);
  287. ///Initialise GPIOs used for L6208
  288. extern void L6208_Board_GpioInit(void);
  289. //Initialize the VREFA or VREFB PWM
  290. extern bool L6208_Board_VrefPwmInit(uint8_t bridgeId, uint32_t pwmFreq);
  291. ///Set duty cycle of VREFA or VREFB PWM
  292. extern bool L6208_Board_VrefPwmSetDutyCycle(uint8_t bridgeId,\
  293. uint16_t value,\
  294. bool valueIsPwmDutyCycle);
  295. ///Start the timer for the VREFA or VREFB PWM
  296. extern bool L6208_Board_VrefPwmStart(uint8_t bridgeId,\
  297. uint32_t pwmFreq);
  298. ///Stop the timer for the VREFA or VREFB PWM
  299. extern bool L6208_Board_VrefPwmStop(uint8_t bridgeId);
  300. ///Get the period of VREFA and VREFB PWM
  301. extern uint32_t L6208_Board_VrefPwmGetPeriod(void);
  302. ///Check that the new VREFA and VREFB PWM frequency is nor too low nor too high
  303. bool L6208_Board_VrefPwmFreqCheck(uint32_t newFreq);
  304. ///Initialize the tick
  305. extern void L6208_Board_TickInit(void);
  306. ///Start the timer for the tick by using the set tick frequency
  307. extern void L6208_Board_TickStart(void);
  308. ///Stop the timer for the tick
  309. extern void L6208_Board_TickStop(void);
  310. ///Get the tick frequency in Hz
  311. extern uint32_t L6208_Board_TickGetFreq(void);
  312. ///Release the reset pin
  313. extern void L6208_Board_ReleaseReset(void);
  314. ///Set the reset pin
  315. extern void L6208_Board_Reset(void);
  316. ///Set the control pin
  317. extern void L6208_Board_CONTROL_PIN_Set(void);
  318. ///Reset the control pin
  319. extern void L6208_Board_CONTROL_PIN_Reset(void);
  320. ///Set the clock pin
  321. extern void L6208_Board_CLOCK_PIN_Set(void);
  322. ///Reset the clock pin
  323. extern void L6208_Board_CLOCK_PIN_Reset(void);
  324. ///Set the half full pin
  325. extern void L6208_Board_HALF_FULL_PIN_Set(void);
  326. ///Reset the half full pin
  327. extern void L6208_Board_HALF_FULL_PIN_Reset(void);
  328. ///Set the dir pin
  329. extern void L6208_Board_DIR_PIN_Set(void);
  330. ///Reset the dir pin
  331. extern void L6208_Board_DIR_PIN_Reset(void);
  332. ///Returns the EN pin state
  333. extern uint32_t L6208_Board_FLAG_PIN_GetState(void);
  334. ///Enable the power bridges (leave the output bridges HiZ)
  335. extern void L6208_Board_Enable(void);
  336. ///Disable the power bridges (leave the output bridges HiZ)
  337. extern void L6208_Board_Disable(void);
  338. /**
  339. * @}
  340. */
  341. /**
  342. * @}
  343. */
  344. /**
  345. * @}
  346. */
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif /* __L6208_H */
  351. /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/