2022-03-26 20:51:40 +08:00

259 lines
6.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************** (C) COPYRIGHT *******************************
* File Name : MCU.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : Ӳ<><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BLE<4C><45>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
/******************************************************************************/
/* ͷ<>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD> */
#include "HAL.h"
tmosTaskID halTaskID;
/*******************************************************************************
* @fn Lib_Calibration_LSI
*
* @brief <20>ڲ<EFBFBD>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 Lib <20><><EFBFBD><EFBFBD>Flash<73>ص<EFBFBD>
*
* @param addr.
* @param num.
* @param pBuf.
*
* @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 Lib <20><><EFBFBD><EFBFBD>Flash<73>ص<EFBFBD>
*
* @param addr.
* @param num.
* @param pBuf.
*
* @return None.
*/
uint32_t Lib_Write_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
{
EEPROM_ERASE(addr, EEPROM_PAGE_SIZE * 2);
EEPROM_WRITE(addr, pBuf, num * 4);
return 0;
}
#endif
/*******************************************************************************
* @fn CH57X_BLEInit
*
* @brief BLE <20><><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
*
* @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);
}
SysTick_Config(SysTick_LOAD_RELOAD_Msk);
PFIC_DisableIRQ(SysTick_IRQn);
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.readFlashCB = Lib_Read_Flash;
cfg.writeFlashCB = Lib_Write_Flash;
#endif
#if(CLK_OSC32K)
cfg.SelRTCClock = (uint32_t)CLK_OSC32K;
#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; // <20><><EFBFBD><EFBFBD><EFBFBD>¶ȱ仯У׼RF<52><46><EFBFBD>ڲ<EFBFBD>RC( <20><><EFBFBD><EFBFBD>7<EFBFBD><37><EFBFBD>϶<EFBFBD> )
#if(CLK_OSC32K)
cfg.rcCB = Lib_Calibration_LSI; // <20>ڲ<EFBFBD>32Kʱ<4B><CAB1>У׼
#endif
#endif
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
cfg.WakeUpTime = WAKE_UP_RTC_MAX_TIME;
cfg.sleepCB = CH58X_LowPower; // <20><><EFBFBD><EFBFBD>˯<EFBFBD><CBAF>
#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]; // ʹ<><CAB9>оƬmac<61><63>ַ
}
}
#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 Ӳ<><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @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)
{ // <20><><EFBFBD><EFBFBD>HAL<41><4C><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>tmos_msg_receive<76><65>ȡ<EFBFBD><C8A1>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>
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) // У׼<D0A3><D7BC><EFBFBD>񣬵<EFBFBD><F1A3ACB5><EFBFBD>У׼<D0A3><D7BC>ʱС<CAB1><D0A1>10ms
BLE_RegInit(); // У׼RF
#if(CLK_OSC32K)
Lib_Calibration_LSI(); // У׼<D0A3>ڲ<EFBFBD>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 Ӳ<><D3B2><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
*
* @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)); // <20><><EFBFBD><EFBFBD>У׼<D0A3><D7BC><EFBFBD>񣬵<EFBFBD><F1A3ACB5><EFBFBD>У׼<D0A3><D7BC>ʱС<CAB1><D0A1>10ms
#endif
// tmos_start_task( halTaskID, HAL_TEST_EVENT, 1600 ); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/*******************************************************************************
* @fn HAL_GetInterTempValue
*
* @brief <20><>ȡ<EFBFBD>ڲ<EFBFBD><DAB2>¸в<C2B8><D0B2><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>ADC<44>жϲ<D0B6><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ˺<DAB4><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>.
*
* @return <20>ڲ<EFBFBD><DAB2>¸в<C2B8><D0B2><EFBFBD>ֵ.
*/
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 ******************************/