2. Use of M601 GPIO

1 GPIO related data structure and API


The data structure and API introduced in this article can refer to the zyf_gpio.h file in the SDK


1.1 GPIO pin enumeration


typedef enum { PINNAME_WAKE_IN = 0, PINNAME_AP_READY, PINNAME_W_DISABLE, PINNAME_NET_MODE, PINNAME_NET_STATUS, PINNAME_SD_INS_DET, PINNAME_PCM_IN, PINNAME_PCM_OUT, PINNAME_PCM_SYNC, PINNAME_PCM_CLK, PINNAME_SDC2_DATA3, PINNAME_SDC2_DATA2, PINNAME_SDC2_DATA1, PINNAME_SDC2_DATA0, PINNAME_SDC2_CMD, PINNAME_SPI_CS_N, PINNAME_SPI_MOSI, PINNAME_SPI_MISO, PINNAME_SPI_CLK, PINNAME_I2C_SCL, PINNAME_I2C_SDA, PINNAME_STATUS, PINNAME_RI , PINNAME_DCD, PINNAME_CTS, PINNAME_RTS, PINNAME_DTR,



























PINNAME_MAX
}Enum_PinName;
These pins can be used as ordinary GPIO, in addition, they can also be reused for other functions. When used as a normal GPIO, please refer to the API introduction in this article for configuration. If used as a reuse function, please refer to the corresponding document for configuration

1.2 GPIO configuration enumeration


Enum_PinDirection configures whether the direction of GPIO is input or output. When configured as output, it can be used for control, such as LED, and configured as input, it can be used for detection, such as interrupt detection.
typedef enum{ PINDIRECTION_IN = 0, PINDIRECTION_OUT = 1 }Enum_PinDirection; When configuring Enum_PinDirection to PINDIRECTION_OUT or 1, Enum_PinPullSel does not need to be concerned, we only need to care about the following Enum_PinLevel. When configured as low, GPIO outputs low level; when configured as high, GPIO outputs high level. typedef enum{ PINLEVEL_LOW = 0, PINLEVEL_HIGH = 1 }Enum_PinLevel; When configuring Enum_PinDirection to PINDIRECTION_IN or 0, Enum_PinLevel does not need to be concerned, we only need to care about the following Enum_PinPullSel. When configured as 0, the pull selection is disabled; when configured as 1, it is a pull-down input; when configured as 2, it is a pull-up input. The GPIO at this time has no output control capability because the configured direction is input. typedef enum{ PINPULLSEL_DISABLE = 0, // Disable pull selection PINPULLSEL_PULLDOWN = 1, // Pull-down PINPULLSEL_PULLUP = 2 // Pull-up













} Enum_PinPullSel;

 

1.3 GPIO related API


1.3.1 ZYF_GpioInit
  This function enables the GPIO function of the specified pin and initializes the configuration, including direction, level and pull selection
. Function prototype
int32_t ZYF_GpioInit(Enum_PinName pinName,
  Enum_PinDirection dir,
  Enum_PinLevel level,
  Enum_PinPullSel pullSel
);

· Parameter
pinName: refer to 1.1 GPIO pin enumeration
dir: refer to 1.2 GPIO configuration enumeration
level: refer to 1.2 GPIO configuration enumeration
pullSel: refer to 1.2 GPIO configuration enumeration
· Return value
ZYF_RET_OK, this function succeeds
ZYF_RET_ERR_NOSUPPORTPIN, input GPIO is invalid
ZYF_RET_ERR_PINALREADYSUBCRIBE, GPIO is being used elsewhere. For example, this GPIO has been used as EINT


1.3.2 ZYF_GpioSetLevel
  This function is used to set the level of the specified GPIO
· Function prototype
int32_t ZYF_GpioSetLevel(Enum_PinName pinName, Enum_PinLevel level);
· Parameter
pinName: refer to 1.1 GPIO pin enumeration
level: refer to 1.2 GPIO configuration enumeration
· Return value
ZYF_RET_OK, this function succeeds
ZYF_RET_ERR_NOSUPPORTPIN, the input GPIO is invalid
ZYF_RET_ERR_NORIGHTOPERATE, cannot be operated, it may be that the GPIO is not initialized
ZYF_RET_ERR_NOGPIOMODE, the input GPIO is not in GPIO mode


1.3.3 ZYF_GpioGetLevel
This function is used to get the level status of the specified GPIO.
· Function prototype
int32_t ZYF_GpioGetLevel(Enum_PinName pinName);
· Parameter
pinName: refer to 1.1 GPIO pin enumeration

· return value

The level value of the specified GPIO, that is, a non-negative integer
ZYF_RET_ERR_NOSUPPORTPIN, indicating that the input GPIO is invalid

1.3.4 ZYF_GpioSetDirection
This function is used to set the direction
of the specified GPIO · Function prototype
int32_t ZYF_GpioSetDirection(Enum_PinName pinName, Enum_PinDirection dir);
· Parameter
pinName: refer to 1.1 GPIO pin enumeration
dir: refer to 1.2 GPIO configuration enumeration
· Return value
ZYF_RET_OK means This function succeeded.
ZYF_RET_ERR_NOSUPPORTPIN, input GPIO is invalid.
ZYF_RET_ERR_NORIGHTOPERATE, unable to operate, it may be that the GPIO is not initialized
ZYF_RET_ERR_NOGPIOMODE, the input GPIO is not in GPIO mode

1.3.5 ZYF_GpioGetDirection
This function is used to get the direction
of the specified GPIO · Function prototype
int32_t ZYF_GpioGetDirection(Enum_PinName pinName);
· Parameter
pinName: Refer to 1.1 GPIO pin enumeration
· Return value
Specify the direction of the GPIO, that is, a non-negative integer
* ZYF_RET_ERR_NOSUPPORTPIN, input GPIO is invalid.
* ZYF_RET_ERR_NORIGHTOPERATE, unable to operate, may be that GPIO is not initialized
* ZYF_RET_ERR_NOGPIOMODE, the input GPIO is not in GPIO mode

1.3.6 ZYF_GpioSetPullSelection
This function is used to set the designated GPIO selection pull
-function prototype
int32_t ZYF_GpioSetPullSelection (Enum_PinName pinName, Enum_PinPullSel
pullSel);
* Parameters
pinName: 1.1 Reference GPIO pins enumerated
pullSel: 1.2 Reference GPIO configured enumeration
· return value
ZYF_RET_OK Indicates that this function is successful.
ZYF_RET_ERR_NOSUPPORTPIN, input GPIO is invalid.
ZYF_RET_ERR_NORIGHTOPERATE, unable to operate, it may be that the GPIO is not initialized
ZYF_RET_ERR_NOGPIOMODE, the input GPIO is not in GPIO mode

1.3.7 ZYF_GpioGetPullSelection
This function is used to get the pull state
of the specified GPIO . Function prototype
int32_t ZYF_GpioGetPullSelection(Enum_PinName pinName);
· Parameter
pinName: refer to 1.1 GPIO pin enumeration
· Return value
This is a non-negative integer pull selection.
If successful, an Enum_PinPullSel value is returned. Otherwise, it returns
ZYF_RET_ERR_NOSUPPORTPIN, and inputting GPIO is invalid.
ZYF_RET_ERR_NORIGHTOPERATE, unable to operate, it may be that the GPIO is not initialized
ZYF_RET_ERR_NOGPIOMODE, the input GPIO is not in GPIO mode

1.3.8 ZYF_GpioUninit
This function is used to release the specified GPIO. After release, the GPIO can be used for other purposes

· Function prototype
int32_t ZYF_GpioUninit(Enum_PinName pinName);
· Parameter
pinName: refer to 1.1 GPIO pin enumeration
· Return value
ZYF_RET_OK, this function is successful.
ZYF_RET_ERR_NOSUPPORTPIN, input GPIO is invalid.
ZYF_RET_ERR_BUSSUBBUSY, GPIO not used as GPIO, used by IIC or SPI, this function cannot be released

2 Introduction to GPIO routines

This chapter mainly introduces how to use example_gpio.c in the SDK to test GPIO functions separately.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac

Use ZYF_GpioInit() to initialize the GPIO that needs to be used. After configuring it as an output, you can use ZYF_GpioGetLevel() to get the current level status, and use ZYF_GpioSetLevel() to set the level.


#include "zyf_trace.h"
#include "zyf_gpio.h"
#include "zyf_app.h"
#include "zyf_uart.h"
#include "zyf_thread.h"


//fixme:
//rw err: PINNAME_41_I2C_SCL

static Enum_PinName gpiopin[PINNAME_MAX];

static Uart_Param_t g_uart1param;

void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void AppUartInit(void)
{
    int32_t ret;
    g_uart1param.port = DEBUG_PORT;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);

    ZYF_LOG("AppUartInit");
    return;
}

void ZYF_GpioTest(void)
{
    int i;

    for (i = 0; i < PINNAME_MAX; i ++) {
        ZYF_GpioInit((Enum_PinName)i, PINDIRECTION_OUT, PINLEVEL_LOW, PINPULLSEL_PULLUP);
        ZYF_ThreadSleep(10);
    }

    uint8_t level;
    for (i = 0; i < PINNAME_MAX; i ++) {
        level = ZYF_GpioGetLevel(i);
        ZYF_LOG("read before GPIO:%d => %d", i, level);
        ZYF_ThreadSleep(200);
        ZYF_GpioSetLevel(i, !level);
        ZYF_LOG("write GPIO:%d => %d", i, !level);
        ZYF_ThreadSleep(200);
        level = ZYF_GpioGetLevel(i);
        ZYF_LOG("read after GPIO:%d => %d", i, level);
        ZYF_ThreadSleep(200);
    }

    ZYF_ThreadSleep(1000);
    for (i = 0; i < PINNAME_MAX; i ++) {
        ZYF_GpioUninit(i);
    }
}


void TimeThread_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");

    ZYF_GpioTest();
    
    while (1) {
        ZYF_LOG("in while.");
        iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(1000);
        }
    }

}

static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}


int appimg_enter(void *param)
{
    AppUartInit();
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("UartThread_Example", TimeThread_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}



 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107082544