Abstract: The Raspberry Pi is an ARM-based PC motherboard developed by the British charity Raspberry Pi Foundation. This paper introduces the process of raspberry PI transplantation based on LiteOS.

This article is shared from Huawei Cloud community “2021 LiteOS Raspberry PI Porting Guide (I)” by Lionlace.

Hardware information

Raspberry Pi 2 Model B

CPU: Broadcom BCM2836

Frequency: 900 MHZ

Memory: 1 gb

GPU: VideoCore IV GPU

Transplantation to prepare

Hardware environment

Raspberry Pi 2 Model B development board, USB-to-TTL module, SDcard and card reader were used in this experiment.

Software environment

  • This experiment needs to build the Linux environment (make, ARM-None-eabi compilation tool chain) according to the LiteOS tutorial on the code cloud. Environment building tutorial: gitee.com/LiteOS/Lite…

  • This experiment tools need to download the official mirror (Raspberry Pi Imager), download address: www.raspberrypi.org/software/

Migration steps

Creating a directory structure

Add the Raspberry_Pi2B directory to the targets directory and transplant it by following the realView-PBX-A9 startup process that is less different from the Cortex-A7 architecture.

  • Copy reset_vector.S and main.c from realView-PBX-a9 to Raspberry_Pi2B and rename reset_vector.S to los_startup_gcc.

  • Merge the contents of board.ld and liteos.ld in realView-PBX-a9 into the file liteos.ld in Raspberry_Pi2B.

  • Copy the include and OS_adapt folders from realview-PBX-a9 to Raspberry_Pi2B and delete unwanted DMA-related header files include/asm/dma.h.

Shut down the SMP and MMU

Add code to turn off SMP and MMU in los_startup_gcc.S.

  • Disable the SMP function

    mrc p15, 0, r0, c1, c0, 1 bic r0, r0, #0x40 mcr p15, 0, r0, c1, c0, 1

The following table provides the bit6 function description of the Auxiliary Control Register (ACTLR). For more information about the REGISTERS, see the Cortex-A7 MPCore Technical Reference Manual.

  • Disable the MMU function

    mrc p15, #0, r0, c1, c0, #0

    bic r0, r0, #1

    mcr p15, #0, r0, c1, c0, #0 @ clear mmu bit

The preceding table shows the function description of System Control Register (SCTLR) bit0. For more information about registers, see cortex-A7MPCore Technical Reference Manual.

  • Remove calls to SMP-related functions

Delete enable_scu and secondary_CPU_start from los_startup_gcc.S.

Can make FPU/ENON

Configuration of FPU/NEON:

/* enable fpu+neon */

LDR     r0, =(0xF << 20)

MCR     p15, 0, r0, c1, c0, 2

MOV     r3, #0x40000000

VMSR    FPEXC, r3
Copy the code

The first two lines above are used to set access for CP10 and CP11, and the last two lines are used to set the EN bit of register FPEXC to enable FPU.

Note: In arm coprocessor design, up to 16 coprocessors can be supported, usually named CP0 ~ CP15.

The preceding table shows the function description of register CPACR BIT20-23. For more information about registers, see cortex-A7 MPCore Technical Reference Manual.

Modifying link scripts

Elf file in the SD card. The program reads the config. TXT file in the SD card, which records some configuration information. If the startup address and startup file are not set, by default the kernel8.img file, which is aARCH64-compiled and starts at 0x80000, is loaded. If the SD card does not have the kernel8.img image file, the kernel7.img image file is loaded. The kernel7.img image file is a program compiled by the 32-bit compiler and starts at 0x8000. The CPU of raspberry PI 2B is 32-bit architecture, so set the boot address in the liteos.ld file to 0x8000.

Stack initialization

Los_startup_gcc. S only sets sp register in SVC mode. Add cpuInit function to initialize sp pointer in other modes. As follows:

VOID cpuInit(VOID)
{
    __asm__ (
    "msr    cpsr_c, %1\n\t"
    "mov    sp,     %0\n\t"
    "msr    cpsr_c, %3\n\t"
    "mov    sp,     %2\n\t"
    "msr    cpsr_c, %5\n\t"
    "mov    sp,     %4\n\t"
    "msr    cpsr_c, %7\n\t"
    "mov    sp,     %6\n\t"
    "msr    cpsr_c, %8\n\t"
        :
        : "r" (__irq_stack_top),
          "I" (PSR_F_BIT | PSR_I_BIT | CPSR_IRQ_MODE),
          "r" (__abt_stack_top),
          "I" (PSR_F_BIT | PSR_I_BIT | CPSR_ABT_MODE),
          "r" (__undef_stack_top),
          "I" (PSR_F_BIT | PSR_I_BIT | CPSR_UNDEF_MODE),
          "r" (__fiq_stack_top),
          "I" (PSR_F_BIT | PSR_I_BIT | CPSR_FIQ_MODE),
          "I" (PSR_F_BIT | PSR_I_BIT | CPSR_SVC_MODE)
        : "r14");
}
Copy the code

Configure the dynamic memory address

#define OS_SYS_MEM_ADDR ((void *)(&__bss_end)) #define LOS_HEAP_ADDR_END (void*)(0x0 + 4 * 1024 * 1024) /* */ # DEFINE OS_SYS_MEM_SIZE (UINT32)((UINT32)LOS_HEAP_ADDR_END - (UINT32)OS_SYS_MEM_ADDR + (64-1)) & ~(64-1)Copy the code

OS_SYS_MEM_ADDR indicates the start address of the dynamic memory, LOS_HEAP_ADDR_END indicates the end address of the dynamic memory, and OS_SYS_MEM_SIZE indicates the size of the dynamic memory.

A serial port to realize

The schematic diagram of RASPberry PI 2B leads to the mini_uART serial ports TXD0 and RXD0, and the corresponding pins are GPIO14 and GPIO15, as shown below:

C and usart.h files, write the serial port initialization function UartInit in usart.c, and implement uart_geTC, uart_hwiCreate and uart_write interfaces in uart_debug.c file, and realize the printf function output from the serial port.

Adapter interrupt

The raspberry PI 2B interrupt belongs to the BCM specific interrupt controller. Add the arm_control.c file in the drivers/interrupt directory and implement the callback function in the HwiControllerOps structure in this file.

STATIC const HwiControllerOps g_armControlOps = {
    .enableIrq      = HalIrqUnmask,
    .disableIrq     = HalIrqMask,
    .getCurIrqNum   = HalCurIrqGet,
    .getIrqVersion  = HalIrqVersion,
    .getHandleForm  = HalIrqGetHandleForm,
    .handleIrq      = IrqEntryArmControl,
    .clearIrq       = HalIrqClear,
    .triggerIrq     = HalIrqPending,
};
Copy the code

The table above shows the offset addresses of interrupt register. Please refer to the official chip manual for more information about registers.

Adapter systick

Raspberry PI 2B triggers systick interrupts through a Timer(arm Side). For details, see drivers\timer\rasp_systick.c.

/* systime=250000000 */
    timer->preDivider = (OS_SYS_CLOCK / OS_SYS_US_PER_SECOND - 1);
    timer->reload   = 0;
    timer->load     = 0;
    timer->IRQClear = 0;
    timer->control  = 0;
    timer->reload   = LOSCFG_BASE_CORE_TICK_PER_SECOND;
    timer->load     = LOSCFG_BASE_CORE_TICK_PER_SECOND;
    /* 23-bit counter, enable interrupt, enable timer */
    timer->control = (1 << 1) | (1 << 5) | (1 << 7);
    UINT32 ret = LOS_HwiEnable(ARM_TIMER_INI);
Copy the code

The Timer is set to trigger a Systick interrupt every 1ms.

The above is the offset address of the Timer register. Please refer to the official chip manual for detailed register information.

Configuration to compile

  • Add the kconfig.raspberry file to the targets directory:

    config LOSCFG_PLATFORM string default “Raspberry_Pi2B” if LOSCFG_PLATFORM_Raspberry_Pi2B choice prompt “Board” depends on LOSCFG_FAMILY_RASPBERRY default LOSCFG_PLATFORM_Raspberry_Pi2B help Raspberry_Pi2B config LOSCFG_PLATFORM_Raspberry_Pi2B bool “Raspberry_Pi2B” select LOSCFG_ARCH_CORTEX_A7 select LOSCFG_USING_BOARD_LD select LOSCFG_PLATFORM_ARM_CONTROL select LOSCFG_Raspberry_Pi2B_SYSTICK endchoice

  • Modify the Makefile file

Modify the following path respectively Makefile (details please refer to the corresponding gitee warehouse files) : driver/timer/Makefiledriver/interrupt/Makefiletargets/Raspberry_Pi2B/Makefile

  • Add the.img generation directive

-o binary(OBJCOPY) −Obinary(OUT)/ @[email protected] @.elf(OUT)/kernel7.img, Used to convert generated ELF files to generate kernel7.img files.

Make and start SDcard

  • Use the Raspberry Pi Imager tool to create the Raspberry Pi system.

Raspberry Pi Imager download link: www.raspberrypi.org/software/

  • Replace the kernel7.img file in SDcard with the kernel7.img file generated by compilation.

  • Insert the SDcard written to the image file into raspberry PI 2B and power it on. Raspberry PI 2B can run LiteOS system. The running results are as follows:

    Hello Huawei LiteOS LiteOS Kernel Version: 5.1.0 Build Data: Jul 13 2021 16:40:42


    OsAppInit cpu 0 entering scheduler app init! Hello, welcome to liteos demo! Huawei LiteOS #

At this point, the LiteOS system is up and running. The port project is available on the Gitee LiteOS community at gitee.com/LiteOS/Lite…

References link

[1] Raspberry Pihardware – Raspberry Pi Documentation:www.raspberrypi.org/documentati…

[2] Raspberry PI official Chip Manual:

Datasheets.raspberrypi.org/bcm2835/bcm…

[3] Cortex-A7 Technical Reference Manual:

Developer.arm.com/documentati…

Click to follow, the first time to learn about Huawei cloud fresh technology ~