]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Projects/IAR/Secure/main_s.c
Add Cortex M23 GCC and IAR ports. Add demo projects for Nuvoton NuMaker-PFM-2351.
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Projects / IAR / Secure / main_s.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 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 __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 static void prvSetupHardware( void )\r
60 {\r
61         /* Init System Clock. */\r
62         /* Enable PLL */\r
63         CLK->PLLCTL = CLK_PLLCTL_64MHz_HIRC;\r
64         /* Wait for PLL to be stable. */\r
65         while( ( CLK->STATUS & CLK_STATUS_PLLSTB_Msk ) == 0 );\r
66 \r
67         /* Set HCLK divider to 1. */\r
68         CLK->CLKDIV0 = ( CLK->CLKDIV0 & ( ~CLK_CLKDIV0_HCLKDIV_Msk ) );\r
69 \r
70         /* Switch HCLK clock source to PLL. */\r
71         CLK->CLKSEL0 = ( CLK->CLKSEL0 & ( ~CLK_CLKSEL0_HCLKSEL_Msk ) ) | CLK_CLKSEL0_HCLKSEL_PLL;\r
72 \r
73         /* Initialize UART0 - It is used for debug output from the non-secure side. */\r
74         /* Enable UART0 clock. */\r
75         CLK->APBCLK0 |= CLK_APBCLK0_UART0CKEN_Msk;\r
76 \r
77         /* Select UART0 clock source. */\r
78         CLK->CLKSEL1 = ( CLK->CLKSEL1 & ( ~CLK_CLKSEL1_UART0SEL_Msk ) ) | CLK_CLKSEL1_UART0SEL_HIRC;\r
79 \r
80         /* Set multi-function pins for UART0 RXD and TXD. */\r
81         SYS->GPB_MFPH = ( SYS->GPB_MFPH & ( ~UART0_RXD_PB12_Msk ) ) | UART0_RXD_PB12;\r
82         SYS->GPB_MFPH = ( SYS->GPB_MFPH & ( ~UART0_TXD_PB13_Msk ) ) | UART0_TXD_PB13;\r
83 \r
84         /* Initialize UART1 - It is used for debug output from the secure side. */\r
85         /* Enable UART1 clock. */\r
86         CLK->APBCLK0 |= CLK_APBCLK0_UART1CKEN_Msk;\r
87 \r
88         /* Select UART1 clock source. */\r
89         CLK->CLKSEL1 = ( CLK->CLKSEL1 & ( ~CLK_CLKSEL1_UART1SEL_Msk ) ) | CLK_CLKSEL1_UART1SEL_HIRC;\r
90 \r
91         /* Set multi-function pins for UART1 RXD and TXD. */\r
92         SYS->GPA_MFPL = ( SYS->GPA_MFPL & ( ~UART1_RXD_PA2_Msk ) ) | UART1_RXD_PA2;\r
93         SYS->GPA_MFPL = ( SYS->GPA_MFPL & ( ~UART1_TXD_PA3_Msk ) ) | UART1_TXD_PA3;\r
94 \r
95         /* Update System Core Clock. */\r
96         PllClock                = 64000000;                             /* PLL. */\r
97         SystemCoreClock = 64000000 / 1;                 /* HCLK. */\r
98         CyclesPerUs             = 64000000 / 1000000;   /* For SYS_SysTickDelay(). */\r
99 \r
100         /* Initialize the debug port. */\r
101         DEBUG_PORT->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);\r
102         DEBUG_PORT->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 static void prvBootNonSecure( uint32_t ulNonSecureStartAddress )\r
107 {\r
108         NonSecureResetHandler_t pxNonSecureResetHandler;\r
109 \r
110         /* Setup the non-secure vector table. */\r
111         SCB_NS->VTOR = ulNonSecureStartAddress;\r
112 \r
113         /* Main Stack Pointer value for the non-secure side is the first entry in\r
114          * the non-secure vector table. Read the first entry and assign the same to\r
115          * the non-secure main stack pointer(MSP_NS). */\r
116         secureportSET_MSP_NS( *( ( uint32_t * )( ulNonSecureStartAddress ) ) );\r
117 \r
118         /* Reset Handler for the non-secure side is the second entry in the\r
119          * non-secure vector table. Read the second entry to get the non-secure\r
120          * Reset Handler. */\r
121         pxNonSecureResetHandler = ( NonSecureResetHandler_t )( * ( ( uint32_t * ) ( ( ulNonSecureStartAddress ) + 4U ) ) );\r
122 \r
123         /* Start non-secure state software application by jumping to the non-secure\r
124          * Reset Handler. */\r
125         pxNonSecureResetHandler();\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /* Secure main. */\r
130 int main(void)\r
131 {\r
132         /* Unlock protected registers. */\r
133         SYS_UnlockReg();\r
134 \r
135         /* Initialize the hardware. */\r
136         prvSetupHardware();\r
137 \r
138         /* Print banner. */\r
139         printf( "\n" );\r
140         printf( "+---------------------------------------------+\n" );\r
141         printf( "|            Secure is running ...            |\n" );\r
142         printf( "+---------------------------------------------+\n" );\r
143 \r
144         /* Do not generate Systick interrupt on secure side. */\r
145         SysTick_Config( 1 );\r
146 \r
147         /* Set GPIO Port A to non-secure for controlling LEDs from the non-secure\r
148          * side . */\r
149         SCU_SET_IONSSET( SCU_IONSSET_PA_Msk );\r
150 \r
151         /* Set UART0 to non-secure for debug output from non-secure side. */\r
152         SCU_SET_PNSSET( UART0_Attr );\r
153 \r
154         /* Lock protected registers before booting non-secure code. */\r
155         SYS_LockReg();\r
156 \r
157         /* Boot the non-secure code. */\r
158         printf( "Entering non-secure world ...\n" );\r
159         prvBootNonSecure( mainNONSECURE_APP_START_ADDRESS );\r
160 \r
161         /* Non-secure software does not return, this code is not executed. */\r
162         for( ; ; )\r
163         {\r
164                 /* Should not reach here. */\r
165         }\r
166 }\r
167 /*-----------------------------------------------------------*/\r