当前位置:网站首页>Imx6ull bare metal development learning 2- use C language to light LED indicator

Imx6ull bare metal development learning 2- use C language to light LED indicator

2022-07-05 08:04:00 Changjiang houlang blog

This chapter follows the previous chapter <IMX6ULL Bare metal development learning 1- Assembly light up led> Upgrade the code .

Programming environment building :

Development board : Wildfire Imx6ull mini plate

Programming environment :Ubuntu18.04 + VScode

Start entering the code content :

C The language needs to run , There is no lack of an environment in which the assembly is ready to run .

Assembly code start.S as follows :

/*******************
* file name :   start.S
* author     :      Guizhiwei 
* mailbox     :  [email protected]
* date     :2022 - 02 -13
* describe     : Bare metal experimental test 2 C Words light up led
 ********************/

 .global    _start      @ Global label 

 /*****
 * describe  : _start function 
  */
  _start :

 /*****
 *1. To configure CPU  Get into SVC Pattern 
  */
  mrs r0,   cpsr                       @ Copy CPSR Status register to R0 in 
  bic r0,   r0,   #0x1f              @ clear CPU Mode bit BIT4-BIT0
  orr r0,   r0,   #0x13             @ To configure BIT4-0 by SVC Pattern 
  msr cpsr,   r0                       @ Register R0 The data is written back to CPSR in 


 /*****
 *2. Set the stack pointer and jump C Language main function 
  */
  ldr sp,   =0x80200000       @ Set the stack 0x80200000->0x80000000  2M Space 
  b   main                                 @ Jump to C Language main function 


  

With the above code , And stm32 Like writing code .

C Code part to a header file led.h, take CPU Relevant register address pointer is defined :

/*******************
* file name :  led.h
* author     :      Guizhiwei 
* mailbox     :  [email protected]
* date     :2022 - 02 -13
* describe     :led Lamp program header file , Store register pointer 
 ********************/
#ifndef _H_LED_H
#define _H_LED_H

/**
 * CCM Clock related register address definition 
*/
#define CCM_CCGR0           *((volatile unsigned int *)0x020c4068)
#define CCM_CCGR1           *((volatile unsigned int *)0x020c406C)
#define CCM_CCGR2           *((volatile unsigned int *)0x020c4070)
#define CCM_CCGR3           *((volatile unsigned int *)0x020c4074)
#define CCM_CCGR4           *((volatile unsigned int *)0x020c4078)
#define CCM_CCGR5           *((volatile unsigned int *)0x020c407C)
#define CCM_CCGR6           *((volatile unsigned int *)0x020c4080)

/**
 * IOMUX Reuse related register address definitions 
 * IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3  Address 229_0014h
 * IOMUXC_SNVS_SW_PAD_CTL_PAD_SNVS_TAMPER3  Address  229_0058h
*/
#define SW_MUX_GPIO5_IO3           *((volatile unsigned int *)0x02290014)
#define SW_PAD_GPIO5_IO3           *((volatile unsigned int *)0x02290058)

/**
 * GPIO5 Related register address definition 
 * 
*/
#define GPIO5_DR                 *((volatile unsigned int *)0x020AC000)
#define GPIO5_GDIR            *((volatile unsigned int *)0x020AC004)

/**
 * GPIO5_IO3 Related action macros 
 *  Open drain output control 
*/
#define LED_ON()                      (GPIO5_DR &= ~(1<<3))
#define LED_OFF()                    (GPIO5_DR |= (1<<3))


#endif // !_H_LED_H

Executable code snippets :

/*******************
* file name :  led.c
* author     :      Guizhiwei 
* mailbox     :  [email protected]
* date     :2022 - 02 -13
* describe     :led Lamp program executable program 
 ********************/
#include "led.h"

/**
 *  Enable all peripherals to control the clock 
 * 
*/
void clk_enable(void)
{
        CCM_CCGR0 = 0xffffffff ;
        CCM_CCGR1 = 0xffffffff ;
        CCM_CCGR2 = 0xffffffff ;
        CCM_CCGR3 = 0xffffffff ;
        CCM_CCGR4 = 0xffffffff ;
        CCM_CCGR5 = 0xffffffff ;
        CCM_CCGR6 = 0xffffffff ;       
}

/**
 * Led Initialize the corresponding GPIO
*/
void led_Init(void)
{
  /*****
  *1.GPIO5_IO03  Reuse function configuration GPIO
  *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3  Address 229_0014h
   */
  SW_MUX_GPIO5_IO3 = 0x5;        //AL5

  /*****
  *2.GPIO5_IO03  To configure IO Property function 
  *IOMUXC_SNVS_SW_PAD_CTL_PAD_SNVS_TAMPER3  Address  229_0058h
  *BIT0  Swaying rate  0- Slow down 
  *BIT5-3  Driving ability R0/6  -110
  *BIT7-6  Speed  100mhz-10
  * BIT11  Open drain output enable  1- Turn on 
  *BIT12  The pull-down function is enabled  0- Close the pull-down button 
  *BIT13  Keep function 0
  *BIT15-14 100K Up and down resistance  -00
  *BIT16 hys-0 close 
  * Set the configuration value :8B0-100010110000
   */
  SW_PAD_GPIO5_IO3 = 0x08B0;

/*****
*3. Set up GPIO5_IO03  For export 
*GPIO5_GDIR    Address 20A_C004
*GPIO5_DR        Address 20A_C000
*/
    GPIO5_GDIR = 0x08;      

/*****
*4 open GPIO LED
*/
    LED_ON() ;

}

/****
 *  Time delay control operation procedure 
 *  Achieve roughly 1ms Unit delay of 
*/
void delay_ms(volatile unsigned int nms)
{
    unsigned int nus;

    while(nms--)
    {
        //1 ms Time delay 
           nus=0x7ff;
           while(nus--)
           {

           }
    }
}

/**
 *  Lord main Entry procedure 
*/
int main(void)
{
    clk_enable();       // Can make the clock 
    led_Init();             // initialization led The lamp 

    //led Flashing cycle 
    while(1)
    {
        LED_OFF();
        delay_ms(500);
        LED_ON();
        delay_ms(500);
    }
    return 0;
}

The code part is ready , Next, prepare the link script , Used to specify the starting address and order of the compiler

SECTIONS{
            . = 0x87800000;
            .text :
            {
                start.o
                led.o
                *(.text)
            }
            .rodata ALIGN(4)    :   {*(.rodata*)}
            .data   ALIGN(4)    :   {  *(.data) }
            __bss_start = .;
            .bss   ALIGN(4)    :   {  *(.bss) *(COMMON)}
            __bss_end = .;
}

Get ready Makefile Take off compilation :

objs := start.o led.o 

led.bin : $(objs)
	arm-linux-gnueabihf-ld	-Timxu.lds -o led.elf $^
	arm-linux-gnueabihf-objcopy -O binary -S  led.elf [email protected]

%.o	: %.s
	arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o [email protected] $<

%.o	: %.S
	arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o [email protected] $<

%.o	: %.c
	arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o [email protected] $<

clean:
	rm -rf *.o led.bin led.elf led.dis *.imx

dump:
	arm-linux-gnueabihf-objdump -D -m arm led.elf > led.dis

dowmload:
	./imxdownload led.bin /dev/sdb

Try compiling :

[email protected]:~/work/imxAss/Ledc$ make
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o start.o start.S
arm-linux-gnueabihf-gcc -Wall -nostdlib -c -o led.o led.c
arm-linux-gnueabihf-ld  -Timxu.lds -o led.elf start.o led.o
arm-linux-gnueabihf-objcopy -O binary -S  led.elf led.bin
原网站

版权声明
本文为[Changjiang houlang blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140547277387.html