inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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