sdk: copy BLE/HAL

This commit is contained in:
Richard Goulter 2023-12-19 22:09:01 +07:00
parent a9c7ea1797
commit 8d00533724
11 changed files with 1568 additions and 0 deletions

137
sdk/BLE/HAL/KEY.c Normal file
View File

@ -0,0 +1,137 @@
/********************************** (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.
*******************************************************************************/
/******************************************************************************/
/* 头文件包含 */
#include "HAL.h"
/**************************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************************/
static uint8_t halKeySavedKeys; /* 保留按键最后的状态,用于查询是否有键值变化 */
/**************************************************************************************************
* 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;
KEY1_DIR;
KEY1_PU;
KEY2_DIR;
KEY2_PU;
}
/**************************************************************************************************
* @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())
{ //读按键1
keys |= HAL_KEY_SW_1;
}
if(HAL_PUSH_BUTTON2())
{ //读按键1
keys |= HAL_KEY_SW_2;
}
if(HAL_PUSH_BUTTON3())
{ //读按键1
keys |= HAL_KEY_SW_3;
}
if(HAL_PUSH_BUTTON4())
{ //读按键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 ******************************/

361
sdk/BLE/HAL/LED.c Normal file
View File

@ -0,0 +1,361 @@
/********************************** (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.
*******************************************************************************/
/******************************************************************************/
/* 头文件包含 */
#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 */
LED1_DDR;
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 ******************************/

265
sdk/BLE/HAL/MCU.c Normal file
View File

@ -0,0 +1,265 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : MCU.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : BLE和硬件初始化
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* 头文件包含 */
#include "HAL.h"
tmosTaskID halTaskID;
uint32_t g_LLE_IRQLibHandlerLocation;
/*******************************************************************************
* @fn Lib_Calibration_LSI
*
* @brief 32k校准
*
* @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 - Read start address
* @param num - Number of units to read (unit: 4 bytes)
* @param pBuf - Buffer to store read data
*
* @return None.
*/
uint32_t Lib_Read_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
{
EEPROM_READ(addr, pBuf, num * 4);
return 0;
}
/*******************************************************************************
* @fn Lib_Write_Flash
*
* @brief Callback function used for BLE lib.
*
* @param addr - Write start address
* @param num - Number of units to write (unit: 4 bytes)
* @param pBuf - Buffer with data to be written
*
* @return None.
*/
uint32_t Lib_Write_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
{
EEPROM_ERASE(addr, num * 4);
EEPROM_WRITE(addr, pBuf, num * 4);
return 0;
}
#endif
/*******************************************************************************
* @fn CH59x_BLEInit
*
* @brief BLE
*
* @param None.
*
* @return None.
*/
void CH59x_BLEInit(void)
{
uint8_t i;
bleConfig_t cfg;
if(tmos_memcmp(VER_LIB, VER_FILE, strlen(VER_FILE)) == FALSE)
{
PRINT("head file error...\n");
while(1);
}
SysTick_Config(SysTick_LOAD_RELOAD_Msk);
PFIC_DisableIRQ(SysTick_IRQn);
g_LLE_IRQLibHandlerLocation = (uint32_t)LLE_IRQLibHandler;
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)
if((BLE_SNV_ADDR + BLE_SNV_BLOCK * BLE_SNV_NUM) > (0x78000 - FLASH_ROM_MAX_SIZE))
{
PRINT("SNV config error...\n");
while(1);
}
cfg.SNVAddr = (uint32_t)BLE_SNV_ADDR;
cfg.SNVBlock = (uint32_t)BLE_SNV_BLOCK;
cfg.SNVNum = (uint32_t)BLE_SNV_NUM;
cfg.readFlashCB = Lib_Read_Flash;
cfg.writeFlashCB = Lib_Write_Flash;
#endif
cfg.ConnectNumber = (PERIPHERAL_MAX_CONNECTION & 3) | (CENTRAL_MAX_CONNECTION << 2);
cfg.srandCB = SYS_GetSysTickCnt;
#if(defined TEM_SAMPLE) && (TEM_SAMPLE == TRUE)
cfg.tsCB = HAL_GetInterTempValue; // 根据温度变化校准RF和内部RC( 大于7摄氏度 )
#if(CLK_OSC32K)
cfg.rcCB = Lib_Calibration_LSI; // 内部32K时钟校准
#endif
#endif
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
cfg.idleCB = CH59x_LowPower; // 启用睡眠
#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];
GetMACAddress(MacAddr);
for(i = 0; i < 6; i++)
{
cfg.MacAddr[i] = MacAddr[i]; // 使用芯片mac地址
}
}
#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);
}
}
/*******************************************************************************
* @fn HAL_ProcessEvent
*
* @brief
*
* @param task_id - The TMOS assigned task ID.
* @param events - events to process. This is a bit map and can
* contain more than one event.
*
* @return events.
*/
tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events)
{
uint8_t *msgPtr;
if(events & SYS_EVENT_MSG)
{ // 处理HAL层消息调用tmos_msg_receive读取消息处理完成后删除消息。
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) // 校准任务单次校准耗时小于10ms
BLE_RegInit(); // 校准RF
#if(CLK_OSC32K)
Lib_Calibration_LSI(); // 校准内部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)
tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD)); // 添加校准任务单次校准耗时小于10ms
#endif
// tmos_start_task( halTaskID, HAL_TEST_EVENT, 1600 ); // 添加一个测试任务
}
/*******************************************************************************
* @fn HAL_GetInterTempValue
*
* @brief 使ADC中断采样.
*
* @return .
*/
uint16_t HAL_GetInterTempValue(void)
{
uint8_t sensor, channel, config, tkey_cfg;
uint16_t adc_data;
tkey_cfg = R8_TKEY_CFG;
sensor = R8_TEM_SENSOR;
channel = R8_ADC_CHANNEL;
config = R8_ADC_CFG;
ADC_InterTSSampInit();
R8_ADC_CONVERT |= RB_ADC_START;
while(R8_ADC_CONVERT & RB_ADC_START);
adc_data = R16_ADC_DATA;
R8_TEM_SENSOR = sensor;
R8_ADC_CHANNEL = channel;
R8_ADC_CFG = config;
R8_TKEY_CFG = tkey_cfg;
return (adc_data);
}
/******************************** endfile @ mcu ******************************/

120
sdk/BLE/HAL/RTC.c Normal file
View File

@ -0,0 +1,120 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : RTC.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : RTC配置及其初始化
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* 头文件包含 */
#include "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 RTC触发时间
*
* @param time - .
*
* @return None.
*/
void RTC_SetTignTime(uint32_t time)
{
sys_safe_access_enable();
R32_RTC_TRIG = time;
sys_safe_access_disable();
RTCTigFlag = 0;
}
/*******************************************************************************
* @fn RTC_IRQHandler
*
* @brief RTC中断处理
*
* @param None.
*
* @return None.
*/
__INTERRUPT
__HIGH_CODE
void RTC_IRQHandler(void)
{
R8_RTC_FLAG_CTRL = (RB_RTC_TMR_CLR | RB_RTC_TRIG_CLR);
RTCTigFlag = 1;
}
/*******************************************************************************
* @fn SYS_GetClockValue
*
* @brief RTC当前计数值
*
* @param None.
*
* @return None.
*/
__HIGH_CODE
static uint32_t SYS_GetClockValue(void)
{
volatile uint32_t i;
do
{
i = R32_RTC_CNT_32K;
} while(i != R32_RTC_CNT_32K);
return (i);
}
/*******************************************************************************
* @fn HAL_Time0Init
*
* @brief
*
* @param None.
*
* @return None.
*/
void HAL_TimeInit(void)
{
bleClockConfig_t conf;
#if(CLK_OSC32K)
sys_safe_access_enable();
R8_CK32K_CONFIG &= ~(RB_CLK_OSC32K_XT | RB_CLK_XT32K_PON);
sys_safe_access_disable();
sys_safe_access_enable();
R8_CK32K_CONFIG |= RB_CLK_INT32K_PON;
sys_safe_access_disable();
Lib_Calibration_LSI();
#else
sys_safe_access_enable();
R8_CK32K_CONFIG |= RB_CLK_OSC32K_XT | RB_CLK_INT32K_PON | RB_CLK_XT32K_PON;
sys_safe_access_disable();
#endif
RTC_InitTime(2020, 1, 1, 0, 0, 0); //RTC时钟初始化当前时间
tmos_memset( &conf, 0, sizeof(bleClockConfig_t) );
conf.ClockAccuracy = CLK_OSC32K ? 1000 : 50;
conf.ClockFrequency = CAB_LSIFQ;
conf.ClockMaxCount = RTC_MAX_COUNT;
conf.getClockValue = SYS_GetClockValue;
TMOS_TimerInit( &conf );
}
/******************************** endfile @ time ******************************/

97
sdk/BLE/HAL/SLEEP.c Normal file
View File

@ -0,0 +1,97 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : SLEEP.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.
*******************************************************************************/
/******************************************************************************/
/* 头文件包含 */
#include "HAL.h"
/*******************************************************************************
* @fn CH59x_LowPower
*
* @brief
*
* @param time - RTC绝对值
*
* @return state.
*/
uint32_t CH59x_LowPower(uint32_t time)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
volatile uint32_t i;
uint32_t time_sleep, time_curr;
unsigned long irq_status;
// 提前唤醒
if (time <= WAKE_UP_RTC_MAX_TIME) {
time = time + (RTC_MAX_COUNT - WAKE_UP_RTC_MAX_TIME);
} else {
time = time - WAKE_UP_RTC_MAX_TIME;
}
SYS_DisableAllIrq(&irq_status);
time_curr = RTC_GetCycle32k();
// 检测睡眠时间
if (time < time_curr) {
time_sleep = time + (RTC_MAX_COUNT - time_curr);
} else {
time_sleep = time - time_curr;
}
// 若睡眠时间小于最小睡眠时间或大于最大睡眠时间,则不睡眠
if ((time_sleep < SLEEP_RTC_MIN_TIME) ||
(time_sleep > SLEEP_RTC_MAX_TIME)) {
SYS_RecoverIrq(irq_status);
return 2;
}
RTC_SetTignTime(time);
SYS_RecoverIrq(irq_status);
#if(DEBUG == Debug_UART1) // 使用其他串口输出打印信息需要修改这行代码
while((R8_UART1_LSR & RB_LSR_TX_ALL_EMP) == 0)
{
__nop();
}
#endif
// LOW POWER-sleep模式
if(!RTCTigFlag)
{
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
return 3;
}
/*******************************************************************************
* @fn HAL_SleepInit
*
* @brief - RTC唤醒
*
* @param None.
*
* @return None.
*/
void HAL_SleepInit(void)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
sys_safe_access_enable();
R8_SLP_WAKE_CTRL |= RB_SLP_RTC_WAKE; // RTC唤醒
sys_safe_access_disable();
sys_safe_access_enable();
R8_RTC_MODE_CTRL |= RB_RTC_TRIG_EN; // 触发模式
sys_safe_access_disable();
PFIC_EnableIRQ(RTC_IRQn);
#endif
}

View File

@ -0,0 +1,152 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : CONFIG.h
* 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.
*******************************************************************************/
/******************************************************************************/
#ifndef __CONFIG_H
#define __CONFIG_H
#define ID_CH592 0x92
#define CHIP_ID ID_CH592
#ifdef CH59xBLE_ROM
#include "CH59xBLE_ROM.h"
#else
#include "CH59xBLE_LIB.h"
#endif
#include "CH59x_common.h"
/*********************************************************************
MAC
BLE_MAC - Mac地址 ( :FALSE - 使Mac地址 )main.c修改Mac地址定义
DCDC
DCDC_ENABLE - 使DCDC ( :FALSE )
SLEEP
HAL_SLEEP - ( :FALSE )
SLEEP_RTC_MIN_TIME - RTC周期
SLEEP_RTC_MAX_TIME - RTC周期
WAKE_UP_RTC_MAX_TIME - 32M晶振稳定时间RTC周期
/ - 45 ()
- 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最后512字节( :0x77E00 )
BLE_SNV_BLOCK - SNV信息保存块大小( :256 )
BLE_SNV_NUM - SNV信息保存数量( :1 )
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~516] )
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 DCDC_ENABLE
#define DCDC_ENABLE FALSE
#endif
#ifndef HAL_SLEEP
#define HAL_SLEEP FALSE
#endif
#ifndef SLEEP_RTC_MIN_TIME
#define SLEEP_RTC_MIN_TIME US_TO_RTC(1000)
#endif
#ifndef SLEEP_RTC_MAX_TIME
#define SLEEP_RTC_MAX_TIME (RTC_MAX_COUNT - 1000 * 1000 * 30)
#endif
#ifndef WAKE_UP_RTC_MAX_TIME
#define WAKE_UP_RTC_MAX_TIME US_TO_RTC(1600)
#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 0x77E00-FLASH_ROM_MAX_SIZE
#endif
#ifndef BLE_SNV_BLOCK
#define BLE_SNV_BLOCK 256
#endif
#ifndef BLE_SNV_NUM
#define BLE_SNV_NUM 1
#endif
#ifndef CLK_OSC32K
#define CLK_OSC32K 1 // 该项请勿在此修改必须在工程配置里的预处理中修改如包含主机角色必须使用外部32K
#endif
#ifndef BLE_MEMHEAP_SIZE
#define BLE_MEMHEAP_SIZE (1024*6)
#endif
#ifndef BLE_BUFF_MAX_LEN
#define BLE_BUFF_MAX_LEN 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_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

80
sdk/BLE/HAL/include/HAL.h Normal file
View File

@ -0,0 +1,80 @@
/********************************** (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 "LED.h"
#include "KEY.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
*/
extern void HAL_Init(void);
/**
* @brief
*
* @param task_id - The TMOS assigned task ID.
* @param events - events to process. This is a bit map and can
* contain more than one event.
*/
extern tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events);
/**
* @brief BLE
*/
extern void CH59x_BLEInit(void);
/**
* @brief 使ADC中断采样.
*
* @return .
*/
extern uint16_t HAL_GetInterTempValue(void);
/**
* @brief 32k校准
*/
extern void Lib_Calibration_LSI(void);
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif

112
sdk/BLE/HAL/include/KEY.h Normal file
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
/* 按键定义 */
/* 1 - KEY */
#define KEY1_BV BV(22)
#define KEY2_BV BV(4)
#define KEY3_BV ()
#define KEY4_BV ()
#define KEY1_PU (R32_PB_PU |= KEY1_BV)
#define KEY2_PU (R32_PB_PU |= KEY2_BV)
#define KEY3_PU ()
#define KEY4_PU ()
#define KEY1_DIR (R32_PB_DIR &= ~KEY1_BV)
#define KEY2_DIR (R32_PB_DIR &= ~KEY2_BV)
#define KEY3_DIR ()
#define KEY4_DIR ()
#define KEY1_IN (ACTIVE_LOW(R32_PB_PIN & KEY1_BV))
#define KEY2_IN (ACTIVE_LOW(R32_PB_PIN & KEY2_BV))
#define KEY3_IN ()
#define KEY4_IN ()
#define HAL_PUSH_BUTTON1() (KEY1_IN) //添加自定义按键
#define HAL_PUSH_BUTTON2() (KEY2_IN)
#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

134
sdk/BLE/HAL/include/LED.h Normal file
View File

@ -0,0 +1,134 @@
/********************************** (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
*/
/* 连接一个LED用于监控演示程序的进度,低电平LED亮 */
/* 1 - LED */
#define LED1_BV BV(15)
#define LED2_BV
#define LED3_BV
#define LED1_OUT (R32_PB_OUT)
#define LED2_OUT 0
#define LED3_OUT 0
#define LED4_OUT 0
#define LED1_DDR (R32_PB_DIR |= LED1_BV)
#define LED2_DDR 0
#define LED3_DDR 0
#define HAL_TURN_OFF_LED1() (LED1_OUT |= LED1_BV)
#define HAL_TURN_OFF_LED2()
#define HAL_TURN_OFF_LED3()
#define HAL_TURN_OFF_LED4()
#define HAL_TURN_ON_LED1() (LED1_OUT &= (~LED1_BV))
#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

60
sdk/BLE/HAL/include/RTC.h Normal file
View File

@ -0,0 +1,60 @@
/********************************** (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
#ifdef CLK_OSC32K
#if (CLK_OSC32K==1)
#define FREQ_RTC 32000
#else
#define FREQ_RTC 32768
#endif
#endif /* CLK_OSC32K */
#define CLK_PER_US (1.0 / ((1.0 / FREQ_RTC) * 1000 * 1000))
#define CLK_PER_MS (CLK_PER_US * 1000)
#define US_PER_CLK (1.0 / CLK_PER_US)
#define MS_PER_CLK (US_PER_CLK / 1000.0)
#define RTC_TO_US(clk) ((uint32_t)((clk) * US_PER_CLK + 0.5))
#define RTC_TO_MS(clk) ((uint32_t)((clk) * MS_PER_CLK + 0.5))
#define US_TO_RTC(us) ((uint32_t)((us) * CLK_PER_US + 0.5))
#define MS_TO_RTC(ms) ((uint32_t)((ms) * CLK_PER_MS + 0.5))
extern volatile uint32_t RTCTigFlag;
/**
* @brief Initialize time Service.
*/
void HAL_TimeInit(void);
/**
* @brief RTC触发时间
*
* @param 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 - RTC唤醒
*/
extern void HAL_SleepInit(void);
/**
* @brief
*
* @param time - RTC绝对值
*
* @return state.
*/
extern uint32_t CH59x_LowPower(uint32_t time);
/*********************************************************************
*********************************************************************/
#ifdef __cplusplus
}
#endif
#endif