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.

Dependencies:   mbed

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?

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