一、GPIO输入输出
GPIO引脚对应的功能以及命名在源码的base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio_ex.h
以及wifiiot_gpio.h
中可以查找。
1.引脚功能信息
**
* @brief Enumerates GPIO hardware pin IDs.
*/
typedef enum {
/** GPIO hardware pin 0 */
WIFI_IOT_IO_NAME_GPIO_0,
/** GPIO hardware pin 1 */
WIFI_IOT_IO_NAME_GPIO_1,
/** GPIO hardware pin 2 */
WIFI_IOT_IO_NAME_GPIO_2,
/** GPIO hardware pin 3 */
WIFI_IOT_IO_NAME_GPIO_3,
/** GPIO hardware pin 4 */
WIFI_IOT_IO_NAME_GPIO_4,
/** GPIO hardware pin 5 */
WIFI_IOT_IO_NAME_GPIO_5,
/** GPIO hardware pin 6 */
WIFI_IOT_IO_NAME_GPIO_6,
/** GPIO hardware pin 7 */
WIFI_IOT_IO_NAME_GPIO_7,
/** GPIO hardware pin 8 */
WIFI_IOT_IO_NAME_GPIO_8,
/** GPIO hardware pin 9 */
WIFI_IOT_IO_NAME_GPIO_9,
/** GPIO hardware pin 10 */
WIFI_IOT_IO_NAME_GPIO_10,
/** GPIO hardware pin 11 */
WIFI_IOT_IO_NAME_GPIO_11,
/** GPIO hardware pin 12 */
WIFI_IOT_IO_NAME_GPIO_12,
/** GPIO hardware pin 13 */
WIFI_IOT_IO_NAME_GPIO_13,
/** GPIO hardware pin 14 */
WIFI_IOT_IO_NAME_GPIO_14,
/** Maximum value */
WIFI_IOT_IO_NAME_MAX,
} WifiIotIoName;
/**
* @brief Enumerates the functions of GPIO hardware pin 0.
*/
typedef enum {
/** GPIO0 function */
WIFI_IOT_IO_FUNC_GPIO_0_GPIO,
/** Functions of UART1 TXD */
WIFI_IOT_IO_FUNC_GPIO_0_UART1_TXD = 2,
/** SPI1 CK function */
WIFI_IOT_IO_FUNC_GPIO_0_SPI1_CK,
/** Functions of JTAG TD0 */
WIFI_IOT_IO_FUNC_GPIO_0_JTAG_TDO,
/** PWM3 OUT function */
WIFI_IOT_IO_FUNC_GPIO_0_PWM3_OUT,
/** I2C1 SDA function */
WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA,
} WifiIotIoFuncGpio0;
/**
* @brief Enumerates the functions of GPIO hardware pin 1.
*/
typedef enum {
/** GPIO1 function */
WIFI_IOT_IO_FUNC_GPIO_1_GPIO,
WIFI_IOT_IO_FUNC_GPIO_1_UART1_RXD = 2,
WIFI_IOT_IO_FUNC_GPIO_1_SPI1_RXD,
WIFI_IOT_IO_FUNC_GPIO_1_JTAG_TCK,
WIFI_IOT_IO_FUNC_GPIO_1_PWM4_OUT,
WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL,
WIFI_IOT_IO_FUNC_GPIO_1_BT_FREQ,
} WifiiIotIoFuncGpio1;
/**
* @brief Enumerates the functions of GPIO hardware pin 2.
*/
typedef enum {
/** GPIO2 function */
WIFI_IOT_IO_FUNC_GPIO_2_GPIO,
/** UART1 RTS function */
WIFI_IOT_IO_FUNC_GPIO_2_UART1_RTS_N = 2,
/** SPI1 TXD function */
WIFI_IOT_IO_FUNC_GPIO_2_SPI1_TXD,
/** JTAG TRSTN function */
WIFI_IOT_IO_FUNC_GPIO_2_JTAG_TRSTN,
/** PWM2 OUT function */
WIFI_IOT_IO_FUNC_GPIO_2_PWM2_OUT,
/** SSI CLK function */
WIFI_IOT_IO_FUNC_GPIO_2_SSI_CLK = 7,
} WifiIotIoFuncGpio2;
/**
* @brief Enumerates the functions of GPIO hardware pin 3.
*/
typedef enum {
/** GPIO3 function */
WIFI_IOT_IO_FUNC_GPIO_3_GPIO,
/** UART0 TXD function */
WIFI_IOT_IO_FUNC_GPIO_3_UART0_TXD,
/** UART1 CTS function */
WIFI_IOT_IO_FUNC_GPIO_3_UART1_CTS_N,
/** SPI CSN function */
WIFI_IOT_IO_FUNC_GPIO_3_SPI1_CSN,
/** JTAG TDI function */
WIFI_IOT_IO_FUNC_GPIO_3_JTAG_TDI,
/** PWM5 OUT function */
WIFI_IOT_IO_FUNC_GPIO_3_PWM5_OUT,
/** I2C1 SDA function */
WIFI_IOT_IO_FUNC_GPIO_3_I2C1_SDA,
/** SSI DATA function */
WIFI_IOT_IO_FUNC_GPIO_3_SSI_DATA,
} WifiIotIoFuncGpio3;
/**
* @brief Enumerates the functions of GPIO hardware pin 4.
*/
typedef enum {
/** GPIO4 function */
WIFI_IOT_IO_FUNC_GPIO_4_GPIO,
/** UART0 RXD function */
WIFI_IOT_IO_FUNC_GPIO_4_UART0_RXD = 2,
/** JTAG TMS function */
WIFI_IOT_IO_FUNC_GPIO_4_JTAG_TMS = 4,
/** PWM1 OUT function */
WIFI_IOT_IO_FUNC_GPIO_4_PWM1_OUT,
/** I2C1 SCL function */
WIFI_IOT_IO_FUNC_GPIO_4_I2C1_SCL,
} WifiIotIoFuncGpio4;
/**
* @brief Enumerates the functions of GPIO hardware pin 5.
*/
typedef enum {
/** GPIO5 function */
WIFI_IOT_IO_FUNC_GPIO_5_GPIO,
/** UART1 RXD function */
WIFI_IOT_IO_FUNC_GPIO_5_UART1_RXD = 2,
/** SPI0 CSN function */
WIFI_IOT_IO_FUNC_GPIO_5_SPI0_CSN,
/** PWM2 OUT function */
WIFI_IOT_IO_FUNC_GPIO_5_PWM2_OUT = 5,
/** I2C0 MCLK function */
WIFI_IOT_IO_FUNC_GPIO_5_I2S0_MCLK,
/** BT STATUS function */
WIFI_IOT_IO_FUNC_GPIO_5_BT_STATUS,
} WifiIotIoFuncGpio5;
/**
* @brief Enumerates the functions of GPIO hardware pin 6.
*/
typedef enum {
/** GPIO6 function */
WIFI_IOT_IO_FUNC_GPIO_6_GPIO,
/** UART1 TXD function */
WIFI_IOT_IO_FUNC_GPIO_6_UART1_TXD = 2,
/** SPI0 CK function */
WIFI_IOT_IO_FUNC_GPIO_6_SPI0_CK,
/** PWM3 OUT function */
WIFI_IOT_IO_FUNC_GPIO_6_PWM3_OUT = 5,
/** I2S0 TX function */
WIFI_IOT_IO_FUNC_GPIO_6_I2S0_TX,
/** COEX switch function */
WIFI_IOT_IO_FUNC_GPIO_6_COEX_SWITCH,
} WifiIotIoFuncGpio6;
/**
* @brief Enumerates the functions of GPIO hardware pin 7.
*/
typedef enum {
/** GPIO7 function */
WIFI_IOT_IO_FUNC_GPIO_7_GPIO,
/** UART1 CTS function */
WIFI_IOT_IO_FUNC_GPIO_7_UART1_CTS_N = 2,
/** SPI0 RXD function */
WIFI_IOT_IO_FUNC_GPIO_7_SPI0_RXD,
/** PWM0 OUT function */
WIFI_IOT_IO_FUNC_GPIO_7_PWM0_OUT = 5,
/** I2S0 BCLK function */
WIFI_IOT_IO_FUNC_GPIO_7_I2S0_BCLK,
/** BT ACTIVE function */
WIFI_IOT_IO_FUNC_GPIO_7_BT_ACTIVE,
} WifiIotIoFuncGpio7;
/**
* @brief Enumerates the functions of GPIO hardware pin 8.
*/
typedef enum {
/** GPIO8 function */
WIFI_IOT_IO_FUNC_GPIO_8_GPIO,
/** UART1 RTS function */
WIFI_IOT_IO_FUNC_GPIO_8_UART1_RTS_N = 2,
/** SPI0 TXD function */
WIFI_IOT_IO_FUNC_GPIO_8_SPI0_TXD,
/** PWM1 OUT function */
WIFI_IOT_IO_FUNC_GPIO_8_PWM1_OUT = 5,
/** I2S0 WS function */
WIFI_IOT_IO_FUNC_GPIO_8_I2S0_WS,
/** WLAN ACTIVE function */
WIFI_IOT_IO_FUNC_GPIO_8_WLAN_ACTIVE,
} WifiIotIoFuncGpio8;
/**
* @brief Enumerates the functions of GPIO hardware pin 9.
*/
typedef enum {
/** GPIO9 function */
WIFI_IOT_IO_FUNC_GPIO_9_GPIO,
/** I2C0 SCL function */
WIFI_IOT_IO_FUNC_GPIO_9_I2C0_SCL,
/** UART2 RTS function */
WIFI_IOT_IO_FUNC_GPIO_9_UART2_RTS_N,
/** SDIO D2 function */
WIFI_IOT_IO_FUNC_GPIO_9_SDIO_D2,
/** SPI0 TXD function */
WIFI_IOT_IO_FUNC_GPIO_9_SPI0_TXD,
/** PWM0 OUT function */
WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT,
/** I2S0 MCLK function */
WIFI_IOT_IO_FUNC_GPIO_9_I2S0_MCLK = 7,
} WifiIotIoFuncGpio9;
/**
* @brief Enumerates the functions of GPIO hardware pin 10.
*/
typedef enum {
/** GPIO10 function */
WIFI_IOT_IO_FUNC_GPIO_10_GPIO,
/** I2C0 SDA function */
WIFI_IOT_IO_FUNC_GPIO_10_I2C0_SDA,
/** UART2 CTS function */
WIFI_IOT_IO_FUNC_GPIO_10_UART2_CTS_N,
/** SDIO D3 function */
WIFI_IOT_IO_FUNC_GPIO_10_SDIO_D3,
/** SPI0 CK function */
WIFI_IOT_IO_FUNC_GPIO_10_SPI0_CK,
/** PWM1 OUT function */
WIFI_IOT_IO_FUNC_GPIO_10_PWM1_OUT,
/** I2S0 TX function */
WIFI_IOT_IO_FUNC_GPIO_10_I2S0_TX = 7,
} WifiIotIoFuncGpio10;
/**
* @brief Enumerates the functions of GPIO hardware pin 11.
*/
typedef enum {
/** GPIO11 function */
WIFI_IOT_IO_FUNC_GPIO_11_GPIO,
/** UART2 TXD function */
WIFI_IOT_IO_FUNC_GPIO_11_UART2_TXD = 2,
/** SDIO CMD function */
WIFI_IOT_IO_FUNC_GPIO_11_SDIO_CMD,
/** SDIO RXD function */
WIFI_IOT_IO_FUNC_GPIO_11_SPI0_RXD,
/** PWM2 OUT function */
WIFI_IOT_IO_FUNC_GPIO_11_PWM2_OUT,
/** RF TX_EN_EXT function */
WIFI_IOT_IO_FUNC_GPIO_11_RF_TX_EN_EXT,
/** I2S0 RX function */
WIFI_IOT_IO_FUNC_GPIO_11_I2S0_RX,
} WifiIotIoFuncGpio11;
/**
* @brief Enumerates the functions of GPIO hardware pin 12.
*/
typedef enum {
/** GPIO12 function */
WIFI_IOT_IO_FUNC_GPIO_12_GPIO,
/** SUART2 RXD function */
WIFI_IOT_IO_FUNC_GPIO_12_UART2_RXD = 2,
/** SDIO CLK function */
WIFI_IOT_IO_FUNC_GPIO_12_SDIO_CLK,
/** SDIO CSN function */
WIFI_IOT_IO_FUNC_GPIO_12_SPI0_CSN,
/** PWM3 OUT function */
WIFI_IOT_IO_FUNC_GPIO_12_PWM3_OUT,
/** RF RX_EN_EXT function */
WIFI_IOT_IO_FUNC_GPIO_12_RF_RX_EN_EXT,
/** I2S0 BCLK function */
WIFI_IOT_IO_FUNC_GPIO_12_I2S0_BCLK,
} WifiIotIoFuncGpio12;
/**
* @brief Enumerates the functions of GPIO hardware pin 13.
*/
typedef enum {
/** SSI DATA function */
WIFI_IOT_IO_FUNC_GPIO_13_SSI_DATA,
/** UART0 TXD function */
WIFI_IOT_IO_FUNC_GPIO_13_UART0_TXD,
/** UART2 RTS function */
WIFI_IOT_IO_FUNC_GPIO_13_UART2_RTS_N,
/** SDIO D0 function */
WIFI_IOT_IO_FUNC_GPIO_13_SDIO_D0,
/** GPIO13 function */
WIFI_IOT_IO_FUNC_GPIO_13_GPIO,
/** PWM4 OUT function */
WIFI_IOT_IO_FUNC_GPIO_13_PWM4_OUT,
/** I2C0 SDA function */
WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA,
/** I2S0 WS function */
WIFI_IOT_IO_FUNC_GPIO_13_I2S0_WS,
} WifiIotIoFuncGpio13;
/**
* @brief Enumerates the functions of GPIO hardware pin 14.
*/
typedef enum {
/** SSI CLK function */
WIFI_IOT_IO_FUNC_GPIO_14_SSI_CLK,
/** UART0 RXD function */
WIFI_IOT_IO_FUNC_GPIO_14_UART0_RXD,
/** UART2 CTS function */
WIFI_IOT_IO_FUNC_GPIO_14_UART2_CTS_N,
/** SDIO D1 function */
WIFI_IOT_IO_FUNC_GPIO_14_SDIO_D1,
/** GPIO14 function */
WIFI_IOT_IO_FUNC_GPIO_14_GPIO,
/** PWM5 OUT function */
WIFI_IOT_IO_FUNC_GPIO_14_PWM5_OUT,
/** I2C0 SCL function */
WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL,
} WifiIotIoFuncGpio14;
/**
* @brief Enumerates I/O driver strength levels.
*
*/
typedef enum {
/** Driver strength level 0 (highest) */
WIFI_IOT_IO_DRIVER_STRENGTH_0 = 0,
/** Driver strength level 1 */
WIFI_IOT_IO_DRIVER_STRENGTH_1,
/** Driver strength level 2 */
WIFI_IOT_IO_DRIVER_STRENGTH_2,
/** Driver strength level 3 */
WIFI_IOT_IO_DRIVER_STRENGTH_3,
/** Driver strength level 4 */
WIFI_IOT_IO_DRIVER_STRENGTH_4,
/** Driver strength level 5 */
WIFI_IOT_IO_DRIVER_STRENGTH_5,
/** Driver strength level 6 */
WIFI_IOT_IO_DRIVER_STRENGTH_6,
/** Driver strength level 7 (lowest) */
WIFI_IOT_IO_DRIVER_STRENGTH_7,
/** Maximum value */
WIFI_IOT_IO_DRIVER_STRENGTH_MAX,
} WifiIotIoDriverStrength;
2.相关接口
2.1 GpioInit 初始化
unsigned int GpioInit(void);
2.2 GPIO GpioDeinit 取消初始化
unsigned int GpioDeinit(void);
2.3 GPIO GpioSetDir 设置GPIO引脚方向
unsigned int GpioSetDir(WifiIotGpioIdx id, WifiIotGpioDir dir);
//设置GPIO_2为输出模式
GpioSetDir(WIFI_IOT_GPIO_IDX_2, WIFI_IOT_GPIO_DIR_OUT);
2.3.1WifiIotGpioIdx与WifiIotGpioDir
/**
* @brief Enumerates GPIO pin IDs.
*/
typedef enum {
/** GPIO0 */
WIFI_IOT_GPIO_IDX_0,
/** GPIO1 */
WIFI_IOT_GPIO_IDX_1,
/** GPIO2 */
WIFI_IOT_GPIO_IDX_2,
/** GPIO3 */
WIFI_IOT_GPIO_IDX_3,
/** GPIO4 */
WIFI_IOT_GPIO_IDX_4,
/** GPIO5 */
WIFI_IOT_GPIO_IDX_5,
/** GPIO6 */
WIFI_IOT_GPIO_IDX_6,
/** GPIO7 */
WIFI_IOT_GPIO_IDX_7,
/** GPIO8 */
WIFI_IOT_GPIO_IDX_8,
/** GPIO9 */
WIFI_IOT_GPIO_IDX_9,
/** GPIO10 */
WIFI_IOT_GPIO_IDX_10,
/** GPIO11 */
WIFI_IOT_GPIO_IDX_11,
/** GPIO12 */
WIFI_IOT_GPIO_IDX_12,
/** GPIO13 */
WIFI_IOT_GPIO_IDX_13,
/** GPIO14 */
WIFI_IOT_GPIO_IDX_14,
/** Maximum value */
WIFI_IOT_GPIO_IDX_MAX,
} WifiIotGpioIdx;
/**
* @brief Enumerates GPIO directions.
*/
typedef enum {
/** Input */
WIFI_IOT_GPIO_DIR_IN = 0,
/** Output */
WIFI_IOT_GPIO_DIR_OUT
} WifiIotGpioDir;
2.4 GpioGetDir 获取GPIO引脚方向
unsigned int GpioGetDir(WifiIotGpioIdx id, WifiIotGpioDir *dir);
WifiIotGpioDir val = {0};
GpioGetDir(WIFI_IOT_GPIO_IDX_2,&val);
printf("GPIO_2 Dir is %d\r\n",val);
2.5 GpioSetOutputVal 设置GPIO引脚输出电平值
unsigned int GpioSetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue val);
2.5.1 WifiIotGpioValue
/**
* @brief Enumerates GPIO level values.
*/
typedef enum {
/** Low GPIO level */
WIFI_IOT_GPIO_VALUE0 = 0,
/** High GPIO level */
WIFI_IOT_GPIO_VALUE1
} WifiIotGpioValue;
2.6 GpioGetOutputVal 获取GPIO引脚输出电平值
unsigned int GpioGetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue *val);
WifiIotGpioValue OutputVal = {0};
GpioGetOutputVal(WIFI_IOT_GPIO_IDX_2,&OutputVal);
printf("GPIO_2 OutputVal is %d\r\n",OutputVal);
2.7 GpioGetInputVal 获取GPIO引脚输入电平值
unsigned int GpioGetInputVal(WifiIotGpioIdx id, WifiIotGpioValue *val);
2.8 IoSetPull 设置GPIO引脚上拉
unsigned int IoSetPull(WifiIotIoName id, WifiIotIoPull val);
2.8.1 WifiIotIoPull
/**
* @brief Enumerates GPIO pull-up or pull-down settings.
*/
typedef enum {
/** No pull */
WIFI_IOT_IO_PULL_NONE,
/** Pull-up */
WIFI_IOT_IO_PULL_UP,
/** Pull-down */
WIFI_IOT_IO_PULL_DOWN,
/** Maximum value */
WIFI_IOT_IO_PULL_MAX,
} WifiIotIoPull;
2.9 IoGetPull 获取GPIO引脚上拉
unsigned int IoGetPull(WifiIotIoName id, WifiIotIoPull *val);
2.10 IoSetFunc 设置GPIO引脚功能
unsigned int IoSetFunc(WifiIotIoName id, unsigned char val);
2.11 IoGetFunc 获取GPIO引脚功能
unsigned int IoGetFunc(WifiIotIoName id, unsigned char *val);
2.12 IOSetDriverStrength 设置GPIO驱动能力
unsigned int IOSetDriverStrength(WifiIotIoName id, WifiIotIoDriverStrength val);
2.13 IOGetDriverStrength 获取GPIO驱动能力
unsigned int IOGetDriverStrength(WifiIotIoName id, WifiIotIoDriverStrength *val);
3. 示例代码
static void LedTask(void)
{
//初始化GPIO
GpioInit();
//设置GPIO_2的复用功能为普通GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
//设置GPIO_2为输出模式
GpioSetDir(WIFI_IOT_GPIO_IDX_2, WIFI_IOT_GPIO_DIR_OUT);
while (1)
{
//设置GPIO_2输出高电平点亮LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 1);
//延时1s
usleep(1000000);
//设置GPIO_2输出低电平熄灭LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 0);
//延时1s
usleep(1000000);
}
}
二、GPIO中断
1. 常用函数
1.1 GpioRegisterIsrFunc 设置GPIO引脚中断功能
/**
* @param GPIO ID.
* @param 触发模式,电平触发,边沿触发
* @param 触发方式.高低电平,下降沿上升沿
* @param 中断回调函数
* @param arg Indicates the pointer to the argument used in the interrupt callback function.
*/
unsigned int GpioRegisterIsrFunc(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity,
GpioIsrCallbackFunc func, char *arg);
/*示例*/
/*11引脚,边沿触发,下降沿触发*/
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1_Pressed, NULL);
1.2 GpioUnregisterIsrFunc 取消GPIO引脚中断功能
unsigned int GpioUnregisterIsrFunc(WifiIotGpioIdx id);
1.3 GpioSetIsrMask 屏蔽GPIO引脚中断功能
/*
* @param id Indicates the GPIO pin ID.
* @param mask 1屏蔽,0不屏蔽
* Indicates whether the interrupt function is masked.
* The value <b>1</b> means to mask the interrupt function,
* and <b>0</b> means not to mask the interrupt function.
*/
unsigned int GpioSetIsrMask(WifiIotGpioIdx id, unsigned char mask);
1.4 GpioSetIsrMode 设置GPIO引脚中断触发模式
/*
* @param id Indicates the GPIO pin ID.
* @param intType Indicates the interrupt type.
* @param intPolarity Indicates the interrupt polarity.
*/
/*GPIO ID,触发类型,触发方式*/
unsigned int GpioSetIsrMode(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity);
2. 示例代码
static void F1_Pressed(char *arg)
{
(void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
}
static void F2_Pressed(char *arg)
{
(void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
}
static void ButtonExampleEntry(void)
{
GpioInit();
//初始化LED灯
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
//初始化F1按键,设置为下降沿触发中断
//设置引脚复用功能
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
//设置引脚方向
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_IN);
//设置为内部上拉
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
//设置中断,边沿触发,下降沿触发
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1_Pressed, NULL);
//初始化F2按键,设置为下降沿触发中断
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2_Pressed, NULL);
}
三、PWM输出
1. 常用函数
1.1 PwmInit 初始化
/**
* @param port Indicates the PWM port number.
* 在对应pwm引脚初始化复用功能中查找对应port number
*/
unsigned int PwmInit(WifiIotPwmPort port);
1.2 PWM PwmDeinit 取消初始化
unsigned int PwmDeinit(WifiIotPwmPort port);
1.3 PWM PwmStart 根据输入参数输出
/**
* @param port Indicates the PWM port number.
* @param duty Indicates the PWM duty cycle.
* @param freq Indicates the frequency-division multiple.
*/
/*端口号,占空比,分频系数*/
unsigned int PwmStart(WifiIotPwmPort port, unsigned short duty, unsigned short freq);
PwmStart(WIFI_IOT_PWM_PORT_PWM2, 0, 40000);//最暗
PwmStart(WIFI_IOT_PWM_PORT_PWM2, 40000, 40000);//最亮
1.4 PWM PwmStop 停止PWM输出
unsigned int PwmStop(WifiIotPwmPort port);
2. 示例代码
static void PWMTask(void)
{
unsigned int i;
//初始化GPIO
GpioInit();
//设置GPIO_2引脚复用功能为PWM
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_PWM2_OUT);
//设置GPIO_2引脚为输出模式
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
//初始化PWM2端口
PwmInit(WIFI_IOT_PWM_PORT_PWM2);
while (1)
{
for (i = 0; i < 40000; i += 100)
{
//输出不同占空比的PWM波
PwmStart(WIFI_IOT_PWM_PORT_PWM2, i, 40000);
usleep(10);
}
i = 0;
}
}
四、ADC采集
1. 常用函数
根据输入参数从指定的ADC通道读取一段采样数据
/**
* @param channel Indicates the ADC channel index.
* @param data Indicates the pointer to the address for storing the read data.
* @param equModel Indicates the equation model.
* @param curBais Indicates the analog power control mode.
* @param rstCnt Indicates the count of the time from reset to conversion start.
* One count is equal to 334 ns. The value must range from 0 to 0xFF0.
*/
/* channel ADC通道 在引脚功能复用中可以查找
* data 指示用于存放读取数据的地址的指针
* equModel 表示平均算法的次数
* curBais 表示模拟功率控制模式 电源电压
* rstCnt 指示从重置到转换开始的时间计数 */
unsigned int AdcRead(WifiIotAdcChannelIndex channel, unsigned short *data, WifiIotAdcEquModelSel equModel,
WifiIotAdcCurBais curBais, unsigned short rstCnt);
2. 示例代码
/***** 获取电压值函数 *****/
static float GetVoltage(void)
{
unsigned int ret;
unsigned short data;
ret = AdcRead(WIFI_IOT_ADC_CHANNEL_5, &data, WIFI_IOT_ADC_EQU_MODEL_8, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0xff);
if (ret != WIFI_IOT_SUCCESS)
{
printf("ADC Read Fail\n");
}
return (float)data * 1.8 * 4 / 4096.0;
}
static void ADCTask(void)
{
float voltage;
//上拉,让按键未按下时GPIO_11保持高电平状态
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
while (1)
{
printf("=======================================\r\n");
printf("***************ADC_example*************\r\n");
printf("=======================================\r\n");
//获取电压值
voltage = GetVoltage();
printf("vlt:%.3fV\n", voltage);
//延时1s
usleep(1000000);
}
}
五、IIC总线
1. 常用函数
1.1 I2cInit 初始化I2C
/**
* @param id Indicates the I2C device ID.(根据引脚复用信息可以确定对应ID号)
* @param baudrate Indicates the I2C baud rate.(通信速率,一般为100、400kbps)
*/
unsigned int I2cInit(WifiIotI2cIdx id, unsigned int baudrate);
1.2 I2cDeinit 取消I2C初始化
unsigned int I2cDeinit(WifiIotI2cIdx id);
1.3 I2cWrite 将数据写入到I2C设备
/**
* @param id Indicates the I2C device ID.
* @param deviceAddr Indicates the I2C device address.
* @param i2cData Indicates the pointer to the data descriptor to write.
*/
unsigned int I2cWrite(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData);
WifiIotI2cData nt3h1101_i2c_data1 = {0};
nt3h1101_i2c_data1.sendBuf = data;
nt3h1101_i2c_data1.sendLen = dataSend;
status = I2cWrite(WIFI_IOT_I2C_IDX_1, (NT3H1X_SLAVE_ADDRESS<<1)|0x00, &nt3h1101_i2c_data1);
if (status != 0)
{
printf("===== Error: I2C write status1 = 0x%x! =====\r\n", status);
return 0;
}
1.4 I2cRead 从I2C设备读取数据
/**
* @param id Indicates the I2C device ID.
* @param deviceAddr Indicates the I2C device address.
* @param i2cData Indicates the pointer to the data descriptor to read.
*/
unsigned int I2cRead(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData);
1.5 I2cWriteread 向I2C设备发送数据并接受数据响应
/**
* @param id Indicates the I2C device ID.
* @param deviceAddr Indicates the I2C device address.
* @param i2cData Indicates the pointer to the device descriptor of the data to receive.
*/
unsigned int I2cWriteread(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData);
WifiIotI2cData nt3h1101_i2c_data = {0};
uint8_t buffer[1] = {address};
/*发送的数据*/
nt3h1101_i2c_data.sendBuf = buffer;
nt3h1101_i2c_data.sendLen = 1;
/*接收的数据*/
nt3h1101_i2c_data.receiveBuf = block_data;
nt3h1101_i2c_data.receiveLen = NFC_PAGE_SIZE;
status = I2cWriteread(WIFI_IOT_I2C_IDX_1, (NT3H1X_SLAVE_ADDRESS<<1)|0x00, &nt3h1101_i2c_data);
if (status != 0)
{
printf("===== Error: I2C write status = 0x%x! =====\r\n", status);
return 0;
}
1.6 I2cSetBaudrate 设置I2C频率
/**
* @param id Indicates the I2C device ID.
* @param baudrate Indicates the I2C baud rate to set.
*/
unsigned int I2cSetBaudrate(WifiIotI2cIdx id, unsigned int baudrate);
I2cSetBaudrate(WIFI_IOT_I2C_IDX_1, 400000);
六、UART串口
1. 常用函数
1.1 UartInit 初始化
/**
* @param id Indicates the UART port number.
* @param param Indicates the pointer to the basic UART attributes.
* @param extraAttr Indicates the pointer to the extended UART attributes.扩转设置接收方式、DMA等
*/
unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr);
WifiIotUartAttribute uart_attr = {
.baudRate = 9600,
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
//Initialize uart driver
ret = UartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
if (ret != WIFI_IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
1.2 UART UartDeinit 取消UART初始化
unsigned int UartDeinit(WifiIotUartIdx id);
1.3 UartRead 从UART读取数据
/**
* @param id Indicates the UART port number.端口号
* @param data Indicates the pointer to the start address of the data to read.保存数据的指针
* @param dataLen Indicates the number of bytes to read.数据长度
*/
int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen);
1.4 UartWrite 将数据写入
/**
* @param id Indicates the UART port number.
* @param data Indicates the pointer to the start address of the data to write.
* @param dataLen Indicates the number of bytes to write.
*/
int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen);
1.5 UART UartSetFlowCtrl 设置UART流控制
/**
* @param id Indicates the UART port number.
* @param flowCtrl Indicates the parameter used for hardware flow control.
*/
unsigned int UartSetFlowCtrl(WifiIotUartIdx id, WifiIotFlowCtrl flowCtrl);
2. 示例代码
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
WifiIotUartAttribute uart_attr = {
//baud_rate: 9600
.baudRate = 9600,
//data_bits: 8bits
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
//Initialize uart driver
ret = UartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
if (ret != WIFI_IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART Test Start\n");
while (1)
{
printf("=======================================\r\n");
printf("*************UART_example**************\r\n");
printf("=======================================\r\n");
//通过串口1发送数据
UartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
//通过串口1接收数据
UartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
printf("Uart1 read data:%s", uart_buff_ptr);
usleep(1000000);
}
}