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.
Diff: IHM01A1/stm32l0xx_hal_msp.cpp
- Revision:
- 0:b9444a40a999
- Child:
- 1:75a41f0c0586
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IHM01A1/stm32l0xx_hal_msp.cpp Sat Sep 05 20:18:14 2015 +0000 @@ -0,0 +1,287 @@ +/** + ****************************************************************************** + * @file Multi/Examples/MotionControl/IHM01A1_ExampleFor1Motor/Src/stm32l0xx_hal_msp.c + * @author IPC Rennes + * @version V1.5.0 + * @date November 12, 2014 + * @brief HAL MSP module. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#ifdef TARGET_STM32L0 +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "mbed.h" + +/** @defgroup HAL_MSP + * @brief HAL MSP module. + * @{ + */ + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ + +/* Private function prototypes -----------------------------------------------*/ +extern void BSP_MotorControl_StepClockHandler(uint8_t deviceId); +extern void BSP_MotorControl_FlagInterruptHandler(void); +/* Private functions ---------------------------------------------------------*/ + +/** @defgroup HAL_MSP_Private_Functions + * @{ + */ + +/** + * @brief SPI MSP Initialization + * This function configures the hardware resources used in this example: + * - Peripheral's clock enable + * - Peripheral's GPIO Configuration + * @param[in] hspi SPI handle pointer + * @retval None + */ +void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + if(hspi->Instance == SPIx) + { + /*##-1- Enable peripherals and GPIO Clocks #################################*/ + /* Enable GPIO TX/RX clock */ + SPIx_SCK_GPIO_CLK_ENABLE(); + SPIx_MISO_GPIO_CLK_ENABLE(); + SPIx_MOSI_GPIO_CLK_ENABLE(); + /* Enable SPI clock */ + SPIx_CLK_ENABLE(); + + /*##-2- Configure peripheral GPIO ##########################################*/ + /* SPI SCK GPIO pin configuration */ + GPIO_InitStruct.Pin = SPIx_SCK_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; + GPIO_InitStruct.Alternate = SPIx_SCK_AF; + + HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); + + /* SPI MISO GPIO pin configuration */ + GPIO_InitStruct.Pin = SPIx_MISO_PIN; + GPIO_InitStruct.Alternate = SPIx_MISO_AF; + + HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct); + + /* SPI MOSI GPIO pin configuration */ + GPIO_InitStruct.Pin = SPIx_MOSI_PIN; + GPIO_InitStruct.Alternate = SPIx_MOSI_AF; + + HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct); + } +} + +/** + * @brief SPI MSP De-Initialization + * This function frees the hardware resources used in this example: + * - Disable the Peripheral's clock + * - Revert GPIO configuration to its default state + * @param[in] hspi SPI handle pointer + * @retval None + */ +void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) +{ + if(hspi->Instance == SPIx) + { + /*##-1- Reset peripherals ##################################################*/ + SPIx_FORCE_RESET(); + SPIx_RELEASE_RESET(); + + /*##-2- Disable peripherals and GPIO Clocks ################################*/ + /* Configure SPI SCK as alternate function */ + HAL_GPIO_DeInit(SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN); + /* Configure SPI MISO as alternate function */ + HAL_GPIO_DeInit(SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN); + /* Configure SPI MOSI as alternate function */ + HAL_GPIO_DeInit(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN); + } +} + +/** + * @brief PWM MSP Initialization + * @param[in] htim_pwm PWM handle pointer + * @retval None + */ +void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) +{ + GPIO_InitTypeDef GPIO_InitStruct; + if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1) + { + /* Peripheral clock enable */ + __BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1_CLCK_ENABLE(); + + /* GPIO configuration */ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PWM_1_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + GPIO_InitStruct.Alternate = BSP_MOTOR_CONTROL_BOARD_AFx_TIMx_PWM1; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PWM_1_PORT, &GPIO_InitStruct); + + /* Set Interrupt Group Priority of Timer Interrupt*/ + HAL_NVIC_SetPriority(BSP_MOTOR_CONTROL_BOARD_PWM1_IRQn, 4, 0); + + /* Enable the timer global Interrupt */ + HAL_NVIC_EnableIRQ(BSP_MOTOR_CONTROL_BOARD_PWM1_IRQn); + } + else if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2) + { + /* Peripheral clock enable */ + __BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2_CLCK_ENABLE(); + + /* GPIO configuration */ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PWM_2_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + GPIO_InitStruct.Alternate = BSP_MOTOR_CONTROL_BOARD_AFx_TIMx_PWM2; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PWM_2_PORT, &GPIO_InitStruct); + + /* Set Interrupt Group Priority of Timer Interrupt*/ + HAL_NVIC_SetPriority(BSP_MOTOR_CONTROL_BOARD_PWM2_IRQn, 4, 0); + + /* Enable the timer2 global Interrupt */ + HAL_NVIC_EnableIRQ(BSP_MOTOR_CONTROL_BOARD_PWM2_IRQn); + + } + else if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3) + { + /* Peripheral clock enable */ + __BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3_CLCK_ENABLE(); + + /* GPIO configuration */ + GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_PWM_3_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_PWM_3_PORT, &GPIO_InitStruct); + + /* Set Interrupt Group Priority of Timer Interrupt*/ + HAL_NVIC_SetPriority(BSP_MOTOR_CONTROL_BOARD_PWM3_IRQn, 3, 0); + + /* Enable the timer global Interrupt */ + HAL_NVIC_EnableIRQ(BSP_MOTOR_CONTROL_BOARD_PWM3_IRQn); + } +} + +/** + * @brief PWM MSP De-Initialization + * @param[in] htim_pwm PWM handle pointer + * @retval None + */ +void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm) +{ + if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1) + { + /* Peripheral clock disable */ + __BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1_CLCK_DISABLE(); + + /* GPIO Deconfiguration */ + HAL_GPIO_DeInit(BSP_MOTOR_CONTROL_BOARD_PWM_1_PORT, BSP_MOTOR_CONTROL_BOARD_PWM_1_PIN); + + } + else if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2) + { + /* Peripheral clock disable */ + __BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2_CLCK_DISABLE(); + + /* GPIO Deconfiguration */ + HAL_GPIO_DeInit(BSP_MOTOR_CONTROL_BOARD_PWM_2_PORT, BSP_MOTOR_CONTROL_BOARD_PWM_2_PIN); + + } + else if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3) + { + /* Peripheral clock disable */ + __BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3_CLCK_DISABLE(); + + /* GPIO Deconfiguration */ + HAL_GPIO_DeInit(BSP_MOTOR_CONTROL_BOARD_PWM_3_PORT, BSP_MOTOR_CONTROL_BOARD_PWM_3_PIN); + } +} + +/** + * @brief PWM Callback + * @param[in] htim PWM handle pointer + * @retval None + */ +void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) +{ + if ((htim->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1)&& (htim->Channel == BSP_MOTOR_CONTROL_BOARD_HAL_ACT_CHAN_TIMER_PWM1)) + { + if (BSP_MotorControl_GetDeviceState(0) != INACTIVE) + { + BSP_MotorControl_StepClockHandler(0); + } + } + if ((htim->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2)&& (htim->Channel == BSP_MOTOR_CONTROL_BOARD_HAL_ACT_CHAN_TIMER_PWM2)) + { + if (BSP_MotorControl_GetDeviceState(1) != INACTIVE) + { + BSP_MotorControl_StepClockHandler(1); + } + } + if ((htim->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3)&& (htim->Channel == BSP_MOTOR_CONTROL_BOARD_HAL_ACT_CHAN_TIMER_PWM3)) + { + HAL_GPIO_TogglePin(BSP_MOTOR_CONTROL_BOARD_PWM_3_PORT, BSP_MOTOR_CONTROL_BOARD_PWM_3_PIN); + if ((BSP_MotorControl_GetDeviceState(2) != INACTIVE)&& + (HAL_GPIO_ReadPin(BSP_MOTOR_CONTROL_BOARD_PWM_3_PORT, BSP_MOTOR_CONTROL_BOARD_PWM_3_PIN) == GPIO_PIN_SET)) + { + BSP_MotorControl_StepClockHandler(2); + } + } +} + +/** + * @brief External Line Callback + * @param[in] GPIO_Pin pin number + * @retval None + */ +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) +{ + if (GPIO_Pin == BSP_MOTOR_CONTROL_BOARD_FLAG_PIN) + { + BSP_MotorControl_FlagInterruptHandler(); + } + } +/** + * @} + */ + +/** + * @} + */ +#endif +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/