主题中讨论的其他器件:SysConfig、
您好!
我将使用 CC studio 12.6并讲解 SimplelLink DATA_STREAM 示例。
当我在 syscfg 文件中配置 GPIO 时、我看不到回调函数选项。 是否可以手动将函数链接到生成的 GPIO?


谢谢
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Joseph、您好!
请从应用代码中配置按钮回调、例如按照 basic_ble menu_module.c 的示例进行操作
bStatus_t MenuModule_initButtons(void)
{
Button_Params params;
Button_Params_init(¶ms);
// Set the buttons parameters
params.buttonEventMask = Button_EV_CLICKED | Button_EV_LONGPRESSED;
params.buttonCallback = MenuModule_buttonsCallback;
params.longPressDuration = MENU_MODULE_LONG_PRESS_DURATION;
params.debounceDuration = MENU_MODULE_DEBOUNCE_DURATION;
// Open the buttons
handle_left = Button_open(CONFIG_BUTTON_0, ¶ms);
handle_right = Button_open(CONFIG_BUTTON_1, ¶ms);
if(handle_left == NULL || handle_right == NULL)
{
return FAILURE;
}
return SUCCESS;
}
这是 TI 驱动程序 参考按钮、但您也可以查看 GPIO.h。
此致、
瑞安
以下是 Button.h 提供的示例
#include <ti/drivers/apps/Button.h>
int main(void)
{
Button_Params params;
Button_Handle handle;
Button_Params_init(¶ms);
handle = Button_open(CONFIG_BUTTON0, buttonCallbackFxn, ¶ms);
...
}
void buttonCallbackFxn(Button_Handle handle, Button_EventMask events)
{
if (events & Button_EV_CLICKED)
{
// Received a click, handle app condition 0 etc
handleAppCond(0);
}
if (events & Button_EV_LONGCLICKED)
{
// Long press, handle app condition 1 etc
handleAppCond(1);
}
...
}
此处、假定已在 SysConfig 中声明 CONFIG_BUTTON0。 您可以从 您启动的 data_stream 示例执行此操作、因为按钮操作不需要空示例。
此致、
瑞安
谢谢你。 button_open fcn 我会收到错误、因为它仅需要两个参数、
Button_Handle button_open (uint_least8_t buttonIndex、Button_Params * params);
我通过手动编辑 params.buttonCallback 规避了这一点。 但这也不起作用。 以下是我的代码。
/*
* ======== empty.c ========
*/
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/apps/Button.h>
// #include <ti/drivers/I2C.h>
// #include <ti/drivers/SPI.h>
// #include <ti/drivers/Watchdog.h>
/* Driver configuration */
#include "ti_drivers_config.h"
/* POSIX Header files */
#include <pthread.h>
/* RTOS header files */
#include <FreeRTOS.h>
#include <task.h>
/* Stack size in bytes */
#define THREADSTACKSIZE 1024
/*external functions */
extern void IncrementGatt();
extern void DecrementGatt();
void buttonCallbackFxn1(Button_Handle handle_left, Button_EventMask events)
{
if (events & Button_EV_CLICKED)
{
// Received a click, handle app condition 0 etc
IncrementGatt();
}
if (events & Button_EV_LONGCLICKED)
{
// Long press, handle app condition 1 etc
IncrementGatt();
}
}
void buttonCallbackFxn2(Button_Handle handle_right, Button_EventMask events)
{
if (events & Button_EV_CLICKED)
{
// Received a click, handle app condition 0 etc
DecrementGatt();
}
if (events & Button_EV_LONGCLICKED)
{
// Long press, handle app condition 1 etc
DecrementGatt();
}
}
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
/* 1 second delay */
uint32_t time = 1;
/* Call driver init functions */
Button_Params params1;
Button_Params params2;
Button_Handle handle_left;
Button_Handle handle_right;
Button_Params_init(¶ms1);
Button_Params_init(¶ms2);
params1.buttonCallback = buttonCallbackFxn1;
params2.buttonCallback = buttonCallbackFxn2;
handle_left = Button_open(CONFIG_BUTTON_0, ¶ms1);
handle_right = Button_open(CONFIG_BUTTON_1, ¶ms2);
GPIO_init();
// I2C_init();
// SPI_init();
// Watchdog_init();
/* Configure the LED pin */
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Turn on user LED */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
/* Enable interrupts */
GPIO_enableInt(CONFIG_GPIO_BUTTON_0_INPUT); //Add this Line!
GPIO_enableInt(CONFIG_GPIO_BUTTON_1_INPUT); //Add this Line!
while (1)
{
sleep(time);
GPIO_toggle(CONFIG_GPIO_LED_0);
}
}
void emptyMain(void)
{
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 = 5; // Lower the priority of this task
retc = pthread_attr_setschedparam(&attrs, &priParam);
retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);
if (retc != 0)
{
/* failed to set attributes */
while (1) {}
}
retc = pthread_create(&thread, &attrs, mainThread, NULL);
if (retc != 0)
{
/* pthread_create() failed */
while (1) {}
}
}
/*********************************************************************
* @fn BLEConnectionEstablished
*
* @brief Called when a Bluetooth connection has been established
* with a peer device.
*
* @param None
*
* @return None.
*/
void BLEConnectionEstablished(void)
{
// Indicate connection by turning on the green LED
GPIO_write(CONFIG_GPIO_LED_1, CONFIG_LED_ON);
}
/*********************************************************************
* @fn BLEConnectionTerminated
*
* @brief Called when the Bluetooth connection has been terminated.
*
* @param None
*
* @return None.
*/
void BLEConnectionTerminated(void)
{
// Indicate disconnection by turning off the LED
GPIO_write(CONFIG_GPIO_LED_1, CONFIG_LED_OFF);
}
/*********************************************************************
* @fn evaluateNewCharacteristicValue
*
* @brief Based on the new value of a given characteristic determine
* if the LED should be turned off or on.
*
* @param newValue: Value of the characteristic to consider
*
* @return None.
*/
void evaluateNewCharacteristicValue(uint8_t newValue)
{
// If the new value of the characteristic is 0, then we turn off the red LED
if(newValue == 0)
{
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_LED_OFF);
}
else
{
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_LED_ON);
}
}
IncrementGatt 和 DecrementGatt 函数是外部函数、如下
void IncrementGatt()
{
Count++;
SimpleGattProfile_setParameter(SIMPLEGATTPROFILE_CHAR1, sizeof(uint8_t), &Count);
}
void DecrementGatt()
{
if(Count > 0){
Count--;
}
SimpleGattProfile_setParameter(SIMPLEGATTPROFILE_CHAR1, sizeof(uint8_t), &Count);
}看到关于 button_open 参数的要点。 这是通过填充回调作为 Button_Params 的一部分来解决的(Button_Callback buttonCallback)。 下面是我验证的适用于我的设置的代码:
/*
* Copyright (c) 2015-2019, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== empty.c ========
*/
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/apps/Button.h>
/* Driver configuration */
#include "ti_drivers_config.h"
void button1CallbackFxn(Button_Handle handle, Button_EventMask events)
{
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
}
void button2CallbackFxn(Button_Handle handle, Button_EventMask events)
{
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
}
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
/* 1 second delay */
uint32_t time = 1;
/* Call driver init functions */
GPIO_init();
/* Configure the LED pin */
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Turn on user LED */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
Button_Params params;
Button_Handle button1Handle;
Button_Handle button2Handle;
Button_Params_init(¶ms);
params.buttonCallback = button1CallbackFxn;
button1Handle = Button_open(CONFIG_BUTTON_0, ¶ms);
params.buttonCallback = button2CallbackFxn;
button2Handle = Button_open(CONFIG_BUTTON_1, ¶ms);
while (1)
{
sleep(time);
}
}
请注意、我还向 SysConfig 文件添加了两个按钮实例、每个实例均连接到 LP 硬件: Launchpad 按钮 BTN-1 (左)或 LaunchPad 按钮 BTN-2 (右)

此致、
瑞安