User | Revision | Line number | New contents of line |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
1
|
/* mbed Microcontroller Library
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
2
|
* Copyright (c) 2017-2017 ARM Limited
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
3
|
*
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
5
|
* you may not use this file except in compliance with the License.
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
6
|
* You may obtain a copy of the License at
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
7
|
*
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
8
|
* http://www.apache.org/licenses/LICENSE-2.0
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
9
|
*
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
10
|
* Unless required by applicable law or agreed to in writing, software
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
11
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
12
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
13
|
* See the License for the specific language governing permissions and
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
14
|
* limitations under the License.
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
15
|
*/
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
16
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
17
|
#include <stdlib.h>
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
18
|
#include <stdarg.h>
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
19
|
#include "device.h"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
20
|
#include "platform/mbed_application.h"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
21
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
22
|
#if MBED_APPLICATION_SUPPORT
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
23
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
24
|
static void powerdown_nvic(void);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
25
|
static void powerdown_scb(uint32_t vtor);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
26
|
static void start_new_application(void *sp, void *pc);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
27
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
28
|
void mbed_start_application(uintptr_t address)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
29
|
{
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
30
|
void *sp;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
31
|
void *pc;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
32
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
33
|
// Interrupts are re-enabled in start_new_application
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
34
|
__disable_irq();
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
35
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
36
|
SysTick->CTRL = 0x00000000;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
37
|
powerdown_nvic();
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
38
|
powerdown_scb(address);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
39
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
40
|
sp = *((void**)address + 0);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
41
|
pc = *((void**)address + 1);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
42
|
start_new_application(sp, pc);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
43
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
44
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
45
|
static void powerdown_nvic()
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
46
|
{
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
47
|
int isr_groups_32;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
48
|
int i;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
49
|
int j;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
50
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
51
|
isr_groups_32 = ((SCnSCB->ICTR & SCnSCB_ICTR_INTLINESNUM_Msk) >> SCnSCB_ICTR_INTLINESNUM_Pos) + 1;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
52
|
for (i = 0; i < isr_groups_32; i++) {
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
53
|
NVIC->ICER[i] = 0xFFFFFFFF;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
54
|
NVIC->ICPR[i] = 0xFFFFFFFF;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
55
|
for (j = 0; j < 8; j++) {
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
56
|
NVIC->IP[i * 8 + j] = 0x00000000;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
57
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
58
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
59
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
60
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
61
|
static void powerdown_scb(uint32_t vtor)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
62
|
{
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
63
|
int i;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
64
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
65
|
// SCB->CPUID - Read only CPU ID register
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
66
|
SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk | SCB_ICSR_PENDSTCLR_Msk;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
67
|
SCB->VTOR = vtor;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
68
|
SCB->AIRCR = 0x05FA | 0x0000;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
69
|
SCB->SCR = 0x00000000;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
70
|
// SCB->CCR - Implementation defined value
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
71
|
for (i = 0; i < 12; i++) {
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
72
|
#if defined(__CORTEX_M7)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
73
|
SCB->SHPR[i] = 0x00;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
74
|
#else
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
75
|
SCB->SHP[i] = 0x00;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
76
|
#endif
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
77
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
78
|
SCB->SHCSR = 0x00000000;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
79
|
SCB->CFSR = 0xFFFFFFFF;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
80
|
SCB->HFSR = SCB_HFSR_DEBUGEVT_Msk | SCB_HFSR_FORCED_Msk | SCB_HFSR_VECTTBL_Msk;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
81
|
SCB->DFSR = SCB_DFSR_EXTERNAL_Msk | SCB_DFSR_VCATCH_Msk |
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
82
|
SCB_DFSR_DWTTRAP_Msk | SCB_DFSR_BKPT_Msk | SCB_DFSR_HALTED_Msk;
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
83
|
// SCB->MMFAR - Implementation defined value
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
84
|
// SCB->BFAR - Implementation defined value
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
85
|
// SCB->AFSR - Implementation defined value
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
86
|
// SCB->PFR - Read only processor feature register
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
87
|
// SCB->DFR - Read only debug feature registers
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
88
|
// SCB->ADR - Read only auxiliary feature registers
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
89
|
// SCB->MMFR - Read only memory model feature registers
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
90
|
// SCB->ISAR - Read only instruction set attribute registers
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
91
|
// SCB->CPACR - Implementation defined value
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
92
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
93
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
94
|
#if defined (__CC_ARM)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
95
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
96
|
__asm static void start_new_application(void *sp, void *pc)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
97
|
{
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
98
|
MOV R2, #0
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
99
|
MSR CONTROL, R2 // Switch to main stack
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
100
|
MOV SP, R0
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
101
|
MSR PRIMASK, R2 // Enable interrupts
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
102
|
BX R1
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
103
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
104
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
105
|
#elif defined (__GNUC__) || defined (__ICCARM__)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
106
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
107
|
void start_new_application(void *sp, void *pc)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
108
|
{
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
109
|
__asm volatile (
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
110
|
"mov r2, #0 \n"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
111
|
"msr control, r2 \n" // Switch to main stack
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
112
|
"mov sp, %0 \n"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
113
|
"msr primask, r2 \n" // Enable interrupts
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
114
|
"bx %1 \n"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
115
|
:
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
116
|
: "l" (sp), "l" (pc)
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
117
|
: "r2", "cc", "memory"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
118
|
);
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
119
|
}
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
120
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
121
|
#else
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
122
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
123
|
#error "Unsupported toolchain"
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
124
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
125
|
#endif
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
126
|
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
127
|
#endif /* MBED_APPLICATION_SUPPORT */
|
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 |
0:bdf663c61a82
|
128
|
|