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.

x_nucleo_ihm05a1_stm32f4xx.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /**
  2. ******************************************************************************
  3. * @file x_nucleo_ihm05a1_stm32f4xx.c
  4. * @author IPC Rennes
  5. * @version V1.5.0
  6. * @date June 1st, 2018
  7. * @brief BSP driver for x-nucleo-ihm05a1 Nucleo extension board
  8. * (based on L6208)
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "x_nucleo_ihm05a1_stm32f4xx.h"
  40. #include "motor.h"
  41. /** @addtogroup BSP
  42. * @{
  43. */
  44. /** @defgroup X_NUCLEO_IHM05A1_STM32F4XX NUCLEO IHM05A1 STM32F4XX
  45. * @{
  46. */
  47. /* Private constants ---------------------------------------------------------*/
  48. /** @defgroup IHM05A1_Private_Constants IHM05A1 Private Constants
  49. * @{
  50. */
  51. /// Tick frequency (Hz)
  52. #define TIMER_TICK_FREQUENCY (10000)
  53. /// Tick Timer Prescaler
  54. #define TIMER_TICK_PRESCALER (64)
  55. /// MCU wait time after power bridges are enabled
  56. #define BRIDGE_TURN_ON_DELAY (10)
  57. /**
  58. * @}
  59. */
  60. /* Private variables ---------------------------------------------------------*/
  61. /** @defgroup IHM05A1_Board_Private_Variables IHM05A1 Board Private Variables
  62. * @{
  63. */
  64. /// L6208 timer handler for VREFA PWM
  65. TIM_HandleTypeDef hTimVrefaPwm;
  66. /// L6208 timer handler for VREFB PWM
  67. TIM_HandleTypeDef hTimVrefbPwm;
  68. /// L6208 timer handler for the tick
  69. TIM_HandleTypeDef hTimTick;
  70. /**
  71. * @}
  72. */
  73. /** @defgroup IHM05A1_Board_Private_Function_Prototypes IHM05A1 Board Private Function Prototypes
  74. * @{
  75. */
  76. void L6208_Board_CLOCK_PIN_Reset(void); //Reset the clock pin
  77. void L6208_Board_CLOCK_PIN_Set(void); //Set the clock pin
  78. void L6208_Board_CONTROL_PIN_Reset(void); //Reset the control pin
  79. void L6208_Board_CONTROL_PIN_Set(void); //Set the control pin
  80. void L6208_Board_Delay(uint32_t delay); //Delay of the requested number of milliseconds
  81. void L6208_Board_DIR_PIN_Reset(void); //Reset the dir pin
  82. void L6208_Board_DIR_PIN_Set(void); //Set the dir pin
  83. void L6208_Board_Disable(void); //Disable the power bridges (leave the output bridges HiZ)
  84. void L6208_Board_DisableIrq(void); //Disable Irq
  85. void L6208_Board_Enable(void); //Enable the power bridges (leave the output bridges HiZ)
  86. void L6208_Board_EnableIrq(void); //Disable Irq
  87. uint32_t L6208_Board_FLAG_PIN_GetState(void); //Returns the EN pin state
  88. void L6208_Board_GpioInit(void); //Initialise GPIOs used for L6208
  89. void L6208_Board_HALF_FULL_PIN_Reset(void); //Reset the half full pin
  90. void L6208_Board_HALF_FULL_PIN_Set(void); //Set the half full pin
  91. bool L6208_Board_VrefPwmInit(uint8_t bridgeId, uint32_t pwmFreq); //Initialize the VREFA or VREFB PWM
  92. void L6208_Board_ReleaseReset(void); //Release the reset pin
  93. void L6208_Board_Reset(void); //Reset the reset pin
  94. uint32_t L6208_Board_TickGetFreq(void); //Get the tick frequency in Hz
  95. void L6208_Board_TickInit(void); //Initialize the tick
  96. void L6208_Board_TickStart(void); //Start the timer for the tick by using the set tick frequency
  97. void L6208_Board_TickStop(void); //Stop the timer for the tick
  98. bool L6208_Board_VrefPwmFreqCheck(uint32_t newFreq); //Check that the new VREFA and VREFB PWM frequency is nor too low nor too high
  99. uint32_t L6208_Board_VrefPwmGetPeriod(void); //Get current VREF PWM period duration
  100. bool L6208_Board_VrefPwmSetDutyCycle(uint8_t bridgeId,\
  101. uint16_t value,\
  102. bool valueIsPwmDutyCycle); //Set duty cycle of VREFA or VREFB PWM
  103. bool L6208_Board_VrefPwmStart(uint8_t bridgeId,\
  104. uint32_t pwmFreq); //Start the timer for the VREFA or VREFB PWM
  105. bool L6208_Board_VrefPwmStop(uint8_t bridgeId); //Stop the timer for the VREFA or VREFB PWM
  106. /**
  107. * @}
  108. */
  109. /** @defgroup IHM05A1_Board_Private_Functions IHM05A1 Board Private Functions
  110. * @{
  111. */
  112. /******************************************************//**
  113. * @brief This function provides an accurate delay in milliseconds
  114. * @param[in] delay time length in milliseconds
  115. * @retval None
  116. **********************************************************/
  117. void L6208_Board_Delay(uint32_t delay)
  118. {
  119. HAL_Delay(delay);
  120. }
  121. /******************************************************//**
  122. * @brief This function disable the interruptions
  123. * @retval None
  124. **********************************************************/
  125. void L6208_Board_DisableIrq(void)
  126. {
  127. //__disable_irq();
  128. }
  129. /******************************************************//**
  130. * @brief This function enable the interruptions
  131. * @retval None
  132. **********************************************************/
  133. void L6208_Board_EnableIrq(void)
  134. {
  135. //__enable_irq();
  136. }
  137. /******************************************************//**
  138. * @brief Start the L6208 library
  139. * @retval None
  140. **********************************************************/
  141. void L6208_Board_GpioInit(void)
  142. {
  143. GPIO_InitTypeDef GPIO_InitStruct;
  144. /* GPIO Ports Clock Enable */
  145. __GPIOC_CLK_ENABLE();
  146. __GPIOA_CLK_ENABLE();
  147. __GPIOB_CLK_ENABLE();
  148. /* Configure L6208 - EN pin -------------------------------*/
  149. /* When this pin is set low, it is configured just before as */
  150. /* GPIO_MODE_OUTPUT_PP with GPIO_NOPULL */
  151. /* When this pin is set high, it is just after configured for OCD and OVT */
  152. /* as GPIO_MODE_IT_FALLING with GPIO_PULLUP */
  153. L6208_Board_Disable();
  154. /* Set Priority of External Line Interrupt used for the OCD OVT interrupt*/
  155. HAL_NVIC_SetPriority(FLAG_EXTI_LINE_IRQn, BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PRIORITY, 0);
  156. /* Enable the External Line Interrupt used for the OCD OVT interrupt*/
  157. HAL_NVIC_EnableIRQ(FLAG_EXTI_LINE_IRQn);
  158. /* Configure L6208 - CW/CCW pin ----------------------------*/
  159. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_PIN;
  160. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  161. GPIO_InitStruct.Pull = GPIO_NOPULL;
  162. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  163. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_PORT, &GPIO_InitStruct);
  164. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_PIN, GPIO_PIN_RESET);
  165. /* Configure L6208 - HALF/FULL pin -------------------------------------------*/
  166. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PIN;
  167. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  168. GPIO_InitStruct.Pull = GPIO_NOPULL;
  169. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  170. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PORT, &GPIO_InitStruct);
  171. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PORT, BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PIN, GPIO_PIN_RESET);
  172. /* Configure L6208 - CONTROL pin -------------------------------------------*/
  173. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_CONTROL_PIN;
  174. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  175. GPIO_InitStruct.Pull = GPIO_NOPULL;
  176. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  177. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_CONTROL_PORT, &GPIO_InitStruct);
  178. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CONTROL_PORT, BSP_MOTOR_CONTROL_BOARD_CONTROL_PIN, GPIO_PIN_RESET);
  179. /* Configure L6208 - CLOCK pin -------------------------------------------*/
  180. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_CLOCK_PIN;
  181. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  182. GPIO_InitStruct.Pull = GPIO_NOPULL;
  183. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  184. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_CLOCK_PORT, &GPIO_InitStruct);
  185. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CLOCK_PORT, BSP_MOTOR_CONTROL_BOARD_CLOCK_PIN, GPIO_PIN_RESET);
  186. /* Configure L6208 - STBY/RESET pin -------------------------------------*/
  187. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_RESET_PIN;
  188. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  189. GPIO_InitStruct.Pull = GPIO_NOPULL;
  190. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  191. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, &GPIO_InitStruct);
  192. L6208_Board_ReleaseReset();
  193. }
  194. /******************************************************//**
  195. * @brief Initialize the tick
  196. * @retval None
  197. **********************************************************/
  198. void L6208_Board_TickInit(void)
  199. {
  200. static TIM_OC_InitTypeDef sConfigOC;
  201. static TIM_MasterConfigTypeDef sMasterConfig;
  202. static TIM_ClockConfigTypeDef sClockSourceConfig;
  203. hTimTick.Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_TICK;
  204. hTimTick.Init.Period = (HAL_RCC_GetSysClockFreq() / (TIMER_TICK_PRESCALER * TIMER_TICK_FREQUENCY)) - 1;
  205. hTimTick.Init.Prescaler = TIMER_TICK_PRESCALER -1;
  206. hTimTick.Init.ClockDivision = 0;
  207. hTimTick.Init.CounterMode = TIM_COUNTERMODE_UP;
  208. HAL_TIM_PWM_Init(&hTimTick);
  209. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  210. HAL_TIM_ConfigClockSource(&hTimTick, &sClockSourceConfig);
  211. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  212. sConfigOC.Pulse = 0;
  213. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  214. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  215. HAL_TIM_PWM_ConfigChannel(&hTimTick, &sConfigOC, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_TICK);
  216. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  217. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  218. HAL_TIMEx_MasterConfigSynchronization(&hTimTick, &sMasterConfig);
  219. }
  220. /******************************************************//**
  221. * @brief Start the timer for the tick by using the set tick frequency
  222. * @retval None
  223. **********************************************************/
  224. void L6208_Board_TickStart(void)
  225. {
  226. uint32_t period = (HAL_RCC_GetSysClockFreq() / (TIMER_TICK_PRESCALER * TIMER_TICK_FREQUENCY)) - 1;
  227. __HAL_TIM_SetAutoreload(&hTimTick, period);
  228. __HAL_TIM_SetCompare(&hTimTick, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_TICK, period >> 1);
  229. HAL_TIM_PWM_Start_IT(&hTimTick, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_TICK);
  230. }
  231. /******************************************************//**
  232. * @brief Stop the timer for the tick
  233. * @retval None
  234. **********************************************************/
  235. void L6208_Board_TickStop(void)
  236. {
  237. HAL_TIM_PWM_Stop_IT(&hTimTick, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_TICK);
  238. }
  239. /******************************************************//**
  240. * @brief Get the tick timer frequency in Hz
  241. * @retval The tick timer frequency in Hz
  242. **********************************************************/
  243. uint32_t L6208_Board_TickGetFreq(void)
  244. {
  245. return TIMER_TICK_FREQUENCY;
  246. }
  247. /******************************************************//**
  248. * @brief Check that the frequency for the VREFA and VREFB PWM
  249. * is high enough
  250. * @param[in] newFreq frequency in Hz of the PWM used to generate
  251. * the reference voltage for the bridge
  252. * @retval FALSE if frequency is too low, else TRUE
  253. **********************************************************/
  254. bool L6208_Board_VrefPwmFreqCheck(uint32_t newFreq)
  255. {
  256. if (newFreq<=(HAL_RCC_GetSysClockFreq()>>16))
  257. {
  258. return FALSE;
  259. }
  260. else
  261. {
  262. return TRUE;
  263. }
  264. }
  265. /******************************************************//**
  266. * @brief Initialize the VREFA or VREFB PWM
  267. * @param[in] bridgeId
  268. * 0 for BRIDGE_A
  269. * 1 for BRIDGE_B
  270. * @param[in] pwmFreq frequency in Hz of the PWM used to generate the reference
  271. * voltage for the bridge
  272. * @retval FALSE if wrong timer handle is used, else TRUE
  273. * @note None
  274. **********************************************************/
  275. bool L6208_Board_VrefPwmInit(uint8_t bridgeId, uint32_t pwmFreq)
  276. {
  277. static TIM_OC_InitTypeDef sConfigOC;
  278. static TIM_MasterConfigTypeDef sMasterConfig;
  279. static TIM_ClockConfigTypeDef sClockSourceConfig;
  280. TIM_HandleTypeDef *pHTim = NULL;
  281. uint32_t channel;
  282. if (bridgeId == 0)
  283. {
  284. pHTim = &hTimVrefaPwm;
  285. pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_VREFA_PWM;
  286. pHTim->Init.Prescaler = 0;
  287. pHTim->Init.Period = (HAL_RCC_GetSysClockFreq() / pwmFreq) - 1;
  288. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFA_PWM;
  289. }
  290. else if (bridgeId == 1)
  291. {
  292. pHTim = &hTimVrefbPwm;
  293. pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_VREFB_PWM;
  294. pHTim->Init.Prescaler = 0;
  295. pHTim->Init.Period = (HAL_RCC_GetSysClockFreq() / pwmFreq) - 1;
  296. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFB_PWM;
  297. }
  298. else
  299. {
  300. return FALSE;
  301. }
  302. pHTim->Init.CounterMode = TIM_COUNTERMODE_UP;
  303. pHTim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  304. HAL_TIM_PWM_Init(pHTim);
  305. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  306. HAL_TIM_ConfigClockSource(pHTim, &sClockSourceConfig);
  307. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  308. sConfigOC.Pulse = 0;
  309. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  310. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  311. HAL_TIM_PWM_ConfigChannel(pHTim, &sConfigOC, channel);
  312. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  313. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  314. HAL_TIMEx_MasterConfigSynchronization(pHTim, &sMasterConfig);
  315. return TRUE;
  316. }
  317. /******************************************************//**
  318. * @brief Set duty cycle of VREFA or VREFB PWM
  319. * @param[in] bridgeId
  320. * 0 for BRIDGE_A
  321. * 1 for BRIDGE_B
  322. * @param[in] value pulse length or PWM duty cycle: 0 - 100 %
  323. * @param[in] valueIsPwmDutyCycle must be TRUE if value is a PWM duty cycle
  324. * @retval FALSE if wrong timer handle is used, else TRUE
  325. **********************************************************/
  326. bool L6208_Board_VrefPwmSetDutyCycle(uint8_t bridgeId, uint16_t value, bool valueIsPwmDutyCycle)
  327. {
  328. TIM_HandleTypeDef *pHTim = NULL;
  329. uint32_t channel;
  330. if (bridgeId == 0)
  331. {
  332. pHTim = &hTimVrefaPwm;
  333. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFA_PWM;
  334. }
  335. else if (bridgeId == 1)
  336. {
  337. pHTim = &hTimVrefbPwm;
  338. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFB_PWM;
  339. }
  340. else
  341. {
  342. return 0;
  343. }
  344. // PWM or OC Channel pulse length
  345. if(valueIsPwmDutyCycle)
  346. {
  347. if (value > 100) value = 100;
  348. value = (uint16_t)(((uint32_t)pHTim->Init.Period * (uint32_t)value) / 100);
  349. }
  350. __HAL_TIM_SetCompare(pHTim, channel, value);
  351. return 1;
  352. }
  353. /******************************************************//**
  354. * @brief Start the timer for the VREFA or VREFB PWM
  355. * @param[in] bridgeId
  356. * 0 for BRIDGE_A
  357. * 1 for BRIDGE_B
  358. * @param[in] pwmFreq frequency in Hz of the PWM used to generate the reference
  359. * voltage for the bridge
  360. * @retval FALSE if wrong timer handle is used, else TRUE
  361. **********************************************************/
  362. bool L6208_Board_VrefPwmStart(uint8_t bridgeId, uint32_t pwmFreq)
  363. {
  364. TIM_HandleTypeDef *pHTim = NULL;
  365. uint32_t period;
  366. uint32_t channel;
  367. if (bridgeId == 0)
  368. {
  369. pHTim = &hTimVrefaPwm;
  370. period = (HAL_RCC_GetSysClockFreq() / pwmFreq) - 1;
  371. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFA_PWM;
  372. }
  373. else if (bridgeId == 1)
  374. {
  375. pHTim = &hTimVrefbPwm;
  376. period = (HAL_RCC_GetSysClockFreq() / pwmFreq) - 1;
  377. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFB_PWM;
  378. }
  379. else
  380. {
  381. return FALSE;
  382. }
  383. __HAL_TIM_SetAutoreload(pHTim, period);
  384. HAL_TIM_PWM_Start(pHTim, channel);
  385. return TRUE;
  386. }
  387. /******************************************************//**
  388. * @brief Stop the VREFA or VREFB PWM
  389. * @param[in] bridgeId
  390. * 0 for BRIDGE_A
  391. * 1 for BRIDGE_B
  392. * @retval None
  393. **********************************************************/
  394. bool L6208_Board_VrefPwmStop(uint8_t bridgeId)
  395. {
  396. TIM_HandleTypeDef *pHTim = NULL;
  397. uint32_t channel;
  398. if (bridgeId == 0)
  399. {
  400. pHTim = &hTimVrefaPwm;
  401. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFA_PWM;
  402. }
  403. else if (bridgeId == 1)
  404. {
  405. pHTim = &hTimVrefbPwm;
  406. channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_VREFB_PWM;
  407. }
  408. else
  409. {
  410. return FALSE;
  411. }
  412. HAL_TIM_PWM_Stop(pHTim, channel);
  413. return TRUE;
  414. }
  415. /******************************************************//**
  416. * @brief Get current VREF PWM period duration
  417. * @retval the current VREF PWM period duration
  418. * @note hTimVrefbPwm.Init.Period is the same as hTimVrefaPwm.Init.Period
  419. **********************************************************/
  420. uint32_t L6208_Board_VrefPwmGetPeriod(void)
  421. {
  422. return ((uint32_t)hTimVrefaPwm.Init.Period);
  423. }
  424. /******************************************************//**
  425. * @brief Releases the L6208 reset (pin set to High) of all devices
  426. * @retval None
  427. **********************************************************/
  428. void L6208_Board_ReleaseReset(void)
  429. {
  430. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, BSP_MOTOR_CONTROL_BOARD_RESET_PIN, GPIO_PIN_SET);
  431. }
  432. /******************************************************//**
  433. * @brief Resets the L6208 (reset pin set to low) of all devices
  434. * @retval None
  435. **********************************************************/
  436. void L6208_Board_Reset(void)
  437. {
  438. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, BSP_MOTOR_CONTROL_BOARD_RESET_PIN, GPIO_PIN_RESET);
  439. }
  440. /******************************************************//**
  441. * @brief Set the L6208 CONTROL pin
  442. * @retval None
  443. **********************************************************/
  444. void L6208_Board_CONTROL_PIN_Set(void)
  445. {
  446. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CONTROL_PORT, BSP_MOTOR_CONTROL_BOARD_CONTROL_PIN, GPIO_PIN_SET);
  447. }
  448. /******************************************************//**
  449. * @brief Reset the L6208 CONTROL pin
  450. * @retval None
  451. **********************************************************/
  452. void L6208_Board_CONTROL_PIN_Reset(void)
  453. {
  454. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CONTROL_PORT, BSP_MOTOR_CONTROL_BOARD_CONTROL_PIN, GPIO_PIN_RESET);
  455. }
  456. /******************************************************//**
  457. * @brief Set the L6208 CLOCK pin
  458. * @retval None
  459. **********************************************************/
  460. void L6208_Board_CLOCK_PIN_Set(void)
  461. {
  462. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CLOCK_PORT, BSP_MOTOR_CONTROL_BOARD_CLOCK_PIN, GPIO_PIN_SET);
  463. }
  464. /******************************************************//**
  465. * @brief Reset the L6208 CLOCK pin
  466. * @retval None
  467. **********************************************************/
  468. void L6208_Board_CLOCK_PIN_Reset(void)
  469. {
  470. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CLOCK_PORT, BSP_MOTOR_CONTROL_BOARD_CLOCK_PIN, GPIO_PIN_RESET);
  471. }
  472. /******************************************************//**
  473. * @brief Set the L6208 HALF_FULL pin
  474. * @retval None
  475. **********************************************************/
  476. void L6208_Board_HALF_FULL_PIN_Set(void)
  477. {
  478. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PORT, BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PIN, GPIO_PIN_SET);
  479. }
  480. /******************************************************//**
  481. * @brief Reset the L6208 HALF_FULL pin
  482. * @retval None
  483. **********************************************************/
  484. void L6208_Board_HALF_FULL_PIN_Reset(void)
  485. {
  486. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PORT, BSP_MOTOR_CONTROL_BOARD_HALF_FULL_PIN, GPIO_PIN_RESET);
  487. }
  488. /******************************************************//**
  489. * @brief Set the L6208 DIR pin
  490. * @retval None
  491. **********************************************************/
  492. void L6208_Board_DIR_PIN_Set(void)
  493. {
  494. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_PIN, GPIO_PIN_SET);
  495. }
  496. /******************************************************//**
  497. * @brief Reset the L6208 DIR pin
  498. * @retval None
  499. **********************************************************/
  500. void L6208_Board_DIR_PIN_Reset(void)
  501. {
  502. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_PIN, GPIO_PIN_RESET);
  503. }
  504. /******************************************************//**
  505. * @brief Returns the FLAG pin state.
  506. * @retval The FLAG pin value.
  507. **********************************************************/
  508. uint32_t L6208_Board_FLAG_PIN_GetState(void)
  509. {
  510. return HAL_GPIO_ReadPin(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PORT, BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN);
  511. }
  512. /******************************************************//**
  513. * @brief Disable the power bridges (leave the output bridges HiZ)
  514. * @retval None
  515. **********************************************************/
  516. void L6208_Board_Disable(void)
  517. {
  518. GPIO_InitTypeDef GPIO_InitStruct;
  519. /* Configure the GPIO connected to EN pin as an output */
  520. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN;
  521. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  522. GPIO_InitStruct.Pull = GPIO_NOPULL;
  523. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  524. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PORT, &GPIO_InitStruct);
  525. __disable_irq();
  526. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PORT, BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN, GPIO_PIN_RESET);
  527. __HAL_GPIO_EXTI_CLEAR_IT(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN);
  528. __enable_irq();
  529. }
  530. /******************************************************//**
  531. * @brief Enable the power bridges (leave the output bridges HiZ)
  532. * @retval None
  533. **********************************************************/
  534. void L6208_Board_Enable(void)
  535. {
  536. GPIO_InitTypeDef GPIO_InitStruct;
  537. HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PORT, BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN, GPIO_PIN_SET);
  538. HAL_Delay(BRIDGE_TURN_ON_DELAY);
  539. /* Configure the GPIO connected to EN pin to take interrupt */
  540. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN;
  541. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  542. GPIO_InitStruct.Pull = GPIO_PULLUP;
  543. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  544. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PORT, &GPIO_InitStruct);
  545. __HAL_GPIO_EXTI_CLEAR_IT(BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN);
  546. HAL_NVIC_ClearPendingIRQ(FLAG_EXTI_LINE_IRQn);
  547. HAL_NVIC_EnableIRQ(FLAG_EXTI_LINE_IRQn);
  548. }
  549. /**
  550. * @}
  551. */
  552. /**
  553. * @}
  554. */
  555. /**
  556. * @}
  557. */
  558. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/