IoT Lora module from entry to proficiency (4) initialization of some ports

I. Introduction

        Due to the uncertainty of program design and development, we often need to initialize some specific pins, and read the pin level status or output high and low levels to the pin.

2. Code implementation

        Quickly find the initialization statement of the port:

        First, find the board.c file. In the position in the figure below, we can see the initialization statement about the port of the LED light.

        Through the Go Definition function or directly find the gpio.c file, you can see the specific implementation method:

/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    (C)2013 Semtech

Description: Generic GPIO driver implementation

Comment: Relies on the specific board GPIO implementation as well as on
         IO expander driver implementation if one is available on the target
         board.

License: Revised BSD License, see LICENSE.TXT file include in the project

Maintainer: Miguel Luis and Gregory Cristian
*/
#include "board.h"

#include "gpio-board.h"


void GpioInit( Gpio_t *obj, PinNames pin, PinModes mode,  PinConfigs config, PinTypes type, uint32_t value )
{
	GpioMcuInit( obj, pin, mode, config, type, value );
}

void GpioSetInterrupt( Gpio_t *obj, IrqModes irqMode, IrqPriorities irqPriority, GpioIrqHandler *irqHandler )
{
    GpioMcuSetInterrupt( obj, irqMode, irqPriority, irqHandler );
}

void GpioRemoveInterrupt( Gpio_t *obj )
{
    GpioMcuRemoveInterrupt( obj );
}

void GpioWrite( Gpio_t *obj, uint32_t value )
{
    GpioMcuWrite( obj, value );
}

void GpioToggle( Gpio_t *obj )
{
    GpioMcuToggle( obj );
}

uint32_t GpioRead( Gpio_t *obj )
{
    return GpioMcuRead( obj );
}

        This time, the method we mainly use is: GpioInit()

        This method has five parameters, let's expand them one by one.

        Parameter 1: Gpio_t *obj: When we use it, we need to create a variable of Gpio_t type first, and pass it in during initialization, so that we can operate on it later.

        Parameter 2: PinNames pin: Here we fill in the port that needs to be operated, such as PB_2.

        Parameter 3: PinModes mode: Here we can fill in two common types, namely PIN_INPUT (input mode), PIN_OUTPUT (output mode), and PIN_ALTERNATE_FCT and PIN_ANALOGIC do not need our attention for the time being.

        Parameter four: PinConfigs config: Here we generally write PIN_PUSH_PULL (push-pull).

        Parameter five: PinTypes type: Here we can choose PIN_NO_PULL, PIN_PULL_UP, PIN_PULL_DOWN according to the needs.

        Parameter 6: Just fill in 0.

        Code example:

        The following shows the operation of reading PB_2 level and lighting LED1 at low level:

 

Gpio_t Device;
int main( void )
{
    Init();
		GpioInit(&Device,PB_2,PIN_INPUT,PIN_PUSH_PULL,PIN_PULL_UP,0);
    while( 1 )
    {
				if(GpioRead(&Device)==0)
					GpioWrite(&Led1,0);
				else
					GpioWrite(&Led1,1);
    }
}

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/131145704