软件版本:5.0.4
编辑器: Keil5
基于工程:\5.0.4\projects\target_apps\ble_examples\ble_app_all_in_one\Keil_5 修改
案例工程下载:https://download.csdn.net/download/jiangchao3392/10833767
添加一个任务 LED灯定时闪烁的任务:
现象: 在连接上蓝牙后, 大概等待5~6秒后, LED开始闪烁。
任务添加于基于系统本身的一个TASK_APP任务:
app,c-----772 行: (不需改动)
void app_init(void)
{
// Reset the environment
memset(&app_env[0], 0, sizeof(app_env));
for (uint8_t i = 0; i < APP_EASY_MAX_ACTIVE_CONNECTION; i++)
{
// Set true by default (several profiles requires security)
app_env[i].sec_en = true;
}
// Create APP task
ke_task_create(TASK_APP, &TASK_DESC_APP);
// Initialize Task state
ke_state_set(TASK_APP, APP_DISABLED);
}
创建了一个TASK_DESC_APP的任务:
app.c-----107行: (不需改动)
static const struct ke_task_desc TASK_DESC_APP = {NULL,
&app_default_handler,
app_state,
APP_STATE_MAX,
APP_IDX_MAX};
&app_default_handler 引用自:
app_task.c------442行
/* Specifies the message handlers that are common to all states. */
const struct ke_state_handler app_default_handler = KE_STATE_HANDLER(app_default_state);
app_state引用自:
app_task.c------445行
/* Defines the place holder for the states of all the task instances. */
ke_state_t app_state[APP_IDX_MAX] __attribute__((section("retention_mem_area0"), zero_init)); //RETENTION MEMORY
其中,ke_state_t 宏定义为 uint8:
ke_msg.h -----70行
/// Task State
typedef uint8_t ke_state_t;
APP_STATE_MAX引用自:
app_task.h-----76行
/// States of APP task
enum APP_STATE
{
·
·
·
/// Number of defined states.
APP_STATE_MAX
};
APP_IDX_MAX定义自:
app_task.h-----49行
/// Number of APP Task Instances
#define APP_IDX_MAX (1)
1, app_task.c
在app_task.c 中默认只有一个默认的:
/* Default State handlers definition. */
const struct ke_msg_handler app_default_state[] =
{
{KE_MSG_DEFAULT_HANDLER, (ke_msg_func_t)app_entry_point_handler},
};
在下面添加一条:
{APP_TIMER_API_MES1, (ke_msg_func_t)app_second_timer_handler},
变为:
/* Default State handlers definition. */
const struct ke_msg_handler app_default_state[] =
{
{KE_MSG_DEFAULT_HANDLER, (ke_msg_func_t)app_entry_point_handler},
{APP_TIMER_API_MES1, (ke_msg_func_t)app_second_timer_handler},
};
2,在APP_TIMER_API_MES1 的定义 在app.h中 含有APP_TIMER_API_MES1, 不用修改
/*
* TYPE DEFINITIONS
****************************************************************************************
*/
/// APP Task messages
enum APP_MSG
{
APP_MODULE_INIT_CMP_EVT = KE_FIRST_MSG(TASK_APP),
#if BLE_PROX_REPORTER
APP_PROXR_TIMER,
#endif //BLE_PROX_REPORTER
#if BLE_BAS_SERVER
APP_BASS_TIMER,
APP_BASS_ALERT_TIMER,
#endif //BLE_BAS_SERVER
#if HAS_MULTI_BOND
APP_ALT_PAIR_TIMER,
#endif //HAS_MULTI_BOND
APP_CREATE_TIMER,
APP_CANCEL_TIMER,
APP_MODIFY_TIMER,
//Do not alter the order of the next messages
//they are considered a range
APP_TIMER_API_MES0,
APP_TIMER_API_MES1=APP_TIMER_API_MES0+1,
APP_TIMER_API_MES2=APP_TIMER_API_MES0+2,
APP_TIMER_API_MES3=APP_TIMER_API_MES0+3,
APP_TIMER_API_MES4=APP_TIMER_API_MES0+4,
APP_TIMER_API_MES5=APP_TIMER_API_MES0+5,
APP_TIMER_API_MES6=APP_TIMER_API_MES0+6,
APP_TIMER_API_MES7=APP_TIMER_API_MES0+7,
APP_TIMER_API_MES8=APP_TIMER_API_MES0+8,
APP_TIMER_API_MES9=APP_TIMER_API_MES0+9,
APP_TIMER_API_LAST_MES=APP_TIMER_API_MES9,
APP_MSG_UTIL_API_MES0,
APP_MSG_UTIL_API_MES1=APP_MSG_UTIL_API_MES0+1,
APP_MSG_UTIL_API_MES2=APP_MSG_UTIL_API_MES0+2,
APP_MSG_UTIL_API_MES3=APP_MSG_UTIL_API_MES0+3,
APP_MSG_UTIL_API_MES4=APP_MSG_UTIL_API_MES0+4,
APP_MSG_UTIL_API_LAST_MES=APP_MSG_UTIL_API_MES4
};
2.创建2个文件,如下所示:
添加一个.C文件
user_led.c内容如下
#include "user_led.h"
#include "app.h"
#include "gpio.h"
#include "user_periph_setup.h"
#define GPIO_LED1_PORT GPIO_PORT_2
#define GPIO_LED1_PIN GPIO_PIN_0
void Led_init(void)
{
// GPIO_ConfigurePin(GPIO_LED1_PORT,GPIO_LED1_PIN,OUTPUT,PID_GPIO,false);
}
int app_second_timer_handler(ke_msg_id_t const msgid,
void const *param,
ke_task_id_t const dest_id,
ke_task_id_t const src_id)
{
static bool flag = 0 ;
if(flag == 0)
{
flag = 1 ;
GPIO_SetActive (GPIO_LED_PORT,GPIO_LED_PIN);
}
else
{
flag = 0 ;
GPIO_SetInactive(GPIO_LED_PORT,GPIO_LED_PIN);
}
// while(1);
app_timer_set(APP_TIMER_API_MES1,TASK_APP,10);
return (KE_MSG_CONSUMED);
}
led的闪烁间隔:10*10ms=100ms
3.在usar_periph_setup.c里面
第85行,以定义: GPIO_ConfigurePin(GPIO_LED_PORT, GPIO_LED_PIN, OUTPUT, PID_GPIO, false);
LED的端口进行了初始化。
void set_pad_functions(void) // set gpio port function mode
{
#ifdef CFG_PRINTF_UART2
GPIO_ConfigurePin(GPIO_UART2_TX_PORT, GPIO_UART2_TX_PIN, OUTPUT, PID_UART2_TX, false);
GPIO_ConfigurePin(GPIO_UART2_RX_PORT, GPIO_UART2_RX_PIN, INPUT, PID_UART2_RX, false);
#endif
GPIO_ConfigurePin(GPIO_BUTTON_PORT, GPIO_BUTTON_PIN, INPUT_PULLUP, PID_GPIO, false);
GPIO_ConfigurePin(GPIO_PWM_PORT, GPIO_PWM_PIN, OUTPUT, PID_PWM0, false);
GPIO_ConfigurePin(GPIO_LED_PORT, GPIO_LED_PIN, OUTPUT, PID_GPIO, false);
4,在usar_periph_setup.c里面,修改GPIO_LED_PORT, GPIO_LED_PIN ,对应的端口
#elif HW_CONFIG_PRO_DK
#define GPIO_LED_PORT GPIO_PORT_2
#define GPIO_LED_PIN GPIO_PIN_0
5,在#include "app_default_handlers.c"中找到广播开始函数 开启定时的led的事件
/*
* FUNCTION DEFINITIONS
****************************************************************************************
*/
void default_advertise_operation(void)
{
if (user_default_hnd_conf.adv_scenario == DEF_ADV_FOREVER)
{
app_easy_gap_undirected_advertise_start();
}
else if (user_default_hnd_conf.adv_scenario == DEF_ADV_WITH_TIMEOUT)
{
app_easy_gap_undirected_advertise_with_timeout_start(user_default_hnd_conf.advertise_period, NULL);
}
app_timer_set(APP_TIMER_API_MES1,TASK_APP,100); //开启led时间函数
}
等待 100*10ms就会跳到app_second_timer_handler函数,开始led的闪烁
参考:https://blog.csdn.net/a369000753/article/details/52611586