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/stm32f4xx_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 stm32f4xx_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_STM32F4 |
gregeric | 0:b9444a40a999 | 39 | /* Includes ------------------------------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 40 | #include "mbed.h" |
gregeric | 0:b9444a40a999 | 41 | #include "stm32f4xx_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 STM32F4XX_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 | 6:19c1b4a04c24 | 76 | /// Timer 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 | /** @defgroup IHM01A1_Board_Private_Function_Prototypes |
gregeric | 0:b9444a40a999 | 85 | * @{ |
gregeric | 0:b9444a40a999 | 86 | */ |
gregeric | 0:b9444a40a999 | 87 | |
gregeric | 0:b9444a40a999 | 88 | //mbed moved to header file |
gregeric | 0:b9444a40a999 | 89 | |
gregeric | 0:b9444a40a999 | 90 | /** |
gregeric | 0:b9444a40a999 | 91 | * @} |
gregeric | 0:b9444a40a999 | 92 | */ |
gregeric | 0:b9444a40a999 | 93 | |
gregeric | 0:b9444a40a999 | 94 | |
gregeric | 0:b9444a40a999 | 95 | /** @defgroup IHM01A1_Board_Private_Functions |
gregeric | 0:b9444a40a999 | 96 | * @{ |
gregeric | 0:b9444a40a999 | 97 | */ |
gregeric | 0:b9444a40a999 | 98 | |
gregeric | 0:b9444a40a999 | 99 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 100 | * @brief This function provides an accurate delay in milliseconds |
gregeric | 0:b9444a40a999 | 101 | * @param[in] delay time length in milliseconds |
gregeric | 0:b9444a40a999 | 102 | * @retval None |
gregeric | 0:b9444a40a999 | 103 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 104 | void BSP_MotorControlBoard_Delay(uint32_t delay) |
gregeric | 0:b9444a40a999 | 105 | { |
gregeric | 0:b9444a40a999 | 106 | HAL_Delay(delay); |
gregeric | 0:b9444a40a999 | 107 | } |
gregeric | 0:b9444a40a999 | 108 | |
gregeric | 0:b9444a40a999 | 109 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 110 | * @brief This function disable the interruptions |
gregeric | 0:b9444a40a999 | 111 | * @param None |
gregeric | 0:b9444a40a999 | 112 | * @retval None |
gregeric | 0:b9444a40a999 | 113 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 114 | void BSP_MotorControlBoard_DisableIrq(void) |
gregeric | 0:b9444a40a999 | 115 | { |
gregeric | 0:b9444a40a999 | 116 | __disable_irq(); |
gregeric | 0:b9444a40a999 | 117 | } |
gregeric | 0:b9444a40a999 | 118 | |
gregeric | 0:b9444a40a999 | 119 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 120 | * @brief This function enable the interruptions |
gregeric | 0:b9444a40a999 | 121 | * @param None |
gregeric | 0:b9444a40a999 | 122 | * @retval None |
gregeric | 0:b9444a40a999 | 123 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 124 | void BSP_MotorControlBoard_EnableIrq(void) |
gregeric | 0:b9444a40a999 | 125 | { |
gregeric | 0:b9444a40a999 | 126 | __enable_irq(); |
gregeric | 0:b9444a40a999 | 127 | } |
gregeric | 0:b9444a40a999 | 128 | |
gregeric | 0:b9444a40a999 | 129 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 130 | * @brief Initiliases the GPIOs used by the L6474s |
gregeric | 0:b9444a40a999 | 131 | * @param[in] nbDevices number of L6474 devices |
gregeric | 0:b9444a40a999 | 132 | * @retval None |
gregeric | 0:b9444a40a999 | 133 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 134 | void BSP_MotorControlBoard_GpioInit(uint8_t nbDevices) |
gregeric | 0:b9444a40a999 | 135 | { |
gregeric | 0:b9444a40a999 | 136 | GPIO_InitTypeDef GPIO_InitStruct; |
gregeric | 0:b9444a40a999 | 137 | |
gregeric | 0:b9444a40a999 | 138 | /* GPIO Ports Clock Enable */ |
gregeric | 0:b9444a40a999 | 139 | __GPIOC_CLK_ENABLE(); |
gregeric | 0:b9444a40a999 | 140 | __GPIOA_CLK_ENABLE(); |
gregeric | 0:b9444a40a999 | 141 | __GPIOB_CLK_ENABLE(); |
gregeric | 0:b9444a40a999 | 142 | |
gregeric | 0:b9444a40a999 | 143 | /* Configure L6474 - DIR pin for device 1 -------------------------------*/ |
gregeric | 0:b9444a40a999 | 144 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_1_PIN; |
gregeric | 0:b9444a40a999 | 145 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 146 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 147 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 148 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_1_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 149 | |
gregeric | 0:b9444a40a999 | 150 | /* Configure L6474 - Flag pin -------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 151 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_FLAG_PIN; |
gregeric | 0:b9444a40a999 | 152 | GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; |
gregeric | 0:b9444a40a999 | 153 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
gregeric | 0:b9444a40a999 | 154 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 155 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_FLAG_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 156 | |
gregeric | 0:b9444a40a999 | 157 | /* Set Priority of External Line Interrupt used for the Flag interrupt*/ |
gregeric | 0:b9444a40a999 | 158 | HAL_NVIC_SetPriority(EXTI_MCU_LINE_IRQn, 5, 0); |
gregeric | 0:b9444a40a999 | 159 | |
gregeric | 0:b9444a40a999 | 160 | /* Enable the External Line Interrupt used for the Flag interrupt*/ |
gregeric | 0:b9444a40a999 | 161 | HAL_NVIC_EnableIRQ(EXTI_MCU_LINE_IRQn); |
gregeric | 0:b9444a40a999 | 162 | |
gregeric | 0:b9444a40a999 | 163 | /* Configure L6474 - CS pin ---------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 164 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_CS_PIN; |
gregeric | 0:b9444a40a999 | 165 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 166 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 167 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 168 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_CS_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 169 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CS_PORT, BSP_MOTOR_CONTROL_BOARD_CS_PIN, GPIO_PIN_SET); |
gregeric | 0:b9444a40a999 | 170 | |
gregeric | 0:b9444a40a999 | 171 | /* Configure L6474 - STBY/RESET pin -------------------------------------*/ |
gregeric | 0:b9444a40a999 | 172 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_RESET_PIN; |
gregeric | 0:b9444a40a999 | 173 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 174 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 175 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 176 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 177 | BSP_MotorControlBoard_Reset(); |
gregeric | 0:b9444a40a999 | 178 | |
gregeric | 0:b9444a40a999 | 179 | if (nbDevices > 1) |
gregeric | 0:b9444a40a999 | 180 | { |
gregeric | 0:b9444a40a999 | 181 | /* Configure L6474 - DIR pin for device 2 ----------------------------*/ |
gregeric | 0:b9444a40a999 | 182 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_2_PIN; |
gregeric | 0:b9444a40a999 | 183 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 184 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 185 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 186 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_2_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 187 | } |
gregeric | 0:b9444a40a999 | 188 | if (nbDevices > 2) |
gregeric | 0:b9444a40a999 | 189 | { |
gregeric | 0:b9444a40a999 | 190 | /* Configure L6474 - DIR pin for device 3 ----------------------------*/ |
gregeric | 0:b9444a40a999 | 191 | GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_DIR_3_PIN; |
gregeric | 0:b9444a40a999 | 192 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
gregeric | 0:b9444a40a999 | 193 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
gregeric | 0:b9444a40a999 | 194 | GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; |
gregeric | 0:b9444a40a999 | 195 | HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_DIR_3_PORT, &GPIO_InitStruct); |
gregeric | 0:b9444a40a999 | 196 | } |
gregeric | 0:b9444a40a999 | 197 | } |
gregeric | 0:b9444a40a999 | 198 | |
gregeric | 0:b9444a40a999 | 199 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 200 | * @brief Sets the frequency of PWM1 used by device 0 |
gregeric | 0:b9444a40a999 | 201 | * @param[in] newFreq in Hz |
gregeric | 0:b9444a40a999 | 202 | * @retval None |
gregeric | 0:b9444a40a999 | 203 | * @note The frequency is directly the current speed of the device |
gregeric | 0:b9444a40a999 | 204 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 205 | void BSP_MotorControlBoard_Pwm1SetFreq(uint16_t newFreq) |
gregeric | 0:b9444a40a999 | 206 | { |
gregeric | 0:b9444a40a999 | 207 | uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); |
gregeric | 0:b9444a40a999 | 208 | uint32_t period = (sysFreq/ (TIMER_PRESCALER * BSP_MOTOR_CONTROL_BOARD_PWM1_FREQ_RESCALER * (uint32_t)newFreq)) - 1; |
gregeric | 0:b9444a40a999 | 209 | |
gregeric | 0:b9444a40a999 | 210 | __HAL_TIM_SetAutoreload(&hTimPwm1, period); |
gregeric | 0:b9444a40a999 | 211 | __HAL_TIM_SetCompare(&hTimPwm1, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1, period >> 1); |
gregeric | 0:b9444a40a999 | 212 | HAL_TIM_PWM_Start_IT(&hTimPwm1, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1); |
gregeric | 0:b9444a40a999 | 213 | } |
gregeric | 0:b9444a40a999 | 214 | |
gregeric | 0:b9444a40a999 | 215 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 216 | * @brief Sets the frequency of PWM2 used by device 1 |
gregeric | 0:b9444a40a999 | 217 | * @param[in] newFreq in Hz |
gregeric | 0:b9444a40a999 | 218 | * @retval None |
gregeric | 0:b9444a40a999 | 219 | * @note The frequency is directly the current speed of the device |
gregeric | 0:b9444a40a999 | 220 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 221 | void BSP_MotorControlBoard_Pwm2SetFreq(uint16_t newFreq) |
gregeric | 0:b9444a40a999 | 222 | { |
gregeric | 0:b9444a40a999 | 223 | uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); |
gregeric | 0:b9444a40a999 | 224 | uint32_t period = (sysFreq/ (TIMER_PRESCALER * BSP_MOTOR_CONTROL_BOARD_PWM2_FREQ_RESCALER * (uint32_t)newFreq)) - 1; |
gregeric | 0:b9444a40a999 | 225 | |
gregeric | 0:b9444a40a999 | 226 | __HAL_TIM_SetAutoreload(&hTimPwm2, period); |
gregeric | 0:b9444a40a999 | 227 | __HAL_TIM_SetCompare(&hTimPwm2, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2, period >> 1); |
gregeric | 0:b9444a40a999 | 228 | HAL_TIM_PWM_Start_IT(&hTimPwm2, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2); |
gregeric | 0:b9444a40a999 | 229 | } |
gregeric | 0:b9444a40a999 | 230 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 231 | * @brief Sets the frequency of PWM3 used by device 2 |
gregeric | 0:b9444a40a999 | 232 | * @param[in] newFreq in Hz |
gregeric | 0:b9444a40a999 | 233 | * @retval None |
gregeric | 0:b9444a40a999 | 234 | * @note The frequency is directly the current speed of the device |
gregeric | 0:b9444a40a999 | 235 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 236 | void BSP_MotorControlBoard_Pwm3SetFreq(uint16_t newFreq) |
gregeric | 0:b9444a40a999 | 237 | { |
gregeric | 0:b9444a40a999 | 238 | uint32_t sysFreq = HAL_RCC_GetSysClockFreq(); |
gregeric | 0:b9444a40a999 | 239 | /* Double the frequency as the SW is generated by SW */ |
gregeric | 0:b9444a40a999 | 240 | uint32_t period = (sysFreq/ (TIMER_PRESCALER * BSP_MOTOR_CONTROL_BOARD_PWM3_FREQ_RESCALER * (uint32_t)newFreq)) - 1; |
gregeric | 0:b9444a40a999 | 241 | |
gregeric | 0:b9444a40a999 | 242 | __HAL_TIM_SetAutoreload(&hTimPwm3, period); |
gregeric | 0:b9444a40a999 | 243 | __HAL_TIM_SetCompare(&hTimPwm3, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3, period >> 1); |
gregeric | 0:b9444a40a999 | 244 | HAL_TIM_PWM_Start_IT(&hTimPwm3, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3); |
gregeric | 0:b9444a40a999 | 245 | } |
gregeric | 0:b9444a40a999 | 246 | |
gregeric | 0:b9444a40a999 | 247 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 248 | * @brief Initialises the PWM uses by the specified device |
gregeric | 0:b9444a40a999 | 249 | * @param[in] deviceId (from 0 to 2) |
gregeric | 0:b9444a40a999 | 250 | * @retval None |
gregeric | 0:b9444a40a999 | 251 | * @note Device 0 uses PWM1 based on timer 1 |
gregeric | 0:b9444a40a999 | 252 | * Device 1 uses PWM 2 based on timer 2 |
gregeric | 0:b9444a40a999 | 253 | * Device 2 uses PWM3 based timer 0 |
gregeric | 0:b9444a40a999 | 254 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 255 | void BSP_MotorControlBoard_PwmInit(uint8_t deviceId) |
gregeric | 0:b9444a40a999 | 256 | { |
gregeric | 0:b9444a40a999 | 257 | TIM_OC_InitTypeDef sConfigOC; |
gregeric | 0:b9444a40a999 | 258 | TIM_MasterConfigTypeDef sMasterConfig; |
gregeric | 0:b9444a40a999 | 259 | TIM_HandleTypeDef *pHTim; |
gregeric | 0:b9444a40a999 | 260 | uint32_t channel; |
gregeric | 0:b9444a40a999 | 261 | |
gregeric | 0:b9444a40a999 | 262 | switch (deviceId) |
gregeric | 0:b9444a40a999 | 263 | { |
gregeric | 0:b9444a40a999 | 264 | |
gregeric | 0:b9444a40a999 | 265 | case 0: |
gregeric | 0:b9444a40a999 | 266 | default: |
gregeric | 0:b9444a40a999 | 267 | pHTim = &hTimPwm1; |
gregeric | 0:b9444a40a999 | 268 | pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_PWM1; |
gregeric | 0:b9444a40a999 | 269 | channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1; |
gregeric | 0:b9444a40a999 | 270 | |
gregeric | 0:b9444a40a999 | 271 | break; |
gregeric | 0:b9444a40a999 | 272 | case 1: |
gregeric | 0:b9444a40a999 | 273 | pHTim = &hTimPwm2; |
gregeric | 0:b9444a40a999 | 274 | pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_PWM2; |
gregeric | 0:b9444a40a999 | 275 | channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2; |
gregeric | 0:b9444a40a999 | 276 | break; |
gregeric | 0:b9444a40a999 | 277 | |
gregeric | 0:b9444a40a999 | 278 | |
gregeric | 0:b9444a40a999 | 279 | case 2: |
gregeric | 0:b9444a40a999 | 280 | pHTim = &hTimPwm3; |
gregeric | 0:b9444a40a999 | 281 | pHTim->Instance = BSP_MOTOR_CONTROL_BOARD_TIMER_PWM3; |
gregeric | 0:b9444a40a999 | 282 | channel = BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3; |
gregeric | 0:b9444a40a999 | 283 | break; |
gregeric | 0:b9444a40a999 | 284 | } |
gregeric | 0:b9444a40a999 | 285 | pHTim->Init.Prescaler = TIMER_PRESCALER -1; |
gregeric | 0:b9444a40a999 | 286 | pHTim->Init.CounterMode = TIM_COUNTERMODE_UP; |
gregeric | 0:b9444a40a999 | 287 | pHTim->Init.Period = 0; |
gregeric | 0:b9444a40a999 | 288 | pHTim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
gregeric | 0:b9444a40a999 | 289 | HAL_TIM_PWM_Init(pHTim); |
gregeric | 0:b9444a40a999 | 290 | |
gregeric | 0:b9444a40a999 | 291 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
gregeric | 0:b9444a40a999 | 292 | sConfigOC.Pulse = 0; |
gregeric | 0:b9444a40a999 | 293 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
gregeric | 0:b9444a40a999 | 294 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
gregeric | 0:b9444a40a999 | 295 | HAL_TIM_PWM_ConfigChannel(pHTim, &sConfigOC, channel); |
gregeric | 0:b9444a40a999 | 296 | |
gregeric | 0:b9444a40a999 | 297 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
gregeric | 0:b9444a40a999 | 298 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
gregeric | 0:b9444a40a999 | 299 | HAL_TIMEx_MasterConfigSynchronization(pHTim, &sMasterConfig); |
gregeric | 0:b9444a40a999 | 300 | } |
gregeric | 0:b9444a40a999 | 301 | |
gregeric | 0:b9444a40a999 | 302 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 303 | * @brief Stops the PWM uses by the specified device |
gregeric | 0:b9444a40a999 | 304 | * @param[in] deviceId (from 0 to 2) |
gregeric | 0:b9444a40a999 | 305 | * @retval None |
gregeric | 0:b9444a40a999 | 306 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 307 | void BSP_MotorControlBoard_PwmStop(uint8_t deviceId) |
gregeric | 0:b9444a40a999 | 308 | { |
gregeric | 0:b9444a40a999 | 309 | switch (deviceId) |
gregeric | 0:b9444a40a999 | 310 | { |
gregeric | 0:b9444a40a999 | 311 | case 0: |
gregeric | 0:b9444a40a999 | 312 | HAL_TIM_PWM_Stop(&hTimPwm1,BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM1); |
gregeric | 0:b9444a40a999 | 313 | |
gregeric | 0:b9444a40a999 | 314 | break; |
gregeric | 0:b9444a40a999 | 315 | case 1: |
gregeric | 0:b9444a40a999 | 316 | HAL_TIM_PWM_Stop(&hTimPwm2,BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM2); |
gregeric | 0:b9444a40a999 | 317 | |
gregeric | 0:b9444a40a999 | 318 | break; |
gregeric | 0:b9444a40a999 | 319 | case 2: |
gregeric | 0:b9444a40a999 | 320 | HAL_TIM_PWM_Stop(&hTimPwm3,BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_PWM3); |
gregeric | 0:b9444a40a999 | 321 | |
gregeric | 0:b9444a40a999 | 322 | break; |
gregeric | 0:b9444a40a999 | 323 | default: |
gregeric | 0:b9444a40a999 | 324 | break;//ignore error |
gregeric | 0:b9444a40a999 | 325 | } |
gregeric | 0:b9444a40a999 | 326 | } |
gregeric | 0:b9444a40a999 | 327 | |
gregeric | 0:b9444a40a999 | 328 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 329 | * @brief Releases the L6474 reset (pin set to High) of all devices |
gregeric | 0:b9444a40a999 | 330 | * @param None |
gregeric | 0:b9444a40a999 | 331 | * @retval None |
gregeric | 0:b9444a40a999 | 332 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 333 | void BSP_MotorControlBoard_ReleaseReset(void) |
gregeric | 0:b9444a40a999 | 334 | { |
gregeric | 0:b9444a40a999 | 335 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, BSP_MOTOR_CONTROL_BOARD_RESET_PIN, GPIO_PIN_SET); |
gregeric | 0:b9444a40a999 | 336 | } |
gregeric | 0:b9444a40a999 | 337 | |
gregeric | 0:b9444a40a999 | 338 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 339 | * @brief Resets the L6474 (reset pin set to low) of all devices |
gregeric | 0:b9444a40a999 | 340 | * @param None |
gregeric | 0:b9444a40a999 | 341 | * @retval None |
gregeric | 0:b9444a40a999 | 342 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 343 | void BSP_MotorControlBoard_Reset(void) |
gregeric | 0:b9444a40a999 | 344 | { |
gregeric | 0:b9444a40a999 | 345 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_RESET_PORT, BSP_MOTOR_CONTROL_BOARD_RESET_PIN, GPIO_PIN_RESET); |
gregeric | 0:b9444a40a999 | 346 | } |
gregeric | 0:b9444a40a999 | 347 | |
gregeric | 0:b9444a40a999 | 348 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 349 | * @brief Set the GPIO used for the direction |
gregeric | 0:b9444a40a999 | 350 | * @param[in] deviceId (from 0 to 2) |
gregeric | 0:b9444a40a999 | 351 | * @param[in] gpioState state of the direction gpio (0 to reset, 1 to set) |
gregeric | 0:b9444a40a999 | 352 | * @retval None |
gregeric | 0:b9444a40a999 | 353 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 354 | void BSP_MotorControlBoard_SetDirectionGpio(uint8_t deviceId, uint8_t gpioState) |
gregeric | 0:b9444a40a999 | 355 | { |
gregeric | 0:b9444a40a999 | 356 | switch (deviceId) |
gregeric | 0:b9444a40a999 | 357 | { |
gregeric | 0:b9444a40a999 | 358 | case 2: |
gregeric | 0:b9444a40a999 | 359 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_3_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_3_PIN, (GPIO_PinState)gpioState); |
gregeric | 0:b9444a40a999 | 360 | break; |
gregeric | 0:b9444a40a999 | 361 | case 1: |
gregeric | 0:b9444a40a999 | 362 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_2_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_2_PIN, (GPIO_PinState)gpioState); |
gregeric | 0:b9444a40a999 | 363 | break; |
gregeric | 0:b9444a40a999 | 364 | case 0: |
gregeric | 0:b9444a40a999 | 365 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_DIR_1_PORT, BSP_MOTOR_CONTROL_BOARD_DIR_1_PIN, (GPIO_PinState)gpioState); |
gregeric | 0:b9444a40a999 | 366 | break; |
gregeric | 0:b9444a40a999 | 367 | default: |
gregeric | 0:b9444a40a999 | 368 | ; |
gregeric | 0:b9444a40a999 | 369 | } |
gregeric | 0:b9444a40a999 | 370 | } |
gregeric | 0:b9444a40a999 | 371 | |
gregeric | 0:b9444a40a999 | 372 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 373 | * @brief Initialise the SPI used by L6474 |
gregeric | 0:b9444a40a999 | 374 | * @param None |
gregeric | 0:b9444a40a999 | 375 | * @retval HAL_OK if SPI transaction is OK, HAL_KO else |
gregeric | 0:b9444a40a999 | 376 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 377 | uint8_t BSP_MotorControlBoard_SpiInit(void) |
gregeric | 0:b9444a40a999 | 378 | { |
gregeric | 0:b9444a40a999 | 379 | HAL_StatusTypeDef status; |
gregeric | 0:b9444a40a999 | 380 | |
gregeric | 0:b9444a40a999 | 381 | /* Initialises the SPI --------------------------------------------------*/ |
gregeric | 0:b9444a40a999 | 382 | SpiHandle.Instance = SPIx; |
gregeric | 0:b9444a40a999 | 383 | SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; |
gregeric | 0:b9444a40a999 | 384 | SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; |
gregeric | 0:b9444a40a999 | 385 | SpiHandle.Init.CLKPhase = SPI_PHASE_2EDGE; |
gregeric | 0:b9444a40a999 | 386 | SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; |
gregeric | 0:b9444a40a999 | 387 | SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; |
gregeric | 0:b9444a40a999 | 388 | SpiHandle.Init.CRCPolynomial = 7; |
gregeric | 0:b9444a40a999 | 389 | SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; |
gregeric | 0:b9444a40a999 | 390 | SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
gregeric | 0:b9444a40a999 | 391 | SpiHandle.Init.NSS = SPI_NSS_SOFT; |
gregeric | 0:b9444a40a999 | 392 | SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; |
gregeric | 0:b9444a40a999 | 393 | |
gregeric | 0:b9444a40a999 | 394 | SpiHandle.Init.Mode = SPI_MODE_MASTER; |
gregeric | 0:b9444a40a999 | 395 | |
gregeric | 0:b9444a40a999 | 396 | status = HAL_SPI_Init(&SpiHandle); |
gregeric | 0:b9444a40a999 | 397 | |
gregeric | 0:b9444a40a999 | 398 | return (uint8_t) status; |
gregeric | 0:b9444a40a999 | 399 | } |
gregeric | 0:b9444a40a999 | 400 | /******************************************************//** |
gregeric | 0:b9444a40a999 | 401 | * @brief Write and read SPI byte to the L6474 |
gregeric | 0:b9444a40a999 | 402 | * @param[in] pByteToTransmit pointer to the byte to transmit |
gregeric | 0:b9444a40a999 | 403 | * @param[in] pReceivedByte pointer to the received byte |
gregeric | 0:b9444a40a999 | 404 | * @param[in] nbDevices Number of device in the SPI chain |
gregeric | 0:b9444a40a999 | 405 | * @retval HAL_OK if SPI transaction is OK, HAL_KO else |
gregeric | 0:b9444a40a999 | 406 | **********************************************************/ |
gregeric | 0:b9444a40a999 | 407 | uint8_t BSP_MotorControlBoard_SpiWriteBytes(uint8_t *pByteToTransmit, uint8_t *pReceivedByte, uint8_t nbDevices) |
gregeric | 0:b9444a40a999 | 408 | { |
gregeric | 0:b9444a40a999 | 409 | HAL_StatusTypeDef status; |
gregeric | 0:b9444a40a999 | 410 | uint32_t i; |
gregeric | 0:b9444a40a999 | 411 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CS_PORT, BSP_MOTOR_CONTROL_BOARD_CS_PIN, GPIO_PIN_RESET); |
gregeric | 0:b9444a40a999 | 412 | for (i = 0; i < nbDevices; i++) |
gregeric | 0:b9444a40a999 | 413 | { |
gregeric | 0:b9444a40a999 | 414 | status = HAL_SPI_TransmitReceive(&SpiHandle, pByteToTransmit, pReceivedByte, 1, SPIx_TIMEOUT_MAX); |
gregeric | 0:b9444a40a999 | 415 | if (status != HAL_OK) |
gregeric | 0:b9444a40a999 | 416 | { |
gregeric | 0:b9444a40a999 | 417 | break; |
gregeric | 0:b9444a40a999 | 418 | } |
gregeric | 0:b9444a40a999 | 419 | pByteToTransmit++; |
gregeric | 0:b9444a40a999 | 420 | pReceivedByte++; |
gregeric | 0:b9444a40a999 | 421 | } |
gregeric | 0:b9444a40a999 | 422 | HAL_GPIO_WritePin(BSP_MOTOR_CONTROL_BOARD_CS_PORT, BSP_MOTOR_CONTROL_BOARD_CS_PIN, GPIO_PIN_SET); |
gregeric | 0:b9444a40a999 | 423 | |
gregeric | 0:b9444a40a999 | 424 | return (uint8_t) status; |
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 | */ |
gregeric | 0:b9444a40a999 | 434 | |
gregeric | 0:b9444a40a999 | 435 | /** |
gregeric | 0:b9444a40a999 | 436 | * @} |
gregeric | 0:b9444a40a999 | 437 | */ |
gregeric | 0:b9444a40a999 | 438 | #endif |
gregeric | 0:b9444a40a999 | 439 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |