1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 // This module is similar to a traditional assembly startup file paired with a 17 // linker script. It turns out that everything typically done in ARMv7-M 18 // assembly startup can be done straight from C code. This makes startup code 19 // easier to maintain, modify, and read. 20 // 21 // Core initialization is comprised of two primary parts: 22 // 23 // 1. Load boot information from ARMv7-M Vector Table: The ARMv7-M vector table 24 // (See ARMv7-M Architecture Reference Manual DDI 0403E.b section B1.5) 25 // dictates the starting Program Counter (PC) and Stack Pointer (SP) when the 26 // SoC powers on. The vector table also contains a number of other vectors to 27 // handle different exceptions. This module does not provide a vector table, 28 // but it does account for it in the linker script. 29 // 30 // 2. Initialize static memory: When execution begins due to SoC power-on (or 31 // the device is reset), static memory regions must be initialized to ensure 32 // they contains the expected values when code begins to run. The SoC doesn't 33 // inherently have a notion of how to do this, so before ANYTHING else the 34 // memory must be initialized. This is done at the beginning of 35 // pw_boot_Entry(). 36 // 37 // 38 // The simple flow is as follows: 39 // Power on -> PC and SP set (from vector_table by SoC) -> pw_boot_Entry() 40 // 41 // In pw_boot_Entry(): 42 // Initialize memory -> pw_PreMainInit() -> main() 43 44 #include <stdint.h> 45 46 #include "pw_preprocessor/compiler.h" 47 #include "pw_preprocessor/util.h" 48 49 PW_EXTERN_C_START 50 51 // The following extern symbols are provided by the linker script, and their 52 // values are accessible via the reference of the symbol. 53 // 54 // Example: 55 // if (stack_pointer < &pw_boot_stack_low_addr) { 56 // PW_LOG_ERROR("Main stack overflowed!") 57 // } 58 59 // pw_boot_stack_[low/high]_addr indicate the range of the main stack. Note that 60 // this might not be the only stack in the system. 61 // 62 // The main stack pointer (sp_main) should be initialized to 63 // pw_boot_stack_high_addr. This can be done by inserting the address into index 64 // 0 of the ARMv7-M vector table. (See ARMv7-M Architecture Reference Manual DDI 65 // 0403E.b section B1.5.3) 66 extern uint8_t pw_boot_stack_low_addr; 67 extern uint8_t pw_boot_stack_high_addr; 68 69 // pw_boot_heap_[low/high]_addr indicate the address range reserved for the 70 // heap. 71 extern uint8_t pw_boot_heap_low_addr; 72 extern uint8_t pw_boot_heap_high_addr; 73 74 // The address that denotes the beginning of the .vector_table section. This 75 // can be used to set VTOR (vector table offset register) by the bootloader. 76 extern uint8_t pw_boot_vector_table_addr; 77 78 PW_EXTERN_C_END 79