From 6e9d7cd7ea52fddf7f5e0d661958190414ada209 Mon Sep 17 00:00:00 2001
From: stark1898y <1658608470@qq.com>
Date: Fri, 13 Dec 2024 13:22:31 +0800
Subject: [PATCH] =?UTF-8?q?log=E6=89=93=E5=8D=B0=E6=97=B6=E9=97=B4?=
=?UTF-8?q?=E6=88=B3TMOS=5FGetSystemClock?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.cproject | 8 ++-
.settings/org.eclipse.core.resources.prefs | 1 +
APP/peripheral.c | 71 ++++++++++++----------
APP/peripheral_main.c | 17 ++++--
BSP/inc/bsp_uart.h | 4 +-
BSP/src/bsp_adc.c | 38 +++++++-----
BSP/src/bsp_uart.c | 1 +
BSP/src/bsp_valve.c | 25 +++++---
common/letter-shell/extensions/log/log.h | 2 +-
common/letter-shell/shell_cfg.h | 2 +-
common/letter-shell/shell_port.h | 2 +-
11 files changed, 100 insertions(+), 71 deletions(-)
diff --git a/.cproject b/.cproject
index 328c437..71b9ccf 100644
--- a/.cproject
+++ b/.cproject
@@ -45,7 +45,7 @@
-
+
@@ -90,7 +90,7 @@
-
+
@@ -105,6 +105,8 @@
+
+
@@ -172,5 +174,5 @@
-
+
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
index 00dd478..126341d 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.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.c b/APP/peripheral.c
index 72717a8..d4282b7 100644
--- a/APP/peripheral.c
+++ b/APP/peripheral.c
@@ -3,8 +3,8 @@
* Author : WCH
* Version : V1.0
* Date : 2018/12/10
- * Description : ӻӦóʼ㲥ӲȻ㲥
- * ӲͨԶ
+ * Description : 外设从机多连接应用程序,初始化广播连接参数,然后广播,连接主机后,
+ * 请求更新连接参数,通过自定义服务传输数据
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
@@ -21,14 +21,14 @@
#include "bsp_valve.h"
+#include "bsp_uart.h"
#include "log.h"
#undef LOG_ENABLE
#define LOG_ENABLE 1
#undef LOG_TAG
-#define LOG_TAG "BSP_VALVE"
-
+#define LOG_TAG "peripheral"
/*********************************************************************
@@ -149,7 +149,7 @@ static uint8_t advertData[] = {
};
#if 0
-// 㲥37ֽڣ豸ַռ6ֽڣֻ31ֽǿõġ
+// 蓝牙广播包的最大长度是37个字节,其中设备地址占用了6个字节,只有31个字节是可用的。
// GAP - SCAN RSP data (max size = 31 bytes)
static uint8_t scanRspData[] = {
@@ -179,18 +179,18 @@ static uint8_t scanRspData[] = {
0x03, // length of this data
GAP_ADTYPE_MANUFACTURER_SPECIFIC,
- // ǰֽڱʾ ID
+ // 前两个字节表示厂商 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
+ 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
};
@@ -286,11 +286,11 @@ void Peripheral_Init()
// Setup the GAP Peripheral Role Profile
{
- //㲥ʹ
+ //开启广播使能
uint8_t initial_advertising_enable = TRUE;
- //СӼ
+ //最小连接间隔
uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
- //Ӽ
+ //最大连接间隔
uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
// Set the GAP Role Parameters
@@ -309,8 +309,8 @@ void Peripheral_Init()
GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);
// https://www.cnblogs.com/debugdabiaoge/p/17871551.html
- // Ĭϴӻڻظɨ㲥¼ĬϹ㲥Ӧ 37 38 39 3 ŵַ
- // ʹܺԱ֤㲥ɨӰ
+ // 默认从机在回复扫描请求后,会结束广播事件(默认广播应该是 37 38 39 在 3 个信道轮发)
+ // 如下使能后,可以保证广播包不受扫描请求的影响
// uint16_t adv_event_contnue=1<<1;
// GAP_SetParamValue(TGAP_ADV_SCAN_REQ_NOTIFY, adv_event_contnue);
@@ -452,7 +452,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);
@@ -785,7 +785,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;
@@ -827,8 +827,10 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len
for (uint8_t i = 0; i < len; i++)
{
logDebug("%02x ", newValue[i]);
+ BSP_UART1_TxLoop();
}
logDebug("\n profile ChangeCB CHAR1.. End\n");
+ BSP_UART1_TxLoop();
break;
}
@@ -836,42 +838,49 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t 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)
{
case kCmdCfg:
// uint8_t data_buf[64] = {0};
// tmos_memset(data_buf, 0, sizeof(data_buf));
- // ֡
+ // 处理数据帧
logDebug("kCmdCfg");
break;
case kCmdCloseVavle:
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("Ч");
+ logError("无效的命令");
// logHexDumpAll(data, len);
break;
}
}
+ else
+ {
+ logError("数据帧解析失败");
+ }
tmos_msg_deallocate((uint8_t*)HostFrameData);
HostFrameData = NULL;
#endif
diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c
index b7da959..27e6695 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"
@@ -29,6 +29,10 @@
#undef LOG_ENABLE
#define LOG_ENABLE 1
+#undef LOG_TAG
+#define LOG_TAG "main"
+
+
/*********************************************************************
* GLOBAL TYPEDEFS
*/
@@ -41,7 +45,7 @@ const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
/*********************************************************************
* @fn Main_Circulation
*
- * @brief ѭ
+ * @brief 主循环
*
* @return none
*/
@@ -122,7 +126,7 @@ __attribute__((noinline)) void Main_Circulation()
/*********************************************************************
* @fn main
*
- * @brief
+ * @brief 主函数
*
* @return none
*/
@@ -144,10 +148,11 @@ int main(void)
// UART1_DefInit();
- // ij1500000
- BSP_UART1_Init(460800);
+ // 改成 1500000 波特率了
+ BSP_UART1_Init(1500000);
// logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
+ logError("中文测试");
#endif
DelayMs(1000 * 3);
diff --git a/BSP/inc/bsp_uart.h b/BSP/inc/bsp_uart.h
index 34acb6d..4343d12 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 512
void BSP_UART1_Init(uint32_t baudrate);
diff --git a/BSP/src/bsp_adc.c b/BSP/src/bsp_adc.c
index d19ec26..ecd8615 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。 注:如果引脚用于模拟功能(ADC/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,9 +65,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_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
// 默认情况下,ADC 引脚和所在 GPIO 引脚的数字功能是同时存在的
@@ -75,12 +84,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 +97,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]); // 注意:由于ADC内部偏差的存在,当采样电压在所选增益范围极限附近的时候,可能会出现数据溢出的现象
- }
- printf("\n");
+ logDebug("original: ");
+ logHexDumpAll(adcBuff, 20);
+ // 注意:由于ADC内部偏差的存在,当采样电压在所选增益范围极限附近的时候,可能会出现数据溢出的现象
for (i = 0; i < 20; i++)
{
@@ -106,9 +112,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 +128,6 @@ 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);
}
diff --git a/BSP/src/bsp_uart.c b/BSP/src/bsp_uart.c
index 3181af1..283cd9e 100644
--- a/BSP/src/bsp_uart.c
+++ b/BSP/src/bsp_uart.c
@@ -23,6 +23,7 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len)
ret = lwrb_write(&Uart1Tx, buf, len);
// 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..518a80d 100644
--- a/BSP/src/bsp_valve.c
+++ b/BSP/src/bsp_valve.c
@@ -18,8 +18,8 @@
#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"
@@ -110,8 +110,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 +121,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 +139,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("数据帧校验通过");
@@ -195,7 +200,7 @@ 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;
}
@@ -210,7 +215,7 @@ void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t bat, int8_t temp,
ValveData.hum = hum; // 阀门湿度 %RH
_GenerateRawFrame(pRawData, kCmdData, (uint8_t*)&ValveData, sizeof(ValveData));
- logHexDumpAll(&pRawData->buf[0], pRawData->len);
+ // logHexDumpAll(&pRawData->buf[0], pRawData->len);
}
void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status)
@@ -218,7 +223,7 @@ void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd,
uint8_t data = 0;
data = status;
_GenerateRawFrame(pRawData, cmd, &data, 1);
- logHexDumpAll(&pRawData->buf[0], pRawData->len);
+ // logHexDumpAll(&pRawData->buf[0], pRawData->len);
}
void BSP_CloseValve(void)
diff --git a/common/letter-shell/extensions/log/log.h b/common/letter-shell/extensions/log/log.h
index e41dfa1..59774f4 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 /**< 允许注册的最大log对象数量 */
#define LOG_AUTO_TAG 1 /**< 是否自动添加TAG */
#define LOG_END "\r\n" /**< log信息结尾 */
-#define LOG_TIME_STAMP 0 /**< 设置获取系统时间戳 */
+#define LOG_TIME_STAMP TMOS_GetSystemClock() /**< 设置获取系统时间戳 */
#ifndef LOG_TAG
#define LOG_TAG __FUNCTION__ /**< 自定添加的TAG */
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 @@
* 定义此宏为获取系统Tick,如`HAL_GetTick()`
* @note 此宏不定义时无法使用双击tab补全命令help,无法使用shell超时锁定
*/
-#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..94992a8 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 512
extern Shell shell;