]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Projects/Keil/Secure/main_s.c
7c2bca58f293482fcc35f714691e6ffcb5b64cad
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Projects / Keil / Secure / main_s.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 /* Standard includes. */\r
29 #include <arm_cmse.h>\r
30 #include <stdio.h>\r
31 \r
32 /* Device includes. */\r
33 #include "NuMicro.h"\r
34 #include "partition_M2351.h"\r
35 \r
36 /* FreeRTOS includes. */\r
37 #include "secure_port_macros.h"\r
38 \r
39 /* Start address of non-secure application. */\r
40 #define mainNONSECURE_APP_START_ADDRESS         ( 0x10040000 )\r
41 \r
42 /* typedef for non-secure Reset Handler. */\r
43 typedef __attribute__( ( cmse_nonsecure_call ) ) void ( *NonSecureResetHandler_t )( void );\r
44 /*-----------------------------------------------------------*/\r
45 \r
46 /**\r
47  * @brief Sets up the hardware - clocks and UARTs.\r
48  */\r
49 static void prvSetupHardware( void );\r
50 \r
51 /**\r
52  * @brief Boots into the non-secure code.\r
53  *\r
54  * @param[in] ulNonSecureStartAddress Start address of the non-secure application.\r
55  */\r
56 static void prvBootNonSecure( uint32_t ulNonSecureStartAddress );\r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /* For instructions on how to build and run this demo, visit the following link:\r
60  * https://www.freertos.org/RTOS-Cortex-M23-NuMaker-PFM-M2351-Keil.html\r
61  */\r
62 \r
63 /* Secure main. */\r
64 int main(void)\r
65 {\r
66         /* Unlock protected registers. */\r
67         SYS_UnlockReg();\r
68 \r
69         /* Initialize the hardware. */\r
70         prvSetupHardware();\r
71 \r
72         /* Print banner. */\r
73         printf( "\n" );\r
74         printf( "+---------------------------------------------+\n" );\r
75         printf( "|            Secure is running ...            |\n" );\r
76         printf( "+---------------------------------------------+\n" );\r
77 \r
78         /* Do not generate Systick interrupt on secure side. */\r
79         SysTick_Config( 1 );\r
80 \r
81         /* Set GPIO Port A to non-secure for controlling LEDs from the non-secure\r
82          * side . */\r
83         SCU_SET_IONSSET( SCU_IONSSET_PA_Msk );\r
84 \r
85         /* Set UART0 to non-secure for debug output from non-secure side. */\r
86         SCU_SET_PNSSET( UART0_Attr );\r
87 \r
88         /* Lock protected registers before booting non-secure code. */\r
89         SYS_LockReg();\r
90 \r
91         /* Boot the non-secure code. */\r
92         printf( "Entering non-secure world ...\n" );\r
93         prvBootNonSecure( mainNONSECURE_APP_START_ADDRESS );\r
94 \r
95         /* Non-secure software does not return, this code is not executed. */\r
96         for( ; ; )\r
97         {\r
98                 /* Should not reach here. */\r
99         }\r
100 }\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 static void prvSetupHardware( void )\r
104 {\r
105         /* Init System Clock. */\r
106         /* Enable PLL */\r
107         CLK->PLLCTL = CLK_PLLCTL_64MHz_HIRC;\r
108         /* Wait for PLL to be stable. */\r
109         while( ( CLK->STATUS & CLK_STATUS_PLLSTB_Msk ) == 0 );\r
110 \r
111         /* Set HCLK divider to 1. */\r
112         CLK->CLKDIV0 = ( CLK->CLKDIV0 & ( ~CLK_CLKDIV0_HCLKDIV_Msk ) );\r
113 \r
114         /* Switch HCLK clock source to PLL. */\r
115         CLK->CLKSEL0 = ( CLK->CLKSEL0 & ( ~CLK_CLKSEL0_HCLKSEL_Msk ) ) | CLK_CLKSEL0_HCLKSEL_PLL;\r
116 \r
117         /* Initialize UART0 - It is used for debug output from the non-secure side. */\r
118         /* Enable UART0 clock. */\r
119         CLK->APBCLK0 |= CLK_APBCLK0_UART0CKEN_Msk;\r
120 \r
121         /* Select UART0 clock source. */\r
122         CLK->CLKSEL1 = ( CLK->CLKSEL1 & ( ~CLK_CLKSEL1_UART0SEL_Msk ) ) | CLK_CLKSEL1_UART0SEL_HIRC;\r
123 \r
124         /* Set multi-function pins for UART0 RXD and TXD. */\r
125         SYS->GPB_MFPH = ( SYS->GPB_MFPH & ( ~UART0_RXD_PB12_Msk ) ) | UART0_RXD_PB12;\r
126         SYS->GPB_MFPH = ( SYS->GPB_MFPH & ( ~UART0_TXD_PB13_Msk ) ) | UART0_TXD_PB13;\r
127 \r
128         /* Initialize UART1 - It is used for debug output from the secure side. */\r
129         /* Enable UART1 clock. */\r
130         CLK->APBCLK0 |= CLK_APBCLK0_UART1CKEN_Msk;\r
131 \r
132         /* Select UART1 clock source. */\r
133         CLK->CLKSEL1 = ( CLK->CLKSEL1 & ( ~CLK_CLKSEL1_UART1SEL_Msk ) ) | CLK_CLKSEL1_UART1SEL_HIRC;\r
134 \r
135         /* Set multi-function pins for UART1 RXD and TXD. */\r
136         SYS->GPA_MFPL = ( SYS->GPA_MFPL & ( ~UART1_RXD_PA2_Msk ) ) | UART1_RXD_PA2;\r
137         SYS->GPA_MFPL = ( SYS->GPA_MFPL & ( ~UART1_TXD_PA3_Msk ) ) | UART1_TXD_PA3;\r
138 \r
139         /* Update System Core Clock. */\r
140         PllClock                = 64000000;                             /* PLL. */\r
141         SystemCoreClock = 64000000 / 1;                 /* HCLK. */\r
142         CyclesPerUs             = 64000000 / 1000000;   /* For SYS_SysTickDelay(). */\r
143 \r
144         /* Initialize the debug port. */\r
145         DEBUG_PORT->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);\r
146         DEBUG_PORT->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 static void prvBootNonSecure( uint32_t ulNonSecureStartAddress )\r
151 {\r
152         NonSecureResetHandler_t pxNonSecureResetHandler;\r
153 \r
154         /* Setup the non-secure vector table. */\r
155         SCB_NS->VTOR = ulNonSecureStartAddress;\r
156 \r
157         /* Main Stack Pointer value for the non-secure side is the first entry in\r
158          * the non-secure vector table. Read the first entry and assign the same to\r
159          * the non-secure main stack pointer(MSP_NS). */\r
160         secureportSET_MSP_NS( *( ( uint32_t * )( ulNonSecureStartAddress ) ) );\r
161 \r
162         /* Reset Handler for the non-secure side is the second entry in the\r
163          * non-secure vector table. Read the second entry to get the non-secure\r
164          * Reset Handler. */\r
165         pxNonSecureResetHandler = ( NonSecureResetHandler_t )( * ( ( uint32_t * ) ( ( ulNonSecureStartAddress ) + 4U ) ) );\r
166 \r
167         /* Start non-secure state software application by jumping to the non-secure\r
168          * Reset Handler. */\r
169         pxNonSecureResetHandler();\r
170 }\r
171 /*-----------------------------------------------------------*/\r