Linux Bare Metal Development Series 3 Based on IMX6ULLmini: Key Detection Input

Table of contents

Turn on the clock corresponding to GPIO5

Set pin multiplexing

Set GPIO5_IO1 input mode

Set detection level

part of the code

button.c

led.c

main.c


 

After finding the corresponding pin on the schematic diagram, you can find the real pin on the board according to the corresponding chart. Here

The actual pin corresponding to SNVS_TAMPER1 is GPIO5_IO1

 

There is a register location corresponding to GPIO5 near the P1357 page, you can jump directly

Turn on the clock corresponding to GPIO5

 

Set pin multiplexing

 

Note: ALT5 mode is only valid when TAMPER pin is used as GPIO. It depends on the FUSE setting "TAMPER_PIN_DISABLE[1:0]".

When the TAMPER pin is used as GPIO, the following is the multiplexing information: SNVS_TAMPER1 ==> GPIO5_01

  • 101 ALT5 — select the multiplexing mode: ALT5 multiplexing port, GPIO5_IO01 in the example, namely gpio5
  • other reservations

So set the multiplexing to GPIO5

The first line of code IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01, 0);sets the alternate function of the pin IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01 to the default value (0). That is, configure the pin to its default function instead of using another alternate mode.

The second line of code IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01, 0x10B0);sets the PAD property of the pin IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01. The specific attribute value is 0x10B0.

Set GPIO5_IO1 input mode

GPIO direction bit. Bit n of this register defines the direction of the GPIO[n] signal.

Note: GPIO_GDIR will only affect the direction of the I/O signal when the corresponding bit in the I/O MUX is configured as GPIO.

  • 0 INPUT — Configure GPIO as input.
  • 1 OUTPUT — Configure GPIO as output 

The first bit is set to 0, the input mode

Set detection level

 

data bits. This register defines the value of the GPIO output when the signal is configured as an output (GDIR[n]=1). Writes to this register are stored in a register. When the signal is configured as an output (GDIR[n]=1), reading GPIO_DR will return the value stored in the register; when the signal is configured as an input (GDIR[n]=0), reading GPIO_DR will return the input The value of the signal.

Note: In order for the value of GPIO_DR to be connected with a signal, the I/O mux must be configured in GPIO mode. Always returns a value of zero if the input path is disabled.

Configured here as input, returns the value of the external signal. According to the principle, the high level of the button is pressed, the value is 1, and the value stored in the first bit of the register is 1, so at this time, it can be verified whether the button is pressed by comparing it with the left shift of 1.

part of the code

button.c

# include  "common.h"



/*按键初始化函数*/

void button_init(void)

{

    /*按键初始化*/

    CCM->CCGR1 = 0xffffffff;

    //CCM_CCGR1_CG15(0x3);  //开启GPIO5的时钟



    

    /*设置 绿灯 引脚的复用功能以及PAD属性*/

    IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01,0);     

    IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER1_GPIO5_IO01, 0x10B0); 

    

    /*设置GPIO5_01为输入模式*/

    GPIO5->GDIR &= ~(1<<1);  

}



/*按键状态输出函数*/

int get_button_status(void)

{

    if((GPIO5->DR)&(1<<1))

    {

        delay(0xFF);

         if((GPIO5->DR)&(1<<1))

         {

             return 1;

         }

    }

    return 0;

}

led.c

# include  "common.h"



  /*led初始化函数*/

void rgb_led_init(void)

{

    /*使能GPIO1时钟*/

      CCM->CCGR1 = 0xffffffff;



     /*设置 红灯 引脚的复用功能以及PAD属性*/

    IOMUXC_SetPinMux(IOMUXC_GPIO1_IO04_GPIO1_IO04,0);     

    IOMUXC_SetPinConfig(IOMUXC_GPIO1_IO04_GPIO1_IO04, 0X10B0); 



    /*设置GPIO1_04为输出模式*/

    GPIO1->GDIR |= (1<<4);  



    /*设置GPIO1_04输出电平为高电平*/

    GPIO1->DR |= (1<<4);  



}

main.c

# include  "common.h"



int main()

{

    int i = 0;



    /*初始化led灯和按键*/

    rgb_led_init();

    button_init();



    while(1)

    {

        /*按键按下*/

        if(get_button_status())

        {

            /*翻转红灯状态*/

            if(i == 0)

            {

                red_led_on;

                i = 1;

            }

            else

            {

                red_led_off;

                i = 0;

            }

            while(get_button_status());//等待按键松开

        }

    }



    return 0;    

}

Guess you like

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