Loading...
Searching...
No Matches
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
25
26#include "irq.h"
27#include "sched.h"
28#include "thread.h"
29#include "cpu_conf.h" /* IWYU pragma: export */
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
42#define STACK_CANARY_WORD (0xE7FEE7FEu)
43
50#define PROVIDES_PM_SET_LOWEST
51
58#define CORTEXM_SCB_CPACR_FPU_ACCESS_FULL (0x00f00000)
59
74void cortexm_init(void);
75
85static inline void cortexm_init_fpu(void)
86{
87 /* initialize the FPU on Cortex-M4F CPUs */
88#if (defined(CPU_CORE_CORTEX_M33) || defined(CPU_CORE_CORTEX_M4F) || defined(CPU_CORE_CORTEX_M7)) && defined(MODULE_CORTEXM_FPU)
89 /* give full access to the FPU */
90 SCB->CPACR |= (uint32_t)CORTEXM_SCB_CPACR_FPU_ACCESS_FULL;
91#endif
92}
93
94#if defined(CPU_CORTEXM_INIT_SUBFUNCTIONS) || defined(DOXYGEN)
95
106
117
118#endif /* defined(CPU_CORTEXM_INIT_SUBFUNCTIONS) || defined(DOXYGEN) */
119
125static inline uintptr_t cpu_get_caller_pc(void)
126{
127 uintptr_t lr_ptr;
128 __asm__ __volatile__("mov %0, lr" : "=r"(lr_ptr));
129 return lr_ptr;
130}
131
138static inline void cortexm_sleep_until_event(void)
139{
140 __WFE();
141}
142
148static inline void cortexm_sleep(int deep)
149{
150 if (deep) {
151 SCB->SCR |= (SCB_SCR_SLEEPDEEP_Msk);
152 }
153 else {
154 SCB->SCR &= ~(SCB_SCR_SLEEPDEEP_Msk);
155 }
156
157 /* ensure that all memory accesses have completed and trigger sleeping */
158 unsigned state = irq_disable();
159 __DSB();
160 __WFI();
161 /* Some CPUs require an ISB after WFI to work around silicon bugs */
162#if CORTEXM_ISB_REQUIRED_AFTER_WFI
163 __ISB();
164#endif
165 irq_restore(state);
166}
167
173static inline void cortexm_isr_end(void)
174{
177 }
178}
179
187static inline void cpu_jump_to_image(uint32_t image_address)
188{
189 /* On Cortex-M platforms, the flash begins with:
190 *
191 * 1. 4 byte pointer to stack to be used at startup
192 * 2. 4 byte pointer to the reset vector function
193 *
194 * On powerup, the CPU sets the stack pointer and starts executing the
195 * reset vector.
196 *
197 * We're doing the same here, but we'd like to start at image_address.
198 *
199 * This function must be called while executing from MSP (Master Stack
200 * Pointer).
201 */
202
203 /* set MSP */
204 __set_MSP(*(uint32_t*)image_address);
205
206 /* skip stack pointer */
207 image_address += 4;
208
209 /* load the images reset_vector address */
210 uint32_t destination_address = *(uint32_t*)image_address;
211
212 /* Make sure the Thumb State bit is set. */
213 destination_address |= 0x1;
214
215 /* Branch execution */
216 __asm("BX %0" :: "r" (destination_address));
217}
218
219/* The following register is only present for
220 Cortex-M0+, -M23, -M3, -M33, -M4 and M7 CPUs */
221#if defined(CPU_CORE_CORTEX_M0PLUS) || defined(CPU_CORE_CORTEX_M23) || \
222 defined(CPU_CORE_CORTEX_M3) || defined(CPU_CORE_CORTEX_M33) || \
223 defined(CPU_CORE_CORTEX_M4) || defined(CPU_CORE_CORTEX_M4F) || \
224 defined(CPU_CORE_CORTEX_M7)
225static inline uint32_t cpu_get_image_baseaddr(void)
226{
227 return SCB->VTOR;
228}
229#endif
230
240bool cpu_check_address(volatile const char *address);
241
242#ifdef __cplusplus
243}
244#endif
245
static uinttxtptr_t cpu_get_caller_pc(void)
Get the last instruction's address.
Definition cpu.h:137
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:148
static void cortexm_isr_end(void)
Trigger a conditional context scheduler run / context switch.
Definition cpu.h:173
static void cpu_jump_to_image(uint32_t image_address)
Jumps to another image in flash.
Definition cpu.h:187
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:58
static void cortexm_sleep_until_event(void)
Put the CPU into the 'wait for event' sleep mode.
Definition cpu.h:138
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:85
static uint32_t cpu_get_image_baseaddr(void)
Returns the address of running application in flash.
Definition cpu.h:26
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.
IRQ driver interface.