FX3学习笔记6-gpio

1、实验环境

硬件平台:CYUSB3KIT-003 EZ-USB® FX3™ SuperSpeed Explorer Kit

sdk版本:EZ-USB FX3 SDK1.3 / SuperSpeed Explorer Kit 1.0

实验例程:cyfxuvc_an75779(cypress官网下载demo)

2、实验目的

使用CYUSB3KIT-003的gpio_50(板子丝印I2S_CLK)配置为simple gpio输出

3、程序代码

3.1 使能gpio的simple功能

  • 注意 (io_cfg.gpioSimpleEn bit 50 必须置1,否则3.2会报CY_U3P_ERROR_NOT_CONFIGURED错误)
main (void)
{
    CyU3PReturnStatus_t apiRetStatus;
    CyU3PIoMatrixConfig_t io_cfg;

    /* Configure the IO matrix for the device. */
    io_cfg.isDQ32Bit        = CyFalse;
    io_cfg.s0Mode           = CyFalse;
    io_cfg.s1Mode           = CyFalse;
    io_cfg.lppMode          = CY_U3P_IO_MATRIX_LPP_DEFAULT;
    io_cfg.gpioSimpleEn[0]  = 0x3f200000;
    io_cfg.gpioSimpleEn[1]  = 0x01f42f00;//bit 50 必须置1,否则3.2会报CY_U3P_ERROR_NOT_CONFIGURED错误
    io_cfg.gpioComplexEn[0] = 0;
    io_cfg.gpioComplexEn[1] = 0x001C0000;
    io_cfg.useUart          = CyTrue;   /* Uart is enabled for logging. */
    io_cfg.useI2C           = CyTrue;   /* I2C is used for the sensor interface. */
    io_cfg.useI2S           = CyFalse;
    io_cfg.useSpi           = CyFalse;

    apiRetStatus = CyU3PDeviceConfigureIOMatrix (&io_cfg);
    if (apiRetStatus != CY_U3P_SUCCESS)
    {
        goto handle_fatal_error;
    }

3.2 初始化gpio为输出高电平(无上拉、下拉)

static void gpio_init()
{
    CyU3PGpioSimpleConfig_t      gpioConfig;
    CyU3PReturnStatus_t          apiRetStatus;

    /* GPIO50 is the version  pin */
    gpioConfig.outValue = CyTrue;
    gpioConfig.inputEn = CyFalse;
    gpioConfig.driveLowEn = CyFalse;
    gpioConfig.driveHighEn = CyFalse;
    gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
    ap20iRetStatus           = CyU3PGpioSetSimpleConfig (50, &gpioConfig);
    if (apiRetStatus != CY_U3P_SUCCESS)
    {
        CyU3PDebugPrint (4, "tcl59108 GPIO Set Config Error, Error Code = %d\n", apiRetStatus);
        CyFxAppErrorHandler (apiRetStatus);
    }
}
  • gpioConfig.inputEn:输入使能(gpio输出、输入功能选择)
  • gpioConfig.driveLowEn:内部下拉
  • gpioConfig.driveHighEn:内部上拉
  • gpioConfig.outValue : 输出电平

3.3 修改gpio输出状态(gpio输出功能)

  • 接口1
/** \brief Update the state of a simple GPIO output pin.

    **Description**\n
    This function updates the state of a simple GPIO output pin. This API works
    only for simple GPIOs and will be faster than the more generic CyU3PGpioSetValue
    API call.

    **Return value**\n
    * CY_U3P_SUCCESS              - If the operation is successful.\n
    * CY_U3P_ERROR_NOT_STARTED    - If the GPIO block has not been initialized.\n
    * CY_U3P_ERROR_BAD_ARGUMENT   - If the GPIO id is invalid.

    **\see
    *\see CyU3PGpioSetSimpleConfig
    *\see CyU3PGpioSetValue
    *\see CyU3PGpioSimpleGetValue
 */
extern CyU3PReturnStatus_t
CyU3PGpioSimpleSetValue (
        uint8_t  gpioId,                /**< GPIO id to be modified. */
        CyBool_t  value                 /**< Value to set on the GPIO pin. */
        );
  • 接口2
/** \brief Update the state of a GPIO output.

    **Description**\n
    This function updates the state of a GPIO output pin. The driveLowEn and driveHighEn
    values set at the time of GPIO configuration will determine whether the pin is actually
    driven with the desired value or tri-stated.

    **Return value**\n
    * CY_U3P_SUCCESS              - If the operation is successful.\n
    * CY_U3P_ERROR_NOT_STARTED    - If the GPIO block has not been initialized.\n
    * CY_U3P_ERROR_BAD_ARGUMENT   - If the GPIO id is invalid.\n
    * CY_U3P_ERROR_NOT_CONFIGURED - If the pin is not selected as a GPIO in the IOMATRIX.

    **\see
    *\see CyU3PGpioSetSimpleConfig
    *\see CyU3PGpioSetComplexConfig
    *\see CyU3PGpioSimpleSetValue
    *\see CyU3PGpioGetValue
 */
extern CyU3PReturnStatus_t
CyU3PGpioSetValue (
        uint8_t  gpioId,                /**< GPIO id to be modified. */
        CyBool_t  value                 /**< Value to set on the GPIO pin. */
        );

3.4 读取gpio输入电平状态(gpio输入功能)

  • 接口1
/** \brief Query the state of simple GPIO input.

    **Description**\n
    Get the current signal state of a simple GPIO configured as an input signal.
    The CyU3PGpioGetValue API supports all kinds of GPIOs and has a fair amount
    of conditions checks that slow the operation down. This dedicated API works
    only on simple GPIOs and will give better GPIO access performance.

    This API can be used to get the current state of an input pin, even if it is
    configured as an interrupt source.

    **Return value**\n
    * CY_U3P_SUCCESS              - If the operation is successful.\n
    * CY_U3P_ERROR_NOT_STARTED    - If the GPIO block has not been initialized.\n
    * CY_U3P_ERROR_BAD_ARGUMENT   - If the GPIO id is invalid.\n
    * CY_U3P_ERROR_NULL_POINTER   - If value_p is NULL.

    **\see
    *\see CyU3PGpioSetSimpleConfig
    *\see CyU3PGpioSimpleSetValue
    *\see CyU3PGpioGetValue
    *\see CyU3PGpioGetIOValues
 */
extern CyU3PReturnStatus_t
CyU3PGpioSimpleGetValue (
        uint8_t  gpioId,                /**< GPIO id to be queried. */
        CyBool_t *value_p               /**< Output parameter that will be filled with the GPIO value. */
        );
  • 接口2
/** \brief Query the state of GPIO input.

    **Description**\n
    Get the current state of an input pin configured as a simple or a complex GPIO.
    This function can also retrieve the last updated outValue for an output pin.

    This API can be used to get the current state of an input pin, even if it is
    configured as an interrupt source.

    **Return value**\n
    * CY_U3P_SUCCESS              - If the operation is successful.\n
    * CY_U3P_ERROR_NOT_STARTED    - If the GPIO block has not been initialized.\n
    * CY_U3P_ERROR_BAD_ARGUMENT   - If the GPIO id is invalid.\n
    * CY_U3P_ERROR_NULL_POINTER   - If value_p is NULL.\n
    * CY_U3P_ERROR_NOT_CONFIGURED - If the pin is not selected as a GPIO in the IOMATRIX.

    **\see
    *\see CyU3PGpioSetSimpleConfig
    *\see CyU3PGpioSetComplexConfig
    *\see CyU3PGpioSimpleGetValue
    *\see CyU3PGpioSetValue
    *\see CyU3PGpioGetIOValues
 */
extern CyU3PReturnStatus_t
CyU3PGpioGetValue (
        uint8_t  gpioId,                /**< GPIO id to be queried. */
        CyBool_t *value_p               /**< Output parameter that will be filled with the GPIO value. */
        );

4、实验现象

配置输出可通过万用表测了gpio电平验证输出状态

配置输入需要给gpio外接高低电平,然后程序查询gpio电平状态

猜你喜欢

转载自blog.csdn.net/gyb510/article/details/78772897
FX