first submission
This commit is contained in:
@@ -0,0 +1,626 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app.c
|
||||
* Author : WCH
|
||||
* Version : V1.1
|
||||
* Date : 2021/11/10
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#include "CONFIG.h"
|
||||
#include "MESH_LIB.h"
|
||||
#include "app_vendor_model.h"
|
||||
#include "app_generic_onoff_model.h"
|
||||
#include "app.h"
|
||||
#include "HAL.h"
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
#define ADV_TIMEOUT K_MINUTES(10)
|
||||
|
||||
#define SELENCE_ADV_ON 0x01
|
||||
#define SELENCE_ADV_OF 0x00
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
|
||||
static uint8_t MESH_MEM[1024 * 2] = {0};
|
||||
|
||||
extern const ble_mesh_cfg_t app_mesh_cfg;
|
||||
extern const struct device app_dev;
|
||||
|
||||
static uint8_t App_TaskID = 0; // Task ID for internal task/event processing
|
||||
|
||||
static uint16_t App_ProcessEvent(uint8_t task_id, uint16_t events);
|
||||
|
||||
#if(!CONFIG_BLE_MESH_PB_GATT)
|
||||
NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, 65);
|
||||
#endif /* !PB_GATT */
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCION
|
||||
*/
|
||||
|
||||
static void link_open(bt_mesh_prov_bearer_t bearer);
|
||||
static void link_close(bt_mesh_prov_bearer_t bearer, uint8_t reason);
|
||||
static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index);
|
||||
static void prov_reset(void);
|
||||
|
||||
static struct bt_mesh_cfg_srv cfg_srv = {
|
||||
.relay = BLE_MESH_RELAY_ENABLED,
|
||||
.beacon = BLE_MESH_BEACON_ENABLED,
|
||||
#if(CONFIG_BLE_MESH_FRIEND)
|
||||
.frnd = BLE_MESH_FRIEND_ENABLED,
|
||||
#endif
|
||||
#if(CONFIG_BLE_MESH_PROXY)
|
||||
.gatt_proxy = BLE_MESH_GATT_PROXY_ENABLED,
|
||||
#endif
|
||||
.default_ttl = 3,
|
||||
|
||||
/* 8 transmissions with 10ms interval */
|
||||
.net_transmit = BLE_MESH_TRANSMIT(7, 10),
|
||||
.relay_retransmit = BLE_MESH_TRANSMIT(7, 10),
|
||||
};
|
||||
|
||||
static struct bt_mesh_health_srv health_srv;
|
||||
|
||||
BLE_MESH_HEALTH_PUB_DEFINE(health_pub, 8);
|
||||
|
||||
uint16_t cfg_srv_keys[CONFIG_MESH_MOD_KEY_COUNT_DEF] = {BLE_MESH_KEY_UNUSED};
|
||||
uint16_t cfg_srv_groups[CONFIG_MESH_MOD_GROUP_COUNT_DEF] = {BLE_MESH_ADDR_UNASSIGNED};
|
||||
|
||||
uint16_t health_srv_keys[CONFIG_MESH_MOD_KEY_COUNT_DEF] = {BLE_MESH_KEY_UNUSED};
|
||||
uint16_t health_srv_groups[CONFIG_MESH_MOD_GROUP_COUNT_DEF] = {BLE_MESH_ADDR_UNASSIGNED};
|
||||
|
||||
uint16_t gen_onoff_srv_keys[CONFIG_MESH_MOD_KEY_COUNT_DEF] = {BLE_MESH_KEY_UNUSED};
|
||||
uint16_t gen_onoff_srv_groups[CONFIG_MESH_MOD_GROUP_COUNT_DEF] = {BLE_MESH_ADDR_UNASSIGNED};
|
||||
|
||||
static struct bt_mesh_model root_models[] = {
|
||||
BLE_MESH_MODEL_CFG_SRV(cfg_srv_keys, cfg_srv_groups, &cfg_srv),
|
||||
BLE_MESH_MODEL_HEALTH_SRV(health_srv_keys, health_srv_groups, &health_srv, &health_pub),
|
||||
BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, gen_onoff_op, NULL, gen_onoff_srv_keys, gen_onoff_srv_groups, NULL),
|
||||
};
|
||||
|
||||
static struct bt_mesh_elem elements[] = {
|
||||
{
|
||||
/* Location Descriptor (GATT Bluetooth Namespace Descriptors) */
|
||||
.loc = (0),
|
||||
.model_count = ARRAY_SIZE(root_models),
|
||||
.models = (root_models),
|
||||
.vnd_model_count = ARRAY_SIZE(vnd_models),
|
||||
.vnd_models = (vnd_models),
|
||||
}
|
||||
};
|
||||
|
||||
// elements 构成 Node Composition
|
||||
const struct bt_mesh_comp app_comp = {
|
||||
.cid = 0x07D7, // WCH 公司id
|
||||
.elem = elements,
|
||||
.elem_count = ARRAY_SIZE(elements),
|
||||
};
|
||||
|
||||
// 配网参数和回调
|
||||
static const struct bt_mesh_prov app_prov = {
|
||||
.uuid = tm_uuid,
|
||||
.static_val_len = ARRAY_SIZE(static_key),
|
||||
.static_val = static_key,
|
||||
.link_open = link_open,
|
||||
.link_close = link_close,
|
||||
.complete = prov_complete,
|
||||
.reset = prov_reset,
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn silen_adv_set
|
||||
*
|
||||
* @brief 设置静默广播
|
||||
*
|
||||
* @param flag - 0(处于未配网广播状态),1(处于静默广播状态).
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void silen_adv_set(uint8_t flag)
|
||||
{
|
||||
tm_uuid[13] &= ~BIT(0);
|
||||
tm_uuid[13] |= (BIT_MASK(1) & flag);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn prov_enable
|
||||
*
|
||||
* @brief 使能配网功能
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void prov_enable(void)
|
||||
{
|
||||
silen_adv_set(SELENCE_ADV_OF);
|
||||
|
||||
if(bt_mesh_is_provisioned())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we're scanning for provisioning inviations
|
||||
bt_mesh_scan_enable();
|
||||
// Enable unprovisioned beacon sending
|
||||
bt_mesh_beacon_enable();
|
||||
|
||||
if(CONFIG_BLE_MESH_PB_GATT)
|
||||
{
|
||||
bt_mesh_proxy_prov_enable();
|
||||
}
|
||||
|
||||
tmos_start_task(App_TaskID, APP_SILENT_ADV_EVT, ADV_TIMEOUT);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn link_open
|
||||
*
|
||||
* @brief 配网时后的link打开回调
|
||||
*
|
||||
* @param bearer - 当前link是PB_ADV还是PB_GATT
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void link_open(bt_mesh_prov_bearer_t bearer)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
|
||||
tmos_stop_task(App_TaskID, APP_SILENT_ADV_EVT);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn link_close
|
||||
*
|
||||
* @brief 配网后的link关闭回调
|
||||
*
|
||||
* @param bearer - 当前link是PB_ADV还是PB_GATT
|
||||
* @param reason - link关闭原因
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void link_close(bt_mesh_prov_bearer_t bearer, uint8_t reason)
|
||||
{
|
||||
APP_DBG("");
|
||||
|
||||
if(!bt_mesh_is_provisioned())
|
||||
{
|
||||
prov_enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
/*天猫精灵不会下发Config_model_app_bind和Config_Model_Subscrption_Add消息。
|
||||
IOT设备需要自行给所有Element的所有model绑定下发的AppKey,并根据产品类型为各个
|
||||
model订阅相应的组播地址(具体品类组播地址请参阅各产品软件规范)。蓝牙Mesh设备
|
||||
完成配网后需要进行消息上报,上报消息包括该设备所有支持的可上报的属性。*/
|
||||
|
||||
/* For Light Subscription group address */
|
||||
root_models[2].groups[0] = (uint16_t)0xC000;
|
||||
root_models[2].groups[1] = (uint16_t)0xCFFF;
|
||||
bt_mesh_store_mod_sub(&root_models[2]);
|
||||
|
||||
root_models[2].keys[0] = (uint16_t)0x0000;
|
||||
bt_mesh_store_mod_bind(&root_models[2]);
|
||||
|
||||
/* For Light Subscription group address */
|
||||
vnd_models[0].groups[0] = (uint16_t)0xC000;
|
||||
vnd_models[0].groups[1] = (uint16_t)0xCFFF;
|
||||
bt_mesh_store_mod_sub(&vnd_models[0]);
|
||||
|
||||
vnd_models[0].keys[0] = (uint16_t)0x0000;
|
||||
bt_mesh_store_mod_bind(&vnd_models[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn prov_complete
|
||||
*
|
||||
* @brief 配网完成回调,重新开始广播
|
||||
*
|
||||
* @param net_idx - 网络key的index
|
||||
* @param addr - 网络地址
|
||||
* @param flags - 是否处于key refresh状态
|
||||
* @param iv_index - 当前网络iv的index
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index)
|
||||
{
|
||||
/* 设备上电后,如果已配网,也需要在1~10s随机间隔后上报所有支持的属性状态。 */
|
||||
tmosTimer rand_timer;
|
||||
APP_DBG(" ");
|
||||
|
||||
rand_timer = K_SECONDS(5) + (tmos_rand() % K_SECONDS(6));
|
||||
tmos_start_task(App_TaskID, APP_SILENT_ADV_EVT, rand_timer);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn prov_reset
|
||||
*
|
||||
* @brief 复位配网功能回调
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void prov_reset(void)
|
||||
{
|
||||
APP_DBG("");
|
||||
|
||||
prov_enable();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ind_end_cb
|
||||
*
|
||||
* @brief 发送复位事件完成回调
|
||||
*
|
||||
* @param err - 错误码
|
||||
* @param cb_data - 回调参数
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void ind_end_cb(int err, void *cb_data)
|
||||
{
|
||||
APP_DBG(" bt_mesh_reset ");
|
||||
bt_mesh_reset();
|
||||
}
|
||||
|
||||
static const struct bt_adv_ind_send_cb reset_cb = {
|
||||
.end = ind_end_cb,
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* @fn send_support_attr
|
||||
*
|
||||
* @brief 发送所有支持的可上报的属性给天猫精灵,此消息决定天猫精灵判断设备支持那些功能
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void send_support_attr(void)
|
||||
{
|
||||
struct bt_mesh_indicate *ind;
|
||||
APP_DBG("");
|
||||
|
||||
if(!bt_mesh_is_provisioned())
|
||||
{
|
||||
APP_DBG("Local Dev Unprovisioned");
|
||||
return;
|
||||
}
|
||||
|
||||
ind = bt_mesh_ind_alloc(32);
|
||||
if(!ind)
|
||||
{
|
||||
APP_DBG("Unable allocate buffers");
|
||||
return;
|
||||
}
|
||||
ind->param.trans_cnt = 0x09;
|
||||
ind->param.period = K_MSEC(300);
|
||||
ind->param.send_ttl = BLE_MESH_TTL_DEFAULT;
|
||||
ind->param.tid = als_avail_tid_get();
|
||||
|
||||
/* Init indication opcode */
|
||||
bt_mesh_model_msg_init(&(ind->buf->b), OP_VENDOR_MESSAGE_ATTR_INDICATION);
|
||||
|
||||
/* Add tid field */
|
||||
net_buf_simple_add_u8(&(ind->buf->b), ind->param.tid);
|
||||
|
||||
// 添加开关属性
|
||||
{
|
||||
/* Add generic onoff attrbute op */
|
||||
net_buf_simple_add_le16(&(ind->buf->b), ALI_GEN_ATTR_TYPE_POWER_STATE);
|
||||
|
||||
/* Add current generic onoff status */
|
||||
net_buf_simple_add_u8(&(ind->buf->b), read_led_state(MSG_PIN));
|
||||
}
|
||||
// // 可选根据阿里云设置的产品属性功能添加对应属性 ( BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET )
|
||||
// // 这里添加亮度属性留作参考,还需要添加对应的opcode处理函数,添加方式参考开关属性的 gen_onoff_op 结构
|
||||
// {
|
||||
// /* Add brightness attrbute opcode */
|
||||
// net_buf_simple_add_le16(&(ind->buf->b), ALI_GEN_ATTR_TYPE_BRIGHTNESS);
|
||||
|
||||
// /* Add brightness status (655~65535对应天猫控制1~100) */
|
||||
// net_buf_simple_add_le16(&(ind->buf->b), 65535);
|
||||
// }
|
||||
|
||||
bt_mesh_indicate_send(ind);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn send_led_state
|
||||
*
|
||||
* @brief 发送当前灯的状态给天猫精灵
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void send_led_state(void)
|
||||
{
|
||||
APP_DBG("");
|
||||
struct indicate_param param = {
|
||||
.trans_cnt = 0x09,
|
||||
.period = K_MSEC(300),
|
||||
.send_ttl = BLE_MESH_TTL_DEFAULT,
|
||||
.tid = als_avail_tid_get(),
|
||||
};
|
||||
|
||||
toggle_led_state(MSG_PIN);
|
||||
|
||||
if(!bt_mesh_is_provisioned())
|
||||
{
|
||||
APP_DBG("Local Dev Unprovisioned");
|
||||
return;
|
||||
}
|
||||
|
||||
send_led_indicate(¶m);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn send_reset_indicate
|
||||
*
|
||||
* @brief 发送复位事件给天猫精灵,发送完成后将清除配网状态,重置自身mesh网络
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void send_reset_indicate(void)
|
||||
{
|
||||
struct bt_mesh_indicate *ind;
|
||||
APP_DBG("");
|
||||
|
||||
if(!bt_mesh_is_provisioned())
|
||||
{
|
||||
APP_DBG("Local Dev Unprovisioned");
|
||||
return;
|
||||
}
|
||||
|
||||
ind = bt_mesh_ind_alloc(16);
|
||||
if(!ind)
|
||||
{
|
||||
APP_DBG("Unable allocate buffers");
|
||||
return;
|
||||
}
|
||||
ind->param.trans_cnt = 0x09;
|
||||
ind->param.period = K_MSEC(300);
|
||||
ind->param.cb = &reset_cb;
|
||||
ind->param.send_ttl = BLE_MESH_TTL_DEFAULT;
|
||||
ind->param.tid = als_avail_tid_get();
|
||||
|
||||
/* Init indication opcode */
|
||||
bt_mesh_model_msg_init(&(ind->buf->b), OP_VENDOR_MESSAGE_ATTR_INDICATION);
|
||||
|
||||
/* Add tid field */
|
||||
net_buf_simple_add_u8(&(ind->buf->b), ind->param.tid);
|
||||
|
||||
/* Add event report opcode */
|
||||
net_buf_simple_add_le16(&(ind->buf->b), ALI_GEN_ATTR_TYPE_EVENT_TRIGGER);
|
||||
|
||||
/* Add reset event */
|
||||
net_buf_simple_add_u8(&(ind->buf->b), ALI_GEN_ATTR_TYPE_HARDWARE_RESET);
|
||||
|
||||
bt_mesh_indicate_send(ind);
|
||||
}
|
||||
|
||||
#define HAL_KEY_SEND_MSG BIT(0)
|
||||
#define HAL_KEY_RESET BIT(1)
|
||||
|
||||
/*********************************************************************
|
||||
* @fn keyPress
|
||||
*
|
||||
* @brief 按键回调
|
||||
*
|
||||
* @param keys - 按键类型
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void keyPress(uint8_t keys)
|
||||
{
|
||||
APP_DBG("keys : %d ", keys);
|
||||
|
||||
switch(keys)
|
||||
{
|
||||
case HAL_KEY_SEND_MSG:
|
||||
send_led_state();
|
||||
break;
|
||||
case HAL_KEY_RESET:
|
||||
send_reset_indicate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn app_silent_adv
|
||||
*
|
||||
* @brief 超时后如果还未配网成功,则进入静默广播模式,若已配网,发送支持的属性给天猫精灵
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void app_silent_adv(void)
|
||||
{
|
||||
APP_DBG("");
|
||||
if(bt_mesh_is_provisioned())
|
||||
{
|
||||
send_support_attr();
|
||||
return;
|
||||
}
|
||||
|
||||
silen_adv_set(SELENCE_ADV_ON);
|
||||
|
||||
/* Disable Scanner not response Provisioner message */
|
||||
bt_mesh_scan_disable();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn blemesh_on_sync
|
||||
*
|
||||
* @brief 同步mesh参数,启用对应功能,不建议修改
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void blemesh_on_sync(void)
|
||||
{
|
||||
int err;
|
||||
mem_info_t info;
|
||||
|
||||
if(tmos_memcmp(VER_MESH_LIB, VER_MESH_FILE, strlen(VER_MESH_FILE)) == FALSE)
|
||||
{
|
||||
APP_DBG("head file error...\n");
|
||||
while(1);
|
||||
}
|
||||
|
||||
info.base_addr = MESH_MEM;
|
||||
info.mem_len = ARRAY_SIZE(MESH_MEM);
|
||||
|
||||
#if(CONFIG_BLE_MESH_FRIEND)
|
||||
friend_init_register(bt_mesh_friend_init, friend_state);
|
||||
#endif /* FRIEND */
|
||||
#if(CONFIG_BLE_MESH_LOW_POWER)
|
||||
lpn_init_register(bt_mesh_lpn_init, lpn_state);
|
||||
#endif /* LPN */
|
||||
|
||||
err = bt_mesh_cfg_set(&app_mesh_cfg, &app_dev, MacAddr, &info);
|
||||
if(err)
|
||||
{
|
||||
APP_DBG("Unable set configuration (err:%d)", err);
|
||||
return;
|
||||
}
|
||||
hal_rf_init();
|
||||
err = bt_mesh_comp_register(&app_comp);
|
||||
|
||||
#if(CONFIG_BLE_MESH_RELAY)
|
||||
bt_mesh_relay_init();
|
||||
#endif /* RELAY */
|
||||
#if(CONFIG_BLE_MESH_PROXY || CONFIG_BLE_MESH_PB_GATT)
|
||||
#if(CONFIG_BLE_MESH_PROXY)
|
||||
bt_mesh_proxy_beacon_init_register((void *)bt_mesh_proxy_beacon_init);
|
||||
gatts_notify_register(bt_mesh_gatts_notify);
|
||||
proxy_gatt_enable_register(bt_mesh_proxy_gatt_enable);
|
||||
#endif /* PROXY */
|
||||
#if(CONFIG_BLE_MESH_PB_GATT)
|
||||
proxy_prov_enable_register(bt_mesh_proxy_prov_enable);
|
||||
#endif /* PB_GATT */
|
||||
|
||||
bt_mesh_proxy_init();
|
||||
#endif /* PROXY || PB-GATT */
|
||||
|
||||
#if(CONFIG_BLE_MESH_PROXY_CLI)
|
||||
bt_mesh_proxy_client_init(cli); //待添加
|
||||
#endif /* PROXY_CLI */
|
||||
|
||||
bt_mesh_prov_retransmit_init();
|
||||
#if(!CONFIG_BLE_MESH_PB_GATT)
|
||||
adv_link_rx_buf_register(&rx_buf);
|
||||
#endif /* !PB_GATT */
|
||||
err = bt_mesh_prov_init(&app_prov);
|
||||
|
||||
bt_mesh_mod_init();
|
||||
bt_mesh_net_init();
|
||||
bt_mesh_trans_init();
|
||||
bt_mesh_beacon_init();
|
||||
|
||||
bt_mesh_adv_init();
|
||||
|
||||
#if((CONFIG_BLE_MESH_PB_GATT) || (CONFIG_BLE_MESH_PROXY) || (CONFIG_BLE_MESH_OTA))
|
||||
bt_mesh_conn_adv_init();
|
||||
#endif /* PROXY || PB-GATT || OTA */
|
||||
|
||||
#if(CONFIG_BLE_MESH_SETTINGS)
|
||||
bt_mesh_settings_init();
|
||||
#endif /* SETTINGS */
|
||||
|
||||
#if(CONFIG_BLE_MESH_PROXY_CLI)
|
||||
bt_mesh_proxy_cli_adapt_init();
|
||||
#endif /* PROXY_CLI */
|
||||
|
||||
#if((CONFIG_BLE_MESH_PROXY) || (CONFIG_BLE_MESH_PB_GATT) || \
|
||||
(CONFIG_BLE_MESH_PROXY_CLI) || (CONFIG_BLE_MESH_OTA))
|
||||
bt_mesh_adapt_init();
|
||||
#endif /* PROXY || PB-GATT || PROXY_CLI || OTA */
|
||||
|
||||
if(err)
|
||||
{
|
||||
APP_DBG("Initializing mesh failed (err %d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
APP_DBG("Bluetooth initialized");
|
||||
|
||||
#if(CONFIG_BLE_MESH_SETTINGS)
|
||||
settings_load();
|
||||
#endif /* SETTINGS */
|
||||
|
||||
if(bt_mesh_is_provisioned())
|
||||
{
|
||||
APP_DBG("Mesh network restored from flash");
|
||||
}
|
||||
else
|
||||
{
|
||||
prov_enable();
|
||||
}
|
||||
|
||||
APP_DBG("Mesh initialized");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn App_Init
|
||||
*
|
||||
* @brief 应用层初始化
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void App_Init(void)
|
||||
{
|
||||
App_TaskID = TMOS_ProcessEventRegister(App_ProcessEvent);
|
||||
|
||||
als_vendor_init(vnd_models);
|
||||
blemesh_on_sync();
|
||||
HAL_KeyInit();
|
||||
HalKeyConfig(keyPress);
|
||||
set_led_state(MSG_PIN, 0);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn App_ProcessEvent
|
||||
*
|
||||
* @brief 应用层事件处理函数
|
||||
*
|
||||
* @param task_id - The TMOS assigned task ID.
|
||||
* @param events - events to process. This is a bit map and can
|
||||
* contain more than one event.
|
||||
*
|
||||
* @return events not processed
|
||||
*/
|
||||
static uint16_t App_ProcessEvent(uint8_t task_id, uint16_t events)
|
||||
{
|
||||
if(events & APP_SILENT_ADV_EVT)
|
||||
{
|
||||
app_silent_adv();
|
||||
return (events ^ APP_SILENT_ADV_EVT);
|
||||
}
|
||||
|
||||
// Discard unknown events
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
@@ -0,0 +1,210 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app_generic_onoff_model.c
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2021/03/24
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#include "CONFIG.h"
|
||||
#include "MESH_LIB.h"
|
||||
#include "app_mesh_config.h"
|
||||
#include "app_generic_onoff_model.h"
|
||||
#include "app_vendor_model.h"
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
|
||||
#define ALI_DEF_TTL (10)
|
||||
|
||||
/*********************************************************************
|
||||
* @fn read_led_state
|
||||
*
|
||||
* @brief 获取当前灯状态.
|
||||
*
|
||||
* @param led_pin - 引脚.
|
||||
*
|
||||
* @return 灯状态
|
||||
*/
|
||||
BOOL read_led_state(uint32_t led_pin)
|
||||
{
|
||||
return (GPIO_ReadOutputDataBit(MSG_GPIO, led_pin) > 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn set_led_state
|
||||
*
|
||||
* @brief 设置当前灯状态.
|
||||
*
|
||||
* @param led_pin - 引脚.
|
||||
* @param on - 状态.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void set_led_state(uint32_t led_pin, BOOL on)
|
||||
{
|
||||
RCC_APB2PeriphClockCmd(MSG_PCENR, ENABLE);
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = led_pin;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO_Init(MSG_GPIO, &GPIO_InitStructure);
|
||||
|
||||
on ? GPIO_WriteBit(MSG_GPIO, led_pin, Bit_RESET) : GPIO_WriteBit(MSG_GPIO, led_pin, Bit_SET);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn toggle_led_state
|
||||
*
|
||||
* @brief 翻转当前灯状态
|
||||
*
|
||||
* @param led_pin - 引脚.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void toggle_led_state(uint32_t led_pin)
|
||||
{
|
||||
RCC_APB2PeriphClockCmd(MSG_PCENR, ENABLE);
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = led_pin;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
GPIO_WriteBit(MSG_GPIO, led_pin, Bit_SET-GPIO_ReadOutputDataBit(MSG_GPIO, led_pin));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn gen_onoff_status
|
||||
*
|
||||
* @brief 回复天猫精灵开关状态
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void gen_onoff_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx)
|
||||
{
|
||||
NET_BUF_SIMPLE_DEFINE(msg, 32);
|
||||
int err;
|
||||
|
||||
bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS);
|
||||
net_buf_simple_add_u8(&msg, read_led_state(MSG_PIN));
|
||||
|
||||
APP_DBG("ttl: 0x%02x dst: 0x%04x", ctx->recv_ttl, ctx->recv_dst);
|
||||
|
||||
if(ctx->recv_ttl != ALI_DEF_TTL)
|
||||
{
|
||||
ctx->send_ttl = BLE_MESH_TTL_DEFAULT;
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx->send_ttl = 0;
|
||||
}
|
||||
|
||||
err = bt_mesh_model_send(model, ctx, &msg, NULL, NULL);
|
||||
if(err)
|
||||
{
|
||||
APP_DBG("send status failed: %d", err);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn gen_onoff_get
|
||||
*
|
||||
* @brief 天猫精灵下发的获取开关状态命令
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void gen_onoff_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
|
||||
gen_onoff_status(model, ctx);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn gen_onoff_set
|
||||
*
|
||||
* @brief 天猫精灵下发的设置开关状态命令,如果与当前状态不同,还需要发送ind给天猫
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void gen_onoff_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
struct indicate_param param = {
|
||||
.trans_cnt = 0x09,
|
||||
.period = K_MSEC(300),
|
||||
.rand = (tmos_rand() % 50),
|
||||
.tid = als_avail_tid_get(),
|
||||
};
|
||||
|
||||
APP_DBG("ttl: 0x%02x dst: 0x%04x rssi: %d", ctx->recv_ttl, ctx->recv_dst, ctx->recv_rssi);
|
||||
|
||||
set_led_state(MSG_PIN, buf->data[0]);
|
||||
|
||||
if(ctx->recv_ttl != ALI_DEF_TTL)
|
||||
{
|
||||
param.send_ttl = BLE_MESH_TTL_DEFAULT;
|
||||
}
|
||||
|
||||
/* Overwrite default configuration */
|
||||
if(BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst))
|
||||
{
|
||||
param.rand = 0;
|
||||
param.send_ttl = BLE_MESH_TTL_DEFAULT;
|
||||
param.period = K_MSEC(100);
|
||||
}
|
||||
|
||||
send_led_indicate(¶m);
|
||||
|
||||
gen_onoff_status(model, ctx);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn gen_onoff_set_unack
|
||||
*
|
||||
* @brief 天猫精灵下发的设置开关状态命令(无应答)
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void gen_onoff_set_unack(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
uint8_t status;
|
||||
|
||||
APP_DBG(" ");
|
||||
|
||||
status = read_led_state(MSG_PIN);
|
||||
if(status != buf->data[0])
|
||||
{
|
||||
set_led_state(MSG_PIN, buf->data[0]);
|
||||
}
|
||||
}
|
||||
|
||||
const struct bt_mesh_model_op gen_onoff_op[] = {
|
||||
{BLE_MESH_MODEL_OP_GEN_ONOFF_GET, 0, gen_onoff_get},
|
||||
{BLE_MESH_MODEL_OP_GEN_ONOFF_SET, 2, gen_onoff_set},
|
||||
{BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2, gen_onoff_set_unack},
|
||||
BLE_MESH_MODEL_OP_END,
|
||||
};
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
@@ -0,0 +1,106 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : main.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/06/22
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "CONFIG.h"
|
||||
#include "MESH_LIB.h"
|
||||
#include "HAL.h"
|
||||
#include "app_mesh_config.h"
|
||||
#include "app.h"
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
__attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
|
||||
#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
|
||||
const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* @fn bt_mesh_lib_init
|
||||
*
|
||||
* @brief Mesh library initialization
|
||||
*
|
||||
* @return state
|
||||
*/
|
||||
uint8_t bt_mesh_lib_init(void)
|
||||
{
|
||||
uint8_t ret;
|
||||
|
||||
if(tmos_memcmp(VER_MESH_LIB, VER_MESH_FILE, strlen(VER_MESH_FILE)) == FALSE)
|
||||
{
|
||||
PRINT("mesh head file error...\n");
|
||||
while(1);
|
||||
}
|
||||
|
||||
ret = RF_RoleInit();
|
||||
|
||||
#if((CONFIG_BLE_MESH_PROXY) || \
|
||||
(CONFIG_BLE_MESH_PB_GATT) || \
|
||||
(CONFIG_BLE_MESH_OTA))
|
||||
ret = GAPRole_PeripheralInit();
|
||||
#endif /* PROXY || PB-GATT || OTA */
|
||||
|
||||
#if(CONFIG_BLE_MESH_PROXY_CLI)
|
||||
ret = GAPRole_CentralInit();
|
||||
#endif /* CONFIG_BLE_MESH_PROXY_CLI */
|
||||
|
||||
MeshTimer_Init();
|
||||
MeshDeamon_Init();
|
||||
ble_sm_alg_ecc_init();
|
||||
|
||||
#if(CONFIG_BLE_MESH_IV_UPDATE_TEST)
|
||||
bt_mesh_iv_update_test(TRUE);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Main_Circulation
|
||||
*
|
||||
* @brief Main loop
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__((section(".highcode")))
|
||||
__attribute__((noinline))
|
||||
void Main_Circulation(void)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
TMOS_SystemProcess();
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn main
|
||||
*
|
||||
* @brief Main function
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
Delay_Init();
|
||||
#ifdef DEBUG
|
||||
USART_Printf_Init( 115200 );
|
||||
#endif
|
||||
PRINT("%s\n", VER_LIB);
|
||||
WCHBLE_Init();
|
||||
HAL_Init();
|
||||
bt_mesh_lib_init();
|
||||
App_Init();
|
||||
Main_Circulation();
|
||||
}
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
@@ -0,0 +1,182 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app_mesh_config.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/06/22
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "CONFIG.h"
|
||||
#include "app_mesh_config.h"
|
||||
#include "MESH_LIB.h"
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
const ble_mesh_cfg_t app_mesh_cfg = {
|
||||
.common_cfg.adv_buf_count = CONFIG_MESH_ADV_BUF_COUNT_DEF,
|
||||
.common_cfg.rpl_count = CONFIG_MESH_RPL_COUNT_DEF,
|
||||
.common_cfg.allow_rpl_cycle = CONFIG_MESH_ALLOW_RPL_CYCLE,
|
||||
.common_cfg.allow_same_addr = CONFIG_MESH_ALLOW_SAME_ADDR,
|
||||
.common_cfg.ivu_divider = CONFIG_MESH_IVU_DIVIDER_DEF,
|
||||
.common_cfg.mod_key_count = CONFIG_MESH_MOD_KEY_COUNT_DEF,
|
||||
.common_cfg.mod_group_count = CONFIG_MESH_MOD_GROUP_COUNT_DEF,
|
||||
|
||||
.proxy_cfg.pxyfilter_count = CONFIG_MESH_PROXY_FILTER_DEF,
|
||||
|
||||
.net_cfg.msgcache_count = CONFIG_MESH_MSG_CACHE_DEF,
|
||||
.net_cfg.subnet_count = CONFIG_MESH_SUBNET_COUNT_DEF,
|
||||
.net_cfg.appkey_count = CONFIG_MESH_APPKEY_COUNT_DEF,
|
||||
.net_cfg.unseg_length = CONFIG_MESH_UNSEG_LENGTH_DEF,
|
||||
.net_cfg.txseg_max = CONFIG_MESH_TX_SEG_DEF,
|
||||
.net_cfg.txseg_count = CONFIG_MESH_TX_SEG_COUNT_DEF,
|
||||
.net_cfg.rxseg_count = CONFIG_MESH_RX_SEG_COUNT_DEF,
|
||||
.net_cfg.rxsdu_max = CONFIG_MESH_RX_SDU_DEF,
|
||||
.net_cfg.label_count = CONFIG_MESH_LABEL_COUNT_DEF,
|
||||
|
||||
.store_cfg.seq_store_rate = CONFIG_MESH_SEQ_STORE_RATE_DEF,
|
||||
.store_cfg.rpl_store_rate = CONFIG_MESH_RPL_STORE_RATE_DEF,
|
||||
.store_cfg.store_rate = CONFIG_MESH_STORE_RATE_DEF,
|
||||
|
||||
.friend_cfg.frndseg_rx = CONFIG_MESH_FRIEND_SEG_RX_COUNT_DEF,
|
||||
.friend_cfg.frndsub_size = CONFIG_MESH_FRIEND_SUB_SIZE_DEF,
|
||||
.friend_cfg.frndlpn_count = CONFIG_MESH_FRIEND_LPN_COUNT_DEF,
|
||||
.friend_cfg.frndqueue_size = CONFIG_MESH_QUEUE_SIZE_DEF,
|
||||
.friend_cfg.frndrecv_win = CONFIG_MESH_FRIEND_RECV_WIN_DEF,
|
||||
|
||||
.lpn_cfg.lpnmin_size = CONFIG_MESH_LPN_REQ_QUEUE_SIZE_DEF,
|
||||
.lpn_cfg.lpnrssi_factor = 0,
|
||||
.lpn_cfg.lpnrecv_factor = 0,
|
||||
.lpn_cfg.lpnpoll_interval = CONFIG_MESH_LPN_POLLINTERVAL_DEF,
|
||||
.lpn_cfg.lpnpoll_timeout = CONFIG_MESH_LPN_POLLTIMEOUT_DEF,
|
||||
.lpn_cfg.lpnrecv_delay = CONFIG_MESH_LPN_RECV_DELAY_DEF,
|
||||
.lpn_cfg.lpnretry_timeout = CONFIG_MESH_RETRY_TIMEOUT_DEF,
|
||||
|
||||
.prov_cfg.node_count = CONFIG_MESH_PROV_NODE_COUNT_DEF,
|
||||
.rf_cfg.rf_accessAddress = CONFIG_MESH_RF_ACCESSADDRESS,
|
||||
.rf_cfg.rf_channel_37 = CONFIG_MESH_RF_CHANNEL_37,
|
||||
.rf_cfg.rf_channel_38 = CONFIG_MESH_RF_CHANNEL_38,
|
||||
.rf_cfg.rf_channel_39 = CONFIG_MESH_RF_CHANNEL_39,
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* @fn read_flash
|
||||
*
|
||||
* @brief read flash
|
||||
*
|
||||
* @param offset - 地址偏移
|
||||
* @param data - 数据指针
|
||||
* @param len - 长度
|
||||
*
|
||||
* @return always success
|
||||
*/
|
||||
int read_flash(int offset, void *data, unsigned int len)
|
||||
{
|
||||
tmos_memcpy(data, (uint8_t*)offset, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn write_flash
|
||||
*
|
||||
* @brief write flash
|
||||
*
|
||||
* @param offset - 地址偏移
|
||||
* @param data - 数据指针
|
||||
* @param len - 长度
|
||||
*
|
||||
* @return state
|
||||
*/
|
||||
int write_flash(int offset, const void *data, unsigned int len)
|
||||
{
|
||||
FLASH_Status status = FLASH_COMPLETE;
|
||||
__attribute__((aligned(4))) uint8_t vec[64]={0};
|
||||
uint8_t i;
|
||||
|
||||
if(len > sizeof(vec))
|
||||
{
|
||||
return -E2BIG;
|
||||
}
|
||||
|
||||
memcpy(vec, data, len);
|
||||
|
||||
FLASH_Unlock();
|
||||
for( i=0; i<(len+1)/2; i++)
|
||||
{
|
||||
status = FLASH_ProgramHalfWord(offset+i*2,vec[i*2]|(vec[i*2+1]<<8));
|
||||
if( status != FLASH_COMPLETE )
|
||||
{
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
FLASH_Lock();
|
||||
|
||||
return NOERR;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn erase_flash
|
||||
*
|
||||
* @brief erase flash
|
||||
*
|
||||
* @param offset - 地址偏移
|
||||
* @param len - 长度
|
||||
*
|
||||
* @return state
|
||||
*/
|
||||
int erase_flash(int offset, unsigned int len)
|
||||
{
|
||||
FLASH_Status status = FLASH_COMPLETE;
|
||||
if(len != 4096)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
FLASH_Unlock();
|
||||
status = FLASH_ErasePage(offset);
|
||||
FLASH_Lock();
|
||||
if(status == FLASH_COMPLETE)
|
||||
{
|
||||
return NOERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn flash_write_protection
|
||||
*
|
||||
* @brief no used
|
||||
*
|
||||
* @param enable - no uesd
|
||||
*
|
||||
* @return always success
|
||||
*/
|
||||
int flash_write_protection(BOOL enable)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct device app_dev = {
|
||||
.api = {
|
||||
.read = read_flash,
|
||||
.write = write_flash,
|
||||
.erase = erase_flash,
|
||||
.write_protection = flash_write_protection,
|
||||
},
|
||||
.info = {
|
||||
.nvs_sector_cnt = CONFIG_MESH_SECTOR_COUNT_DEF,
|
||||
.nvs_write_size = sizeof(int),
|
||||
.nvs_sector_size = CONFIG_MESH_SECTOR_SIZE_DEF,
|
||||
.nvs_store_baddr = CONFIG_MESH_NVS_ADDR_DEF,
|
||||
},
|
||||
};
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
@@ -0,0 +1,615 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app_vendor_model.c
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2021/03/24
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#include "app_mesh_config.h"
|
||||
#include "MESH_LIB.h"
|
||||
#include "CONFIG.h"
|
||||
#include "app_vendor_model.h"
|
||||
#include "app_generic_onoff_model.h"
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
/* Mac Address Light2 */
|
||||
#define PID 9701
|
||||
#define MAC_ADDR \
|
||||
{ \
|
||||
0xf8, 0xa7, 0x63, 0x6a, 0xec, 0x3f \
|
||||
}
|
||||
#define ALI_SECRET \
|
||||
{ \
|
||||
0x6b, 0xfa, 0x68, 0x6f, 0x9d, 0x1b, 0x37, 0x00, 0x01, 0xd1, 0xfd, 0xb8, 0x27, 0x7d, 0xc0, 0x81 \
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
|
||||
uint8_t tm_uuid[16];
|
||||
uint8_t static_key[16];
|
||||
const uint8_t MacAddr[6] = MAC_ADDR;
|
||||
|
||||
static const struct bt_als_cfg cfg = {
|
||||
.mac = MAC_ADDR,
|
||||
.secret = ALI_SECRET,
|
||||
.pid = PID,
|
||||
.cid = 0x01a8,
|
||||
.version = 0x00060000,
|
||||
};
|
||||
|
||||
static uint8_t als_vendor_model_TaskID = 0; // Task ID for internal task/event processing
|
||||
static uint8_t als_tid = 0;
|
||||
|
||||
static struct net_buf ind_buf[CONFIG_INDICATE_NUM] = {0};
|
||||
static struct bt_mesh_indicate indicate[CONFIG_INDICATE_NUM] = {0};
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
static void ind_reset(struct bt_mesh_indicate *ind, int err);
|
||||
static uint16_t als_vendor_model_ProcessEvent(uint8_t task_id, uint16_t events);
|
||||
|
||||
/*********************************************************************
|
||||
* @fn tm_attr_get
|
||||
*
|
||||
* @brief 天猫获取属性命令
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void tm_attr_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn tm_attr_set
|
||||
*
|
||||
* @brief 天猫设置属性命令
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void tm_attr_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn tm_attr_set_unack
|
||||
*
|
||||
* @brief 天猫设置属性命令(无应答)
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void tm_attr_set_unack(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn tm_attr_status
|
||||
*
|
||||
* @brief 天猫状态应答
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void tm_attr_status(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn tm_attr_confirm
|
||||
*
|
||||
* @brief 收到天猫精灵发出的confirm - 该消息用于Vendor Model Client回复给Vendor Model Server,
|
||||
* 用于表示已收到Vendor Model Server发出的Indication
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void tm_attr_confirm(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
uint8_t recv_tid;
|
||||
|
||||
recv_tid = net_buf_simple_pull_u8(buf);
|
||||
|
||||
APP_DBG("src: 0x%04x dst: 0x%04x tid 0x%02x rssi: %d", ctx->addr, ctx->recv_dst, recv_tid, ctx->recv_rssi);
|
||||
|
||||
for(int i = 0; i < CONFIG_INDICATE_NUM; i++)
|
||||
{
|
||||
if(indicate[i].param.tid == recv_tid)
|
||||
{
|
||||
ind_reset(&indicate[i], 0);
|
||||
tmos_stop_task(als_vendor_model_TaskID, indicate[i].event);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn tm_attr_trans
|
||||
*
|
||||
* @brief 该消息用于Mesh设备与天猫精灵之间透传数据
|
||||
*
|
||||
* @param model - 模型参数.
|
||||
* @param ctx - 数据参数.
|
||||
* @param buf - 数据内容.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void tm_attr_trans(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf)
|
||||
{
|
||||
APP_DBG(" ");
|
||||
}
|
||||
|
||||
// opcode 对应的处理函数
|
||||
static const struct bt_mesh_model_op vnd_model_op[] = {
|
||||
{OP_VENDOR_MESSAGE_ATTR_GET, 0, tm_attr_get},
|
||||
{OP_VENDOR_MESSAGE_ATTR_SET, 0, tm_attr_set},
|
||||
{OP_VENDOR_MESSAGE_ATTR_SET_UNACK, 0, tm_attr_set_unack},
|
||||
{OP_VENDOR_MESSAGE_ATTR_STATUS, 0, tm_attr_status},
|
||||
{OP_VENDOR_MESSAGE_ATTR_CONFIRMATION, 1, tm_attr_confirm},
|
||||
{OP_VENDOR_MESSAGE_ATTR_TRANSPARENT_MSG, 0, tm_attr_trans},
|
||||
BLE_MESH_MODEL_OP_END,
|
||||
};
|
||||
|
||||
uint16_t vnd_model_srv_keys[CONFIG_MESH_MOD_KEY_COUNT_DEF] = {BLE_MESH_KEY_UNUSED};
|
||||
uint16_t vnd_model_srv_groups[CONFIG_MESH_MOD_GROUP_COUNT_DEF] = {BLE_MESH_ADDR_UNASSIGNED};
|
||||
|
||||
struct bt_mesh_model vnd_models[] = {
|
||||
BLE_MESH_MODEL_VND_CB(CID_ALI_GENIE, 0x0000, vnd_model_op, NULL, vnd_model_srv_keys, vnd_model_srv_groups, NULL,
|
||||
NULL),
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* @fn als_avail_tid_get
|
||||
*
|
||||
* @brief TID selection method
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return next TID
|
||||
*/
|
||||
uint8_t als_avail_tid_get(void)
|
||||
{
|
||||
return als_tid++;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn uuid_generate
|
||||
*
|
||||
* @brief 生成阿里规范的UUID
|
||||
*
|
||||
* @param cfg - 配置信息
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void uuid_generate(struct bt_als_cfg const *cfg)
|
||||
{
|
||||
/* Company ID */
|
||||
tm_uuid[0] = cfg->cid;
|
||||
tm_uuid[1] = cfg->cid >> 8;
|
||||
|
||||
/* bit3~0 :蓝牙广播包版本号,目前是0x01
|
||||
bit4为1:一机一密
|
||||
bit5为1:支持OTA
|
||||
bit7~6:蓝牙协议版本
|
||||
00:BLE4.0
|
||||
01:BLE4.2
|
||||
10:BLE5.0
|
||||
11:BLE5.0以上 */
|
||||
/* Advertising Verison */
|
||||
tm_uuid[2] = BIT(0) | /* adv version */
|
||||
BIT(4) | /* secret */
|
||||
BIT(5) | /* ota */
|
||||
BIT(7); /* ble verison */
|
||||
|
||||
/* Product Id */
|
||||
tm_uuid[2 + 1] = cfg->pid;
|
||||
tm_uuid[2 + 2] = cfg->pid >> 8;
|
||||
tm_uuid[2 + 3] = cfg->pid >> 16;
|
||||
tm_uuid[2 + 4] = cfg->pid >> 24;
|
||||
|
||||
/* Device Mac Address */
|
||||
for(int i = 0; i < 6; i++)
|
||||
{
|
||||
tm_uuid[2 + 1 + 4 + i] = cfg->mac[5 - i];
|
||||
}
|
||||
|
||||
/* UUID Verison */
|
||||
tm_uuid[2 + 1 + 4 + 6] = BIT(1);
|
||||
|
||||
/* RFU */
|
||||
tm_uuid[2 + 1 + 4 + 6 + 1] = 0x00;
|
||||
tm_uuid[2 + 1 + 4 + 6 + 2] = 0x00;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn num_to_str
|
||||
*
|
||||
* @brief 数字转字符
|
||||
*
|
||||
* @param out - 输出
|
||||
* @param in - 输入
|
||||
* @param in_len - 长度
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void num_to_str(uint8_t *out, const uint8_t *in, uint16_t in_len)
|
||||
{
|
||||
uint16_t i;
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
|
||||
for(i = 0; i < in_len; i++)
|
||||
{
|
||||
out[i * 2] = hex[in[i] >> 4];
|
||||
out[i * 2 + 1] = hex[in[i] & 0xf];
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn oob_key_generate
|
||||
*
|
||||
* @brief 生成OOB key
|
||||
*
|
||||
* @param cfg - 配置信息
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void oob_key_generate(struct bt_als_cfg const *cfg)
|
||||
{
|
||||
int err;
|
||||
uint32_t pid;
|
||||
uint8_t out[8 + 1 + 12 + 1 + 32], dig[32];
|
||||
struct tc_sha256_state_struct s;
|
||||
|
||||
tc_sha256_init(&s);
|
||||
|
||||
/** pid value */
|
||||
pid = ((uint32_t)((((cfg->pid) >> 24) & 0xff) | (((cfg->pid) >> 8) & 0xff00) | (((cfg->pid) & 0xff00) << 8) | (((cfg->pid) & 0xff) << 24)));
|
||||
num_to_str(out, (void *)&pid, 4);
|
||||
|
||||
/** Separator */
|
||||
strcpy((void *)(out + 8), ",");
|
||||
/** mac value */
|
||||
num_to_str(out + 8 + 1, (void *)cfg->mac, 6);
|
||||
|
||||
/** Separator */
|
||||
strcpy((void *)(out + 8 + 1 + 12), ",");
|
||||
/** secret value */
|
||||
num_to_str(out + 8 + 1 + 12 + 1, (void *)cfg->secret, 16);
|
||||
|
||||
err = tc_sha256_update(&s, out, sizeof(out));
|
||||
if(err != TC_CRYPTO_SUCCESS)
|
||||
{
|
||||
APP_DBG("Unable Update Sha256");
|
||||
return;
|
||||
}
|
||||
|
||||
err = tc_sha256_final(dig, &s);
|
||||
if(err != TC_CRYPTO_SUCCESS)
|
||||
{
|
||||
APP_DBG("Unable Generate sha256 value");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(static_key, dig, 16);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ind_reset
|
||||
*
|
||||
* @brief 移除列表,调用发送完成回调,释放缓存
|
||||
*
|
||||
* @param ind - 需要重置的通知
|
||||
* @param err - 错误码
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void ind_reset(struct bt_mesh_indicate *ind, int err)
|
||||
{
|
||||
if(ind->param.cb && ind->param.cb->end)
|
||||
{
|
||||
ind->param.cb->end(err, ind->param.cb_data);
|
||||
}
|
||||
|
||||
tmos_msg_deallocate(ind->buf->__buf);
|
||||
ind->buf->__buf = NULL;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn bt_mesh_indicate_reset
|
||||
*
|
||||
* @brief 释放所有未发送的通知
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void bt_mesh_indicate_reset(void)
|
||||
{
|
||||
uint8_t i;
|
||||
for(i = 0; i < CONFIG_INDICATE_NUM; i++)
|
||||
{
|
||||
if(indicate[i].buf->__buf != NULL)
|
||||
{
|
||||
ind_reset(&indicate[i], -ECANCELED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ind_start
|
||||
*
|
||||
* @brief 发送 indicate 开始回调
|
||||
*
|
||||
* @param duration - 本次发送将要持续的时长
|
||||
* @param err - 错误码
|
||||
* @param cb_data - 回调参数
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void ind_start(uint16_t duration, int err, void *cb_data)
|
||||
{
|
||||
struct bt_mesh_indicate *ind = cb_data;
|
||||
|
||||
if(ind->buf->__buf == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(err)
|
||||
{
|
||||
APP_DBG("Unable send indicate (err:%d)", err);
|
||||
tmos_start_task(als_vendor_model_TaskID, ind->event, K_MSEC(100));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ind_end
|
||||
*
|
||||
* @brief 发送 indicate 结束回调
|
||||
*
|
||||
* @param err - 错误码
|
||||
* @param cb_data - 回调参数
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void ind_end(int err, void *cb_data)
|
||||
{
|
||||
struct bt_mesh_indicate *ind = cb_data;
|
||||
|
||||
if(ind->buf->__buf == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
tmos_start_task(als_vendor_model_TaskID, ind->event, ind->param.period);
|
||||
}
|
||||
|
||||
// 发送 indicate 回调结构体
|
||||
const struct bt_mesh_send_cb ind_cb = {
|
||||
.start = ind_start,
|
||||
.end = ind_end,
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* @fn adv_ind_send
|
||||
*
|
||||
* @brief 发送 indicate
|
||||
*
|
||||
* @param ind - 需要发送的通知
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void adv_ind_send(struct bt_mesh_indicate *ind)
|
||||
{
|
||||
int err;
|
||||
NET_BUF_SIMPLE_DEFINE(msg, 32);
|
||||
|
||||
struct bt_mesh_msg_ctx ctx = {
|
||||
.app_idx = vnd_models[0].keys[0],
|
||||
.addr = ALI_TM_SUB_ADDRESS,
|
||||
};
|
||||
|
||||
if(ind->buf->__buf == NULL)
|
||||
{
|
||||
APP_DBG("NULL buf");
|
||||
return;
|
||||
}
|
||||
|
||||
if(ind->param.trans_cnt == 0)
|
||||
{
|
||||
ind_reset(ind, -ETIMEDOUT);
|
||||
return;
|
||||
}
|
||||
|
||||
ind->param.trans_cnt--;
|
||||
|
||||
ctx.send_ttl = ind->param.send_ttl;
|
||||
|
||||
net_buf_simple_add_mem(&msg, ind->buf->data, ind->buf->len);
|
||||
|
||||
err = bt_mesh_model_send(vnd_models, &ctx, &msg, &ind_cb, ind);
|
||||
if(err)
|
||||
{
|
||||
APP_DBG("Unable send model message (err:%d)", err);
|
||||
ind_reset(ind, -EIO);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn bt_mesh_ind_alloc
|
||||
*
|
||||
* @brief 找一个空的indicate,并分配内存
|
||||
*
|
||||
* @param len - 需要分配的数据长度
|
||||
*
|
||||
* @return indicate结构体指针
|
||||
*/
|
||||
struct bt_mesh_indicate *bt_mesh_ind_alloc(uint16_t len)
|
||||
{
|
||||
uint8_t i;
|
||||
for(i = 0; i < CONFIG_INDICATE_NUM; i++)
|
||||
{
|
||||
if(indicate[i].buf->__buf == NULL)
|
||||
break;
|
||||
}
|
||||
if(i == CONFIG_INDICATE_NUM)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
indicate[i].buf->__buf = tmos_msg_allocate(len);
|
||||
indicate[i].buf->size = len;
|
||||
|
||||
if(indicate[i].buf->__buf == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &indicate[i];
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn bt_mesh_indicate_send
|
||||
*
|
||||
* @brief 启动发送通知的事件
|
||||
*
|
||||
* @param ind - indicate结构体指针
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void bt_mesh_indicate_send(struct bt_mesh_indicate *ind)
|
||||
{
|
||||
tmos_start_task(als_vendor_model_TaskID, ind->event, ind->param.rand);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn send_led_indicate
|
||||
*
|
||||
* @brief 发送当前LED状态,当有LED状态更新时都需要调用此函数
|
||||
*
|
||||
* @param param - 发送通知的发送参数
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void send_led_indicate(struct indicate_param *param)
|
||||
{
|
||||
struct bt_mesh_indicate *ind;
|
||||
|
||||
ind = bt_mesh_ind_alloc(16);
|
||||
if(!ind)
|
||||
{
|
||||
APP_DBG("Unable allocate buffers");
|
||||
return;
|
||||
}
|
||||
memcpy(&(ind->param), param, sizeof(struct indicate_param));
|
||||
|
||||
/* Init indication opcode */
|
||||
bt_mesh_model_msg_init(&(ind->buf->b), OP_VENDOR_MESSAGE_ATTR_INDICATION);
|
||||
|
||||
/* Add tid field */
|
||||
net_buf_simple_add_u8(&(ind->buf->b), param->tid);
|
||||
|
||||
/* Add generic onoff attrbute op */
|
||||
net_buf_simple_add_le16(&(ind->buf->b), ALI_GEN_ATTR_TYPE_POWER_STATE);
|
||||
|
||||
/* Add current generic onoff status */
|
||||
net_buf_simple_add_u8(&(ind->buf->b), read_led_state(MSG_PIN));
|
||||
|
||||
bt_mesh_indicate_send(ind);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn als_vendor_init
|
||||
*
|
||||
* @brief 阿里 厂家模型 初始化
|
||||
*
|
||||
* @param model - 回调模型参数
|
||||
*
|
||||
* @return always success
|
||||
*/
|
||||
int als_vendor_init(struct bt_mesh_model *model)
|
||||
{
|
||||
uint32_t ran;
|
||||
|
||||
uuid_generate(&cfg);
|
||||
oob_key_generate(&cfg);
|
||||
|
||||
/** Random Local TID Value
|
||||
* @Caution Don't use single octer only.
|
||||
*/
|
||||
ran = tmos_rand();
|
||||
als_tid += ((uint8_t *)&ran)[0];
|
||||
als_tid += ((uint8_t *)&ran)[1];
|
||||
als_tid += ((uint8_t *)&ran)[2];
|
||||
als_tid += ((uint8_t *)&ran)[3];
|
||||
|
||||
for(int i = 0; i < CONFIG_INDICATE_NUM; i++)
|
||||
{
|
||||
indicate[i].buf = &ind_buf[i];
|
||||
indicate[i].event = (1 << i);
|
||||
}
|
||||
|
||||
als_vendor_model_TaskID = TMOS_ProcessEventRegister(als_vendor_model_ProcessEvent);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn als_vendor_model_ProcessEvent
|
||||
*
|
||||
* @brief 阿里厂商模型事件处理
|
||||
*
|
||||
* @param task_id - The TMOS assigned task ID.
|
||||
* @param events - events to process. This is a bit map and can
|
||||
* contain more than one event.
|
||||
*
|
||||
* @return events not processed
|
||||
*/
|
||||
static uint16_t als_vendor_model_ProcessEvent(uint8_t task_id, uint16_t events)
|
||||
{
|
||||
for(int i = 0; i < CONFIG_INDICATE_NUM; i++)
|
||||
{
|
||||
if(events & indicate[i].event)
|
||||
{
|
||||
adv_ind_send(&indicate[i]);
|
||||
return (events ^ indicate[i].event);
|
||||
}
|
||||
}
|
||||
|
||||
// Discard unknown events
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
@@ -0,0 +1,74 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_it.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : Main Interrupt Service Routines.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "ch32v20x_it.h"
|
||||
#include "CONFIG.h"
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void BB_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void LLE_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NMI_Handler
|
||||
*
|
||||
* @brief This function handles NMI exception.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HardFault_Handler
|
||||
*
|
||||
* @brief This function handles Hard Fault exception.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn BB_IRQHandler
|
||||
*
|
||||
* @brief BB Interrupt for BLE.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void BB_IRQHandler(void)
|
||||
{
|
||||
BB_IRQLibHandler();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn LLE_IRQHandler
|
||||
*
|
||||
* @brief LLE Interrupt for BLE.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void LLE_IRQHandler(void)
|
||||
{
|
||||
LLE_IRQLibHandler();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/11/12
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef app_H
|
||||
#define app_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define APP_SILENT_ADV_EVT (1 << 0)
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief 应用层初始化
|
||||
*/
|
||||
void App_Init(void);
|
||||
|
||||
/**
|
||||
* @brief 发送当前灯的状态给天猫精灵
|
||||
*/
|
||||
void send_led_state(void);
|
||||
|
||||
/**
|
||||
* @brief 发送复位事件给天猫精灵,发送完成后将清除配网状态,重置自身mesh网络
|
||||
*/
|
||||
void send_reset_indicate(void);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,56 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app_generic_onoff_model.h
|
||||
* Author : WCH
|
||||
* Version : V1.1
|
||||
* Date : 2021/11/18
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef app_generic_onoff_model_H
|
||||
#define app_generic_onoff_model_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "MESH_LIB.h"
|
||||
|
||||
#define MSG_PIN GPIO_Pin_14
|
||||
#define MSG_PCENR RCC_APB2Periph_GPIOB
|
||||
#define MSG_GPIO GPIOB
|
||||
|
||||
extern const struct bt_mesh_model_op gen_onoff_op[];
|
||||
|
||||
/**
|
||||
* @brief 삿혤뎠품됐榴檄.
|
||||
*
|
||||
* @param led_pin - 多신.
|
||||
*
|
||||
* @return 됐榴檄
|
||||
*/
|
||||
BOOL read_led_state(uint32_t led_pin);
|
||||
|
||||
/**
|
||||
* @brief �零뎠품됐榴檄.
|
||||
*
|
||||
* @param led_pin - 多신.
|
||||
* @param on - 榴檄.
|
||||
*/
|
||||
void set_led_state(uint32_t led_pin, BOOL on);
|
||||
|
||||
/**
|
||||
* @brief 럇瘻뎠품됐榴檄
|
||||
*
|
||||
* @param led_pin - 多신.
|
||||
*/
|
||||
void toggle_led_state(uint32_t led_pin);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,219 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app_mesh_config.h
|
||||
* Author : WCH
|
||||
* Version : V1.1
|
||||
* Date : 2021/11/18
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef APP_MESH_CONFIG_H
|
||||
#define APP_MESH_CONFIG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/**************************功能配置,建议直接参考不同例程*****************************/
|
||||
|
||||
// relay功能
|
||||
#define CONFIG_BLE_MESH_RELAY 1
|
||||
// 代理功能
|
||||
#define CONFIG_BLE_MESH_PROXY 0
|
||||
// GATT层配网功能
|
||||
#define CONFIG_BLE_MESH_PB_GATT 0
|
||||
// FLASH存储功能
|
||||
#define CONFIG_BLE_MESH_SETTINGS 1
|
||||
// 朋友节点功能
|
||||
#define CONFIG_BLE_MESH_FRIEND 0
|
||||
// 低功耗节点功能
|
||||
#define CONFIG_BLE_MESH_LOW_POWER 0
|
||||
// config模型客户端功能
|
||||
#define CONFIG_BLE_MESH_CFG_CLI 0
|
||||
// health模型客户端功能
|
||||
#define CONFIG_BLE_MESH_HLTH_CLI 0
|
||||
|
||||
/************************************参数配置******************************************/
|
||||
|
||||
// Net数据缓存个数
|
||||
#define CONFIG_MESH_ADV_BUF_COUNT_MIN (6)
|
||||
#define CONFIG_MESH_ADV_BUF_COUNT_DEF (10)
|
||||
#define CONFIG_MESH_ADV_BUF_COUNT_MAX (256)
|
||||
|
||||
// RPL数据缓存个数,需不小于配网发起者支持的配网设备节点个数
|
||||
#define CONFIG_MESH_RPL_COUNT_MIN (6)
|
||||
#define CONFIG_MESH_RPL_COUNT_DEF (20)
|
||||
#define CONFIG_MESH_RPL_COUNT_MAX (128)
|
||||
|
||||
// RPL缓存循环使用,开启后网络中节点数量不受RPL限制,但NVS将不存储RPL数据
|
||||
#define CONFIG_MESH_ALLOW_RPL_CYCLE (FALSE)
|
||||
|
||||
// IV Update State Timer 基于96H的分频系数
|
||||
#define CONFIG_MESH_IVU_DIVIDER_MIN (1)
|
||||
#define CONFIG_MESH_IVU_DIVIDER_DEF (96)
|
||||
#define CONFIG_MESH_IVU_DIVIDER_MAX (96)
|
||||
|
||||
// 代理黑白名单功能存储个数
|
||||
#define CONFIG_MESH_PROXY_FILTER_MIN (2)
|
||||
#define CONFIG_MESH_PROXY_FILTER_DEF (5)
|
||||
#define CONFIG_MESH_PROXY_FILTER_MAX (20)
|
||||
|
||||
// 消息贮存个数
|
||||
#define CONFIG_MESH_MSG_CACHE_MIN (3)
|
||||
#define CONFIG_MESH_MSG_CACHE_DEF (20)
|
||||
#define CONFIG_MESH_MSG_CACHE_MAX (20)
|
||||
|
||||
// 子网络个数
|
||||
#define CONFIG_MESH_SUBNET_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_SUBNET_COUNT_DEF (1)
|
||||
#define CONFIG_MESH_SUBNET_COUNT_MAX (4)
|
||||
|
||||
// APP key个数
|
||||
#define CONFIG_MESH_APPKEY_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_APPKEY_COUNT_DEF (3)
|
||||
#define CONFIG_MESH_APPKEY_COUNT_MAX (5)
|
||||
|
||||
// 可存储的模型密钥数量
|
||||
#define CONFIG_MESH_MOD_KEY_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_MOD_KEY_COUNT_DEF (1)
|
||||
#define CONFIG_MESH_MOD_KEY_COUNT_MAX (3)
|
||||
|
||||
// 可存储的订阅地址数量
|
||||
#define CONFIG_MESH_MOD_GROUP_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_MOD_GROUP_COUNT_DEF (6)
|
||||
#define CONFIG_MESH_MOD_GROUP_COUNT_MAX (64)
|
||||
|
||||
// 是否允许一个网络中存在同地址的节点(使能后分包功能不可用)
|
||||
#define CONFIG_MESH_ALLOW_SAME_ADDR (FALSE)
|
||||
|
||||
// 不分包消息支持的长度(超过此长度则需要分包,默认值为7,注意同网络里所有设备配置需统一)
|
||||
#define CONFIG_MESH_UNSEG_LENGTH_MIN (7)
|
||||
#define CONFIG_MESH_UNSEG_LENGTH_DEF (7)
|
||||
#define CONFIG_MESH_UNSEG_LENGTH_MAX (221)
|
||||
|
||||
// 每个消息的最大分包数
|
||||
#define CONFIG_MESH_TX_SEG_MIN (2)
|
||||
#define CONFIG_MESH_TX_SEG_DEF (8)
|
||||
#define CONFIG_MESH_TX_SEG_MAX (32)
|
||||
|
||||
// 允许同时存在的分包消息的最大发送个数
|
||||
#define CONFIG_MESH_TX_SEG_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_TX_SEG_COUNT_DEF (2)
|
||||
#define CONFIG_MESH_TX_SEG_COUNT_MAX (4)
|
||||
|
||||
// 允许同时存在的分包消息的最大接收个数
|
||||
#define CONFIG_MESH_RX_SEG_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_RX_SEG_COUNT_DEF (2)
|
||||
#define CONFIG_MESH_RX_SEG_COUNT_MAX (4)
|
||||
|
||||
// 每个接收的分包消息的最大字节数
|
||||
#define CONFIG_MESH_RX_SDU_MIN (12)
|
||||
#define CONFIG_MESH_RX_SDU_DEF (192)
|
||||
#define CONFIG_MESH_RX_SDU_MAX (384)
|
||||
|
||||
// 虚拟地址个数
|
||||
#define CONFIG_MESH_LABEL_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_LABEL_COUNT_DEF (2)
|
||||
#define CONFIG_MESH_LABEL_COUNT_MAX (4)
|
||||
|
||||
// NVS存储使用扇区个数
|
||||
#define CONFIG_MESH_SECTOR_COUNT_MIN (2)
|
||||
#define CONFIG_MESH_SECTOR_COUNT_DEF (3)
|
||||
|
||||
// NVS存储扇区大小
|
||||
#define CONFIG_MESH_SECTOR_SIZE_DEF (4096)
|
||||
|
||||
// NVS存储首地址
|
||||
#define CONFIG_MESH_NVS_ADDR_DEF (448*1024+0x08000000)
|
||||
|
||||
// RPL更新超过多少次后存储
|
||||
#define CONFIG_MESH_RPL_STORE_RATE_MIN (5)
|
||||
#define CONFIG_MESH_RPL_STORE_RATE_DEF (60)
|
||||
#define CONFIG_MESH_RPL_STORE_RATE_MAX (3600)
|
||||
|
||||
// SEQ更新超过多少次后存储
|
||||
#define CONFIG_MESH_SEQ_STORE_RATE_MIN (5)
|
||||
#define CONFIG_MESH_SEQ_STORE_RATE_DEF (60)
|
||||
#define CONFIG_MESH_SEQ_STORE_RATE_MAX (3600)
|
||||
|
||||
// 其他信息更新后存储的超时时长(s)
|
||||
#define CONFIG_MESH_STORE_RATE_MIN (2)
|
||||
#define CONFIG_MESH_STORE_RATE_DEF (2)
|
||||
#define CONFIG_MESH_STORE_RATE_MAX (5)
|
||||
|
||||
// 朋友节点支持的每个消息的分包个数
|
||||
#define CONFIG_MESH_FRIEND_SEG_RX_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_FRIEND_SEG_RX_COUNT_DEF (2)
|
||||
#define CONFIG_MESH_FRIEND_SEG_RX_COUNT_MAX (4)
|
||||
|
||||
// 朋友节点支持的订阅个数
|
||||
#define CONFIG_MESH_FRIEND_SUB_SIZE_MIN (1)
|
||||
#define CONFIG_MESH_FRIEND_SUB_SIZE_DEF (4)
|
||||
#define CONFIG_MESH_FRIEND_SUB_SIZE_MAX (8)
|
||||
|
||||
// 朋友节点支持的低功耗节点个数
|
||||
#define CONFIG_MESH_FRIEND_LPN_COUNT_MIN (1)
|
||||
#define CONFIG_MESH_FRIEND_LPN_COUNT_DEF (1)
|
||||
#define CONFIG_MESH_FRIEND_LPN_COUNT_MAX (4)
|
||||
|
||||
// 朋友节点存储的消息队列大小
|
||||
#define CONFIG_MESH_QUEUE_SIZE_MIN (2)
|
||||
#define CONFIG_MESH_QUEUE_SIZE_DEF (4)
|
||||
#define CONFIG_MESH_QUEUE_SIZE_MAX (30)
|
||||
|
||||
// 朋友节点接收窗口大小(ms)
|
||||
#define CONFIG_MESH_FRIEND_RECV_WIN_MIN (1)
|
||||
#define CONFIG_MESH_FRIEND_RECV_WIN_DEF (30)
|
||||
#define CONFIG_MESH_FRIEND_RECV_WIN_MAX (255)
|
||||
|
||||
// 低功耗节点的请求消息队列大小
|
||||
#define CONFIG_MESH_LPN_REQ_QUEUE_SIZE_MIN (2)
|
||||
#define CONFIG_MESH_LPN_REQ_QUEUE_SIZE_DEF (2)
|
||||
#define CONFIG_MESH_LPN_REQ_QUEUE_SIZE_MAX (20)
|
||||
|
||||
// 低功耗节点的请求消息间隔(100ms)
|
||||
#define CONFIG_MESH_LPN_POLLINTERVAL_MIN (1)
|
||||
#define CONFIG_MESH_LPN_POLLINTERVAL_DEF (80)
|
||||
#define CONFIG_MESH_LPN_POLLINTERVAL_MAX (3455999)
|
||||
|
||||
// 低功耗节点的请求消息超时时长(100ms)
|
||||
#define CONFIG_MESH_LPN_POLLTIMEOUT_MIN (10)
|
||||
#define CONFIG_MESH_LPN_POLLTIMEOUT_DEF (300)
|
||||
#define CONFIG_MESH_LPN_POLLTIMEOUT_MAX (3455999)
|
||||
|
||||
// 低功耗节点支持的接收延迟(ms)
|
||||
#define CONFIG_MESH_LPN_RECV_DELAY_MIN (10)
|
||||
#define CONFIG_MESH_LPN_RECV_DELAY_DEF (100)
|
||||
#define CONFIG_MESH_LPN_RECV_DELAY_MAX (255)
|
||||
|
||||
// 朋友关系重建等待时长(s)
|
||||
#define CONFIG_MESH_RETRY_TIMEOUT_MIN (3)
|
||||
#define CONFIG_MESH_RETRY_TIMEOUT_DEF (10)
|
||||
#define CONFIG_MESH_RETRY_TIMEOUT_MAX (60)
|
||||
|
||||
// 配网发起者支持的配网设备节点个数
|
||||
#define CONFIG_MESH_PROV_NODE_COUNT_DEF (0)
|
||||
|
||||
// ADV_RF配置
|
||||
#define CONFIG_MESH_RF_ACCESSADDRESS (0x8E89BED6)
|
||||
#define CONFIG_MESH_RF_CHANNEL_37 (37)
|
||||
#define CONFIG_MESH_RF_CHANNEL_38 (38)
|
||||
#define CONFIG_MESH_RF_CHANNEL_39 (39)
|
||||
|
||||
#define APP_DBG(X...) \
|
||||
if(1) \
|
||||
{ \
|
||||
PRINT("APP_DBG %s> ", __FUNCTION__); \
|
||||
PRINT(X); \
|
||||
PRINT("\n"); \
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,188 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : app_vendor_model.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/11/12
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef app_vendor_model_H
|
||||
#define app_vendor_model_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
|
||||
#include "MESH_LIB.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define CID_ALI_GENIE 0x01A8
|
||||
|
||||
#define OP_VENDOR_MESSAGE_ATTR_GET BLE_MESH_MODEL_OP_3(0xD0, CID_ALI_GENIE)
|
||||
#define OP_VENDOR_MESSAGE_ATTR_SET BLE_MESH_MODEL_OP_3(0xD1, CID_ALI_GENIE)
|
||||
#define OP_VENDOR_MESSAGE_ATTR_SET_UNACK BLE_MESH_MODEL_OP_3(0xD2, CID_ALI_GENIE)
|
||||
#define OP_VENDOR_MESSAGE_ATTR_STATUS BLE_MESH_MODEL_OP_3(0xD3, CID_ALI_GENIE)
|
||||
#define OP_VENDOR_MESSAGE_ATTR_INDICATION BLE_MESH_MODEL_OP_3(0xD4, CID_ALI_GENIE)
|
||||
#define OP_VENDOR_MESSAGE_ATTR_CONFIRMATION BLE_MESH_MODEL_OP_3(0xD5, CID_ALI_GENIE)
|
||||
#define OP_VENDOR_MESSAGE_ATTR_TRANSPARENT_MSG BLE_MESH_MODEL_OP_3(0xCF, CID_ALI_GENIE)
|
||||
|
||||
#define ALI_SYS_ATTR_TYPE_ERROR 0x0000
|
||||
#define ALI_SYS_ATTR_TYPE_VERSION 0xFF01
|
||||
#define ALI_SYS_ATTR_TYPE_DEV_FEATURE 0xFF02
|
||||
#define ALI_SYS_ATTR_TYPE_TOTAL_FLASH_SIZE 0xFF03
|
||||
#define ALI_SYS_ATTR_TYPE_USED_FLASH_SIZE 0xFF04
|
||||
#define ALI_SYS_ATTR_TYPE_FREE_FLASH_SIZE 0xFF05
|
||||
#define ALI_SYS_ATTR_TYPE_ENGINEER_MODE 0xFF06
|
||||
|
||||
#define ALI_GEN_ATTR_TYPE_WORK_STATUS 0xF001
|
||||
#define ALI_GEN_ATTR_TYPE_USER_ID 0xF002
|
||||
#define ALI_GEN_ATTR_TYPE_DEVICE_NAME 0xF003
|
||||
#define ALI_GEN_ATTR_TYPE_MODE_NUMBER 0xF004
|
||||
#define ALI_GEN_ATTR_TYPE_ONOFF_PLAN 0xF008
|
||||
#define ALI_GEN_ATTR_TYPE_EVENT_TRIGGER 0xF009
|
||||
#define ALI_GEN_ATTR_TYPE_EVENT_CLEAR 0xF019
|
||||
#define ALI_GEN_ATTR_TYPE_SIGNAL_INTENSITY 0xF00A
|
||||
#define ALI_GEN_ATTR_TYPE_DELTA_VALUE 0xF00B
|
||||
#define ALI_GEN_ATTR_TYPE_ELEMENT_COUNT 0xF00C
|
||||
#define ALI_GEN_ATTR_TYPE_ATTR_SWITCH 0xF00D
|
||||
#define ALI_GEN_ATTR_TYPE_REMOTE_ADDRESS 0xF00E
|
||||
#define ALI_GEN_ATTR_TYPE_NEARBY_SIGNAL_INTEN 0xF00F
|
||||
#define ALI_GEN_ATTR_TYPE_SET_VALUE_TIMING 0xF010
|
||||
#define ALI_GEN_ATTR_TYPE_SET_VALUE_PERIODIC 0xF011
|
||||
#define ALI_GEN_ATTR_TYPE_DEL_TIMING 0xF012
|
||||
#define ALI_GEN_ATTR_TYPE_REQ_UPDATE_TIMING 0xF013
|
||||
#define ALI_GEN_ATTR_TYPE_SETTING_TIMING 0xF01D
|
||||
#define ALI_GEN_ATTR_TYPE_TIME_ZONE 0xF01E
|
||||
#define ALI_GEN_ATTR_TYPE_UNIX_TIMER 0xF01F
|
||||
#define ALI_GEN_ATTR_TYPE_POWERDOWN_MEM 0xF021
|
||||
#define ALI_GEN_ATTR_TYPE_CALORIES 0x06D5
|
||||
#define ALI_GEN_ATTR_TYPE_SPORTCOUNT 0x0212
|
||||
#define ALI_GEN_ATTR_TYPE_POWER_STATE 0x0100
|
||||
#define ALI_GEN_ATTR_TYPE_BRIGHTNESS 0x0121
|
||||
#define ALI_GEN_ATTR_TYPE_HARDWARE_RESET 0x0023
|
||||
|
||||
#define ALI_TM_SUB_ADDRESS 0xF000
|
||||
|
||||
/** Default number of Indication */
|
||||
#define CONFIG_INDICATE_NUM (3)
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief 发送数据的开始和结束回调函数定义
|
||||
*/
|
||||
struct bt_adv_ind_send_cb
|
||||
{
|
||||
void (*start)(int err, void *cb_data);
|
||||
void (*end)(int err, void *cb_data);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 发送参数结构体
|
||||
*/
|
||||
struct indicate_param
|
||||
{
|
||||
uint8_t tid;
|
||||
uint8_t trans_cnt;
|
||||
int32_t period;
|
||||
int32_t rand;
|
||||
uint8_t send_ttl;
|
||||
|
||||
void *cb_data;
|
||||
const struct bt_adv_ind_send_cb *cb;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief indicate 发送缓存
|
||||
*/
|
||||
struct bt_mesh_indicate
|
||||
{
|
||||
struct indicate_param param;
|
||||
struct net_buf *buf;
|
||||
uint8_t event;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 天猫精灵相关信息结构体
|
||||
*/
|
||||
struct bt_als_cfg
|
||||
{
|
||||
/** Company Identify */
|
||||
uint16_t cid;
|
||||
|
||||
/** Product Identify */
|
||||
uint32_t pid;
|
||||
|
||||
/** Mac Address */
|
||||
uint8_t mac[6];
|
||||
|
||||
/** Secret Information */
|
||||
uint8_t secret[16];
|
||||
|
||||
/** Currently Library Version */
|
||||
uint32_t version;
|
||||
};
|
||||
|
||||
extern struct bt_mesh_model vnd_models[1];
|
||||
extern uint8_t static_key[16];
|
||||
extern uint8_t tm_uuid[16];
|
||||
extern const struct bt_mesh_model_cb bt_mesh_als_vendor_model_cb;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief TID selection method
|
||||
*
|
||||
* @return next TID
|
||||
*/
|
||||
uint8_t als_avail_tid_get(void);
|
||||
|
||||
/**
|
||||
* @brief 找一个空的indicate,并分配内存
|
||||
*
|
||||
* @param len - 需要分配的数据长度
|
||||
*
|
||||
* @return indicate结构体指针
|
||||
*/
|
||||
struct bt_mesh_indicate *bt_mesh_ind_alloc(uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief 启动发送通知的事件
|
||||
*
|
||||
* @param ind - indicate结构体指针
|
||||
*/
|
||||
void bt_mesh_indicate_send(struct bt_mesh_indicate *ind);
|
||||
|
||||
/**
|
||||
* @brief 发送当前LED状态,当有LED状态更新时都需要调用此函数
|
||||
*
|
||||
* @param param - 发送通知的发送参数
|
||||
*/
|
||||
void send_led_indicate(struct indicate_param *param);
|
||||
|
||||
/**
|
||||
* @brief 释放所有未发送的通知
|
||||
*/
|
||||
void bt_mesh_indicate_reset(void);
|
||||
|
||||
/**
|
||||
* @brief 阿里 厂家模型 初始化
|
||||
*
|
||||
* @param model - 回调模型参数
|
||||
*
|
||||
* @return always success
|
||||
*/
|
||||
int als_vendor_init(struct bt_mesh_model *model);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_conf.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : Library configuration file.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __CH32V20x_CONF_H
|
||||
#define __CH32V20x_CONF_H
|
||||
|
||||
#include "ch32v20x_adc.h"
|
||||
#include "ch32v20x_bkp.h"
|
||||
#include "ch32v20x_can.h"
|
||||
#include "ch32v20x_crc.h"
|
||||
#include "ch32v20x_dbgmcu.h"
|
||||
#include "ch32v20x_dma.h"
|
||||
#include "ch32v20x_exti.h"
|
||||
#include "ch32v20x_flash.h"
|
||||
#include "ch32v20x_gpio.h"
|
||||
#include "ch32v20x_i2c.h"
|
||||
#include "ch32v20x_iwdg.h"
|
||||
#include "ch32v20x_pwr.h"
|
||||
#include "ch32v20x_rcc.h"
|
||||
#include "ch32v20x_rtc.h"
|
||||
#include "ch32v20x_spi.h"
|
||||
#include "ch32v20x_tim.h"
|
||||
#include "ch32v20x_usart.h"
|
||||
#include "ch32v20x_wwdg.h"
|
||||
#include "ch32v20x_it.h"
|
||||
#include "ch32v20x_misc.h"
|
||||
|
||||
#endif /* __CH32V20x_CONF_H */
|
||||
@@ -0,0 +1,18 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_it.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : This file contains the headers of the interrupt handlers.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __CH32V20x_IT_H
|
||||
#define __CH32V20x_IT_H
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
#endif /* __CH32V20x_IT_H */
|
||||
@@ -0,0 +1,31 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : system_ch32v20x.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : CH32V20x Device Peripheral Access Layer System Header File.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __SYSTEM_ch32v20x_H
|
||||
#define __SYSTEM_ch32v20x_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
|
||||
|
||||
/* System_Exported_Functions */
|
||||
extern void SystemInit(void);
|
||||
extern void SystemCoreClockUpdate(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__CH32V20x_SYSTEM_H */
|
||||
@@ -0,0 +1,989 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : system_ch32v20x.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2021/06/06
|
||||
* Description : CH32V20x Device Peripheral Access Layer System Source File.
|
||||
* For HSE = 32Mhz (CH32V208x/CH32V203RBT6)
|
||||
* For HSE = 8Mhz (other CH32V203x)
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include "ch32v20x.h"
|
||||
|
||||
/*
|
||||
* Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
|
||||
* reset the HSI is used as SYSCLK source).
|
||||
* If none of the define below is enabled, the HSI is used as System clock source.
|
||||
*/
|
||||
//#define SYSCLK_FREQ_HSE HSE_VALUE
|
||||
//#define SYSCLK_FREQ_48MHz_HSE 48000000
|
||||
//#define SYSCLK_FREQ_56MHz_HSE 56000000
|
||||
//#define SYSCLK_FREQ_72MHz_HSE 72000000
|
||||
#define SYSCLK_FREQ_96MHz_HSE 96000000
|
||||
//#define SYSCLK_FREQ_120MHz_HSE 120000000
|
||||
//#define SYSCLK_FREQ_144MHz_HSE 144000000
|
||||
//#define SYSCLK_FREQ_HSI HSI_VALUE
|
||||
//#define SYSCLK_FREQ_48MHz_HSI 48000000
|
||||
//#define SYSCLK_FREQ_56MHz_HSI 56000000
|
||||
//#define SYSCLK_FREQ_72MHz_HSI 72000000
|
||||
//#define SYSCLK_FREQ_96MHz_HSI 96000000
|
||||
//#define SYSCLK_FREQ_120MHz_HSI 120000000
|
||||
//#define SYSCLK_FREQ_144MHz_HSI 144000000
|
||||
|
||||
/* Clock Definitions */
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#else
|
||||
uint32_t SystemCoreClock = HSI_VALUE; /* System Clock Frequency (Core Clock) */
|
||||
|
||||
#endif
|
||||
|
||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
|
||||
|
||||
/* system_private_function_proto_types */
|
||||
static void SetSysClock(void);
|
||||
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
static void SetSysClockToHSE( void );
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
static void SetSysClockTo48_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
static void SetSysClockTo56_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
static void SetSysClockTo72_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
static void SetSysClockTo96_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
static void SetSysClockTo120_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
static void SetSysClockTo144_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
static void SetSysClockTo48_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
static void SetSysClockTo56_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
static void SetSysClockTo72_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
static void SetSysClockTo96_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
static void SetSysClockTo120_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
static void SetSysClockTo144_HSI( void );
|
||||
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SystemInit
|
||||
*
|
||||
* @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
|
||||
* the PLL and update the SystemCoreClock variable.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SystemInit (void)
|
||||
{
|
||||
RCC->CTLR |= (uint32_t)0x00000001;
|
||||
RCC->CFGR0 &= (uint32_t)0xF8FF0000;
|
||||
RCC->CTLR &= (uint32_t)0xFEF6FFFF;
|
||||
RCC->CTLR &= (uint32_t)0xFFFBFFFF;
|
||||
RCC->CFGR0 &= (uint32_t)0xFF80FFFF;
|
||||
RCC->INTR = 0x009F0000;
|
||||
SetSysClock();
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SystemCoreClockUpdate
|
||||
*
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SystemCoreClockUpdate (void)
|
||||
{
|
||||
uint32_t tmp = 0, pllmull = 0, pllsource = 0, Pll_6_5 = 0;
|
||||
|
||||
tmp = RCC->CFGR0 & RCC_SWS;
|
||||
|
||||
switch (tmp)
|
||||
{
|
||||
case 0x00:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04:
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08:
|
||||
pllmull = RCC->CFGR0 & RCC_PLLMULL;
|
||||
pllsource = RCC->CFGR0 & RCC_PLLSRC;
|
||||
pllmull = ( pllmull >> 18) + 2;
|
||||
|
||||
if(pllmull == 17) pllmull = 18;
|
||||
|
||||
if (pllsource == 0x00)
|
||||
{
|
||||
if(EXTEN->EXTEN_CTR & EXTEN_PLL_HSI_PRE){
|
||||
SystemCoreClock = HSI_VALUE * pllmull;
|
||||
}
|
||||
else{
|
||||
SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined (CH32V20x_D8W)
|
||||
if((RCC->CFGR0 & (3<<22)) == (3<<22))
|
||||
{
|
||||
SystemCoreClock = ((HSE_VALUE>>1)) * pllmull;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if ((RCC->CFGR0 & RCC_PLLXTPRE) != (uint32_t)RESET)
|
||||
{
|
||||
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
|
||||
SystemCoreClock = ((HSE_VALUE>>2) >> 1) * pllmull;
|
||||
#else
|
||||
SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
|
||||
SystemCoreClock = (HSE_VALUE>>2) * pllmull;
|
||||
#else
|
||||
SystemCoreClock = HSE_VALUE * pllmull;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if(Pll_6_5 == 1) SystemCoreClock = (SystemCoreClock / 2);
|
||||
|
||||
break;
|
||||
default:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
}
|
||||
|
||||
tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClock
|
||||
*
|
||||
* @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClock(void)
|
||||
{
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
SetSysClockToHSE();
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
SetSysClockTo48_HSE();
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
SetSysClockTo56_HSE();
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
SetSysClockTo72_HSE();
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
SetSysClockTo96_HSE();
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
SetSysClockTo120_HSE();
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
SetSysClockTo144_HSE();
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
SetSysClockTo48_HSI();
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
SetSysClockTo56_HSI();
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
SetSysClockTo72_HSI();
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
SetSysClockTo96_HSI();
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
SetSysClockTo120_HSI();
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
SetSysClockTo144_HSI();
|
||||
|
||||
#endif
|
||||
|
||||
/* If none of the define above is enabled, the HSI is used as System clock
|
||||
* source (default after reset)
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockToHSE
|
||||
*
|
||||
* @brief Sets HSE as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockToHSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
|
||||
|
||||
/* Select HSE as system clock source
|
||||
* CH32V20x_D6 (HSE=8MHZ)
|
||||
* CH32V20x_D8 (HSE=32MHZ)
|
||||
* CH32V20x_D8W (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
|
||||
|
||||
/* Wait till HSE is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo48_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo48_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 6 = 48 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL6);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo56_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo56_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 7 = 56 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL7);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo72_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo72_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 9 = 72 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL9);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo96_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo96_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 12 = 96 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL12);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo120_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo120_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if(HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
#if defined (CH32V20x_D8W)
|
||||
RCC->CFGR0 |= (uint32_t)(3<<22);
|
||||
/* HCLK = SYSCLK/2 */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV2;
|
||||
#else
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
#endif
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 15 = 120 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 15 = 120 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/2 * 15 = 240 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL15);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo144_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo144_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 18 = 144 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL18);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo48_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo48_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 6 = 48 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL6);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo56_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo56_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 7 = 48 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL7);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo72_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo72_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 9 = 72 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL9);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo96_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo96_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 12 = 96 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL12);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo120_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo120_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 15 = 120 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL15);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo144_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo144_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 18 = 144 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL18);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user