X-CUBE-SPN11 for X-NUCLEO-IHM11M1: main_F401.c Source File

X-CUBE-SPN11 for X-NUCLEO-IHM11M1

main_F401.c
Go to the documentation of this file.
1 /**
2  ******************************************************************************
3  * @file main_F401.c
4  * @author IPC
5  * @version V0
6  * @date 10/07/2016
7  * @brief This file provides a set of functions needed to configure STM32 MCU.
8  ******************************************************************************
9  * @attention
10  *
11  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  * 3. Neither the name of STMicroelectronics nor the names of its contributors
21  * may be used to endorse or promote products derived from this software
22  * without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************
36  */
37 
38 /* Includes ------------------------------------------------------------------*/
39 #include "stm32f4xx_hal.h"
40 
41 /* USER CODE BEGIN Includes */
42 #include "6Step_Lib.h"
43 /* USER CODE END Includes */
44 
45 /* Private variables ---------------------------------------------------------*/
46 ADC_HandleTypeDef hadc1;
47 
48 TIM_HandleTypeDef htim1;
49 TIM_HandleTypeDef htim2;
50 TIM_HandleTypeDef htim3;
51 TIM_HandleTypeDef htim4;
52 
53 UART_HandleTypeDef huart2;
54 
55 /* USER CODE BEGIN PV */
56 /* USER CODE END PV */
57 
58 /* Private function prototypes -----------------------------------------------*/
59 void SystemClock_Config(void);
60 static void MX_ADC1_Init(void);
61 static void MX_TIM1_Init(void);
62 static void MX_TIM3_Init(void);
63 static void MX_TIM4_Init(void);
64 static void MX_USART2_UART_Init(void);
65 
66 /* USER CODE BEGIN PFP */
67 
68 /* USER CODE END PFP */
69 
70 /* USER CODE BEGIN 0 */
71 
72 /* USER CODE END 0 */
73 
74 int main(void)
75 {
76 
77  /* USER CODE BEGIN 1 */
78 
79  /* USER CODE END 1 */
80 
81  /* MCU Configuration----------------------------------------------------------*/
82 
83  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
84  HAL_Init();
85 
86  /* Configure the system clock */
88 
89  /* Initialize all configured peripherals */
90  MX_ADC1_Init();
91  MX_TIM1_Init();
92  MX_TIM3_Init();
93  MX_TIM4_Init();
95 
96 
97  /* USER CODE BEGIN 2 */
98  /* ****************************************************************************
99  ==============================================================================
100  ###### This function initializes 6-Step lib ######
101  ==============================================================================
102  **************************************************************************** */
103  MC_SixStep_INIT();
104  /****************************************************************************/
105  /* USER CODE END 2 */
106 
107  /* USER CODE BEGIN 3 */
108  /* Infinite loop */
109  while (1)
110  {
111 /*! **************************************************************************
112  ==============================================================================
113  ###### How to use the 6Step FW Example project ######
114  ==============================================================================
115  This workspace contains the middleware layer with Motor Control library to drive
116  a motor connected on X-Nucleo board performing a 6-step control algorithm
117  allowing the motor speed regulation through a potentiometer. The 6-step algorithm
118  is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.
119  The workspace is provided for STM32Fxx-Nucleo in four different configurations,
120  normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event
121  to start the motor, the "demo" mode starts and stop the motor automatically, the
122  "comm" mode enables the communication protocol with external PC terminal and the
123  "boot" mode enables the FW for external boot loader.
124 
125  A list of APIs is provided to send command to 6Step lib, for instance:
126 
127  (#) MC_StartMotor() -> Start the motor
128 
129  (#) MC_StoptMotor() -> Stop the motor
130 
131  (#) MC_Set_Speed(...) -> Set the new motor speed
132 
133  The MC_SixStep_param.h contains the full list of MC parameters
134 
135  ==============================================================================
136  ###### USER SPACE ######
137  ==============================================================================
138  *****************************************************************************/
139 
140 
141  /****************************************************************************/
142  }
143  /* USER CODE END 3 */
144 
145 }
146 
147 /** System Clock Configuration
148 */
150 {
151 
152  RCC_OscInitTypeDef RCC_OscInitStruct;
153  RCC_ClkInitTypeDef RCC_ClkInitStruct;
154 
155  __PWR_CLK_ENABLE();
156 
157  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
158 
159  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
160  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
161  RCC_OscInitStruct.HSICalibrationValue = 6;
162  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
163  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
164  RCC_OscInitStruct.PLL.PLLM = 16;
165  RCC_OscInitStruct.PLL.PLLN = 336;
166  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
167  RCC_OscInitStruct.PLL.PLLQ = 7;
168  HAL_RCC_OscConfig(&RCC_OscInitStruct);
169 
170  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
171  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
172  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
173  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
174  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
175  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
176 
177 }
178 
179 /* ADC1 init function */
180 void MX_ADC1_Init(void)
181 {
182 
183  ADC_ChannelConfTypeDef sConfig;
184 
185  /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
186  */
187  hadc1.Instance = ADC1;
188  hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
189  hadc1.Init.Resolution = ADC_RESOLUTION12b;
190  hadc1.Init.ScanConvMode = DISABLE;
191  hadc1.Init.ContinuousConvMode = DISABLE;
192  hadc1.Init.DiscontinuousConvMode = DISABLE;
193  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
194  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
195  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
196  hadc1.Init.NbrOfConversion = 1;
197  hadc1.Init.DMAContinuousRequests = DISABLE;
198  hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
199  HAL_ADC_Init(&hadc1);
200 
201  /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
202  */
203  sConfig.Channel = ADC_CHANNEL_2;
204  sConfig.Rank = 1;
205  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
206  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
207 
208 }
209 
210 
211 
212 /* TIM1 init function */
213 void MX_TIM1_Init(void)
214 {
215  TIM_OC_InitTypeDef sConfig;
216  TIM_SlaveConfigTypeDef sSlaveConfig;
217  TIM_MasterConfigTypeDef sMasterConfig;
218  /*##-1- Configure the TIM peripheral #######################################*/
219  /* Initialize TIMx peripheral as follow:
220  + Prescaler = 0
221  + Period = uwPeriod (to have an output frequency equal to 17.57 KHz)
222  + ClockDivision = 0
223  + Counter direction = Up
224  */
225 
226  htim1.Instance = TIM1;
227 
228  htim1.Init.Period = 839;
229  htim1.Init.Prescaler = 0;
230  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
231  htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
232  htim1.Init.RepetitionCounter = 0;
233 
234  HAL_TIM_PWM_Init(&htim1) ;
235 
236 
237  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
238  sSlaveConfig.InputTrigger = TIM_TS_ITR3;
239  HAL_TIM_SlaveConfigSynchronization(&htim1, &sSlaveConfig);
240 
241  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
242  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
243  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
244 
245  /*##-2- Configure the PWM channels #########################################*/
246  /* Common configuration for all channels */
247  sConfig.OCMode = TIM_OCMODE_PWM2;
248  sConfig.OCFastMode = TIM_OCFAST_DISABLE;
249  sConfig.OCPolarity = TIM_OCPOLARITY_LOW;
250  sConfig.OCNPolarity = TIM_OCNPOLARITY_LOW;
251  sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
252  sConfig.OCNIdleState= TIM_OCNIDLESTATE_RESET;
253  sConfig.Pulse = 100;
254 
255  /* Set the pulse value for channel 1 */
256  HAL_TIM_PWM_ConfigChannel(&htim1, &sConfig, TIM_CHANNEL_1) ;
257 
258  /* Set the pulse value for channel 2 */
259  HAL_TIM_PWM_ConfigChannel(&htim1, &sConfig, TIM_CHANNEL_2) ;
260 
261  /* Set the pulse value for channel 3 */
262  HAL_TIM_PWM_ConfigChannel(&htim1, &sConfig, TIM_CHANNEL_3) ;
263 }
264 
265 
266 
267 
268 
269 
270 /* TIM3 init function */
271 void MX_TIM3_Init(void)
272 {
273 
274  TIM_SlaveConfigTypeDef sSlaveConfig;
275  TIM_MasterConfigTypeDef sMasterConfig;
276  TIM_OC_InitTypeDef sConfigOC;
277 
278  htim3.Instance = TIM3;
279  htim3.Init.Prescaler = 0;
280  htim3.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
281  htim3.Init.Period = 839;
282  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
283  HAL_TIM_Base_Init(&htim3);
284 
285  HAL_TIM_PWM_Init(&htim3);
286 
287  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
288  sSlaveConfig.InputTrigger = TIM_TS_ITR3;
289  HAL_TIM_SlaveConfigSynchronization(&htim3, &sSlaveConfig);
290 
291  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
292  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
293  HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
294 
295  sConfigOC.OCMode = TIM_OCMODE_PWM1;
296  sConfigOC.Pulse = 671;
297  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
298  sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
299  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
300 
301 }
302 
303 /* TIM4 init function */
304 void MX_TIM4_Init(void)
305 {
306 
307  TIM_ClockConfigTypeDef sClockSourceConfig;
308  TIM_MasterConfigTypeDef sMasterConfig;
309 
310  htim4.Instance = TIM4;
311  htim4.Init.Prescaler = 0;
312  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
313  htim4.Init.Period = 24000;
314  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
315  HAL_TIM_Base_Init(&htim4);
316 
317  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
318  HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig);
319 
320  sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
321  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
322  HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig);
323 
324 }
325 
326 /* USART2 init function */
328 {
329 
330  huart2.Instance = USART2;
331  huart2.Init.BaudRate = 19200;
332  huart2.Init.WordLength = UART_WORDLENGTH_9B;
333  huart2.Init.StopBits = UART_STOPBITS_1;
334  huart2.Init.Parity = UART_PARITY_ODD;
335  huart2.Init.Mode = UART_MODE_TX_RX;
336  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
337  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
338  HAL_UART_Init(&huart2);
339 
340 }
341 
342 
343 
344 /* USER CODE BEGIN 4 */
345 
346 /* USER CODE END 4 */
347 
348 #ifdef USE_FULL_ASSERT
349 
350 /**
351  * @brief Reports the name of the source file and the source line number
352  * where the assert_param error has occurred.
353  * @param file: pointer to the source file name
354  * @param line: assert_param error line source number
355  * @retval None
356  */
357 void assert_failed(uint8_t* file, uint32_t line)
358 {
359  /* USER CODE BEGIN 6 */
360  /* User can add his own implementation to report the file name and line number,
361  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
362  /* USER CODE END 6 */
363 
364 }
365 
366 #endif
367 
368 /**
369  * @}
370  */
371 
372 /**
373  * @}
374 */
375 
376 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
static void MX_TIM4_Init(void)
Definition: main_F401.c:304
static void MX_ADC1_Init(void)
Definition: main_F401.c:180
int main(void)
Definition: main_F401.c:74
UART_HandleTypeDef huart2
Definition: main_F401.c:53
void SystemClock_Config(void)
System Clock Configuration.
Definition: main_F401.c:149
static void MX_TIM1_Init(void)
Definition: main_F401.c:213
static void MX_TIM3_Init(void)
Definition: main_F401.c:271
TIM_HandleTypeDef htim2
Definition: main_F401.c:49
ADC_HandleTypeDef hadc1
Definition: main_F401.c:46
TIM_HandleTypeDef htim4
Definition: main_F401.c:51
This header file provides the set of functions for Motor Control library.
TIM_HandleTypeDef htim3
Definition: main_F401.c:50
void MC_SixStep_INIT(void)
Definition: 6Step_Lib.c:1087
static void MX_USART2_UART_Init(void)
Definition: main_F401.c:327
TIM_HandleTypeDef htim1
Definition: main_F401.c:48
Generated by   doxygen 1.8.11