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

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