xref: /btstack/port/stm32-f4discovery-usb/Core/Src/main.c (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
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_host.h"
23 
24 /* Private includes ----------------------------------------------------------*/
25 /* USER CODE BEGIN Includes */
26 #include "port.h"
27 
28 /* USER CODE END Includes */
29 
30 /* Private typedef -----------------------------------------------------------*/
31 /* USER CODE BEGIN PTD */
32 
33 /* USER CODE END PTD */
34 
35 /* Private define ------------------------------------------------------------*/
36 /* USER CODE BEGIN PD */
37 /* USER CODE END PD */
38 
39 /* Private macro -------------------------------------------------------------*/
40 /* USER CODE BEGIN PM */
41 
42 /* USER CODE END PM */
43 
44 /* Private variables ---------------------------------------------------------*/
45 
46 /* USER CODE BEGIN PV */
47 
48 /* USER CODE END PV */
49 
50 /* Private function prototypes -----------------------------------------------*/
51 void SystemClock_Config(void);
52 static void MX_GPIO_Init(void);
53 void MX_USB_HOST_Process(void);
54 
55 /* USER CODE BEGIN PFP */
56 
57 /* USER CODE END PFP */
58 
59 /* Private user code ---------------------------------------------------------*/
60 /* USER CODE BEGIN 0 */
61 
62 /* USER CODE END 0 */
63 
64 /**
65   * @brief  The application entry point.
66   * @retval int
67   */
68 int main(void)
69 {
70   /* USER CODE BEGIN 1 */
71 
72   /* USER CODE END 1 */
73 
74   /* MCU Configuration--------------------------------------------------------*/
75 
76   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
77   HAL_Init();
78 
79   /* USER CODE BEGIN Init */
80 
81   /* USER CODE END Init */
82 
83   /* Configure the system clock */
84   SystemClock_Config();
85 
86   /* USER CODE BEGIN SysInit */
87 
88   /* USER CODE END SysInit */
89 
90   /* Initialize all configured peripherals */
91   MX_GPIO_Init();
92   MX_USB_HOST_Init();
93   /* USER CODE BEGIN 2 */
94   port_main();
95   /* USER CODE END 2 */
96 
97   /* Infinite loop */
98   /* USER CODE BEGIN WHILE */
99   while (1)
100   {
101     /* USER CODE END WHILE */
102     MX_USB_HOST_Process();
103 
104     /* USER CODE BEGIN 3 */
105   }
106   /* USER CODE END 3 */
107 }
108 
109 /**
110   * @brief System Clock Configuration
111   * @retval None
112   */
113 void SystemClock_Config(void)
114 {
115   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
116   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
117 
118   /** Configure the main internal regulator output voltage
119   */
120   __HAL_RCC_PWR_CLK_ENABLE();
121   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
122   /** Initializes the RCC Oscillators according to the specified parameters
123   * in the RCC_OscInitTypeDef structure.
124   */
125   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
126   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
127   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
128   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
129   RCC_OscInitStruct.PLL.PLLM = 4;
130   RCC_OscInitStruct.PLL.PLLN = 72;
131   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
132   RCC_OscInitStruct.PLL.PLLQ = 3;
133   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
134   {
135     Error_Handler();
136   }
137   /** Initializes the CPU, AHB and APB buses clocks
138   */
139   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
140                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
141   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
142   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
143   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
144   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
145 
146   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
147   {
148     Error_Handler();
149   }
150 }
151 
152 /**
153   * @brief GPIO Initialization Function
154   * @param None
155   * @retval None
156   */
157 static void MX_GPIO_Init(void)
158 {
159   GPIO_InitTypeDef GPIO_InitStruct = {0};
160 
161   /* GPIO Ports Clock Enable */
162   __HAL_RCC_GPIOE_CLK_ENABLE();
163   __HAL_RCC_GPIOC_CLK_ENABLE();
164   __HAL_RCC_GPIOH_CLK_ENABLE();
165   __HAL_RCC_GPIOA_CLK_ENABLE();
166   __HAL_RCC_GPIOB_CLK_ENABLE();
167   __HAL_RCC_GPIOD_CLK_ENABLE();
168 
169   /*Configure GPIO pin Output Level */
170   HAL_GPIO_WritePin(CS_I2C_SPI_GPIO_Port, CS_I2C_SPI_Pin, GPIO_PIN_RESET);
171 
172   /*Configure GPIO pin Output Level */
173   HAL_GPIO_WritePin(OTG_FS_PowerSwitchOn_GPIO_Port, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);
174 
175   /*Configure GPIO pin Output Level */
176   HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin
177                           |Audio_RST_Pin, GPIO_PIN_RESET);
178 
179   /*Configure GPIO pin : CS_I2C_SPI_Pin */
180   GPIO_InitStruct.Pin = CS_I2C_SPI_Pin;
181   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
182   GPIO_InitStruct.Pull = GPIO_NOPULL;
183   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
184   HAL_GPIO_Init(CS_I2C_SPI_GPIO_Port, &GPIO_InitStruct);
185 
186   /*Configure GPIO pin : OTG_FS_PowerSwitchOn_Pin */
187   GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin;
188   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
189   GPIO_InitStruct.Pull = GPIO_NOPULL;
190   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
191   HAL_GPIO_Init(OTG_FS_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
192 
193   /*Configure GPIO pin : PDM_OUT_Pin */
194   GPIO_InitStruct.Pin = PDM_OUT_Pin;
195   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
196   GPIO_InitStruct.Pull = GPIO_NOPULL;
197   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
198   GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
199   HAL_GPIO_Init(PDM_OUT_GPIO_Port, &GPIO_InitStruct);
200 
201   /*Configure GPIO pin : B1_Pin */
202   GPIO_InitStruct.Pin = B1_Pin;
203   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
204   GPIO_InitStruct.Pull = GPIO_NOPULL;
205   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
206 
207   /*Configure GPIO pin : I2S3_WS_Pin */
208   GPIO_InitStruct.Pin = I2S3_WS_Pin;
209   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
210   GPIO_InitStruct.Pull = GPIO_NOPULL;
211   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
212   GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
213   HAL_GPIO_Init(I2S3_WS_GPIO_Port, &GPIO_InitStruct);
214 
215   /*Configure GPIO pins : SPI1_SCK_Pin SPI1_MISO_Pin SPI1_MOSI_Pin */
216   GPIO_InitStruct.Pin = SPI1_SCK_Pin|SPI1_MISO_Pin|SPI1_MOSI_Pin;
217   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
218   GPIO_InitStruct.Pull = GPIO_NOPULL;
219   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
220   GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
221   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
222 
223   /*Configure GPIO pin : BOOT1_Pin */
224   GPIO_InitStruct.Pin = BOOT1_Pin;
225   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
226   GPIO_InitStruct.Pull = GPIO_NOPULL;
227   HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
228 
229   /*Configure GPIO pin : CLK_IN_Pin */
230   GPIO_InitStruct.Pin = CLK_IN_Pin;
231   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
232   GPIO_InitStruct.Pull = GPIO_NOPULL;
233   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
234   GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
235   HAL_GPIO_Init(CLK_IN_GPIO_Port, &GPIO_InitStruct);
236 
237   /*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin
238                            Audio_RST_Pin */
239   GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin
240                           |Audio_RST_Pin;
241   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
242   GPIO_InitStruct.Pull = GPIO_NOPULL;
243   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
244   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
245 
246   /*Configure GPIO pins : I2S3_MCK_Pin I2S3_SCK_Pin I2S3_SD_Pin */
247   GPIO_InitStruct.Pin = I2S3_MCK_Pin|I2S3_SCK_Pin|I2S3_SD_Pin;
248   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
249   GPIO_InitStruct.Pull = GPIO_NOPULL;
250   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
251   GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
252   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
253 
254   /*Configure GPIO pin : OTG_FS_OverCurrent_Pin */
255   GPIO_InitStruct.Pin = OTG_FS_OverCurrent_Pin;
256   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
257   GPIO_InitStruct.Pull = GPIO_NOPULL;
258   HAL_GPIO_Init(OTG_FS_OverCurrent_GPIO_Port, &GPIO_InitStruct);
259 
260   /*Configure GPIO pins : Audio_SCL_Pin Audio_SDA_Pin */
261   GPIO_InitStruct.Pin = Audio_SCL_Pin|Audio_SDA_Pin;
262   GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
263   GPIO_InitStruct.Pull = GPIO_PULLUP;
264   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
265   GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
266   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
267 
268   /*Configure GPIO pin : MEMS_INT2_Pin */
269   GPIO_InitStruct.Pin = MEMS_INT2_Pin;
270   GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
271   GPIO_InitStruct.Pull = GPIO_NOPULL;
272   HAL_GPIO_Init(MEMS_INT2_GPIO_Port, &GPIO_InitStruct);
273 
274 }
275 
276 /* USER CODE BEGIN 4 */
277 
278 /* USER CODE END 4 */
279 
280 /**
281   * @brief  This function is executed in case of error occurrence.
282   * @retval None
283   */
284 void Error_Handler(void)
285 {
286   /* USER CODE BEGIN Error_Handler_Debug */
287   /* User can add his own implementation to report the HAL error return state */
288   __disable_irq();
289   while (1)
290   {
291   }
292   /* USER CODE END Error_Handler_Debug */
293 }
294 
295 #ifdef  USE_FULL_ASSERT
296 /**
297   * @brief  Reports the name of the source file and the source line number
298   *         where the assert_param error has occurred.
299   * @param  file: pointer to the source file name
300   * @param  line: assert_param error line source number
301   * @retval None
302   */
303 void assert_failed(uint8_t *file, uint32_t line)
304 {
305   /* USER CODE BEGIN 6 */
306   /* User can add his own implementation to report the file name and line number,
307      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
308   /* USER CODE END 6 */
309 }
310 #endif /* USE_FULL_ASSERT */
311 
312 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
313