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.
IHM01A1/stm32f0xx_nucleo_ihm01a1.cpp@6:19c1b4a04c24, 2015-10-13 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
gregeric | 0:b9444a40a999 | 1 | /** |
gregeric | 0:b9444a40a999 | 2 | ****************************************************************************** |
gregeric | 0:b9444a40a999 | 3 | * @file stm32f0xx_nucleo_ihm01a1.c |
gregeric | 0:b9444a40a999 | 4 | * @author IPC Rennes |
gregeric | 0:b9444a40a999 | 5 | * @version V1.5.0 |
gregeric | 0:b9444a40a999 | 6 | * @date November 12, 2014 |
gregeric | 0:b9444a40a999 | 7 | * @brief BSP driver for x-nucleo-ihm01a1 Nucleo extension board |
gregeric | 0:b9444a40a999 | 8 | * (based on L6474) |
gregeric | 0:b9444a40a999 | 9 | ****************************************************************************** |
gregeric | 0:b9444a40a999 | 10 | * @attention |
gregeric | 0:b9444a40a999 | 11 | * |
gregeric | 0:b9444a40a999 | 12 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
gregeric | 0:b9444a40a999 | 13 | * |
gregeric | 0:b9444a40a999 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
gregeric | 0:b9444a40a999 | 15 | * are permitted provided that the following conditions are met: |
gregeric | 0:b9444a40a999 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
gregeric | 0:b9444a40a999 | 17 | * this list of conditions and the following disclaimer. |
gregeric | 0:b9444a40a999 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
gregeric | 0:b9444a40a999 | 19 | * this list of conditions and the following disclaimer in the documentation |
gregeric | 0:b9444a40a999 | 20 | * and/or other materials provided with the distribution. |
gregeric | 0:b9444a40a999 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
gregeric | 0:b9444a40a999 | 22 | * may be used to endorse or promote products derived from this software |
gregeric | 0:b9444a40a999 | 23 | * without specific prior written permission. |
gregeric | 0:b9444a40a999 | 24 | * |
gregeric | 0:b9444a40a999 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
gregeric | 0:b9444a40a999 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
gregeric | 0:b9444a40a999 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
gregeric | 0:b9444a40a999 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
gregeric | 0:b9444a40a999 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
gregeric | 0:b9444a40a999 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
gregeric | 0:b9444a40a999 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
gregeric | 0:b9444a40a999 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
gregeric | 0:b9444a40a999 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
gregeric | 0:b9444a40a999 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
gregeric | 0:b9444a40a999 | 35 | * |
gregeric | 0:b9444a40a999 | 36 | ****************************************************************************** |
gregeric | 0:b9444a40a999 | 37 | */ |
gregeric | 0:b9444a40a999 | 38 | #ifdef TARGET_STM32F0 |
gregeric | 0:b9444a40a999 | 39 | /* Includes ------------------------------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 40 | #include "mbed.h" |
gregeric | 0:b9444a40a999 | 41 | #include "stm32f0xx_nucleo_ihm01a1.h" |
gregeric | 0:b9444a40a999 | 42 | |
gregeric | 0:b9444a40a999 | 43 | /** @addtogroup BSP |
gregeric | 0:b9444a40a999 | 44 | * @{ |
gregeric | 0:b9444a40a999 | 45 | */ |
gregeric | 0:b9444a40a999 | 46 | |
gregeric | 0:b9444a40a999 | 47 | /** @defgroup STM32F0XX_NUCLEO_IHM01A1 |
gregeric | 0:b9444a40a999 | 48 | * @{ |
gregeric | 0:b9444a40a999 | 49 | */ |
gregeric | 0:b9444a40a999 | 50 | |
gregeric | 0:b9444a40a999 | 51 | /* Private constants ---------------------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 52 | |
gregeric | 0:b9444a40a999 | 53 | /** @defgroup IHM01A1_Private_Constants |
gregeric | 0:b9444a40a999 | 54 | * @{ |
gregeric | 0:b9444a40a999 | 55 | */ |
gregeric | 0:b9444a40a999 | 56 | |
gregeric | 0:b9444a40a999 | 57 | /// Timer Prescaler |
gregeric | 0:b9444a40a999 | 58 | #define TIMER_PRESCALER (1024) |
gregeric | 0:b9444a40a999 | 59 | |
gregeric | 0:b9444a40a999 | 60 | /// SPI Maximum Timeout values for flags waiting loops |
gregeric | 0:b9444a40a999 | 61 | #define SPIx_TIMEOUT_MAX ((uint32_t)0x1000) |
gregeric | 0:b9444a40a999 | 62 | |
gregeric | 0:b9444a40a999 | 63 | /** |
gregeric | 0:b9444a40a999 | 64 | * @} |
gregeric | 0:b9444a40a999 | 65 | */ |
gregeric | 0:b9444a40a999 | 66 | |
gregeric | 0:b9444a40a999 | 67 | /* Private variables ---------------------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 68 | |
gregeric | 0:b9444a40a999 | 69 | /** @defgroup IHM01A1_Board_Private_Variables |
gregeric | 0:b9444a40a999 | 70 | * @{ |
gregeric | 0:b9444a40a999 | 71 | */ |
gregeric | 0:b9444a40a999 | 72 | /// SPI handler declaration |
gregeric | 0:b9444a40a999 | 73 | static SPI_HandleTypeDef SpiHandle; |
gregeric | 0:b9444a40a999 | 74 | /// Timer handler for PWM1 |
gregeric | 0:b9444a40a999 | 75 | TIM_HandleTypeDef hTimPwm1; |
gregeric | 0:b9444a40a999 | 76 | /// imer handler for PWM2 |
gregeric | 0:b9444a40a999 | 77 | TIM_HandleTypeDef hTimPwm2; |
gregeric | 0:b9444a40a999 | 78 | /// Timer handler for PWM3 |
gregeric | 0:b9444a40a999 | 79 | TIM_HandleTypeDef hTimPwm3; |
gregeric | 0:b9444a40a999 | 80 | /** |
gregeric | 0:b9444a40a999 | 81 | * @} |
gregeric | 0:b9444a40a999 | 82 | */ |
gregeric | 0:b9444a40a999 | 83 | |
gregeric | 0:b9444a40a999 | 84 | |
gregeric | 0:b9444a40a999 | 85 | /** |
gregeric | 0:b9444a40a999 | 86 | * @} |
gregeric | 0:b9444a40a999 | 87 | */ |
gregeric | 0:b9444a40a999 | 88 | |
gregeric | 0:b9444a40a999 | 89 | |
gregeric | 0:b9444a40a999 | 90 | /** @defgroup IHM01A1_Board_Private_Functions |
gregeric | 0:b9444a40a999 | 91 | * @{ |
gregeric | 0:b9444a40a999 | 92 | */ |
gregeric | 0:b9444a40a999 | 93 | |
gregeric | 0:b9444a40a999 | 94 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 95 | * @brief This function provides an accurate delay in milliseconds |
gregeric | 0:b9444a40a999 | 96 | * @param[in] delay time length in milliseconds |
gregeric | 0:b9444a40a999 | 97 | * @retval None |
gregeric | 0:b9444a40a999 | 98 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 99 | void BSP_MotorControlBoard_Delay(uint32_t delay) |
gregeric | 0:b9444a40a999 | 100 | { |
gregeric | 0:b9444a40a999 | 101 | HAL_Delay(delay); |
gregeric | 0:b9444a40a999 | 102 | } |
gregeric | 0:b9444a40a999 | 103 | |
gregeric | 0:b9444a40a999 | 104 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 105 | * @brief This function disable the interruptions |
gregeric | 0:b9444a40a999 | 106 | * @param None |
gregeric | 0:b9444a40a999 | 107 | * @retval None |
gregeric | 0:b9444a40a999 | 108 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 109 | void BSP_MotorControlBoard_DisableIrq(void) |
gregeric | 0:b9444a40a999 | 110 | { |
gregeric | 0:b9444a40a999 | 111 | __disable_irq(); |
gregeric | 0:b9444a40a999 | 112 | } |
gregeric | 0:b9444a40a999 | 113 | |
gregeric | 0:b9444a40a999 | 114 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 115 | * @brief This function enable the interruptions |
gregeric | 0:b9444a40a999 | 116 | * @param None |
gregeric | 0:b9444a40a999 | 117 | * @retval None |
gregeric | 0:b9444a40a999 | 118 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 119 | void BSP_MotorControlBoard_EnableIrq(void) |
gregeric | 0:b9444a40a999 | 120 | { |
gregeric | 0:b9444a40a999 | 121 | __enable_irq(); |
gregeric | 0:b9444a40a999 | 122 | } |
gregeric | 0:b9444a40a999 | 123 | |
gregeric | 0:b9444a40a999 | 124 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 125 | * @brief Initiliases the GPIOs used by the L6474s |
gregeric | 0:b9444a40a999 | 126 | * @param[in] nbDevices number of L6474 devices |
gregeric | 0:b9444a40a999 | 127 | * @retval None |
gregeric | 0:b9444a40a999 | 128 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 129 | void BSP_MotorControlBoard_GpioInit(uint8_t nbDevices) |
gregeric | 0:b9444a40a999 | 130 | { |
gregeric | 0:b9444a40a999 | 131 | GPIO_InitTypeDef GPIO_InitStruct; |
gregeric | 0:b9444a40a999 | 132 | |
gregeric | 0:b9444a40a999 | 133 | /* GPIO Ports Clock Enable */ |
gregeric | 0:b9444a40a999 | 134 | __GPIOC_CLK_ENABLE(); |
gregeric | 0:b9444a40a999 | 135 | __GPIOA_CLK_ENABLE(); |
gregeric | 0:b9444a40a999 | 136 | __GPIOB_CLK_ENABLE(); |
gregeric | 0:b9444a40a999 | 137 | |
gregeric | 0:b9444a40a999 | 138 | /* Configure L6474 - DIR pin for device 1 -------------------------------*/ |
gregeric | 0:b9444a40a999 | 139 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_1_PIN; |
gregeric | 0:b9444a40a999 | 140 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 141 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 142 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 143 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_1_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 144 | |
gregeric | 0:b9444a40a999 | 145 | /* Configure L6474 - Flag pin -------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 146 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_FLAG_PIN; |
gregeric | 0:b9444a40a999 | 147 | GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; |
gregeric | 0:b9444a40a999 | 148 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
gregeric | 0:b9444a40a999 | 149 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 150 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_FLAG_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 151 | |
gregeric | 0:b9444a40a999 | 152 | /* Set Priority of External Line Interrupt used for the Flag interrupt*/ |
gregeric | 0:b9444a40a999 | 153 | HAL_NVIC_SetPriority(EXTI_MCU_LINE_IRQn, 5, 0); |
gregeric | 0:b9444a40a999 | 154 | |
gregeric | 0:b9444a40a999 | 155 | /* Enable the External Line Interrupt used for the Flag interrupt*/ |
gregeric | 0:b9444a40a999 | 156 | HAL_NVIC_EnableIRQ(EXTI_MCU_LINE_IRQn); |
gregeric | 0:b9444a40a999 | 157 | |
gregeric | 0:b9444a40a999 | 158 | /* Configure L6474 - CS pin ---------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 159 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_CS_PIN; |
gregeric | 0:b9444a40a999 | 160 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 161 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 162 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 163 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_CS_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 164 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CS_PORT, BSP_MOTOR_CONTROL_BOARD_CS_PIN, GPIO_PIN_SET); |
gregeric | 0:b9444a40a999 | 165 | |
gregeric | 0:b9444a40a999 | 166 | /* Configure L6474 - STBY/RESET pin -------------------------------------*/ |
gregeric | 0:b9444a40a999 | 167 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_RESET_PIN; |
gregeric | 0:b9444a40a999 | 168 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 169 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 170 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 171 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 172 | BSP_MotorControlBoard_Reset(); |
gregeric | 0:b9444a40a999 | 173 | |
gregeric | 0:b9444a40a999 | 174 | if (nbDevices > 1) |
gregeric | 0:b9444a40a999 | 175 | { |
gregeric | 0:b9444a40a999 | 176 | /* Configure L6474 - DIR pin for device 2 ----------------------------*/ |
gregeric | 0:b9444a40a999 | 177 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_2_PIN; |
gregeric | 0:b9444a40a999 | 178 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 179 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 180 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 181 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_2_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 182 | } |
gregeric | 0:b9444a40a999 | 183 | if (nbDevices > 2) |
gregeric | 0:b9444a40a999 | 184 | { |
gregeric | 0:b9444a40a999 | 185 | /* Configure L6474 - DIR pin for device 3 ----------------------------*/ |
gregeric | 0:b9444a40a999 | 186 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_3_PIN; |
gregeric | 0:b9444a40a999 | 187 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 188 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 189 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 190 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_3_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 191 | } |
gregeric | 0:b9444a40a999 | 192 | } |
gregeric | 0:b9444a40a999 | 193 | |
gregeric | 0:b9444a40a999 | 194 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 195 | * @brief Sets the frequency of PWM1 used by device 0 |
gregeric | 0:b9444a40a999 | 196 | * @param[in] newFreq in Hz |
gregeric | 0:b9444a40a999 | 197 | * @retval None |
gregeric | 0:b9444a40a999 | 198 | * @note The frequency is directly the current speed of the device |
gregeric | 0:b9444a40a999 | 199 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 200 | void BSP_MotorControlBoard_Pwm1SetFreq(uint16_t newFreq) |
gregeric | 0:b9444a40a999 | 201 | { |
gregeric | 0:b9444a40a999 | 202 | uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); |
gregeric | 0:b9444a40a999 | 203 | uint32_t period = (sysFreq/ (TIMER_PRESCALER * BSP_MOTOR_CONTROL_BOARD_PWM1_FREQ_RESCALER * (uint32_t)newFreq)) - 1; |
gregeric | 0:b9444a40a999 | 204 | |
gregeric | 0:b9444a40a999 | 205 | __HAL_TIM_SetAutoreload(&hTimPwm1, period); |
gregeric | 0:b9444a40a999 | 206 | __HAL_TIM_SetCompare(&hTimPwm1, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1, period >> 1); |
gregeric | 0:b9444a40a999 | 207 | HAL_TIM_PWM_Start_IT(&hTimPwm1, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1); |
gregeric | 0:b9444a40a999 | 208 | } |
gregeric | 0:b9444a40a999 | 209 | |
gregeric | 0:b9444a40a999 | 210 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 211 | * @brief Sets the frequency of PWM2 used by device 1 |
gregeric | 0:b9444a40a999 | 212 | * @param[in] newFreq in Hz |
gregeric | 0:b9444a40a999 | 213 | * @retval None |
gregeric | 0:b9444a40a999 | 214 | * @note The frequency is directly the current speed of the device |
gregeric | 0:b9444a40a999 | 215 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 216 | void BSP_MotorControlBoard_Pwm2SetFreq(uint16_t newFreq) |
gregeric | 0:b9444a40a999 | 217 | { |
gregeric | 0:b9444a40a999 | 218 | uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); |
gregeric | 0:b9444a40a999 | 219 | uint32_t period = (sysFreq/ (TIMER_PRESCALER * BSP_MOTOR_CONTROL_BOARD_PWM2_FREQ_RESCALER * (uint32_t)newFreq)) - 1; |
gregeric | 0:b9444a40a999 | 220 | |
gregeric | 0:b9444a40a999 | 221 | __HAL_TIM_SetAutoreload(&hTimPwm2, period); |
gregeric | 0:b9444a40a999 | 222 | __HAL_TIM_SetCompare(&hTimPwm2, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2, period >> 1); |
gregeric | 0:b9444a40a999 | 223 | HAL_TIM_PWM_Start_IT(&hTimPwm2, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2); |
gregeric | 0:b9444a40a999 | 224 | } |
gregeric | 0:b9444a40a999 | 225 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 226 | * @brief Sets the frequency of PWM3 used by device 2 |
gregeric | 0:b9444a40a999 | 227 | * @param[in] newFreq in Hz |
gregeric | 0:b9444a40a999 | 228 | * @retval None |
gregeric | 0:b9444a40a999 | 229 | * @note The frequency is directly the current speed of the device |
gregeric | 0:b9444a40a999 | 230 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 231 | void BSP_MotorControlBoard_Pwm3SetFreq(uint16_t newFreq) |
gregeric | 0:b9444a40a999 | 232 | { |
gregeric | 0:b9444a40a999 | 233 | uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); |
gregeric | 0:b9444a40a999 | 234 | /* Double the frequency as the SW is generated by SW */ |
gregeric | 0:b9444a40a999 | 235 | uint32_t period = (sysFreq/ (TIMER_PRESCALER * BSP_MOTOR_CONTROL_BOARD_PWM3_FREQ_RESCALER * (uint32_t)newFreq)) - 1; |
gregeric | 0:b9444a40a999 | 236 | |
gregeric | 0:b9444a40a999 | 237 | __HAL_TIM_SetAutoreload(&hTimPwm3, period); |
gregeric | 0:b9444a40a999 | 238 | __HAL_TIM_SetCompare(&hTimPwm3, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3, period >> 1); |
gregeric | 0:b9444a40a999 | 239 | HAL_TIM_PWM_Start_IT(&hTimPwm3, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3); |
gregeric | 0:b9444a40a999 | 240 | } |
gregeric | 0:b9444a40a999 | 241 | |
gregeric | 0:b9444a40a999 | 242 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 243 | * @brief Initialises the PWM uses by the specified device |
gregeric | 0:b9444a40a999 | 244 | * @param[in] deviceId (from 0 to 2) |
gregeric | 0:b9444a40a999 | 245 | * @retval None |
gregeric | 0:b9444a40a999 | 246 | * @note Device 0 uses PWM1 based on timer 1 |
gregeric | 0:b9444a40a999 | 247 | * Device 1 uses PWM 2 based on timer 2 |
gregeric | 0:b9444a40a999 | 248 | * Device 2 uses PWM3 based timer 0 |
gregeric | 0:b9444a40a999 | 249 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 250 | void BSP_MotorControlBoard_PwmInit(uint8_t deviceId) |
gregeric | 0:b9444a40a999 | 251 | { |
gregeric | 0:b9444a40a999 | 252 | TIM_OC_InitTypeDef sConfigOC; |
gregeric | 0:b9444a40a999 | 253 | TIM_MasterConfigTypeDef sMasterConfig; |
gregeric | 0:b9444a40a999 | 254 | TIM_HandleTypeDef *pHTim; |
gregeric | 0:b9444a40a999 | 255 | uint32_t channel; |
gregeric | 0:b9444a40a999 | 256 | |
gregeric | 0:b9444a40a999 | 257 | switch (deviceId) |
gregeric | 0:b9444a40a999 | 258 | { |
gregeric | 0:b9444a40a999 | 259 | |
gregeric | 0:b9444a40a999 | 260 | case 0: |
gregeric | 0:b9444a40a999 | 261 | default: |
gregeric | 0:b9444a40a999 | 262 | pHTim = &hTimPwm1; |
gregeric | 0:b9444a40a999 | 263 | pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1; |
gregeric | 0:b9444a40a999 | 264 | channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1; |
gregeric | 0:b9444a40a999 | 265 | |
gregeric | 0:b9444a40a999 | 266 | break; |
gregeric | 0:b9444a40a999 | 267 | case 1: |
gregeric | 0:b9444a40a999 | 268 | pHTim = &hTimPwm2; |
gregeric | 0:b9444a40a999 | 269 | pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2; |
gregeric | 0:b9444a40a999 | 270 | channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2; |
gregeric | 0:b9444a40a999 | 271 | break; |
gregeric | 0:b9444a40a999 | 272 | |
gregeric | 0:b9444a40a999 | 273 | |
gregeric | 0:b9444a40a999 | 274 | case 2: |
gregeric | 0:b9444a40a999 | 275 | pHTim = &hTimPwm3; |
gregeric | 0:b9444a40a999 | 276 | pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3; |
gregeric | 0:b9444a40a999 | 277 | channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3; |
gregeric | 0:b9444a40a999 | 278 | break; |
gregeric | 0:b9444a40a999 | 279 | } |
gregeric | 0:b9444a40a999 | 280 | pHTim->Init.Prescaler = TIMER_PRESCALER -1; |
gregeric | 0:b9444a40a999 | 281 | pHTim->Init.CounterMode = TIM_COUNTERMODE_UP; |
gregeric | 0:b9444a40a999 | 282 | pHTim->Init.Period = 0; |
gregeric | 0:b9444a40a999 | 283 | pHTim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
gregeric | 0:b9444a40a999 | 284 | HAL_TIM_PWM_Init(pHTim); |
gregeric | 0:b9444a40a999 | 285 | |
gregeric | 0:b9444a40a999 | 286 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
gregeric | 0:b9444a40a999 | 287 | sConfigOC.Pulse = 0; |
gregeric | 0:b9444a40a999 | 288 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
gregeric | 0:b9444a40a999 | 289 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
gregeric | 0:b9444a40a999 | 290 | HAL_TIM_PWM_ConfigChannel(pHTim, &sConfigOC, channel); |
gregeric | 0:b9444a40a999 | 291 | |
gregeric | 0:b9444a40a999 | 292 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
gregeric | 0:b9444a40a999 | 293 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
gregeric | 0:b9444a40a999 | 294 | HAL_TIMEx_MasterConfigSynchronization(pHTim, &sMasterConfig); |
gregeric | 0:b9444a40a999 | 295 | } |
gregeric | 0:b9444a40a999 | 296 | |
gregeric | 0:b9444a40a999 | 297 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 298 | * @brief Stops the PWM uses by the specified device |
gregeric | 0:b9444a40a999 | 299 | * @param[in] deviceId (from 0 to 2) |
gregeric | 0:b9444a40a999 | 300 | * @retval None |
gregeric | 0:b9444a40a999 | 301 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 302 | void BSP_MotorControlBoard_PwmStop(uint8_t deviceId) |
gregeric | 0:b9444a40a999 | 303 | { |
gregeric | 0:b9444a40a999 | 304 | switch (deviceId) |
gregeric | 0:b9444a40a999 | 305 | { |
gregeric | 0:b9444a40a999 | 306 | case 0: |
gregeric | 0:b9444a40a999 | 307 | HAL_TIM_PWM_Stop(&hTimPwm1,BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1); |
gregeric | 0:b9444a40a999 | 308 | |
gregeric | 0:b9444a40a999 | 309 | break; |
gregeric | 0:b9444a40a999 | 310 | case 1: |
gregeric | 0:b9444a40a999 | 311 | HAL_TIM_PWM_Stop(&hTimPwm2,BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2); |
gregeric | 0:b9444a40a999 | 312 | |
gregeric | 0:b9444a40a999 | 313 | break; |
gregeric | 0:b9444a40a999 | 314 | case 2: |
gregeric | 0:b9444a40a999 | 315 | HAL_TIM_PWM_Stop(&hTimPwm3,BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3); |
gregeric | 0:b9444a40a999 | 316 | |
gregeric | 0:b9444a40a999 | 317 | break; |
gregeric | 0:b9444a40a999 | 318 | default: |
gregeric | 0:b9444a40a999 | 319 | break;//ignore error |
gregeric | 0:b9444a40a999 | 320 | } |
gregeric | 0:b9444a40a999 | 321 | } |
gregeric | 0:b9444a40a999 | 322 | |
gregeric | 0:b9444a40a999 | 323 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 324 | * @brief Releases the L6474 reset (pin set to High) of all devices |
gregeric | 0:b9444a40a999 | 325 | * @param None |
gregeric | 0:b9444a40a999 | 326 | * @retval None |
gregeric | 0:b9444a40a999 | 327 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 328 | void BSP_MotorControlBoard_ReleaseReset(void) |
gregeric | 0:b9444a40a999 | 329 | { |
gregeric | 0:b9444a40a999 | 330 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, BSP_MOTOR_CONTROL_BOARD_RESET_PIN, GPIO_PIN_SET); |
gregeric | 0:b9444a40a999 | 331 | } |
gregeric | 0:b9444a40a999 | 332 | |
gregeric | 0:b9444a40a999 | 333 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 334 | * @brief Resets the L6474 (reset pin set to low) of all devices |
gregeric | 0:b9444a40a999 | 335 | * @param None |
gregeric | 0:b9444a40a999 | 336 | * @retval None |
gregeric | 0:b9444a40a999 | 337 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 338 | void BSP_MotorControlBoard_Reset(void) |
gregeric | 0:b9444a40a999 | 339 | { |
gregeric | 0:b9444a40a999 | 340 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, BSP_MOTOR_CONTROL_BOARD_RESET_PIN, GPIO_PIN_RESET); |
gregeric | 0:b9444a40a999 | 341 | } |
gregeric | 0:b9444a40a999 | 342 | |
gregeric | 0:b9444a40a999 | 343 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 344 | * @brief Set the GPIO used for the direction |
gregeric | 0:b9444a40a999 | 345 | * @param[in] deviceId (from 0 to 2) |
gregeric | 0:b9444a40a999 | 346 | * @param[in] gpioState state of the direction gpio (0 to reset, 1 to set) |
gregeric | 0:b9444a40a999 | 347 | * @retval None |
gregeric | 0:b9444a40a999 | 348 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 349 | void BSP_MotorControlBoard_SetDirectionGpio(uint8_t deviceId, uint8_t gpioState) |
gregeric | 0:b9444a40a999 | 350 | { |
gregeric | 0:b9444a40a999 | 351 | switch (deviceId) |
gregeric | 0:b9444a40a999 | 352 | { |
gregeric | 0:b9444a40a999 | 353 | case 2: |
gregeric | 0:b9444a40a999 | 354 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_3_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_3_PIN, (GPIO_PinState)gpioState); |
gregeric | 0:b9444a40a999 | 355 | break; |
gregeric | 0:b9444a40a999 | 356 | case 1: |
gregeric | 0:b9444a40a999 | 357 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_2_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_2_PIN, (GPIO_PinState)gpioState); |
gregeric | 0:b9444a40a999 | 358 | break; |
gregeric | 0:b9444a40a999 | 359 | case 0: |
gregeric | 0:b9444a40a999 | 360 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_1_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_1_PIN, (GPIO_PinState)gpioState); |
gregeric | 0:b9444a40a999 | 361 | break; |
gregeric | 0:b9444a40a999 | 362 | default: |
gregeric | 0:b9444a40a999 | 363 | ; |
gregeric | 0:b9444a40a999 | 364 | } |
gregeric | 0:b9444a40a999 | 365 | } |
gregeric | 0:b9444a40a999 | 366 | |
gregeric | 0:b9444a40a999 | 367 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 368 | * @brief Initialise the SPI used by L6474 |
gregeric | 0:b9444a40a999 | 369 | * @param None |
gregeric | 0:b9444a40a999 | 370 | * @retval HAL_OK if SPI transaction is OK, HAL_KO else |
gregeric | 0:b9444a40a999 | 371 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 372 | uint8_t BSP_MotorControlBoard_SpiInit(void) |
gregeric | 0:b9444a40a999 | 373 | { |
gregeric | 0:b9444a40a999 | 374 | HAL_StatusTypeDef status; |
gregeric | 0:b9444a40a999 | 375 | |
gregeric | 0:b9444a40a999 | 376 | /* Initialises the SPI --------------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 377 | SpiHandle.Instance = SPIx; |
gregeric | 0:b9444a40a999 | 378 | SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; |
gregeric | 0:b9444a40a999 | 379 | SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; |
gregeric | 0:b9444a40a999 | 380 | SpiHandle.Init.CLKPhase = SPI_PHASE_2EDGE; |
gregeric | 0:b9444a40a999 | 381 | SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; |
gregeric | 0:b9444a40a999 | 382 | SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; |
gregeric | 0:b9444a40a999 | 383 | SpiHandle.Init.CRCPolynomial = 7; |
gregeric | 0:b9444a40a999 | 384 | SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; |
gregeric | 0:b9444a40a999 | 385 | SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
gregeric | 0:b9444a40a999 | 386 | SpiHandle.Init.NSS = SPI_NSS_SOFT; |
gregeric | 0:b9444a40a999 | 387 | SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; |
gregeric | 0:b9444a40a999 | 388 | |
gregeric | 0:b9444a40a999 | 389 | SpiHandle.Init.Mode = SPI_MODE_MASTER; |
gregeric | 0:b9444a40a999 | 390 | |
gregeric | 0:b9444a40a999 | 391 | status = HAL_SPI_Init(&SpiHandle); |
gregeric | 0:b9444a40a999 | 392 | |
gregeric | 0:b9444a40a999 | 393 | return (uint8_t) status; |
gregeric | 0:b9444a40a999 | 394 | } |
gregeric | 0:b9444a40a999 | 395 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 396 | * @brief Write and read SPI byte to the L6474 |
gregeric | 0:b9444a40a999 | 397 | * @param[in] pByteToTransmit pointer to the byte to transmit |
gregeric | 0:b9444a40a999 | 398 | * @param[in] pReceivedByte pointer to the received byte |
gregeric | 0:b9444a40a999 | 399 | * @param[in] nbDevices Number of device in the SPI chain |
gregeric | 0:b9444a40a999 | 400 | * @retval HAL_OK if SPI transaction is OK, HAL_KO else |
gregeric | 0:b9444a40a999 | 401 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 402 | uint8_t BSP_MotorControlBoard_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte, uint8_t nbDevices) |
gregeric | 0:b9444a40a999 | 403 | { |
gregeric | 0:b9444a40a999 | 404 | HAL_StatusTypeDef status; |
gregeric | 0:b9444a40a999 | 405 | uint32_t i; |
gregeric | 0:b9444a40a999 | 406 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CS_PORT, BSP_MOTOR_CONTROL_BOARD_CS_PIN, GPIO_PIN_RESET); |
gregeric | 0:b9444a40a999 | 407 | for (i = 0; i < nbDevices; i++) |
gregeric | 0:b9444a40a999 | 408 | { |
gregeric | 0:b9444a40a999 | 409 | status = HAL_SPI_TransmitReceive(&SpiHandle, pByteToTransmit, pReceivedByte, 1, SPIx_TIMEOUT_MAX); |
gregeric | 0:b9444a40a999 | 410 | if (status != HAL_OK) |
gregeric | 0:b9444a40a999 | 411 | { |
gregeric | 0:b9444a40a999 | 412 | break; |
gregeric | 0:b9444a40a999 | 413 | } |
gregeric | 0:b9444a40a999 | 414 | pByteToTransmit++; |
gregeric | 0:b9444a40a999 | 415 | pReceivedByte++; |
gregeric | 0:b9444a40a999 | 416 | } |
gregeric | 0:b9444a40a999 | 417 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CS_PORT, BSP_MOTOR_CONTROL_BOARD_CS_PIN, GPIO_PIN_SET); |
gregeric | 0:b9444a40a999 | 418 | |
gregeric | 0:b9444a40a999 | 419 | return (uint8_t) status; |
gregeric | 0:b9444a40a999 | 420 | } |
gregeric | 0:b9444a40a999 | 421 | |
gregeric | 0:b9444a40a999 | 422 | /** |
gregeric | 0:b9444a40a999 | 423 | * @} |
gregeric | 0:b9444a40a999 | 424 | */ |
gregeric | 0:b9444a40a999 | 425 | |
gregeric | 0:b9444a40a999 | 426 | /** |
gregeric | 0:b9444a40a999 | 427 | * @} |
gregeric | 0:b9444a40a999 | 428 | */ |
gregeric | 0:b9444a40a999 | 429 | |
gregeric | 0:b9444a40a999 | 430 | /** |
gregeric | 0:b9444a40a999 | 431 | * @} |
gregeric | 0:b9444a40a999 | 432 | */ |
gregeric | 0:b9444a40a999 | 433 | #endif |
gregeric | 0:b9444a40a999 | 434 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |