简介
服务端程序架构
- 工程初始化
- 添加头文件路径
- 指定代码链接空间
- 驱动
- 添加驱动文件
- 驱动初始化处理
应用
-
指定代码链接空间
不指定会出现空间不足的故障
修改“Options for Target ‘Target 1’ -> Linker -> Edit”里面的: :::tips LR_ROM_XIP 0x1101b000 0x01e000 {
ER_ROM_XIP 0x1101b000 0x01e000 { ; load address = execution address
.
.
.
oled.o(+RO)
fs.o(+RO)
.
.
.
}
}
:::
添加驱动文件
- driver
- oled.c
- bsp_button.c
- bsp_button_tack.c
- bsp_gpio.c
blemesh
-
驱动初始化处理
led处理:
修改gpio为没有用到的引脚#if (SDK_VER_CHIP == __DEF_CHIP_QFN32__)
#define GPIO_GREEN P7 //重点
#define GPIO_BLUE P31//重点
#define GPIO_RED P14//重点
#else
#define GPIO_GREEN P3
#define GPIO_BLUE P7
#define GPIO_RED P2
#endif
入网和退网
入网:
如果设备还没入网,重新上电就会广播入网请求if (MS_TRUE == is_prov_req)
{
CONSOLE_OUT("\r\n outline \r\n");
osal_start_timerEx(Demo_TaskID, BTN_DEMO_OUTLINE_EVT,1);
/* Start Provisioning over GATT here */
/**
setup <role:[1 - Device, 2 - Provisioner]> <bearer:[1 - Adv, 2 - GATT]
*/
role = PROV_ROLE_DEVICE;
brr = PROV_BRR_GATT|PROV_BRR_ADV; //PROV_BRR_ADV,PROV_BRR_GATT
printf("Bearer type = 0x%02X(Bit0-adv, Bit1-GATT)\r\n", brr);
// UI_prov_brr_handle = brr;
/**
Setting up an Unprovisioned Device over GATT
*/
LIGHT_ONLY_RED_ON;
blebrr_prov_started = MS_FALSE;
UI_setup_prov(role, brr);//广播入网请求 //重点
// UI_prov_bind(brr, 0x00);
//ms_access_ps_store(MS_PS_RECORD_SEQ_NUMBER);
CONSOLE_OUT("\r\n Setting up as an Unprovisioned Device\r\n");
}
退网:
长按确认键,重置nvs数据并广播入网请求case BSP_BTN_LPK_TYPE:
CONSOLE_OUT("long press keep ");
if(line_state)
{
key_long_flag = 1;
if(set_state==0)
{
set_state=1;
oled_flash_x();
}
else
{
if(BSP_BTN_INDEX(evt)==0)
{
if(index<2)index++;
else index=0;
oled_flash_x();
}
if(BSP_BTN_INDEX(evt)==2)
{
if(index>0)index--;
else index=2;
oled_flash_x();
}
if(BSP_BTN_INDEX(evt)==1)//重点
{
osal_start_timerEx(Demo_TaskID, BTN_DEMO_OUTLINE_EVT, 1);
out_link();
}
}
osal_start_timerEx(Demo_TaskID, BTN_DEMO_SET_TIMEOUT_EVT, 10*1000);
}
break;
void out_link()
{
UCHAR proxy_state;
UCHAR role, brr;
proxy_state = UI_proxy_state_get();
nvs_reset(NVS_BANK_PERSISTENT);
role = PROV_ROLE_DEVICE;
brr = PROV_BRR_GATT|PROV_BRR_ADV; //PROV_BRR_ADV,PROV_BRR_GATT
blebrr_prov_started = MS_FALSE;
UI_setup_prov(role, brr);
if((ms_iv_index.iv_expire_time!=0)&&(ms_iv_index.iv_expire_time!=0xffffffff))
{
MS_net_start_iv_update_timer(ms_iv_index.iv_update_state,MS_TRUE);
}
}
设置设备号
1.短按“+/-”键来增加或减少数字,长按“+/-”键来选这个十百位数
2.短按“确定”键确定设备号 ```c uint16_t derver_num//设备号,全局变量 void hal_bsp_btn_callback(uint8_t evt) { CONSOLE_OUT(“gpio evt:0x%x “,evt); char str[10]; uint8_t buf[4]; switch(BSP_BTN_TYPE(evt)) { case BSP_BTN_PD_TYPE: CONSOLE_OUT(“press down “); break;
-
case BSP_BTN_UP_TYPE:
CONSOLE_OUT("press up ");
if(line_state)
{
if(set_state==0)
{
set_state=1;
oled_flash_x();
}
else
{
if(key_long_flag==0)
{
//--------------------------------------------key0------------------------------------//
if(BSP_BTN_INDEX(evt)==0)
{
if(index==0)
{
if((derver_num%10)<9)derver_num+=1;
else derver_num-=9;
}
if(index==1)
{
if((derver_num/10)%10<9)derver_num+=10;
else derver_num-=90;
}
if(index==2)
{
if((derver_num/100)<9)derver_num+=100;
else derver_num-=900;
}
oled_flash_x();
}
//--------------------------------------------key2------------------------------------//
if(BSP_BTN_INDEX(evt)==2)
{
if(index==0)
{
if((derver_num%10)>0)derver_num-=1;
else derver_num+=9;
}
if(index==1)
{
if((derver_num/10)%10>0)derver_num-=10;
else derver_num+=90;
}
if(index==2)
{
if((derver_num/100)>0)derver_num-=100;
else derver_num+=900;
}
oled_flash_x();
}
//--------------------------------------------key1------------------------------------//
if(BSP_BTN_INDEX(evt)==1)
{
set_out();
}
}
else
{
key_long_flag=0;
}
}
osal_start_timerEx(Demo_TaskID, BTN_DEMO_SET_TIMEOUT_EVT, 10*1000);
}
break;
设备号通过fs文件系统保存到flash里面([【PHY6252-SDK-components】NVM的使用(存储参数)](https://www.yuque.com/agui-lab/zauxto/udgg4k?view=doc_embed))
```c
static void set_out()
{
set_state=0;
osal_snv_write(123, 2, &derver_num);
OLED_ShowString(18,24," ",12,0);OLED_ShowString(42,24," ",12,0);OLED_ShowString(30,24," ",12,0);
OLED_Refresh();
}
vendor模型
vendor回调处理:
1.接收到来自客户端发来的MS_ACCESS_VENDORMODEL_NOTIFY_OPCODE数据
2.根据截取的数据段,来判断是否使用DIDO控制(bsp_set_DO,bsp_get_DI)
3.如ack数据段为1,需返回相应的应答数据给客户端
case MS_ACCESS_VENDORMODEL_NOTIFY_OPCODE:
{
UINT8 ack;
uint16 index;
UINT8 ch;
UINT8 val;
uint32 delayms;
UINT8 mode;
data_param = state_params->vendormodel_param;
marker = 0;
MS_UNPACK_LE_1_BYTE(&ack, data_param+marker);
marker += 1;
MS_UNPACK_LE_2_BYTE(&index, data_param+marker);
marker += 2;
MS_UNPACK_LE_1_BYTE(&ch, data_param+marker);
marker += 1;
MS_UNPACK_LE_1_BYTE(&val, data_param+marker);
marker += 1;
MS_UNPACK_LE_4_BYTE(&delayms, data_param+marker);
marker += 4;
MS_UNPACK_LE_1_BYTE(&mode, data_param+marker);
marker += 1;
CONSOLE_OUT("[PDU_Rx] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
ack,index,ch,val,delayms,mode);
if(index == derver_num)
{
if((ch==0)||(ch==1))
{
CONSOLE_OUT("OPEN_lock:%d\n",ch);
bsp_set_DO(ch,val,delayms,mode);
}
if(ack)
{
marker = 0;
MS_PACK_LE_1_BYTE_VAL(data_param+marker,++vendor_tid);
marker+=1;
MS_PACK_LE_1_BYTE_VAL(data_param+marker,ack);
marker+=1;
MS_PACK_LE_2_BYTE_VAL(data_param+marker,index);
marker+=2;
MS_PACK_LE_1_BYTE_VAL(data_param+marker,ch);
marker+=1;
if((ch==2)||(ch==3))
{
val = get_DI(ch-1);
CONSOLE_OUT("GET_DI:%d,val:%d\n",ch,val);
}
MS_PACK_LE_1_BYTE_VAL(data_param+marker,val);
marker+=1;
MS_PACK_LE_4_BYTE_VAL(data_param+marker,delayms);
marker+=4;
MS_PACK_LE_1_BYTE_VAL(data_param+marker,mode);
marker+=1;
state_params->vendormodel_param = data_param;
req_type->to_be_acked = 0x01;
CONSOLE_OUT("[PDU_Tx] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
ack,index,ch,val,delayms,mode);
}
}
}
break;
控制DO,同时设置DO的常闭常开
void bsp_set_DO(uint8_t ch,uint8_t val,uint32_t delayms,uint8_t mode)
{
if(ch==0)ch_buf = DO1_PIN;
else ch_buf = DO2_PIN;
val_buf = val;
if(delayms)
{
hal_gpio_fast_write(ch_buf,val_buf);
osal_start_timerEx(Demo_TaskID, BTN_DEMO_DO_EVT, delayms);
}
else
{
hal_gpio_fast_write(ch_buf,val);
}
if((ch==0)&&(DO1_mode!=mode))
{
DO1_mode = mode;
osal_snv_write(126, 1, &DO1_mode);
osal_snv_read(126, 1, &DO1_mode);
CONSOLE_OUT("set_do1_mode:%d\n",DO1_mode);
}
else if((ch==1)&&(DO2_mode!=mode))
{
DO2_mode = mode;
osal_snv_write(127, 1, &DO2_mode);
osal_snv_read(127, 1, &DO2_mode);
CONSOLE_OUT("set_do2_mode:%d\n",DO2_mode);
}
}
获取DI
bool get_DI(uint8 di)
{
if(di==1)
{
return hal_gpio_read(DI1_PIN);
}
else
{
return hal_gpio_read(DI2_PIN);
}
return 0;
}