Pi Zi Heng Embedded: ARM Cortex-M Files (1) - Source Files (.c/.h/.s)

Publisher:skyhcgLatest update time:2019-12-20 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  As we all know, embedded development is a low-level development, and the main programming languages ​​are C and assembly. So the source files to be discussed in this article mainly refer to C files and assembly files.


  Although in normal development, we only pay attention to the .c/.h/.s source files we created, but in fact we are dealing with many source files that are not created by us without realizing it. So the question is, what source files will a complete embedded project (taking a project based on ARM Cortex-M controller as an example) contain?


  Now it's time for Pi Ziheng to show off. Pi Ziheng divides these files into five categories and ten types according to their sources. Below, Pi Ziheng analyzes these files one by one by category:


Category 1: Provided by Committee

  The first type of files is provided by the C standards committee, and this type of files grows with the release of the standard. This type of files is mainly one type, namely the C standard library.


1. C standard library

  Everyone knows that C language has standards. Common C standards include ANSI C (C89), C99, and C11. The C Standard Library is a collection of all header files that conform to the C standard, as well as commonly used library implementation programs. The C Standard Library is developed and released by the Committee and is usually included in the IDE. Here are some common files and functions. Do you feel familiar?


/* Commonly used files */ assert.h, stdio.h, stddef.h, stdint.h, string.h ...

/* Common definitions */ bool, NULL, uint8_t, uint16_t, uint32_t...

/* Common functions */ assert(), printf(), memset(), memcpy()...


Category 2: Provided by IDE (Compiler)

  The second type of files is provided by the IDE. C is a compiled language, which requires a compiler to assemble the C program into machine code, so there are some function libraries related to the compiler characteristics.


2. Compiler Library

  When developing embedded applications, we need to use an integrated development environment (IDE). Common IDEs include GCC (GNUC), Keil MDK (ARMCC), and IAR EWARM (ICCARM). These IDEs have matching C compilers. These compilers have their own characteristics. In order to fully demonstrate the characteristics of each compiler, matching function libraries came into being.


  The compiler library varies depending on the IDE. Here we only give an example for reference. For more information, please refer to the manual of each IDE.


  Take the DLib_Product_string.h file in IAR EWARM as an example, the implementation of memcpy is redefined in this file:


#define _DLIB_STRING_SKIP_INLINE_MEMCPY

#pragma inline=forced_no_body

__EFF_NENR1NW2R1 __ATTRIBUTES void * memcpy(void * _D, const void * _S, size_t _N)

{

  __aeabi_memcpy(_D, _S, _N);

  return _D;

}


Category 3: Provided by ARM

  The third type of file is provided by ARM. The execution of embedded programs relies on the controller kernel (the kernel here refers to the ARM kernel). When designing the kernel, ARM provides some kernel module interfaces. Developers can access kernel resources through these interfaces. The CMSIS header contains the interfaces of these kernel module resources.


3. CMSIS header

  The complete CMSIS header directory should look like this, and the only file that must be paid attention to is the core_cmx.h file under CMSISInclude.


CMSIS

     Core      

     DAP /* ARM debugger implementation */

     Driver /* ARM unified common peripheral driver API */

     DSP_Lib /* ARM optimized DSP Lib */

     Include /* ARM kernel resource interface */

            arm_xx.h

            cmsis_xx.h

            core_cmx.h

     Lib /* ARM optimized standard Lib */

     Pack

     RTOS /* RTOS-RTX launched by ARM */

     RTOS2

     SVD

     Utilities

  The core_cmx.h file defines the kernel resource interface. The three most commonly used modules are SCB, SysTick, and NVIC. An experienced embedded developer should wave to Pi Ziheng when seeing these modules. Come on, let Pi Ziheng see your hands~~~


Category 4: Provided by Chip Producer

  The fourth type of files is provided by ARM chip manufacturers. When we select an ARM chip, in addition to the ARM core type, we also have to look at the chip's internal peripheral resources. These peripherals lead to differences in ARM chips, so there are major ARM manufacturers competing with each other, such as NXP (Freescale), ST, Microchip (Atmel). ARM manufacturers give ARM chips various peripheral resources and also provide interfaces for these peripheral resources. There are four types of files under this category:


4. device.h: chip header file, mainly including interrupt number definition (xx_IRQn), peripheral module type definition (xx_Type), and peripheral base address definition (xx_BASE).


/////////////////////////////////////////////////////

//Interrupt number definition

typedef enum IRQn {

  NotAvail_IRQn = -128,

  /* Core interrupts */

  NonMaskableInt_IRQn = -14,

  HardFault_IRQn = -13,

  ...

  SysTick_IRQn = -1,

  /* Device specific interrupts */

  WDT0_IRQn = 0,

  ...

} IRQn_Type;

////////////////////////////////////////////////////

//Peripheral register definition

typedef struct {

  __IO uint32_t MOD;

  ...

  __IO uint32_t WINDOW;

} WWDT_Type;

#define WWDT_WINDOW_WINDOW_MASK (0xFFFFFFU)

#define WWDT_WINDOW_WINDOW_SHIFT (0U)

#define WWDT_WINDOW_WINDOW(x) (((uint32_t)(((uint32_t)(x)) << WWDT_WINDOW_WINDOW_SHIFT)) & WWDT_WINDOW_WINDOW_MASK)

////////////////////////////////////////////////////

//Peripheral base address definition

#define WWDT0_BASE (0x5000E000u)


5. startup_device.s: Chip interrupt vector table file, mainly including interrupt vector table definition (DCD xx_Handler) and weak definition of each interrupt service program (PUBWEAK). Note: This file varies depending on the compiler.


;;Based on IAR's startup_device.s file

        MODULE ?cstartup

        ;; Forward declaration of sections.

        SECTION CSTACK:DATA:NOROOT(3)

        SECTION .intvec:CODE:NOROOT(2)

        PUBLIC __vector_table

        PUBLIC __Vectors_End

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Interrupt vector table definition

        DATA

__vector_table

        DCD sfe(CSTACK)

        DCD Reset_Handler

        DCD NMI_Handler

        DCD HardFault_Handler

        ...

        DCD SysTick_Handler

        ; External Interrupts

        DCD WDT0_IRQHandler

        ...

__Vectors_End

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Interrupt service routine weak definition

        THUMB

        PUBWEAK WDT0_IRQHandler

        PUBWEAK WDT0_DriverIRQHandler

        SECTION .text:CODE:REORDER:NOROOT(2)

WDT0_IRQHandler

        LDR R0, =WDT0_DriverIRQHandler

        BX

WDT0_DriverIRQHandler

        B.

        END


6. system_device.c/h: chip system initialization file, mainly including the definition of the global variable SystemCoreClock (providing the default operating frequency of the chip core) and the definition of the SystemInit() function (completing the most basic system initialization, such as WDOG initialization, RAM enabling, etc., which varies depending on the chip design).


7. Device SDK Library: The chip peripheral SDK driver package file provided by the official. With this SDK package, you can directly use the on-chip peripherals to design your own applications without having to look up the peripheral module registers in the chip manual to rewrite the peripheral driver. Of course, not every manufacturer has a complete SDK package, which depends on the importance each manufacturer places on software services.


// WWDT driver API from NXP SDK

void WWDT_GetDefaultConfig(wwdt_config_t *config);

void WWDT_Init(WWDT_Type *base, const wwdt_config_t *config);

void WWDT_Deinit(WWDT_Type *base);

void WWDT_ClearStatusFlags(WWDT_Type *base, uint32_t mask);

void WWDT_Refresh(WWDT_Type *base);


Category 5: Created by Developer

  The fifth type of files are created by developers themselves to implement their own embedded applications, which are divided into application system startup files, application system initialization files, and application files. Among them, the application system startup and initialization files are files before the main function, which can generally be used universally. Most developers do not care about their specific content, but understanding their process can deepen their understanding of the embedded system structure.


8. reset.s: Application system reset startup file. Those who understand the ARM principle know that the first 8 bytes of image data are the initial SP and PC when the chip is powered on. PC points to Reset_Handler in this file, which is the first function entry executed by the chip. This function is mainly used to complete the application system initialization work, including redirecting the application interrupt vector table, calling chip system initialization, clearing the ARM system register rx, initializing the application data segments, initializing the ARM system interrupt, and jumping to the main function.


// A classic startup code

        SECTION .noinit : CODE

        THUMB

        import SystemInit

        import init_data_bss

        import main

        import CSTACK$$Limit

        import init_interrupts

        EXTERN __vector_table

        REQUIRE __vector_table

#define SCB_BASE (0xE000ED00)

#define SCB_VTOR_OFFSET (0x00000008)

        PUBLIC Reset_Handler

        EXPORT Reset_Handler

Reset_Handler

        // Mask interrupts

        cpsid i

        // Set VTOR register in SCB first thing we do.

        ldr r0,=__vector_table

        ldr r1,=SCB_BASE

        str r0,[r1, #SCB_VTOR_OFFSET]

        // Init the rest of the registers

        ldr r2,=0

        ldr r3,=0

        ldr r4,=0

        ldr r5,=0

        ldr r6,=0

        ldr r7,=0

        mov r8,r7

        mov r9,r7

        mov r10,r7

        mov r11,r7

        mov r12,r7

        // Initialize the stack pointer

        ldr r0,=CSTACK$$Limit

        mov r13,r0

        // Call the CMSIS system init routine

        ldr r0,=SystemInit

        blx r0

        // Init .data and .bss sections

        ldr r0,=init_data_bss

        blx r0

        // Init interrupts

        ldr r0,=init_interrupts

[1] [2]
Keywords:ARM Reference address:Pi Zi Heng Embedded: ARM Cortex-M Files (1) - Source Files (.c/.h/.s)

Previous article:Pi Ziheng Embedded: ARM Cortex-M Files (0) - File Association
Next article:Pi Zi Heng Embedded: ARM Cortex-M Files (2) - Link File (.icf)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号