From d68908b417453481a7a7acb7a2951938a598c9b8 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:28:43 +0800
Subject: [PATCH 01/16] =?UTF-8?q?=E5=A4=8D=E5=88=B6=E4=BA=86=E6=96=B0?=
=?UTF-8?q?=E7=9A=84common=EF=BC=8C=E5=BC=80=E4=BA=86SLEEP=E6=B2=A1?=
=?UTF-8?q?=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.settings/language.settings.xml | 2 +-
APP/peripheral_main.c | 2 +-
HAL/include/CONFIG.h | 2 +-
common/letter-shell/extensions/log/log.h | 2 +-
common/letter-shell/shell_cfg.h | 2 +-
common/letter-shell/shell_port.h | 2 +-
6 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml
index 2e3b5bb..18c8056 100644
--- a/.settings/language.settings.xml
+++ b/.settings/language.settings.xml
@@ -5,7 +5,7 @@
-
+
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index b7da959..b70f54f 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -145,7 +145,7 @@ int main(void)
// UART1_DefInit();
// 改成1500000波特率了
- BSP_UART1_Init(460800);
+ BSP_UART1_Init(1500000);
// logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
#endif
diff --git a/HAL/include/CONFIG.h b/HAL/include/CONFIG.h
index 9508fd2..672d892 100644
--- a/HAL/include/CONFIG.h
+++ b/HAL/include/CONFIG.h
@@ -82,7 +82,7 @@
#define DCDC_ENABLE TRUE
#endif
#ifndef HAL_SLEEP
-#define HAL_SLEEP FALSE
+#define HAL_SLEEP TRUE
#endif
#ifndef SLEEP_RTC_MIN_TIME
#define SLEEP_RTC_MIN_TIME US_TO_RTC(1000)
diff --git a/common/letter-shell/extensions/log/log.h b/common/letter-shell/extensions/log/log.h
index e41dfa1..0842210 100644
--- a/common/letter-shell/extensions/log/log.h
+++ b/common/letter-shell/extensions/log/log.h
@@ -27,7 +27,7 @@ extern "C" {
#define LOG_MAX_NUMBER 5 /**< 鍏佽娉ㄥ唽鐨勬渶澶og瀵硅薄鏁伴噺 */
#define LOG_AUTO_TAG 1 /**< 鏄惁鑷姩娣诲姞TAG */
#define LOG_END "\r\n" /**< log淇℃伅缁撳熬 */
-#define LOG_TIME_STAMP 0 /**< 璁剧疆鑾峰彇绯荤粺鏃堕棿鎴 */
+#define LOG_TIME_STAMP BSP_Get_Tick() /**< 璁剧疆鑾峰彇绯荤粺鏃堕棿鎴 */
#ifndef LOG_TAG
#define LOG_TAG __FUNCTION__ /**< 鑷畾娣诲姞鐨凾AG */
diff --git a/common/letter-shell/shell_cfg.h b/common/letter-shell/shell_cfg.h
index b2f2b58..10c6e0a 100644
--- a/common/letter-shell/shell_cfg.h
+++ b/common/letter-shell/shell_cfg.h
@@ -185,7 +185,7 @@
* 瀹氫箟姝ゅ畯涓鸿幏鍙栫郴缁烼ick锛屽`HAL_GetTick()`
* @note 姝ゅ畯涓嶅畾涔夋椂鏃犳硶浣跨敤鍙屽嚮tab琛ュ叏鍛戒护help锛屾棤娉曚娇鐢╯hell瓒呮椂閿佸畾
*/
-#define SHELL_GET_TICK() BSP_Get_Tick()
+#define SHELL_GET_TICK() TMOS_GetSystemClock()
#endif /** SHELL_GET_TICK */
#ifndef SHELL_USING_LOCK
diff --git a/common/letter-shell/shell_port.h b/common/letter-shell/shell_port.h
index 4b55018..069982a 100644
--- a/common/letter-shell/shell_port.h
+++ b/common/letter-shell/shell_port.h
@@ -14,7 +14,7 @@
#include "shell.h"
-#define SHELL_BUF_LENGTH 1024
+#define SHELL_BUF_LENGTH 256
extern Shell shell;
From 16ff36c10bc7f631bbafadbd73be7712a07972ff Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:34:51 +0800
Subject: [PATCH 02/16] =?UTF-8?q?float=E6=89=93=E5=8D=B0=E5=8F=AF=E4=BB=A5?=
=?UTF-8?q?=EF=BC=8C=E5=8F=AA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.cproject | 6 +++---
.settings/org.eclipse.core.resources.prefs | 1 +
APP/peripheral_main.c | 16 +++++++++++-----
3 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/.cproject b/.cproject
index 328c437..74bb70a 100644
--- a/.cproject
+++ b/.cproject
@@ -45,7 +45,7 @@
-
+
@@ -90,7 +90,7 @@
-
+
@@ -105,6 +105,7 @@
+
@@ -172,5 +173,4 @@
-
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
index 00dd478..6827126 100644
--- a/.settings/org.eclipse.core.resources.prefs
+++ b/.settings/org.eclipse.core.resources.prefs
@@ -1,4 +1,5 @@
eclipse.preferences.version=1
+encoding//APP/peripheral_main.c=UTF-8
encoding//BSP/inc/bsp_valve.h=UTF-8
encoding//BSP/src/bsp_valve.c=UTF-8
encoding//common/letter-shell/extensions/log/log.h=UTF-8
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index b70f54f..2e46b03 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -3,7 +3,7 @@
* Author : WCH
* Version : V1.1
* Date : 2020/08/06
- * Description : 外设从机应用主函数及任务系统初始化
+ * Description : 澶栬浠庢満搴旂敤涓诲嚱鏁板強浠诲姟绯荤粺鍒濆鍖
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
@@ -11,7 +11,7 @@
*******************************************************************************/
/******************************************************************************/
-/* 头文件包含 */
+/* 澶存枃浠跺寘鍚 */
#include "CONFIG.h"
#include "HAL.h"
#include "gattprofile.h"
@@ -41,7 +41,7 @@ const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
/*********************************************************************
* @fn Main_Circulation
*
- * @brief 主循环
+ * @brief 涓诲惊鐜
*
* @return none
*/
@@ -122,7 +122,7 @@ __attribute__((noinline)) void Main_Circulation()
/*********************************************************************
* @fn main
*
- * @brief 主函数
+ * @brief 涓诲嚱鏁
*
* @return none
*/
@@ -144,10 +144,11 @@ int main(void)
// UART1_DefInit();
- // 改成1500000波特率了
+ // 鏀规垚1500000娉㈢壒鐜囦簡
BSP_UART1_Init(1500000);
// logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
+ logError("涓枃娴嬭瘯 %2.2f", 123.456);
#endif
DelayMs(1000 * 3);
@@ -162,7 +163,12 @@ int main(void)
BSP_ADC_Init();
CH59x_BLEInit();
+
logDebug("%s\n", VER_LIB);
+ uint8_t MacAddr[6];
+ GetMACAddress(MacAddr);
+ logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]);
+
HAL_Init();
GAPRole_PeripheralInit();
Peripheral_Init();
From 0752338f1e4c7c387debbfdc34766edd73c4d1ac Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:35:01 +0800
Subject: [PATCH 03/16] =?UTF-8?q?float=E6=89=93=E5=8D=B0=E5=8F=AF=E4=BB=A5?=
=?UTF-8?q?=EF=BC=8C=E5=8F=AAUse=20wchprintfloat(-lprintfloat)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
APP/peripheral_main.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 2e46b03..3fff56f 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -149,6 +149,7 @@ int main(void)
// logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logError("涓枃娴嬭瘯 %2.2f", 123.456);
+
#endif
DelayMs(1000 * 3);
From aa74f64e75915b9c4b32e3192325bf0cec69a16f Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:37:05 +0800
Subject: [PATCH 04/16] =?UTF-8?q?bsp=5Fadc=E6=8D=A2=E4=BA=86=E6=96=B0?=
=?UTF-8?q?=E7=9A=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
BSP/inc/bsp_adc.h | 2 +-
BSP/src/bsp_adc.c | 42 ++++++++++++++++++++++++++----------------
2 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/BSP/inc/bsp_adc.h b/BSP/inc/bsp_adc.h
index bc5c354..93da33e 100644
--- a/BSP/inc/bsp_adc.h
+++ b/BSP/inc/bsp_adc.h
@@ -12,7 +12,7 @@
#define __BSP_ADC_H__
-#include "CH59x_common.h"
+#include "CONFIG.h"
void BSP_ADC_Init(void);
diff --git a/BSP/src/bsp_adc.c b/BSP/src/bsp_adc.c
index d19ec26..afeadb5 100644
--- a/BSP/src/bsp_adc.c
+++ b/BSP/src/bsp_adc.c
@@ -1,5 +1,14 @@
#include "bsp_adc.h"
+#include "bsp_uart.h"
+#include "log.h"
+
+#undef LOG_ENABLE
+#define LOG_ENABLE 1
+
+#undef LOG_TAG
+#define LOG_TAG "adc"
+
// https://www.cnblogs.com/gscw/p/17682385.html
// 璁$畻鍏紡鍙傝冩墜鍐岋紝寤鸿浣跨敤 鈥滃缓璁疄闄呭彲鐢ㄦ祴閲忕數鍘嬭寖鍥粹 浣滀负 ADC 閲囬泦鐨勫鐩婇夋嫨鍒ゆ柇鏍囧噯銆傝绠楀叕寮忓弬鑰冩墜鍐屻
// 浠呬娇鐢 ADC 鍔熻兘锛屽悓鏃堕檷浣庡姛鑰楋紝鍙互灏嗗紩鑴氱殑鏁板瓧杈撳叆鍔熻兘鍏抽棴锛岄厤缃 R16_PIN_ANALOG_IE銆 娉細濡傛灉寮曡剼鐢ㄤ簬妯℃嫙鍔熻兘锛圓DC/TouchKey锛夛紝寤鸿灏嗚寮曡剼鐨勬暟瀛楄緭鍏ュ姛鑳藉叧闂紝鍗宠缃暟瀛楄緭鍏ョ鐢紝浠庤屽彲浠ラ檷浣庡姛鑰楋紝骞舵湁鍒╀簬鍑忓皯骞叉壈銆
@@ -23,7 +32,7 @@ void BSP_ADC_Init(void)
#if 0
// 卤10C锛屾病鍟ュ弬鑰冧环鍊
/* 娓╁害閲囨牱骞惰緭鍑 */
- PRINT("\n1.Temperature sampling...\n");
+ logDebug("\n1.Temperature sampling...\n");
ADC_InterTSSampInit();
for (i = 0; i < 20; i++)
{
@@ -33,7 +42,7 @@ void BSP_ADC_Init(void)
{
uint32_t C25 = 0;
C25 = (*((PUINT32)ROM_CFG_TMP_25C));
- PRINT("%d %d %d \n", adc_to_temperature_celsius(adcBuff[i]), adcBuff[i], C25);
+ logDebug("%d %d %d \n", adc_to_temperature_celsius(adcBuff[i]), adcBuff[i], C25);
}
#endif
@@ -56,11 +65,12 @@ void BSP_ADC_Init(void)
min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 杞欢婊ゆ尝
max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
}
- printf("min_number = %d, max_number = %d\n", min_number, max_number);
+ logDebug("min_number = %d, max_number = %d", min_number, max_number);
CountBat = (CountBat - min_number - max_number) / 18; // 鍒犻櫎鏈灏忎笌鏈澶у
- printf("AverageCountBat = %d\n", CountBat);
+ logDebug("AverageCountBat = %d", CountBat);
#endif
+#if 0
// 榛樿鎯呭喌涓嬶紝ADC 寮曡剼鍜屾墍鍦 GPIO 寮曡剼鐨勬暟瀛楀姛鑳芥槸鍚屾椂瀛樺湪鐨
// 锛屽湪杩涜 ADC 娴嬮噺鏃跺欙紝闇瑕佸惂 GPIO 璁剧疆涓洪珮闃昏緭鍏
// 锛屽綋 ADC 鐨勭數骞冲浜庝腑闂存佺殑鏃跺欙紝杩欐椂鍊欏線寰浼氬鑷存暟瀛楅儴鍒嗘紡鐢
@@ -75,12 +85,12 @@ void BSP_ADC_Init(void)
ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_0);
RoughCalib_Value = ADC_DataCalib_Rough(); // 鐢ㄤ簬璁$畻ADC鍐呴儴鍋忓樊锛岃褰曞埌鍏ㄥ眬鍙橀噺 RoughCalib_Value涓
- PRINT("RoughCalib_Value =%d \n", RoughCalib_Value);
+ logDebug("RoughCalib_Value =%d", RoughCalib_Value);
// 鍒氫笂鐢碉紝瑕佺粰鐢靛鍏呯數
DelayMs(300);
- printf("PA13:::::\n");
+ logDebug("PA13:::::");
ADC_ChannelCfg(3);
for (i = 0; i < 20; i++)
@@ -88,12 +98,9 @@ void BSP_ADC_Init(void)
adcBuff[i] = ADC_ExcutSingleConver() + RoughCalib_Value; // 杩炵画閲囨牱20娆
// DelayMs(5);
}
- printf("original: ");
- for (i = 0; i < 20; i++)
- {
- PRINT("%d ", adcBuff[i]); // 娉ㄦ剰锛氱敱浜嶢DC鍐呴儴鍋忓樊鐨勫瓨鍦紝褰撻噰鏍风數鍘嬪湪鎵閫夊鐩婅寖鍥存瀬闄愰檮杩戠殑鏃跺欙紝鍙兘浼氬嚭鐜版暟鎹孩鍑虹殑鐜拌薄
- }
- printf("\n");
+ logDebug("original: ");
+ logHexDumpAll(adcBuff, 20);
+ // 娉ㄦ剰锛氱敱浜嶢DC鍐呴儴鍋忓樊鐨勫瓨鍦紝褰撻噰鏍风數鍘嬪湪鎵閫夊鐩婅寖鍥存瀬闄愰檮杩戠殑鏃跺欙紝鍙兘浼氬嚭鐜版暟鎹孩鍑虹殑鐜拌薄
for (i = 0; i < 20; i++)
{
@@ -106,9 +113,9 @@ void BSP_ADC_Init(void)
min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 杞欢婊ゆ尝
max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
}
- printf("min=%d, max=%d, diff=%d\n", min_number, max_number, (max_number - min_number));
+ logDebug("min=%d, max=%d, diff=%d", min_number, max_number, (max_number - min_number));
countadc = (countadc - min_number - max_number) / 18; // 鍒犻櫎鏈灏忎笌鏈澶у
- printf("countaveradc = %d\n", countadc);
+ logDebug("countaveradc = %d", countadc);
/*
int16_t adc_raw;
adc_raw = ADC_ExcutSingleConver() + RoughCalib_Value;
@@ -122,6 +129,9 @@ void BSP_ADC_Init(void)
voltage_mv = adc_raw*1050/4096 + (1050/2);
*/
voltage = (double)(countadc) / 2048 * 1.05;
- printf("voltage=%1.3lf V\n", voltage);
- PRINT("VIN: %1.3lf V\n", voltage * 2);
+ logDebug("voltage=%1.3lf V", voltage);
+ logDebug("VIN: %1.3lf V", voltage * 2);
+#endif
}
+
+
From d3d0b22b1ee4bc6c6801bb78a607c457f600bb64 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:39:03 +0800
Subject: [PATCH 05/16] =?UTF-8?q?bsp=5Fbeep=E6=96=B0=E7=9A=84ok?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
BSP/inc/bsp_beep_led_emv.h | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/BSP/inc/bsp_beep_led_emv.h b/BSP/inc/bsp_beep_led_emv.h
index dd30e43..f009f8c 100644
--- a/BSP/inc/bsp_beep_led_emv.h
+++ b/BSP/inc/bsp_beep_led_emv.h
@@ -2,8 +2,8 @@
* @Author : stark1898y 1658608470@qq.com
* @Date : 2024-12-09 09:50:56
* @LastEditors : stark1898y 1658608470@qq.com
- * @LastEditTime : 2024-12-09 11:27:01
- * @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_beep_led_emv.h
+ * @LastEditTime : 2024-12-15 15:37:31
+ * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\inc\bsp_beep_led_emv.h
* @Description :
*
* Copyright (c) 2024 by yzy, All Rights Reserved.
@@ -11,22 +11,23 @@
#ifndef __BSP_BEEP_LED_EMV_H__
#define __BSP_BEEP_LED_EMV_H__
-#include "CH59x_common.h"
+
+#include "CONFIG.h"
-/**********************BOOST************************ */
+/**********************BOOST 12V_EN************************ */
#define BOOST_EN \
do \
{ \
- GPIOB_SetBits(GPIO_Pin_14); \
- GPIOB_ModeCfg(GPIO_Pin_14, GPIO_ModeOut_PP_5mA); \
+ GPIOB_SetBits(GPIO_Pin_4); \
+ GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeOut_PP_5mA); \
} while (0);
#define BOOST_OFF_DEINIT \
do \
{ \
- GPIOB_ResetBits(GPIO_Pin_14); \
- GPIOB_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_PD); \
+ GPIOB_ResetBits(GPIO_Pin_4); \
+ GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PD); \
} while (0);
/***********************BEEP************************ */
@@ -105,4 +106,5 @@
GPIOB_ModeCfg(GPIO_Pin_12, GPIO_ModeIN_Floating); \
} while (0);
+
#endif // !__BSP_BEEP_LED_EMV_H__
From c7eb86a58c6bf6e79a2c2c908bec4909de6f88e7 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:41:49 +0800
Subject: [PATCH 06/16] bsp_flash ok
---
APP/peripheral_main.c | 4 ++++
BSP/inc/bsP_bt.h | 21 ---------------------
2 files changed, 4 insertions(+), 21 deletions(-)
delete mode 100644 BSP/inc/bsP_bt.h
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 3fff56f..5986dd1 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -17,6 +17,7 @@
#include "gattprofile.h"
#include "peripheral.h"
+#include "bsp_flash.h"
#include "bsp_adc.h"
#include "bsp_key.h"
@@ -173,6 +174,9 @@ int main(void)
HAL_Init();
GAPRole_PeripheralInit();
Peripheral_Init();
+
+ BSP_FLASH_Init();
+
Main_Circulation();
}
diff --git a/BSP/inc/bsP_bt.h b/BSP/inc/bsP_bt.h
deleted file mode 100644
index 102bcf3..0000000
--- a/BSP/inc/bsP_bt.h
+++ /dev/null
@@ -1,21 +0,0 @@
-
-
-struct __attribute__((packed)) valve_data
-{
- // uint8_t valve_id;
- // uint8_t valve_mac[6];
- uint8_t header;
- uint8_t cmd;
- uint8_t data_len;
- uint8_t valve_type; // 闃闂ㄧ被鍨
- uint8_t valve_bat; // 1B 鐢垫睜鐢靛帇 30=3V,18=1.8V
- uint8_t valve_status; // 1B 闃闂ㄥ伐浣滅姸鎬 瓒呮瑺鍘嬨佽繃娴
- uint8_t valve_connct_status; // 1B 闃闂ㄨ繛鎺ョ姸鎬 锛堢數纾侀榾BLE鎺у埗鐩掞級 0=鏈繛鎺 1=宸茶繛鎺
- uint8_t valve_switch_status; // 1B 闃闂ㄥ紑鍏崇姸鎬 0=鏈煡 1=鍏抽棴 2=鍏抽棴杩囷紝鏈寜鎸夐敭鎭㈠ 3=鍏抽棴杩囷紝鎸変笅浜嗘仮澶嶉敭 4=鎵撳紑
- int8_t valve_temp; // 1B 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩
- uint8_t valve_humidity; // 1B 闃闂ㄦ箍搴 0~100%;
- uint32_t valve_in_pressure; // 4B 闃闂ㄥ叆鍙e帇鍔 Pa
- uint32_t valve_out_pressure; // 4B 闃闂ㄥ嚭鍙e帇鍔 Pa
- uint32_t valve_atm_pressure; // 4B 闃闂ㄥぇ姘斿帇鍔 Pa
- uint8_t tail;
-};
From a4e58354cd2f4fe4cdaf15aeec7a6263f10d19fc Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 15:45:11 +0800
Subject: [PATCH 07/16] =?UTF-8?q?bsp=5Fi2c=20=E5=87=BA=E7=8E=B0=E6=89=93?=
=?UTF-8?q?=E5=8D=B0float=E5=BC=82=E5=B8=B8=EF=BC=8C=E8=BF=9E=E6=8E=A5?=
=?UTF-8?q?=E4=B8=8A=E5=87=BA=E9=94=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
APP/peripheral_main.c | 5 +
BSP/inc/bsp_i2c.h | 143 ++++++++
BSP/src/bsp_i2c.c | 759 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 907 insertions(+)
create mode 100644 BSP/inc/bsp_i2c.h
create mode 100644 BSP/src/bsp_i2c.c
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 5986dd1..734ec73 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -24,6 +24,8 @@
#include "bsp_beep_led_emv.h"
+#include "bsp_i2c.h"
+
#include "log.h"
#include "bsp_uart.h"
@@ -175,6 +177,9 @@ int main(void)
GAPRole_PeripheralInit();
Peripheral_Init();
+ DelayMs(100);
+ GXHTC3C_Init();
+
BSP_FLASH_Init();
Main_Circulation();
diff --git a/BSP/inc/bsp_i2c.h b/BSP/inc/bsp_i2c.h
new file mode 100644
index 0000000..ada6b44
--- /dev/null
+++ b/BSP/inc/bsp_i2c.h
@@ -0,0 +1,143 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-15 15:42:00
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 15:42:00
+ * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\inc\bsp_i2c.h
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
+#ifndef __BSP_I2C_H__
+#define __BSP_I2C_H__
+
+#include
+#include
+#include
+
+#include "CONFIG.h"
+
+#define GXHTC3C_ADDR 0x70
+// #define GXHTC3C_7BIT_ADDR 0x38 // (GXHTC3C_ADDR >> 1)
+
+// 渚涚數鐢靛帇VDD浠0涓婂崌涓婄數鐢靛帇VPOR锛岃姱鐗囦細杩涘叆绌洪棽鐘舵併傜劧鍚庡簲璇ラ氳繃鍙戦佸懡浠よ鑺墖杩涘叆浼戠湢鐘舵佷互闄嶄綆鑺墖鍔熻
+#define GXHTC3C_CMD_SLEEP 0xB098
+// 褰撹姱鐗囧浜庝紤鐪犵姸鎬佹椂锛屽鏋滆杩涜鍏跺畠鐨勫懡浠ゆ搷浣滐紝闇瑕佸彂閫佸敜閱掑懡浠
+#define GXHTC3C_CMD_WAKEUP 0x3517
+
+#define GXHTC3C_CMD_NORMAL_CLK_STRE_ON_TEMP_FRONT 0x7CA2
+#define GXHTC3C_CMD_NORMAL_CLK_STRE_ON_HUMI_FRONT 0x5C24
+
+#define GXHTC3C_CMD_NORMAL_CLK_STRE_OFF_TEMP_FRONT 0x7866
+#define GXHTC3C_CMD_NORMAL_CLK_STRE_OFF_HUMI_FRONT 0x58E0
+
+#define GXHTC3C_CMD_LOW_CLK_STRE_ON_TEMP_FRONT 0x6458
+#define GXHTC3C_CMD_LOW_CLK_STRE_ON_HUMI_FRONT 0x44DE
+
+#define GXHTC3C_CMD_LOW_CLK_STRE_OFF_TEMP_FRONT 0x609C
+#define GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT 0x401A
+
+#define GXHTC3C_CMD_SOFT_REST 0x805D
+
+// 璇讳紶鎰熷櫒搴忓垪鍙
+#define GXHTC3C_CMD_READ_ID 0xEFC8
+
+#define I2C_BUFFER_LENGTH 32
+#define I2C_READ 1
+#define I2C_WRITE 0
+
+typedef enum {
+ I2C_READY,
+ I2C_MRX,
+ I2C_MTX,
+ I2C_SRX,
+ I2C_STX,
+}i2c_state_t;
+
+typedef enum {
+ I2C_NO_MEM = 1,
+ I2C_STATE,
+ I2C_MT_NACK,
+ I2C_ARB_LOST,
+ I2C_BUS_ERROR,
+ I2C_OVR,
+ I2C_PECERR,
+ I2C_TIMEOUT,
+ I2C_SMBALERT,
+}i2c_error_t;
+
+/**
+ * @brief User callback function on I2C slave transmitting.
+ *
+ * @param data Pointer to user data to transmit.
+ *
+ * @param len Pointer to user data length.
+ */
+typedef void (*i2c_on_slave_transmit)(uint8_t *data, uint8_t *len);
+
+/**
+ * @brief User callback function on I2C slave received.
+ *
+ * @param data Pointer to current received data.
+ *
+ * @param len Received data length.
+ */
+typedef void (*i2c_on_slave_receive)(uint8_t *data, uint8_t len);
+
+struct i2c_slave_cb {
+ i2c_on_slave_transmit on_transmit;
+ i2c_on_slave_receive on_receive;
+};
+
+// /**
+// * @brief I2C interrupt routine initialization.
+// *
+// * @param address I2C address.
+// */
+// void i2c_app_init(uint8_t address);
+
+/**
+ * @brief I2C slave user callback function regiester.
+ *
+ * @param cb Pointer to user callback function.
+ */
+void i2c_slave_cb_register(struct i2c_slave_cb *cb);
+
+/**
+ * @brief I2C master write data to slave.
+ *
+ * @param addr_7bit I2C slave 7bit address.
+ * @param data Pointer to the write data.
+ * @param length Write data length.
+ * @param wait Choose to wait for the write process to end or not.
+ * @param send_stop Choose to send stop or not.
+ * @return 0 If successful.
+ */
+int i2c_write_to(uint8_t addr_7bit, const uint8_t *data, uint8_t length,
+ uint8_t wait, uint8_t send_stop);
+
+/**
+ * @brief I2C master read data to slave
+ *
+ * @param addr_7bit I2C slave 7bit address.
+ * @param data Pointer to the read data to put in.
+ * @param length Read data length.
+ * @param send_stop Choose to send stop or not
+ * @param timeout Read process timeout.
+ * @return Negative on error code otherwise indicates the actual read length.
+ */
+int i2c_read_from(uint8_t addr_7bit, uint8_t *data, uint8_t length,
+ uint8_t send_stop, int timeout);
+
+void GXHTC3C_Sleep(void);
+void GXHTC3C_Wakeup(void);
+void GXHTC3C_GetStart(void);
+uint8_t GXHTC3C_GetTempHumi(float *humi, float *temp);
+
+void BSP_I2C_DeInit(void);
+
+void GXHTC3C_Init(void);
+
+
+#endif // !__BSP_I2C_H__
+
diff --git a/BSP/src/bsp_i2c.c b/BSP/src/bsp_i2c.c
new file mode 100644
index 0000000..926dfce
--- /dev/null
+++ b/BSP/src/bsp_i2c.c
@@ -0,0 +1,759 @@
+#include "bsp_i2c.h"
+
+#include "bsp_uart.h"
+#include "log.h"
+
+#undef LOG_ENABLE
+#define LOG_ENABLE 1
+
+#undef LOG_TAG
+#define LOG_TAG "I2C"
+
+// #define CONFIG_I2C_DEBUG
+
+#ifdef CONFIG_I2C_DEBUG
+#define I2C_DBG(...) logDebug(__VA_ARGS__)
+#else
+#define I2C_DBG(...)
+#endif
+
+static volatile uint8_t i2c_state;
+static volatile uint8_t i2c_slave_addr_rw;
+static volatile uint8_t i2c_send_stop; // should the transaction end with a stop
+static volatile uint8_t i2c_in_repstart; // in the middle of a repeated start
+
+static uint8_t i2c_master_buffer[I2C_BUFFER_LENGTH];
+static volatile uint8_t i2c_master_buffer_index;
+static uint8_t i2c_master_buffer_length;
+
+static uint8_t i2c_slave_txbuffer[I2C_BUFFER_LENGTH];
+static volatile uint8_t i2c_slave_txbuffer_index;
+static uint8_t i2c_slave_txbuffer_length;
+
+static uint8_t i2c_slave_rxbuffer[I2C_BUFFER_LENGTH];
+static volatile uint8_t i2c_slave_rxbuffer_index;
+
+static uint8_t is_nack_sent = false;
+
+static volatile uint8_t i2c_error;
+
+static struct i2c_slave_cb *slave_cb = NULL;
+
+#define MASTER_ADDR 0x42
+
+uint8_t i2c_tx_data[2];
+uint8_t i2c_rx_data[6];
+
+void BSP_I2C_Init(uint8_t address)
+{
+ i2c_state = I2C_READY;
+ i2c_send_stop = true;
+ i2c_in_repstart = false;
+
+ GPIOB_ModeCfg(GPIO_Pin_14 | GPIO_Pin_15, GPIO_ModeIN_PU);
+
+ I2C_Init(I2C_Mode_I2C, 400000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, address);
+
+ I2C_ITConfig(I2C_IT_BUF, ENABLE);
+ I2C_ITConfig(I2C_IT_EVT, ENABLE);
+ I2C_ITConfig(I2C_IT_ERR, ENABLE);
+
+ PFIC_EnableIRQ(I2C_IRQn);
+
+ logDebug("BSP_I2C_Init(%02x)", address);
+}
+
+void i2c_slave_cb_register(struct i2c_slave_cb *cb)
+{
+ slave_cb = cb;
+}
+
+int I2C_Write(uint8_t addr_7bit, const uint8_t *data, uint8_t length,
+ uint8_t wait, uint8_t send_stop)
+{
+ if (length > I2C_BUFFER_LENGTH)
+ {
+ return -I2C_NO_MEM;
+ }
+
+ if (i2c_state != I2C_READY)
+ {
+ return -I2C_STATE;
+ }
+
+ if (!length)
+ {
+ return 0;
+ }
+
+ i2c_state = I2C_MTX;
+ i2c_send_stop = send_stop;
+
+ i2c_error = 0;
+
+ // initialize buffer iteration vars
+ i2c_master_buffer_index = 0;
+ i2c_master_buffer_length = length;
+
+ memcpy(i2c_master_buffer, data, length);
+
+ i2c_slave_addr_rw = I2C_WRITE;
+ i2c_slave_addr_rw |= addr_7bit << 1;
+
+ I2C_GenerateSTOP(DISABLE);
+
+ if (i2c_in_repstart == true)
+ {
+ i2c_in_repstart = false;
+
+ do {
+ I2C_SendData(i2c_slave_addr_rw);
+ } while (R16_I2C_CTRL1 & RB_I2C_BTF);
+
+ /* Disabled in IRS */
+ I2C_ITConfig(I2C_IT_BUF, ENABLE);
+ I2C_ITConfig(I2C_IT_EVT, ENABLE);
+ I2C_ITConfig(I2C_IT_ERR, ENABLE);
+ }
+ else
+ {
+ I2C_GenerateSTART(ENABLE);
+ }
+
+ while (wait && (i2c_state == I2C_MTX))
+ {
+ continue;
+ }
+
+ if (i2c_error)
+ {
+ return -i2c_error;
+ }
+
+ return 0;
+}
+
+int I2C_Read(uint8_t addr_7bit, uint8_t *data, uint8_t length,
+ uint8_t send_stop, int timeout)
+{
+ int to = 0;
+ uint8_t forever = (timeout == -1);
+
+ if (length > I2C_BUFFER_LENGTH)
+ {
+ return -I2C_NO_MEM;
+ }
+
+ if (i2c_state != I2C_READY)
+ {
+ return -I2C_STATE;
+ }
+
+ if (!length)
+ {
+ return 0;
+ }
+
+ i2c_state = I2C_MRX;
+ i2c_send_stop = send_stop;
+
+ i2c_error = 0;
+
+ // initialize buffer iteration vars
+ i2c_master_buffer_index = 0;
+ i2c_master_buffer_length = length - 1;
+
+ i2c_slave_addr_rw = I2C_READ;
+ i2c_slave_addr_rw |= addr_7bit << 1;
+
+ I2C_GenerateSTOP(DISABLE);
+
+ if (i2c_in_repstart == true)
+ {
+ i2c_in_repstart = false;
+
+ do {
+ I2C_SendData(i2c_slave_addr_rw);
+ } while (R16_I2C_CTRL1 & RB_I2C_BTF);
+
+ /* Disabled in IRS */
+ I2C_ITConfig(I2C_IT_BUF, ENABLE);
+ I2C_ITConfig(I2C_IT_EVT, ENABLE);
+ I2C_ITConfig(I2C_IT_ERR, ENABLE);
+ }
+ else
+ {
+ I2C_GenerateSTART(ENABLE);
+ }
+
+ // wait for read operation to complete
+ while (i2c_state == I2C_MRX)
+ {
+ mDelaymS(1);
+ to++;
+ if (!forever && (to >= timeout))
+ {
+ break;
+ }
+ }
+
+ if (i2c_master_buffer_index < length)
+ length = i2c_master_buffer_index;
+
+ // copy i2c buffer to data
+ memcpy(data, i2c_master_buffer, length);
+
+ return length;
+}
+
+#ifdef CONFIG_I2C_DEBUG
+static void print_i2c_irq_sta(uint32_t state)
+{
+ I2C_DBG("i2c irq: ( ");
+
+ if (state & RB_I2C_SB)
+ I2C_DBG("SB ");
+ if (state & RB_I2C_ADDR)
+ I2C_DBG("ADDR ");
+ if (state & RB_I2C_BTF)
+ I2C_DBG("BTF ");
+ if (state & RB_I2C_ADD10)
+ I2C_DBG("ADD10 ");
+ if (state & RB_I2C_STOPF)
+ I2C_DBG("STOP ");
+ if (state & RB_I2C_RxNE)
+ I2C_DBG("RxNE ");
+ if (state & RB_I2C_TxE)
+ I2C_DBG("TxE ");
+ if (state & RB_I2C_BERR)
+ I2C_DBG("BERR ");
+ if (state & RB_I2C_ARLO)
+ I2C_DBG("ARLO ");
+ if (state & RB_I2C_AF)
+ I2C_DBG("AF ");
+ if (state & RB_I2C_OVR)
+ I2C_DBG("OVR ");
+ if (state & RB_I2C_PECERR)
+ I2C_DBG("PECERR ");
+ if (state & RB_I2C_TIMEOUT)
+ I2C_DBG("TIMEOUT ");
+ if (state & RB_I2C_SMBALERT)
+ I2C_DBG("SMBALERT ");
+ if (state & (RB_I2C_MSL << 16))
+ I2C_DBG("MSL ");
+ if (state & (RB_I2C_BUSY << 16))
+ I2C_DBG("BUSY ");
+ if (state & (RB_I2C_TRA << 16))
+ I2C_DBG("TRA ");
+ if (state & (RB_I2C_GENCALL << 16))
+ I2C_DBG("GENCALL ");
+ if (state & (RB_I2C_SMBDEFAULT << 16))
+ I2C_DBG("SMBDEFAULT ");
+ if (state & (RB_I2C_SMBHOST << 16))
+ I2C_DBG("SMBHOST ");
+ if (state & (RB_I2C_DUALF << 16))
+ I2C_DBG("DUALF ");
+
+ I2C_DBG(")");
+}
+#else
+static inline void print_i2c_irq_sta(uint32_t state)
+{
+ (void)state;
+}
+#endif
+
+void BSP_I2C_DeInit(void)
+{
+ GPIOB_SetBits(GPIO_Pin_14);
+ GPIOB_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_PU);
+
+ GPIOB_SetBits(GPIO_Pin_15);
+ GPIOB_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU);
+}
+
+// GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT
+void GXHTC3C_SendCmd(uint16_t cmd)
+{
+ int ret;
+ i2c_tx_data[0] = HI_UINT16(cmd);
+ i2c_tx_data[1] = LO_UINT16(cmd);
+ ret = I2C_Write(GXHTC3C_ADDR, (const uint8_t *)&i2c_tx_data, 2, true, true);
+ logDebug("GXHTC3C_SendCmd %s", ret ? "failed" : "success");
+}
+
+void GXHTC3C_Sleep(void)
+{
+ GXHTC3C_SendCmd(GXHTC3C_CMD_SLEEP);
+}
+
+void GXHTC3C_Wakeup(void)
+{
+ GXHTC3C_SendCmd(GXHTC3C_CMD_WAKEUP);
+}
+
+void GXHTC3C_GetStart(void)
+{
+ // 浣庡姛鑰椼丆lock stretching鍏抽棴銆佹箍搴﹀湪鍓
+ GXHTC3C_SendCmd(GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT);
+}
+
+// 瀹氫箟CRC-8鍙傛暟
+#define POLYNOMIAL 0x31 // 鐢熸垚澶氶」寮忥細x8+x5+x4+1 (鐪佺暐鏈楂樹綅1)
+#define INIT_VALUE 0xFF // 鍒濆鍖栧
+#define REFLECT_IN false // 涓嶅弽灏勮緭鍏
+#define REFLECT_OUT false // 涓嶅弽灏勮緭鍑
+
+uint8_t GXHTC3C_CRC_8(uint8_t *p_crc, uint8_t len)
+{
+ uint8_t crc = INIT_VALUE;
+
+ for (size_t i = 0; i < len; ++i)
+ {
+ crc ^= p_crc[i]; // 寮傛垨褰撳墠瀛楄妭
+
+ for (uint8_t j = 0; j < 8; ++j)
+ {
+ if (crc & 0x80)
+ { // 濡傛灉鏈楂樹綅鏄1
+ crc = (crc << 1) ^ POLYNOMIAL; // 宸︾Щ涓浣嶅苟寮傛垨澶氱浉寮
+ }
+ else
+ {
+ crc <<= 1; // 鍚﹀垯鍙乏绉讳竴浣
+ }
+
+ // 纭繚crc淇濇寔鍦8浣嶅唴
+ crc &= 0xFF;
+ }
+ }
+
+ // 鏈缁堣繑鍥濩RC鍊
+ return crc;
+}
+
+// GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT
+uint8_t GXHTC3C_GetTempHumi(float *humi, float *temp)
+{
+ int ret;
+ ret = I2C_Read(GXHTC3C_ADDR, (uint8_t *)&i2c_rx_data, 6, true, 1000);
+ logDebug("read %d byte(s) from %#x", ret, GXHTC3C_ADDR);
+ if (ret != 6)
+ {
+ logError("read failed");
+ logHexDumpAll(i2c_rx_data, ret);
+ return 1;
+ }
+
+ // 浣庡姛鑰椼丆lock stretching鍏抽棴銆佹箍搴﹀湪鍓
+ uint8_t humi_raw_data[2];
+ uint8_t temp_raw_data[2];
+
+ humi_raw_data[0] = i2c_rx_data[0];
+ humi_raw_data[1] = i2c_rx_data[1];
+ uint16_t raw_humi = (humi_raw_data[0] << 8) | humi_raw_data[1];
+ uint8_t crc_humi = GXHTC3C_CRC_8(humi_raw_data, 2);
+
+ temp_raw_data[0] = i2c_rx_data[3];
+ temp_raw_data[1] = i2c_rx_data[4];
+ uint16_t raw_temp = (temp_raw_data[0] << 8) | temp_raw_data[1];
+ uint8_t crc_temp = GXHTC3C_CRC_8(temp_raw_data, 2);
+
+ if ((crc_humi == i2c_rx_data[2]) && (crc_temp == i2c_rx_data[5]))
+ {
+ logDebug("crc ok");
+ logHexDumpAll(i2c_rx_data, 6);
+
+ *humi = (100.0 * raw_humi) / 65536.0; // 婀垮害鐪熷疄鍊
+ *temp = (175.0 * raw_temp) / 65536.0 - 45.0; // 娓╁害鐪熷疄鍊
+ // logDebug("humi %f, temp %f", *humi, *temp);
+ }
+ else
+ {
+ logHexDumpAll(i2c_rx_data, 6);
+ logError("crc_temp 0x%02x, crc_humi 0x%02x", crc_temp, crc_humi);
+ logError("crc error");
+ return 2;
+ }
+ return 0;
+}
+
+void GXHTC3C_Read(void)
+{
+ int ret;
+
+ GXHTC3C_Wakeup();
+ DelayMs(20);
+
+ GXHTC3C_GetStart();
+ DelayMs(20);
+ float humi, temp;
+ ret = GXHTC3C_GetTempHumi(&humi, &temp);
+ if (ret == 0)
+ {
+ logDebug("humi %.2f %, temp %.2f C", humi, temp);
+ }
+
+ GXHTC3C_Sleep();
+}
+
+void GXHTC3C_Init(void)
+{
+#if 1
+ BSP_I2C_Init(MASTER_ADDR);
+ int ret;
+
+ GXHTC3C_Sleep();
+ DelayMs(100);
+
+ GXHTC3C_Wakeup();
+ DelayMs(20);
+
+ GXHTC3C_GetStart();
+ DelayMs(20);
+ float humi, temp;
+ ret = GXHTC3C_GetTempHumi(&humi, &temp);
+ if (ret == 0)
+ {
+ logDebug("humi %.2f %, temp %.2f C", humi, temp);
+ }
+
+ GXHTC3C_Sleep();
+
+ for (uint8_t i = 0; i < 3; i++)
+ {
+ GXHTC3C_Read();
+ }
+
+#endif
+}
+
+#if 1
+__INTERRUPT
+__HIGH_CODE
+void I2C_IRQHandler(void)
+{
+ uint32_t event = I2C_GetLastEvent();
+ print_i2c_irq_sta(event);
+
+ /* I2C Master */
+ if (event & (RB_I2C_MSL << 16))
+ {
+ if (event & RB_I2C_SB)
+ {
+ /* Start condition sent, send address */
+ I2C_SendData(i2c_slave_addr_rw);
+ I2C_DBG("Master selected, send address");
+ }
+
+ /* I2C Master transmitter */
+ if (event & (RB_I2C_TRA << 16))
+ {
+ I2C_DBG("Master transmitter:");
+ /* Slave receiver acked address or sent bit */
+ if (event & (RB_I2C_ADDR | RB_I2C_BTF | RB_I2C_TxE | (RB_I2C_TRA << 16)))
+ {
+ /* if there is data to send, send it, otherwise stop */
+ if (i2c_master_buffer_index < i2c_master_buffer_length)
+ {
+ I2C_SendData(i2c_master_buffer[i2c_master_buffer_index++]);
+ I2C_DBG(" send (%#x)\n",
+ i2c_master_buffer[i2c_master_buffer_index - 1]);
+ }
+ else
+ {
+ if (i2c_send_stop)
+ {
+ i2c_state = I2C_READY;
+ I2C_GenerateSTOP(ENABLE);
+ I2C_DBG(" send STOP");
+ }
+ else
+ {
+ i2c_in_repstart = true;
+ /* we're gonna send the START, don't enable the interrupt. */
+ I2C_ITConfig(I2C_IT_BUF, DISABLE);
+ I2C_ITConfig(I2C_IT_EVT, DISABLE);
+ I2C_ITConfig(I2C_IT_ERR, DISABLE);
+ I2C_GenerateSTART(ENABLE);
+ i2c_state = I2C_READY;
+ I2C_DBG(" restart");
+ }
+ }
+ }
+
+ /* Address or data sent, nack received */
+ if (event & RB_I2C_AF)
+ {
+ I2C_ClearFlag(I2C_FLAG_AF);
+
+ i2c_error = I2C_MT_NACK;
+ i2c_state = I2C_READY;
+ I2C_GenerateSTOP(ENABLE);
+ I2C_DBG(" NACK received, sent stop");
+ }
+ }
+ else
+ {
+ /* I2C Master reveiver */
+ I2C_DBG("Master receiver:");
+
+ /* address sent, ack received */
+ if (event & RB_I2C_ADDR)
+ {
+ /* ack if more bytes are expected, otherwise nack */
+ if (i2c_master_buffer_length)
+ {
+ I2C_AcknowledgeConfig(ENABLE);
+ I2C_DBG(" address sent");
+ I2C_DBG(" ACK next");
+ }
+ else
+ {
+ // XXX: Should not delay too match before NACK
+ I2C_AcknowledgeConfig(DISABLE);
+ is_nack_sent = true;
+ I2C_DBG(" address sent");
+ I2C_DBG(" NACK next");
+ }
+ }
+
+ /* data reveived */
+ if (event & (RB_I2C_RxNE))
+ {
+ /* put byte into buffer */
+ i2c_master_buffer[i2c_master_buffer_index++] = I2C_ReceiveData();
+
+ if (i2c_master_buffer_index < i2c_master_buffer_length)
+ {
+ I2C_AcknowledgeConfig(ENABLE);
+ I2C_DBG(" ACK next");
+ }
+ else
+ {
+ // XXX: Should not delay too match before NACK
+ I2C_AcknowledgeConfig(DISABLE);
+ I2C_DBG(" NACK next");
+
+ if (is_nack_sent)
+ {
+ is_nack_sent = false;
+ if (i2c_send_stop)
+ {
+ I2C_GenerateSTOP(ENABLE);
+ i2c_state = I2C_READY;
+ I2C_DBG(" send STOP");
+ }
+ else
+ {
+ i2c_in_repstart = true;
+ /* we're gonna send the START, don't enable the interrupt. */
+ I2C_ITConfig(I2C_IT_BUF, DISABLE);
+ I2C_ITConfig(I2C_IT_EVT, DISABLE);
+ I2C_ITConfig(I2C_IT_ERR, DISABLE);
+ I2C_GenerateSTART(ENABLE);
+ i2c_state = I2C_READY;
+ I2C_DBG(" restart");
+ }
+ }
+ else
+ {
+ is_nack_sent = true;
+ }
+ }
+
+ I2C_DBG(" received data (%#x)\n",
+ i2c_master_buffer[i2c_master_buffer_index - 1]);
+ }
+
+ /* nack received */
+ if (event & RB_I2C_AF)
+ {
+ I2C_ClearFlag(I2C_FLAG_AF);
+ /* put final byte into buffer */
+ i2c_master_buffer[i2c_master_buffer_index++] = I2C_ReceiveData();
+
+ if (i2c_send_stop)
+ {
+ i2c_state = I2C_READY;
+ I2C_GenerateSTOP(ENABLE);
+ I2C_DBG(" NACK received, send STOP");
+ }
+ else
+ {
+ i2c_in_repstart = true;
+ /* we're gonna send the START, don't enable the interrupt. */
+ I2C_ITConfig(I2C_IT_BUF, DISABLE);
+ I2C_ITConfig(I2C_IT_EVT, DISABLE);
+ I2C_ITConfig(I2C_IT_ERR, DISABLE);
+ I2C_GenerateSTART(ENABLE);
+ i2c_state = I2C_READY;
+ I2C_DBG(" restart");
+ }
+ }
+ }
+ }
+ else
+ {
+ /* I2C slave */
+ /* addressed, returned ack */
+ if (event & RB_I2C_ADDR)
+ {
+
+ if (event & ((RB_I2C_TRA << 16) | RB_I2C_TxE))
+ {
+ I2C_DBG("Slave transmitter address matched");
+
+ i2c_state = I2C_STX;
+ i2c_slave_txbuffer_index = 0;
+ i2c_slave_txbuffer_length = 0;
+
+ if (slave_cb && slave_cb->on_transmit)
+ {
+ slave_cb->on_transmit(i2c_slave_txbuffer, &i2c_slave_txbuffer_length);
+ }
+ }
+ else
+ {
+ I2C_DBG("Slave reveiver address matched");
+
+ i2c_state = I2C_SRX;
+ i2c_slave_rxbuffer_index = 0;
+ }
+ }
+
+ if (event & (RB_I2C_TRA << 16))
+ { // TODO: STOP?
+ /* Slave transmintter */
+ I2C_AcknowledgeConfig(ENABLE);
+ I2C_DBG("Slave transmitter:");
+
+ if (event & RB_I2C_AF)
+ {
+ /* Nack received */
+ I2C_ClearFlag(I2C_FLAG_AF);
+ I2C_AcknowledgeConfig(ENABLE);
+ I2C_DBG(" Nack received");
+
+ /* leave slave receiver state */
+ i2c_state = I2C_READY;
+ /* clear status */
+ event = 0;
+ }
+
+ if (event & (RB_I2C_BTF | RB_I2C_TxE))
+ {
+ /* if there is more to send, ack, otherwise send 0xff */
+ if (i2c_slave_txbuffer_index < i2c_slave_txbuffer_length)
+ {
+ /* copy data to output register */
+ I2C_SendData(i2c_slave_txbuffer[i2c_slave_txbuffer_index++]);
+ I2C_DBG(" send (%#x)\n",
+ i2c_slave_txbuffer[i2c_slave_txbuffer_index - 1]);
+ }
+ else
+ {
+ I2C_SendData(0xff);
+ I2C_DBG(" no more data, send 0xff");
+ }
+ }
+ }
+ else
+ {
+ /* Slave receiver */
+ I2C_DBG("Slave receiver:");
+
+ if (event & RB_I2C_RxNE)
+ {
+ /* if there is still room in the rx buffer */
+ if (i2c_slave_rxbuffer_index < I2C_BUFFER_LENGTH)
+ {
+ /* put byte in buffer and ack */
+ i2c_slave_rxbuffer[i2c_slave_rxbuffer_index++] = I2C_ReceiveData();
+ I2C_AcknowledgeConfig(ENABLE);
+ I2C_DBG(" received (%#x)\n",
+ i2c_slave_rxbuffer[i2c_slave_rxbuffer_index - 1]);
+ }
+ else
+ {
+ // otherwise nack
+ I2C_AcknowledgeConfig(DISABLE);
+ }
+ }
+
+ if (event & RB_I2C_STOPF)
+ {
+ /* ack future responses and leave slave receiver state */
+ R16_I2C_CTRL1 |= RB_I2C_PE; // clear flag
+
+ I2C_DBG(" reveive stop");
+
+ /* callback to user defined callback */
+ if (slave_cb && slave_cb->on_receive)
+ {
+ slave_cb->on_receive(i2c_slave_rxbuffer, i2c_slave_rxbuffer_index);
+ }
+ /* since we submit rx buffer , we can reset it */
+ i2c_slave_rxbuffer_index = 0;
+ }
+
+ if (event & RB_I2C_AF)
+ {
+ I2C_ClearFlag(I2C_FLAG_AF);
+
+ /* ack future responses */
+ I2C_AcknowledgeConfig(ENABLE);
+ }
+ }
+ }
+
+ if (event & RB_I2C_BERR)
+ {
+ I2C_ClearFlag(RB_I2C_BERR);
+ I2C_GenerateSTOP(ENABLE);
+
+ i2c_error = I2C_BUS_ERROR;
+ I2C_DBG("RB_I2C_BERR");
+ }
+
+ if (event & RB_I2C_ARLO)
+ {
+ I2C_ClearFlag(RB_I2C_ARLO);
+
+ i2c_error = I2C_ARB_LOST;
+ I2C_DBG("RB_I2C_ARLO");
+ }
+
+ if (event & RB_I2C_OVR)
+ {
+ I2C_ClearFlag(RB_I2C_OVR);
+
+ i2c_error = I2C_OVR;
+ I2C_DBG("RB_I2C_OVR");
+ }
+
+ if (event & RB_I2C_PECERR)
+ {
+ I2C_ClearFlag(RB_I2C_PECERR);
+
+ i2c_error = I2C_PECERR;
+ I2C_DBG("RB_I2C_PECERR");
+ }
+
+ if (event & RB_I2C_TIMEOUT)
+ {
+ I2C_ClearFlag(RB_I2C_TIMEOUT);
+
+ i2c_error = I2C_TIMEOUT;
+ I2C_DBG("RB_I2C_TIMEOUT");
+ }
+
+ if (event & RB_I2C_SMBALERT)
+ {
+ I2C_ClearFlag(RB_I2C_SMBALERT);
+
+ i2c_error = I2C_SMBALERT;
+ I2C_DBG("RB_I2C_SMBALERT");
+ }
+}
+#endif
From 77d80fe2732b311ef3594e65d38250c3ddefca76 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:13:29 +0800
Subject: [PATCH 08/16] bsp_i2c ok
---
.cproject | 7 ++++---
.project | 6 +++---
APP/peripheral_main.c | 7 +++++--
BSP/src/bsp_i2c.c | 17 ++++++++++-------
BSP/src/bsp_uart.c | 1 +
5 files changed, 23 insertions(+), 15 deletions(-)
diff --git a/.cproject b/.cproject
index 74bb70a..7420052 100644
--- a/.cproject
+++ b/.cproject
@@ -90,7 +90,7 @@
-
+
@@ -103,9 +103,9 @@
-
+
-
+
@@ -173,4 +173,5 @@
+
diff --git a/.project b/.project
index d7d4c38..b2e792e 100644
--- a/.project
+++ b/.project
@@ -1,7 +1,7 @@
-
+
BLE_TYQ_CH592F
-
+
@@ -26,7 +26,7 @@
1602468250279
-
+
22
org.eclipse.ui.ide.multiFilter
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 734ec73..440e574 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -165,6 +165,8 @@ int main(void)
BSP_KEY_Init();
+
+
BSP_ADC_Init();
CH59x_BLEInit();
@@ -177,11 +179,12 @@ int main(void)
GAPRole_PeripheralInit();
Peripheral_Init();
- DelayMs(100);
- GXHTC3C_Init();
BSP_FLASH_Init();
+ DelayMs(100);
+ GXHTC3C_Init();
+
Main_Circulation();
}
diff --git a/BSP/src/bsp_i2c.c b/BSP/src/bsp_i2c.c
index 926dfce..6ea2fc8 100644
--- a/BSP/src/bsp_i2c.c
+++ b/BSP/src/bsp_i2c.c
@@ -42,7 +42,8 @@ static struct i2c_slave_cb *slave_cb = NULL;
#define MASTER_ADDR 0x42
uint8_t i2c_tx_data[2];
-uint8_t i2c_rx_data[6];
+// __attribute__((aligned(4)))
+uint8_t i2c_rx_data[8];
void BSP_I2C_Init(uint8_t address)
{
@@ -95,7 +96,7 @@ int I2C_Write(uint8_t addr_7bit, const uint8_t *data, uint8_t length,
i2c_master_buffer_index = 0;
i2c_master_buffer_length = length;
- memcpy(i2c_master_buffer, data, length);
+ tmos_memcpy(i2c_master_buffer, data, length);
i2c_slave_addr_rw = I2C_WRITE;
i2c_slave_addr_rw |= addr_7bit << 1;
@@ -201,7 +202,7 @@ int I2C_Read(uint8_t addr_7bit, uint8_t *data, uint8_t length,
length = i2c_master_buffer_index;
// copy i2c buffer to data
- memcpy(data, i2c_master_buffer, length);
+ tmos_memcpy(data, i2c_master_buffer, length);
return length;
}
@@ -279,7 +280,7 @@ void GXHTC3C_SendCmd(uint16_t cmd)
i2c_tx_data[0] = HI_UINT16(cmd);
i2c_tx_data[1] = LO_UINT16(cmd);
ret = I2C_Write(GXHTC3C_ADDR, (const uint8_t *)&i2c_tx_data, 2, true, true);
- logDebug("GXHTC3C_SendCmd %s", ret ? "failed" : "success");
+ // logDebug("GXHTC3C_SendCmd %s", ret ? "failed" : "success");
}
void GXHTC3C_Sleep(void)
@@ -361,8 +362,8 @@ uint8_t GXHTC3C_GetTempHumi(float *humi, float *temp)
if ((crc_humi == i2c_rx_data[2]) && (crc_temp == i2c_rx_data[5]))
{
- logDebug("crc ok");
- logHexDumpAll(i2c_rx_data, 6);
+ // logDebug("crc ok");
+ // logHexDumpAll(i2c_rx_data, 6);
*humi = (100.0 * raw_humi) / 65536.0; // 婀垮害鐪熷疄鍊
*temp = (175.0 * raw_temp) / 65536.0 - 45.0; // 娓╁害鐪熷疄鍊
@@ -375,6 +376,7 @@ uint8_t GXHTC3C_GetTempHumi(float *humi, float *temp)
logError("crc error");
return 2;
}
+ // logDebug("0 success");
return 0;
}
@@ -391,6 +393,7 @@ void GXHTC3C_Read(void)
ret = GXHTC3C_GetTempHumi(&humi, &temp);
if (ret == 0)
{
+ // logDebug("ret=0");
logDebug("humi %.2f %, temp %.2f C", humi, temp);
}
@@ -415,7 +418,7 @@ void GXHTC3C_Init(void)
ret = GXHTC3C_GetTempHumi(&humi, &temp);
if (ret == 0)
{
- logDebug("humi %.2f %, temp %.2f C", humi, temp);
+ // logDebug("humi %.2f %, temp %.2f C", humi, temp);
}
GXHTC3C_Sleep();
diff --git a/BSP/src/bsp_uart.c b/BSP/src/bsp_uart.c
index 3181af1..457804c 100644
--- a/BSP/src/bsp_uart.c
+++ b/BSP/src/bsp_uart.c
@@ -22,6 +22,7 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len)
unsigned int ret;
ret = lwrb_write(&Uart1Tx, buf, len);
+ BSP_UART1_TxLoop();
// UART1_INTCfg(ENABLE, RB_IER_THR_EMPTY);
return ret;
}
From 0bbc09d01e58e0ed8854dc18129a166fe8b2e582 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:14:46 +0800
Subject: [PATCH 09/16] bsp_iwdg ok
---
BSP/inc/bsp_iwdg.h | 47 +++++++++++++++++++
BSP/src/bsp_iwdg.c | 109 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 156 insertions(+)
create mode 100644 BSP/inc/bsp_iwdg.h
create mode 100644 BSP/src/bsp_iwdg.c
diff --git a/BSP/inc/bsp_iwdg.h b/BSP/inc/bsp_iwdg.h
new file mode 100644
index 0000000..70b167c
--- /dev/null
+++ b/BSP/inc/bsp_iwdg.h
@@ -0,0 +1,47 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-15 16:13:37
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 16:13:37
+ * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\inc\bsp_iwdg.h
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-14 10:51:01
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 11:16:24
+ * @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_iwdg.h
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
+#ifndef __BSP_IWDG_H__
+#define __BSP_IWDG_H__
+
+#include "CONFIG.h"
+
+#define RB_RLR 0x0FFF // RW, watch-dog counter reload (write protect)
+#define RB_PR 0x7000 // PR, prescale (write protect)
+#define RB_PVU 0x8000 // RO, register update flag (write protect)
+#define RB_COUNT 0xFF0000 // RO, watch-dog down counter
+#define RB_STOP_EN 0x20000000 // RW, watch-dog stop enable (write protect)
+#define RB_WR_PROTECT 0x40000000 // RO, write protect
+#define RB_IWDG_EN 0x80000000 // RO, watch-dog enable
+
+void IWDG_Init(uint16_t ms);
+
+// 闇瑕佸畾鏃惰皟鐢ㄥ杺鐙
+// 鍦ㄧ湅闂ㄧ嫍閿瘎瀛樺櫒 (R32_IWDG_KR) 涓紝杞欢瑕佷互涓瀹氱殑闂撮殧鍐欏叆 0xAAAA锛岄噸瑁呰浇璁℃暟鍊硷紝杩欏氨鏄杺鐙楃殑鎿嶄綔銆
+// 鍚﹀垯锛屽綋璁℃暟鍣ㄤ负 0 鏃讹紝鐪嬮棬鐙椾細浜х敓澶嶄綅銆
+#define FEED_IWDG() {R32_IWDG_KR=0xAAAA;}
+
+void ShowRestartReason(void);
+
+#endif // !__BSP_IWDG_H__
+
+
+
+
diff --git a/BSP/src/bsp_iwdg.c b/BSP/src/bsp_iwdg.c
new file mode 100644
index 0000000..a4c5f80
--- /dev/null
+++ b/BSP/src/bsp_iwdg.c
@@ -0,0 +1,109 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-15 16:13:41
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 16:13:42
+ * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\src\bsp_iwdg.c
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
+#include "bsp_iwdg.h"
+
+#include "bsp_uart.h"
+#include "log.h"
+
+// https://www.cnblogs.com/debugdabiaoge/p/17580033.html
+// https://www.cnblogs.com/risc5-ble/p/17853714.html
+
+#undef LOG_ENABLE
+#define LOG_ENABLE 1
+
+#undef LOG_TAG
+#define LOG_TAG "idwg"
+
+// 32k rc
+// div 0:4 | 1:8 | 2:16 | 3:32 |
+// 4:64 | 5:128 | 6:256 | 7:512(32K鍒嗛512锛屾椂閽62.5Hz) |
+// reload max 4095
+
+// 鐙珛鐪嬮棬鐙楋紙IWDG锛夌敱涓撶敤鐨勫唴閮ㄤ綆閫熸椂閽燂紙LSI锛夐┍鍔紝鑳藉鍦ㄤ綆鍔熻楁ā寮忎笅姝e父宸ヤ綔銆
+void IWDG_Init(uint16_t ms)
+{
+ uint16_t div = 512;
+ float tick_ms = 1000 / (32000 / 512);
+
+ uint16_t reload = (ms / tick_ms) + 1;
+
+ // 瑙i櫎IWDG淇濇姢
+ R32_IWDG_KR = 0x5555;
+
+ R32_IWDG_CFG = (RB_PR & (div << 12)) | (RB_STOP_EN) | (reload & RB_RLR);
+ // 寮鍚疘WDG淇濇姢
+ R32_IWDG_KR = 0xCCCC;
+
+ logDebug("R32_IWDG_CFG = %X", R32_IWDG_CFG);
+}
+
+// #define RB_RESET_FLAG 0x07 // RO: recent reset flag
+// #define RST_FLAG_SW 0x00
+// #define RST_FLAG_RPOR 0x01
+// #define RST_FLAG_WTR 0x02
+// #define RST_FLAG_MR 0x03
+// //#define RST_FLAG_GPWSM 0x04 // RO, power on reset flag during sleep/shutdown: 0=no power on reset during sleep/shutdown, 1=power on reset occurred during sleep/shutdown
+// #define RST_FLAG_GPWSM 0x05
+// // RB_RESET_FLAG: recent reset flag
+// // 000 - SR, software reset, by RB_SOFTWARE_RESET=1 @RB_WDOG_RST_EN=0
+// // 001 - RPOR, real power on reset
+// // 010 - WTR, watch-dog timer-out reset
+// // 011 - MR, external manual reset by RST pin input low
+// // 101 - GRWSM, global reset by waking under shutdown mode
+// 1?? - LRW, power on reset occurred during sleep
+
+// 鎵撳嵃澶嶄綅鐘舵佸瘎瀛樺櫒锛屾樉绀哄浣嶅師鍥
+// 鏈杩戜竴娆″浣嶇姸鎬侊細
+// 000锛氳蒋浠跺浣 SR锛圧B_WDOG_RST_EN=0鏃惰蒋浠跺浣嶅彲浜х敓姝ょ姸鎬侊紝鍚﹀垯鍙浣嶄絾涓嶄骇鐢熸鐘舵侊級锛
+// 001锛氫笂鐢靛浣 RPOR锛
+// 010锛氱湅闂ㄧ嫍瓒呮椂澶嶄綅 WTR锛
+// 011锛氬閮ㄦ墜鍔ㄥ浣 MR锛
+// 101锛氫粠涓嬬數妯″紡鍞ら啋鏃剁殑澶嶄綅 GRWSM锛
+// 100/110/111锛氬敜閱掑浣 LRW锛屼笖姝ゅ墠鐨勪笂涓娆″浣嶅垎鍒槸 SR/WTR/MR銆
+void ShowRestartReason(void)
+{
+ uint8_t reason = R8_RESET_STATUS&0x07;
+ logDebug("R8_RESET_STATUS = %02x", reason);
+ switch (reason)
+ {
+ case RST_FLAG_SW:
+ logDebug("RST_FLAG_SW");
+ break;
+ case RST_FLAG_RPOR:
+ logDebug("RST_FLAG_RPOR");
+ break;
+ case RST_FLAG_WTR:
+ logDebug("RST_FLAG_WTR");
+ break;
+ case RST_FLAG_MR:
+ logDebug("RST_FLAG_MR");
+ break;
+
+ case RST_FLAG_GPWSM:
+ logDebug("RST_FLAG_GPWSM");
+ break;
+
+ case 0x04:
+ logDebug("LRW, last SR");
+ break;
+ case 0x06:
+ logDebug("LRW, last WTR");
+ break;
+ case 0x07:
+ logDebug("LRW, last MR");
+
+ default:
+ break;
+ }
+}
+
+
+
From fe13f8273f8e5275f8d4ca1e892cb8264ad320bf Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:24:06 +0800
Subject: [PATCH 10/16] =?UTF-8?q?bsp=5Fkey=E8=A7=A6=E5=8F=91=E6=9C=89?=
=?UTF-8?q?=E7=82=B9=E9=97=AE=E9=A2=98=EF=BC=8CHAL=5FSLEEP=E5=AF=B9?=
=?UTF-8?q?=E5=BA=94=E6=94=B9=E4=BA=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.settings/org.eclipse.core.resources.prefs | 1 +
APP/peripheral.c | 15 +-
APP/peripheral_main.c | 35 ++-
BSP/inc/bsp_key.h | 27 +-
BSP/src/bsp_key.c | 329 ++++++++++++++++++++-
HAL/SLEEP.c | 33 +++
HAL/include/SLEEP.h | 6 +-
7 files changed, 421 insertions(+), 25 deletions(-)
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
index 6827126..a253c4c 100644
--- a/.settings/org.eclipse.core.resources.prefs
+++ b/.settings/org.eclipse.core.resources.prefs
@@ -1,6 +1,7 @@
eclipse.preferences.version=1
encoding//APP/peripheral_main.c=UTF-8
encoding//BSP/inc/bsp_valve.h=UTF-8
+encoding//BSP/src/bsp_key.c=UTF-8
encoding//BSP/src/bsp_valve.c=UTF-8
encoding//common/letter-shell/extensions/log/log.h=UTF-8
encoding//common/letter-shell/shell.c=UTF-8
diff --git a/APP/peripheral.c b/APP/peripheral.c
index 72717a8..b5d0f30 100644
--- a/APP/peripheral.c
+++ b/APP/peripheral.c
@@ -94,17 +94,18 @@
*/
static uint8_t Peripheral_TaskID = INVALID_TASK_ID; // Task ID for internal task/event processing
+// TODO:响应体,名称加上MAC地址
// GAP - SCAN RSP data (max size = 31 bytes)
static uint8_t scanRspData[] = {
// complete name
0x12, // length of this data
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
- 'S',
- 'i',
- 'm',
- 'p',
- 'l',
- 'e',
+ 'B',
+ 'L',
+ 'E',
+ 'T',
+ 'Y',
+ 'Q',
' ',
'P',
'e',
@@ -215,7 +216,7 @@ static uint8_t advertData[] = {
#endif
// GAP GATT Attributes
-static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple Peripheral";
+static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "BLE_TYQ";
// Connection item list
static peripheralConnItem_t peripheralConnList;
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index 440e574..dfe5ece 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -32,15 +32,32 @@
#undef LOG_ENABLE
#define LOG_ENABLE 1
-/*********************************************************************
- * 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
+void app_task_handler(TeAppEvtType app_evt_type)
+{
+ switch(app_evt_type)
+ {
+ case kKeyShort:{
+ logDebug("button short press\r\n");
+ break;
+ }
+ case kKeyLong:
+ logDebug("button long press\r\n");
+ break;
+ case kKeyRelease:
+ // BSP_KEY_EnterLowpower();
+ DelayMs(10);
+ BSP_RequestSleep();
+ logDebug("gpio relase;BSP_RequestSleep\r\n");
+
+ break;
+ default:
+ break;
+ }
+}
/*********************************************************************
* @fn Main_Circulation
*
@@ -163,10 +180,6 @@ int main(void)
EMV_CHARGE_OFF_DEINIT;
EMV_OFF_DEINIT;
- BSP_KEY_Init();
-
-
-
BSP_ADC_Init();
CH59x_BLEInit();
@@ -185,6 +198,8 @@ int main(void)
DelayMs(100);
GXHTC3C_Init();
+ BSP_KEY_Init(app_task_handler);
+
Main_Circulation();
}
diff --git a/BSP/inc/bsp_key.h b/BSP/inc/bsp_key.h
index c739711..4aa94cc 100644
--- a/BSP/inc/bsp_key.h
+++ b/BSP/inc/bsp_key.h
@@ -1,17 +1,40 @@
#ifndef __BSP_KEY_H__
#define __BSP_KEY_H__
+#include "CONFIG.h"
-#include "CH59x_common.h"
+#include
+#include
+#define KEY_TIMOUT_MS 1000
+#define KEY_LONG_PRESS_MS 5000
+#define KEY_SACN_MS 40
+#define KEY_SCAN_EVT (0x0001 << 0)
+#define KEY_WAKEUP_EVT (0x0001 << 1)
+#define KEY_RELEASE_EVT (0x0001 << 2)
+#define KEY_IDLE_TIMEOUT_EVT (0x0001 << 2)
+// KEY_B_PIN at PB7,low->active
+#define KEY_B_PIN GPIO_Pin_7
+#define IS_KEY_Vaild() ((GPIOB_ReadPortPin(KEY_B_PIN) ? 0 : 1))
+typedef enum
+{
+ kKeyShort = 0,
+ kKeyLong,
+ kKeyRelease,
+} TeAppEvtType;
-void BSP_KEY_Init(void);
+typedef void (*app_task_evt_handler_t)(TeAppEvtType app_task_evt_type);
+void BSP_KEY_EnterLowpower(void);
+void BSP_KEY_ExitLowpower(void);
+void BSP_KEY_Init(app_task_evt_handler_t handler);
+
+void KEY_ProcessLoop(void);
#endif // !__BSP_KEY_H__
diff --git a/BSP/src/bsp_key.c b/BSP/src/bsp_key.c
index 1d62190..8369b02 100644
--- a/BSP/src/bsp_key.c
+++ b/BSP/src/bsp_key.c
@@ -1,14 +1,333 @@
#include "bsp_key.h"
+#include "bsp_uart.h"
+#include "log.h"
+#include "SLEEP.h"
+// https://www.cnblogs.com/iot-fan/p/14304943.html
+#undef LOG_ENABLE
+#define LOG_ENABLE 1
+#undef LOG_TAG
+#define LOG_TAG "key"
-void BSP_KEY_Init(void)
+tmosTaskID key_task_id = INVALID_TASK_ID;
+
+volatile uint8_t key_wakeup_flag = 0;
+volatile uint8_t key_timeout_flag = 0;
+
+static app_task_evt_handler_t p_handler = NULL;
+
+static volatile uint8_t key_timeout_cnt = 0;
+
+static void KEY_Task_ProcessTmosMsg(tmos_event_hdr_t *pMsg)
{
- // KEY
- GPIOB_SetBits(GPIO_Pin_15);
- // 鐢卞閮ㄤ笂鎷夌數闃讳簡
- GPIOB_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_Floating);
+ switch (pMsg->event)
+ {
+ default:
+ logDebug("pMsg->event %04x", pMsg->event);
+ break;
+ }
+}
+
+#if 1
+static uint16_t KEY_Task_ProcessEvent(uint8_t task_id, uint16_t events)
+{
+ if (events & SYS_EVENT_MSG)
+ {
+ uint8_t *pMsg;
+ if ((pMsg = tmos_msg_receive(key_task_id)) != NULL)
+ {
+ KEY_Task_ProcessTmosMsg((tmos_event_hdr_t *)pMsg);
+ // Release the TMOS message
+ tmos_msg_deallocate(pMsg);
+ }
+ // return unprocessed events
+ return (events ^ SYS_EVENT_MSG);
+ }
+
+ if (events & KEY_SCAN_EVT)
+ {
+ static volatile uint8_t key_vaild_times = 0;
+ static volatile bool key_vaild_for_long_press = false;
+ if (IS_KEY_Vaild())
+ {
+ if (key_vaild_times > (KEY_LONG_PRESS_MS / KEY_SACN_MS))
+ { //> 20ms*100=2000ms
+ if (false == key_vaild_for_long_press)
+ {
+ if (NULL != p_handler)
+ {
+ p_handler(kKeyLong);
+ }
+ // PRINT("WE should power switch here\r\n");
+ key_vaild_for_long_press = true;
+ }
+ }
+ else
+ {
+ key_vaild_times++;
+ }
+ key_timeout_cnt = 10;
+ }
+ else
+ { // button release
+
+ if (key_vaild_times)
+ {
+ if ((key_vaild_times) < (KEY_LONG_PRESS_MS / KEY_SACN_MS))
+ {
+ p_handler(kKeyShort);
+ }
+ key_vaild_times = 0;
+ // PRINT("KEY VAILED\r\n");
+ }
+
+ key_vaild_for_long_press = false;
+ }
+ if (key_timeout_cnt)
+ {
+ key_timeout_cnt--;
+ logDebug("key_timeout_cnt %d", key_timeout_cnt);
+ tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(KEY_SACN_MS)); // 40ms
+ }
+ else
+ {
+ p_handler(kKeyRelease);
+ }
+ return (events ^ KEY_SCAN_EVT);
+ }
+
+ // Discard unknown events
+ return 0;
+}
+
+#endif
+
+// 鐢辨寜閿腑鏂敜閱掑悗寮鍚寜閿壂鎻
+void BSP_KEY_EnterLowpower(void)
+{
+ // key_wakeup_flag = 0;
+ // tmos_stop_task(key_task_id, KEY_SCAN_EVT);
+
+ // R16_PB_INT_MODE |= KEY_B_PIN; // edge mode
+ // GPIOB_ResetBits(KEY_B_PIN); // edge fall
+ // R16_PB_INT_IF = KEY_B_PIN;
+ // R16_PB_INT_EN |= KEY_B_PIN;
+
+ // 鐢卞閮ㄤ笂鎷夌數闃讳簡
+ // 璁剧疆涓烘诞绌鸿緭鍏ユā寮
+ // GPIOB_SetBits(KEY_B_PIN);
+ // GPIOB_ModeCfg(KEY_B_PIN, GPIO_ModeIN_PU);
+
+ // TODO:鎸夐敭鐢靛钩瑙﹀彂璁剧疆
+ // 涓嬮檷娌胯Е鍙
+ GPIOB_ITModeCfg(KEY_B_PIN, GPIO_ITMode_FallEdge);
+
+ // 寮鍚疓PIO鐨勭潯鐪犲敜閱,濡傛灉闇瑕佺殑璇
+ // PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
+ PWR_PeriphWakeUpCfg(ENABLE, RB_GPIO_WAKE_MODE | RB_SLP_GPIO_WAKE, Long_Delay);
+
+ // 寮鍚疓PIOB涓柇
+ PFIC_EnableIRQ(GPIO_B_IRQn);
+
+ BSP_RequestSleep();
+}
+
+void BSP_KEY_ExitLowpower(void)
+{
+ BSP_BlockSleep();
+
+ // 鍏抽棴GPIOB涓柇
+ PFIC_DisableIRQ(GPIO_B_IRQn);
+ PWR_PeriphWakeUpCfg(DISABLE, RB_GPIO_WAKE_MODE | RB_SLP_GPIO_WAKE, Long_Delay);
+}
+
+#if 0
+uint8_t read_button_GPIO(uint8_t button_id)
+{
+ // you can share the GPIO read function with multiple Buttons
+ switch(button_id)
+ {
+ case btn1_id:
+ return (uint8_t)GPIOB_ReadPortPin(KEY_B_PIN);
+ default:
+ // logAssert(0, while (1));
+ return 0;
+ }
+}
+
+void BTN1_PRESS_DOWN_Handler(void* btn)
+{
+ logDebug("BTN1_PRESS_DOWN_Handler");
+ // BSP_KEY_EnterLowpower();
+}
+
+void BTN1_PRESS_UP_Handler(void* btn)
+{
+ logDebug("BTN1_PRESS_UP_Handler");
+ tmos_stop_task(key_task_id, KEY_IDLE_TIMEOUT_EVT);
+ tmos_start_task(key_task_id, KEY_IDLE_TIMEOUT_EVT, MS1_TO_SYSTEM_TIME(1000 * 5));
+ logDebug("tmos_start_task KEY_IDLE_TIMEOUT_EVT");
+ // BSP_KEY_EnterLowpower();
+}
+
+void BTN1_SINGLE_Click_Handler(void* btn)
+{
+ logDebug("BTN1_SINGLE_Click_Handler");
+ // BSP_KEY_EnterLowpower();
+}
+
+void BTN1_DOUBLE_Click_Handler(void* btn)
+{
+ logDebug("BTN1_DOUBLE_Click_Handler");
+ // BSP_KEY_EnterLowpower();
+}
+
+void BTN1_LONG_PRESS_START_Handler(void* btn)
+{
+ logDebug("BTN1_LONG_PRESS_START_Handler");
+ // BSP_KEY_EnterLowpower();
+}
+
+static uint16_t KEY_Task_ProcessEvent(uint8_t task_id, uint16_t events)
+{
+ if (events & SYS_EVENT_MSG)
+ {
+ uint8_t *pMsg;
+ if ((pMsg = tmos_msg_receive(key_task_id)) != NULL)
+ {
+ KEY_Task_ProcessTmosMsg((tmos_event_hdr_t *)pMsg);
+ // Release the TMOS message
+ tmos_msg_deallocate(pMsg);
+ }
+ // return unprocessed events
+ return (events ^ SYS_EVENT_MSG);
+ }
+ // if (events & KEY_WAKEUP_EVT)
+ // {
+ // logDebug("KEY_WAKEUP_EVT");
+
+ // BSP_KEY_ExitLowpower();
+ // return (events ^ KEY_WAKEUP_EVT);
+ // }
+ if (events & KEY_SCAN_EVT)
+ {
+ // 鎸夐敭娌℃姮璧锋潵灏辩户缁壂鎻
+ if (0 == key_timeout_flag)
+ {
+ tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(5));
+ }
+ else
+ {
+ logDebug("KEY_SCAN_EVT timeout");
+ BSP_KEY_EnterLowpower();
+ }
+ // logDebug("KEY_SCAN_EVT");
+ button_ticks();
+
+ return (events ^ KEY_SCAN_EVT);
+ }
+ if (events & KEY_IDLE_TIMEOUT_EVT)
+ {
+ tmos_stop_task(key_task_id, KEY_SCAN_EVT);
+ BSP_KEY_EnterLowpower();
+ logDebug("KEY_IDLE_TIMEOUT_EVT");
+ key_timeout_flag = 1;
+ return (events ^ KEY_IDLE_TIMEOUT_EVT);
+ }
+
+
+ // Discard unknown events
+ return 0;
+}
+#endif
+
+void BSP_KEY_Init(app_task_evt_handler_t handler)
+{
+ p_handler = handler;
+ key_task_id = TMOS_ProcessEventRegister(KEY_Task_ProcessEvent);
+
+ // 鐢卞閮ㄤ笂鎷夌數闃讳簡
+ // 璁剧疆涓烘诞绌鸿緭鍏ユā寮
+ // GPIOB_SetBits(KEY_B_PIN);
+ GPIOB_ModeCfg(KEY_B_PIN, GPIO_ModeIN_PU);
+
+ // 涓嬮檷娌胯Е鍙
+ GPIOB_ITModeCfg(KEY_B_PIN, GPIO_ITMode_FallEdge);
+
+ // 寮鍚疓PIO鐨勭潯鐪犲敜閱,濡傛灉闇瑕佺殑璇
+ // PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
+ PWR_PeriphWakeUpCfg(ENABLE, RB_GPIO_WAKE_MODE | RB_SLP_GPIO_WAKE, Long_Delay);
+
+ // 寮鍚疓PIOB涓柇
+ PFIC_EnableIRQ(GPIO_B_IRQn);
+
+ tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(KEY_SACN_MS));
+
+ // tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(5));
+
+ //寮濮嬩竴涓畾鏃朵簨浠,涓嶆柇鐨勬墽琛,闄ら潪杩愯tmos_stop_task鍏虫帀,
+ //tmosTimer鍏蜂綋鏄 1600 = 1s
+ // BSP_KEY_EnterLowpower();
+}
+
+__HIGH_CODE
+__attribute__((noinline))
+void KEY_ProcessLoop(void)
+{
+ // {
+ // if (R16_PB_INT_IF & KEY_B_PIN)
+ // {
+ // R16_PB_INT_IF = KEY_B_PIN;
+ // tmos_set_event(key_task_id, KEY_SCAN_EVT);
+ // logDebug("KEY_ProcessLoop");
+ // }
+ // }
+ if (key_wakeup_flag)
+ {
+ key_wakeup_flag = 0;
+ tmos_set_event(key_task_id, KEY_SCAN_EVT);
+ logDebug("KEY_ProcessLoop");
+ }
+
+
+ // if (key_wakeup_flag)
+ // {
+ // key_wakeup_flag = 0;
+ // tmos_set_event(key_task_id, KEY_SCAN_EVT);
+ // // tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(20));
+ // // tmos_start_task(key_task_id, KEY_IDLE_TIMEOUT_EVT, MS1_TO_SYSTEM_TIME(1000 * 10));
+ // logDebug("key_wakeup_flag");
+ // }
+}
+
+
+__INTERRUPT // 鍛婅瘔缂栬瘧鍣ㄤ娇鐢ㄧ‖浠跺帇鏍
+__HIGH_CODE // 鏀惧埌RAM閲
+void GPIOB_IRQHandler(void)
+{
+ // 闃绘鐫$湢
+ BSP_BlockSleep();
+ // 鍏虫寜閿腑鏂
+ BSP_KEY_ExitLowpower();
+
+ logDebug("KEY IRQ");
+
+ GPIOB_ClearITFlagBit(KEY_B_PIN);
+
+ key_wakeup_flag = 1;
+ // key_timeout_flag = 0;
+
+ // BSP_BlockSleep();
+
+ // if (R16_PB_INT_IF & KEY_B_PIN)
+ // {
+ // R16_PB_INT_IF = KEY_B_PIN;
+ // // tmos_set_event(key_task_id, KEY_SCAN_EVT);
+ // BSP_KEY_ExitLowpower();
+ // }
+ // tmos_set_event(key_task_id,KEY_SCAN_EVT);
}
diff --git a/HAL/SLEEP.c b/HAL/SLEEP.c
index f33267e..97be725 100644
--- a/HAL/SLEEP.c
+++ b/HAL/SLEEP.c
@@ -14,6 +14,31 @@
/* 头文件包含 */
#include "HAL.h"
+#include "bsp_key.h"
+
+#include "log.h"
+#include "bsp_uart.h"
+
+
+#undef LOG_ENABLE
+#define LOG_ENABLE 1
+
+#undef LOG_TAG
+#define LOG_TAG "sleep"
+
+static volatile bool block_sleep_flag = false;
+
+void BSP_RequestSleep(void)
+{
+ block_sleep_flag = false;
+}
+
+void BSP_BlockSleep(void)
+{
+ block_sleep_flag = true;
+}
+
+
/*******************************************************************************
* @fn CH59x_LowPower
*
@@ -25,6 +50,11 @@
*/
uint32_t CH59x_LowPower(uint32_t time)
{
+ if( true == block_sleep_flag)
+ {
+ // logDebug("block_sleep");
+ return 0;
+ }
#if (defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
volatile uint32_t i;
uint32_t time_sleep, time_curr;
@@ -76,10 +106,13 @@ uint32_t CH59x_LowPower(uint32_t time)
// LOW POWER-sleep模式
if (!RTCTigFlag)
{
+ BSP_KEY_EnterLowpower();
+
LowPower_Sleep(RB_PWR_RAM2K | RB_PWR_RAM24K | RB_PWR_EXTEND | RB_XT_PRE_EN);
HSECFG_Current(HSE_RCur_100); // 降为额定电流(低功耗函数中提升了HSE偏置电流)
i = RTC_GetCycle32k();
while (i == RTC_GetCycle32k());
+
return 0;
}
#endif
diff --git a/HAL/include/SLEEP.h b/HAL/include/SLEEP.h
index ffacb5e..1e798dc 100644
--- a/HAL/include/SLEEP.h
+++ b/HAL/include/SLEEP.h
@@ -6,7 +6,7 @@
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- * Attention: This software (modified or not) and binary are used for
+ * Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
@@ -22,6 +22,10 @@ extern "C" {
* GLOBAL VARIABLES
*/
+void BSP_RequestSleep(void);
+void BSP_BlockSleep(void);
+
+
/*********************************************************************
* FUNCTIONS
*/
From 5a30a18ee5cf46442ad0e45e3d9424e29286bfd8 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:25:58 +0800
Subject: [PATCH 11/16] bsp_tim ok
---
BSP/inc/bsp_tim.h | 10 ++++++++++
BSP/src/bsp_tim.c | 13 ++++++++++++-
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/BSP/inc/bsp_tim.h b/BSP/inc/bsp_tim.h
index 9405455..4074919 100644
--- a/BSP/inc/bsp_tim.h
+++ b/BSP/inc/bsp_tim.h
@@ -1,3 +1,13 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-15 15:01:15
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 16:24:28
+ * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\inc\bsp_tim.h
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
#ifndef __BSP_TIM_H__
#define __BSP_TIM_H__
diff --git a/BSP/src/bsp_tim.c b/BSP/src/bsp_tim.c
index f5ecc06..f8f71c9 100644
--- a/BSP/src/bsp_tim.c
+++ b/BSP/src/bsp_tim.c
@@ -1,3 +1,13 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-15 15:01:15
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 16:24:32
+ * @FilePath : \BLE_TYQ_CH592F - 副本\BSP\src\bsp_tim.c
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
#include "bsp_tim.h"
// tick_1ms_cnt在SysTick_Handler()中1ms +1
@@ -10,7 +20,8 @@ volatile uint32_t tick_1ms_cnt = 0;
uint32_t BSP_Get_Tick(void)
{
/* Platform implementation */
- return tick_1ms_cnt;
+ // current system clock (in 0.625ms)
+ return (uint32_t)((double)TMOS_GetSystemClock() / 1.6);
}
// SysTick中断函数
From 8f72577baa71042a9e86d3e2edb7971bdb341be0 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:38:50 +0800
Subject: [PATCH 12/16] bsp_vavle ok
---
APP/peripheral.c | 66 ++++++++++++++++++++++---
BSP/inc/bsp_uart.h | 13 +----
BSP/inc/bsp_valve.h | 34 ++++++++-----
BSP/src/bsp_uart.c | 2 +-
BSP/src/bsp_valve.c | 116 +++++++++++++++++++++++++++++++++++++-------
5 files changed, 182 insertions(+), 49 deletions(-)
diff --git a/APP/peripheral.c b/APP/peripheral.c
index b5d0f30..01cafbc 100644
--- a/APP/peripheral.c
+++ b/APP/peripheral.c
@@ -21,14 +21,17 @@
#include "bsp_valve.h"
+#include "bsp_uart.h"
#include "log.h"
+#include "bsp_i2c.h"
+#include "bsp_adc.h"
+
#undef LOG_ENABLE
#define LOG_ENABLE 1
#undef LOG_TAG
-#define LOG_TAG "BSP_VALVE"
-
+#define LOG_TAG "peripheral"
/*********************************************************************
@@ -760,14 +763,61 @@ static void performPeriodicTask(void)
// // peripheralChar4Notify(notiData, SIMPLEPROFILE_CHAR4_LEN);
// peripheralChar4Notify(notiData, 5);
// counter++;
- TsRawFrameData RawData;
- static uint8_t humi;
- humi = 0;
- BSP_VALVE_Generate_Data(&RawData, 30, 25, humi++);
- if (humi > 99)
+
+ #if 1
+ // VBAT
+ uint32_t CountBat = 0;
+ uint16_t adcBuff[40];
+ uint8_t i = 0;
+ // uint32_t countadc = 0;
+ uint16_t min_number = 0;
+ uint16_t max_number = 0;
+
+ ADC_InterBATSampInit();
+ for (i = 0; i < 20; i++)
{
- humi = 0;
+ adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次
}
+ for (i = 0; i < 20; i++)
+ {
+ CountBat += adcBuff[i];
+ if (i == 0)
+ {
+ min_number = adcBuff[i];
+ max_number = adcBuff[i];
+ }
+ min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 软件滤波
+ max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number);
+ }
+ logDebug("min_number = %d, max_number = %d", min_number, max_number);
+ CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值
+ logDebug("AverageCountBat = %d", CountBat);
+#endif
+
+ int ret;
+
+ GXHTC3C_Wakeup();
+ DelayMs(20);
+
+ GXHTC3C_GetStart();
+ DelayMs(20);
+ float humi, temp;
+ ret = GXHTC3C_GetTempHumi(&humi, &temp);
+ if (ret == 0)
+ {
+ logDebug("humi %.2f %, temp %.2f C", humi, temp);
+ }
+
+ GXHTC3C_Sleep();
+
+ TsRawFrameData RawData;
+// static uint8_t humi;
+// humi++;
+ BSP_VALVE_Generate_Data(&RawData, 0, CountBat/100, temp, humi);
+// if (humi > 99)
+// {
+// humi = 0;
+// }
peripheralChar4Notify(&RawData.buf[0], RawData.len);
}
diff --git a/BSP/inc/bsp_uart.h b/BSP/inc/bsp_uart.h
index 34acb6d..938cfd1 100644
--- a/BSP/inc/bsp_uart.h
+++ b/BSP/inc/bsp_uart.h
@@ -4,8 +4,8 @@
#include "CONFIG.h"
-#define UART1_RX_BUFFER_LENGTH 64U
-#define UART1_TX_BUFFER_LENGTH 1024U
+#define UART1_RX_BUFFER_LENGTH 24
+#define UART1_TX_BUFFER_LENGTH 256
void BSP_UART1_Init(uint32_t baudrate);
@@ -22,14 +22,5 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len);
-
-
-
-
-
-
-
-
-
#endif
diff --git a/BSP/inc/bsp_valve.h b/BSP/inc/bsp_valve.h
index 2b58587..76ed40d 100644
--- a/BSP/inc/bsp_valve.h
+++ b/BSP/inc/bsp_valve.h
@@ -2,7 +2,7 @@
* @Author : stark1898y 1658608470@qq.com
* @Date : 2024-12-12 15:55:38
* @LastEditors : stark1898y 1658608470@qq.com
- * @LastEditTime : 2024-12-12 19:08:06
+ * @LastEditTime : 2024-12-15 10:40:07
* @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_valve.h
* @Description :
*
@@ -13,6 +13,15 @@
#include "CONFIG.h"
+#define VAVLE_RX_DATA_EVT (0x0001 << 0)
+#define VAVLE_TX_DATA_EVT (0x0001 << 1)
+
+#define VAVLE_CLOSE_START_EVT (0x0001 << 2) // 鍏抽榾锛屽紑濮嬪厖鐢
+#define VAVLE_CLOSE_END_EVT (0x0001 << 2) // 鍏抽榾锛屽紑濮嬪厖鐢
+
+#define CHARGE_TIME_MS 1000
+
+
#define FRAME_HEADER 0xAA
#define FRAME_TAIL 0x55
@@ -55,19 +64,18 @@ typedef enum
typedef struct __attribute__((packed))
{
- uint8_t type; // 闃闂ㄧ被鍨
-
- uint8_t bat; // 1B 鐢垫睜鐢靛帇 30=3V,18=1.8V
- int8_t temp; // 1B 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩
- uint8_t hum; // 1B 闃闂ㄦ箍搴 %RH
-
// uint8_t status; // 1B 闃闂ㄥ伐浣滅姸鎬 瓒呮瑺鍘嬨佽繃娴
// uint8_t connct_status; // 1B 闃闂ㄨ繛鎺ョ姸鎬 锛堢數纾侀榾BLE鎺у埗鐩掞級 0=鏈繛鎺 1=宸茶繛鎺
- // uint8_t switch_status; // 1B 闃闂ㄥ紑鍏崇姸鎬 0=鏈煡 1=鍏抽棴 2=鍏抽棴杩囷紝鏈寜鎸夐敭鎭㈠ 3=鍏抽棴杩囷紝鎸変笅浜嗘仮澶嶉敭 4=鎵撳紑
+ uint8_t switch_status; // 1B 闃闂ㄥ紑鍏崇姸鎬 0=鏈煡 1=鍏抽棴 2=鍏抽棴杩囷紝鏈寜鎸夐敭鎭㈠ 3=鍏抽棴杩囷紝鎸変笅浜嗘仮澶嶉敭 4=鎵撳紑
+ int8_t temp; // 1B 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩
- // uint32_t in_pressure; // 4B 闃闂ㄥ叆鍙e帇鍔 Pa
- // uint32_t out_pressure; // 4B 闃闂ㄥ嚭鍙e帇鍔 Pa
- // uint32_t atm_pressure; // 4B 闃闂ㄥぇ姘斿帇鍔 Pa
+ uint32_t in_pressure; // 4B 闃闂ㄥ叆鍙e帇鍔 Pa
+ uint32_t out_pressure; // 4B 闃闂ㄥ嚭鍙e帇鍔 Pa
+ uint32_t atm_pressure; // 4B 闃闂ㄥぇ姘斿帇鍔 Pa
+ uint8_t type; // 闃闂ㄧ被鍨
+ uint8_t bat; // 1B 鐢垫睜鐢靛帇 30=3V,18=1.8V
+ uint8_t hum; // 1B 闃闂ㄦ箍搴 %RH
+ uint8_t rssi;
} TsValveData;
uint8_t CheckSum(const uint8_t *data, size_t len);
@@ -76,7 +84,9 @@ void BSP_VAVLE_Init(void);
TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len);
-void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t bat, int8_t temp, uint8_t hum);
+uint8_t GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len);
+
+void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t switch_status, uint8_t bat, int8_t temp, uint8_t hum);
void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status);
#endif // ! __BSP_VALVE_H__
diff --git a/BSP/src/bsp_uart.c b/BSP/src/bsp_uart.c
index 457804c..283cd9e 100644
--- a/BSP/src/bsp_uart.c
+++ b/BSP/src/bsp_uart.c
@@ -22,8 +22,8 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len)
unsigned int ret;
ret = lwrb_write(&Uart1Tx, buf, len);
- BSP_UART1_TxLoop();
// UART1_INTCfg(ENABLE, RB_IER_THR_EMPTY);
+ BSP_UART1_TxLoop();
return ret;
}
diff --git a/BSP/src/bsp_valve.c b/BSP/src/bsp_valve.c
index 4d44407..fe34fb3 100644
--- a/BSP/src/bsp_valve.c
+++ b/BSP/src/bsp_valve.c
@@ -1,3 +1,13 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-12-15 15:01:15
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-15 16:31:46
+ * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\src\bsp_valve.c
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
/*
* @Author: mbw
* @Date: 2024-12-09 11:40:10
@@ -10,20 +20,21 @@
*/
#include "bsp_valve.h"
#include "bsp_flash.h"
-#include "bsp_uart.h"
#include "bsp_beep_led_emv.h"
// 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55
-
+#include "bsp_uart.h"
#include "log.h"
-#undef LOG_ENABLE
-#define LOG_ENABLE 0
+#undef LOG_ENABLE
+#define LOG_ENABLE 0
#undef LOG_TAG
#define LOG_TAG "BSP_VALVE"
+//瀛樺偍 褰撳墠task id 鐨勫叏灞鍙橀噺
+tmosTaskID vavle_task_id = INVALID_TASK_ID;
/**
@@ -110,8 +121,9 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len)
if (len < 4)
{ // 鑷冲皯闇瑕 4 涓瓧鑺傦細璧峰鐮併侀暱搴︺佹牎楠岀爜銆佺粨鏉熺爜
logError("鏁版嵁甯ч暱搴︿笉瓒");
- logHexDumpAll(data, len);
+ // logHexDumpAll(data, len);
// return 1;
+ return NULL;
}
while (index < len && data[index] != FRAME_HEADER) // 瀵绘壘璧峰鐮 0xAA
{
@@ -120,14 +132,16 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len)
if (index >= len - 3) // 涓嶅绌洪棿瀹圭撼闀垮害銆佹牎楠岀爜鍜岀粨鏉熺爜
{
logError("鏁版嵁甯ц捣濮嬬爜閿欒");
- logHexDumpAll(data, len);
+ // logHexDumpAll(data, len);
+ return NULL;
// return 2;
}
uint16_t data_len = data[index + 2]; // 璇诲彇鏁版嵁闀垮害
if (index + 3 + data_len + 1 >= len) // 妫鏌ユ暟鎹暱搴︽槸鍚﹀悎鐞 鏁版嵁闀垮害 + 鏍¢獙鐮 + 缁撴潫鐮
{
logError("鏁版嵁甯ч暱搴﹂敊璇");
- logHexDumpAll(data, len);
+ // logHexDumpAll(data, len);
+ return NULL;
// return 3;
}
@@ -136,13 +150,15 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len)
if (check_sum != calculated_sum)
{
logError("鏁版嵁甯ф牎楠岀爜閿欒 check_sum = %02X, calculated_sum = %02X", check_sum, calculated_sum);
- logHexDumpAll(data, len);
+ // logHexDumpAll(data, len);
+ return NULL;
// return 4;
}
if (data[index + 3 + data_len + 1] != FRAME_TAIL) // 妫鏌ョ粨鏉熺爜
{
logError("鏁版嵁甯х粨鏉熺爜閿欒");
- logHexDumpAll(data, len);
+ // logHexDumpAll(data, len);
+ return NULL;
// return 5;
}
logDebug("鏁版嵁甯ф牎楠岄氳繃");
@@ -177,7 +193,7 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len)
// }
}
-uint8_t _GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len)
+uint8_t GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len)
{
// 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55
pRawData->len = src_len + 5;
@@ -195,30 +211,38 @@ uint8_t _GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *
pRawData->buf[pRawData->len - 2] = CheckSum(&pRawData->buf[0], pRawData->len - 2);
pRawData->buf[pRawData->len - 1] = FRAME_TAIL;
- logHexDumpAll(&pRawData->buf[0], pRawData->len);
+ // logHexDumpAll(&pRawData->buf[0], pRawData->len);
return 0;
}
-void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t bat, int8_t temp, uint8_t hum)
+void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t switch_status, uint8_t bat, int8_t temp, uint8_t hum)
{
TsValveData ValveData;
+ ValveData.switch_status = 0;
+ ValveData.temp = temp; // 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩
+
+ ValveData.in_pressure = 1.6 * 1000000;
+ ValveData.out_pressure = 3000;
+ ValveData.atm_pressure = 96000;
+
ValveData.type = kTyq; // 闃闂ㄧ被鍨
ValveData.bat = bat; // 鐢垫睜鐢靛帇 30=3V,18=1.8V
- ValveData.temp = temp; // 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩
ValveData.hum = hum; // 闃闂ㄦ箍搴 %RH
- _GenerateRawFrame(pRawData, kCmdData, (uint8_t*)&ValveData, sizeof(ValveData));
- logHexDumpAll(&pRawData->buf[0], pRawData->len);
+ ValveData.rssi = 0;
+
+ GenerateRawFrame(pRawData, kCmdData, (uint8_t*)&ValveData, sizeof(ValveData));
+ // logHexDumpAll(&pRawData->buf[0], pRawData->len);
}
void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status)
{
uint8_t data = 0;
data = status;
- _GenerateRawFrame(pRawData, cmd, &data, 1);
- logHexDumpAll(&pRawData->buf[0], pRawData->len);
+ GenerateRawFrame(pRawData, cmd, &data, 1);
+ // logHexDumpAll(&pRawData->buf[0], pRawData->len);
}
void BSP_CloseValve(void)
@@ -238,8 +262,66 @@ void BSP_CloseValve(void)
DelayMs(100);
}
+static void VAVLE_Task_ProcessTmosMsg(tmos_event_hdr_t *pMsg)
+{
+ switch (pMsg->event)
+ {
+ default:
+ logDebug("pMsg->event %04x", pMsg->event);
+ break;
+ }
+}
+
+//task鐨別vent澶勭悊鍥炶皟鍑芥暟,闇瑕佸湪娉ㄥ唽task鏃跺,浼犺繘鍘
+static uint16_t VAVLE_Task_ProcessEvent( uint8_t task_id, uint16_t events )
+{
+ if (events & SYS_EVENT_MSG)
+ {
+ uint8_t *pMsg;
+ if ((pMsg = tmos_msg_receive(vavle_task_id)) != NULL)
+ {
+ VAVLE_Task_ProcessTmosMsg((tmos_event_hdr_t *)pMsg);
+ // Release the TMOS message
+ tmos_msg_deallocate(pMsg);
+ }
+ // return unprocessed events
+ return (events ^ SYS_EVENT_MSG);
+ }
+ if (events & VAVLE_RX_DATA_EVT)
+ {
+ logDebug("VAVLE_RX_DATA_EVT");
+
+ return (events ^ VAVLE_RX_DATA_EVT);
+ }
+ if (events & VAVLE_TX_DATA_EVT)
+ {
+ logDebug("VAVLE_TX_DATA_EVT");
+
+ return (events ^ VAVLE_TX_DATA_EVT);
+ }
+ if (events & VAVLE_CLOSE_START_EVT)
+ {
+ logDebug("VAVLE_CLOSE_START_EVT");
+
+ //寮濮嬩竴涓畾鏃秂vent,1s鍚庝骇鐢,褰撳墠璇彞鍙細浜х敓涓娆vent
+ //鍙互鍦╡vent浜х敓鍚庡幓寮鍚痚vent,鍙互鏄埆鐨則ask鐨,涔熷彲浠ユ槸褰撳墠task鐨別vent
+ tmos_start_task(vavle_task_id, VAVLE_CLOSE_END_EVT, MS1_TO_SYSTEM_TIME(CHARGE_TIME_MS));
+
+ return (events ^ VAVLE_CLOSE_START_EVT);
+ }
+
+
+
+ // Discard unknown events
+ return 0;
+}
+
void BSP_VAVLE_Init(void)
{
+ vavle_task_id = TMOS_ProcessEventRegister(VAVLE_Task_ProcessEvent);
+
+ // //绔嬪嵆寮濮嬩竴涓猠vent
+ // tmos_set_event(vavle_task_id, VAVLE_RX_DATA_EVT);
logInfo("BSP_Valve_Init");
}
From ba4f6509cbc9a68f2677cf3d6d7b1ed1222e16a5 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:43:31 +0800
Subject: [PATCH 13/16] =?UTF-8?q?=E6=9A=82=E5=AD=98=EF=BC=8C=E5=89=A9?=
=?UTF-8?q?=E4=B8=8BAPP=E7=9A=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HAL/include/CONFIG.h | 2 +-
common/FlexibleButton-2.0.1/README.md | 325 ++++++++++++++++++
common/FlexibleButton-2.0.1/flexible_button.c | 317 +++++++++++++++++
common/FlexibleButton-2.0.1/flexible_button.h | 164 +++++++++
common/MultiButton/LICENSE | 21 ++
common/MultiButton/README.md | 131 +++++++
common/MultiButton/multi_button.c | 208 +++++++++++
common/MultiButton/multi_button.h | 62 ++++
8 files changed, 1229 insertions(+), 1 deletion(-)
create mode 100644 common/FlexibleButton-2.0.1/README.md
create mode 100644 common/FlexibleButton-2.0.1/flexible_button.c
create mode 100644 common/FlexibleButton-2.0.1/flexible_button.h
create mode 100644 common/MultiButton/LICENSE
create mode 100644 common/MultiButton/README.md
create mode 100644 common/MultiButton/multi_button.c
create mode 100644 common/MultiButton/multi_button.h
diff --git a/HAL/include/CONFIG.h b/HAL/include/CONFIG.h
index 672d892..4d10b1d 100644
--- a/HAL/include/CONFIG.h
+++ b/HAL/include/CONFIG.h
@@ -124,7 +124,7 @@
#define CLK_OSC32K 1 // 该项请勿在此修改,必须在工程配置里的预处理中修改,如包含主机角色必须使用外部32K
#endif
#ifndef BLE_MEMHEAP_SIZE
-#define BLE_MEMHEAP_SIZE (1024*6)
+#define BLE_MEMHEAP_SIZE (1024*7)
#endif
#ifndef BLE_BUFF_MAX_LEN
#define BLE_BUFF_MAX_LEN 251
diff --git a/common/FlexibleButton-2.0.1/README.md b/common/FlexibleButton-2.0.1/README.md
new file mode 100644
index 0000000..cebc90c
--- /dev/null
+++ b/common/FlexibleButton-2.0.1/README.md
@@ -0,0 +1,325 @@
+# FlexibleButton
+
+FlexibleButton 鏄竴涓熀浜庢爣鍑 C 璇█鐨勫皬宸х伒娲荤殑鎸夐敭澶勭悊搴擄紝鏀寔鍗曞嚮銆佽繛鍑汇佺煭鎸夈侀暱鎸夈佽嚜鍔ㄦ秷鎶栵紝鍙互鑷敱璁剧疆缁勫悎鎸夐敭锛屽彲鐢ㄤ簬涓柇鍜屼綆鍔熻楀満鏅
+
+璇ユ寜閿簱瑙h︿簡鍏蜂綋鐨勬寜閿‖浠剁粨鏋勶紝鐞嗚涓婃敮鎸佽交瑙︽寜閿笌鑷攣鎸夐敭锛屽苟鍙互鏃犻檺鎵╁睍鎸夐敭鏁伴噺銆傚彟澶栵紝FlexibleButton 浣跨敤鎵弿鐨勬柟寮忎竴娆℃ц鍙栨墍鏈夋墍鏈夌殑鎸夐敭鐘舵侊紝鐒跺悗閫氳繃浜嬩欢鍥炶皟鏈哄埗涓婃姤鎸夐敭浜嬩欢銆傛牳蹇冪殑鎸夐敭鎵弿浠g爜浠呮湁涓夎锛屾病閿欙紝灏辨槸缁忓吀鐨 **涓夎鎸夐敭鎵弿绠楁硶**銆備娇鐢 C 璇█鏍囧噯搴 API 缂栧啓锛屼篃浣垮緱璇ユ寜閿簱鍙互鏃犵紳鍏煎浠绘剰鐨勫鐞嗗櫒骞冲彴锛屽苟涓旀敮鎸佷换鎰 OS 鍜 non-OS锛堣8鏈虹紪绋嬶級銆
+
+## 鑾峰彇
+
+### Git 鏂瑰紡
+
+```SHELL
+git clone https://github.com/murphyzhao/FlexibleButton.git
+```
+
+### RT-Thread menuconfig 鏂瑰紡
+
+```
+RT-Thread online packages --->
+ miscellaneous packages --->
+ [*] FlexibleButton: Small and flexible button driver --->
+ [*] Enable flexible button demo
+ version (latest) --->
+```
+
+閰嶇疆瀹屾垚鍚庯紝杈撳叆 `pkgs --update` 涓嬭浇杞欢鍖呫
+
+## 璧勬簮缁熻
+
+ARMCC -O0 浼樺寲鐨勬儏鍐典笅锛孎lexibleButton 璧勬簮鍗犵敤濡備笅锛
+
+- CODE锛798 瀛楄妭
+- RO DATA锛0
+- RW DATA锛13 瀛楄妭
+- ZI DATA锛0
+
+## 蹇熶綋楠
+
+FlexibleButton 搴撲腑鎻愪緵浜嗕竴涓祴璇曚緥绋 [`./examples/demo_rtt_iotboard.c`](./examples/demo_rtt_iotboard.c)锛岃渚嬬▼鍩轰簬 RT-Thread OS 杩涜娴嬭瘯锛岀‖浠跺钩鍙伴夋嫨浜 *RT-Thread IoT Board Pandora v2.51* 寮鍙戞澘銆傚綋鐒朵綘鍙互閫夋嫨浣跨敤鍏朵粬鐨 OS锛屾垨鑰呬娇鐢ㄨ8鏈烘祴璇曪紝鍙渶瑕佺Щ闄 OS 鐩稿叧鐨勭壒鎬у嵆鍙
+
+濡傛灉浣犱娇鐢ㄨ嚜宸辩殑纭欢骞冲彴锛屽彧闇瑕佸皢 FlexibleButton 搴撴簮鐮佸拰渚嬬▼鍔犲叆浣犳棦鏈夌殑宸ョ▼涓嬪嵆鍙
+
+## DEMO 绋嬪簭璇存槑
+
+璇ョず渚嬬▼搴忓彲浠ョ洿鎺ュ湪 RT-Thread [`stm32l475-atk-pandora`](https://github.com/RT-Thread/rt-thread/tree/master/bsp/stm32/stm32l475-atk-pandora) BSP 涓繍琛岋紝鍙互鍦ㄨ BSP 鐩綍涓嬶紝浣跨敤 menuconfig 鑾峰彇鏈蒋浠跺寘銆
+
+### 纭畾鐢ㄦ埛鎸夐敭
+
+```C
+typedef enum
+{
+ USER_BUTTON_0 = 0, // 瀵瑰簲 IoT Board 寮鍙戞澘鐨 PIN_KEY0
+ USER_BUTTON_1, // 瀵瑰簲 IoT Board 寮鍙戞澘鐨 PIN_KEY1
+ USER_BUTTON_2, // 瀵瑰簲 IoT Board 寮鍙戞澘鐨 PIN_KEY2
+ USER_BUTTON_3, // 瀵瑰簲 IoT Board 寮鍙戞澘鐨 PIN_WK_UP
+ USER_BUTTON_MAX
+} user_button_t;
+
+static flex_button_t user_button[USER_BUTTON_MAX];
+```
+
+涓婅堪浠g爜瀹氫箟浜 4 涓寜閿紝鏁版嵁缁撴瀯瀛樺偍鍦 `user_button` 鏁扮粍涓
+
+### 绋嬪簭鍏ュ彛
+
+```C
+int flex_button_main(void)
+{
+ rt_thread_t tid = RT_NULL;
+ user_button_init();
+ /* 鍒涘缓鎸夐敭鎵弿绾跨▼ flex_btn锛岀嚎绋嬫爤 1024 byte锛屼紭鍏堢骇 10 */
+ tid = rt_thread_create("flex_btn", button_scan, RT_NULL, 1024, 10, 10);
+ if(tid != RT_NULL)
+ {
+ rt_thread_startup(tid);
+ }
+ return 0;
+}
+/* 浣跨敤 RT-Thread 鐨勮嚜鍔ㄥ垵濮嬪寲 */
+INIT_APP_EXPORT(flex_button_main);
+```
+
+濡備笂浠g爜鎵绀猴紝棣栧厛浣跨敤 `user_button_init();` 鍒濆鍖栫敤鎴锋寜閿‖浠讹紝璇ユ楠ゅ皢鐢ㄦ埛鎸夐敭缁戝畾鍒 FlexibleButton 搴撱傜劧鍚庯紝浣跨敤 RT-Thread 鐨 `INIT_APP_EXPORT` 鎺ュ彛瀵煎嚭涓轰笂鐢佃嚜鍔ㄥ垵濮嬪寲锛屽垱寤轰簡涓涓 鈥渇lex_btn鈥 鍚嶅瓧鐨勬寜閿壂鎻忕嚎绋嬶紝绾跨▼閲屾壂鎻忔鏌ユ寜閿簨浠躲
+
+### 鎸夐敭鍒濆鍖栦唬鐮
+
+`user_button_init();` 鍒濆鍖栦唬鐮佸涓嬫墍绀猴細
+
+```
+static void user_button_init(void)
+{
+ int i;
+
+ /* 鍒濆鍖栨寜閿暟鎹粨鏋 */
+ rt_memset(&user_button[0], 0x0, sizeof(user_button));
+
+ /* 鍒濆鍖 IoT Board 鎸夐敭寮曡剼锛屼娇鐢 rt-thread PIN 璁惧妗嗘灦 */
+ rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP); /* 璁剧疆 GPIO 涓轰笂鎷夎緭鍏ユā寮 */
+ rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT_PULLUP); /* 璁剧疆 GPIO 涓轰笂鎷夎緭鍏ユā寮 */
+ rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT_PULLUP); /* 璁剧疆 GPIO 涓轰笂鎷夎緭鍏ユā寮 */
+ rt_pin_mode(PIN_WK_UP, PIN_MODE_INPUT_PULLDOWN); /* 璁剧疆 GPIO 涓轰笅鎷夎緭鍏ユā寮 */
+
+ for (i = 0; i < USER_BUTTON_MAX; i ++)
+ {
+ user_button[i].id = i;
+ user_button[i].usr_button_read = common_btn_read;
+ user_button[i].cb = common_btn_evt_cb;
+ user_button[i].pressed_logic_level = 0;
+ user_button[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1500);
+ user_button[i].long_press_start_tick = FLEX_MS_TO_SCAN_CNT(3000);
+ user_button[i].long_hold_start_tick = FLEX_MS_TO_SCAN_CNT(4500);
+
+ if (i == USER_BUTTON_3)
+ {
+ user_button[USER_BUTTON_3].pressed_logic_level = 1;
+ }
+
+ flex_button_register(&user_button[i]);
+ }
+}
+```
+
+鏍稿績鐨勯厤缃涓嬶細
+
+|閰嶇疆椤箌璇存槑|
+| :---- | :----|
+| id | 鎸夐敭缂栧彿 |
+| usr_button_read | 璁剧疆鎸夐敭璇诲煎洖璋冨嚱鏁 |
+| cb | 璁剧疆鎸夐敭浜嬩欢鍥炶皟鍑芥暟 |
+| pressed_logic_level | 璁剧疆鎸夐敭鎸変笅鏃剁殑閫昏緫鐢靛钩 |
+| short_press_start_tick | 鐭寜璧峰 tick锛屼娇鐢 FLEX_MS_TO_SCAN_CNT 瀹忚浆鍖栦负鎵弿娆℃暟 |
+| long_press_start_tick | 闀挎寜璧峰 tick锛屼娇鐢 FLEX_MS_TO_SCAN_CNT 瀹忚浆鍖栦负鎵弿娆℃暟 |
+| long_hold_start_tick | 瓒呴暱鎸夎捣濮 tick锛屼娇鐢 FLEX_MS_TO_SCAN_CNT 瀹忚浆鍖栦负鎵弿娆℃暟 |
+
+娉ㄦ剰锛宻hort_press_start_tick銆乴ong_press_start_tick 鍜 long_hold_start_tick 蹇呴』浣跨敤 `FLEX_MS_TO_SCAN_CNT` 灏嗘绉掓椂闂磋浆鍖栦负鎵弿娆℃暟銆
+
+`user_button[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1500);` 琛ㄧず鎸夐敭鎸変笅寮濮嬭鏃讹紝1500 ms 鍚庢寜閿緷鏃ф槸鎸変笅鐘舵佺殑璇濓紝灏辨柇瀹氫负鐭寜寮濮嬨
+
+### 浜嬩欢澶勭悊浠g爜
+
+```C
+static void common_btn_evt_cb(void *arg)
+{
+ flex_button_t *btn = (flex_button_t *)arg;
+
+ rt_kprintf("id: [%d - %s] event: [%d - %30s] repeat: %d\n",
+ btn->id, enum_btn_id_string[btn->id],
+ btn->event, enum_event_string[btn->event],
+ btn->click_cnt);
+
+ if (flex_button_event_read(&user_button[USER_BUTTON_0]) == flex_button_event_read(&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_CLICK)
+ {
+ rt_kprintf("[combination]: button 0 and button 1\n");
+ }
+}
+```
+
+绀轰緥浠g爜涓紝灏嗘墍鏈夌殑鎸夐敭浜嬩欢鍥炶皟鍧囩粦瀹氬埌 `common_btn_evt_cb` 鍑芥暟锛屽湪璇ュ嚱鏁颁腑鎵撳嵃浜嗘寜閿 ID 鍜屾寜閿簨浠讹紝浠ュ強鎸夐敭杩炲嚮娆℃暟锛屽苟婕旂ず浜嗗浣曚娇鐢ㄧ粍鍚堟寜閿
+
+## FlexibleButton 浠g爜璇存槑
+
+### 鎸夐敭浜嬩欢瀹氫箟
+
+鎸夐敭浜嬩欢鐨勫畾涔夊苟娌℃湁浣跨敤 Windows 椹卞姩涓婄殑瀹氫箟锛屼富瑕佹槸鏂逛究宓屽叆寮忚澶囦腑鐨勫簲鐢ㄥ満鏅紙涔熷彲鑳芥槸鎴戠悊瑙g殑鍋忓樊锛夛紝鎸夐敭浜嬩欢瀹氫箟濡備笅锛
+
+```C
+typedef enum
+{
+ FLEX_BTN_PRESS_DOWN = 0, // 鎸変笅浜嬩欢
+ FLEX_BTN_PRESS_CLICK, // 鍗曞嚮浜嬩欢
+ FLEX_BTN_PRESS_DOUBLE_CLICK, // 鍙屽嚮浜嬩欢
+ FLEX_BTN_PRESS_REPEAT_CLICK, // 杩炲嚮浜嬩欢锛屼娇鐢 flex_button_t 涓殑 click_cnt 鏂畾杩炲嚮娆℃暟
+ FLEX_BTN_PRESS_SHORT_START, // 鐭寜寮濮嬩簨浠
+ FLEX_BTN_PRESS_SHORT_UP, // 鐭寜鎶捣浜嬩欢
+ FLEX_BTN_PRESS_LONG_START, // 闀挎寜寮濮嬩簨浠
+ FLEX_BTN_PRESS_LONG_UP, // 闀挎寜鎶捣浜嬩欢
+ FLEX_BTN_PRESS_LONG_HOLD, // 闀挎寜淇濇寔浜嬩欢
+ FLEX_BTN_PRESS_LONG_HOLD_UP, // 闀挎寜淇濇寔鐨勬姮璧蜂簨浠
+ FLEX_BTN_PRESS_MAX,
+ FLEX_BTN_PRESS_NONE,
+} flex_button_event_t;
+```
+
+鍏朵腑 `FLEX_BTN_PRESS_LONG_HOLD` 浜嬩欢鍙互鐢ㄦ潵瀹炵幇闀挎寜绱姞鐨勫簲鐢ㄥ満鏅
+
+### 鎸夐敭鏁版嵁缁撴瀯
+
+```
+typedef struct flex_button
+{
+ struct flex_button* next;
+
+ uint8_t (*usr_button_read)(void *);
+ flex_button_response_callback cb;
+
+ uint16_t scan_cnt;
+ uint16_t click_cnt;
+ uint16_t max_multiple_clicks_interval;
+
+ uint16_t debounce_tick;
+ uint16_t short_press_start_tick;
+ uint16_t long_press_start_tick;
+ uint16_t long_hold_start_tick;
+
+ uint8_t id;
+ uint8_t pressed_logic_level : 1;
+ uint8_t event : 4;
+ uint8_t status : 3;
+} flex_button_t;
+```
+
+| 搴忓彿 | 鏁版嵁鎴愬憳 | 鏄惁闇瑕佺敤鎴峰垵濮嬪寲 | 璇存槑 |
+| :----: | :---- | :----: | :---- |
+| 1 | next | 鍚 | 鎸夐敭搴撲娇鐢ㄥ崟鍚戦摼琛ㄤ覆璧锋墍鏈夌殑鎸夐敭 |
+| 2 | usr_button_read | 鏄 | 鐢ㄦ埛璁惧鐨勬寜閿紩鑴氱數骞宠鍙栧嚱鏁帮紝**閲嶈** |
+| 3 | cb | 鏄 | 璁剧疆鎸夐敭浜嬩欢鍥炶皟锛岀敤浜庡簲鐢ㄥ眰瀵规寜閿簨浠剁殑鍒嗙被澶勭悊 |
+| 4 | scan_cnt | 鍚 | 鐢ㄤ簬璁板綍鎵弿娆℃暟锛屾寜閿寜涓嬫槸寮濮嬩粠闆惰鏁 |
+| 5 | click_cnt | 鍚 | 璁板綍鍗曞嚮娆℃暟锛岀敤浜庡垽瀹氬崟鍑汇佽繛鍑 |
+| 6 | max_multiple_clicks_interval | 鏄 | 杩炲嚮闂撮殭锛岀敤浜庡垽瀹氭槸鍚︾粨鏉熻繛鍑昏鏁帮紝鏈夐粯璁ゅ `MAX_MULTIPLE_CLICKS_INTERVAL` |
+| 7 | debounce_tick | 鍚 | 娑堟姈鏃堕棿锛屾殏鏈娇鐢紝渚濋潬鎵弿闂撮殭杩涜娑堟姈 |
+| 8 | short_press_start_tick | 鏄 | 璁剧疆鐭寜浜嬩欢瑙﹀彂鐨勮捣濮 tick |
+| 9 | long_press_start_tick | 鏄 | 璁剧疆闀挎寜浜嬩欢瑙﹀彂鐨勮捣濮 tick |
+| 10 | long_hold_start_tick | 鏄 | 璁剧疆闀挎寜淇濇寔浜嬩欢瑙﹀彂鐨勮捣濮 tick |
+| 11 | id | 鏄 | 褰撳涓寜閿娇鐢ㄥ悓涓涓洖璋冨嚱鏁版椂锛岀敤浜庢柇瀹氬睘浜庡摢涓寜閿 |
+| 12 | pressed_logic_level | 鏄 | 璁剧疆鎸夐敭鎸変笅鐨勯昏緫鐢靛钩銆1锛氭爣璇嗘寜閿寜涓嬬殑鏃跺欎负楂樼數骞筹紱0锛氭爣璇嗘寜閿寜涓嬬殑鏃跺欐湭浣庣數骞筹紝**閲嶈** |
+| 13 | event | 鍚 | 鐢ㄤ簬璁板綍褰撳墠鎸夐敭浜嬩欢 |
+| 14 | status | 鍚 | 鐢ㄤ簬璁板綍褰撳墠鎸夐敭鐨勭姸鎬侊紝鐢ㄤ簬鍐呴儴鐘舵佹満 |
+
+娉ㄦ剰锛屽湪浣跨敤 `max_multiple_clicks_interval`銆乣debounce_tick`銆乣short_press_start_tick`銆乣long_press_start_tick`銆乣long_hold_start_tick` 鐨勬椂鍊欙紝娉ㄦ剰闇瑕佷娇鐢ㄥ畯 `**FLEX_MS_TO_SCAN_CNT(ms)**` 灏嗘绉掑艰浆鎹负鎵弿娆℃暟銆傚洜涓烘寜閿簱鍩轰簬鎵弿娆℃暟杩愯浆銆傜ず渚嬪涓嬶細
+
+```
+user_button[1].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1500); // 1500 姣
+```
+
+涓婅堪浠g爜琛ㄧず锛氳〃绀烘寜閿寜涓嬪悗寮濮嬭鏃讹紝1500ms 鐨勬椂鍊欙紝鎸夐敭渚濇棫鎸変笅锛屽垯鏂畾涓虹煭鎸夊紑濮嬶紝骞朵笂鎶 `FLEX_BTN_PRESS_SHORT_START` 浜嬩欢銆
+
+### 鎸夐敭娉ㄥ唽鎺ュ彛
+
+浣跨敤璇ユ帴鍙f敞鍐屼竴涓敤鎴锋寜閿紝鍏ュ弬涓轰竴涓 flex_button_t 缁撴瀯浣撳疄渚嬬殑鍦板潃銆
+
+```C
+int8_t flex_button_register(flex_button_t *button);
+```
+
+### 鎸夐敭浜嬩欢璇诲彇鎺ュ彛
+
+浣跨敤璇ユ帴鍙h幏鍙栨寚瀹氭寜閿殑浜嬩欢銆
+
+```C
+flex_button_event_t flex_button_event_read(flex_button_t* button);
+````
+
+### 鎸夐敭鎵弿鎺ュ彛
+
+鎸夐敭鎵弿鐨勬牳蹇冨嚱鏁帮紝闇瑕佹斁鍒板簲鐢ㄧ▼搴忎腑瀹氭椂鎵弿锛屾壂鎻忛棿闅斿缓璁 20 姣銆
+
+```C
+void flex_button_scan(void);
+```
+
+## 娉ㄦ剰浜嬮」
+
+- 闃诲闂
+
+ 鍥犱负鎸夐敭浜嬩欢鍥炶皟鍑芥暟浠ュ強鎸夐敭閿艰鍙栧嚱鏁版槸鍦ㄦ寜閿壂鎻忕殑杩囩▼涓墽琛岀殑锛屽洜姝よ涓嶈鍦ㄨ繖绫诲嚱鏁颁腑浣跨敤闃诲鎺ュ彛锛屼笉瑕佽繘琛屽欢鏃舵搷浣溿
+
+- 鎸夐敭鎵弿鍑芥暟鏍堥渶姹
+
+ 鎸夐敭鎵弿鍑芥暟鏈韩瀵规爤鐨勯渶姹傚皬浜 300 瀛楄妭锛屼絾鏄寜閿簨浠跺洖璋冨嚱鏁板拰鎸夐敭閿艰鍙栧嚱鏁伴兘鏄湪鎸夐敭鎵弿鍑芥暟鐨勪笂涓嬫枃涓墽琛岀殑锛岃鏍煎鍏冲績鎸夐敭浜嬩欢鍥炶皟鍑芥暟涓庢寜閿敭鍊艰鍙栧嚱鏁板鏍堢┖闂寸殑闇姹傘
+
+## 鍏跺畠
+
+### 鍏充簬浣庡姛鑰
+
+鏈寜閿簱鏄氳繃涓嶉棿鏂壂鎻忕殑鏂瑰紡鏉ユ鏌ユ寜閿姸鎬侊紝鍥犳浼氫竴鐩村崰鐢 CPU 璧勬簮锛岃繖瀵逛綆鍔熻楀簲鐢ㄥ満鏅槸涓嶅弸濂界殑銆備负浜嗛檷浣庢甯稿伐浣滄ā寮忎笅鐨勫姛鑰楋紝寤鸿鍚堢悊閰嶇疆鎵弿鍛ㄦ湡锛5ms - 20ms锛夛紝鎵弿闂撮殭閲 CPU 鍙互杩涘叆杞诲害鐫$湢銆
+
+璇ユ寜閿簱涓嶅湪搴曞眰瀹炵幇浣庡姛鑰楀鐞嗭紝搴旂敤灞傚彲浠ユ牴鎹嚜宸辩殑鍔熻楁ā寮忕伒娲诲鐞嗭紝閫氬父浼氭湁浠ヤ笅涓ょ鏂瑰紡锛
+
+1. 杩涘叆浣庡姛鑰楀墠锛屾寕璧锋寜閿壂鎻忕嚎绋嬶紱閫鍑轰綆鍔熻楀悗锛屽敜閱掓寜閿壂鎻忋
+2. 澧炲姞鎸夐敭涓柇妯″紡锛屾墍鏈夌殑鎸夐敭涓柇鏉ワ紝灏辫Е鍙戜竴娆℃寜閿壂鎻忥紝浠ョ‘璁ゆ墍鏈夌殑鎸夐敭鐘舵併
+
+> 浣庡姛鑰楃浉鍏崇殑鎺㈣鍙傝 [issue 1](https://github.com/murphyzhao/FlexibleButton/issues/1) 涓殑璁ㄨ銆
+
+### 鍏充簬鎸夐敭涓柇妯″紡
+
+鐢变簬璇ユ寜閿簱涓娆℃壂鎻忓彲浠ョ‘瀹氭墍鏈夌殑鎸夐敭鐘舵侊紝鍥犳鍙互灏嗘墍鏈夌殑鎸夐敭涓柇閫氳繃 鈥**鎴**鈥 鐨勬柟寮忚浆鍖栦负涓涓腑鏂紝鐒跺悗鍦ㄤ腑鏂鐞嗗嚱鏁颁腑鎵ц涓娆℃寜閿壂鎻忋
+
+涓柇 鈥**鎴**鈥 鐨勬柟寮忓彲浠ラ氳繃纭欢鏉ュ畬鎴愶紝涔熷彲浠ラ氳繃杞欢鏉ュ畬鎴愩
+
+纭欢鏂瑰紡锛岄渶瑕佷娇鐢ㄤ竴涓 **鎴栭棬** 鑺墖锛屽涓緭鍏ユ潯浠惰浆鍖栦负涓涓緭鍑烘潯浠讹紝鐒跺悗閫氳繃涓涓閮ㄤ腑鏂嵆鍙畬鎴愭墍鏈夋寜閿殑涓柇鏂瑰紡妫娴嬨
+
+杞欢鏂瑰紡锛岄渶瑕佷负姣忎竴涓寜閿厤缃负涓柇瑙﹀彂妯″紡锛岀劧鍚庡湪姣忎竴涓寜閿腑鏂殑涓柇澶勭悊鍑芥暟涓墽琛屾寜閿壂鎻忋
+
+涓轰簡鍦ㄩ檷浣庝腑鏂鐞嗗嚱鏁颁腑鎵ц鎸夐敭鎵弿甯︽潵鐨勬椂寤讹紝鍙互閫氳繃淇″彿閲忕殑鏂瑰紡鏉ュ紓姝ュ鐞嗭紝浠呭湪涓柇澶勭悊鍑芥暟涓噴鏀句竴涓寜閿壂鎻忕殑淇″彿閲忥紝鐒跺悗鍦ㄦ寜閿壂鎻忕嚎绋嬩腑鐩戞祴璇ヤ俊鍙烽噺銆
+
+### 鍏充簬缁勫悎鎸夐敭
+
+璇ユ寜閿簱浠呭仛浜嗗簳灞傜殑鎸夐敭鎵弿澶勭悊锛屼竴娆℃壂鎻忓彲浠ョ‘瀹氭墍鏈夌殑鎸夐敭鐘舵侊紝骞朵笂鎶ュ搴旂殑鎸夐敭浜嬩欢锛屽鏋滈渶瑕佹敮鎸佺粍鍚堟寜閿紝璇峰啀灏佷竴灞傦紝鏍规嵁鎸夐敭搴撹繑鍥炵殑浜嬩欢灏佽闇瑕佺殑缁勫悎鎸夐敭銆俒绀轰緥绋嬪簭](./examples/demo_rtt_iotboard.c)鎻愪緵浜嗙畝鍗曠殑瀹炵幇銆
+
+### 鍏充簬鐭╅樀閿洏
+
+涓嶇浣犵殑鐭╅樀閿洏鏄氳繃浠涔堥氫俊鏂瑰紡鑾峰彇鎸夐敭鐘舵佺殑锛屽彧瑕佷綘灏嗚鍙栨寜閿姸鎬佺殑鍑芥暟瀵规帴鍒 Flexible_button 鏁版嵁缁撴瀯涓殑 `uint8_t (*usr_button_read)(void*);` 鍑芥暟涓婂嵆鍙
+
+> 鍙傝 [issue 2](https://github.com/murphyzhao/FlexibleButton/issues/2) 涓殑璁ㄨ銆
+
+## 闂鍜屽缓璁
+
+濡傛灉鏈変粈涔堥棶棰樻垨鑰呭缓璁杩庢彁浜 [Issue](https://github.com/murphyzhao/FlexibleButton/issues) 杩涜璁ㄨ銆
+
+## 缁存姢
+
+- [MurphyZhao](https://github.com/murphyzhao)
+
+## 鎰熻阿
+
+鎰熻阿鎵鏈変竴璧锋帰璁ㄧ殑鏈嬪弸锛屾劅璋㈡墍鏈変娇鐢 flexible_button 鐨勬湅鍙嬶紝鎰熻阿浣犱滑鐨 Star 鍜 Fork锛岃阿璋綘浠殑鏀寔銆
+
+- 鎰熻阿 [BOBBOM](https://github.com/BOBBOM) 鍙戠幇 flex_button_register 鍑芥暟涓殑閫昏緫闂
+- 鎰熻阿 [BOBBOM](https://github.com/BOBBOM) 瑙i櫎 flexible_button 涓鎸夐敭鏁伴噺鐨勯檺鍒
+- 鎰熻阿 [**rt-thread**](https://mp.weixin.qq.com/s/HJEcSXhykBq1T5Hx0TdjMw) 鐨勬敮鎸
+- 鎰熻阿 [**鐢靛瓙鍙戠儳鍙**](https://mp.weixin.qq.com/s/mQFyrPAvz_TSktQLrSqQfA) 鐨勬敮鎸
+- 鎰熻阿 [**濞侀┌鐢靛瓙**](https://mp.weixin.qq.com/s/oAwFXPostMFBtb2EGxTdig) 鐨勬敮鎸
+
+## 鍙嬫儏閾炬帴
+
+- RT-Thread [IoT Board](https://github.com/RT-Thread/IoT_Board) 寮鍙戞澘
diff --git a/common/FlexibleButton-2.0.1/flexible_button.c b/common/FlexibleButton-2.0.1/flexible_button.c
new file mode 100644
index 0000000..7cbcdd8
--- /dev/null
+++ b/common/FlexibleButton-2.0.1/flexible_button.c
@@ -0,0 +1,317 @@
+/**
+ * @File: flexible_button.c
+ * @Author: MurphyZhao
+ * @Date: 2018-09-29
+ *
+ * Copyright (c) 2018-2019 MurphyZhao
+ * https://github.com/murphyzhao
+ * All rights reserved.
+ * License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Change logs:
+ * Date Author Notes
+ * 2018-09-29 MurphyZhao First add
+ * 2019-08-02 MurphyZhao Migrate code to github.com/murphyzhao account
+ * 2019-12-26 MurphyZhao Refactor code and implement multiple clicks
+ *
+*/
+
+#include "flexible_button.h"
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+#define EVENT_SET_AND_EXEC_CB(btn, evt) \
+ do \
+ { \
+ btn->event = evt; \
+ if(btn->cb) \
+ btn->cb((flex_button_t*)btn); \
+ } while(0)
+
+/**
+ * BTN_IS_PRESSED
+ *
+ * 1: is pressed
+ * 0: is not pressed
+*/
+#define BTN_IS_PRESSED(i) (g_btn_status_reg & (1 << i))
+
+enum FLEX_BTN_STAGE
+{
+ FLEX_BTN_STAGE_DEFAULT = 0,
+ FLEX_BTN_STAGE_DOWN = 1,
+ FLEX_BTN_STAGE_MULTIPLE_CLICK = 2
+};
+
+typedef uint32_t btn_type_t;
+
+static flex_button_t *btn_head = NULL;
+
+/**
+ * g_logic_level
+ *
+ * The logic level of the button pressed,
+ * Each bit represents a button.
+ *
+ * First registered button, the logic level of the button pressed is
+ * at the low bit of g_logic_level.
+*/
+btn_type_t g_logic_level = (btn_type_t)0;
+
+/**
+ * g_btn_status_reg
+ *
+ * The status register of all button, each bit records the pressing state of a button.
+ *
+ * First registered button, the pressing state of the button is
+ * at the low bit of g_btn_status_reg.
+*/
+btn_type_t g_btn_status_reg = (btn_type_t)0;
+
+static uint8_t button_cnt = 0;
+
+/**
+ * @brief Register a user button
+ *
+ * @param button: button structure instance
+ * @return Number of keys that have been registered
+*/
+int8_t flex_button_register(flex_button_t *button)
+{
+ flex_button_t *curr = btn_head;
+
+ if (!button || (button_cnt > sizeof(btn_type_t) * 8))
+ {
+ return -1;
+ }
+
+ while (curr)
+ {
+ if(curr == button)
+ {
+ return -1; /* already exist. */
+ }
+ curr = curr->next;
+ }
+
+ /**
+ * First registered button is at the end of the 'linked list'.
+ * btn_head points to the head of the 'linked list'.
+ */
+ button->next = btn_head;
+ button->status = FLEX_BTN_STAGE_DEFAULT;
+ button->event = FLEX_BTN_PRESS_NONE;
+ button->scan_cnt = 0;
+ button->click_cnt = 0;
+ button->max_multiple_clicks_interval = MAX_MULTIPLE_CLICKS_INTERVAL;
+ btn_head = button;
+
+ /**
+ * First registered button, the logic level of the button pressed is
+ * at the low bit of g_logic_level.
+ */
+ g_logic_level |= (button->pressed_logic_level << button_cnt);
+ button_cnt ++;
+
+ return button_cnt;
+}
+
+/**
+ * @brief Read all key values in one scan cycle
+ *
+ * @param void
+ * @return none
+*/
+static void flex_button_read(void)
+{
+ uint8_t i;
+ flex_button_t* target;
+
+ /* The button that was registered first, the button value is in the low position of raw_data */
+ btn_type_t raw_data = 0;
+
+ for(target = btn_head, i = button_cnt - 1;
+ (target != NULL) && (target->usr_button_read != NULL);
+ target = target->next, i--)
+ {
+ raw_data = raw_data | ((target->usr_button_read)(target) << i);
+ }
+
+ g_btn_status_reg = (~raw_data) ^ g_logic_level;
+}
+
+/**
+ * @brief Handle all key events in one scan cycle.
+ * Must be used after 'flex_button_read' API
+ *
+ * @param void
+ * @return none
+*/
+static void flex_button_process(void)
+{
+ uint8_t i;
+ flex_button_t* target;
+
+ for (target = btn_head, i = 0; target != NULL; target = target->next, i ++)
+ {
+ if (target->status > FLEX_BTN_STAGE_DEFAULT)
+ {
+ target->scan_cnt ++;
+ if (target->scan_cnt >= ((1 << (sizeof(target->scan_cnt) * 8)) - 1))
+ {
+ target->scan_cnt = target->long_hold_start_tick;
+ }
+ }
+
+ switch (target->status)
+ {
+ case FLEX_BTN_STAGE_DEFAULT: /* stage: default(button up) */
+ if (BTN_IS_PRESSED(i)) /* is pressed */
+ {
+ target->scan_cnt = 0;
+ target->click_cnt = 0;
+
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_DOWN);
+
+ /* swtich to button down stage */
+ target->status = FLEX_BTN_STAGE_DOWN;
+ }
+ else
+ {
+ target->event = FLEX_BTN_PRESS_NONE;
+ }
+ break;
+
+ case FLEX_BTN_STAGE_DOWN: /* stage: button down */
+ if (BTN_IS_PRESSED(i)) /* is pressed */
+ {
+ if (target->click_cnt > 0) /* multiple click */
+ {
+ if (target->scan_cnt > target->max_multiple_clicks_interval)
+ {
+ EVENT_SET_AND_EXEC_CB(target,
+ target->click_cnt < FLEX_BTN_PRESS_REPEAT_CLICK ?
+ target->click_cnt :
+ FLEX_BTN_PRESS_REPEAT_CLICK);
+
+ /* swtich to button down stage */
+ target->status = FLEX_BTN_STAGE_DOWN;
+ target->scan_cnt = 0;
+ target->click_cnt = 0;
+ }
+ }
+ else if (target->scan_cnt >= target->long_hold_start_tick)
+ {
+ if (target->event != FLEX_BTN_PRESS_LONG_HOLD)
+ {
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_HOLD);
+ }
+ }
+ else if (target->scan_cnt >= target->long_press_start_tick)
+ {
+ if (target->event != FLEX_BTN_PRESS_LONG_START)
+ {
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_START);
+ }
+ }
+ else if (target->scan_cnt >= target->short_press_start_tick)
+ {
+ if (target->event != FLEX_BTN_PRESS_SHORT_START)
+ {
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_SHORT_START);
+ }
+ }
+ }
+ else /* button up */
+ {
+ if (target->scan_cnt >= target->long_hold_start_tick)
+ {
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_HOLD_UP);
+ target->status = FLEX_BTN_STAGE_DEFAULT;
+ }
+ else if (target->scan_cnt >= target->long_press_start_tick)
+ {
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_LONG_UP);
+ target->status = FLEX_BTN_STAGE_DEFAULT;
+ }
+ else if (target->scan_cnt >= target->short_press_start_tick)
+ {
+ EVENT_SET_AND_EXEC_CB(target, FLEX_BTN_PRESS_SHORT_UP);
+ target->status = FLEX_BTN_STAGE_DEFAULT;
+ }
+ else
+ {
+ /* swtich to multiple click stage */
+ target->status = FLEX_BTN_STAGE_MULTIPLE_CLICK;
+ target->click_cnt ++;
+ }
+ }
+ break;
+
+ case FLEX_BTN_STAGE_MULTIPLE_CLICK: /* stage: multiple click */
+ if (BTN_IS_PRESSED(i)) /* is pressed */
+ {
+ /* swtich to button down stage */
+ target->status = FLEX_BTN_STAGE_DOWN;
+ target->scan_cnt = 0;
+ }
+ else
+ {
+ if (target->scan_cnt > target->max_multiple_clicks_interval)
+ {
+ EVENT_SET_AND_EXEC_CB(target,
+ target->click_cnt < FLEX_BTN_PRESS_REPEAT_CLICK ?
+ target->click_cnt :
+ FLEX_BTN_PRESS_REPEAT_CLICK);
+
+ /* swtich to default stage */
+ target->status = FLEX_BTN_STAGE_DEFAULT;
+ }
+ }
+ break;
+ }
+ }
+}
+
+/**
+ * flex_button_event_read
+ *
+ * @brief Get the button event of the specified button.
+ *
+ * @param button: button structure instance
+ * @return button event
+*/
+flex_button_event_t flex_button_event_read(flex_button_t* button)
+{
+ return (flex_button_event_t)(button->event);
+}
+
+/**
+ * flex_button_scan
+ *
+ * @brief Start key scan.
+ * Need to be called cyclically within the specified period.
+ * Sample cycle: 5 - 20ms
+ *
+ * @param void
+ * @return none
+*/
+void flex_button_scan(void)
+{
+ flex_button_read();
+ flex_button_process();
+}
diff --git a/common/FlexibleButton-2.0.1/flexible_button.h b/common/FlexibleButton-2.0.1/flexible_button.h
new file mode 100644
index 0000000..2456610
--- /dev/null
+++ b/common/FlexibleButton-2.0.1/flexible_button.h
@@ -0,0 +1,164 @@
+/*
+ * @Author : stark1898y 1658608470@qq.com
+ * @Date : 2024-07-04 16:13:57
+ * @LastEditors : stark1898y 1658608470@qq.com
+ * @LastEditTime : 2024-12-14 15:56:11
+ * @FilePath : \BLE_TYQ_CH592F\common\FlexibleButton-2.0.1\flexible_button.h
+ * @Description :
+ *
+ * Copyright (c) 2024 by yzy, All Rights Reserved.
+ */
+/**
+ * @File: flexible_button.h
+ * @Author: MurphyZhao
+ * @Date: 2018-09-29
+ *
+ * Copyright (c) 2018-2019 MurphyZhao
+ * https://github.com/murphyzhao
+ * All rights reserved.
+ * License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Change logs:
+ * Date Author Notes
+ * 2018-09-29 MurphyZhao First add
+ * 2019-08-02 MurphyZhao Migrate code to github.com/murphyzhao account
+ * 2019-12-26 MurphyZhao Refactor code and implement multiple clicks
+ *
+*/
+
+#ifndef __FLEXIBLE_BUTTON_H__
+#define __FLEXIBLE_BUTTON_H__
+
+#include "stdint.h"
+
+#define FLEX_BTN_SCAN_FREQ_HZ 50 // How often flex_button_scan () is called
+#define FLEX_MS_TO_SCAN_CNT(ms) (ms / (1000 / FLEX_BTN_SCAN_FREQ_HZ)) //
+/* Multiple clicks interval, default 300ms */
+#define MAX_MULTIPLE_CLICKS_INTERVAL (FLEX_MS_TO_SCAN_CNT(300))
+
+typedef void (*flex_button_response_callback)(void*);
+
+typedef enum
+{
+ FLEX_BTN_PRESS_DOWN = 0,
+ FLEX_BTN_PRESS_CLICK,
+ FLEX_BTN_PRESS_DOUBLE_CLICK,//
+ FLEX_BTN_PRESS_REPEAT_CLICK,
+ FLEX_BTN_PRESS_SHORT_START,
+ FLEX_BTN_PRESS_SHORT_UP,
+ FLEX_BTN_PRESS_LONG_START,
+ FLEX_BTN_PRESS_LONG_UP,
+ FLEX_BTN_PRESS_LONG_HOLD,// 闀挎寜
+ FLEX_BTN_PRESS_LONG_HOLD_UP,
+ FLEX_BTN_PRESS_MAX,
+ FLEX_BTN_PRESS_NONE,
+} flex_button_event_t;
+
+/**
+ * flex_button_t
+ *
+ * @brief Button data structure
+ * Below are members that need to user init before scan.
+ *
+ * @member next
+ * Internal use.
+ * One-way linked list, pointing to the next button.
+ *
+ * @member usr_button_read
+ * User function is used to read button vaule.
+ *
+ * @member cb
+ * Button event callback function.
+ *
+ * @member scan_cnt
+ * Internal use, user read-only.
+ * Number of scans, counted when the button is pressed, plus one per scan cycle.
+ *
+ * @member click_cnt
+ * Internal use, user read-only.
+ * Number of button clicks
+ *
+ * @member max_multiple_clicks_interval
+ * Multiple click interval. Default 'MAX_MULTIPLE_CLICKS_INTERVAL'.
+ * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
+ *
+ * @member debounce_tick
+ * Debounce. Not used yet.
+ * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
+ *
+ * @member short_press_start_tick
+ * Short press start time. Requires user configuration.
+ * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
+ *
+ * @member long_press_start_tick
+ * Long press start time. Requires user configuration.
+ * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
+ *
+ * @member long_hold_start_tick
+ * Long hold press start time. Requires user configuration.
+ *
+ * @member id
+ * Button id. Requires user configuration.
+ * When multiple buttons use the same button callback function,
+ * they are used to distinguish the buttons.
+ * Each button id must be unique.
+ *
+ * @member pressed_logic_level
+ * Requires user configuration.
+ * The logic level of the button pressed, each bit represents a button.
+ *
+ * @member event
+ * Internal use, users can call 'flex_button_event_read' to get current button event.
+ * Used to record the current button event.
+ *
+ * @member status
+ * Internal use, user unavailable.
+ * Used to record the current state of buttons.
+ *
+*/
+typedef struct flex_button
+{
+ struct flex_button* next;
+
+ uint8_t (*usr_button_read)(void *);
+ flex_button_response_callback cb;
+
+ uint16_t scan_cnt;
+ uint16_t click_cnt;
+ uint16_t max_multiple_clicks_interval;
+
+ uint16_t debounce_tick;
+ uint16_t short_press_start_tick;
+ uint16_t long_press_start_tick;
+ uint16_t long_hold_start_tick;
+
+ uint8_t id;
+ uint8_t pressed_logic_level : 1;
+ uint8_t event : 4;
+ uint8_t status : 3;
+} flex_button_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int8_t flex_button_register(flex_button_t *button);
+flex_button_event_t flex_button_event_read(flex_button_t* button);
+void flex_button_scan(void);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* __FLEXIBLE_BUTTON_H__ */
diff --git a/common/MultiButton/LICENSE b/common/MultiButton/LICENSE
new file mode 100644
index 0000000..1603075
--- /dev/null
+++ b/common/MultiButton/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2018 Zibin Zheng
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/common/MultiButton/README.md b/common/MultiButton/README.md
new file mode 100644
index 0000000..41ee54d
--- /dev/null
+++ b/common/MultiButton/README.md
@@ -0,0 +1,131 @@
+# MultiButton
+
+## 绠浠
+MultiButton 鏄竴涓皬宸х畝鍗曟槗鐢ㄧ殑浜嬩欢椹卞姩鍨嬫寜閿┍鍔ㄦā鍧楋紝鍙棤闄愰噺鎵╁睍鎸夐敭锛屾寜閿簨浠剁殑鍥炶皟寮傛澶勭悊鏂瑰紡鍙互绠鍖栦綘鐨勭▼搴忕粨鏋勶紝鍘婚櫎鍐椾綑鐨勬寜閿鐞嗙‖缂栫爜锛岃浣犵殑鎸夐敭涓氬姟閫昏緫鏇存竻鏅般
+
+## 浣跨敤鏂规硶
+1.鍏堢敵璇蜂竴涓寜閿粨鏋
+
+```c
+struct Button button1;
+```
+2.鍒濆鍖栨寜閿璞★紝缁戝畾鎸夐敭鐨凣PIO鐢靛钩璇诲彇鎺ュ彛**read_button_pin()** 锛屽悗涓涓弬鏁拌缃湁鏁堣Е鍙戠數骞
+
+```c
+button_init(&button1, read_button_pin, 0, 0);
+```
+3.娉ㄥ唽鎸夐敭浜嬩欢
+
+```c
+button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
+button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
+...
+```
+4.鍚姩鎸夐敭
+
+```c
+button_start(&button1);
+```
+5.璁剧疆涓涓5ms闂撮殧鐨勫畾鏃跺櫒寰幆璋冪敤鍚庡彴澶勭悊鍑芥暟
+
+```c
+while(1) {
+ ...
+ if(timer_ticks == 5) {
+ timer_ticks = 0;
+
+ button_ticks();
+ }
+}
+```
+
+## 鐗规
+
+MultiButton 浣跨敤C璇█瀹炵幇锛屽熀浜庨潰鍚戝璞℃柟寮忚璁℃濊矾锛屾瘡涓寜閿璞″崟鐙敤涓浠芥暟鎹粨鏋勭鐞嗭細
+
+```c
+struct Button {
+ uint16_t ticks;
+ uint8_t repeat: 4;
+ uint8_t event : 4;
+ uint8_t state : 3;
+ uint8_t debounce_cnt : 3;
+ uint8_t active_level : 1;
+ uint8_t button_level : 1;
+ uint8_t button_id;
+ uint8_t (*hal_button_Level)(uint8_t button_id_);
+ BtnCallback cb[number_of_event];
+ struct Button* next;
+};
+```
+杩欐牱姣忎釜鎸夐敭浣跨敤鍗曞悜閾捐〃鐩歌繛锛屼緷娆¤繘鍏 button_handler(struct Button* handle) 鐘舵佹満澶勭悊锛屾墍浠ユ瘡涓寜閿殑鐘舵佸郊姝ょ嫭绔嬨
+
+
+## 鎸夐敭浜嬩欢
+
+浜嬩欢 | 璇存槑
+---|---
+PRESS_DOWN | 鎸夐敭鎸変笅锛屾瘡娆℃寜涓嬮兘瑙﹀彂
+PRESS_UP | 鎸夐敭寮硅捣锛屾瘡娆℃澗寮閮借Е鍙
+PRESS_REPEAT | 閲嶅鎸変笅瑙﹀彂锛屽彉閲弐epeat璁℃暟杩炲嚮娆℃暟
+SINGLE_CLICK | 鍗曞嚮鎸夐敭浜嬩欢
+DOUBLE_CLICK | 鍙屽嚮鎸夐敭浜嬩欢
+LONG_PRESS_START | 杈惧埌闀挎寜鏃堕棿闃堝兼椂瑙﹀彂涓娆
+LONG_PRESS_HOLD | 闀挎寜鏈熼棿涓鐩磋Е鍙
+
+
+## Examples
+
+```c
+#include "button.h"
+
+unit8_t btn1_id = 0;
+
+struct Button btn1;
+
+uint8_t read_button_GPIO(uint8_t button_id)
+{
+ // you can share the GPIO read function with multiple Buttons
+ switch(button_id)
+ {
+ case btn1_id:
+ return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
+ break;
+
+ default:
+ return 0;
+ break;
+ }
+}
+void BTN1_PRESS_DOWN_Handler(void* btn)
+{
+ //do something...
+}
+
+void BTN1_PRESS_UP_Handler(void* btn)
+{
+ //do something...
+}
+
+...
+
+int main()
+{
+ button_init(&btn1, read_button_GPIO, 0, btn1_id);
+ button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
+ button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
+ button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
+ button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
+ button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
+ button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
+ button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
+ button_start(&btn1);
+
+ //make the timer invoking the button_ticks() interval 5ms.
+ //This function is implemented by yourself.
+ __timer_start(button_ticks, 0, 5);
+
+ while(1)
+ {}
+}
+```
diff --git a/common/MultiButton/multi_button.c b/common/MultiButton/multi_button.c
new file mode 100644
index 0000000..66d1020
--- /dev/null
+++ b/common/MultiButton/multi_button.c
@@ -0,0 +1,208 @@
+/*
+ * Copyright (c) 2016 Zibin Zheng
+ * All rights reserved
+ */
+
+#include "multi_button.h"
+
+#define EVENT_CB(ev) if(handle->cb[ev])handle->cb[ev]((void*)handle)
+#define PRESS_REPEAT_MAX_NUM 15 /*!< The maximum value of the repeat counter */
+
+//button handle list head.
+static struct Button* head_handle = NULL;
+
+static void button_handler(struct Button* handle);
+
+/**
+ * @brief Initializes the button struct handle.
+ * @param handle: the button handle struct.
+ * @param pin_level: read the HAL GPIO of the connected button level.
+ * @param active_level: pressed GPIO level.
+ * @param button_id: the button id.
+ * @retval None
+ */
+void button_init(struct Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id)
+{
+ memset(handle, 0, sizeof(struct Button));
+ handle->event = (uint8_t)NONE_PRESS;
+ handle->hal_button_Level = pin_level;
+ handle->button_level = !active_level;
+ handle->active_level = active_level;
+ handle->button_id = button_id;
+}
+
+/**
+ * @brief Attach the button event callback function.
+ * @param handle: the button handle struct.
+ * @param event: trigger event type.
+ * @param cb: callback function.
+ * @retval None
+ */
+void button_attach(struct Button* handle, PressEvent event, BtnCallback cb)
+{
+ handle->cb[event] = cb;
+}
+
+/**
+ * @brief Inquire the button event happen.
+ * @param handle: the button handle struct.
+ * @retval button event.
+ */
+PressEvent get_button_event(struct Button* handle)
+{
+ return (PressEvent)(handle->event);
+}
+
+/**
+ * @brief Button driver core function, driver state machine.
+ * @param handle: the button handle struct.
+ * @retval None
+ */
+static void button_handler(struct Button* handle)
+{
+ uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id);
+
+ //ticks counter working..
+ if((handle->state) > 0) handle->ticks++;
+
+ /*------------button debounce handle---------------*/
+ if(read_gpio_level != handle->button_level) { //not equal to prev one
+ //continue read 3 times same new level change
+ if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) {
+ handle->button_level = read_gpio_level;
+ handle->debounce_cnt = 0;
+ }
+ } else { //level not change ,counter reset.
+ handle->debounce_cnt = 0;
+ }
+
+ /*-----------------State machine-------------------*/
+ switch (handle->state) {
+ case 0:
+ if(handle->button_level == handle->active_level) { //start press down
+ handle->event = (uint8_t)PRESS_DOWN;
+ EVENT_CB(PRESS_DOWN);
+ handle->ticks = 0;
+ handle->repeat = 1;
+ handle->state = 1;
+ } else {
+ handle->event = (uint8_t)NONE_PRESS;
+ }
+ break;
+
+ case 1:
+ if(handle->button_level != handle->active_level) { //released press up
+ handle->event = (uint8_t)PRESS_UP;
+ EVENT_CB(PRESS_UP);
+ handle->ticks = 0;
+ handle->state = 2;
+ } else if(handle->ticks > LONG_TICKS) {
+ handle->event = (uint8_t)LONG_PRESS_START;
+ EVENT_CB(LONG_PRESS_START);
+ handle->state = 5;
+ }
+ break;
+
+ case 2:
+ if(handle->button_level == handle->active_level) { //press down again
+ handle->event = (uint8_t)PRESS_DOWN;
+ EVENT_CB(PRESS_DOWN);
+ if(handle->repeat != PRESS_REPEAT_MAX_NUM) {
+ handle->repeat++;
+ }
+ EVENT_CB(PRESS_REPEAT); // repeat hit
+ handle->ticks = 0;
+ handle->state = 3;
+ } else if(handle->ticks > SHORT_TICKS) { //released timeout
+ if(handle->repeat == 1) {
+ handle->event = (uint8_t)SINGLE_CLICK;
+ EVENT_CB(SINGLE_CLICK);
+ } else if(handle->repeat == 2) {
+ handle->event = (uint8_t)DOUBLE_CLICK;
+ EVENT_CB(DOUBLE_CLICK); // repeat hit
+ }
+ handle->state = 0;
+ }
+ break;
+
+ case 3:
+ if(handle->button_level != handle->active_level) { //released press up
+ handle->event = (uint8_t)PRESS_UP;
+ EVENT_CB(PRESS_UP);
+ if(handle->ticks < SHORT_TICKS) {
+ handle->ticks = 0;
+ handle->state = 2; //repeat press
+ } else {
+ handle->state = 0;
+ }
+ } else if(handle->ticks > SHORT_TICKS) { // SHORT_TICKS < press down hold time < LONG_TICKS
+ handle->state = 1;
+ }
+ break;
+
+ case 5:
+ if(handle->button_level == handle->active_level) {
+ //continue hold trigger
+ handle->event = (uint8_t)LONG_PRESS_HOLD;
+ EVENT_CB(LONG_PRESS_HOLD);
+ } else { //released
+ handle->event = (uint8_t)PRESS_UP;
+ EVENT_CB(PRESS_UP);
+ handle->state = 0; //reset
+ }
+ break;
+ default:
+ handle->state = 0; //reset
+ break;
+ }
+}
+
+/**
+ * @brief Start the button work, add the handle into work list.
+ * @param handle: target handle struct.
+ * @retval 0: succeed. -1: already exist.
+ */
+int button_start(struct Button* handle)
+{
+ struct Button* target = head_handle;
+ while(target) {
+ if(target == handle) return -1; //already exist.
+ target = target->next;
+ }
+ handle->next = head_handle;
+ head_handle = handle;
+ return 0;
+}
+
+/**
+ * @brief Stop the button work, remove the handle off work list.
+ * @param handle: target handle struct.
+ * @retval None
+ */
+void button_stop(struct Button* handle)
+{
+ struct Button** curr;
+ for(curr = &head_handle; *curr; ) {
+ struct Button* entry = *curr;
+ if(entry == handle) {
+ *curr = entry->next;
+// free(entry);
+ return;//glacier add 2021-8-18
+ } else {
+ curr = &entry->next;
+ }
+ }
+}
+
+/**
+ * @brief background ticks, timer repeat invoking interval 5ms.
+ * @param None.
+ * @retval None
+ */
+void button_ticks(void)
+{
+ struct Button* target;
+ for(target=head_handle; target; target=target->next) {
+ button_handler(target);
+ }
+}
diff --git a/common/MultiButton/multi_button.h b/common/MultiButton/multi_button.h
new file mode 100644
index 0000000..839e903
--- /dev/null
+++ b/common/MultiButton/multi_button.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2016 Zibin Zheng
+ * All rights reserved
+ */
+
+#ifndef _MULTI_BUTTON_H_
+#define _MULTI_BUTTON_H_
+
+#include
+#include
+
+//According to your need to modify the constants.
+#define TICKS_INTERVAL 5 //ms
+#define DEBOUNCE_TICKS 3 //MAX 7 (0 ~ 7)
+#define SHORT_TICKS (400 /TICKS_INTERVAL)
+#define LONG_TICKS (1500 /TICKS_INTERVAL)
+
+
+typedef void (*BtnCallback)(void*);
+
+typedef enum {
+ PRESS_DOWN = 0,
+ PRESS_UP,
+ PRESS_REPEAT,
+ SINGLE_CLICK,
+ DOUBLE_CLICK,
+ LONG_PRESS_START,
+ LONG_PRESS_HOLD,
+ number_of_event,
+ NONE_PRESS
+}PressEvent;
+
+typedef struct Button {
+ uint16_t ticks;
+ uint8_t repeat : 4;
+ uint8_t event : 4;
+ uint8_t state : 3;
+ uint8_t debounce_cnt : 3;
+ uint8_t active_level : 1;
+ uint8_t button_level : 1;
+ uint8_t button_id;
+ uint8_t (*hal_button_Level)(uint8_t button_id_);
+ BtnCallback cb[number_of_event];
+ struct Button* next;
+}Button;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void button_init(struct Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id);
+void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
+PressEvent get_button_event(struct Button* handle);
+int button_start(struct Button* handle);
+void button_stop(struct Button* handle);
+void button_ticks(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
From 76de810741be736a2b6d0bd3e8037de15971a4e0 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 16:52:59 +0800
Subject: [PATCH 14/16] =?UTF-8?q?=E6=9A=82=E5=AD=98=EF=BC=8C=E6=94=B9per.c?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
APP/peripheral.c | 10 +++++-----
APP/peripheral_main.c | 41 +++++++++++++++++++++++++++++++++--------
2 files changed, 38 insertions(+), 13 deletions(-)
diff --git a/APP/peripheral.c b/APP/peripheral.c
index 01cafbc..a4e8671 100644
--- a/APP/peripheral.c
+++ b/APP/peripheral.c
@@ -43,16 +43,16 @@
*/
// How often to perform periodic event
-#define SBP_PERIODIC_EVT_PERIOD 1600
+#define SBP_PERIODIC_EVT_PERIOD (160 * 10) // (160 = 100ms)
// How often to perform read rssi event
-#define SBP_READ_RSSI_EVT_PERIOD 3200
+#define SBP_READ_RSSI_EVT_PERIOD (1600 * 3) // (160 = 100ms)
// Parameter update delay
-#define SBP_PARAM_UPDATE_DELAY 6400
+#define SBP_PARAM_UPDATE_DELAY (1600 * 4)
// PHY update delay
-#define SBP_PHY_UPDATE_DELAY 2400
+#define SBP_PHY_UPDATE_DELAY (1600 * 2)
// What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
#define DEFAULT_ADVERTISING_INTERVAL (160 * 1)
@@ -62,7 +62,7 @@
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
// Minimum connection interval (units of 1.25ms, 6=7.5ms)
-#define DEFAULT_DESIRED_MIN_CONN_INTERVAL (80 * 5)
+#define DEFAULT_DESIRED_MIN_CONN_INTERVAL (80 * 5) // (80 = 100ms)
// Maximum connection interval (units of 1.25ms, 100=125ms)
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL (80 * 10)
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index dfe5ece..00294cb 100644
--- a/APP/peripheral_main.c
+++ b/APP/peripheral_main.c
@@ -10,28 +10,32 @@
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
-/******************************************************************************/
-/* 澶存枃浠跺寘鍚 */
#include "CONFIG.h"
#include "HAL.h"
#include "gattprofile.h"
#include "peripheral.h"
-#include "bsp_flash.h"
#include "bsp_adc.h"
#include "bsp_key.h"
#include "bsp_beep_led_emv.h"
-#include "bsp_i2c.h"
-
#include "log.h"
#include "bsp_uart.h"
+#include "bsp_i2c.h"
+
+#include "bsp_iwdg.h"
+#include "bsp_flash.h"
+
+
#undef LOG_ENABLE
#define LOG_ENABLE 1
+#undef LOG_TAG
+#define LOG_TAG "main"
+
__attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
@@ -71,8 +75,10 @@ __attribute__((noinline)) void Main_Circulation()
while (1)
{
TMOS_SystemProcess();
+ KEY_ProcessLoop();
BSP_UART1_TxLoop();
- if (GPIOB_ReadPortPin(GPIO_Pin_15) == 0)
+#if 0
+ if (GPIOB_ReadPortPin(GPIO_Pin_7) == 0)
{
// 12V_EN
BOOST_EN;
@@ -113,7 +119,6 @@ __attribute__((noinline)) void Main_Circulation()
DelayMs(1000);
logDebug("LED_ALL_OFF_DEINIT\n");
-
logDebug("Delay 6s\n");
// EMV_CHARGE
@@ -136,9 +141,24 @@ __attribute__((noinline)) void Main_Circulation()
EMV_CHARGE_OFF_DEINIT;
EMV_OFF_DEINIT;
}
+#endif
}
}
+// https://www.cnblogs.com/gscw/p/18530905
+// 鍦ㄨ繛鎺ラ棿闅斿拰骞挎挱闂撮殧鍒版潵鏃讹紝浼氳繘鍏ヨ鍥炶皟锛屽彲浠ュ湪鍥炶皟鍑芥暟鍔犱笂鍏朵粬鎵ц閫昏緫浠g爜锛屽鍠傜嫍绛夈
+void BLE_AdvertiseEventCB(uint32_t timeUs)
+{
+ // FEED_IWDG();
+ logDebug("BLE_AdvertiseEventCB");
+}
+
+void BLE_ConnectEventCB(uint32_t timeUs)
+{
+ // FEED_IWDG();
+ logDebug("BLE_ConnectEventCB");
+}
+
/*********************************************************************
* @fn main
*
@@ -166,7 +186,6 @@ int main(void)
// 鏀规垚1500000娉㈢壒鐜囦簡
BSP_UART1_Init(1500000);
- // logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logError("涓枃娴嬭瘯 %2.2f", 123.456);
@@ -186,12 +205,16 @@ int main(void)
logDebug("%s\n", VER_LIB);
uint8_t MacAddr[6];
GetMACAddress(MacAddr);
+ // 鎵嬫満APP鏄剧ず鏄掑簭鐨刐5-0],涓嶆槸[0-5]
logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]);
HAL_Init();
GAPRole_PeripheralInit();
Peripheral_Init();
+ // 鍦ㄨ繛鎺ラ棿闅斿拰骞挎挱闂撮殧鍒版潵鏃讹紝浼氳繘鍏ュ洖璋冧腑鍠傜嫍
+ // LL_AdvertiseEventRegister(BLE_AdvertiseEventCB);
+ // LL_ConnectEventRegister(BLE_ConnectEventCB);
BSP_FLASH_Init();
@@ -200,6 +223,8 @@ int main(void)
BSP_KEY_Init(app_task_handler);
+ // IWDG_Init(1500);
+
Main_Circulation();
}
From c23b7e1804dd0eca9d5cfda031d7cb0efbe88b8d Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 17:16:36 +0800
Subject: [PATCH 15/16] =?UTF-8?q?=E6=9A=82=E5=AD=98=EF=BC=8C=E7=9C=8B?=
=?UTF-8?q?=E8=B5=B7=E6=9D=A5ok'?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
APP/peripheral.c | 199 +++++++++++++---------------------
BSP/src/bsp_uart.c | 1 +
HAL/include/CONFIG.h | 2 +-
Profile/include/gattprofile.h | 10 +-
4 files changed, 80 insertions(+), 132 deletions(-)
diff --git a/APP/peripheral.c b/APP/peripheral.c
index a4e8671..6952739 100644
--- a/APP/peripheral.c
+++ b/APP/peripheral.c
@@ -43,7 +43,7 @@
*/
// How often to perform periodic event
-#define SBP_PERIODIC_EVT_PERIOD (160 * 10) // (160 = 100ms)
+#define SBP_PERIODIC_EVT_PERIOD (160 * 10 * 5) // (160 = 100ms)
// How often to perform read rssi event
#define SBP_READ_RSSI_EVT_PERIOD (1600 * 3) // (160 = 100ms)
@@ -76,27 +76,13 @@
// Company Identifier: WCH
#define WCH_COMPANY_ID 0x07D7
-/*********************************************************************
- * TYPEDEFS
- */
+#define MAC_NAME "TYQ-93:B4:8F:10:53:5C"
+// TYQ-93:B4:8F:10:53:5C
+#define MAC_NAME_LEN 22
-/*********************************************************************
- * GLOBAL VARIABLES
- */
-
-/*********************************************************************
- * EXTERNAL VARIABLES
- */
-
-/*********************************************************************
- * EXTERNAL FUNCTIONS
- */
-
-/*********************************************************************
- * LOCAL VARIABLES
- */
static uint8_t Peripheral_TaskID = INVALID_TASK_ID; // Task ID for internal task/event processing
+// 蓝牙广播包的最大长度是37个字节,其中设备地址占用了6个字节,只有31个字节是可用的。
// TODO:响应体,名称加上MAC地址
// GAP - SCAN RSP data (max size = 31 bytes)
static uint8_t scanRspData[] = {
@@ -152,72 +138,6 @@ static uint8_t advertData[] = {
HI_UINT16(SIMPLEPROFILE_SERV_UUID)
};
-#if 0
-// 蓝牙广播包的最大长度是37个字节,其中设备地址占用了6个字节,只有31个字节是可用的。
-
-// GAP - SCAN RSP data (max size = 31 bytes)
-static uint8_t scanRspData[] = {
- // complete name
- 0x08, // length of this data
- GAP_ADTYPE_LOCAL_NAME_COMPLETE,
- 'B',
- 'L',
- 'E',
- '-',
- 'T',
- 'Y',
- 'Q',
-
- // connection interval range
- 0x05, // length of this data
- GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
- LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL), // 100ms
- HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
- LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL), // 1s
- HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
-
- // Tx power level
- 0x02, // length of this data
- GAP_ADTYPE_POWER_LEVEL,
- 0 // 0dBm
-
- 0x03, // length of this data
- GAP_ADTYPE_MANUFACTURER_SPECIFIC,
- // 前两个字节表示厂商 ID
- 0,
- 0,
- 0x10, // 1B 电池电压 30=3V,18=1.8V
- 0x01, // 1B 阀门类型
- 0x00, // 1B 阀门工作状态 超欠压、过流
- 0x00, // 1B 阀门连接状态 (电磁阀BLE控制盒) 0=未连接 1=已连接
- 0x01, // 1B 阀门开关状态 0=未知 1=关闭 2=关闭过,未按按键恢复 3=关闭过,按下了恢复键 4=打开
- 0x03, // 1B 阀门温度 有符号整数 25 C
- 0x04, // 1B 阀门进口压力 0~156 (0)kPa
- 0x05, // 1B 阀门出口压力 0~100 hPa 0.1kpa
- 0x06, // 1B 阀门大气压力 80~110 kPa
-};
-
-
-// GAP - Advertisement data (max size = 31 bytes, though this is
-// best kept short to conserve power while advertising)
-static uint8_t advertData[] = {
- // Flags; this sets the device to use limited discoverable
- // mode (advertises for 30 seconds at a time) instead of general
- // discoverable mode (advertises indefinitely)
- 0x02, // length of this data
- GAP_ADTYPE_FLAGS,
- DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
-
- // service UUID, to notify central devices what services are included
- // in this peripheral
- 0x03, // length of this data
- GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
- LO_UINT16(SIMPLEPROFILE_SERV_UUID),
- HI_UINT16(SIMPLEPROFILE_SERV_UUID)
-};
-
-#endif
-
// GAP GATT Attributes
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "BLE_TYQ";
@@ -301,6 +221,7 @@ void Peripheral_Init()
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
+
GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval);
GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval);
}
@@ -347,11 +268,11 @@ void Peripheral_Init()
// Setup the SimpleProfile Characteristic Values
{
- uint8_t charValue1[SIMPLEPROFILE_CHAR1_LEN] = {1};
- uint8_t charValue2[SIMPLEPROFILE_CHAR2_LEN] = {2};
- uint8_t charValue3[SIMPLEPROFILE_CHAR3_LEN] = {3};
- uint8_t charValue4[SIMPLEPROFILE_CHAR4_LEN] = {4};
- uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = {1, 2, 3, 4, 5};
+ uint8_t charValue1[SIMPLEPROFILE_CHAR1_LEN] = {0};
+ uint8_t charValue2[SIMPLEPROFILE_CHAR2_LEN] = {0};
+ uint8_t charValue3[SIMPLEPROFILE_CHAR3_LEN] = {0};
+ uint8_t charValue4[SIMPLEPROFILE_CHAR4_LEN] = {0};
+ uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = {0, 0, 0, 0, 0};
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR1, SIMPLEPROFILE_CHAR1_LEN, charValue1);
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR2, SIMPLEPROFILE_CHAR2_LEN, charValue2);
@@ -456,7 +377,7 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
if(events & SBP_PHY_UPDATE_EVT)
{
// start phy update
- logDebug("PHY Update %x...\n", GAPRole_UpdatePHY(peripheralConnList.connHandle, 0,
+ logDebug("PHY Update %x...", GAPRole_UpdatePHY(peripheralConnList.connHandle, 0,
GAP_PHY_BIT_LE_2M, GAP_PHY_BIT_LE_2M, 0));
return (events ^ SBP_PHY_UPDATE_EVT);
@@ -488,7 +409,7 @@ static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent)
{
case GAP_SCAN_REQUEST_EVENT:
{
- logDebug("Receive scan req from %x %x %x %x %x %x ..\n", pEvent->scanReqEvt.scannerAddr[0],
+ logDebug("Receive scan req from %x %x %x %x %x %x ..", pEvent->scanReqEvt.scannerAddr[0],
pEvent->scanReqEvt.scannerAddr[1], pEvent->scanReqEvt.scannerAddr[2], pEvent->scanReqEvt.scannerAddr[3],
pEvent->scanReqEvt.scannerAddr[4], pEvent->scanReqEvt.scannerAddr[5]);
break;
@@ -496,7 +417,7 @@ static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent)
case GAP_PHY_UPDATE_EVENT:
{
- logDebug("Phy update Rx:%x Tx:%x ..\n", pEvent->linkPhyUpdate.connRxPHYS, pEvent->linkPhyUpdate.connTxPHYS);
+ logDebug("Phy update Rx:%x Tx:%x ..", pEvent->linkPhyUpdate.connRxPHYS, pEvent->linkPhyUpdate.connTxPHYS);
break;
}
@@ -532,7 +453,7 @@ static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
if(pMsgEvent->method == ATT_MTU_UPDATED_EVENT)
{
peripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU;
- logDebug("mtu exchange: %d\n", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
+ logDebug("mtu exchange: %d", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
}
break;
}
@@ -559,7 +480,7 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
if(peripheralConnList.connHandle != GAP_CONNHANDLE_INIT)
{
GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
- logDebug("Connection max...\n");
+ logDebug("Connection max...");
}
else
{
@@ -577,7 +498,7 @@ static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
// Start read rssi
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
- logDebug("Conn %x - Int %x \n", event->connectionHandle, event->connInterval);
+ logDebug("Conn %x - Int %x ", event->connectionHandle, event->connInterval);
}
}
@@ -605,13 +526,17 @@ static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
// Restart advertising
{
+ // 三个广播通道全部打开
+ // uint8_t init_adv_channel_map = GAP_ADVCHAN_ALL ;
+ // GAPRole_SetParameter( GAPROLE_ADV_CHANNEL_MAP, sizeof( uint8_t ), &init_adv_channel_map);
+
uint8_t advertising_enable = TRUE;
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertising_enable);
}
}
else
{
- logDebug("ERR..\n");
+ logError("ERR..");
}
}
@@ -627,7 +552,7 @@ static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
*/
static void peripheralRssiCB(uint16_t connHandle, int8_t rssi)
{
- logDebug("RSSI -%d dB Conn %x \n", -rssi, connHandle);
+ logDebug("RSSI -%d dB Conn %x ", -rssi, connHandle);
}
/*********************************************************************
@@ -656,7 +581,7 @@ static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
}
else
{
- logDebug("ERR..\n");
+ logError("ERR..");
}
}
@@ -674,19 +599,19 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
switch(newState & GAPROLE_STATE_ADV_MASK)
{
case GAPROLE_STARTED:
- logDebug("Initialized..\n");
+ logDebug("Initialized..");
break;
case GAPROLE_ADVERTISING:
if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
{
Peripheral_LinkTerminated(pEvent);
- logDebug("Disconnected.. Reason:%x\n", pEvent->linkTerminate.reason);
- logDebug("Advertising..\n");
+ logDebug("Disconnected.. Reason:%x", pEvent->linkTerminate.reason);
+ logDebug("Advertising..");
}
else if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
{
- logDebug("Advertising..\n");
+ logDebug("Advertising..");
}
break;
@@ -694,46 +619,46 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven
if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
{
Peripheral_LinkEstablished(pEvent);
- logDebug("Connected..\n");
+ logDebug("Connected..");
}
break;
case GAPROLE_CONNECTED_ADV:
if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
{
- logDebug("Connected Advertising..\n");
+ logDebug("Connected Advertising..");
}
break;
case GAPROLE_WAITING:
if(pEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT)
{
- logDebug("Waiting for advertising..\n");
+ logDebug("Waiting for advertising..");
}
else if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
{
Peripheral_LinkTerminated(pEvent);
- logDebug("Disconnected.. Reason:%x\n", pEvent->linkTerminate.reason);
+ logDebug("Disconnected.. Reason:%x", pEvent->linkTerminate.reason);
}
else if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
{
if(pEvent->gap.hdr.status != SUCCESS)
{
- logDebug("Waiting for advertising..\n");
+ logDebug("Waiting for advertising..");
}
else
{
- logDebug("Error..\n");
+ logError("Err..");
}
}
else
{
- logDebug("Error..%x\n", pEvent->gap.opcode);
+ logError("Err..%x", pEvent->gap.opcode);
}
break;
case GAPROLE_ERROR:
- logDebug("Error..\n");
+ logError("Err..");
break;
default:
@@ -836,7 +761,7 @@ static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
attHandleValueNoti_t noti;
if(len > (peripheralMTU - 3))
{
- logDebug("Too large noti\n");
+ logDebug("Too large noti");
return;
}
noti.len = len;
@@ -851,9 +776,6 @@ static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
}
}
-
-uint8_t newValue[SIMPLEPROFILE_CHAR3_LEN];
-
/*********************************************************************
* @fn simpleProfileChangeCB
*
@@ -867,6 +789,7 @@ uint8_t newValue[SIMPLEPROFILE_CHAR3_LEN];
*/
static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len)
{
+ // tmos_event_hdr_t *test_message;
TsRawFrameData RawData;
switch(paramID)
{
@@ -874,30 +797,37 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
{
uint8_t newValue[SIMPLEPROFILE_CHAR1_LEN];
tmos_memcpy(newValue, pValue, len);
- logDebug("profile ChangeCB CHAR1.. Start\n");
+ logDebug("profile ChangeCB CHAR1.. Start");
for (uint8_t i = 0; i < len; i++)
{
logDebug("%02x ", newValue[i]);
+ BSP_UART1_TxLoop();
}
- logDebug("\n profile ChangeCB CHAR1.. End\n");
+ logDebug("\n profile ChangeCB CHAR1.. End");
+
+ // test_message =(tmos_event_hdr_t*) tmos_msg_allocate(sizeof(tmos_event_hdr_t));
+ BSP_UART1_TxLoop();
+
break;
}
case SIMPLEPROFILE_CHAR3:
{
+ uint8_t newValue[SIMPLEPROFILE_CHAR3_LEN];
tmos_memset(newValue, 0, sizeof(newValue));
tmos_memcpy(newValue, pValue, len);
- logDebug("profile ChangeCB CHAR3.. Start");
- // logHexDumpAll(newValue, len);
- for (uint8_t i = 0; i < len; i++)
- {
- logDebug("%02x ", newValue[i]);
- }
+ logDebug("CHAR3 Start");
+ logHexDumpAll(newValue, len);
+ // for (uint8_t i = 0; i < len; i++)
+ // {
+ // logDebug("%02x ", newValue[i]);
+ // }
#if 1
TsFrameData *HostFrameData = BSP_VAVLE_GetFrameData(newValue, len);
if (HostFrameData != NULL)
{
- logHexDumpAll(&HostFrameData->data[0], HostFrameData->len); // 数据段
+ // logHexDumpAll(&HostFrameData->data[0], HostFrameData->len); // 数据段
+ // BSP_UART1_TxLoop();
// HR_ProcessData(HostFrameData);
switch (HostFrameData->cmd)
{
@@ -911,11 +841,13 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
logDebug("kCmdCloseVavle");
BSP_VALVE_Generate_ValveResponse(&RawData, kCmdCloseVavle, 1);
peripheralChar4Notify((uint8_t*)&RawData.buf[0], RawData.len);
+ BSP_UART1_TxLoop();
break;
case kCmdOpenVavle:
logDebug("kCmdOpenVavle");
BSP_VALVE_Generate_ValveResponse(&RawData, kCmdOpenVavle, 1);
peripheralChar4Notify((uint8_t*)&RawData.buf[0], RawData.len);
+ BSP_UART1_TxLoop();
break;
default:
logError("无效的命令");
@@ -923,6 +855,10 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
break;
}
}
+ else
+ {
+ logError("数据帧解析失败");
+ }
tmos_msg_deallocate((uint8_t*)HostFrameData);
HostFrameData = NULL;
#endif
@@ -936,5 +872,16 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
}
}
-/*********************************************************************
-*********************************************************************/
+// TODO:应用加入广播数据更新部分状态数据
+// 动态更新广播数据
+// taskID: 请求更改广播任务的 ID
+// adType:TRUE 更改广播数据,FALSE 更改扫描回复数据
+// datalen:数据长度
+// *pAdvertData:数据指针
+void Peripheral_UpdateAdvertData(uint8_t adType, uint8_t dataLen, uint8_t *pAdvertData)
+{
+ GAP_UpdateAdvertisingData(Peripheral_TaskID, adType, dataLen, pAdvertData);
+}
+
+
+
diff --git a/BSP/src/bsp_uart.c b/BSP/src/bsp_uart.c
index 283cd9e..8534726 100644
--- a/BSP/src/bsp_uart.c
+++ b/BSP/src/bsp_uart.c
@@ -3,6 +3,7 @@
#include "CH59x_uart.h"
#include "shell_port.h"
+// https://www.cnblogs.com/iot-fan/p/14744671.html
lwrb_t Uart1Rx;
diff --git a/HAL/include/CONFIG.h b/HAL/include/CONFIG.h
index 4d10b1d..a633c3f 100644
--- a/HAL/include/CONFIG.h
+++ b/HAL/include/CONFIG.h
@@ -142,7 +142,7 @@
#define PERIPHERAL_MAX_CONNECTION 1
#endif
#ifndef CENTRAL_MAX_CONNECTION
-#define CENTRAL_MAX_CONNECTION 3
+#define CENTRAL_MAX_CONNECTION 2
#endif
extern uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
diff --git a/Profile/include/gattprofile.h b/Profile/include/gattprofile.h
index 29b9340..163cdee 100644
--- a/Profile/include/gattprofile.h
+++ b/Profile/include/gattprofile.h
@@ -46,11 +46,11 @@ extern "C" {
#define SIMPLEPROFILE_SERVICE 0x00000001
// Length of characteristic in bytes ( Default MTU is 23 )
-#define SIMPLEPROFILE_CHAR1_LEN 30
-#define SIMPLEPROFILE_CHAR2_LEN 30
-#define SIMPLEPROFILE_CHAR3_LEN 30
-#define SIMPLEPROFILE_CHAR4_LEN 30
-#define SIMPLEPROFILE_CHAR5_LEN 30
+#define SIMPLEPROFILE_CHAR1_LEN 64
+#define SIMPLEPROFILE_CHAR2_LEN 64
+#define SIMPLEPROFILE_CHAR3_LEN 64
+#define SIMPLEPROFILE_CHAR4_LEN 64
+#define SIMPLEPROFILE_CHAR5_LEN 64
/*********************************************************************
* TYPEDEFS
From 7fc07d561d41de7e6870696514676439b81aeb36 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Sun, 15 Dec 2024 17:19:26 +0800
Subject: [PATCH 16/16] =?UTF-8?q?ok,=E5=9B=9E=E5=8E=BBmain?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
APP/peripheral.c | 2 +-
HAL/MCU.c | 10 +++++-----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/APP/peripheral.c b/APP/peripheral.c
index 6952739..47b8e37 100644
--- a/APP/peripheral.c
+++ b/APP/peripheral.c
@@ -55,7 +55,7 @@
#define SBP_PHY_UPDATE_DELAY (1600 * 2)
// What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
-#define DEFAULT_ADVERTISING_INTERVAL (160 * 1)
+#define DEFAULT_ADVERTISING_INTERVAL (160 * 10)
// Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely
diff --git a/HAL/MCU.c b/HAL/MCU.c
index 612d895..c7cae20 100644
--- a/HAL/MCU.c
+++ b/HAL/MCU.c
@@ -156,11 +156,11 @@ void CH59x_BLEInit(void)
{
uint8_t MacAddr[6];
GetMACAddress(MacAddr);
- for(i = 0; i < 6; i++)
- {
- cfg.MacAddr[i] = MacAddr[i]; // 使用芯片mac地址
- PRINT("MacAddr[%d]: %x\n", i, MacAddr[i]);
- }
+ // for(i = 0; i < 6; i++)
+ // {
+ // cfg.MacAddr[i] = MacAddr[i]; // 使用芯片mac地址
+ // PRINT("MacAddr[%d]: %x\n", i, MacAddr[i]);
+ // }
}
#endif
if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024)