]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_M4_AMP_STM32H745I_Discovery_IAR/CM7/main.c
66d81170c94165707fa0c3bada133ae456f2a839
[freertos] / FreeRTOS / Demo / CORTEX_M7_M4_AMP_STM32H745I_Discovery_IAR / CM7 / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * See https://www.freertos.org/STM32H7_Dual_Core_AMP_RTOS_demo.html for usage\r
30  * instructions, and the following blog post for a more detailed explanation\r
31  * https://www.freertos.org/articles/001_simple_freertos_core_to_core_communication/simple_freertos_core_to_core_communication_AMP.html\r
32  *\r
33  * Behavior\r
34  * --------\r
35  *\r
36  * This example stress tests a simple Asymmetric Multi Processing (AMP) core to\r
37  * core communication mechanism implemented using FreeRTOS message buffers:\r
38  * https://www.freertos.org/RTOS-stream-message-buffers.html  Message buffers\r
39  * are used to pass an ASCII representation of an incrementing number (so "0",\r
40  * followed by "1", followed by "2", etc.) from a single 'sending' task that\r
41  * runs on the Arm Cortex-M7 core (the M7 core) to two "receiving" tasks\r
42  * running on the Arm Cortex-M4 core (the M4 core).  There are two data message\r
43  * buffers, one for each receiving task.  To distinguish between the receiving\r
44  * tasks one is assigned the task number 0, and the other task number 1.\r
45  *\r
46  * The M7 task sits in a loop sending the ascii strings to each M4 task.  If a\r
47  * receiving task receives the next expected value in the sequence it prints its\r
48  * task number to the UART.  If a receiving task receives anything else, or its\r
49  * attempt to receive data times out, then it hits an assert() that prints an\r
50  * error message to the UART before stopping all further processing on the M4\r
51  * core.  If the example is running correctly you will see lots of "0"s (from\r
52  * the receiving task assigned task number 0) and "1"s (from the receiving task\r
53  * assigned task number 1) streaming from the UART.  The time taken to output\r
54  * characters from the UART is the only thing throttling the speed of the core\r
55  * to core communication as it causes the message buffers to become full - which\r
56  * would probably happen anyway as the M7 core is executing at twice the\r
57  * frequency of the M4 core.\r
58  *\r
59  *\r
60  * Implementation of sbSEND_COMPLETED()\r
61  * ------------------------------------\r
62  *\r
63  * sbSEND_COMPLETED is a macro called by FreeRTOS after data has been sent to a\r
64  * message buffer in case there was a task blocked on the message buffer waiting\r
65  * for data to become available - in which case the waiting task would be\r
66  * unblocked:  https://www.freertos.org/RTOS-message-buffer-example.html\r
67  * However, the default sbSEND_COMPLETED implementation assumes the sending task\r
68  * (or interrupt) and the receiving task are under the control of the same\r
69  * instance of the FreeRTOS kernel and run on the same MCU core.  In this AMP\r
70  * example the sending task and the receiving tasks are under the control of two\r
71  * different instances of the FreeRTOS kernel, and run on different MCU cores,\r
72  * so the default sbSEND_COMPLETED implementation won't work (each FreeRTOS\r
73  * kernel instance only knowns about the tasks under its control).  AMP\r
74  * scenarios therefore require the sbSEND_COMPLETED macro (and potentially the\r
75  * sbRECEIVE_COMPLETED macro, see below) to be overridden, which is done by\r
76  * simply providing your own implementation in the project's FreeRTOSConfig.h\r
77  * file.  Note this example has a FreeRTOSConfig.h file used by the application\r
78  * that runs on the M7 core and a separate FreeRTOSConfig.h file used by the\r
79  * application that runs on the M4 core.  The implementation of sbSEND_COMPLETED\r
80  * used by the M7 core simply triggers an interrupt in the M4 core.  The\r
81  * interrupt's handler (the ISR that was triggered by the M7 core but executes\r
82  * on the M4 core) must then do the job that would otherwise be done by the\r
83  * default implementation of sbSEND_COMPLETE - namely unblock a task if the task\r
84  * was waiting to receive data from the message buffer that now contains data.\r
85  * There are two data message buffers though, so first ISR must determine which\r
86  * of the two contains data.\r
87  *\r
88  * This demo only has two data message buffers, so it would be reasonable to\r
89  * have the ISR simply query both to see which contained data, but that solution\r
90  * would not scale if there are many message buffers, or if the number of\r
91  * message buffers was unknown.  Therefore, to demonstrate a more scalable\r
92  * solution, this example introduced a third message buffer - a 'control'\r
93  * message buffer as opposed to a 'data' message buffer.  After the task on the\r
94  * M7 core writes to a data message buffer it writes the handle of the message\r
95  * buffer that contains data to the control message buffer.  The ISR running on\r
96  * the M4 core then reads from the control message buffer to know which data\r
97  * message buffer contains data.\r
98  *\r
99  * The above described scenario contains many implementation decisions.\r
100  * Alternative methods of enabling the M4 core to know data message buffer\r
101  * contains data include:\r
102  *\r
103  *  1) Using a different interrupt for each data message buffer.\r
104  *  2) Passing all data from the M7 core to the M4 core through a single message\r
105  *     buffer, along with additional data that tells the ISR running on the M4\r
106  *     core which task to forward the data to.\r
107  *\r
108  *\r
109  * Implementation of sbRECEIVE_COMPLETED()\r
110  * ---------------------------------------\r
111  *\r
112  * sbRECEIVE_COMPLETED is the complement of sbSEND_COMPLETED.  It is a macro\r
113  * called by FreeRTOS after data has been read from a message buffer in case\r
114  * there was a task blocked on the message buffer waiting for space to become\r
115  * available - in which case the waiting task would be unblocked so it can\r
116  * complete its write to the buffer.\r
117  *\r
118  * In this example the M7 task writes to the message buffers faster than the M4\r
119  * tasks read from them (in part because the M7 is running faster, and in part\r
120  * because the M4 cores write to the UART), so the buffers become full, and the\r
121  * M7 task enters the Blocked state to wait for space to become available.  As\r
122  * with the sbSEND_COMPLETED macro, the default implementation of the\r
123  * sbRECEIVE_COMPLETED macro only works if the sender and receiver are under the\r
124  * control of the same instance of FreeRTOS and execute on the same core.\r
125  * Therefore, just as the application that executes on the M7 core overrides\r
126  * the default implementation of sbSEND_SOMPLETED(), the application that runs\r
127  * on the M4 core overrides the default implementation of sbRECEIVE_COMPLETED()\r
128  * to likewise generate an interrupt in the M7 core - so sbRECEIVE_COMPLETED()\r
129  * executes on the M4 core and generates an interrupt on the M7 core.  To keep\r
130  * things simple the ISR that runs on the M7 core does not use a control\r
131  * message buffer to know which data message buffer contains space, and instead\r
132  * simply sends a notification to both data message buffers.  Note however that\r
133  * this overly simplistic implementation is only acceptable because it is\r
134  * known that there is only one sending task, and that task cannot be blocked on\r
135  * both message buffers at the same time.  Also, sending the notification to the\r
136  * data message buffer updates the receiving task's direct to task notification\r
137  * state: https://www.freertos.org/RTOS-task-notifications.html which is only ok\r
138  * because it is known the task is not using its notification state for any\r
139  * other purpose.\r
140  *\r
141  */\r
142 \r
143 /* Standard includes. */\r
144 #include "stdio.h"\r
145 #include "string.h"\r
146 \r
147 /* FreeRTOS includes. */\r
148 #include "FreeRTOS.h"\r
149 #include "task.h"\r
150 #include "message_buffer.h"\r
151 #include "MessageBufferLocations.h"\r
152 \r
153 /* ST includes. */\r
154 #include "stm32h7xx_hal.h"\r
155 #include "stm32h745i_discovery.h"\r
156 \r
157 /* When the cores boot they very crudely wait for each other in a non chip\r
158 specific way by waiting for the other core to start incrementing a shared\r
159 variable within an array.  mainINDEX_TO_TEST sets the index within the array to\r
160 the variable this core tests to see if it is incrementing, and\r
161 mainINDEX_TO_INCREMENT sets the index within the array to the variable this core\r
162 increments to indicate to the other core that it is at the sync point.  Note\r
163 this is not a foolproof method and it is better to use a hardware specific\r
164 solution, such as having one core boot the other core when it was ready, or\r
165 using some kind of shared semaphore or interrupt. */\r
166 #define mainINDEX_TO_TEST               0\r
167 #define mainINDEX_TO_INCREMENT  1\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /*\r
172  * Implements the task that sends messages to the M7 core.\r
173  */\r
174 static void prvM7CoreTasks( void *pvParameters );\r
175 \r
176 /*\r
177  * configSUPPORT_STATIC_ALLOCATION is set to 1, requiring this callback to\r
178  * provide statically allocated data for use by the idle task, which is a task\r
179  * created by the scheduler when it starts.\r
180  */\r
181 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, uint32_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );\r
182 \r
183 /*\r
184  * Just waits to see a variable being incremented by the M4 core to know when\r
185  * the M4 has created the message buffers used for core to core communication.\r
186  */\r
187 static void prvWaitForOtherCoreToStart( uint32_t ulIndexToTest, uint32_t ulIndexToIncrement );\r
188 \r
189 /*\r
190  * Setup the hardware ready to run this demo.\r
191  */\r
192 static void prvSetupHardware( void );\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static TaskHandle_t xM7AMPTask = NULL;\r
197 \r
198 int main( void )\r
199 {\r
200 BaseType_t x;\r
201 \r
202         /*** See the comments at the top of this page ***/\r
203 \r
204         prvSetupHardware();\r
205 \r
206         /* Create the control and data message buffers, as described at the top of\r
207         this file.  The message buffers are statically allocated at a known location\r
208         as both cores need to know where they are.  See MessageBufferLocations.h. */\r
209         xControlMessageBuffer = xMessageBufferCreateStatic( /* The buffer size in bytes. */\r
210                                                                                                                 mbaCONTROL_MESSAGE_BUFFER_SIZE,\r
211                                                                                                                 /* Statically allocated buffer storage area. */\r
212                                                                                                                 ucControlBufferStorage,\r
213                                                                                                                 /* Message buffer handle. */\r
214                                                                                                                 &xControlMessageBufferStruct );\r
215         for( x = 0; x < mbaNUMBER_OF_CORE_2_TASKS; x++ )\r
216         {\r
217                 xDataMessageBuffers[ x ] = xMessageBufferCreateStatic( mbaTASK_MESSAGE_BUFFER_SIZE,\r
218                                                                                                                            &( ucDataBufferStorage[ x ][ 0 ] ),\r
219                                                                                                                            &( xDataMessageBufferStructs[ x ] ) );\r
220         }\r
221 \r
222         /* The message buffers have been initialised so it is safe for both cores to\r
223         synchronise their startup. */\r
224         prvWaitForOtherCoreToStart( mainINDEX_TO_TEST, mainINDEX_TO_INCREMENT );\r
225 \r
226         /* Start the task that executes on the M7 core. */\r
227         xTaskCreate( prvM7CoreTasks,                    /* Function that implements the task. */\r
228                                  "AMPM7Core",                           /* Task name, for debugging only. */\r
229                                  configMINIMAL_STACK_SIZE,  /* Size of stack (in words) to allocate for this task. */\r
230                                  NULL,                                          /* Task parameter, not used in this case. */\r
231                                  tskIDLE_PRIORITY,                      /* Task priority. */\r
232                                  &xM7AMPTask );                         /* Task handle, used to unblock task from interrupt. */\r
233 \r
234         /* Start scheduler */\r
235         vTaskStartScheduler();\r
236 \r
237         /* Will not get here if the scheduler starts successfully.  If you do end up\r
238         here then there wasn't enough heap memory available to start either the idle\r
239         task or the timer/daemon task.  https://www.freertos.org/a00111.html */\r
240         for( ;; );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static void prvM7CoreTasks( void *pvParameters )\r
245 {\r
246 BaseType_t x;\r
247 uint32_t ulNextValue = 0;\r
248 char cString[ 15 ];\r
249 size_t xStringLength;\r
250 \r
251         /* Remove warning about unused parameters. */\r
252         ( void ) pvParameters;\r
253 \r
254         for( ;; )\r
255         {\r
256                 /* Create the next string to send.  The value is incremented on each\r
257                 loop iteration, and the length of the string changes as the number of\r
258                 digits in the value increases. */\r
259                 sprintf( cString, "%lu", ( unsigned long ) ulNextValue );\r
260                 xStringLength = strlen( cString );\r
261 \r
262                 /* This task runs on the M7 core, use the message buffers to send the\r
263                 strings to the tasks running on the M4 core.  This will result in\r
264                 sbSEND_COMPLETED() being executed, which in turn will write the handle\r
265                 of the message buffer written to into xControlMessageBuffer then\r
266                 generate an interrupt in M4 core. */\r
267                 for( x = 0; x < mbaNUMBER_OF_CORE_2_TASKS; x++ )\r
268                 {\r
269                         while( xMessageBufferSend(      xDataMessageBuffers[ x ],\r
270                                                                                 ( void * ) cString,\r
271                                                                                 xStringLength,\r
272                                                                                 portMAX_DELAY ) != xStringLength );\r
273                 }\r
274 \r
275                 ulNextValue++;\r
276         }\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void vGenerateM7ToM4Interrupt( void * xUpdatedMessageBuffer )\r
281 {\r
282 MessageBufferHandle_t xUpdatedBuffer = ( MessageBufferHandle_t ) xUpdatedMessageBuffer;\r
283 \r
284         /* Called by the implementation of sbSEND_COMPLETED() in FreeRTOSConfig.h.\r
285         See the comments at the top of this file.  Write the handle of the data\r
286         message buffer to which data was written to the control message buffer. */\r
287         if( xUpdatedBuffer != xControlMessageBuffer )\r
288         {\r
289                 while( xMessageBufferSend( xControlMessageBuffer, &xUpdatedBuffer, sizeof( xUpdatedBuffer ), mbaDONT_BLOCK ) != sizeof( xUpdatedBuffer ) )\r
290                 {\r
291                         /* Nothing to do here. */\r
292                 }\r
293 \r
294                 /* Generate interrupt in the M4 core. */\r
295                 HAL_EXTI_D1_EventInputConfig( EXTI_LINE0, EXTI_MODE_IT, DISABLE );\r
296                 HAL_EXTI_D2_EventInputConfig( EXTI_LINE0, EXTI_MODE_IT, ENABLE );\r
297                 HAL_EXTI_GenerateSWInterrupt( EXTI_LINE0 );\r
298         }\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, uint32_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
303 {\r
304 /* If the buffers to be provided to the Idle task are declared inside this\r
305 function then they must be declared static - otherwise they will be allocated on\r
306 the stack and so not exists after this function exits. */\r
307 static StaticTask_t xIdleTaskTCB;\r
308 static uint32_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
309 \r
310         /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide\r
311         an implementation of vApplicationGetIdleTaskMemory() to provide the memory\r
312         that is used by the Idle task.\r
313         https://www.freertos.org/a00110.html#configSUPPORT_STATIC_ALLOCATION */\r
314 \r
315         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
316         state will be stored. */\r
317         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
318 \r
319         /* Pass out the array that will be used as the Idle task's stack. */\r
320         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
321 \r
322         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
323         Note that, as the array is necessarily of type StackType_t,\r
324         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
325         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 static void prvWaitForOtherCoreToStart( uint32_t ulIndexToTest, uint32_t ulIndexToIncrement )\r
330 {\r
331 volatile uint32_t ulInitialCount = ulStartSyncCounters[ ulIndexToTest ], x;\r
332 const uint32_t ulCrudeLoopDelay = 0xfffffUL;\r
333 \r
334         /* When the cores boot they very crudely wait for each other in a non chip\r
335         specific way by waiting for the other core to start incrementing a shared\r
336         variable within an array.  mainINDEX_TO_TEST sets the index within the array\r
337         to the variable this core tests to see if it is incrementing, and\r
338         mainINDEX_TO_INCREMENT sets the index within the array to the variable this\r
339         core increments to indicate to the other core that it is at the sync point.\r
340         Note this is not a foolproof method and it is better to use a hardware\r
341         specific solution, such as having one core boot the other core when it was\r
342         ready, or using some kind of shared semaphore or interrupt. */\r
343 \r
344         /* Wait for the other core to reach the synchronisation point. */\r
345         while( ulStartSyncCounters[ ulIndexToTest ] == ulInitialCount );\r
346         ulInitialCount = ulStartSyncCounters[ ulIndexToTest ];\r
347 \r
348         for( ;; )\r
349         {\r
350                 ulStartSyncCounters[ ulIndexToIncrement ]++;\r
351                 if( ulStartSyncCounters[ ulIndexToTest ] != ulInitialCount )\r
352                 {\r
353                         ulStartSyncCounters[ ulIndexToIncrement ]++;\r
354                         break;\r
355                 }\r
356 \r
357                 /* Unlike the M4 core, this core does not have direct access to the UART,\r
358                 so simply toggle an LED to show its status. */\r
359                 for( x = 0; x < ulCrudeLoopDelay; x++ ) __asm volatile( "NOP" );\r
360                 BSP_LED_Off( LED2 );\r
361                 for( x = 0; x < ulCrudeLoopDelay; x++ ) __asm volatile( "NOP" );\r
362                 BSP_LED_On( LED2 );\r
363         }\r
364 }\r
365 /*-----------------------------------------------------------*/\r
366 \r
367 void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )\r
368 {\r
369 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
370 uint32_t x;\r
371 \r
372         configASSERT( xM7AMPTask );\r
373 \r
374         HAL_EXTI_D1_ClearFlag( EXTI_LINE1 );\r
375 \r
376         /* Task can't be blocked on both so just send the notification to both. */\r
377         for( x = 0; x < mbaNUMBER_OF_CORE_2_TASKS; x++ )\r
378         {\r
379                 xMessageBufferReceiveCompletedFromISR( xDataMessageBuffers[ x ], &xHigherPriorityTaskWoken );\r
380         }\r
381 \r
382         /* Normal FreeRTOS "yield from interrupt" semantics, where\r
383         xHigherPriorityTaskWoken is initialzed to pdFALSE and will then get set to\r
384         pdTRUE if the interrupt unblocks a task that has a priority above that of\r
385         the currently executing task. */\r
386         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 static void prvSetupHardware( void )\r
391 {\r
392 MPU_Region_InitTypeDef MPU_InitStruct;\r
393 RCC_ClkInitTypeDef RCC_ClkInitStruct;\r
394 RCC_OscInitTypeDef RCC_OscInitStruct;\r
395 \r
396         /* Configure the MPU attributes as Not Cachable for Internal D3SRAM.  The\r
397         Base Address is 0x38000000 (D3_SRAM_BASE), and the size is 64K. */\r
398         HAL_MPU_Disable();\r
399         MPU_InitStruct.Enable = MPU_REGION_ENABLE;\r
400         MPU_InitStruct.BaseAddress = D3_SRAM_BASE;\r
401         MPU_InitStruct.Size = MPU_REGION_SIZE_64KB;\r
402         MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;\r
403         MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;\r
404         MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;\r
405         MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;\r
406         MPU_InitStruct.Number = MPU_REGION_NUMBER0;\r
407         MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;\r
408         MPU_InitStruct.SubRegionDisable = 0x00;\r
409         MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;\r
410         HAL_MPU_ConfigRegion(&MPU_InitStruct);\r
411         HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);\r
412 \r
413         /* Enable I-Cache */\r
414         SCB_EnableICache();\r
415 \r
416         /* Enable D-Cache */\r
417         SCB_EnableDCache();\r
418 \r
419         HAL_Init();\r
420         BSP_LED_Init(LED1);\r
421 \r
422 \r
423         /*\r
424         System Clock Configuration:\r
425                 System Clock source    = PLL (HSE)\r
426                 SYSCLK(Hz)             = 400000000 (Cortex-M7 CPU Clock)\r
427                 HCLK(Hz)               = 200000000 (Cortex-M4 CPU, Bus matrix Clocks)\r
428                 AHB Prescaler          = 2\r
429                 D1 APB3 Prescaler      = 2 (APB3 Clock  100MHz)\r
430                 D2 APB1 Prescaler      = 2 (APB1 Clock  100MHz)\r
431                 D2 APB2 Prescaler      = 2 (APB2 Clock  100MHz)\r
432                 D3 APB4 Prescaler      = 2 (APB4 Clock  100MHz)\r
433                 HSE Frequency(Hz)      = 25000000\r
434                 PLL_M                  = 5\r
435                 PLL_N                  = 160\r
436                 PLL_P                  = 2\r
437                 PLL_Q                  = 4\r
438                 PLL_R                  = 2\r
439                 VDD(V)                 = 3.3\r
440                 Flash Latency(WS)      = 4\r
441         */\r
442 \r
443         HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);\r
444 \r
445         /* The voltage scaling allows optimizing the power consumption when the\r
446         device is clocked below the maximum system frequency, to update the voltage\r
447         scaling value regarding system frequency refer to product datasheet. */\r
448         __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);\r
449 \r
450         while( !__HAL_PWR_GET_FLAG( PWR_FLAG_VOSRDY ) )\r
451         {\r
452                 __asm volatile ( "NOP" );\r
453         }\r
454 \r
455         /* Enable HSE Oscillator and activate PLL with HSE as source */\r
456         RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;\r
457         RCC_OscInitStruct.HSEState = RCC_HSE_ON;\r
458         RCC_OscInitStruct.HSIState = RCC_HSI_OFF;\r
459         RCC_OscInitStruct.CSIState = RCC_CSI_OFF;\r
460         RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;\r
461         RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;\r
462 \r
463         RCC_OscInitStruct.PLL.PLLM = 5;\r
464         RCC_OscInitStruct.PLL.PLLN = 160;\r
465         RCC_OscInitStruct.PLL.PLLFRACN = 0;\r
466         RCC_OscInitStruct.PLL.PLLP = 2;\r
467         RCC_OscInitStruct.PLL.PLLR = 2;\r
468         RCC_OscInitStruct.PLL.PLLQ = 4;\r
469 \r
470         RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;\r
471         RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;\r
472         configASSERT( HAL_RCC_OscConfig( &RCC_OscInitStruct ) == HAL_OK );\r
473 \r
474         /* Select PLL as system clock source and configure  bus clocks dividers */\r
475         RCC_ClkInitStruct.ClockType = ( RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \\r
476                                                                     RCC_CLOCKTYPE_PCLK2  | RCC_CLOCKTYPE_D3PCLK1 );\r
477 \r
478         RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;\r
479         RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;\r
480         RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;\r
481         RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;\r
482         RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;\r
483         RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;\r
484         RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;\r
485         configASSERT( HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_4 ) == HAL_OK );\r
486 \r
487         /* AIEC Common configuration: make CPU1 and CPU2 SWI line0 sensitive to\r
488         rising edge. */\r
489         HAL_EXTI_EdgeConfig( EXTI_LINE0, EXTI_RISING_EDGE );\r
490 \r
491         /* Interrupt used for M4 to M7 notifications. */\r
492         HAL_NVIC_SetPriority( EXTI1_IRQn, 0xFU, 0U );\r
493         HAL_NVIC_EnableIRQ( EXTI1_IRQn );\r
494 }\r
495 \r