refactor(vd960Loop): 算法回退到 DLD154V4B,四通道适配

- 用 DLD154V4B vd1_task/per_channel 替换 vds_task 复杂算法
- 移除 FUNCTION_B/二次判断/快速变化/多重确认等增强特性
- 保留平坦性离开算法 (CN200910309382),每通道独立状态
- 灵敏度表改为 DLD154V4B 4级: {216,108,36,10} / {108,72,18,9}
- 清理废弃类型: FltHistoryManager, Loop_ACS_Info, StageRangeConfig 等
- 首次添加 vd960DBN 完整源码
This commit is contained in:
wangfq
2026-06-25 16:21:57 +08:00
parent 6fd4e564e3
commit 95808f9f25
966 changed files with 406958 additions and 84 deletions

140
vd960DBN/BLE/HAL/KEY.c Normal file
View File

@@ -0,0 +1,140 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : KEY.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* Header file contains */
#include "HAL.h"
/**************************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************************/
static uint8_t halKeySavedKeys; /* Keep the last state of the button to query whether there is a key value change */
/**************************************************************************************************
* FUNCTIONS - Local
**************************************************************************************************/
static halKeyCBack_t pHalKeyProcessFunction; /* callback function */
/**************************************************************************************************
* @fn HAL_KeyInit
*
* @brief Initilize Key Service
*
* @param none
*
* @return None
**************************************************************************************************/
void HAL_KeyInit(void)
{
/* Initialize previous key to 0 */
halKeySavedKeys = 0;
/* Initialize callback function */
pHalKeyProcessFunction = NULL;
RCC_APB2PeriphClockCmd(KEY1_PCENR, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = KEY1_BV;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY1_GPIO, &GPIO_InitStructure);
}
/**************************************************************************************************
* @fn HalKeyConfig
*
* @brief Configure the Key serivce
*
* @param cback - pointer to the CallBack function
*
* @return None
**************************************************************************************************/
void HalKeyConfig(halKeyCBack_t cback)
{
/* Register the callback fucntion */
pHalKeyProcessFunction = cback;
tmos_start_task(halTaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE); /* Kick off polling */
}
/**************************************************************************************************
* @fn HalKeyRead
*
* @brief Read the current value of a key
*
* @param None
*
* @return keys - current keys status
**************************************************************************************************/
uint8_t HalKeyRead(void)
{
uint8_t keys = 0;
if(HAL_PUSH_BUTTON1())
{ //Read button 1
keys |= HAL_KEY_SW_1;
}
if(HAL_PUSH_BUTTON2())
{ //Read button 1
keys |= HAL_KEY_SW_2;
}
if(HAL_PUSH_BUTTON3())
{ //Read button 1
keys |= HAL_KEY_SW_3;
}
if(HAL_PUSH_BUTTON4())
{ //Read button 1
keys |= HAL_KEY_SW_4;
}
return keys;
}
/**************************************************************************************************
* @fn HAL_KeyPoll
*
* @brief Called by hal_driver to poll the keys
*
* @param None
*
* @return None
**************************************************************************************************/
void HAL_KeyPoll(void)
{
uint8_t keys = 0;
if(HAL_PUSH_BUTTON1())
{
keys |= HAL_KEY_SW_1;
}
if(HAL_PUSH_BUTTON2())
{
keys |= HAL_KEY_SW_2;
}
if(HAL_PUSH_BUTTON3())
{
keys |= HAL_KEY_SW_3;
}
if(HAL_PUSH_BUTTON4())
{
keys |= HAL_KEY_SW_4;
}
if(keys == halKeySavedKeys)
{ /* Exit - since no keys have changed */
return;
}
halKeySavedKeys = keys; /* Store the current keys for comparation next time */
/* Invoke Callback if new keys were depressed */
if(keys && (pHalKeyProcessFunction))
{
(pHalKeyProcessFunction)(keys);
}
}
/******************************** endfile @ key ******************************/

366
vd960DBN/BLE/HAL/LED.c Normal file
View File

@@ -0,0 +1,366 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : LED.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* Header file contains */
#include "HAL.h"
/* LED control structure */
typedef struct
{
uint8_t mode; /* Operation mode */
uint8_t todo; /* Blink cycles left */
uint8_t onPct; /* On cycle percentage */
uint16_t time; /* On/off cycle time (msec) */
uint32_t next; /* Time for next change */
} HalLedControl_t;
typedef struct
{
HalLedControl_t HalLedControlTable[HAL_LED_DEFAULT_MAX_LEDS];
uint8_t sleepActive;
} HalLedStatus_t;
/***************************************************************************************************
* GLOBAL VARIABLES
***************************************************************************************************/
static uint8_t HalLedState; // LED state at last set/clr/blink update
static uint8_t preBlinkState; // Original State before going to blink mode
// bit 0, 1, 2, 3 represent led 0, 1, 2, 3
static HalLedStatus_t HalLedStatusControl;
/***************************************************************************************************
* LOCAL FUNCTION
***************************************************************************************************/
void HalLedOnOff(uint8_t leds, uint8_t mode);
/***************************************************************************************************
* FUNCTIONS - API
***************************************************************************************************/
/*********************************************************************
* @fn HAL_LedInit
*
* @brief Initialize LED Service
*
* @return none
*/
void HAL_LedInit(void)
{
/* Initialize all LEDs to OFF */
RCC_APB2PeriphClockCmd(LED1_PCENR, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LED1_BV;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED1_GPIO, &GPIO_InitStructure);
HalLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF);
// just test
HalLedBlink(HAL_LED_1, 10, 30, 4000);
/* Initialize sleepActive to FALSE */
HalLedStatusControl.sleepActive = FALSE;
}
/*********************************************************************
* @fn HalLedSet
*
* @brief Turn ON/OFF/TOGGLE given LEDs
*
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
* @param mode - BLINK, FLASH, TOGGLE, ON, OFF
*
* @return 0
*/
uint8_t HalLedSet(uint8_t leds, uint8_t mode)
{
uint8_t led;
HalLedControl_t *sts;
switch(mode)
{
case HAL_LED_MODE_BLINK:
{
/* Default blink, 1 time, D% duty cycle */
HalLedBlink(leds, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);
break;
}
case HAL_LED_MODE_FLASH:
{
/* Default flash, N times, D% duty cycle */
HalLedBlink(leds, HAL_LED_DEFAULT_FLASH_COUNT, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);
break;
}
case HAL_LED_MODE_ON:
case HAL_LED_MODE_OFF:
case HAL_LED_MODE_TOGGLE:
{
led = HAL_LED_1;
leds &= HAL_LED_ALL;
sts = HalLedStatusControl.HalLedControlTable;
while(leds)
{
if(leds & led)
{
if(mode != HAL_LED_MODE_TOGGLE)
{
sts->mode = mode; /* ON or OFF */
}
else
{
sts->mode ^= HAL_LED_MODE_ON; /* Toggle */
}
HalLedOnOff(led, sts->mode);
leds ^= led;
}
led <<= 1;
sts++;
}
break;
}
default:
break;
}
return (0);
}
/*********************************************************************
* @fn HalLedBlink
*
* @brief Blink the leds
*
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
* @param numBlinks - number of blinks
* @param percent - the percentage in each period where the led will be on
* @param period - length of each cycle in milliseconds
*
* @return none
*/
void HalLedBlink(uint8_t leds, uint8_t numBlinks, uint8_t percent, uint16_t period)
{
uint8_t led;
HalLedControl_t *sts;
if(leds && percent && period)
{
if(percent < 100)
{
led = HAL_LED_1;
leds &= HAL_LED_ALL;
sts = HalLedStatusControl.HalLedControlTable;
while(leds)
{
if(leds & led)
{
/* Store the current state of the led before going to blinking */
preBlinkState |= (led & HalLedState);
sts->mode = HAL_LED_MODE_OFF; /* Stop previous blink */
sts->time = period; /* Time for one on/off cycle */
sts->onPct = percent; /* % of cycle LED is on */
sts->todo = numBlinks; /* Number of blink cycles */
if(!numBlinks)
{
sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */
}
sts->next = TMOS_GetSystemClock(); /* Start now */
sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */
leds ^= led;
}
led <<= 1;
sts++;
}
tmos_start_task(halTaskID, LED_BLINK_EVENT, 0);
}
else
{
HalLedSet(leds, HAL_LED_MODE_ON); /* >= 100%, turn on */
}
}
else
{
HalLedSet(leds, HAL_LED_MODE_OFF); /* No on time, turn off */
}
}
/*********************************************************************
* @fn HalLedUpdate
*
* @brief Update leds to work with blink
*
* @return none
*/
void HalLedUpdate(void)
{
uint8_t led, pct, leds;
uint16_t next, wait;
uint32_t time;
HalLedControl_t *sts;
next = 0;
led = HAL_LED_1;
leds = HAL_LED_ALL;
sts = HalLedStatusControl.HalLedControlTable;
/* Check if sleep is active or not */
if(!HalLedStatusControl.sleepActive)
{
while(leds)
{
if(leds & led)
{
if(sts->mode & HAL_LED_MODE_BLINK)
{
time = TMOS_GetSystemClock();
if(time >= sts->next)
{
if(sts->mode & HAL_LED_MODE_ON)
{
pct = 100 - sts->onPct; /* Percentage of cycle for off */
sts->mode &= ~HAL_LED_MODE_ON; /* Say it's not on */
HalLedOnOff(led, HAL_LED_MODE_OFF); /* Turn it off */
if(!(sts->mode & HAL_LED_MODE_FLASH))
{
if(sts->todo != 0xff)
{
sts->todo--; /* Not continuous, reduce count */
}
if(!sts->todo)
{
sts->mode ^= HAL_LED_MODE_BLINK; /* No more blinks */
}
}
}
else
{
pct = sts->onPct; /* Percentage of cycle for on */
sts->mode |= HAL_LED_MODE_ON; /* Say it's on */
HalLedOnOff(led, HAL_LED_MODE_ON); /* Turn it on */
}
if(sts->mode & HAL_LED_MODE_BLINK)
{
wait = (((uint32_t)pct * (uint32_t)sts->time) / 100);
sts->next = time + wait;
}
else
{
/* no more blink, no more wait */
wait = 0;
/* After blinking, set the LED back to the state before it blinks */
HalLedSet(led, ((preBlinkState & led) != 0) ? HAL_LED_MODE_ON : HAL_LED_MODE_OFF);
/* Clear the saved bit */
preBlinkState &= (led ^ 0xFF);
}
}
else
{
wait = sts->next - time; /* Time left */
}
if(!next || (wait && (wait < next)))
{
next = wait;
}
}
leds ^= led;
}
led <<= 1;
sts++;
}
if(next)
{
tmos_start_task(halTaskID, LED_BLINK_EVENT, next); /* Schedule event */
}
}
}
/*********************************************************************
* @fn HalLedOnOff
*
* @brief Turns specified LED ON or OFF
*
* @param led - LED bit mask
* @param mode - LED_ON,LED_OFF,
*
* @return none
*/
void HalLedOnOff(uint8_t leds, uint8_t mode)
{
if(leds & HAL_LED_1)
{
if(mode == HAL_LED_MODE_ON)
{
HAL_TURN_ON_LED1();
}
else
{
HAL_TURN_OFF_LED1();
}
}
if(leds & HAL_LED_2)
{
if(mode == HAL_LED_MODE_ON)
{
HAL_TURN_ON_LED2();
}
else
{
HAL_TURN_OFF_LED2();
}
}
if(leds & HAL_LED_3)
{
if(mode == HAL_LED_MODE_ON)
{
HAL_TURN_ON_LED3();
}
else
{
HAL_TURN_OFF_LED3();
}
}
if(leds & HAL_LED_4)
{
if(mode == HAL_LED_MODE_ON)
{
HAL_TURN_ON_LED4();
}
else
{
HAL_TURN_OFF_LED4();
}
}
/* Remember current state */
if(mode)
{
HalLedState |= leds;
}
else
{
HalLedState &= (leds ^ 0xFF);
}
}
/***************************************************************************************************
* @fn HalGetLedState
*
* @brief Dim LED2 - Dim (set level) of LED2
*
* @return led state
*/
uint8_t HalLedGetState()
{
return HalLedState;
}
/******************************** endfile @ led ******************************/

204
vd960DBN/BLE/HAL/Link.ld Normal file
View File

@@ -0,0 +1,204 @@
ENTRY( _start )
__stack_size = 2048;
PROVIDE( _stack_size = __stack_size );
MEMORY
{
/* CH32V20x_D6 - CH32V203F6-CH32V203G6-CH32V203C6 */
/*
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
*/
/* CH32V20x_D6 - CH32V203K8-CH32V203C8-CH32V203G8-CH32V203F8 */
/*
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
*/
/* CH32V20x_D8 - CH32V203RB
CH32V20x_D8W - CH32V208x
FLASH + RAM supports the following configuration
FLASH-128K + RAM-64K
FLASH-144K + RAM-48K
FLASH-160K + RAM-32K
*/
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 448K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
}
SECTIONS
{
.init :
{
_sinit = .;
. = ALIGN(4);
KEEP(*(SORT_NONE(.init)))
. = ALIGN(4);
_einit = .;
} >FLASH AT>FLASH
.vector :
{
*(.vector);
. = ALIGN(64);
KEEP(*(SORT_NONE(.handle_reset)))
} >FLASH AT>FLASH
.highcode :
{
. = ALIGN(4);
*(.highcode);
*(.highcode.*);
. = ALIGN(4);
} >FLASH AT>FLASH
.text :
{
. = ALIGN(4);
EXCLUDE_FILE (*wchble.a) *(.text .text*)
*(.text)
*(.text.*)
*(.rodata)
*(.rodata*)
*(.sdata2.*)
*(.glue_7)
*(.glue_7t)
*(.gnu.linkonce.t.*)
. = ALIGN(4);
} >FLASH AT>FLASH
.fini :
{
KEEP(*(SORT_NONE(.fini)))
. = ALIGN(4);
} >FLASH AT>FLASH
PROVIDE( _etext = . );
PROVIDE( _eitcm = . );
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH AT>FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH AT>FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH AT>FLASH
.ctors :
{
/* gcc uses crtbegin.o to find the start of
the constructors, so we make sure it is
first. Because this is a wildcard, it
doesn't matter if the user does not
actually link against crtbegin.o; the
linker won't look for a file to match a
wildcard. The wildcard also means that it
doesn't matter which directory crtbegin.o
is in. */
KEEP (*crtbegin.o(.ctors))
KEEP (*crtbegin?.o(.ctors))
/* We don't want to include the .ctor section from
the crtend.o file until after the sorted ctors.
The .ctor section from the crtend file contains the
end of ctors marker and it must be last */
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
KEEP (*(SORT(.ctors.*)))
KEEP (*(.ctors))
} >FLASH AT>FLASH
.dtors :
{
KEEP (*crtbegin.o(.dtors))
KEEP (*crtbegin?.o(.dtors))
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
KEEP (*(SORT(.dtors.*)))
KEEP (*(.dtors))
} >FLASH AT>FLASH
.dalign :
{
. = ALIGN(4);
PROVIDE(_data_vma = .);
} >RAM AT>FLASH
.dlalign :
{
. = ALIGN(4);
PROVIDE(_data_lma = .);
} >FLASH AT>FLASH
.data :
{
*(.gnu.linkonce.r.*)
*(.data .data.*)
*(.gnu.linkonce.d.*)
. = ALIGN(8);
PROVIDE( __global_pointer$ = . + 0x800 );
*(.sdata .sdata.*)
*(.gnu.linkonce.s.*)
. = ALIGN(8);
*(.srodata.cst16)
*(.srodata.cst8)
*(.srodata.cst4)
*(.srodata.cst2)
*(.srodata .srodata.*)
. = ALIGN(4);
PROVIDE( _edata = .);
} >RAM AT>FLASH
.bss :
{
. = ALIGN(4);
PROVIDE( _sbss = .);
*(.sbss*)
*(.gnu.linkonce.sb.*)
*(.bss*)
*(.gnu.linkonce.b.*)
*(COMMON*)
. = ALIGN(4);
PROVIDE( _ebss = .);
} >RAM AT>FLASH
PROVIDE( _end = _ebss);
PROVIDE( end = . );
.stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
{
PROVIDE( _heap_end = . );
. = ALIGN(4);
PROVIDE(_susrstack = . );
. = . + __stack_size;
PROVIDE( _eusrstack = .);
} >RAM
/*.stack ORIGIN(RAM)+LENGTH(RAM) :
{
PROVIDE( _heap_end = . );
. = ALIGN(4);
PROVIDE(_eusrstack = . );
} >RAM */
}

326
vd960DBN/BLE/HAL/MCU.c Normal file
View File

@@ -0,0 +1,326 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : MCU.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : HAL task processing function and BLE and hardware initialization
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* Header file contains */
#include "HAL.h"
#include "string.h"
tmosTaskID halTaskID;
uint32_t g_LLE_IRQLibHandlerLocation;
/*******************************************************************************
* @fn Lib_Calibration_LSI
*
* @brief Internal 32K calibration
*
* @param None.
*
* @return None.
*/
void Lib_Calibration_LSI(void)
{
Calibration_LSI(Level_64);
}
#if(defined(BLE_SNV)) && (BLE_SNV == TRUE)
/*******************************************************************************
* @fn Lib_Read_Flash
*
* @brief Callback function used for BLE lib.
*
* @param addr.
* @param num.
* @param pBuf.
*
* @return None.
*/
uint32_t Lib_Read_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
{
tmos_memcpy(pBuf, (uint32_t*)addr, num*4);
return 0;
}
/*******************************************************************************
* @fn Lib_Write_Flash
*
* @brief Callback function used for BLE lib.
*
* @param addr.
* @param num.
* @param pBuf.
*
* @return None.
*/
uint32_t Lib_Write_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
{
FLASH_Unlock_Fast();
FLASH_ErasePage_Fast( addr );
FLASH_ProgramPage_Fast( addr, pBuf);
FLASH_Lock_Fast();
Delay_Us(1);
return 0;
}
#endif
/*******************************************************************************
* @fn WCHBLE_Init
*
* @brief BLE library initialization
*
* @param None.
*
* @return None.
*/
void WCHBLE_Init(void)
{
uint8_t i;
bleConfig_t cfg;
g_LLE_IRQLibHandlerLocation = (uint32_t)LLE_IRQLibHandler;
if(!tmos_memcmp(VER_LIB, VER_FILE, strlen(VER_FILE)))
{
PRINT("head file error...\n");
while(1);
}
// 32M crystal capacitance and current
OSC->HSE_CAL_CTRL &= ~(0x07<<28);
OSC->HSE_CAL_CTRL |= 0x03<<28;
OSC->HSE_CAL_CTRL |= 3<<24;
tmos_memset(&cfg, 0, sizeof(bleConfig_t));
cfg.MEMAddr = (uint32_t)MEM_BUF;
cfg.MEMLen = (uint32_t)BLE_MEMHEAP_SIZE;
cfg.BufMaxLen = (uint32_t)BLE_BUFF_MAX_LEN;
cfg.BufNumber = (uint32_t)BLE_BUFF_NUM;
cfg.TxNumEvent = (uint32_t)BLE_TX_NUM_EVENT;
cfg.TxPower = (uint32_t)BLE_TX_POWER;
#if(defined(BLE_SNV)) && (BLE_SNV == TRUE)
cfg.SNVAddr = (uint32_t)BLE_SNV_ADDR;
cfg.SNVNum = (uint32_t)BLE_SNV_NUM;
cfg.readFlashCB = Lib_Read_Flash;
cfg.writeFlashCB = Lib_Write_Flash;
#endif
cfg.ClockFrequency = CAB_LSIFQ/2;
#if(CLK_OSC32K==0)
cfg.ClockAccuracy = 50;
#else
cfg.ClockAccuracy = 1000;
#endif
cfg.ConnectNumber = (PERIPHERAL_MAX_CONNECTION & 3) | (CENTRAL_MAX_CONNECTION << 2);
#if(defined TEM_SAMPLE) && (TEM_SAMPLE == TRUE)
// Calibrate RF and internal RC according to temperature changes (greater than 7 degrees Celsius)
cfg.tsCB = HAL_GetInterTempValue;
#if(CLK_OSC32K)
cfg.rcCB = Lib_Calibration_LSI; // Internal 32K clock calibration
#endif
#endif
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
cfg.idleCB = BLE_LowPower; // Enable sleep
#endif
#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
for(i = 0; i < 6; i++)
{
cfg.MacAddr[i] = MacAddr[5 - i];
}
#else
{
uint8_t MacAddr[6];
FLASH_GetMACAddress(MacAddr);
for(i = 0; i < 6; i++)
{
cfg.MacAddr[i] = MacAddr[i]; // Use chip mac address
}
}
#endif
if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024)
{
while(1);
}
i = BLE_LibInit(&cfg);
if(i)
{
PRINT("LIB init error code: %x ...\n", i);
while(1);
}
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE );
NVIC_EnableIRQ( BB_IRQn );
NVIC_EnableIRQ( LLE_IRQn );
}
/*******************************************************************************
* @fn HAL_ProcessEvent
*
* @brief HAL processing
*
* @param task_id - The TMOS assigned task ID.
* @param events - events to process. This is a bit map and can
* contain more than one event.
*
* @return events.
*/
tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events)
{
uint8_t *msgPtr;
if(events & SYS_EVENT_MSG)
{
/**
* Process the HAL layer message, call tmos_msg_receive to read the message,
* and delete the message after processing.
*/
msgPtr = tmos_msg_receive(task_id);
if(msgPtr)
{
/* De-allocate */
tmos_msg_deallocate(msgPtr);
}
return events ^ SYS_EVENT_MSG;
}
if(events & LED_BLINK_EVENT)
{
#if(defined HAL_LED) && (HAL_LED == TRUE)
HalLedUpdate();
#endif // HAL_LED
return events ^ LED_BLINK_EVENT;
}
if(events & HAL_KEY_EVENT)
{
#if(defined HAL_KEY) && (HAL_KEY == TRUE)
HAL_KeyPoll(); /* Check for keys */
tmos_start_task(halTaskID, HAL_KEY_EVENT, MS1_TO_SYSTEM_TIME(100));
return events ^ HAL_KEY_EVENT;
#endif
}
if(events & HAL_REG_INIT_EVENT)
{
#if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE) // Calibration tasks, a single time is less than 10ms
BLE_RegInit(); // Calibrate RF
#if(CLK_OSC32K)
Lib_Calibration_LSI(); // Calibrate internal RC
#endif
tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD));
return events ^ HAL_REG_INIT_EVENT;
#endif
}
if(events & HAL_TEST_EVENT)
{
PRINT("* \n");
tmos_start_task(halTaskID, HAL_TEST_EVENT, MS1_TO_SYSTEM_TIME(1000));
return events ^ HAL_TEST_EVENT;
}
return 0;
}
/*******************************************************************************
* @fn HAL_Init
*
* @brief Ó²¼þ³õʼ»¯
*
* @param None.
*
* @return None.
*/
void HAL_Init()
{
halTaskID = TMOS_ProcessEventRegister(HAL_ProcessEvent);
HAL_TimeInit();
#if(defined HAL_SLEEP) && (HAL_SLEEP == TRUE)
HAL_SleepInit();
#endif
#if(defined HAL_LED) && (HAL_LED == TRUE)
HAL_LedInit();
#endif
#if(defined HAL_KEY) && (HAL_KEY == TRUE)
HAL_KeyInit();
#endif
#if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE)
// Add a calibration task, and a single calibration takes less than 10ms
tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD));
#endif
// tmos_start_task(halTaskID, HAL_TEST_EVENT, MS1_TO_SYSTEM_TIME(1000)); // Add a test task
}
/*******************************************************************************
* @fn HAL_GetInterTempValue
*
* @brief Get the internal temperature sampling value, if the ADC interrupt sampling is used,
* it is necessary to temporarily shield the interrupt in this function.
*
* @return Internal temperature sampling value.
*/
uint16_t HAL_GetInterTempValue(void)
{
uint32_t rcc_apb2pcenr, rcc_cfgr0, adc1_ctrl1, adc1_ctrl2, adc1_rsqr1, adc1_rsqr2, adc1_rsqr3, adc1_samptr1, adc1_samptr2;
uint32_t adc1_iofr1, adc1_iofr2, adc1_iofr3, adc1_iofr4, adc1_wdhtr, adc1_wdltr, adc1_isqr;
ADC_InitTypeDef ADC_InitStructure = {0};
uint16_t adc_data;
rcc_apb2pcenr = RCC->APB2PCENR;
rcc_cfgr0 = RCC->CFGR0;
adc1_ctrl1 = ADC1->CTLR1;
adc1_ctrl2 = ADC1->CTLR2;
adc1_rsqr1 = ADC1->RSQR1;
adc1_rsqr2 = ADC1->RSQR2;
adc1_rsqr3 = ADC1->RSQR3;
adc1_samptr1 = ADC1->SAMPTR1;
adc1_samptr2 = ADC1->SAMPTR2;
adc1_iofr1 = ADC1->IOFR1;
adc1_iofr2 = ADC1->IOFR2;
adc1_iofr3 = ADC1->IOFR3;
adc1_iofr4 = ADC1->IOFR4;
adc1_wdhtr = ADC1->WDHTR;
adc1_wdltr = ADC1->WDLTR;
adc1_isqr = ADC1->ISQR;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_BufferCmd(ADC1, ENABLE); //enable buffer
ADC_TempSensorVrefintCmd(ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_239Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc_data = ADC_GetConversionValue(ADC1);
ADC_DeInit(ADC1);
RCC->APB2PCENR = rcc_apb2pcenr;
RCC->CFGR0 = rcc_cfgr0;
ADC1->CTLR1 = adc1_ctrl1;
ADC1->CTLR2 = adc1_ctrl2;
ADC1->RSQR1 = adc1_rsqr1;
ADC1->RSQR2 = adc1_rsqr2;
ADC1->RSQR3 = adc1_rsqr3;
ADC1->SAMPTR1 = adc1_samptr1;
ADC1->SAMPTR2 = adc1_samptr2;
ADC1->IOFR1 = adc1_iofr1;
ADC1->IOFR2 = adc1_iofr2;
ADC1->IOFR3 = adc1_iofr3;
ADC1->IOFR4 = adc1_iofr4;
ADC1->WDHTR = adc1_wdhtr;
ADC1->WDLTR = adc1_wdltr;
ADC1->ISQR = adc1_isqr;
return (adc_data);
}
/******************************** endfile @ mcu ******************************/

113
vd960DBN/BLE/HAL/RTC.c Normal file
View File

@@ -0,0 +1,113 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : RTC.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : RTC configuration and its initialization
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* Header file contains */
#include "HAL.h"
/*********************************************************************
* CONSTANTS
*/
#define RTC_INIT_TIME_HOUR 0
#define RTC_INIT_TIME_MINUTE 0
#define RTC_INIT_TIME_SECEND 0
/***************************************************
* Global variables
*/
volatile uint32_t RTCTigFlag;
/*******************************************************************************
* @fn RTC_SetTignTime
*
* @brief Configure RTC trigger time
*
* @param time - Trigger time.
*
* @return None.
*/
void RTC_SetTignTime(uint32_t time)
{
RTC_WaitForLastTask();
RTC_SetAlarm(time);
RTC_WaitForLastTask();
RTCTigFlag = 0;
}
/*******************************************************************************
* @fn HAL_Time0Init
*
* @brief System timer initialization
*
* @param None.
*
* @return None.
*/
void HAL_TimeInit(void)
{
uint16_t temp=0;
uint8_t state=0;
bleClockConfig_t conf={0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
#if( CLK_OSC32K )
RCC_LSICmd(ENABLE);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#else
RCC_LSEConfig(RCC_LSE_ON);
/* Check the specified RCC logo position settings or not,
* wait for the low-speed crystal oscillator to be ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
temp++;
Delay_Ms(10);
}
if(temp>=250)
{
printf("time error..\n");
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#endif
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForLastTask();
RTC_WaitForLastTask();
RTC_SetPrescaler(1);
RTC_WaitForLastTask();
RTC_SetCounter(0);
RTC_WaitForLastTask();
#if( CLK_OSC32K )
Lib_Calibration_LSI();
#endif
conf.ClockAccuracy = CLK_OSC32K?1000:100;
conf.ClockFrequency = CAB_LSIFQ/2;
conf.ClockMaxCount = 0xFFFFFFFF;
conf.getClockValue = RTC_GetCounter;
state = TMOS_TimerInit( &conf );
if(state)
{
PRINT("TMOS_TimerInit err %x\n",state);
}
}
__attribute__((interrupt("WCH-Interrupt-fast")))
void RTCAlarm_IRQHandler(void)
{
RTCTigFlag = 1;
EXTI_ClearITPendingBit(EXTI_Line17);
RTC_ClearITPendingBit(RTC_IT_ALR);
RTC_WaitForLastTask();
}
/******************************** endfile @ time ******************************/

107
vd960DBN/BLE/HAL/SLEEP.c Normal file
View File

@@ -0,0 +1,107 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : SLEEP.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : Sleep configuration and its initialization
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* Header file contains */
#include "HAL.h"
#define US_TO_TICK(us) (uint32_t)((us) / (1000000 / ((CAB_LSIFQ / 2))))
#define SLEEP_PERIOD_MIN_US 200
#define SLEEP_PERIOD_MAX_TICK 0xFFD2393F
#define SLEEP_PERIOD_MIN_TICK US_TO_TICK(SLEEP_PERIOD_MIN_US)
#define HESREADY_TICK US_TO_TICK(WAKE_UP_MAX_TIME_US)
/*******************************************************************************
* @fn BLE_LowPower
*
* @brief 启动睡眠
*
* @param time - 唤醒的时间点RTC绝对值
*
* @return state.
*/
uint32_t BLE_LowPower(uint32_t time)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
uint32_t current_time;
uint32_t sleep_period;
uint32_t wake_time;
wake_time = time - HESREADY_TICK;
__disable_irq();
current_time = RTC_GetCounter();
sleep_period = wake_time - current_time;
if((sleep_period < SLEEP_PERIOD_MIN_TICK) || (sleep_period > SLEEP_PERIOD_MAX_TICK))
{
__enable_irq();
return 2;
}
RTC_SetTignTime(wake_time);
__enable_irq();
#if(DEBUG == DEBUG_UART1) // To use other serial ports to output printing information, you need to modify this line of code
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
__NOP();
}
#endif
// LOW POWER-sleep
if(!RTCTigFlag)
{
PWR_EnterSTOPMode_RAM_LV(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
SystemInit();
}
else
{
return 3;
}
#endif
return 0;
}
/*******************************************************************************
* @fn HAL_SleepInit
*
* @brief Configure sleep Wake-up source - RTC wake up, trigger mode
*
* @param None.
*
* @return None.
*/
void HAL_SleepInit(void)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_ALR, ENABLE);
EXTI_InitTypeDef EXTI_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
}

View File

@@ -0,0 +1,82 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : HAL.h
* Author : WCH
* Version : V1.0
* Date : 2016/05/05
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
#ifndef __HAL_H
#define __HAL_H
#ifdef __cplusplus
extern "C" {
#endif
#include "config.h"
#include "RTC.h"
#include "SLEEP.h"
#include "KEY.h"
#include "LED.h"
/* hal task Event */
#define LED_BLINK_EVENT 0x0001
#define HAL_KEY_EVENT 0x0002
#define HAL_REG_INIT_EVENT 0x2000
#define HAL_TEST_EVENT 0x4000
/*********************************************************************
* GLOBAL VARIABLES
*/
extern tmosTaskID halTaskID;
/*********************************************************************
* GLOBAL FUNCTIONS
*/
/**
* @brief Hardware initialization
*/
extern void HAL_Init(void);
/**
* @brief HAL processing
*
* @param task_id - The TMOS assigned task ID.
* @param events - events to process. This is a bit map and can
* contain more than one event.
*/
extern tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events);
/**
* @brief Initialization of the BLE library
*/
extern void WCHBLE_Init(void);
/**
* @brief Get the internal temperature sampling value.
* If the ADC interrupt sampling is used,
* the interrupt is temporarily shielded in this function.
*
* @return Internal temperature sampling value.
*/
extern uint16_t HAL_GetInterTempValue(void);
/**
* @brief Internal 32K calibration
*/
extern void Lib_Calibration_LSI(void);
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,112 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : KEY.h
* Author : WCH
* Version : V1.0
* Date : 2016/04/12
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
#ifndef __KEY_H
#define __KEY_H
#ifdef __cplusplus
extern "C" {
#endif
/**************************************************************************************************
* MACROS
**************************************************************************************************/
#define HAL_KEY_POLLING_VALUE 100
/* Switches (keys) */
#define HAL_KEY_SW_1 0x01 // key1
#define HAL_KEY_SW_2 0x02 // key2
#define HAL_KEY_SW_3 0x04 // key3
#define HAL_KEY_SW_4 0x08 // key4
/* Key definition */
/* 1 - KEY */
#define KEY1_PCENR (RCC_APB2Periph_GPIOB)
#define KEY2_PCENR ()
#define KEY3_PCENR ()
#define KEY4_PCENR ()
#define KEY1_GPIO (GPIOB)
#define KEY2_GPIO ()
#define KEY3_GPIO ()
#define KEY4_GPIO ()
#define KEY1_BV BV(13)
#define KEY2_BV ()
#define KEY3_BV ()
#define KEY4_BV ()
#define KEY1_IN (GPIO_ReadInputDataBit(KEY1_GPIO, KEY1_BV)==0)
#define KEY2_IN ()
#define KEY3_IN ()
#define KEY4_IN ()
#define HAL_PUSH_BUTTON1() (KEY1_IN) //Add custom button
#define HAL_PUSH_BUTTON2() (0)
#define HAL_PUSH_BUTTON3() (0)
#define HAL_PUSH_BUTTON4() (0)
/**************************************************************************************************
* TYPEDEFS
**************************************************************************************************/
typedef void (*halKeyCBack_t)(uint8_t keys);
typedef struct
{
uint8_t keys; // keys
} keyChange_t;
/**************************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************************/
/*********************************************************************
* FUNCTIONS
*/
/**
* @brief Initialize the Key Service
*/
void HAL_KeyInit(void);
/**
* @brief This is for internal used by hal_driver
*/
void HAL_KeyPoll(void);
/**
* @brief Configure the Key serivce
*
* @param cback - pointer to the CallBack function
*/
void HalKeyConfig(const halKeyCBack_t cback);
/**
* @brief Read the Key callback
*/
void HalKeyCallback(uint8_t keys);
/**
* @brief Read the Key status
*/
uint8_t HalKeyRead(void);
/**************************************************************************************************
**************************************************************************************************/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,133 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : LED.h
* Author : WCH
* Version : V1.0
* Date : 2016/04/12
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
#ifndef __LED_H
#define __LED_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************************************************************
* CONSTANTS
*/
/* LEDS - The LED number is the same as the bit position */
#define HAL_LED_1 0x01
#define HAL_LED_2 0x02
#define HAL_LED_3 0x04
#define HAL_LED_4 0x08
#define HAL_LED_ALL (HAL_LED_1 | HAL_LED_2 | HAL_LED_3 | HAL_LED_4)
/* Modes */
#define HAL_LED_MODE_OFF 0x00
#define HAL_LED_MODE_ON 0x01
#define HAL_LED_MODE_BLINK 0x02
#define HAL_LED_MODE_FLASH 0x04
#define HAL_LED_MODE_TOGGLE 0x08
/* Defaults */
#define HAL_LED_DEFAULT_MAX_LEDS 4
#define HAL_LED_DEFAULT_DUTY_CYCLE 5
#define HAL_LED_DEFAULT_FLASH_COUNT 50
#define HAL_LED_DEFAULT_FLASH_TIME 1000
/*********************************************************************
* TYPEDEFS
*/
/* Connect an LED to monitor the progress of the demo program, the low-level LED is on */
/* 1 - LED */
#define LED1_PCENR (RCC_APB2Periph_GPIOB)
#define LED2_PCENR
#define LED3_PCENR
#define LED1_GPIO (GPIOB)
#define LED2_GPIO
#define LED3_GPIO
#define LED1_BV BV(15)
#define LED2_BV
#define LED3_BV
#define HAL_TURN_OFF_LED1() (GPIO_WriteBit(LED1_GPIO, LED1_BV, Bit_SET))
#define HAL_TURN_OFF_LED2()
#define HAL_TURN_OFF_LED3()
#define HAL_TURN_OFF_LED4()
#define HAL_TURN_ON_LED1() (GPIO_WriteBit(LED1_GPIO, LED1_BV, Bit_RESET))
#define HAL_TURN_ON_LED2()
#define HAL_TURN_ON_LED3()
#define HAL_TURN_ON_LED4()
#define HAL_STATE_LED1() 0
#define HAL_STATE_LED2() 0
#define HAL_STATE_LED3() 0
#define HAL_STATE_LED4() 0
/*********************************************************************
* GLOBAL VARIABLES
*/
/**
* @brief Initialize LED Service.
*/
void HAL_LedInit(void);
/**
* @brief update time LED Service.
*/
void HalLedUpdate(void);
/**
* @brief Turn ON/OFF/TOGGLE given LEDs
*
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
* @param mode - BLINK, FLASH, TOGGLE, ON, OFF
*/
extern uint8_t HalLedSet(uint8_t led, uint8_t mode);
/**
* @brief Blink the leds
*
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
* @param numBlinks - number of blinks
* @param percent - the percentage in each period where the led will be on
* @param period - length of each cycle in milliseconds
*/
extern void HalLedBlink(uint8_t leds, uint8_t cnt, uint8_t duty, uint16_t time);
/**
* @brief Put LEDs in sleep state - store current values
*/
extern void HalLedEnterSleep(void);
/**
* @brief Retore LEDs from sleep state
*/
extern void HalLedExitSleep(void);
/**
* @brief Return LED state
*/
extern uint8_t HalLedGetState(void);
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,40 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : RTC.h
* Author : WCH
* Version : V1.0
* Date : 2016/04/12
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
#ifndef __RTC_H
#define __RTC_H
#ifdef __cplusplus
extern "C" {
#endif
extern volatile uint32_t RTCTigFlag;
/**
* @brief Initialize time Service.
*/
void HAL_TimeInit(void);
/**
* @brief Configure RTC trigger time
*
* @param time - Trigger time.
*/
extern void RTC_SetTignTime(uint32_t time);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,50 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : SLEEP.h
* Author : WCH
* Version : V1.0
* Date : 2018/11/12
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
#ifndef __SLEEP_H
#define __SLEEP_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************************************************************
* GLOBAL VARIABLES
*/
/*********************************************************************
* FUNCTIONS
*/
/**
* @brief Configure sleep Wake-up source - RTC wake up, trigger mode
*/
extern void HAL_SleepInit(void);
/**
* @brief Start sleep
*
* @param time - Wake-up time (RTC absolute value)
*
* @return state.
*/
extern uint32_t BLE_LowPower(uint32_t time);
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,137 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : CONFIG.h
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : Configuration description and default value,
* it is recommended to modify the current value in the
* pre-processing of the engineering configuration
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
#ifndef __CONFIG_H
#define __CONFIG_H
#define ID_CH32V208 0x0208
#define CHIP_ID ID_CH32V208
#ifdef WCHBLE_ROM
#include "WCHBLE_ROM.H"
#else
#include "wchble.H"
#endif
#include "ch32v20x.h"
/*********************************************************************
【MAC】
BLE_MAC - 是否自定义蓝牙Mac地址 ( 默认:FALSE - 使用芯片Mac地址 )需要在main.c修改Mac地址定义
【SLEEP】
HAL_SLEEP - 是否开启睡眠功能 ( 默认:FALSE )
WAKE_UP_MAX_TIME_US - 提前唤醒时间,即系统时钟稳定所需要时间
暂停模式 - 45
空闲模式 - 5
【TEMPERATION】
TEM_SAMPLE - 是否打开根据温度变化校准的功能单次校准耗时小于10ms( 默认:TRUE )
【CALIBRATION】
BLE_CALIBRATION_ENABLE - 是否打开定时校准的功能单次校准耗时小于10ms( 默认:TRUE )
BLE_CALIBRATION_PERIOD - 定时校准的周期单位ms( 默认:120000 )
【SNV】
BLE_SNV - 是否开启SNV功能用于储存绑定信息( 默认:TRUE )
BLE_SNV_ADDR - SNV信息保存地址使用data flash最后( 默认:0x77E00 )
BLE_SNV_NUM - SNV信息存储扇区数量等于可存储的绑定数量( 默认:3 )
- 如果配置了SNVNum参数则需要对应修改Lib_Write_Flash函数内擦除的flash大小大小为SNVBlock*SNVNum
【RTC】
CLK_OSC32K - RTC时钟选择如包含主机角色必须使用外部32K( 0 外部(32768Hz),默认:1内部(32000Hz)2内部(32768Hz) )
【MEMORY】
BLE_MEMHEAP_SIZE - 蓝牙协议栈使用的RAM大小不小于6K ( 默认:(1024*6) )
【DATA】
BLE_BUFF_MAX_LEN - 单个连接最大包长度( 默认:27 (ATT_MTU=23),取值范围[27~251] )
BLE_BUFF_NUM - 控制器缓存的包数量( 默认:5 )
BLE_TX_NUM_EVENT - 单个连接事件最多可以发多少个数据包( 默认:1 )
BLE_TX_POWER - 发射功率( 默认:LL_TX_POWEER_0_DBM (0dBm) )
【MULTICONN】
PERIPHERAL_MAX_CONNECTION - 最多可同时做多少从机角色( 默认:1 )
CENTRAL_MAX_CONNECTION - 最多可同时做多少主机角色( 默认:3 )
**********************************************************************/
/*********************************************************************
* 默认配置值
*/
#ifndef BLE_MAC
#define BLE_MAC FALSE
#endif
#ifndef HAL_SLEEP
#define HAL_SLEEP FALSE
#endif
#ifndef WAKE_UP_MAX_TIME_US
#define WAKE_UP_MAX_TIME_US 2400
#endif
#ifndef HAL_KEY
#define HAL_KEY FALSE
#endif
#ifndef HAL_LED
#define HAL_LED FALSE
#endif
#ifndef TEM_SAMPLE
#define TEM_SAMPLE TRUE
#endif
#ifndef BLE_CALIBRATION_ENABLE
#define BLE_CALIBRATION_ENABLE TRUE
#endif
#ifndef BLE_CALIBRATION_PERIOD
#define BLE_CALIBRATION_PERIOD 120000
#endif
#ifndef BLE_SNV
#define BLE_SNV TRUE
#endif
#ifndef BLE_SNV_ADDR
#define BLE_SNV_ADDR 0x08077C00
#endif
#ifndef BLE_SNV_NUM
#define BLE_SNV_NUM 3
#endif
#ifndef CLK_OSC32K
#define CLK_OSC32K 1 // 该项请勿在此修改必须在工程配置里的预处理中修改如包含主机角色必须使用外部32K
#endif
#ifndef BLE_MEMHEAP_SIZE
#define BLE_MEMHEAP_SIZE (1024*7)
#endif
#ifndef BLE_BUFF_MAX_LEN
#define BLE_BUFF_MAX_LEN 100 //27
#endif
#ifndef BLE_BUFF_NUM
#define BLE_BUFF_NUM 5
#endif
#ifndef BLE_TX_NUM_EVENT
#define BLE_TX_NUM_EVENT 1
#endif
#ifndef BLE_TX_POWER
#define BLE_TX_POWER LL_TX_POWEER_7_DBM //LL_TX_POWEER_7_DBM //LL_TX_POWEER_0_DBM
#endif
#ifndef PERIPHERAL_MAX_CONNECTION
#define PERIPHERAL_MAX_CONNECTION 1
#endif
#ifndef CENTRAL_MAX_CONNECTION
#define CENTRAL_MAX_CONNECTION 3
#endif
extern uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
extern const uint8_t MacAddr[6];
#endif