main.c


1
/* USER CODE BEGIN Header */
2
/**
3
  ******************************************************************************
4
  * @file           : main.c
5
  * @brief          : Main program body
6
  ******************************************************************************
7
  * @attention
8
  *
9
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
10
  * All rights reserved.</center></h2>
11
  *
12
  * This software component is licensed by ST under Ultimate Liberty license
13
  * SLA0044, the "License"; You may not use this file except in compliance with
14
  * the License. You may obtain a copy of the License at:
15
  *                             www.st.com/SLA0044
16
  *
17
  ******************************************************************************
18
  */
19
/* USER CODE END Header */
20
/* Includes ------------------------------------------------------------------*/
21
#include "main.h"
22
#include "usb_device.h"
23
24
/* Private includes ----------------------------------------------------------*/
25
/* USER CODE BEGIN Includes */
26
#include "taste_yt_remote.h"
27
/* USER CODE END Includes */
28
29
/* Private typedef -----------------------------------------------------------*/
30
/* USER CODE BEGIN PTD */
31
32
/* USER CODE END PTD */
33
34
/* Private define ------------------------------------------------------------*/
35
/* USER CODE BEGIN PD */
36
/* USER CODE END PD */
37
38
/* Private macro -------------------------------------------------------------*/
39
/* USER CODE BEGIN PM */
40
41
/* USER CODE END PM */
42
43
/* Private variables ---------------------------------------------------------*/
44
I2C_HandleTypeDef hi2c2;
45
46
SPI_HandleTypeDef hspi2;
47
48
TIM_HandleTypeDef htim3;
49
50
TSC_HandleTypeDef htsc;
51
52
/* USER CODE BEGIN PV */
53
54
/* USER CODE END PV */
55
56
/* Private function prototypes -----------------------------------------------*/
57
void SystemClock_Config(void);
58
static void MX_GPIO_Init(void);
59
static void MX_I2C2_Init(void);
60
static void MX_SPI2_Init(void);
61
static void MX_TSC_Init(void);
62
static void MX_TIM3_Init(void);
63
/* USER CODE BEGIN PFP */
64
65
/*Hier selbst die Fkt eingefügt */
66
//void set_blaueTaste(uint8_t push); unnötig
67
/* USER CODE END PFP */
68
69
/* Private user code ---------------------------------------------------------*/
70
/* USER CODE BEGIN 0 */
71
72
/* USER CODE END 0 */
73
74
/**
75
  * @brief  The application entry point.
76
  * @retval int
77
  */
78
int main(void)
79
{
80
  /* USER CODE BEGIN 1 */
81
82
  /* USER CODE END 1 */
83
84
  /* MCU Configuration--------------------------------------------------------*/
85
86
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
87
  HAL_Init();
88
89
  /* USER CODE BEGIN Init */
90
91
  /* USER CODE END Init */
92
93
  /* Configure the system clock */
94
  SystemClock_Config();
95
96
  /* USER CODE BEGIN SysInit */
97
98
  /* USER CODE END SysInit */
99
100
  /* Initialize all configured peripherals */
101
  MX_GPIO_Init();
102
  MX_I2C2_Init();
103
  MX_SPI2_Init();
104
  MX_TSC_Init();
105
  MX_USB_DEVICE_Init();
106
  MX_TIM3_Init();
107
  /* USER CODE BEGIN 2 */
108
  //i=0;
109
  __HAL_TIM_SET_PRESCALER(&htim3, SystemCoreClock/ 1000000 - 1);
110
  __HAL_TIM_SET_AUTORELOAD(&htim3, 1500-1);
111
  HAL_TIM_Base_Start_IT(&htim3);
112
  /* USER CODE END 2 */
113
114
  /* Infinite loop */
115
  /* USER CODE BEGIN WHILE */
116
117
  while (1)
118
  {
119
    /* USER CODE END WHILE */
120
121
    /* USER CODE BEGIN 3 */
122
  }
123
  /* USER CODE END 3 */
124
}
125
126
/**
127
  * @brief System Clock Configuration
128
  * @retval None
129
  */
130
void SystemClock_Config(void)
131
{
132
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
133
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
134
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
135
136
  /** Initializes the RCC Oscillators according to the specified parameters
137
  * in the RCC_OscInitTypeDef structure.
138
  */
139
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI48;
140
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
141
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
142
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
143
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
144
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
145
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
146
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
147
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
148
  {
149
    Error_Handler();
150
  }
151
  /** Initializes the CPU, AHB and APB buses clocks
152
  */
153
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
154
                              |RCC_CLOCKTYPE_PCLK1;
155
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
156
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
157
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
158
159
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
160
  {
161
    Error_Handler();
162
  }
163
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
164
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
165
166
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
167
  {
168
    Error_Handler();
169
  }
170
}
171
172
/**
173
  * @brief I2C2 Initialization Function
174
  * @param None
175
  * @retval None
176
  */
177
static void MX_I2C2_Init(void)
178
{
179
180
  /* USER CODE BEGIN I2C2_Init 0 */
181
182
  /* USER CODE END I2C2_Init 0 */
183
184
  /* USER CODE BEGIN I2C2_Init 1 */
185
186
  /* USER CODE END I2C2_Init 1 */
187
  hi2c2.Instance = I2C2;
188
  hi2c2.Init.Timing = 0x20303E5D;
189
  hi2c2.Init.OwnAddress1 = 0;
190
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
191
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
192
  hi2c2.Init.OwnAddress2 = 0;
193
  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
194
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
195
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
196
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
197
  {
198
    Error_Handler();
199
  }
200
  /** Configure Analogue filter
201
  */
202
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
203
  {
204
    Error_Handler();
205
  }
206
  /** Configure Digital filter
207
  */
208
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
209
  {
210
    Error_Handler();
211
  }
212
  /* USER CODE BEGIN I2C2_Init 2 */
213
214
  /* USER CODE END I2C2_Init 2 */
215
216
}
217
218
/**
219
  * @brief SPI2 Initialization Function
220
  * @param None
221
  * @retval None
222
  */
223
static void MX_SPI2_Init(void)
224
{
225
226
  /* USER CODE BEGIN SPI2_Init 0 */
227
228
  /* USER CODE END SPI2_Init 0 */
229
230
  /* USER CODE BEGIN SPI2_Init 1 */
231
232
  /* USER CODE END SPI2_Init 1 */
233
  /* SPI2 parameter configuration*/
234
  hspi2.Instance = SPI2;
235
  hspi2.Init.Mode = SPI_MODE_MASTER;
236
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
237
  hspi2.Init.DataSize = SPI_DATASIZE_4BIT;
238
  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
239
  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
240
  hspi2.Init.NSS = SPI_NSS_SOFT;
241
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
242
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
243
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
244
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
245
  hspi2.Init.CRCPolynomial = 7;
246
  hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
247
  hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
248
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
249
  {
250
    Error_Handler();
251
  }
252
  /* USER CODE BEGIN SPI2_Init 2 */
253
254
  /* USER CODE END SPI2_Init 2 */
255
256
}
257
258
/**
259
  * @brief TIM3 Initialization Function
260
  * @param None
261
  * @retval None
262
  */
263
static void MX_TIM3_Init(void)
264
{
265
266
  /* USER CODE BEGIN TIM3_Init 0 */
267
268
  /* USER CODE END TIM3_Init 0 */
269
270
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
271
  TIM_MasterConfigTypeDef sMasterConfig = {0};
272
273
  /* USER CODE BEGIN TIM3_Init 1 */
274
275
  /* USER CODE END TIM3_Init 1 */
276
  htim3.Instance = TIM3;
277
  htim3.Init.Prescaler = 47;
278
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
279
  htim3.Init.Period = 999;
280
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
281
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
282
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
283
  {
284
    Error_Handler();
285
  }
286
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
287
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
288
  {
289
    Error_Handler();
290
  }
291
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
292
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
293
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
294
  {
295
    Error_Handler();
296
  }
297
  /* USER CODE BEGIN TIM3_Init 2 */
298
299
  /* USER CODE END TIM3_Init 2 */
300
301
}
302
303
/**
304
  * @brief TSC Initialization Function
305
  * @param None
306
  * @retval None
307
  */
308
static void MX_TSC_Init(void)
309
{
310
311
  /* USER CODE BEGIN TSC_Init 0 */
312
313
  /* USER CODE END TSC_Init 0 */
314
315
  /* USER CODE BEGIN TSC_Init 1 */
316
317
  /* USER CODE END TSC_Init 1 */
318
  /** Configure the TSC peripheral
319
  */
320
  htsc.Instance = TSC;
321
  htsc.Init.CTPulseHighLength = TSC_CTPH_2CYCLES;
322
  htsc.Init.CTPulseLowLength = TSC_CTPL_2CYCLES;
323
  htsc.Init.SpreadSpectrum = DISABLE;
324
  htsc.Init.SpreadSpectrumDeviation = 1;
325
  htsc.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
326
  htsc.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV4;
327
  htsc.Init.MaxCountValue = TSC_MCV_8191;
328
  htsc.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
329
  htsc.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING;
330
  htsc.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
331
  htsc.Init.MaxCountInterrupt = DISABLE;
332
  htsc.Init.ChannelIOs = TSC_GROUP1_IO3|TSC_GROUP2_IO3|TSC_GROUP3_IO2;
333
  htsc.Init.ShieldIOs = 0;
334
  htsc.Init.SamplingIOs = TSC_GROUP1_IO4|TSC_GROUP2_IO4|TSC_GROUP3_IO3;
335
  if (HAL_TSC_Init(&htsc) != HAL_OK)
336
  {
337
    Error_Handler();
338
  }
339
  /* USER CODE BEGIN TSC_Init 2 */
340
341
  /* USER CODE END TSC_Init 2 */
342
343
}
344
345
/**
346
  * @brief GPIO Initialization Function
347
  * @param None
348
  * @retval None
349
  */
350
static void MX_GPIO_Init(void)
351
{
352
  GPIO_InitTypeDef GPIO_InitStruct = {0};
353
354
  /* GPIO Ports Clock Enable */
355
  __HAL_RCC_GPIOC_CLK_ENABLE();
356
  __HAL_RCC_GPIOA_CLK_ENABLE();
357
  __HAL_RCC_GPIOB_CLK_ENABLE();
358
359
  /*Configure GPIO pin Output Level */
360
  HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|EXT_RESET_Pin|LD3_Pin|LD6_Pin
361
                          |LD4_Pin|LD5_Pin, GPIO_PIN_RESET);
362
363
  /*Configure GPIO pins : NCS_MEMS_SPI_Pin EXT_RESET_Pin LD3_Pin LD6_Pin
364
                           LD4_Pin LD5_Pin */
365
  GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|EXT_RESET_Pin|LD3_Pin|LD6_Pin
366
                          |LD4_Pin|LD5_Pin;
367
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
368
  GPIO_InitStruct.Pull = GPIO_NOPULL;
369
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
370
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
371
372
  /*Configure GPIO pins : MEMS_INT1_Pin MEMS_INT2_Pin */
373
  GPIO_InitStruct.Pin = MEMS_INT1_Pin|MEMS_INT2_Pin;
374
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
375
  GPIO_InitStruct.Pull = GPIO_NOPULL;
376
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
377
378
  /*Configure GPIO pin : B1_Pin */
379
  GPIO_InitStruct.Pin = B1_Pin;
380
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
381
  GPIO_InitStruct.Pull = GPIO_NOPULL;
382
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
383
384
  /*Configure GPIO pin : B2_Pin */
385
  GPIO_InitStruct.Pin = B2_Pin;
386
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
387
  GPIO_InitStruct.Pull = GPIO_NOPULL;
388
  HAL_GPIO_Init(B2_GPIO_Port, &GPIO_InitStruct);
389
390
}
391
392
/* USER CODE BEGIN 4 */
393
394
/* USER CODE END 4 */
395
396
/**
397
  * @brief  This function is executed in case of error occurrence.
398
  * @retval None
399
  */
400
void Error_Handler(void)
401
{
402
  /* USER CODE BEGIN Error_Handler_Debug */
403
  /* User can add his own implementation to report the HAL error return state */
404
  __disable_irq();
405
  while (1)
406
  {
407
  }
408
  /* USER CODE END Error_Handler_Debug */
409
}
410
411
#ifdef  USE_FULL_ASSERT
412
/**
413
  * @brief  Reports the name of the source file and the source line number
414
  *         where the assert_param error has occurred.
415
  * @param  file: pointer to the source file name
416
  * @param  line: assert_param error line source number
417
  * @retval None
418
  */
419
void assert_failed(uint8_t *file, uint32_t line)
420
{
421
  /* USER CODE BEGIN 6 */
422
  /* User can add his own implementation to report the file name and line number,
423
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
424
  /* USER CODE END 6 */
425
}
426
#endif /* USE_FULL_ASSERT */
427
428
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/