|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 STMicroelectronics |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file |
| 9 | + * @brief System control block context helpers for Cortex-M CPUs |
| 10 | + * |
| 11 | + * System control block context helpers for backup and restore |
| 12 | + */ |
| 13 | + |
| 14 | +#ifndef ARM_CORTEX_M_SCB_H_ |
| 15 | +#define ARM_CORTEX_M_SCB_H_ |
| 16 | + |
| 17 | +#include <stdint.h> |
| 18 | +#include <cmsis_core.h> |
| 19 | + |
| 20 | +/* Define macro for SHPR size */ |
| 21 | +#if defined(CONFIG_CPU_CORTEX_M0) || \ |
| 22 | + defined(CONFIG_CPU_CORTEX_M0PLUS) || \ |
| 23 | + defined(CONFIG_CPU_CORTEX_M1) || \ |
| 24 | + defined(CONFIG_CPU_CORTEX_M23) |
| 25 | +#define SHPR_SIZE_W 2 |
| 26 | +#else |
| 27 | +#define SHPR_SIZE_W 3 |
| 28 | +#endif |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief Structure to store essential, mutable SCB register values for backup/restore. |
| 32 | + * |
| 33 | + * This structure explicitly lists the SCB registers that are safe and meaningful |
| 34 | + * to backup and restore for common system state management. It avoids volatile, |
| 35 | + * read-only, or write-only status bits that should not be directly restored. |
| 36 | + */ |
| 37 | +typedef struct { |
| 38 | +#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) |
| 39 | + uint32_t vtor; /*!< offset 0x08, Vector Table Offset Register */ |
| 40 | +#endif |
| 41 | + uint32_t aircr; /*!< offset 0x0C, Application Interrupt and Reset Control Register |
| 42 | + * (only modifiable bits) |
| 43 | + */ |
| 44 | + uint32_t scr; /*!< offset 0x10, System Control Register */ |
| 45 | + uint32_t ccr; /*!< offset 0x14, Configuration Control Register */ |
| 46 | + uint32_t shpr[SHPR_SIZE_W]; /*!< offset 0x18 or 0x1C, System Handler Priority Registers */ |
| 47 | + uint32_t shcsr; /*/< offset 0x24, System Handler Control and State Register */ |
| 48 | +} scb_context_t; |
| 49 | + |
| 50 | +/** |
| 51 | + * @name SCB Register Backup/Restore Functions |
| 52 | + * @brief Functions for saving and restoring mutable SCB register state. |
| 53 | + * @{ |
| 54 | + */ |
| 55 | + |
| 56 | +/** |
| 57 | + * @brief Save essential SCB registers into a provided context structure. |
| 58 | + * |
| 59 | + * This function reads the current values of critical System Control Block (SCB) |
| 60 | + * registers that are safe to backup and stores them into the `context` structure. |
| 61 | + * |
| 62 | + * @param context Pointer to an `scb_context_t` structure where the register |
| 63 | + * values will be stored. Must not be NULL. |
| 64 | + */ |
| 65 | +void z_arm_save_scb_context(scb_context_t *context); |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief Restores essential SCB registers from a provided context structure. |
| 69 | + * |
| 70 | + * This function writes the values from the `context` structure back to the |
| 71 | + * respective System Control Block (SCB) registers. |
| 72 | + * |
| 73 | + * @warning Extreme caution is advised when restoring SCB registers. Only |
| 74 | + * mutable registers are restored. Specifically, the ICSR register |
| 75 | + * is NOT restored directly due to its volatile nature and read-only/ |
| 76 | + * write-only bits. |
| 77 | + * |
| 78 | + * @param context Pointer to a `scb_context_t` structure containing the |
| 79 | + * register values to be restored. Must not be NULL. |
| 80 | + */ |
| 81 | +void z_arm_restore_scb_context(const scb_context_t *context); |
| 82 | + |
| 83 | +/** @} */ |
| 84 | + |
| 85 | +#endif /* ARM_CORTEX_M_SCB_H_ */ |
0 commit comments