BLE_TYQ_BJQ_CH584M/HAL/MCU.c

278 lines
7.4 KiB
C
Raw Normal View History

2024-12-03 11:11:30 +08:00
/********************************** (C) COPYRIGHT *******************************
* File Name : MCU.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
2024-12-07 17:09:36 +08:00
* Description : Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BLE<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
2024-12-03 11:11:30 +08:00
*********************************************************************************
* 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.
*******************************************************************************/
/******************************************************************************/
2024-12-07 17:09:36 +08:00
/* ͷ<>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD> */
2024-12-03 11:11:30 +08:00
#include "HAL.h"
tmosTaskID halTaskID;
uint32_t g_LLE_IRQLibHandlerLocation;
/*******************************************************************************
* @fn Lib_Calibration_LSI
*
2024-12-07 17:09:36 +08:00
* @brief <EFBFBD>ڲ<EFBFBD>32kУ׼
2024-12-03 11:11:30 +08:00
*
* @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 CH58x_BLEInit
*
2024-12-07 17:09:36 +08:00
* @brief BLE
2024-12-03 11:11:30 +08:00
*
* @param None.
*
* @return None.
*/
void CH58x_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);
}
2024-12-07 17:09:36 +08:00
SysTick_Config(SysTick_LOAD_RELOAD_Msk);
2024-12-03 11:11:30 +08:00
PFIC_DisableIRQ(SysTick_IRQn);
g_LLE_IRQLibHandlerLocation = (uint32_t)LLE_IRQLibHandler;
PFIC_SetPriority(BLEL_IRQn, 0xF0);
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)
2024-12-07 17:09:36 +08:00
cfg.tsCB = HAL_GetInterTempValue;
2024-12-03 11:11:30 +08:00
#if(CLK_OSC32K)
2024-12-07 17:09:36 +08:00
cfg.rcCB = Lib_Calibration_LSI;
2024-12-03 11:11:30 +08:00
#endif
#endif
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
2024-12-07 17:09:36 +08:00
cfg.idleCB = CH58x_LowPower;
2024-12-03 11:11:30 +08:00
#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++)
{
2024-12-07 17:09:36 +08:00
cfg.MacAddr[i] = MacAddr[i];
PRINT("%02x ", cfg.MacAddr[i]);
2024-12-03 11:11:30 +08:00
}
}
#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
*
2024-12-07 17:09:36 +08:00
* @brief
2024-12-03 11:11:30 +08:00
*
* @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)
2024-12-07 17:09:36 +08:00
{
2024-12-03 11:11:30 +08:00
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)
{
uint8_t x32Kpw;
2024-12-07 17:09:36 +08:00
#if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE)
2024-12-03 11:11:30 +08:00
#ifndef RF_8K
2024-12-07 17:09:36 +08:00
BLE_RegInit();
2024-12-03 11:11:30 +08:00
#endif
#if(CLK_OSC32K)
2024-12-07 17:09:36 +08:00
Lib_Calibration_LSI();
2024-12-03 11:11:30 +08:00
#elif(HAL_SLEEP)
x32Kpw = (R8_XT32K_TUNE & 0xfc) | 0x01;
sys_safe_access_enable();
2024-12-07 17:09:36 +08:00
R8_XT32K_TUNE = x32Kpw;
2024-12-03 11:11:30 +08:00
sys_safe_access_disable();
#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
*
2024-12-07 17:09:36 +08:00
* @brief
2024-12-03 11:11:30 +08:00
*
* @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)
2024-12-07 17:09:36 +08:00
tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, 800);
2024-12-03 11:11:30 +08:00
#endif
2024-12-07 17:09:36 +08:00
// tmos_start_task( halTaskID, HAL_TEST_EVENT, 1600 );
2024-12-03 11:11:30 +08:00
}
/*******************************************************************************
* @fn HAL_GetInterTempValue
*
2024-12-07 17:09:36 +08:00
* @brief
2024-12-03 11:11:30 +08:00
*
2024-12-07 17:09:36 +08:00
* @return
2024-12-03 11:11:30 +08:00
*/
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 ******************************/