blob: c9cf53d76218f6323e889ba08448736a61a8f812 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "cmsis.h"
18
19extern void *__Vectors; /* see irqs.c */
20
21/*----------------------------------------------------------------------------*\
22 * Define clocks (uses OSC1 ACLK) *
23\*----------------------------------------------------------------------------*/
24#define __XTAL (25000000) /* Oscillator frequency */
25#define __SYSTEM_CLOCK (__XTAL)
26
27#define STR(x) #x
28#define RESET_REG(n) __ASM volatile("MOV " STR(r##n) ", #0" : : : STR(r##n))
29
30#if defined(CPU_CORTEX_M55)
31#define CCR_DL (1 << 19)
32#else
33#error "Invalid CPU; This file only services Cortex-M55 CPUs"
34#endif /* (CPU_CORTEX_M55) */
35
36/*----------------------------------------------------------------------------
37 System Core Clock Variable (Core Clock)
38 *----------------------------------------------------------------------------*/
39uint32_t SystemCoreClock = __SYSTEM_CLOCK;
40
41
42/*----------------------------------------------------------------------------
43 Clock functions
44 *----------------------------------------------------------------------------*/
45/**
46 * @brief Updates the SystemCoreClock variable with current core Clock
47 * retrieved from cpu registers.
48 */
49void SystemCoreClockUpdate(void)
50{
51 /* Update the SystemCoreClock variable */
52 SystemCoreClock = __SYSTEM_CLOCK;
53}
54
55uint32_t GetSystemCoreClock(void)
56{
57 return SystemCoreClock;
58}
59
60/**
61 * @brief Setup the microcontroller system.
62 * Initialize the System.
63 **/
64void SystemInit(void)
65{
66#if (defined (__FPU_USED) && (__FPU_USED == 1U)) || \
67 (defined (__MVE_USED) && (__MVE_USED == 1U))
68 SCB->CPACR |= ((3U << 10U*2U) | /* enable CP10 Full Access */
69 (3U << 11U*2U) );
70#endif
71
72 /* Initialise registers r0-r12 and LR(=r14)
73 * They must have a valid value before being potentially pushed to stack by
74 * C calling convention or by context saving in exception handling
75 */
76 RESET_REG(0);
77 RESET_REG(1);
78 RESET_REG(2);
79 RESET_REG(3);
80 RESET_REG(4);
81 RESET_REG(5);
82 RESET_REG(6);
83 RESET_REG(7);
84 RESET_REG(8);
85 RESET_REG(9);
86 RESET_REG(10);
87 RESET_REG(11);
88 RESET_REG(12);
89 RESET_REG(14);
90
91#if defined (__VTOR_PRESENT) && (__VTOR_PRESENT == 1U)
92 SCB->VTOR = (uint32_t) &__Vectors;
93#endif
94
95 /* Enable hard, bus, mem and usage fault detection in SHCSR, bits 16-18.
96 * Enable stkof, bf, div_0_trp, unalign_trp and usersetm bits in CCR.
97 */
98 SCB->SHCSR = (
99 _VAL2FLD(SCB_SHCSR_USGFAULTENA, 1) |
100 _VAL2FLD(SCB_SHCSR_BUSFAULTENA, 1) |
101 _VAL2FLD(SCB_SHCSR_MEMFAULTENA, 1));
102
103 SCB->CCR = (_VAL2FLD(SCB_CCR_USERSETMPEND, 1) |
104 _VAL2FLD(SCB_CCR_DIV_0_TRP, 1) |
105 _VAL2FLD(SCB_CCR_BFHFNMIGN, 1) |
106 _VAL2FLD(SCB_CCR_STKOFHFNMIGN, 1));
107#ifdef UNALIGNED_SUPPORT_DISABLE
108 SCB->CCR |= _VAL2FLD(SCB_CCR_UNALIGN_TRP, 1);
109#endif
110
111 SCB->CCR |= CCR_DL;
112
113 /* Reset pipeline. */
114 __DSB();
115 __ISB();
116
117#ifdef UNALIGNED_SUPPORT_DISABLE
118 SCB->CCR |= SCB_CCR_UNALIGN_TRP_Msk;
119#endif
120
121 SystemCoreClock = __SYSTEM_CLOCK;
122}