Linux Bare Metal Development Series 2 Based on IMX6ULLmini: Use C language and SDK to light up the LED

Import sdk header files

 sudo chown -R gec /opt

Use this command to grant permissions to gec, otherwise the access permissions are not enough to read, as shown in the figure below successfully

Purpose: To solve the difficulty of checking and setting the register address

  • devices/MCIMX6Y2/MCIMX6Y2.h

    Record peripheral registers and their related operations

  • devices/MCIMX6Y2/drivers/fsl_iomuxc.h

    Document pin muxing and its related operations

Note :

MCIMX6Y2.h comments the following header files to include

#include "core_ca7.h"  
#include "system_MCIMX6Y2.h"   

Add the following macro definitions:

#define __O  volatile
#define __IO  volatile 
#define __I  volatile const 
​
#define uint32_t unsigned int
#define uint16_t unsigned short
#define uint8_t unsigned char

Use C language

Purpose: Improve development efficiency

Introduction to the composition of the bin file

Sections are the basic building blocks of programs:

  • .text section: code text

  • .rodata segment: read-only variables, such as variables modified by const

  • .data segment: non-zero global variables, static variables

  • .bss: Global variables with a value of 0, static variables

  • .comment: store comments

  • ...

Prepare the C language environment

  • clear the bss segment

The bss segment is a storage area for global or static variables that need to be initialized when the program is running. The variables in the bss section do not allocate specific memory space at compile time, but will be cleared at runtime after the program is loaded into memory.

The operation of clearing the bss segment is called "bss segment clearing", which means to initialize the memory space of all variables in the bss segment to zero. This is usually what happens at program startup. By clearing the bss segment, you can ensure that all global and static variables are initialized to a zero value before being used. This avoids unpredictable behavior when using these variables in an uninitialized state.

The clearing of the bss segment is performed by the operating system or the runtime library. They will traverse the memory space of the bss segment when loading the program into memory, and set its content to zero. In this way, all variables located in the bss segment will have a predefined default value, which is zero value, before the program starts executing.

It should be noted that only variables located in the bss segment will be cleared, and global or static variables that have been explicitly initialized in the code will not be affected.

  • stack pointer (sp)

The stack pointer (Stack Pointer) is a pointer to the top of the stack (Stack) when the program is running. The stack is a data structure used to store function calls, local variables, and other temporary data, following the principle of last-in-first-out (LIFO).

The stack pointer (SP) moves dynamically during program execution to reflect the current location of the top of the stack. Normally, the stack pointer is moved down (decremented) to point to one entry on the stack at a time.

When a function is called, it pushes the return address, parameters, and other necessary data onto the stack. The stack pointer will move down accordingly to accommodate the new data. When the function exits, the stack pointer is moved up to free data previously pushed onto the stack.

The specific implementation of the stack pointer depends on the underlying hardware architecture and operating system. In some architectures, the stack pointer can be stored in a specific register, such as ESP (Extended Stack Pointer) in x86 architecture or SP (Stack Pointer) register in ARM architecture. In other cases, the stack pointer may be stored at a specific location in memory.

The correct use of the stack pointer is very important for the correct execution of the program and memory management. In the process of programming, we should follow the correct stack operation, ensure that the stack pointer moves correctly during the function call and return process, and avoid problems such as stack overflow .

Bare Metal Program Control Peripherals

Features: read data sheet, set register value

  • Find out which registers are associated with the peripheral

  • Find out how peripheral related registers are set

linker script import

Purpose: Specify the link address, the position of the start code in the text segment, and the position of other segments

Official source: Using LD, the GNU linker

SECTIONS{ 
​.
    =xxx //Link start address 
    . Section name 
    { 
        xxx 
        *(. Section name) 
    } 
    ... 
}

This is an example of a linker script fragment that defines the memory layout of the program and the start addresses of the segments. Below is a brief description of each segment:

  • .text: Contains the program's executable code and read-only data. .text Sections are aligned on 4-byte boundaries and contain  build/start.o object files and all other  .text sections.
  • .rodata: Contains read-only constant data. .rodata Segments are aligned to 4-byte boundaries and contain all  .rodata sections.
  • .data: Contains initialized global and static variables. .data Segments are aligned to 4-byte boundaries and contain all  .data sections.
  • .bss: Contains uninitialized global and static variables. .bss Segments are aligned to 4-byte boundaries and contain all  .bss sections and  COMMON sections.

This linker script also defines two symbols __bss_startand __bss_end, which point to .bssthe start address and end address of the segment respectively. These symbols can be used in the program to determine the size and location of the BSS segment.

It's worth noting that this is just an example snippet of a linker script, and the exact memory layout and section definitions may vary depending on the target platform and toolchain. In actual use, you need to write and adjust the linker script according to your needs and target environment.

Makefile modification

  • Compatible with .s assembly files

  • Add compiler command

  • Add sd card burning command

 There is an error

gec@ubuntu:~/bare_mental/part_3$ make burn
cp build/led.bin /home/embedfire/bare_mental/part_1/download_tool
cp: cannot create plain file '/home/embedfire/bare_mental/part_1/download_tool': No such file Or directory
Makefile:45: recipe for target 'burn' failed
make: *** [burn] Error 1

 

 Just change it to your current username here

Guess you like

Origin blog.csdn.net/qq_51519091/article/details/132347501