main.c
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**********************************************************
*
* @file : main.c
* @author : HaewonSeo
*
* @note : Main of secure world
**********************************************************/
#include <arm_cmse.h>
#include <stdio.h>
#include <stdint.h>
#include "NuMicro.h"
#include "partition_M2351.h"
#include "nsc.h"
#include "MAX30102.h"
#include "M2351_crypto.h"
//extern int ssl_client1(void);
#define DEBUG_PORT UART0_NS
#define WIFI_PORT UART3_NS // Used to connect to WIFI module
#define NEXT_BOOT_BASE 0x10040000
#define JUMP_HERE 0xe7fee7ff /* Instruction Code of "B ." */
/* typedef for NonSecure callback functions */
typedef __NONSECURE_CALL void (*NonSecure_funcptr)(uint32_t);
typedef int32_t (*Secure_funcptr)(uint32_t);
volatile uint32_t millis_counter;
/*----------------------------------------------------------------------------
SysTick IRQ Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void)
{
static uint32_t u32Ticks;
millis_counter++;
switch(u32Ticks++)
{
case 0:
break;
case 100:
break;
case 200:
break;
case 300:
break;
case 400:
break;
case 500:
break;
case 600:
u32Ticks = 0;
break;
default:
if(u32Ticks > 600)
{
u32Ticks = 0;
}
}
}
/*---------------------------------------------------------------------------*/
/* Functions */
/*---------------------------------------------------------------------------*/
void SYS_Init(void)
{
/* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Select HCLK clock source as HIRC and HCLK source divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable HXT clock */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Enable PLL */
CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;
/* Waiting for PLL stable */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
/* Select HCLK clock source as PLL and HCLK source divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(1));
/* Select UART clock source */
//CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_UART0SEL_Msk)) | CLK_CLKSEL1_UART0SEL_HIRC;
//CLK->CLKSEL3 = (CLK->CLKSEL3 & (~CLK_CLKSEL3_UART3SEL_Msk)) | CLK_CLKSEL3_UART3SEL_HIRC;
CLK->PWRCTL |= CLK_PWRCTL_HIRC48EN_Msk;
while((CLK->STATUS & CLK_STATUS_HIRC48STB_Msk) == 0);
CLK->CLKSEL0 = CLK_CLKSEL0_HCLKSEL_HIRC48;
/* Select IP clock source */
CLK->CLKSEL1 = CLK_CLKSEL1_UART0SEL_HIRC | CLK_CLKSEL1_UART1SEL_HIRC;
CLK->CLKSEL3 = CLK_CLKSEL3_UART2SEL_HIRC | CLK_CLKSEL3_UART3SEL_HIRC | CLK_CLKSEL3_UART5SEL_HIRC;
/* Enable Crypto Accelerator */
CLK->AHBCLK |= CLK_AHBCLK_CRPTCKEN_Msk;
/* Enable IP clock */
CLK->APBCLK0 |= CLK_APBCLK0_UART0CKEN_Msk | CLK_APBCLK0_TMR0CKEN_Msk | CLK_APBCLK0_UART1CKEN_Msk |
CLK_APBCLK0_UART2CKEN_Msk | CLK_APBCLK0_UART3CKEN_Msk | CLK_APBCLK0_UART5CKEN_Msk;
/* Enable UART module clock */
//CLK_EnableModuleClock(UART0_MODULE);
/* Select UART module clock source as HXT `and UART module clock divider as 1 */
//CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
/* Select HIRC as the clock source of SPI0 */
CLK_SetModuleClock(SPI1_MODULE, CLK_CLKSEL2_SPI1SEL_PCLK0, MODULE_NoMsk);
//CLK_SetModuleClock(SDH0_MODULE, CLK_CLKSEL2_SPI0SEL_HIRC, MODULE_NoMsk);
/* Enable I2C0 peripheral clock */
CLK_EnableModuleClock(I2C0_MODULE);
/* Enable I2C1 peripheral clock */
//CLK_EnableModuleClock(I2C1_MODULE);
/* Enable SPI0 peripheral clock */
CLK_EnableModuleClock(SPI1_MODULE);
//CLK_EnableModuleClock(SDH0_MODULE);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
//SystemCoreClockUpdate();
PllClock = 128000000; // PLL
SystemCoreClock = 64000000 / 1; // HCLK
CyclesPerUs = 64000000 / 1000000; // For SYS_SysTickDelay()
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;
/* Set PA multi-function pins for I2C0 SDA and SCL */
/* PA.4(56):SDA, PA.5(55):SCL*/
SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA4MFP_Msk | SYS_GPA_MFPL_PA5MFP_Msk);
SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA4MFP_I2C0_SDA | SYS_GPA_MFPL_PA5MFP_I2C0_SCL);
/* Set PA multi-function pins for I2C1 SDA and SCL */
/* PA.2(58):SDA, PA.3(57):SCL*/
//SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
//SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA2MFP_I2C1_SDA | SYS_GPA_MFPL_PA3MFP_I2C1_SCL);
/* Setup SPI0 multi-function pins */
/* Reference the Arduino UNO compatible interface */
//SYS->GPF_MFPL &= ~(SYS_GPF_MFPL_PF6MFP_Msk | SYS_GPF_MFPL_PF7MFP_Msk | SYS_GPF_MFPH_PF8MFP_Msk | SYS_GPF_MFPH_PF9MFP_Msk);
//SYS->GPF_MFPL |= (SYS_GPF_MFPL_PF6MFP_SPI0_MOSI | SYS_GPF_MFPL_PF7MFP_SPI0_MISO | SYS_GPF_MFPH_PF8MFP_SPI0_CLK | SYS_GPF_MFPH_PF9MFP_SPI0_SS);
/* Setup SPI0 multi-function pins */
//SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD2MFP_Msk | SYS_GPD_MFPL_PD3MFP_Msk);
//SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_SPI0_MOSI | SYS_GPD_MFPL_PD2MFP_SPI0_CLK | SYS_GPD_MFPL_PD3MFP_SPI0_SS);
/* Setup SPI0 multi-function pins */
//SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA0MFP_Msk | SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
//SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA0MFP_SPI0_MOSI | SYS_GPA_MFPL_PA2MFP_SPI0_CLK | SYS_GPA_MFPL_PA3MFP_SPI0_SS);
/* Setup SPI1 multi-function pins */
SYS->GPH_MFPH &= ~(SYS_GPH_MFPH_PH8MFP_Msk | SYS_GPH_MFPH_PH9MFP_Msk);
SYS->GPH_MFPH |= (SYS_GPH_MFPH_PH8MFP_SPI1_CLK | SYS_GPH_MFPH_PH9MFP_SPI1_SS);
SYS->GPE_MFPL &= ~(SYS_GPE_MFPL_PE0MFP_Msk);
SYS->GPE_MFPL |= (SYS_GPE_MFPL_PE0MFP_SPI1_MOSI);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART0_NS :115200, 8-bit word, no parity bit, 1 stop bit. */
/*---------------------------------------------------------------------------------------------------------*/
void DEBUG_PORT_Init()
{
DEBUG_PORT->LINE = UART_PARITY_NONE | UART_STOP_BIT_1 | UART_WORD_LEN_8;
DEBUG_PORT->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART3_NS :115200, 8-bit word, no parity bit, 1 stop bit. */
/*---------------------------------------------------------------------------------------------------------*/
void WIFI_PORT_Init()
{
CLK->APBCLK0 |= CLK_APBCLK0_UART4CKEN_Msk;
CLK->CLKSEL3 = (CLK->CLKSEL3 & (~CLK_CLKSEL3_UART3SEL_Msk)) | CLK_CLKSEL3_UART3SEL_HIRC;
WIFI_PORT->LINE = UART_PARITY_NONE | UART_STOP_BIT_1 | UART_WORD_LEN_8;
WIFI_PORT->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);
/* Set multi-function pins for RXD and TXD */
//SYS->GPC_MFPL = (SYS->GPC_MFPL & (~(UART4_RXD_PC6_Msk | UART4_TXD_PC7_Msk))) | UART4_RXD_PC6 | UART4_TXD_PC7;
SYS->GPD_MFPL = (SYS->GPD_MFPL & (~(UART3_RXD_PD0_Msk | UART3_TXD_PD1_Msk))) | UART3_RXD_PD0 | UART3_TXD_PD1;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init I2C0 */
/*---------------------------------------------------------------------------------------------------------*/
void I2C0_Init(void)
{
/* Open I2C0 module and set bus clock */
I2C_Open(I2C0, 400000);
/* Get I2C0 Bus Clock */
//printf("I2C0 clock %d Hz\n", I2C_GetBusClockFreq(I2C0));
/* Set I2C0 Slave Addresses */
I2C_SetSlaveAddr(I2C0, 0, MAX30102_ADDR, 0); /* MAX30102 Slave Address : 0x57 */
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init SPI */
/*---------------------------------------------------------------------------------------------------------*/
/* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */
void SPI_Init(void)
{
/* Set IP clock divider. SPI clock rate = 2MHz */
SPI_Open(SPI1_NS, SPI_MASTER, SPI_MODE_0, 32, 2000000);
/* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
SPI_EnableAutoSS(SPI1_NS, SPI_SS, SPI_SS_ACTIVE_LOW);
SPI_SET_DATA_WIDTH(SPI1_NS, 8);
}
void Nonsecure_Init(void)
{
NonSecure_funcptr fp;
/* SCB_NS.VTOR points to the Non-secure vector table base address. */
SCB_NS->VTOR = NEXT_BOOT_BASE;
/* 1st Entry in the vector table is the Non-secure Main Stack Pointer. */
__TZ_set_MSP_NS(*((uint32_t *)SCB_NS->VTOR)); /* Set up MSP in Non-secure code */
/* 2nd entry contains the address of the Reset_Handler (CMSIS-CORE) function */
fp = ((NonSecure_funcptr)(*(((uint32_t *)SCB_NS->VTOR) + 1)));
/* Clear the LSB of the function address to indicate the function-call
will cause a state switch from Secure to Non-secure */
fp = cmse_nsfptr_create(fp);
/* Check if the Reset_Handler address is in Non-secure space */
if(cmse_is_nsfptr(fp) && (((uint32_t)fp & 0xf0000000) == 0x10000000))
{
printf("\nExecute Non-secure code ...\n");
fp(0); /* Non-secure function call */
}
else
{
/* Something went wrong */
printf("No code in non-secure region!\n");
printf("CPU will halted at non-secure state\n");
/* Set nonsecure MSP in nonsecure region */
__TZ_set_MSP_NS(NON_SECURE_SRAM_BASE + 512);
/* Try to halted in non-secure state (SRAM) */
M32(NON_SECURE_SRAM_BASE) = JUMP_HERE;
fp = (NonSecure_funcptr)(NON_SECURE_SRAM_BASE + 1);
fp(0);
while(1);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, IP clock and multi-function I/O. */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Call secure API to get system core clock */
//SystemCoreClock = GetSystemCoreClock();
/* Generate Systick interrupt each 1 ms */
SysTick_Config(SystemCoreClock / 1000);
/* Inin UART and I2C0 */
DEBUG_PORT_Init();
WIFI_PORT_Init();
I2C0_Init();
SPI_Init();
printf("+---------------------------------------------+\n");
printf("| Secure is running ... |\n");
printf("+---------------------------------------------+\n");
/* Config MAX30102 */
MAX30102_Config();
Nonsecure_Init();
do
{
//OLED_HeartRate(0, hr++);
//CLK_SysTickLongDelay(2000000);
__WFI();
}
while(1);
}