comparison f103c8/Src/main.c @ 2:0c59e7a7782a

Working on GPIO and RCC
author cin
date Mon, 16 Jan 2017 11:04:47 +0300
parents
children
comparison
equal deleted inserted replaced
1:a0b14b11ad9f 2:0c59e7a7782a
1 /**
2 ******************************************************************************
3 * File Name : main.c
4 * Description : Main program body
5 ******************************************************************************
6 *
7 * COPYRIGHT(c) 2017 STMicroelectronics
8 *
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. Neither the name of STMicroelectronics nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 ******************************************************************************
32 */
33 /* Includes ------------------------------------------------------------------*/
34 #include "main.h"
35 #include "stm32f1xx_hal.h"
36
37 /* USER CODE BEGIN Includes */
38
39 /* USER CODE END Includes */
40
41 /* Private variables ---------------------------------------------------------*/
42 TIM_HandleTypeDef htim4;
43
44 /* USER CODE BEGIN PV */
45 /* Private variables ---------------------------------------------------------*/
46
47 /* USER CODE END PV */
48
49 /* Private function prototypes -----------------------------------------------*/
50 void SystemClock_Config(void);
51 void Error_Handler(void);
52 static void MX_GPIO_Init(void);
53 static void MX_TIM4_Init(void);
54 void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
55
56
57 /* USER CODE BEGIN PFP */
58 /* Private function prototypes -----------------------------------------------*/
59
60 /* USER CODE END PFP */
61
62 /* USER CODE BEGIN 0 */
63
64 /* USER CODE END 0 */
65
66 int main(void)
67 {
68
69 /* USER CODE BEGIN 1 */
70
71 /* USER CODE END 1 */
72
73 /* MCU Configuration----------------------------------------------------------*/
74
75 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
76 HAL_Init();
77
78 /* Configure the system clock */
79 SystemClock_Config();
80
81 /* Initialize all configured peripherals */
82 MX_GPIO_Init();
83 MX_TIM4_Init();
84
85 /* USER CODE BEGIN 2 */
86
87 /* USER CODE END 2 */
88
89 /* Infinite loop */
90 /* USER CODE BEGIN WHILE */
91 while (1)
92 {
93 /* USER CODE END WHILE */
94
95 /* USER CODE BEGIN 3 */
96
97 }
98 /* USER CODE END 3 */
99
100 }
101
102 /** System Clock Configuration
103 */
104 void SystemClock_Config(void)
105 {
106
107 RCC_OscInitTypeDef RCC_OscInitStruct;
108 RCC_ClkInitTypeDef RCC_ClkInitStruct;
109
110 /**Initializes the CPU, AHB and APB busses clocks
111 */
112 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
113 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
114 RCC_OscInitStruct.HSICalibrationValue = 16;
115 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
116 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
117 {
118 Error_Handler();
119 }
120
121 /**Initializes the CPU, AHB and APB busses clocks
122 */
123 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
124 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
125 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
126 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
127 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
128 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
129
130 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
131 {
132 Error_Handler();
133 }
134
135 /**Configure the Systick interrupt time
136 */
137 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
138
139 /**Configure the Systick
140 */
141 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
142
143 /* SysTick_IRQn interrupt configuration */
144 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
145 }
146
147 /* TIM4 init function */
148 static void MX_TIM4_Init(void)
149 {
150
151 TIM_ClockConfigTypeDef sClockSourceConfig;
152 TIM_MasterConfigTypeDef sMasterConfig;
153 TIM_OC_InitTypeDef sConfigOC;
154
155 htim4.Instance = TIM4;
156 htim4.Init.Prescaler = 0;
157 htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
158 htim4.Init.Period = 0;
159 htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
160 if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
161 {
162 Error_Handler();
163 }
164
165 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
166 if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
167 {
168 Error_Handler();
169 }
170
171 if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
172 {
173 Error_Handler();
174 }
175
176 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
177 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
178 if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
179 {
180 Error_Handler();
181 }
182
183 sConfigOC.OCMode = TIM_OCMODE_PWM1;
184 sConfigOC.Pulse = 0;
185 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
186 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
187 if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
188 {
189 Error_Handler();
190 }
191
192 HAL_TIM_MspPostInit(&htim4);
193
194 }
195
196 /** Configure pins as
197 * Analog
198 * Input
199 * Output
200 * EVENT_OUT
201 * EXTI
202 */
203 static void MX_GPIO_Init(void)
204 {
205
206 GPIO_InitTypeDef GPIO_InitStruct;
207
208 /* GPIO Ports Clock Enable */
209 __HAL_RCC_GPIOC_CLK_ENABLE();
210 __HAL_RCC_GPIOD_CLK_ENABLE();
211 __HAL_RCC_GPIOA_CLK_ENABLE();
212 __HAL_RCC_GPIOB_CLK_ENABLE();
213
214 /*Configure GPIO pin Output Level */
215 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
216
217 /*Configure GPIO pin : PA5 */
218 GPIO_InitStruct.Pin = GPIO_PIN_5;
219 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
220 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
221 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
222
223 }
224
225 /* USER CODE BEGIN 4 */
226
227 /* USER CODE END 4 */
228
229 /**
230 * @brief This function is executed in case of error occurrence.
231 * @param None
232 * @retval None
233 */
234 void Error_Handler(void)
235 {
236 /* USER CODE BEGIN Error_Handler */
237 /* User can add his own implementation to report the HAL error return state */
238 while(1)
239 {
240 }
241 /* USER CODE END Error_Handler */
242 }
243
244 #ifdef USE_FULL_ASSERT
245
246 /**
247 * @brief Reports the name of the source file and the source line number
248 * where the assert_param error has occurred.
249 * @param file: pointer to the source file name
250 * @param line: assert_param error line source number
251 * @retval None
252 */
253 void assert_failed(uint8_t* file, uint32_t line)
254 {
255 /* USER CODE BEGIN 6 */
256 /* User can add his own implementation to report the file name and line number,
257 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
258 /* USER CODE END 6 */
259
260 }
261
262 #endif
263
264 /**
265 * @}
266 */
267
268 /**
269 * @}
270 */
271
272 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/