Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2017 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "main.h"
  35. #include "stm32l4xx_hal.h"
  36.  
  37. /* USER CODE BEGIN Includes */
  38.  
  39. /* USER CODE END Includes */
  40.  
  41. /* Private variables ---------------------------------------------------------*/
  42. I2C_HandleTypeDef hi2c1;
  43.  
  44. QSPI_HandleTypeDef hqspi;
  45.  
  46. SAI_HandleTypeDef hsai_BlockA1;
  47. DMA_HandleTypeDef hdma_sai1_a;
  48.  
  49. TIM_HandleTypeDef htim7;
  50.  
  51. /* USER CODE BEGIN PV */
  52. /* Private variables ---------------------------------------------------------*/
  53. uint32_t address = 0x90000000 + 44;
  54. uint32_t end_address = 0x90000000 + 2400044; //file size
  55. /* USER CODE END PV */
  56.  
  57. /* Private function prototypes -----------------------------------------------*/
  58. void SystemClock_Config(void);
  59. void Error_Handler(void);
  60. static void MX_GPIO_Init(void);
  61. static void MX_DMA_Init(void);
  62. static void MX_TIM7_Init(void);
  63. static void MX_I2C1_Init(void);
  64. static void MX_QUADSPI_Init(void);
  65. static void MX_SAI1_Init(void);
  66.  
  67. /* USER CODE BEGIN PFP */
  68. /* Private function prototypes -----------------------------------------------*/
  69. void codec_init(void);
  70. /* USER CODE END PFP */
  71.  
  72. /* USER CODE BEGIN 0 */
  73. void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
  74. {
  75. if(address < end_address)
  76. {
  77. address +=120000; //in bytes
  78. }
  79. else
  80. {
  81. address = 0x90000000 + 44;
  82. }
  83. DMA2_Channel1->CMAR = address;
  84. }
  85.  
  86. void codec_init(void)
  87. {
  88. uint8_t init_data[2] = {0, 0};
  89. HAL_GPIO_WritePin(GPIOE ,GPIO_PIN_3, GPIO_PIN_SET);
  90. HAL_Delay(5);
  91. init_data[0] = 0x05;
  92. init_data[1] = 0x80;
  93. HAL_I2C_Master_Transmit(&hi2c1, 0x94, init_data, 2, 100);
  94. init_data[0] = 0x06;
  95. init_data[1] = 0x07;
  96. HAL_I2C_Master_Transmit(&hi2c1, 0x94, init_data, 2, 100);
  97. init_data[0] = 0x22;
  98. init_data[1] = 0xEE;
  99. HAL_I2C_Master_Transmit(&hi2c1, 0x94, init_data, 2, 100);
  100. init_data[0] = 0x23;
  101. init_data[1] = 0xEE;
  102. HAL_I2C_Master_Transmit(&hi2c1, 0x94, init_data, 2, 100);
  103. init_data[0] = 0x02;
  104. init_data[1] = 0x9E;
  105. HAL_I2C_Master_Transmit(&hi2c1, 0x94, init_data, 2, 100);
  106. }
  107. /* USER CODE END 0 */
  108.  
  109. int main(void)
  110. {
  111.  
  112. /* USER CODE BEGIN 1 */
  113.  
  114. /* USER CODE END 1 */
  115.  
  116. /* MCU Configuration----------------------------------------------------------*/
  117.  
  118. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  119. HAL_Init();
  120.  
  121. /* Configure the system clock */
  122. SystemClock_Config();
  123.  
  124. /* Initialize all configured peripherals */
  125. MX_GPIO_Init();
  126. MX_DMA_Init();
  127. MX_TIM7_Init();
  128. MX_I2C1_Init();
  129. MX_QUADSPI_Init();
  130. MX_SAI1_Init();
  131.  
  132. /* USER CODE BEGIN 2 */
  133. QUADSPI->CCR = 0x0F282DEB;
  134. codec_init();
  135. HAL_SAI_Transmit_DMA(&hsai_BlockA1, (uint8_t *)address, 60000);
  136. /* USER CODE END 2 */
  137.  
  138. /* Infinite loop */
  139. /* USER CODE BEGIN WHILE */
  140. while (1)
  141. {
  142. /* USER CODE END WHILE */
  143.  
  144. /* USER CODE BEGIN 3 */
  145.  
  146. }
  147. /* USER CODE END 3 */
  148.  
  149. }
  150.  
  151. /** System Clock Configuration
  152. */
  153. void SystemClock_Config(void)
  154. {
  155.  
  156. RCC_OscInitTypeDef RCC_OscInitStruct;
  157. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  158. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  159.  
  160. /**Initializes the CPU, AHB and APB busses clocks
  161. */
  162. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  163. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  164. RCC_OscInitStruct.MSICalibrationValue = 0;
  165. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  166. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  167. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  168. RCC_OscInitStruct.PLL.PLLM = 1;
  169. RCC_OscInitStruct.PLL.PLLN = 40;
  170. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  171. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  172. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  173. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  174. {
  175. Error_Handler();
  176. }
  177.  
  178. /**Initializes the CPU, AHB and APB busses clocks
  179. */
  180. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  181. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  182. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  183. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  184. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  185. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  186.  
  187. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191.  
  192. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SAI1|RCC_PERIPHCLK_I2C1;
  193. PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  194. PeriphClkInit.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1;
  195. PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
  196. PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  197. PeriphClkInit.PLLSAI1.PLLSAI1N = 48;
  198. PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV17;
  199. PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  200. PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  201. PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK;
  202. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  203. {
  204. Error_Handler();
  205. }
  206.  
  207. /**Configure the main internal regulator output voltage
  208. */
  209. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  210. {
  211. Error_Handler();
  212. }
  213.  
  214. /**Configure the Systick interrupt time
  215. */
  216. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  217.  
  218. /**Configure the Systick
  219. */
  220. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  221.  
  222. /* SysTick_IRQn interrupt configuration */
  223. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  224. }
  225.  
  226. /* I2C1 init function */
  227. static void MX_I2C1_Init(void)
  228. {
  229.  
  230. hi2c1.Instance = I2C1;
  231. hi2c1.Init.Timing = 0x10909CEC;
  232. hi2c1.Init.OwnAddress1 = 0;
  233. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  234. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  235. hi2c1.Init.OwnAddress2 = 0;
  236. hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  237. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  238. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  239. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  240. {
  241. Error_Handler();
  242. }
  243.  
  244. /**Configure Analogue filter
  245. */
  246. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  247. {
  248. Error_Handler();
  249. }
  250.  
  251. }
  252.  
  253. /* QUADSPI init function */
  254. static void MX_QUADSPI_Init(void)
  255. {
  256.  
  257. hqspi.Instance = QUADSPI;
  258. hqspi.Init.ClockPrescaler = 1;
  259. hqspi.Init.FifoThreshold = 1;
  260. hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
  261. hqspi.Init.FlashSize = 23;
  262. hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
  263. hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
  264. if (HAL_QSPI_Init(&hqspi) != HAL_OK)
  265. {
  266. Error_Handler();
  267. }
  268.  
  269. }
  270.  
  271. /* SAI1 init function */
  272. static void MX_SAI1_Init(void)
  273. {
  274.  
  275. hsai_BlockA1.Instance = SAI1_Block_A;
  276. hsai_BlockA1.Init.AudioMode = SAI_MODEMASTER_TX;
  277. hsai_BlockA1.Init.Synchro = SAI_ASYNCHRONOUS;
  278. hsai_BlockA1.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
  279. hsai_BlockA1.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
  280. hsai_BlockA1.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_EMPTY;
  281. hsai_BlockA1.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_44K;
  282. hsai_BlockA1.Init.SynchroExt = SAI_SYNCEXT_DISABLE;
  283. hsai_BlockA1.Init.MonoStereoMode = SAI_STEREOMODE;
  284. hsai_BlockA1.Init.CompandingMode = SAI_NOCOMPANDING;
  285. hsai_BlockA1.Init.TriState = SAI_OUTPUT_NOTRELEASED;
  286. if (HAL_SAI_InitProtocol(&hsai_BlockA1, SAI_I2S_STANDARD, SAI_PROTOCOL_DATASIZE_16BIT, 2) != HAL_OK)
  287. {
  288. Error_Handler();
  289. }
  290.  
  291. }
  292.  
  293. /* TIM7 init function */
  294. static void MX_TIM7_Init(void)
  295. {
  296.  
  297. TIM_MasterConfigTypeDef sMasterConfig;
  298.  
  299. htim7.Instance = TIM7;
  300. htim7.Init.Prescaler = 79;
  301. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  302. htim7.Init.Period = 9999;
  303. if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  304. {
  305. Error_Handler();
  306. }
  307.  
  308. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  309. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  310. if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  311. {
  312. Error_Handler();
  313. }
  314.  
  315. }
  316.  
  317. /**
  318. * Enable DMA controller clock
  319. */
  320. static void MX_DMA_Init(void)
  321. {
  322. /* DMA controller clock enable */
  323. __HAL_RCC_DMA2_CLK_ENABLE();
  324.  
  325. /* DMA interrupt init */
  326. /* DMA2_Channel1_IRQn interrupt configuration */
  327. HAL_NVIC_SetPriority(DMA2_Channel1_IRQn, 0, 0);
  328. HAL_NVIC_EnableIRQ(DMA2_Channel1_IRQn);
  329.  
  330. }
  331.  
  332. /** Configure pins as
  333. * Analog
  334. * Input
  335. * Output
  336. * EVENT_OUT
  337. * EXTI
  338. */
  339. static void MX_GPIO_Init(void)
  340. {
  341.  
  342. GPIO_InitTypeDef GPIO_InitStruct;
  343.  
  344. /* GPIO Ports Clock Enable */
  345. __HAL_RCC_GPIOE_CLK_ENABLE();
  346. __HAL_RCC_GPIOB_CLK_ENABLE();
  347.  
  348. /*Configure GPIO pin Output Level */
  349. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3|GPIO_PIN_8, GPIO_PIN_RESET);
  350.  
  351. /*Configure GPIO pin Output Level */
  352. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
  353.  
  354. /*Configure GPIO pins : PE3 PE8 */
  355. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_8;
  356. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  357. GPIO_InitStruct.Pull = GPIO_NOPULL;
  358. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  359. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  360.  
  361. /*Configure GPIO pin : PB2 */
  362. GPIO_InitStruct.Pin = GPIO_PIN_2;
  363. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  364. GPIO_InitStruct.Pull = GPIO_NOPULL;
  365. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  366. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  367.  
  368. }
  369.  
  370. /* USER CODE BEGIN 4 */
  371.  
  372. /* USER CODE END 4 */
  373.  
  374. /**
  375. * @brief This function is executed in case of error occurrence.
  376. * @param None
  377. * @retval None
  378. */
  379. void Error_Handler(void)
  380. {
  381. /* USER CODE BEGIN Error_Handler */
  382. /* User can add his own implementation to report the HAL error return state */
  383. while(1)
  384. {
  385. }
  386. /* USER CODE END Error_Handler */
  387. }
  388.  
  389. #ifdef USE_FULL_ASSERT
  390.  
  391. /**
  392. * @brief Reports the name of the source file and the source line number
  393. * where the assert_param error has occurred.
  394. * @param file: pointer to the source file name
  395. * @param line: assert_param error line source number
  396. * @retval None
  397. */
  398. void assert_failed(uint8_t* file, uint32_t line)
  399. {
  400. /* USER CODE BEGIN 6 */
  401. /* User can add his own implementation to report the file name and line number,
  402. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  403. /* USER CODE END 6 */
  404.  
  405. }
  406.  
  407. #endif
  408.  
  409. /**
  410. * @}
  411. */
  412.  
  413. /**
  414. * @}
  415. */
  416.  
  417. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement