Guest User

Untitled

a guest
May 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. #include "main.h"
  2. #include "stm32f4xx_hal.h"
  3. #include "usb_host.h"
  4. ADC_HandleTypeDef hadc1;
  5. DMA_HandleTypeDef hdma_adc1;
  6. DAC_HandleTypeDef hdac;
  7. DMA_HandleTypeDef hdma_dac1;
  8. I2C_HandleTypeDef hi2c1;
  9. TIM_HandleTypeDef htim3;
  10.  
  11.  
  12. /* Private variables ---------------------------------------------------------*/
  13. int do_once;
  14. int entry_count=0;
  15. int interrupt_counter=0;
  16. uint16_t raw_value = 0;
  17. uint16_t buffer[2000]={0};
  18. float VDDAmillivolts=0;
  19.  
  20. /* USER CODE END PV */
  21.  
  22. /* Private function prototypes -----------------------------------------------*/
  23. void SystemClock_Config(void);
  24. static void MX_GPIO_Init(void);
  25. static void MX_DMA_Init(void);
  26. static void MX_I2C1_Init(void);
  27. static void MX_ADC1_Init(void);
  28. static void MX_DAC_Init(void);
  29. static void MX_TIM3_Init(void);
  30. void MX_USB_HOST_Process(void);
  31. void errorhandle();
  32.  
  33.  
  34. /* USER CODE BEGIN PFP */
  35. /* Private function prototypes -----------------------------------------------*/
  36.  
  37. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  38. {
  39. if(htim->Instance==TIM3)
  40. {
  41. HAL_ADC_Stop_DMA(&hadc1);
  42. HAL_TIM_Base_Stop_IT(&htim3);
  43. do_once=1;
  44. interrupt_counter++;
  45.  
  46. }
  47. }
  48.  
  49. /* USER CODE END PFP */
  50.  
  51.  
  52. * @brief The application entry point.
  53. *
  54. * @retval None
  55. */
  56. int main(void)
  57. {
  58.  
  59.  
  60. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61. HAL_Init();
  62. /* Configure the system clock */
  63. SystemClock_Config();
  64.  
  65. /* Initialize all configured peripherals */
  66. MX_GPIO_Init();
  67. MX_DMA_Init();
  68. MX_I2C1_Init();
  69. MX_USB_HOST_Init();
  70. MX_ADC1_Init();
  71. MX_DAC_Init();
  72. MX_TIM3_Init();
  73.  
  74.  
  75. /* Infinite loop */
  76. /* USER CODE BEGIN WHILE */
  77.  
  78. while (1)
  79. {
  80. do_once =0;
  81. for(int index=0;index<2000;index++)
  82. {
  83. buffer[index]=0;
  84. }
  85. HAL_ADC_Start(&hadc1);
  86. while (!(__HAL_ADC_GET_FLAG(&hadc1,ADC_FLAG_EOC)));
  87. raw_value=HAL_ADC_GetValue(&hadc1);
  88.  
  89. VDDAmillivolts=(raw_value*3000)/(0xFFF);
  90. if(VDDAmillivolts1>200)
  91. {
  92. entry_count++;
  93. HAL_TIM_Base_Start_IT(&htim3);
  94. while(do_once==0)
  95. {
  96.  
  97. if(HAL_ADC_Start_DMA(&hadc1,(uint32_t*)buffer,2000)!= HAL_OK)
  98. errorhandle();
  99. }
  100. }
  101.  
  102. }
  103.  
  104. /* USER CODE END WHILE */
  105.  
  106. /**
  107. * @brief System Clock Configuration
  108. * @retval None
  109. */
  110. void SystemClock_Config(void)
  111. {
  112.  
  113. RCC_OscInitTypeDef RCC_OscInitStruct;
  114. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  115.  
  116. /**Configure the main internal regulator output voltage
  117. */
  118. __HAL_RCC_PWR_CLK_ENABLE();
  119.  
  120. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  121.  
  122. /**Initializes the CPU, AHB and APB busses clocks
  123. */
  124. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  125. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  126. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  127. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  128. RCC_OscInitStruct.PLL.PLLM = 8;
  129. RCC_OscInitStruct.PLL.PLLN = 336;
  130. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  131. RCC_OscInitStruct.PLL.PLLQ = 7;
  132. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  133. {
  134. _Error_Handler(__FILE__, __LINE__);
  135. }
  136.  
  137. /**Initializes the CPU, AHB and APB busses 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_DIV4;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
  145.  
  146. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  147. {
  148. _Error_Handler(__FILE__, __LINE__);
  149. }
  150.  
  151. /**Configure the Systick interrupt time
  152. */
  153. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  154.  
  155. /**Configure the Systick
  156. */
  157. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  158.  
  159. /* SysTick_IRQn interrupt configuration */
  160. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  161. }
  162.  
  163. /* ADC1 init function */
  164. static void MX_ADC1_Init(void)
  165. {
  166.  
  167. ADC_ChannelConfTypeDef sConfig;
  168.  
  169. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  170. */
  171. hadc1.Instance = ADC1;
  172. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  173. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  174. hadc1.Init.ScanConvMode = DISABLE;
  175. hadc1.Init.ContinuousConvMode = ENABLE;
  176. hadc1.Init.DiscontinuousConvMode = DISABLE;
  177. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  178. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  179. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  180. hadc1.Init.NbrOfConversion = 1;
  181. hadc1.Init.DMAContinuousRequests = ENABLE;
  182. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  183. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  184. {
  185. _Error_Handler(__FILE__, __LINE__);
  186. }
  187.  
  188. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  189. */
  190. sConfig.Channel = ADC_CHANNEL_1;
  191. sConfig.Rank = 1;
  192. sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  193. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  194. {
  195. _Error_Handler(__FILE__, __LINE__);
  196. }
  197.  
  198. }
  199.  
  200. /* DAC init function */
  201. static void MX_DAC_Init(void)
  202. {
  203.  
  204. DAC_ChannelConfTypeDef sConfig;
  205.  
  206. /**DAC Initialization
  207. */
  208. hdac.Instance = DAC;
  209. if (HAL_DAC_Init(&hdac) != HAL_OK)
  210. {
  211. _Error_Handler(__FILE__, __LINE__);
  212. }
  213.  
  214. /**DAC channel OUT1 config
  215. */
  216. sConfig.DAC_Trigger = DAC_TRIGGER_SOFTWARE;
  217. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  218. if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  219. {
  220. _Error_Handler(__FILE__, __LINE__);
  221. }
  222.  
  223. /**DAC channel OUT2 config
  224. */
  225. if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  226. {
  227. _Error_Handler(__FILE__, __LINE__);
  228. }
  229.  
  230. }
  231.  
  232. /* I2C1 init function */
  233. static void MX_I2C1_Init(void)
  234. {
  235.  
  236. hi2c1.Instance = I2C1;
  237. hi2c1.Init.ClockSpeed = 100000;
  238. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  239. hi2c1.Init.OwnAddress1 = 0;
  240. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  241. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  242. hi2c1.Init.OwnAddress2 = 0;
  243. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  244. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  245. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  246. {
  247. _Error_Handler(__FILE__, __LINE__);
  248. }
  249.  
  250. }
  251.  
  252. /* TIM3 init function */
  253. static void MX_TIM3_Init(void)
  254. {
  255.  
  256. TIM_ClockConfigTypeDef sClockSourceConfig;
  257. TIM_MasterConfigTypeDef sMasterConfig;
  258.  
  259. htim3.Instance = TIM3;
  260. htim3.Init.Prescaler = 8399;
  261. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  262. htim3.Init.Period = 999;
  263. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  264. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  265. {
  266. _Error_Handler(__FILE__, __LINE__);
  267. }
  268.  
  269. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  270. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  271. {
  272. _Error_Handler(__FILE__, __LINE__);
  273. }
  274.  
  275. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  276. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  277. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  278. {
  279. _Error_Handler(__FILE__, __LINE__);
  280. }
  281.  
  282. }
  283.  
  284. /**
  285. * Enable DMA controller clock
  286. */
  287. static void MX_DMA_Init(void)
  288. {
  289. /* DMA controller clock enable */
  290. __HAL_RCC_DMA2_CLK_ENABLE();
  291. __HAL_RCC_DMA1_CLK_ENABLE();
  292.  
  293. /* DMA interrupt init */
  294. /* DMA1_Stream5_IRQn interrupt configuration */
  295. HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
  296. HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
  297. /* DMA2_Stream0_IRQn interrupt configuration */
  298. HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  299. HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
  300.  
  301. }
  302.  
  303. /** Configure pins as
  304. * Analog
  305. * Input
  306. * Output
  307. * EVENT_OUT
  308. * EXTI
  309. PC3 ------> I2S2_SD
  310. PA6 ------> SPI1_MISO
  311. PA7 ------> SPI1_MOSI
  312. PB10 ------> I2S2_CK
  313. PC7 ------> I2S3_MCK
  314. PC10 ------> I2S3_CK
  315. PC12 ------> I2S3_SD
  316. */
  317. static void MX_GPIO_Init(void)
  318. {
  319.  
  320. GPIO_InitTypeDef GPIO_InitStruct;
  321.  
  322. /* GPIO Ports Clock Enable */
  323. __HAL_RCC_GPIOE_CLK_ENABLE();
  324. __HAL_RCC_GPIOC_CLK_ENABLE();
  325. __HAL_RCC_GPIOH_CLK_ENABLE();
  326. __HAL_RCC_GPIOA_CLK_ENABLE();
  327. __HAL_RCC_GPIOB_CLK_ENABLE();
  328. __HAL_RCC_GPIOD_CLK_ENABLE();
  329.  
  330. /*Configure GPIO pin Output Level */
  331. HAL_GPIO_WritePin(CS_I2C_SPI_GPIO_Port, CS_I2C_SPI_Pin, GPIO_PIN_RESET);
  332.  
  333. /*Configure GPIO pin Output Level */
  334. HAL_GPIO_WritePin(OTG_FS_PowerSwitchOn_GPIO_Port, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);
  335.  
  336. /*Configure GPIO pin Output Level */
  337. HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin
  338. |Audio_RST_Pin, GPIO_PIN_RESET);
  339.  
  340. /*Configure GPIO pin : CS_I2C_SPI_Pin */
  341. GPIO_InitStruct.Pin = CS_I2C_SPI_Pin;
  342. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  343. GPIO_InitStruct.Pull = GPIO_NOPULL;
  344. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  345. HAL_GPIO_Init(CS_I2C_SPI_GPIO_Port, &GPIO_InitStruct);
  346.  
  347. /*Configure GPIO pin : OTG_FS_PowerSwitchOn_Pin */
  348. GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin;
  349. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  350. GPIO_InitStruct.Pull = GPIO_NOPULL;
  351. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  352. HAL_GPIO_Init(OTG_FS_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
  353.  
  354. /*Configure GPIO pin : PDM_OUT_Pin */
  355. GPIO_InitStruct.Pin = PDM_OUT_Pin;
  356. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  357. GPIO_InitStruct.Pull = GPIO_NOPULL;
  358. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  359. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  360. HAL_GPIO_Init(PDM_OUT_GPIO_Port, &GPIO_InitStruct);
  361.  
  362. /*Configure GPIO pin : B1_Pin */
  363. GPIO_InitStruct.Pin = B1_Pin;
  364. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  365. GPIO_InitStruct.Pull = GPIO_NOPULL;
  366. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  367.  
  368. /*Configure GPIO pins : SPI1_MISO_Pin SPI1_MOSI_Pin */
  369. GPIO_InitStruct.Pin = SPI1_MISO_Pin|SPI1_MOSI_Pin;
  370. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  371. GPIO_InitStruct.Pull = GPIO_NOPULL;
  372. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  373. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  374. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  375.  
  376. /*Configure GPIO pin : BOOT1_Pin */
  377. GPIO_InitStruct.Pin = BOOT1_Pin;
  378. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  379. GPIO_InitStruct.Pull = GPIO_NOPULL;
  380. HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
  381.  
  382. /*Configure GPIO pin : CLK_IN_Pin */
  383. GPIO_InitStruct.Pin = CLK_IN_Pin;
  384. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  385. GPIO_InitStruct.Pull = GPIO_NOPULL;
  386. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  387. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  388. HAL_GPIO_Init(CLK_IN_GPIO_Port, &GPIO_InitStruct);
  389.  
  390. /*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin
  391. Audio_RST_Pin */
  392. GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin
  393. |Audio_RST_Pin;
  394. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  395. GPIO_InitStruct.Pull = GPIO_NOPULL;
  396. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  397. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  398.  
  399. /*Configure GPIO pins : I2S3_MCK_Pin I2S3_SCK_Pin I2S3_SD_Pin */
  400. GPIO_InitStruct.Pin = I2S3_MCK_Pin|I2S3_SCK_Pin|I2S3_SD_Pin;
  401. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  402. GPIO_InitStruct.Pull = GPIO_NOPULL;
  403. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  404. GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
  405. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  406.  
  407. /*Configure GPIO pin : OTG_FS_OverCurrent_Pin */
  408. GPIO_InitStruct.Pin = OTG_FS_OverCurrent_Pin;
  409. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  410. GPIO_InitStruct.Pull = GPIO_NOPULL;
  411. HAL_GPIO_Init(OTG_FS_OverCurrent_GPIO_Port, &GPIO_InitStruct);
  412.  
  413. /*Configure GPIO pin : MEMS_INT2_Pin */
  414. GPIO_InitStruct.Pin = MEMS_INT2_Pin;
  415. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  416. GPIO_InitStruct.Pull = GPIO_NOPULL;
  417. HAL_GPIO_Init(MEMS_INT2_GPIO_Port, &GPIO_InitStruct);
  418.  
  419. }
  420.  
  421. /* USER CODE BEGIN 4 */
  422.  
  423. /* USER CODE END 4 */
  424.  
  425. /**
  426. * @brief This function is executed in case of error occurrence.
  427. * @param file: The file name as string.
  428. * @param line: The line in file as a number.
  429. * @retval None
  430. */
  431. void errorhandle()
  432. {
  433. while(1)
  434. {
  435. }
  436. }
  437. void _Error_Handler(char *file, int line)
  438. {
  439. /* USER CODE BEGIN Error_Handler_Debug */
  440. /* User can add his own implementation to report the HAL error return state */
  441. while(1)
  442. {
  443. }
  444. /* USER CODE END Error_Handler_Debug */
  445. }
  446.  
  447. #ifdef USE_FULL_ASSERT
  448. /**
  449. * @brief Reports the name of the source file and the source line number
  450. * where the assert_param error has occurred.
  451. * @param file: pointer to the source file name
  452. * @param line: assert_param error line source number
  453. * @retval None
  454. */
  455. void assert_failed(uint8_t* file, uint32_t line)
  456. {
  457. /* USER CODE BEGIN 6 */
  458. /* User can add his own implementation to report the file name and line number,
  459. tex: printf("Wrong parameters value: file %s on line %drn", file, line) */
  460. /* USER CODE END 6 */
  461. }
  462. #endif /* USE_FULL_ASSERT */
  463.  
  464. /**
  465. * @}
  466. */
  467.  
  468. /**
  469. * @}
  470. */
  471.  
  472. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Add Comment
Please, Sign In to add comment