3 changed files with 245 additions and 1 deletions
-
153components/fan_group_ctrl_module/fan_group_ctrl_module.cpp
-
91components/fan_group_ctrl_module/fan_group_ctrl_module.hpp
-
2components/zprotocols/zcancmder_v2
@ -0,0 +1,153 @@ |
|||||
|
#include "fan_group_ctrl_module.hpp"
|
||||
|
|
||||
|
#include "sdk\components\zprotocols\zcancmder_v2\api\api.hpp"
|
||||
|
#include "sdk\components\zprotocols\zcancmder_v2\api\errorcode.hpp"
|
||||
|
using namespace iflytop; |
||||
|
#define TAG "FGCM"
|
||||
|
void FanGroupCtrlMoudle::initialize(config_t* pcfg) { |
||||
|
ZASSERT(pcfg->name != nullptr); |
||||
|
ZASSERT(pcfg); |
||||
|
m_mutex.init(); |
||||
|
// ZASSERT(pcfg->fanCtrlTim);
|
||||
|
cfg = *pcfg; |
||||
|
m_enablefbcheck = pcfg->enablefbcheck; |
||||
|
|
||||
|
if (m_enablefbcheck) { |
||||
|
for (int32_t i = 0; i < ZARRAY_SIZE(cfg.fanFBGpioCfg); i++) { |
||||
|
if (cfg.fanFBGpioCfg[i].pin != PinNull) { |
||||
|
m_fanFBGpio[i].initAsInput(cfg.fanFBGpioCfg[i].pin, //
|
||||
|
cfg.fanFBGpioCfg[i].mode, //
|
||||
|
ZGPIO::kIRQ_risingIrq, //
|
||||
|
cfg.fanFBGpioCfg[i].mirror); |
||||
|
m_fanFBGpio[i].regListener([this, i](ZGPIO* GPIO_Pin, ZGPIO::IrqTypeEvent_t irqevent) { onfbgpioirq(i); }); |
||||
|
m_nfanFBGpio++; |
||||
|
} |
||||
|
} |
||||
|
ZASSERT(pcfg->nfan == m_nfanFBGpio); |
||||
|
} |
||||
|
|
||||
|
for (int32_t i = 0; i < ZARRAY_SIZE(cfg.fanPowerGpioCfg); i++) { |
||||
|
if (cfg.fanPowerGpioCfg[i].pin != PinNull) { |
||||
|
m_fanPowerGpio[i].initAsOutput(cfg.fanPowerGpioCfg[i].pin, //
|
||||
|
cfg.fanPowerGpioCfg[i].mode, //
|
||||
|
cfg.fanPowerGpioCfg[i].initLevel, //
|
||||
|
cfg.fanPowerGpioCfg[i].mirror); |
||||
|
m_fanPowerGpio[i].enableTrace(pcfg->enableTrace); |
||||
|
m_nfanPowerPin++; |
||||
|
} |
||||
|
} |
||||
|
cfg.pwm_cfg.calltrace = pcfg->enableTrace; |
||||
|
m_fanCtrlPwm.initialize(&cfg.pwm_cfg); |
||||
|
OSDefaultSchduler::getInstance()->regPeriodJob([this](OSDefaultSchduler::Context&) { checkfanState(); }, 1000); |
||||
|
} |
||||
|
|
||||
|
int32_t FanGroupCtrlMoudle::module_xxx_reg(int32_t param_id, bool read, int32_t& val) { |
||||
|
switch (param_id) { |
||||
|
MODULE_COMMON_PROCESS_REG_CB(); |
||||
|
default: |
||||
|
return err::kmodule_not_find_config_index; |
||||
|
break; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
void FanGroupCtrlMoudle::onfbgpioirq(int32_t fanindex) { |
||||
|
// IRQ Context,ÖжÏÉÏÏÂÎÄ
|
||||
|
m_fanstate[fanindex].fanFBCount++; |
||||
|
m_fanstate[fanindex].lastupdateFanFBCountTime = zos_get_tick(); |
||||
|
} |
||||
|
|
||||
|
int32_t FanGroupCtrlMoudle::startModule(int32_t duty) { |
||||
|
// ZLOGI("startModule duty:%d", duty);
|
||||
|
zlock_guard lck(m_mutex); |
||||
|
if (duty < 20) duty = 20; |
||||
|
fanisworking = true; |
||||
|
|
||||
|
clearError(); |
||||
|
startFanPWM(duty); |
||||
|
powerON(); |
||||
|
m_lastcheckTime = zos_get_tick(); |
||||
|
return 0; |
||||
|
} |
||||
|
int32_t FanGroupCtrlMoudle::stopModule() { |
||||
|
zlock_guard lck(m_mutex); |
||||
|
|
||||
|
fanisworking = false; |
||||
|
stopFanPWM(); |
||||
|
powerOff(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int32_t FanGroupCtrlMoudle::getSubModuleErrorState(int32_t fanindex, int32_t* error_state) { |
||||
|
if (fanindex > 3) { |
||||
|
return err::kfail; |
||||
|
} |
||||
|
|
||||
|
*error_state = m_fanstate[fanindex].fanerrorstate; |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
bool FanGroupCtrlMoudle::isError() { |
||||
|
bool iserror = false; |
||||
|
for (int32_t i = 0; i < cfg.nfan; i++) { |
||||
|
if (m_fanstate[i].fanerrorstate > 0) { |
||||
|
iserror = true; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
return iserror; |
||||
|
} |
||||
|
|
||||
|
void FanGroupCtrlMoudle::checkfanState() { |
||||
|
zlock_guard lck(m_mutex); |
||||
|
if (!m_enablefbcheck) return; |
||||
|
|
||||
|
if (zos_haspassedms(m_lastcheckTime) < 3 * 1000) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
m_lastcheckTime = zos_get_tick(); |
||||
|
for (int32_t i = 0; i < cfg.nfan; i++) { |
||||
|
checkfanState(i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void FanGroupCtrlMoudle::checkfanState(int32_t fanindex) { |
||||
|
if (fanisworking && zos_haspassedms(m_fanstate[fanindex].lastupdateFanFBCountTime) > 10 * 1000) { |
||||
|
m_fanstate[fanindex].fanerrorstate = 1; |
||||
|
ZLOGE(TAG, "%s checkfanState fanindex:%d error", cfg.name, fanindex); |
||||
|
stopModule(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void FanGroupCtrlMoudle::powerON() { |
||||
|
for (int32_t i = 0; i < m_nfanPowerPin; i++) { |
||||
|
m_fanPowerGpio[i].setState(true); |
||||
|
} |
||||
|
} |
||||
|
void FanGroupCtrlMoudle::powerOff() { |
||||
|
for (int32_t i = 0; i < m_nfanPowerPin; i++) { |
||||
|
m_fanPowerGpio[i].setState(false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void FanGroupCtrlMoudle::clearError() { |
||||
|
for (int32_t i = 0; i < cfg.nfan; i++) { |
||||
|
m_fanstate[i].fanerrorstate = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void FanGroupCtrlMoudle::startFanPWM(int32_t duty) { |
||||
|
for (int32_t i = 0; i < ZARRAY_SIZE(cfg.fan0Channel); i++) { |
||||
|
if (cfg.fan0Channel[i] != 0) { |
||||
|
m_fanCtrlPwm.startPWM(cfg.fan0Channel[i], duty); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
void FanGroupCtrlMoudle::stopFanPWM() { |
||||
|
for (int32_t i = 0; i < ZARRAY_SIZE(cfg.fan0Channel); i++) { |
||||
|
if (cfg.fan0Channel[i] != 0) { |
||||
|
m_fanCtrlPwm.stopPWM(cfg.fan0Channel[i]); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,91 @@ |
|||||
|
#pragma once
|
||||
|
/**
|
||||
|
* @file |
||||
|
* @author zhaohe (h_zhaohe@163.com) |
||||
|
* @brief |
||||
|
* @version 0.1 |
||||
|
* @date 2023-04-14 |
||||
|
* |
||||
|
* @copyright Copyright (c) 2023 |
||||
|
* |
||||
|
*/ |
||||
|
#include <stdint.h>
|
||||
|
|
||||
|
#include "sdk/os/zos.hpp"
|
||||
|
#include "sdk\chip\zpwm_generator_muti_channel.hpp"
|
||||
|
#include "sdk\components\zprotocols\zcancmder_v2\api\api.hpp"
|
||||
|
#include "sdk\components\zprotocols\zcancmder_v2\api\zi_module.hpp"
|
||||
|
#include "sdk\os\mutex.hpp"
|
||||
|
//
|
||||
|
|
||||
|
namespace iflytop { |
||||
|
// SUPPORT REG
|
||||
|
|
||||
|
class FanGroupCtrlMoudle : public ZIModule { |
||||
|
ENABLE_MODULE(FanGroupCtrlMoudle, kfan_ctrl_module, 1); |
||||
|
|
||||
|
public: |
||||
|
typedef struct { |
||||
|
// TIM_HandleTypeDef* fanCtrlTim;
|
||||
|
const char* name; |
||||
|
|
||||
|
ZPWMGeneratorMutiChannel::hardware_config_t pwm_cfg; |
||||
|
|
||||
|
int32_t nfan; |
||||
|
int32_t fan0Channel[4]; |
||||
|
ZGPIO::InputGpioCfg_t fanFBGpioCfg[4]; |
||||
|
ZGPIO::OutputGpioCfg_t fanPowerGpioCfg[4]; |
||||
|
bool enablefbcheck; |
||||
|
bool enableTrace; |
||||
|
} config_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
int32_t fanFBCount; |
||||
|
uint32_t lastupdateFanFBCountTime; |
||||
|
int32_t fanerrorstate; |
||||
|
} fanState_t; |
||||
|
|
||||
|
ZPWMGeneratorMutiChannel m_fanCtrlPwm; |
||||
|
|
||||
|
ZGPIO m_fanFBGpio[4]; |
||||
|
int32_t m_nfanFBGpio = 0; |
||||
|
|
||||
|
ZGPIO m_fanPowerGpio[4]; |
||||
|
int32_t m_nfanPowerPin = 0; |
||||
|
|
||||
|
fanState_t m_fanstate[4]; |
||||
|
bool fanisworking = false; |
||||
|
uint32_t m_lastcheckTime = 0; |
||||
|
|
||||
|
config_t cfg; |
||||
|
|
||||
|
bool m_enablefbcheck = false; |
||||
|
zmutex m_mutex; |
||||
|
|
||||
|
public: |
||||
|
FanGroupCtrlMoudle(){}; |
||||
|
|
||||
|
void initialize(config_t* fangroup); |
||||
|
|
||||
|
int32_t startModule(int32_t duty); |
||||
|
int32_t stopModule(); |
||||
|
|
||||
|
int32_t getSubModuleErrorState(int32_t fanindex, int32_t* error_state); |
||||
|
bool isError(); |
||||
|
|
||||
|
private: |
||||
|
void checkfanState(); |
||||
|
void onfbgpioirq(int32_t fanindex); |
||||
|
|
||||
|
private: |
||||
|
void checkfanState(int32_t fanindex); |
||||
|
void powerON(); |
||||
|
void powerOff(); |
||||
|
void clearError(); |
||||
|
void startFanPWM(int32_t duty); |
||||
|
void stopFanPWM(); |
||||
|
|
||||
|
virtual int32_t module_xxx_reg(int32_t param_id, bool read, int32_t& val) override; |
||||
|
}; |
||||
|
|
||||
|
} // namespace iflytop
|
@ -1 +1 @@ |
|||||
Subproject commit d4ad18bf21a66dec4fdbbdcbd078c43df88ca923 |
|
||||
|
Subproject commit 2a6b375ff43a398d758eee8e6323771d73edf3e6 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue