cpu.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2014-2015 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
29 #include "irq.h"
30 #include "sched.h"
31 #include "thread.h"
32 #include "cpu_conf.h" /* IWYU pragma: export */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
45 #define STACK_CANARY_WORD (0xE7FEE7FEu)
46 
53 #define PROVIDES_PM_SET_LOWEST
54 
61 #define CORTEXM_SCB_CPACR_FPU_ACCESS_FULL (0x00f00000)
62 
77 void cortexm_init(void);
78 
88 static inline void cortexm_init_fpu(void)
89 {
90  /* initialize the FPU on Cortex-M4F CPUs */
91 #if (defined(CPU_CORE_CORTEX_M33) || defined(CPU_CORE_CORTEX_M4F) || defined(CPU_CORE_CORTEX_M7)) && defined(MODULE_CORTEXM_FPU)
92  /* give full access to the FPU */
93  SCB->CPACR |= (uint32_t)CORTEXM_SCB_CPACR_FPU_ACCESS_FULL;
94 #endif
95 }
96 
97 #if defined(CPU_CORTEXM_INIT_SUBFUNCTIONS) || defined(DOXYGEN)
98 
109 
119 void cortexm_init_misc(void);
120 
121 #endif /* defined(CPU_CORTEXM_INIT_SUBFUNCTIONS) || defined(DOXYGEN) */
122 
128 static inline uintptr_t cpu_get_caller_pc(void)
129 {
130  uintptr_t lr_ptr;
131  __asm__ __volatile__("mov %0, lr" : "=r"(lr_ptr));
132  return lr_ptr;
133 }
134 
141 static inline void cortexm_sleep_until_event(void)
142 {
143  __WFE();
144 }
145 
151 static inline void cortexm_sleep(int deep)
152 {
153  if (deep) {
154  SCB->SCR |= (SCB_SCR_SLEEPDEEP_Msk);
155  }
156  else {
157  SCB->SCR &= ~(SCB_SCR_SLEEPDEEP_Msk);
158  }
159 
160  /* ensure that all memory accesses have completed and trigger sleeping */
161  unsigned state = irq_disable();
162  __DSB();
163  __WFI();
164  /* Some CPUs require an ISB after WFI to work around silicon bugs */
165 #if CORTEXM_ISB_REQUIRED_AFTER_WFI
166  __ISB();
167 #endif
168  irq_restore(state);
169 }
170 
176 static inline void cortexm_isr_end(void)
177 {
180  }
181 }
182 
190 static inline void cpu_jump_to_image(uint32_t image_address)
191 {
192  /* On Cortex-M platforms, the flash begins with:
193  *
194  * 1. 4 byte pointer to stack to be used at startup
195  * 2. 4 byte pointer to the reset vector function
196  *
197  * On powerup, the CPU sets the stack pointer and starts executing the
198  * reset vector.
199  *
200  * We're doing the same here, but we'd like to start at image_address.
201  *
202  * This function must be called while executing from MSP (Master Stack
203  * Pointer).
204  */
205 
206  /* set MSP */
207  __set_MSP(*(uint32_t*)image_address);
208 
209  /* skip stack pointer */
210  image_address += 4;
211 
212  /* load the images reset_vector address */
213  uint32_t destination_address = *(uint32_t*)image_address;
214 
215  /* Make sure the Thumb State bit is set. */
216  destination_address |= 0x1;
217 
218  /* Branch execution */
219  __asm("BX %0" :: "r" (destination_address));
220 }
221 
222 /* The following register is only present for
223  Cortex-M0+, -M23, -M3, -M33, -M4 and M7 CPUs */
224 #if defined(CPU_CORE_CORTEX_M0PLUS) || defined(CPU_CORE_CORTEX_M23) || \
225  defined(CPU_CORE_CORTEX_M3) || defined(CPU_CORE_CORTEX_M33) || \
226  defined(CPU_CORE_CORTEX_M4) || defined(CPU_CORE_CORTEX_M4F) || \
227  defined(CPU_CORE_CORTEX_M7)
228 static inline uint32_t cpu_get_image_baseaddr(void)
229 {
230  return SCB->VTOR;
231 }
232 #endif
233 
243 bool cpu_check_address(volatile const char *address);
244 
245 #ifdef __cplusplus
246 }
247 #endif
248 
static uint32_t cpu_get_image_baseaddr(void)
Returns the address of running application in flash.
Definition: cpu.h:29
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
volatile unsigned int sched_context_switch_request
Flag indicating whether a context switch is necessary after handling an interrupt.
THREAD_MAYBE_INLINE void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
static uintptr_t cpu_get_caller_pc(void)
Returns the current content of the link register (lr)
Definition: cpu.h:128
void cortexm_init_misc(void)
Initialize Cortex-M misc functions.
static void cortexm_sleep(int deep)
Put the CPU into (deep) sleep mode, using the WFI instruction.
Definition: cpu.h:151
static void cortexm_isr_end(void)
Trigger a conditional context scheduler run / context switch.
Definition: cpu.h:176
static void cpu_jump_to_image(uint32_t image_address)
Jumps to another image in flash.
Definition: cpu.h:190
void cortexm_init(void)
Initialize Cortex-M specific core parts of the CPU.
void cortexm_init_isr_priorities(void)
Initialize Cortex-M interrupt priorities.
#define CORTEXM_SCB_CPACR_FPU_ACCESS_FULL
Pattern to write into the co-processor Access Control Register to allow full FPU access.
Definition: cpu.h:61
static void cortexm_sleep_until_event(void)
Put the CPU into the 'wait for event' sleep mode.
Definition: cpu.h:141
bool cpu_check_address(volatile const char *address)
Checks is memory address valid or not.
static void cortexm_init_fpu(void)
Initialize Cortex-M FPU.
Definition: cpu.h:88
IRQ driver interface.
Scheduler API definition.