-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Init.cpp
More file actions
344 lines (275 loc) · 9.03 KB
/
Basic_Init.cpp
File metadata and controls
344 lines (275 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "Basic_Init.h"
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler();
}
}
void GPIO_Init(GPIO_TypeDef *GPIOx , uint16_t GPIO_Pin , uint32_t GPIO_Mode , uint8_t GPIO_Pull , uint8_t GPIO_Speed)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_Mode;
GPIO_InitStruct.Pull = GPIO_Pull;
GPIO_InitStruct.Speed = GPIO_Speed;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
void PWM_Init(TIM_HandleTypeDef* htim , TIM_TypeDef* TIM , uint8_t Channel , uint64_t PulseWidth ,
GPIO_TypeDef *GPIOx , uint16_t GPIO_Pin , uint8_t Alter_Func)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim->Instance = TIM;
htim->Init.Prescaler = F_CPU/8.0;
htim->Init.CounterMode = TIM_COUNTERMODE_UP;
htim->Init.Period = PulseWidth - 1;
htim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(htim) != HAL_OK)
{
_Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(htim, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler();
}
if (HAL_TIM_PWM_Init(htim) != HAL_OK)
{
_Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(htim, &sMasterConfig) != HAL_OK)
{
_Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(htim, &sConfigOC, Channel) != HAL_OK)
{
_Error_Handler();
}
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = Alter_Func;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
void PWM_SetDutyCycle(TIM_HandleTypeDef *htim , uint8_t Channel , float DutyCycle)
{
switch(Channel)
{
case TIM_CHANNEL_1 :
htim->Instance->CCR1 = DutyCycle*htim->Init.Period/100 - 1;
break;
case TIM_CHANNEL_2 :
htim->Instance->CCR2 = DutyCycle*htim->Init.Period/100 - 1;
break;
case TIM_CHANNEL_3 :
htim->Instance->CCR3 = DutyCycle*htim->Init.Period/100 - 1;
break;
case TIM_CHANNEL_4 :
htim->Instance->CCR4 = DutyCycle*htim->Init.Period/100 - 1;
break;
}
}
void Timer_InterruptEnable(TIM_HandleTypeDef *htim , TIM_TypeDef *TIM , uint32_t InterruptTime_us , IRQn_Type IRQ , uint8_t PreemptPriority , uint8_t SubPriority)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim->Instance = TIM;
htim->Init.Prescaler = F_CPU;
htim->Init.CounterMode = TIM_COUNTERMODE_UP;
htim->Init.Period = InterruptTime_us - 1;
htim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(htim) != HAL_OK)
{
_Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(htim, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(htim, &sMasterConfig) != HAL_OK)
{
_Error_Handler();
}
HAL_TIM_Base_Start_IT(htim);
HAL_NVIC_EnableIRQ(IRQ);
HAL_NVIC_SetPriority(IRQ,PreemptPriority,SubPriority);
}
void SYSTICKS_Init(void)
{
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 5 , 0);
}
void ExtInt_Init(GPIO_TypeDef *GPIOx , uint16_t GPIO_Pin , uint32_t GPIO_Mode , uint8_t GPIO_Pull , uint8_t PreemptPriority , uint8_t SubPriority)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_Mode;
GPIO_InitStruct.Pull = GPIO_Pull;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
switch(GPIO_Pin)
{
case GPIO_PIN_0 :
HAL_NVIC_SetPriority(EXTI0_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
break;
case GPIO_PIN_1 :
HAL_NVIC_SetPriority(EXTI1_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
break;
case GPIO_PIN_2 :
HAL_NVIC_SetPriority(EXTI2_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI2_IRQn);
break;
case GPIO_PIN_3 :
HAL_NVIC_SetPriority(EXTI3_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
break;
case GPIO_PIN_4 :
HAL_NVIC_SetPriority(EXTI4_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
break;
default :
if(GPIO_Pin>=GPIO_PIN_5 && GPIO_Pin<=GPIO_PIN_9)
{
HAL_NVIC_SetPriority(EXTI9_5_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
else
{
HAL_NVIC_SetPriority(EXTI15_10_IRQn, PreemptPriority , SubPriority);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
break;
}
}
void _Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
//while(1)
//{
//}
/* USER CODE END Error_Handler_Debug */
}
void ActuateLeftWheel(bool dir , uint16_t PWM)
{
TIM2->CCR2 = PWM;
if(dir)
{
HAL_GPIO_WritePin(GPIOA , GPIO_PIN_10 , GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA , GPIO_PIN_10 , GPIO_PIN_RESET);
}
}
void ActRightWheel(bool dir , uint16_t PWM)
{
TIM2->CCR3 = PWM;
if(dir)
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_4 , GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOB , GPIO_PIN_4 , GPIO_PIN_RESET);
}
}
void I2C_Init(I2C_HandleTypeDef *hi2c , I2C_TypeDef *I2C , uint32_t ClockSpeed , GPIO_TypeDef *GPIOx ,
uint16_t GPIO_Pin_SCL , uint16_t GPIO_Pin_SDA , uint8_t Alter_Func)
{
hi2c->Instance = I2C;
hi2c->Init.ClockSpeed = ClockSpeed;
hi2c->Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c->Init.OwnAddress1 = 0;
hi2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c->Init.OwnAddress2 = 0;
hi2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(hi2c) != HAL_OK)
{
_Error_Handler();
}
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_Pin_SCL;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = Alter_Func;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_Pin_SDA;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
HAL_I2C_Init(hi2c);
}
void HMC5883L_Init(I2C_HandleTypeDef *hi2c)
{
uint8_t data[2];
data[0] = 0;
data[1] = 0x70;
HAL_I2C_Master_Transmit(hi2c , HMC5883L_Adrr , data , 2 ,100);
data[0] = 1;
data[1] = 0xA0;
HAL_I2C_Master_Transmit(hi2c , HMC5883L_Adrr , data , 2 ,100);
data[0] = 2;
data[1] = 0x00;
HAL_I2C_Master_Transmit(hi2c , HMC5883L_Adrr , data , 2 ,100);
}
float HMC5883L_GetHeadings(I2C_HandleTypeDef *hi2c)
{
uint8_t d=3;
uint8_t raw[6];
int16_t raw_x , raw_y;
HAL_I2C_Master_Transmit(hi2c , HMC5883L_Adrr , &d , 1 ,100);
HAL_I2C_Master_Receive(hi2c , HMC5883L_Adrr | 0x01 , raw , 6 , 1000);
raw_x = (raw[0] << 8);
raw_x |= raw[1];
raw_y = (raw[4] << 8);
raw_y |= raw[5];
return (atan2((double)raw_y,(double)raw_x) * 180 / 3.1416);
}