目录
- API
- 头文件
- 函数 (机翻)
- 宏
- GPIO驱动程序api返回的通用状态代码
- GPIO_PinConfig输出引脚配置宏
- GPIO_PinConfig输入引脚配置宏
- GPIO_PinConfig中断配置宏
- 特殊的GPIO_PinConfig配置宏
- 类型别名
- 示例
- 配置引脚示例
- 配置外部中断示例
- 上机实战
- 配置引脚
- main.c
- myTask.c
- myTask.h
- 实验结果
平台:Code Composer Studio 10.4.0
MSP432P401R SimpleLink™ 微控制器 LaunchPad™ 开发套件
(MSP-EXP432P401R)
API
GPIO API官方手册
头文件
#include <ti/drivers/GPIO.h>
函数 (机翻)
void GPIO_clearInt (uint_least8_t index)
清除GPIO管脚中断标志
void GPIO_disableInt (uint_least8_t index)
禁用GPIO脚中断
void GPIO_enableInt (uint_least8_t index)
使能GPIO脚中断
void GPIO_getConfig (uint_least8_t index, GPIO_PinConfig *pinConfig)
获取gpio引脚的当前配置
void GPIO_init ()
初始化GPIO模块
uint_fast8_t GPIO_read (uint_least8_t index)
读取GPIO管脚的值
void GPIO_setCallback (uint_least8_t index, GPIO_CallbackFxn callback)
绑定回调函数到GPIO脚中断
int_fast16_t GPIO_setConfig (uint_least8_t index, GPIO_PinConfig pinConfig)
配置gpio管脚
void GPIO_toggle (uint_least8_t index)
切换GPIO的当前状态
void GPIO_write (uint_least8_t index, unsigned int value)
将该值写入GPIO管脚
宏
GPIO驱动程序api返回的通用状态代码
#define GPIO_STATUS_RESERVED (-32)
//通用GPIO状态码保留偏移量
#define GPIO_STATUS_SUCCESS (0)
//GPI_setConfig()返回成功的状态代码
#define GPIO_STATUS_ERROR (-1)
//GPI_setConfig()返回的通用错误状态代码
GPIO_PinConfig输出引脚配置宏
#define GPIO_CFG_OUTPUT
引脚是输出引脚
#define GPIO_CFG_OUT_STD
引脚是是主动输出高/低
#define GPIO_CFG_OUT_OD_NOPULL
输出引脚是开漏模式
#define GPIO_CFG_OUT_OD_PU
输出引脚是开漏上拉
#define GPIO_CFG_OUT_OD_PD
输出引脚是开漏下拉
#define GPIO_CFG_OUT_STR_LOW
将输出引脚输出强度设置为低
#define GPIO_CFG_OUT_STR_MED
将输出引脚输出强度设置为中
#define GPIO_CFG_OUT_STR_HIGH
将输出引脚输出强度设置为高
#define GPIO_CFG_OUT_HIGH
设置引脚输出为1
#define GPIO_CFG_OUT_LOW
设置引脚输出为0
GPIO_PinConfig输入引脚配置宏
#define GPIO_CFG_INPUT
引脚是输入引脚
#define GPIO_CFG_IN_NOPULL
输入引脚不内部上拉\下拉
#define GPIO_CFG_IN_PU
输入引脚内部上拉
#define GPIO_CFG_IN_PD
输入引脚不内部下拉
GPIO_PinConfig中断配置宏
#define GPIO_CFG_IN_INT_NONE
没有中断
#define GPIO_CFG_IN_INT_FALLING
在下降沿触发中断
#define GPIO_CFG_IN_INT_RISING
在上升沿触发中断
#define GPIO_CFG_IN_INT_BOTH_EDGES
在上升沿和下降沿触发中断
#define GPIO_CFG_IN_INT_LOW
低电平触发中断
#define GPIO_CFG_IN_INT_HIGH
高电平触发中断
特殊的GPIO_PinConfig配置宏
#define GPIO_CFG_IN_INT_ONLY
//GPIO_PinConfig参数中以或运算通知GPIO_setConfig()只配置GPIO输入引脚的中断属性
#define GPIO_DO_NOT_CONFIG
//使用这个GPIO_PinConfig宏来通知GPIO_init()不要配置相应的pin
类型别名
typedef uint32_t GPIO_PinConfig
GPIO管脚配置参数
typedef void(* GPIO_CallbackFxn) (uint_least8_t index)
GPIO回调函数名
示例
// Import GPIO Driver definitions
#include <ti/drivers/GPIO.h>
// Define names for GPIO pin indexes
#define BUTTON 0
#define LED 1
// One-time init of GPIO driver
GPIO_init();
// Read GPIO pin
unsigned int state = GPIO_read(BUTTON);
// Write to GPIO pin
GPIO_write(LED, state);
配置引脚示例
// Driver header file
#include <ti/drivers/GPIO.h>
// TI Driver configuration
#include "ti_drivers_config.h"
#define LED CONFIG_GPIO_LED0
#define BUTTON CONFIG_GPIO_BUTTON0
void main()
{// One-time init of GPIO driverGPIO_init();// Configure a button input pinGPIO_setConfig(BUTTON, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);// Configure an LED output pinGPIO_setConfig(LED, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
}
配置外部中断示例
// Driver header file
#include <ti/drivers/GPIO.h>
// TI Drivers Configuration
#include "ti_drivers_config.h"
// Board file
#include <ti/drivers/Board.h>
// GPIO button call back function
void gpioButton0Fxn(uint_least8_t index);
main()
{// One-time Board initializationBoard_init();// One-time init of GPIO driverGPIO_init();// Turn on user LEDGPIO_write(CONFIG_GPIO_LED0, CONFIG_GPIO_LED_ON);// install Button callbackGPIO_setCallback(CONFIG_GPIO_BUTTON0, gpioButton0Fxn);// Enable interruptsGPIO_enableInt(CONFIG_GPIO_BUTTON0);
}
//
// ======== gpioButton0Fxn ========
// Callback function for the GPIO interrupt on CONFIG_GPIO_BUTTON0
//
// Note: index is the GPIO id for the button which is not used here
//
void gpioButton0Fxn(uint_least8_t index)
{// Toggle the LEDGPIO_toggle(CONFIG_GPIO_LED0);
}
上机实战
配置引脚
继续在syscfg文件中配置引脚
build后会在
中生成对应的映射
并在GPIO_init()中自动地初始化,因此若是不需要动态修改引脚配置,调用GPIO_init()就行。
main.c
实现两个回调函数,并在mainThread中启用中断
/** ======== main_tirtos.c ========*/
#include <stdint.h>/* POSIX Header files */
#include <pthread.h>/* RTOS header files */
#include <ti/sysbios/BIOS.h>/* Driver configuration */
#include <ti/drivers/Board.h>
#include <ti/drivers/GPIO.h>#include "./inc/myTask.h"/* Driver configuration */
#include "ti_drivers_config.h"void S1_Callback_Function(void)
{usleep(100000);if(!GPIO_read(S1)){GPIO_toggle(LED1);while(!GPIO_read(S1));}
}void S2_Callback_Function(void)
{usleep(100000);if(!GPIO_read(S2)){GPIO_toggle(LED2);while(!GPIO_read(S2));}
}/** ======== main ========*/
int main(void)
{/* Call driver init functions */Board_init();GPIO_init();My_Task_Init(mainThread, 1, 1024);BIOS_start();return (0);
}/** ======== mainThread ========*/
void *mainThread(void *arg0)
{GPIO_enableInt(S1);GPIO_enableInt(S2);while(1){}return (NULL);
}
本次实验不需要建立其他任务
myTask.c
/** myTask.c** Created on: 2021年8月2日* Author: Royic*/#include <stdint.h>/* POSIX Header files */
#include <pthread.h>/* RTOS header files */
#include <ti/sysbios/BIOS.h>/* Driver configuration */
#include "ti_drivers_config.h"#include "./inc/myTask.h"/* Driver Header files */
#include <ti/drivers/GPIO.h>
// #include <ti/drivers/I2C.h>
// #include <ti/drivers/SPI.h>
// #include <ti/drivers/UART.h>
// #include <ti/drivers/Watchdog.h>void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize)
{pthread_t thread;pthread_attr_t attrs;struct sched_param priParam;int retc;/* Initialize the attributes structure with default values */pthread_attr_init(&attrs);/* Set priority, detach state, and stack size attributes */priParam.sched_priority = priority;retc = pthread_attr_setschedparam(&attrs, &priParam);retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);retc |= pthread_attr_setstacksize(&attrs, stacksize);if (retc != 0){/* failed to set attributes */while (1){}}retc = pthread_create(&thread, &attrs, startroutine, NULL);if (retc != 0){/* pthread_create() failed */while (1){}}
}
myTask.h
/** myTask.h** Created on: 2021年8月2日* Author: Royic*/#ifndef INC_MYTASK_H_
#define INC_MYTASK_H_/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>void *mainThread(void *arg0);
void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);#endif /* INC_MYTASK_H_ */
实验结果
按下S1 LED1反转,按下S2 LED2反转