Interrupt/exception vectors in STM32, Assembler

I'm studying the Startup file for STM32, and I can't understand one thing. It is said that the interrupt and exception vectors are at the beginning of the ROM. There is an AREA directive, and it has two parameters CODE and DATA. The book says that the DATA parameter corresponds to RAM memory, and the CODE parameter corresponds to program memory - FLASH. The question is, why is the declaration of vectors made in the data memory i.e. RAM? I attach the listing. Specifically, the section - AREA RESET,DATA, READONLY-is not clear. After all, it should be CODE, not DATA. In advance thanks.

; Программый модуль стартового файла StartUp_1
; Определить переменную "Размер стека" (1 К байт)
Stack_Size      EQU     0x00000400
; Объявить секцию данных для размещения стека системы
; без инициализации памяти, с атрибутом выравнивания 
; по 8 байтам
                                AREA    STACK, NOINIT, READWRITE, ALIGN=3
; Зарезервировать область памяти под стек 
; с числом байт Stack_Size            
Stack_Mem       SPACE   Stack_Size      ; (1 К байт)
; Метка вершины стека (авто-декрементный стек)
__initial_sp

; Vector Table 
; Объявить секцию для размещения таблицы векторов 
; прерываний/исключений
; Для компановщика определяется как область памяти 
; данных RESET. Будет автоматически размещена компоновщиком 
; в начале памяти программ
                AREA    RESET, DATA, READONLY
; Объявить параметры таблицы векторов - глобальными именами
                EXPORT  __Vectors
                EXPORT  __Vectors_End
                EXPORT  __Vectors_Size
; Инициализация векторов обработчиков 
; прерываний/исключений
__Vectors       DCD     __initial_sp    ; Вершина стека - Top of Stack 
                                DCD     Reset_Handler ; Точка выхода в обработчик исключения
                                                                            ; по сбросу процессора Reset Handler
                                DCD     NMI_Handler   ; Точка входа в обработчик
                                                                            ; немаскируемого прерывания NMI
;                               ...
; Далее по аналогии могут быть объявлены и остальные 
; вектора обработчиков прерываний/исключений
__Vectors_End
__Vectors_Size  EQU     __Vectors_End - __Vectors

; Объявление кодовой секции для размещения 
; подпрограмм обработчиков прерываний/исключений
Author: Шапкин Сергей, 2020-03-24

2 answers

In STM32, it is possible to place vectors not only in flash, but also in RAM, and at the same time you can also specify an address for them. I advise you to read the answer to STM32 transfer of the interrupt vector Well, then configure everything according to the requirements of the project. For example, in bootloader, it would be logical to place the vectors in RAM or also in the flash at the address of the beginning of the program. Most importantly, point the controller to the address of the vectors in the register SCB - >VTOR corresponding to the address in which the compiler placed the vectors.

Where, by the way, is such a strange Startup file coming from? Among the files with the STM32 peripheral library, there is no suitable one?

 3
Author: Storozhev DJ, 2020-03-24 05:28:43

And what makes you think that the vector table is in RAM? After all, the determining factor is the READONLY attribute(read only), so this is the FLASH area. And the READWRITE attribute (read / write) corresponds to SRAM. And the file is not strange at all the same beginning and in the original from ST. Only in the original there are no Russian-language comments and there is an announcement of the heap heap (apparently it was omitted for simplification, I understand the training and combat file :)). And the interrupt and exception vectors are really they are located at the address 0x08000000-the top of the stack, 0x08000001-the entry point(reset_handler), and then the rest of the interrupts went.(0x08000000 FLASH start address)


Theory: A special directive - AREA-allows you to break the program into separate sections. It has the following syntax: AREA Section_Name {, type} {, attr} ..., where:

Section_name - the name of the section. type - the type of the section. For a section containing data, specify the DATA type, and for a section containing commands, specify the type CODE. attr - additional attributes. For example, the readonly or readwrite attributes specify in which memory the section should be placed, the align=0 attribute..31 specifies how the section should be aligned in memory, the noinit attribute is used to allocate areas of memory that do not need to be initialized or initialized with zeros (when using this attribute, you do not need to specify the section type, since it can only be used for data sections). And it is better to draw from: http://www.keil.com/support/man/docs/armasm/default.htm


As mentioned above, the vector table can indeed be moved to a different address(FLASH and even SRAM), but this is a completely different topic.

PS: here is the beginning of the file from ST startup_stm32f10x_hd. s(here is only the code without the initial comments):

Stack_Size      EQU     0x00000400

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp



Heap_Size       EQU     0x00000200

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit

                PRESERVE8
                THUMB                       



                AREA    RESET, DATA, READONLY
                EXPORT  __Vectors
                EXPORT  __Vectors_End
                EXPORT  __Vectors_Size

__Vectors       DCD     __initial_sp               ; Top of Stack
                DCD     Reset_Handler              ; Reset Handler
                DCD     NMI_Handler                ; NMI Handler
                DCD     HardFault_Handler          ; Hard Fault Handler
                DCD     MemManage_Handler          ; MPU Fault Handler
                DCD     BusFault_Handler           ; Bus Fault Handler
                DCD     UsageFault_Handler         ; Usage Fault Handler
 1
Author: Геннадий, 2020-05-14 19:09:01