IoT_SCV_CH584M/HAL/MCU.c

287 lines
8.2 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 : 硬件任务处理函数及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"
#include "bsp_uart.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 CH58x_BLEInit
*
* @brief BLE 库初始化
*
* @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);// 配置SysTick并打开中断
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)
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 = CH58x_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);
}
// BLE_Lib 占用了VTF Interrupt 2号和3号
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)
{
uint8_t x32Kpw;
#if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE) // 校准任务单次校准耗时小于10ms
#ifndef RF_8K
BLE_RegInit(); // 校准RF会关闭RF并改变RF相关寄存器如果使用了RF收发函数需注意校准后再重新启用
#endif
#if(CLK_OSC32K)
Lib_Calibration_LSI(); // 校准内部RC
#elif(HAL_SLEEP)
x32Kpw = (R8_XT32K_TUNE & 0xfc) | 0x01;
sys_safe_access_enable();
R8_XT32K_TUNE = x32Kpw; // LSE驱动电流降低到额定电流
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
*
* @brief 硬件初始化
*
* @param None.
*
* @return None.
*/
void HAL_Init()
{
halTaskID = TMOS_ProcessEventRegister(HAL_ProcessEvent);
HAL_TimeInit();
#if 1
// 开启独立看门狗会强制开启内部32K但是开启状态没有跟内部的RC32K寄存器同步
// 而复位时候是根据内部RC32K寄存器状态来决定是否启动RC32K
// 不关闭ch584的内部32KRC
sys_safe_access_enable();
R8_CK32K_CONFIG |= RB_CLK_INT32K_PON;
sys_safe_access_disable();
#endif
#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, 800); // 添加校准任务500ms启动单次校准耗时小于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 ******************************/