Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.27 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * @file : main.c
  4. * @brief : Main program body
  5. ******************************************************************************
  6. * This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * Copyright (c) 2018 STMicroelectronics International N.V.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted, provided that the following conditions are met:
  17. *
  18. * 1. Redistribution of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of other
  24. * contributors to this software may be used to endorse or promote products
  25. * derived from this software without specific written permission.
  26. * 4. This software, including modifications and/or derivative works of this
  27. * software, must execute solely and exclusively on microcontroller or
  28. * microprocessor devices manufactured by or for STMicroelectronics.
  29. * 5. Redistribution and use of this software other than as permitted under
  30. * this license is void and will automatically terminate your rights under
  31. * this license.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  36. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  37. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  38. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  41. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  44. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "main.h"
  50. #include "stm32l4xx_hal.h"
  51. #include "usb_host.h"
  52.  
  53. /* USER CODE BEGIN Includes */
  54. #define USART_GPS LPUART1
  55. #define USART_GPS_IRQn LPUART1_IRQn
  56. #define USART_GPS_IRQHandler LPUART1_IRQHandler
  57.  
  58.  
  59. #ifdef USART_CR1_TXEIE_TXFNFIE // FIFO Support (L4R9)
  60. #define USART_CR1_TXEIE USART_CR1_TXEIE_TXFNFIE
  61. #define USART_ISR_TXE USART_ISR_TXE_TXFNF
  62. #define USART_CR1_RXNEIE USART_CR1_RXNEIE_RXFNEIE
  63. #define USART_ISR_RXNE USART_ISR_RXNE_RXFNE
  64. #endif
  65. #define LINEMAX 200
  66. /* USER CODE END Includes */
  67.  
  68. /* Private variables ---------------------------------------------------------*/
  69. I2C_HandleTypeDef hi2c2;
  70.  
  71. LCD_HandleTypeDef hlcd;
  72.  
  73. QSPI_HandleTypeDef hqspi;
  74.  
  75. SAI_HandleTypeDef hsai_BlockA1;
  76. SAI_HandleTypeDef hsai_BlockB1;
  77.  
  78. SPI_HandleTypeDef hspi2;
  79.  
  80. TIM_HandleTypeDef htim2;
  81.  
  82. UART_HandleTypeDef huart1;
  83. UART_HandleTypeDef huart2;
  84.  
  85. /* USER CODE BEGIN PV */
  86. /* Private variables ---------------------------------------------------------*/
  87.  
  88. /* USER CODE END PV */
  89.  
  90. /* Private function prototypes -----------------------------------------------*/
  91. void SystemClock_Config(void);
  92. static void MX_GPIO_Init(void);
  93. static void MX_I2C2_Init(void);
  94. static void MX_LCD_Init(void);
  95. static void MX_QUADSPI_Init(void);
  96. static void MX_SAI1_Init(void);
  97. static void MX_SPI2_Init(void);
  98. static void MX_USART2_UART_Init(void);
  99. static void MX_USART1_UART_Init(void);
  100. static void MX_TIM2_Init(void);
  101. void MX_USB_HOST_Process(void);
  102.  
  103. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
  104.  
  105.  
  106. /* USER CODE BEGIN PFP */
  107. /* Private function prototypes -----------------------------------------------*/
  108. uint8_t rx =0;
  109. uint8_t rx_buffer[50];
  110. /* USER CODE END PFP */
  111.  
  112. /* USER CODE BEGIN 0 */
  113. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  114. {
  115. if(htim->Instance == TIM2)
  116. {
  117.  
  118. }
  119.  
  120.  
  121. }
  122. /* USER CODE END 0 */
  123.  
  124. /**
  125. * @brief The application entry point.
  126. *
  127. * @retval None
  128. */
  129. int main(void)
  130. {
  131. /* USER CODE BEGIN 1 */
  132.  
  133. /* USER CODE END 1 */
  134.  
  135. /* MCU Configuration----------------------------------------------------------*/
  136.  
  137. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  138. HAL_Init();
  139.  
  140. /* USER CODE BEGIN Init */
  141.  
  142. /* USER CODE END Init */
  143.  
  144. /* Configure the system clock */
  145. SystemClock_Config();
  146.  
  147. /* USER CODE BEGIN SysInit */
  148.  
  149. /* USER CODE END SysInit */
  150.  
  151. /* Initialize all configured peripherals */
  152. MX_GPIO_Init();
  153. MX_I2C2_Init();
  154. MX_LCD_Init();
  155. MX_QUADSPI_Init();
  156. MX_SAI1_Init();
  157. MX_SPI2_Init();
  158. MX_USART2_UART_Init();
  159. MX_USB_HOST_Init();
  160. MX_USART1_UART_Init();
  161. MX_TIM2_Init();
  162. /* USER CODE BEGIN 2 */
  163. HAL_TIM_Base_Start_IT(&htim2);
  164. HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
  165. int i=0;
  166. /* USER CODE END 2 */
  167.  
  168. /* Infinite loop */
  169. /* USER CODE BEGIN WHILE */
  170. while (1)
  171. {
  172. /* USER CODE END WHILE */
  173. MX_USB_HOST_Process();
  174.  
  175. /* USER CODE BEGIN 3 */
  176. HAL_UART_Receive(&huart1, &rx, sizeof(char), 1000);
  177. rx_buffer[i] = rx;
  178. i++;
  179. if(rx == '\n')
  180. {
  181. if(rx_buffer[4] == 'G')
  182. {
  183.  
  184. HAL_UART_Transmit(&huart2, rx_buffer, sizeof(rx_buffer), 1000);
  185.  
  186. }
  187. i=0;
  188. }
  189.  
  190. }
  191. /* USER CODE END 3 */
  192.  
  193. }
  194.  
  195. /**
  196. * @brief System Clock Configuration
  197. * @retval None
  198. */
  199. void SystemClock_Config(void)
  200. {
  201.  
  202. RCC_OscInitTypeDef RCC_OscInitStruct;
  203. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  204. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  205.  
  206. /**Configure LSE Drive Capability
  207. */
  208. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  209.  
  210. /**Initializes the CPU, AHB and APB busses clocks
  211. */
  212. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE
  213. |RCC_OSCILLATORTYPE_MSI;
  214. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  215. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  216. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  217. RCC_OscInitStruct.MSICalibrationValue = 0;
  218. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  219. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  220. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  221. RCC_OscInitStruct.PLL.PLLM = 1;
  222. RCC_OscInitStruct.PLL.PLLN = 20;
  223. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  224. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  225. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  226. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  227. {
  228. _Error_Handler(__FILE__, __LINE__);
  229. }
  230.  
  231. /**Initializes the CPU, AHB and APB busses clocks
  232. */
  233. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  234. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  235. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  236. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  237. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  238. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  239.  
  240. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  241. {
  242. _Error_Handler(__FILE__, __LINE__);
  243. }
  244.  
  245. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
  246. |RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_SAI1
  247. |RCC_PERIPHCLK_I2C2|RCC_PERIPHCLK_USB;
  248. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  249. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  250. PeriphClkInit.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
  251. PeriphClkInit.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1;
  252. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  253. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  254. PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
  255. PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  256. PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
  257. PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  258. PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  259. PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  260. PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK|RCC_PLLSAI1_48M2CLK;
  261. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  262. {
  263. _Error_Handler(__FILE__, __LINE__);
  264. }
  265.  
  266. /**Configure the main internal regulator output voltage
  267. */
  268. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  269. {
  270. _Error_Handler(__FILE__, __LINE__);
  271. }
  272.  
  273. /**Configure the Systick interrupt time
  274. */
  275. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  276.  
  277. /**Configure the Systick
  278. */
  279. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  280.  
  281. /**Enable MSI Auto calibration
  282. */
  283. HAL_RCCEx_EnableMSIPLLMode();
  284.  
  285. /* SysTick_IRQn interrupt configuration */
  286. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  287. }
  288.  
  289. /* I2C2 init function */
  290. static void MX_I2C2_Init(void)
  291. {
  292.  
  293. hi2c2.Instance = I2C2;
  294. hi2c2.Init.Timing = 0x00404C74;
  295. hi2c2.Init.OwnAddress1 = 0;
  296. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  297. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  298. hi2c2.Init.OwnAddress2 = 0;
  299. hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  300. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  301. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  302. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  303. {
  304. _Error_Handler(__FILE__, __LINE__);
  305. }
  306.  
  307. /**Configure Analogue filter
  308. */
  309. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  310. {
  311. _Error_Handler(__FILE__, __LINE__);
  312. }
  313.  
  314. /**Configure Digital filter
  315. */
  316. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  317. {
  318. _Error_Handler(__FILE__, __LINE__);
  319. }
  320.  
  321. }
  322.  
  323. /* LCD init function */
  324. static void MX_LCD_Init(void)
  325. {
  326.  
  327. hlcd.Instance = LCD;
  328. hlcd.Init.Prescaler = LCD_PRESCALER_1;
  329. hlcd.Init.Divider = LCD_DIVIDER_16;
  330. hlcd.Init.Duty = LCD_DUTY_1_4;
  331. hlcd.Init.Bias = LCD_BIAS_1_4;
  332. hlcd.Init.VoltageSource = LCD_VOLTAGESOURCE_INTERNAL;
  333. hlcd.Init.Contrast = LCD_CONTRASTLEVEL_0;
  334. hlcd.Init.DeadTime = LCD_DEADTIME_0;
  335. hlcd.Init.PulseOnDuration = LCD_PULSEONDURATION_0;
  336. hlcd.Init.MuxSegment = LCD_MUXSEGMENT_DISABLE;
  337. hlcd.Init.BlinkMode = LCD_BLINKMODE_OFF;
  338. hlcd.Init.BlinkFrequency = LCD_BLINKFREQUENCY_DIV8;
  339. hlcd.Init.HighDrive = LCD_HIGHDRIVE_DISABLE;
  340. if (HAL_LCD_Init(&hlcd) != HAL_OK)
  341. {
  342. _Error_Handler(__FILE__, __LINE__);
  343. }
  344.  
  345. }
  346.  
  347. /* QUADSPI init function */
  348. static void MX_QUADSPI_Init(void)
  349. {
  350.  
  351. /* QUADSPI parameter configuration*/
  352. hqspi.Instance = QUADSPI;
  353. hqspi.Init.ClockPrescaler = 255;
  354. hqspi.Init.FifoThreshold = 1;
  355. hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
  356. hqspi.Init.FlashSize = 1;
  357. hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
  358. hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
  359. if (HAL_QSPI_Init(&hqspi) != HAL_OK)
  360. {
  361. _Error_Handler(__FILE__, __LINE__);
  362. }
  363.  
  364. }
  365.  
  366. /* SAI1 init function */
  367. static void MX_SAI1_Init(void)
  368. {
  369.  
  370. hsai_BlockA1.Instance = SAI1_Block_A;
  371. hsai_BlockA1.Init.Protocol = SAI_FREE_PROTOCOL;
  372. hsai_BlockA1.Init.AudioMode = SAI_MODEMASTER_TX;
  373. hsai_BlockA1.Init.DataSize = SAI_DATASIZE_24;
  374. hsai_BlockA1.Init.FirstBit = SAI_FIRSTBIT_MSB;
  375. hsai_BlockA1.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  376. hsai_BlockA1.Init.Synchro = SAI_ASYNCHRONOUS;
  377. hsai_BlockA1.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
  378. hsai_BlockA1.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
  379. hsai_BlockA1.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_EMPTY;
  380. hsai_BlockA1.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_192K;
  381. hsai_BlockA1.Init.SynchroExt = SAI_SYNCEXT_DISABLE;
  382. hsai_BlockA1.Init.MonoStereoMode = SAI_STEREOMODE;
  383. hsai_BlockA1.Init.CompandingMode = SAI_NOCOMPANDING;
  384. hsai_BlockA1.Init.TriState = SAI_OUTPUT_NOTRELEASED;
  385. hsai_BlockA1.FrameInit.FrameLength = 8;
  386. hsai_BlockA1.FrameInit.ActiveFrameLength = 1;
  387. hsai_BlockA1.FrameInit.FSDefinition = SAI_FS_STARTFRAME;
  388. hsai_BlockA1.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
  389. hsai_BlockA1.FrameInit.FSOffset = SAI_FS_FIRSTBIT;
  390. hsai_BlockA1.SlotInit.FirstBitOffset = 0;
  391. hsai_BlockA1.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
  392. hsai_BlockA1.SlotInit.SlotNumber = 1;
  393. hsai_BlockA1.SlotInit.SlotActive = 0x00000000;
  394. if (HAL_SAI_Init(&hsai_BlockA1) != HAL_OK)
  395. {
  396. _Error_Handler(__FILE__, __LINE__);
  397. }
  398.  
  399. hsai_BlockB1.Instance = SAI1_Block_B;
  400. hsai_BlockB1.Init.Protocol = SAI_FREE_PROTOCOL;
  401. hsai_BlockB1.Init.AudioMode = SAI_MODESLAVE_RX;
  402. hsai_BlockB1.Init.DataSize = SAI_DATASIZE_24;
  403. hsai_BlockB1.Init.FirstBit = SAI_FIRSTBIT_MSB;
  404. hsai_BlockB1.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  405. hsai_BlockB1.Init.Synchro = SAI_SYNCHRONOUS;
  406. hsai_BlockB1.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
  407. hsai_BlockB1.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_EMPTY;
  408. hsai_BlockB1.Init.SynchroExt = SAI_SYNCEXT_DISABLE;
  409. hsai_BlockB1.Init.MonoStereoMode = SAI_STEREOMODE;
  410. hsai_BlockB1.Init.CompandingMode = SAI_NOCOMPANDING;
  411. hsai_BlockB1.Init.TriState = SAI_OUTPUT_NOTRELEASED;
  412. hsai_BlockB1.FrameInit.FrameLength = 24;
  413. hsai_BlockB1.FrameInit.ActiveFrameLength = 1;
  414. hsai_BlockB1.FrameInit.FSDefinition = SAI_FS_STARTFRAME;
  415. hsai_BlockB1.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
  416. hsai_BlockB1.FrameInit.FSOffset = SAI_FS_FIRSTBIT;
  417. hsai_BlockB1.SlotInit.FirstBitOffset = 0;
  418. hsai_BlockB1.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
  419. hsai_BlockB1.SlotInit.SlotNumber = 1;
  420. hsai_BlockB1.SlotInit.SlotActive = 0x00000000;
  421. if (HAL_SAI_Init(&hsai_BlockB1) != HAL_OK)
  422. {
  423. _Error_Handler(__FILE__, __LINE__);
  424. }
  425.  
  426. }
  427.  
  428. /* SPI2 init function */
  429. static void MX_SPI2_Init(void)
  430. {
  431.  
  432. /* SPI2 parameter configuration*/
  433. hspi2.Instance = SPI2;
  434. hspi2.Init.Mode = SPI_MODE_MASTER;
  435. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  436. hspi2.Init.DataSize = SPI_DATASIZE_4BIT;
  437. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  438. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  439. hspi2.Init.NSS = SPI_NSS_SOFT;
  440. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  441. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  442. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  443. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  444. hspi2.Init.CRCPolynomial = 7;
  445. hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  446. hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  447. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  448. {
  449. _Error_Handler(__FILE__, __LINE__);
  450. }
  451.  
  452. }
  453.  
  454. /* TIM2 init function */
  455. static void MX_TIM2_Init(void)
  456. {
  457.  
  458. TIM_ClockConfigTypeDef sClockSourceConfig;
  459. TIM_MasterConfigTypeDef sMasterConfig;
  460. TIM_OC_InitTypeDef sConfigOC;
  461.  
  462. htim2.Instance = TIM2;
  463. htim2.Init.Prescaler = 20000;
  464. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  465. htim2.Init.Period = 999;
  466. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  467. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  468. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  469. {
  470. _Error_Handler(__FILE__, __LINE__);
  471. }
  472.  
  473. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  474. if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  475. {
  476. _Error_Handler(__FILE__, __LINE__);
  477. }
  478.  
  479. if (HAL_TIM_OC_Init(&htim2) != HAL_OK)
  480. {
  481. _Error_Handler(__FILE__, __LINE__);
  482. }
  483.  
  484. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  485. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  486. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  487. {
  488. _Error_Handler(__FILE__, __LINE__);
  489. }
  490.  
  491. sConfigOC.OCMode = TIM_OCMODE_TIMING;
  492. sConfigOC.Pulse = 0;
  493. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  494. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  495. if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  496. {
  497. _Error_Handler(__FILE__, __LINE__);
  498. }
  499.  
  500. HAL_TIM_MspPostInit(&htim2);
  501.  
  502. }
  503.  
  504. /* USART1 init function */
  505. static void MX_USART1_UART_Init(void)
  506. {
  507.  
  508. huart1.Instance = USART1;
  509. huart1.Init.BaudRate = 9600;
  510. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  511. huart1.Init.StopBits = UART_STOPBITS_1;
  512. huart1.Init.Parity = UART_PARITY_NONE;
  513. huart1.Init.Mode = UART_MODE_TX_RX;
  514. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  515. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  516. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  517. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  518. if (HAL_UART_Init(&huart1) != HAL_OK)
  519. {
  520. _Error_Handler(__FILE__, __LINE__);
  521. }
  522.  
  523. }
  524.  
  525. /* USART2 init function */
  526. static void MX_USART2_UART_Init(void)
  527. {
  528.  
  529. huart2.Instance = USART2;
  530. huart2.Init.BaudRate = 115200;
  531. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  532. huart2.Init.StopBits = UART_STOPBITS_1;
  533. huart2.Init.Parity = UART_PARITY_NONE;
  534. huart2.Init.Mode = UART_MODE_TX_RX;
  535. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  536. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  537. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  538. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  539. if (HAL_UART_Init(&huart2) != HAL_OK)
  540. {
  541. _Error_Handler(__FILE__, __LINE__);
  542. }
  543.  
  544. }
  545.  
  546. /** Configure pins as
  547. * Analog
  548. * Input
  549. * Output
  550. * EVENT_OUT
  551. * EXTI
  552. */
  553. static void MX_GPIO_Init(void)
  554. {
  555.  
  556. GPIO_InitTypeDef GPIO_InitStruct;
  557.  
  558. /* GPIO Ports Clock Enable */
  559. __HAL_RCC_GPIOE_CLK_ENABLE();
  560. __HAL_RCC_GPIOC_CLK_ENABLE();
  561. __HAL_RCC_GPIOH_CLK_ENABLE();
  562. __HAL_RCC_GPIOA_CLK_ENABLE();
  563. __HAL_RCC_GPIOB_CLK_ENABLE();
  564. __HAL_RCC_GPIOD_CLK_ENABLE();
  565.  
  566. /*Configure GPIO pin Output Level */
  567. HAL_GPIO_WritePin(GPIOE, AUDIO_RST_Pin|LD_G_Pin|XL_CS_Pin, GPIO_PIN_RESET);
  568.  
  569. /*Configure GPIO pin Output Level */
  570. HAL_GPIO_WritePin(GPIOB, LD_R_Pin|M3V3_REG_ON_Pin, GPIO_PIN_RESET);
  571.  
  572. /*Configure GPIO pin Output Level */
  573. HAL_GPIO_WritePin(OTG_FS_PowerSwitchOn_GPIO_Port, OTG_FS_PowerSwitchOn_Pin, GPIO_PIN_SET);
  574.  
  575. /*Configure GPIO pin Output Level */
  576. HAL_GPIO_WritePin(OTG_FS_VBUS_GPIO_Port, OTG_FS_VBUS_Pin, GPIO_PIN_RESET);
  577.  
  578. /*Configure GPIO pin Output Level */
  579. HAL_GPIO_WritePin(GYRO_CS_GPIO_Port, GYRO_CS_Pin, GPIO_PIN_RESET);
  580.  
  581. /*Configure GPIO pin : AUDIO_RST_Pin */
  582. GPIO_InitStruct.Pin = AUDIO_RST_Pin;
  583. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  584. GPIO_InitStruct.Pull = GPIO_NOPULL;
  585. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  586. HAL_GPIO_Init(AUDIO_RST_GPIO_Port, &GPIO_InitStruct);
  587.  
  588. /*Configure GPIO pins : MFX_IRQ_OUT_Pin OTG_FS_OverCurrent_Pin */
  589. GPIO_InitStruct.Pin = MFX_IRQ_OUT_Pin|OTG_FS_OverCurrent_Pin;
  590. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  591. GPIO_InitStruct.Pull = GPIO_NOPULL;
  592. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  593.  
  594. /*Configure GPIO pins : PC0 MAG_INT_Pin MAG_DRDY_Pin */
  595. GPIO_InitStruct.Pin = GPIO_PIN_0|MAG_INT_Pin|MAG_DRDY_Pin;
  596. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  597. GPIO_InitStruct.Pull = GPIO_NOPULL;
  598. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  599.  
  600. /*Configure GPIO pins : JOY_CENTER_Pin JOY_LEFT_Pin JOY_RIGHT_Pin JOY_UP_Pin */
  601. GPIO_InitStruct.Pin = JOY_CENTER_Pin|JOY_LEFT_Pin|JOY_RIGHT_Pin|JOY_UP_Pin;
  602. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  603. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  604. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  605.  
  606. /*Configure GPIO pin : MFX_WAKEUP_Pin */
  607. GPIO_InitStruct.Pin = MFX_WAKEUP_Pin;
  608. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  609. GPIO_InitStruct.Pull = GPIO_NOPULL;
  610. HAL_GPIO_Init(MFX_WAKEUP_GPIO_Port, &GPIO_InitStruct);
  611.  
  612. /*Configure GPIO pin : LD_R_Pin */
  613. GPIO_InitStruct.Pin = LD_R_Pin;
  614. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  615. GPIO_InitStruct.Pull = GPIO_PULLUP;
  616. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  617. HAL_GPIO_Init(LD_R_GPIO_Port, &GPIO_InitStruct);
  618.  
  619. /*Configure GPIO pin : LD_G_Pin */
  620. GPIO_InitStruct.Pin = LD_G_Pin;
  621. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  622. GPIO_InitStruct.Pull = GPIO_PULLUP;
  623. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  624. HAL_GPIO_Init(LD_G_GPIO_Port, &GPIO_InitStruct);
  625.  
  626. /*Configure GPIO pins : OTG_FS_PowerSwitchOn_Pin OTG_FS_VBUS_Pin */
  627. GPIO_InitStruct.Pin = OTG_FS_PowerSwitchOn_Pin|OTG_FS_VBUS_Pin;
  628. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  629. GPIO_InitStruct.Pull = GPIO_NOPULL;
  630. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  631. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  632.  
  633. /*Configure GPIO pins : EXT_RST_Pin GYRO_INT1_Pin */
  634. GPIO_InitStruct.Pin = EXT_RST_Pin|GYRO_INT1_Pin;
  635. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  636. GPIO_InitStruct.Pull = GPIO_NOPULL;
  637. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  638.  
  639. /*Configure GPIO pin : GYRO_CS_Pin */
  640. GPIO_InitStruct.Pin = GYRO_CS_Pin;
  641. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  642. GPIO_InitStruct.Pull = GPIO_NOPULL;
  643. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  644. HAL_GPIO_Init(GYRO_CS_GPIO_Port, &GPIO_InitStruct);
  645.  
  646. /*Configure GPIO pin : M3V3_REG_ON_Pin */
  647. GPIO_InitStruct.Pin = M3V3_REG_ON_Pin;
  648. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  649. GPIO_InitStruct.Pull = GPIO_NOPULL;
  650. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  651. HAL_GPIO_Init(M3V3_REG_ON_GPIO_Port, &GPIO_InitStruct);
  652.  
  653. /*Configure GPIO pin : GYRO_INT2_Pin */
  654. GPIO_InitStruct.Pin = GYRO_INT2_Pin;
  655. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  656. GPIO_InitStruct.Pull = GPIO_NOPULL;
  657. HAL_GPIO_Init(GYRO_INT2_GPIO_Port, &GPIO_InitStruct);
  658.  
  659. /*Configure GPIO pin : XL_CS_Pin */
  660. GPIO_InitStruct.Pin = XL_CS_Pin;
  661. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  662. GPIO_InitStruct.Pull = GPIO_NOPULL;
  663. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  664. HAL_GPIO_Init(XL_CS_GPIO_Port, &GPIO_InitStruct);
  665.  
  666. /*Configure GPIO pin : XL_INT_Pin */
  667. GPIO_InitStruct.Pin = XL_INT_Pin;
  668. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  669. GPIO_InitStruct.Pull = GPIO_NOPULL;
  670. HAL_GPIO_Init(XL_INT_GPIO_Port, &GPIO_InitStruct);
  671.  
  672. }
  673.  
  674. /* USER CODE BEGIN 4 */
  675.  
  676. /* USER CODE END 4 */
  677.  
  678. /**
  679. * @brief This function is executed in case of error occurrence.
  680. * @param file: The file name as string.
  681. * @param line: The line in file as a number.
  682. * @retval None
  683. */
  684. void _Error_Handler(char *file, int line)
  685. {
  686. /* USER CODE BEGIN Error_Handler_Debug */
  687. /* User can add his own implementation to report the HAL error return state */
  688. while(1)
  689. {
  690. }
  691. /* USER CODE END Error_Handler_Debug */
  692. }
  693.  
  694. #ifdef USE_FULL_ASSERT
  695. /**
  696. * @brief Reports the name of the source file and the source line number
  697. * where the assert_param error has occurred.
  698. * @param file: pointer to the source file name
  699. * @param line: assert_param error line source number
  700. * @retval None
  701. */
  702. void assert_failed(uint8_t* file, uint32_t line)
  703. {
  704. /* USER CODE BEGIN 6 */
  705. /* User can add his own implementation to report the file name and line number,
  706. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  707. /* USER CODE END 6 */
  708. }
  709. #endif /* USE_FULL_ASSERT */
  710.  
  711. /**
  712. * @}
  713. */
  714.  
  715. /**
  716. * @}
  717. */
  718.  
  719. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement