Advertisement
Guest User

lab2_c4

a guest
Sep 16th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  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) 2019 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.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23. #include "usb_host.h"
  24.  
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  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.  
  38. /* USER CODE END PD */
  39.  
  40. /* Private macro -------------------------------------------------------------*/
  41. /* USER CODE BEGIN PM */
  42.  
  43. /* USER CODE END PM */
  44.  
  45. /* Private variables ---------------------------------------------------------*/
  46. I2C_HandleTypeDef hi2c1;
  47.  
  48. I2S_HandleTypeDef hi2s3;
  49.  
  50. SPI_HandleTypeDef hspi1;
  51.  
  52. UART_HandleTypeDef huart2;
  53.  
  54. /* USER CODE BEGIN PV */
  55.  
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. static void MX_GPIO_Init(void);
  61. static void MX_I2C1_Init(void);
  62. static void MX_I2S3_Init(void);
  63. static void MX_SPI1_Init(void);
  64. static void MX_USART2_UART_Init(void);
  65. void MX_USB_HOST_Process(void);
  66.  
  67. /* USER CODE BEGIN PFP */
  68.  
  69. /* USER CODE END PFP */
  70.  
  71. /* Private user code ---------------------------------------------------------*/
  72. /* USER CODE BEGIN 0 */
  73.  
  74. /* USER CODE END 0 */
  75.  
  76. /**
  77. * @brief The application entry point.
  78. * @retval int
  79. */
  80. void SystemClock_Config(void)
  81. {
  82. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  83. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  84. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  85.  
  86. /** Configure the main internal regulator output voltage
  87. */
  88. __HAL_RCC_PWR_CLK_ENABLE();
  89. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  90. /** Initializes the CPU, AHB and APB busses clocks
  91. */
  92. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  93. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  94. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  95. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  96. RCC_OscInitStruct.PLL.PLLM = 8;
  97. RCC_OscInitStruct.PLL.PLLN = 336;
  98. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  99. RCC_OscInitStruct.PLL.PLLQ = 7;
  100. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  101. {
  102. Error_Handler();
  103. }
  104. /** Initializes the CPU, AHB and APB busses clocks
  105. */
  106. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  107. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  108. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  109. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  110. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  111. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  112.  
  113. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  114. {
  115. Error_Handler();
  116. }
  117. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  118. PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
  119. PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  120. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. }
  125.  
  126. int main(void)
  127. {
  128. /* USER CODE BEGIN 1 */
  129.  
  130. /* USER CODE END 1 */
  131.  
  132.  
  133. /* MCU Configuration--------------------------------------------------------*/
  134.  
  135. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  136. HAL_Init();
  137.  
  138. /* USER CODE BEGIN Init */
  139.  
  140. /* USER CODE END Init */
  141.  
  142. /* Configure the system clock */
  143. SystemClock_Config();
  144.  
  145. /* USER CODE BEGIN SysInit */
  146.  
  147. /* USER CODE END SysInit */
  148.  
  149. /* Initialize all configured peripherals */
  150. MX_GPIO_Init();
  151. MX_I2C1_Init();
  152. MX_I2S3_Init();
  153. MX_SPI1_Init();
  154. MX_USB_HOST_Init();
  155. MX_USART2_UART_Init();
  156. /* USER CODE BEGIN 2 */
  157. char c;
  158. char state_char;
  159. int state = 0;
  160. char opt;
  161. /* USER CODE END 2 */
  162.  
  163. /* Infinite loop */
  164. /* USER CODE BEGIN WHILE */
  165. while (1)
  166. {
  167. /* USER CODE END WHILE */
  168. MX_USB_HOST_Process();
  169.  
  170. /* USER CODE BEGIN 3 */
  171. if (HAL_UART_Receive(&huart2, &c, 1, 1000) == HAL_OK) {
  172. c = c;
  173. switch (state) {
  174. case 0: //x
  175. if (c == 'o'){
  176. state = 1;
  177. }
  178. else state = 5;
  179. break;
  180. case 1: //xo
  181. if (c == 'n'){
  182. state = 3;
  183. }
  184. else if (c == 'f'){
  185. state = 2;
  186. }
  187. else {
  188. state = 0;
  189. }
  190. break;
  191. case 2: //xof
  192. if (c =='f'){
  193. state = 4;
  194. } else {
  195. state = 0;
  196. }
  197. break;
  198.  
  199. case 3:
  200. if (c == '\n') {
  201. // opt = 'Y';
  202. // HAL_UART_Transmit(&huart2, &opt, 1, 1000);
  203.  
  204. } else if (c == '\r') {
  205. state = 0;
  206. opt = 'Y';
  207. //HAL_UART_Transmit(&huart2, &opt, 1, 1000);
  208. HAL_GPIO_WritePin(GPIOD, LD5_Pin, 1);
  209. }
  210. else state = 5;
  211. break;
  212. case 4:
  213. if (c == '\n') {
  214. // opt = 'X';
  215. // HAL_UART_Transmit(&huart2, &opt, 1, 1000);
  216.  
  217. } else if (c == '\r') {
  218. opt = 'X';
  219. //HAL_UART_Transmit(&huart2, &opt, 1, 1000);
  220. HAL_GPIO_WritePin(GPIOD, LD5_Pin, 0);
  221. state = 0;
  222. }
  223. else state = 5;
  224. break;
  225. case 5:
  226. if (c =='\n' || c=='\r') {
  227. state = 0;
  228. }
  229. }
  230. state_char = '0' + state;
  231. HAL_UART_Transmit(&huart2, &c, 1, 1000);
  232. }
  233. }
  234. /* USER CODE END 3 */
  235. }
  236.  
  237. /**
  238. * @brief System Clock Configuration
  239. * @retval None
  240. */
  241. /**
  242. * @brief I2C1 Initialization Function
  243. * @param None
  244. * @retval None
  245. */
  246. static void MX_I2C1_Init(void)
  247. {
  248.  
  249. /* USER CODE BEGIN I2C1_Init 0 */
  250.  
  251. /* USER CODE END I2C1_Init 0 */
  252.  
  253. /* USER CODE BEGIN I2C1_Init 1 */
  254.  
  255. /* USER CODE END I2C1_Init 1 */
  256. hi2c1.Instance = I2C1;
  257. hi2c1.Init.ClockSpeed = 100000;
  258. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  259. hi2c1.Init.OwnAddress1 = 0;
  260. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  261. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  262. hi2c1.Init.OwnAddress2 = 0;
  263. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  264. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  265. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  266. {
  267. Error_Handler();
  268. }
  269. /* USER CODE BEGIN I2C1_Init 2 */
  270.  
  271. /* USER CODE END I2C1_Init 2 */
  272.  
  273. }
  274.  
  275. /**
  276. * @brief I2S3 Initialization Function
  277. * @param None
  278. * @retval None
  279. */
  280. static void MX_I2S3_Init(void)
  281. {
  282.  
  283. /* USER CODE BEGIN I2S3_Init 0 */
  284.  
  285. /* USER CODE END I2S3_Init 0 */
  286.  
  287. /* USER CODE BEGIN I2S3_Init 1 */
  288.  
  289. /* USER CODE END I2S3_Init 1 */
  290. hi2s3.Instance = SPI3;
  291. hi2s3.Init.Mode = I2S_MODE_MASTER_TX;
  292. hi2s3.Init.Standard = I2S_STANDARD_PHILIPS;
  293. hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
  294. hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
  295. hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_96K;
  296. hi2s3.Init.CPOL = I2S_CPOL_LOW;
  297. hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
  298. hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
  299. if (HAL_I2S_Init(&hi2s3) != HAL_OK)
  300. {
  301. Error_Handler();
  302. }
  303. /* USER CODE BEGIN I2S3_Init 2 */
  304.  
  305. /* USER CODE END I2S3_Init 2 */
  306.  
  307. }
  308.  
  309. /**
  310. * @brief SPI1 Initialization Function
  311. * @param None
  312. * @retval None
  313. */
  314. static void MX_SPI1_Init(void)
  315. {
  316.  
  317. /* USER CODE BEGIN SPI1_Init 0 */
  318.  
  319. /* USER CODE END SPI1_Init 0 */
  320.  
  321. /* USER CODE BEGIN SPI1_Init 1 */
  322.  
  323. /* USER CODE END SPI1_Init 1 */
  324. /* SPI1 parameter configuration*/
  325. hspi1.Instance = SPI1;
  326. hspi1.Init.Mode = SPI_MODE_MASTER;
  327. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  328. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  329. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  330. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  331. hspi1.Init.NSS = SPI_NSS_SOFT;
  332. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  333. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  334. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  335. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  336. hspi1.Init.CRCPolynomial = 10;
  337. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  338. {
  339. Error_Handler();
  340. }
  341. /* USER CODE BEGIN SPI1_Init 2 */
  342.  
  343. /* USER CODE END SPI1_Init 2 */
  344.  
  345. }
  346.  
  347. /**
  348. * @brief USART2 Initialization Function
  349. * @param None
  350. * @retval None
  351. */
  352. static void MX_USART2_UART_Init(void)
  353. {
  354.  
  355. /* USER CODE BEGIN USART2_Init 0 */
  356.  
  357. /* USER CODE END USART2_Init 0 */
  358.  
  359. /* USER CODE BEGIN USART2_Init 1 */
  360.  
  361. /* USER CODE END USART2_Init 1 */
  362. huart2.Instance = USART2;
  363. huart2.Init.BaudRate = 115200;
  364. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  365. huart2.Init.StopBits = UART_STOPBITS_1;
  366. huart2.Init.Parity = UART_PARITY_NONE;
  367. huart2.Init.Mode = UART_MODE_TX_RX;
  368. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  369. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  370. if (HAL_UART_Init(&huart2) != HAL_OK)
  371. {
  372. Error_Handler();
  373. }
  374. /* USER CODE BEGIN USART2_Init 2 */
  375.  
  376. /* USER CODE END USART2_Init 2 */
  377.  
  378. }
  379.  
  380. /**
  381. * @brief GPIO Initialization Function
  382. * @param None
  383. * @retval None
  384. */
  385. static void MX_GPIO_Init(void)
  386. {
  387. GPIO_InitTypeDef GPIO_InitStruct = {0};
  388.  
  389. /* GPIO Ports Clock Enable */
  390. __HAL_RCC_GPIOE_CLK_ENABLE();
  391. __HAL_RCC_GPIOC_CLK_ENABLE();
  392. __HAL_RCC_GPIOH_CLK_ENABLE();
  393. __HAL_RCC_GPIOA_CLK_ENABLE();
  394. __HAL_RCC_GPIOB_CLK_ENABLE();
  395. __HAL_RCC_GPIOD_CLK_ENABLE();
  396.  
  397. /*Configure GPIO pin Output Level */
  398. HAL_GPIO_WritePin(CS_I2C_SPI_GPIO_Port, CS_I2C_SPI_Pin, GPIO_PIN_RESET);
  399.  
  400. /*Configure GPIO pin Output Level */
  401. HAL_GPIO_WritePin(OTG_FS_PowerSwitchOn_GPIO_Port, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);
  402.  
  403. /*Configure GPIO pin Output Level */
  404. HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin
  405. |Audio_RST_Pin, GPIO_PIN_RESET);
  406.  
  407. /*Configure GPIO pin : CS_I2C_SPI_Pin */
  408. GPIO_InitStruct.Pin = CS_I2C_SPI_Pin;
  409. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  410. GPIO_InitStruct.Pull = GPIO_NOPULL;
  411. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  412. HAL_GPIO_Init(CS_I2C_SPI_GPIO_Port, &GPIO_InitStruct);
  413.  
  414. /*Configure GPIO pin : OTG_FS_PowerSwitchOn_Pin */
  415. GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin;
  416. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  417. GPIO_InitStruct.Pull = GPIO_NOPULL;
  418. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  419. HAL_GPIO_Init(OTG_FS_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
  420.  
  421. /*Configure GPIO pin : PDM_OUT_Pin */
  422. GPIO_InitStruct.Pin = PDM_OUT_Pin;
  423. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  424. GPIO_InitStruct.Pull = GPIO_NOPULL;
  425. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  426. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  427. HAL_GPIO_Init(PDM_OUT_GPIO_Port, &GPIO_InitStruct);
  428.  
  429. /*Configure GPIO pin : B1_Pin */
  430. GPIO_InitStruct.Pin = B1_Pin;
  431. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  432. GPIO_InitStruct.Pull = GPIO_NOPULL;
  433. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  434.  
  435. /*Configure GPIO pin : BOOT1_Pin */
  436. GPIO_InitStruct.Pin = BOOT1_Pin;
  437. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  438. GPIO_InitStruct.Pull = GPIO_NOPULL;
  439. HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
  440.  
  441. /*Configure GPIO pin : CLK_IN_Pin */
  442. GPIO_InitStruct.Pin = CLK_IN_Pin;
  443. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  444. GPIO_InitStruct.Pull = GPIO_NOPULL;
  445. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  446. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  447. HAL_GPIO_Init(CLK_IN_GPIO_Port, &GPIO_InitStruct);
  448.  
  449. /*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin
  450. Audio_RST_Pin */
  451. GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin
  452. |Audio_RST_Pin;
  453. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  454. GPIO_InitStruct.Pull = GPIO_NOPULL;
  455. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  456. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  457.  
  458. /*Configure GPIO pin : OTG_FS_OverCurrent_Pin */
  459. GPIO_InitStruct.Pin = OTG_FS_OverCurrent_Pin;
  460. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  461. GPIO_InitStruct.Pull = GPIO_NOPULL;
  462. HAL_GPIO_Init(OTG_FS_OverCurrent_GPIO_Port, &GPIO_InitStruct);
  463.  
  464. /*Configure GPIO pin : MEMS_INT2_Pin */
  465. GPIO_InitStruct.Pin = MEMS_INT2_Pin;
  466. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  467. GPIO_InitStruct.Pull = GPIO_NOPULL;
  468. HAL_GPIO_Init(MEMS_INT2_GPIO_Port, &GPIO_InitStruct);
  469.  
  470. }
  471.  
  472. /* USER CODE BEGIN 4 */
  473.  
  474. /* USER CODE END 4 */
  475.  
  476. /**
  477. * @brief This function is executed in case of error occurrence.
  478. * @retval None
  479. */
  480. void Error_Handler(void)
  481. {
  482. /* USER CODE BEGIN Error_Handler_Debug */
  483. /* User can add his own implementation to report the HAL error return state */
  484.  
  485. /* USER CODE END Error_Handler_Debug */
  486. }
  487.  
  488. #ifdef USE_FULL_ASSERT
  489. /**
  490. * @brief Reports the name of the source file and the source line number
  491. * where the assert_param error has occurred.
  492. * @param file: pointer to the source file name
  493. * @param line: assert_param error line source number
  494. * @retval None
  495. */
  496. void assert_failed(uint8_t *file, uint32_t line)
  497. {
  498. /* USER CODE BEGIN 6 */
  499. /* User can add his own implementation to report the file name and line number,
  500. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  501. /* USER CODE END 6 */
  502. }
  503. #endif /* USE_FULL_ASSERT */
  504.  
  505. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement