fork download
  1. /**
  2.   ******************************************************************************
  3.   * @file DMA/DMA_FLASHToRAM/Src/main.c
  4.   * @author MCD Application Team
  5.   * @version V1.0.1
  6.   * @date 22-April-2016
  7.   * @brief This example provides a description of how to use a DMA channel
  8.   * to transfer a word data buffer from FLASH memory to embedded
  9.   * SRAM memory through the STM32F7xx HAL API.
  10.   ******************************************************************************
  11.   * @attention
  12.   *
  13.   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   * 1. Redistributions of source code must retain the above copyright notice,
  18.   * this list of conditions and the following disclaimer.
  19.   * 2. Redistributions in binary form must reproduce the above copyright notice,
  20.   * this list of conditions and the following disclaimer in the documentation
  21.   * and/or other materials provided with the distribution.
  22.   * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   * may be used to endorse or promote products derived from this software
  24.   * without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39.  
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "main.h"
  42.  
  43. /** @addtogroup STM32F7xx_HAL_Examples
  44.   * @{
  45.   */
  46.  
  47. /** @addtogroup DMA_FLASHToRAM
  48.   * @{
  49.   */
  50.  
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* DMA Handle declaration */
  56. DMA_HandleTypeDef DmaHandle;
  57. static GPIO_InitTypeDef GPIO_InitStruct;
  58. static const uint32_t aSRC_Const_Buffer[BUFFER_SIZE] =
  59. {
  60. 0xFFFFFFFF, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  61. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20,
  62. 0x21222324, 0x25262728, 0x292A2B2C, 0x2D2E2F30,
  63. 0x31323334, 0x35363738, 0x393A3B3C, 0x3D3E3F40,
  64. 0x41424344, 0x45464748, 0x494A4B4C, 0x4D4E4F50,
  65. 0x51525354, 0x55565758, 0x595A5B5C, 0x5D5E5F60,
  66. 0x61626364, 0x65666768, 0x696A6B6C, 0x6D6E6F70,
  67. 0x71727374, 0x75767778, 0x797A7B7C, 0x7D7E7F80
  68. };
  69.  
  70. static uint32_t aDST_Buffer[BUFFER_SIZE];
  71.  
  72. static __IO uint32_t transferErrorDetected; /* Set to 1 if an error transfer is detected */
  73. static __IO uint32_t transferCompleteDetected; /* Set to 1 if transfer is correctly completed */
  74.  
  75. /* Private function prototypes -----------------------------------------------*/
  76. static void DMA_Config(void);
  77. void SystemClock_Config(void);
  78. static void Error_Handler(void);
  79. static void TransferComplete(DMA_HandleTypeDef *DmaHandle);
  80. static void TransferError(DMA_HandleTypeDef *DmaHandle);
  81. static void CPU_CACHE_Enable(void);
  82.  
  83. /* Private functions ---------------------------------------------------------*/
  84.  
  85. /**
  86.   * @brief Main program
  87.   * @param None
  88.   * @retval None
  89.   */
  90. int main(void)
  91. {
  92. /* Enable the CPU Cache */
  93. CPU_CACHE_Enable();
  94.  
  95. /* STM32F7xx HAL library initialization:
  96.   - Configure the Flash ART accelerator
  97.   - Systick timer is configured by default as source of time base, but user
  98.   can eventually implement his proper time base source (a general purpose
  99.   timer for example or other time source), keeping in mind that Time base
  100.   duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  101.   handled in milliseconds basis.
  102.   - Set NVIC Group Priority to 4
  103.   - Low Level Initialization
  104.   */
  105. HAL_Init();
  106.  
  107. /* Configure the system clock to 216 MHz */
  108. SystemClock_Config();
  109.  
  110. /* Initialize LED */
  111. BSP_LED_Init(LED1);
  112.  
  113. /* Set to 1 if an transfer error is detected */
  114. transferErrorDetected = 0;
  115. transferCompleteDetected = 0;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /* -1- Enable GPIO Clock (to be able to program the configuration registers) */
  125. __HAL_RCC_GPIOB_CLK_ENABLE();
  126.  
  127. /* -2- Configure IO in output push-pull mode to drive external LEDs */
  128. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  129. GPIO_InitStruct.Pull = GPIO_PULLUP;
  130. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  131.  
  132. GPIO_InitStruct.Pin = GPIO_PIN_0;
  133. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  134. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
  135. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
  136.  
  137.  
  138.  
  139. /* Configure and enable the DMA stream for Memory to Memory transfer */
  140. DMA_Config();
  141.  
  142. /* Infinite loop */
  143. while (1)
  144. {
  145. if (transferErrorDetected == 1)
  146. {
  147. /* Toggle LED1 with a period of 200 ms */
  148. BSP_LED_Toggle(LED1);
  149. HAL_Delay(200);
  150. }
  151. if (transferCompleteDetected == 1)
  152. {
  153. /* Turn LED1 on*/
  154. BSP_LED_On(LED1);
  155. transferCompleteDetected = 0;
  156. }
  157. }
  158. }
  159.  
  160. /**
  161.   * @brief Configure the DMA controller according to the Stream parameters
  162.   * defined in main.h file
  163.   * @note This function is used to :
  164.   * -1- Enable DMA2 clock
  165.   * -2- Select the DMA functional Parameters
  166.   * -3- Select the DMA instance to be used for the transfer
  167.   * -4- Select Callbacks functions called after Transfer complete and
  168.   Transfer error interrupt detection
  169.   * -5- Initialize the DMA stream
  170.   * -6- Configure NVIC for DMA transfer complete/error interrupts
  171.   * -7- Start the DMA transfer using the interrupt mode
  172.   * @param None
  173.   * @retval None
  174.   */
  175. static void DMA_Config(void)
  176. {
  177. /*## -1- Enable DMA2 clock #################################################*/
  178. __HAL_RCC_DMA2_CLK_ENABLE();
  179.  
  180. /*##-2- Select the DMA functional Parameters ###############################*/
  181. DmaHandle.Init.Channel = DMA_CHANNEL; /* DMA_CHANNEL_0 */
  182. DmaHandle.Init.Direction = DMA_MEMORY_TO_PERIPH; /* M2M transfer mode */
  183. DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; /* Peripheral increment mode Enable */
  184. DmaHandle.Init.MemInc = DMA_MINC_ENABLE; /* Memory increment mode Enable */
  185. DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; /* Peripheral data alignment : Word */
  186. DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; /* memory data alignment : Word */
  187. DmaHandle.Init.Mode = DMA_NORMAL; /* Normal DMA mode */
  188. DmaHandle.Init.Priority = DMA_PRIORITY_HIGH; /* priority level : high */
  189. DmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE; /* FIFO mode disabled */
  190. DmaHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  191. DmaHandle.Init.MemBurst = DMA_MBURST_SINGLE; /* Memory burst */
  192. DmaHandle.Init.PeriphBurst = DMA_PBURST_SINGLE; /* Peripheral burst */
  193.  
  194. /*##-3- Select the DMA instance to be used for the transfer : DMA2_Stream0 #*/
  195. DmaHandle.Instance = DMA_INSTANCE;
  196.  
  197. /*##-4- Initialize the DMA stream ##########################################*/
  198. if (HAL_DMA_Init(&DmaHandle) != HAL_OK)
  199. {
  200. /* Initialization Error */
  201. Error_Handler();
  202. }
  203.  
  204. /*##-5- Select Callbacks functions called after Transfer complete and Transfer error */
  205. HAL_DMA_RegisterCallback(&DmaHandle, HAL_DMA_XFER_CPLT_CB_ID, TransferComplete);
  206. HAL_DMA_RegisterCallback(&DmaHandle, HAL_DMA_XFER_ERROR_CB_ID, TransferError);
  207.  
  208. /*##-6- Configure NVIC for DMA transfer complete/error interrupts ##########*/
  209. /* Set Interrupt Group Priority */
  210. HAL_NVIC_SetPriority(DMA_INSTANCE_IRQ, 0, 0);
  211.  
  212. /* Enable the DMA STREAM global Interrupt */
  213. HAL_NVIC_EnableIRQ(DMA_INSTANCE_IRQ);
  214.  
  215. /*##-7- Start the DMA transfer using the interrupt mode ####################*/
  216. /* Configure the source, destination and buffer size DMA fields and Start DMA Stream transfer */
  217. /* Enable All the DMA interrupts */
  218. if (HAL_DMA_Start_IT(&DmaHandle, (uint32_t)&aSRC_Const_Buffer, (uint32_t)&GPIOB->ODR, BUFFER_SIZE) != HAL_OK)
  219. {
  220. /* Transfer Error */
  221. Error_Handler();
  222. }
  223. }
  224.  
  225. /**
  226.   * @brief DMA conversion complete callback
  227.   * @note This function is executed when the transfer complete interrupt
  228.   * is generated
  229.   * @retval None
  230.   */
  231. static void TransferComplete(DMA_HandleTypeDef *DmaHandle)
  232. {
  233. transferCompleteDetected = 1;
  234. }
  235.  
  236. /**
  237.   * @brief DMA conversion error callback
  238.   * @note This function is executed when the transfer error interrupt
  239.   * is generated during DMA transfer
  240.   * @retval None
  241.   */
  242. static void TransferError(DMA_HandleTypeDef *DmaHandle)
  243. {
  244. transferErrorDetected = 1;
  245. }
  246.  
  247. /**
  248.   * @brief System Clock Configuration
  249.   * The system Clock is configured as follow :
  250.   * System Clock source = PLL (HSE)
  251.   * SYSCLK(Hz) = 216000000
  252.   * HCLK(Hz) = 216000000
  253.   * AHB Prescaler = 1
  254.   * APB1 Prescaler = 4
  255.   * APB2 Prescaler = 2
  256.   * HSE Frequency(Hz) = 8000000
  257.   * PLL_M = 8
  258.   * PLL_N = 432
  259.   * PLL_P = 2
  260.   * PLL_Q = 9
  261.   * VDD(V) = 3.3
  262.   * Main regulator output voltage = Scale1 mode
  263.   * Flash Latency(WS) = 7
  264.   * @param None
  265.   * @retval None
  266.   */
  267. void SystemClock_Config(void)
  268. {
  269. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  270. RCC_OscInitTypeDef RCC_OscInitStruct;
  271.  
  272. /* Enable HSE Oscillator and activate PLL with HSE as source */
  273. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  274. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  275. RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  276. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  277. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  278. RCC_OscInitStruct.PLL.PLLM = 8;
  279. RCC_OscInitStruct.PLL.PLLN = 432;
  280. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  281. RCC_OscInitStruct.PLL.PLLQ = 9;
  282. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  283. {
  284. while(1) {};
  285. }
  286.  
  287. /* Activate the OverDrive to reach the 216 Mhz Frequency */
  288. if(HAL_PWREx_EnableOverDrive() != HAL_OK)
  289. {
  290. while(1) {};
  291. }
  292.  
  293. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  294.   clocks dividers */
  295. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  296. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  297. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  298. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  299. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  300. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  301. {
  302. while(1) {};
  303. }
  304. }
  305.  
  306. /**
  307.   * @brief This function is executed in case of error occurrence.
  308.   * @param None
  309.   * @retval None
  310.   */
  311. static void Error_Handler(void)
  312. {
  313. while (1)
  314. {
  315. /* Toggle LED1 with a period of 1 s */
  316. BSP_LED_Toggle(LED1);
  317. HAL_Delay(1000);
  318. }
  319. }
  320.  
  321. /**
  322.   * @brief CPU L1-Cache enable.
  323.   * @param None
  324.   * @retval None
  325.   */
  326. static void CPU_CACHE_Enable(void)
  327. {
  328. /* Enable I-Cache */
  329. SCB_EnableICache();
  330.  
  331. /* Enable D-Cache */
  332. SCB_EnableDCache();
  333. }
  334.  
  335. #ifdef USE_FULL_ASSERT
  336. /**
  337.   * @brief Reports the name of the source file and the source line number
  338.   * where the assert_param error has occurred.
  339.   * @param file: pointer to the source file name
  340.   * @param line: assert_param error line source number
  341.   * @retval None
  342.   */
  343. void assert_failed(uint8_t *file, uint32_t line)
  344. {
  345. /* User can add his own implementation to report the file name and line number,
  346.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  347.  
  348. /* Infinite loop */
  349. while (1)
  350. {
  351. }
  352. }
  353. #endif
  354.  
  355. /**
  356.   * @}
  357.   */
  358.  
  359. /**
  360.   * @}
  361.   */
  362.  
  363. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  364.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:41:18: fatal error: main.h: No such file or directory
compilation terminated.
stdout
Standard output is empty