From 7beec02dd6cfa28cd5823033fe96b0929d47d394 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 30 Oct 2023 09:47:28 +0800 Subject: [PATCH] update --- .../sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp | 93 +++++++++++++++------- .../sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp | 34 ++++++-- components/zcancmder/zcanreceiver.cpp | 13 +++ components/zcancmder/zcanreceiver.hpp | 6 +- components/zcancmder/zcanreceiver_master.cpp | 14 +++- components/zcancmder/zcanreceiver_master.hpp | 2 + ...o_computer_module_device_script_cmder_paser.cpp | 3 + 7 files changed, 122 insertions(+), 43 deletions(-) diff --git a/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp index 742a146..ba07493 100644 --- a/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp +++ b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp @@ -10,67 +10,63 @@ using namespace std; using namespace iflytop; -#define READ_ADD 0x53 -#define MAX_SIZE (128 * 16) -#define TAG "M24LR64E_I2CEEPROM" -void M24LR64E_I2CEEPROM::initialize(I2C_HandleTypeDef* i2c_handle) { +#define READ_ADD 0x53 +#define MAX_SIZE (128 * 16) +#define SECTOR_SIZE (128) +#define SECTOR_NUM (16) +#define TAG "M24LR64E_I2CEEPROM" +void M24LR64E_I2CEEPROM::initialize(int id, I2C_HandleTypeDef* i2c_handle, ZIEventBusSender* event_bus_sender) { m_i2c_handle = i2c_handle; ZASSERT(m_i2c_handle); m_mutex.init(); m_monitor_thread.init(TAG, 1024, osPriorityNormal); + this->id = id; + + m_event_bus_sender = event_bus_sender; + start_monitor_status(); } -int32_t M24LR64E_I2CEEPROM::start_monitor_status(function cb) { +int32_t M24LR64E_I2CEEPROM::start_monitor_status() { ZLOGI(TAG, "start_monitor_status"); - m_monitor_thread.start([this, cb]() { - eeprom_status_t status; - - m_is_online_flag = isonline(); - status.connected = m_is_online_flag; - if (cb) cb(status); + m_monitor_thread.start([this]() { + // eeprom_status_t status; +#if 1 while (!m_monitor_thread.getExitFlag()) { - /** - * @brief CHECK ONLINE - */ bool is_online = isonline(); - if (is_online != m_is_online_flag) { - ZLOGI(TAG, "detect M24LR64E_I2CEEPROM online status changed: %d", is_online); - m_is_online_flag = is_online; - status.connected = m_is_online_flag; - if (cb) cb(status); - } - + change_is_online_flag(is_online); osDelay(100); } +#endif }); + return 0; }; int32_t M24LR64E_I2CEEPROM::stop_monitor_status() { ZLOGI(TAG, "stop_monitor_status"); m_monitor_thread.stop(); return 0; }; -int32_t M24LR64E_I2CEEPROM::read(u16 sector_index, u16 sector_size, zcancmder_read_ram_ack_t& ack) { +int32_t M24LR64E_I2CEEPROM::module_read_raw(int32_t index, uint8_t* data, int32_t* len) { zlock_guard guard(m_mutex); - if (sector_size > sizeof(ack.packet)) return err::kbuffer_not_enough; + int32_t sector_size = 128; - uint16_t add = sector_index * sector_size; + if (sector_size > *len) return err::kbuffer_not_enough; + + uint16_t add = index * sector_size; if (add >= MAX_SIZE) return err::kparam_out_of_range; if (add + sector_size > MAX_SIZE) { - ack.len = MAX_SIZE - add; - ack.is_end = 1; + *len = MAX_SIZE - add; } else { - ack.len = sector_size; - ack.is_end = 0; + *len = sector_size; } - HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, READ_ADD, add, I2C_MEMADD_SIZE_8BIT, ack.packet, ack.len, 30); + HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, READ_ADD, add, I2C_MEMADD_SIZE_8BIT, data, *len, 30); if (status != HAL_OK) { printf("M24LR64E_I2CEEPROM::read error: %d\n", status); return err::kdevice_offline; } return 0; -}; +} bool M24LR64E_I2CEEPROM::isonline() { zlock_guard guard(m_mutex); @@ -81,5 +77,42 @@ bool M24LR64E_I2CEEPROM::isonline() { } return true; } +int32_t M24LR64E_I2CEEPROM::module_set_reg(int32_t param_id, int32_t param_value) { return module_xxx_reg(param_id, false, param_value); } +int32_t M24LR64E_I2CEEPROM::module_get_reg(int32_t param_id, int32_t* param_value) { return module_xxx_reg(param_id, true, *param_value); } +int32_t M24LR64E_I2CEEPROM::module_xxx_reg(int32_t param_id, bool read, int32_t& val) { + switch (param_id) { + PROCESS_REG(kreg_module_version, /* */ val = 0x0001, ACTION_NONE); + PROCESS_REG(kreg_module_type, /* */ val = 0, ACTION_NONE); + PROCESS_REG(kreg_module_status, /* */ val = read_status(), ACTION_NONE); + PROCESS_REG(kreg_module_errorcode, /* */ val = 0, ACTION_NONE); + PROCESS_REG(kreg_module_initflag, /* */ val = module_get_inited_flag(), module_set_inited_flag(val)); + PROCESS_REG(kreg_module_enableflag, /* */ val = 1, ACTION_NONE); + PROCESS_REG(kreg_module_last_cmd_exec_status, val = 0, ACTION_NONE); + PROCESS_REG(kreg_module_raw_sector_size, /* */ val = SECTOR_SIZE, ACTION_NONE); + PROCESS_REG(kreg_module_raw_sector_num, /* */ val = SECTOR_NUM, ACTION_NONE); + PROCESS_REG(kreg_module_is_online, /* */ val = isonline(), ACTION_NONE); + + default: + return err::kmodule_not_find_config_index; + break; + } + return 0; +} + +void M24LR64E_I2CEEPROM::change_is_online_flag(bool state) { + if (m_is_online_flag != state) { + m_is_online_flag = state; + if (m_event_bus_sender) { + m_event_bus_sender->push_reg_state_change_event(id, kreg_module_is_online, m_is_online_flag); + } + } +} + +int32_t M24LR64E_I2CEEPROM::read_status() { + if (!m_monitor_thread.isworking()) { + return 0; + } + return 1; +} #endif diff --git a/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp index eab2e50..94c6268 100644 --- a/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp +++ b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp @@ -1,27 +1,47 @@ -#include "sdk\components\zprotocols\zcancmder\api\i_eeprom.hpp" +#include "sdk\components\zprotocols\zcancmder_v2\api\api.hpp" #include "sdk\os\zos.hpp" #ifdef HAL_I2C_MODULE_ENABLED namespace iflytop { using namespace std; -class M24LR64E_I2CEEPROM : public I_EEPROMModule { +class M24LR64E_I2CEEPROM : public ZIModule { I2C_HandleTypeDef* m_i2c_handle; ZThread m_monitor_thread; bool m_is_online_flag = false; - zmutex m_mutex; + zmutex m_mutex; + ZIEventBusSender* m_event_bus_sender; + + int32_t id = 0; public: M24LR64E_I2CEEPROM(){}; ~M24LR64E_I2CEEPROM(){}; - void initialize(I2C_HandleTypeDef* i2c_handle); + void initialize(int id, I2C_HandleTypeDef* i2c_handle, ZIEventBusSender* event_bus_sender); + + public: + int32_t start_monitor_status(); + int32_t stop_monitor_status(); + int32_t code_scaner_read_scaner_result(int32_t startadd, uint8_t* data, int32_t* len); + + public: + virtual int32_t getid(int32_t* id) override { + *id = this->id; + return 0; + } + + virtual int32_t module_set_reg(int32_t param_id, int32_t param_value); + virtual int32_t module_get_reg(int32_t param_id, int32_t* param_value); - int32_t start_monitor_status(function cb) override; - int32_t stop_monitor_status() override; - int32_t read(u16 sector_index, u16 sector_size, zcancmder_read_ram_ack_t& ack) override; + virtual int32_t module_read_raw(int32_t index, uint8_t* data, int32_t* len); + + private: + int32_t module_xxx_reg(int32_t param_id, bool read, int32_t& param_value); + int32_t read_status(); private: bool isonline(); + void change_is_online_flag(bool state); }; } // namespace iflytop diff --git a/components/zcancmder/zcanreceiver.cpp b/components/zcancmder/zcanreceiver.cpp index 28c0bc1..1584f0b 100644 --- a/components/zcancmder/zcanreceiver.cpp +++ b/components/zcancmder/zcanreceiver.cpp @@ -183,6 +183,19 @@ int32_t ZCanCmder::sendBufAck(zcr_cmd_header_t *rx_cmd_header, uint8_t *data, in sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len); return 0; } +int32_t ZCanCmder::triggerEvent(zcr_cmd_header_t *rx_cmd_header, uint8_t *data, int32_t len) { + zlock_guard l(m_lock); + m_reportIndex++; + ZASSERT(sizeof(txbuff) > sizeof(zcr_cmd_header_t) + len); + zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff; + memcpy(txheader, rx_cmd_header, sizeof(zcr_cmd_header_t)); + txheader->packetType = kptv2_event; + txheader->packetindex = m_reportIndex; + memcpy(txheader->data, data, len); + sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len); + return 0; +} + int32_t ZCanCmder::sendAck(zcr_cmd_header_t *rx_cmd_header, int32_t *ackvar, int32_t nack) { zlock_guard l(m_lock); zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff; diff --git a/components/zcancmder/zcanreceiver.hpp b/components/zcancmder/zcanreceiver.hpp index 3792cb8..22408a4 100644 --- a/components/zcancmder/zcanreceiver.hpp +++ b/components/zcancmder/zcanreceiver.hpp @@ -54,12 +54,13 @@ class ZCanCmder : public ZCanIRQListener, public IZCanCmder { int txPacketInterval_ms = 0; zmutex m_lock; + int32_t m_reportIndex = 0; + public: ZCanCmder() {} CFG *createCFG(uint8_t deviceId); void init(CFG *cfg); - #if 0 void sendExecStatusReport(zcr_cmd_header_t *rxcmdheader, uint8_t *data, size_t len); void sendStatusReport(zcr_cmd_header_t *rxcmdheader, uint8_t *data, size_t len); @@ -70,10 +71,11 @@ class ZCanCmder : public ZCanIRQListener, public IZCanCmder { uint8_t getDeviceId() { return m_config->deviceId; } void setTxPacketInterval(int interval_ms) { txPacketInterval_ms = interval_ms; } - virtual void registerListener(IZcanCmderListener *listener) override; + virtual void registerListener(IZcanCmderListener *listener) override; virtual int32_t sendBufAck(zcr_cmd_header_t *rx_cmd_header, uint8_t *data, int32_t len) override; virtual int32_t sendAck(zcr_cmd_header_t *rx_cmd_header, int32_t *ackvar, int32_t nack) override; virtual int32_t sendErrorAck(zcr_cmd_header_t *rx_cmd_header, int32_t errorcode) override; + virtual int32_t triggerEvent(zcr_cmd_header_t *rx_cmd_header, uint8_t *data, int32_t len) override; void loop(); diff --git a/components/zcancmder/zcanreceiver_master.cpp b/components/zcancmder/zcanreceiver_master.cpp index 7f8ac7e..c04d16e 100644 --- a/components/zcancmder/zcanreceiver_master.cpp +++ b/components/zcancmder/zcanreceiver_master.cpp @@ -227,6 +227,8 @@ int32_t ZCanCommnaderMaster::sendCmd(int32_t cmdid, int32_t submoduleid, int32_t return errocode; } +void ZCanCommnaderMaster::regEventPacketListener(function on_event) { m_on_event = on_event; } + void ZCanCommnaderMaster::regListener(uint16_t index, zcan_commnader_master_onpacket_t onack) { zlock_guard l(m_on_packet_map_lock); if (m_on_packet_map.size() > 10000) { @@ -261,13 +263,17 @@ bool ZCanCommnaderMaster::isListenerReg(uint16_t index) { void ZCanCommnaderMaster::callListener(CanPacketRxBuffer *report) { uint16_t index = report->get_cmdheader()->packetindex; - { + if (report->get_cmdheader()->packetType == kptv2_ack || report->get_cmdheader()->packetType == kptv2_error_ack) { zlock_guard l(m_on_packet_map_lock); auto it = m_on_packet_map.find(index); if (it != m_on_packet_map.end()) { - if (report->get_cmdheader()->packetType == kptv2_ack || report->get_cmdheader()->packetType == kptv2_error_ack) { - if (it->second.on_ack) it->second.on_ack(report); - } + if (it->second.on_ack) it->second.on_ack(report); + } + } + + if (report->get_cmdheader()->packetType == kptv2_event) { + if (m_on_event) { + m_on_event(report->get_data(), report->get_datalen()); } } } diff --git a/components/zcancmder/zcanreceiver_master.hpp b/components/zcancmder/zcanreceiver_master.hpp index e2447fe..4f3c012 100644 --- a/components/zcancmder/zcanreceiver_master.hpp +++ b/components/zcancmder/zcanreceiver_master.hpp @@ -56,6 +56,7 @@ class ZCanCommnaderMaster : public ZCanIRQListener, public IZcanCmderMaster { map m_on_packet_map; zmutex m_on_packet_map_lock; uint16_t m_index_off = 0; + function m_on_event; zmutex txlock; @@ -67,6 +68,7 @@ class ZCanCommnaderMaster : public ZCanIRQListener, public IZcanCmderMaster { void setTxPacketInterval(int interval_ms) { txPacketInterval_ms = interval_ms; } virtual int32_t sendCmd(int32_t cmdid, int32_t moduleid, int32_t *param, size_t npara, int32_t *ack, size_t nack, int overtime_ms) override; virtual int32_t sendCmdAndReceiveBuf(int32_t cmdid, int32_t moduleid, int32_t *param, size_t npara, uint8_t *ack, int32_t *rxsize, int overtime_ms) override; + virtual void regEventPacketListener(function on_event) override; public: virtual void STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *can); diff --git a/components/zprotocol_helper/micro_computer_module_device_script_cmder_paser.cpp b/components/zprotocol_helper/micro_computer_module_device_script_cmder_paser.cpp index 941203b..13019e1 100644 --- a/components/zprotocol_helper/micro_computer_module_device_script_cmder_paser.cpp +++ b/components/zprotocol_helper/micro_computer_module_device_script_cmder_paser.cpp @@ -232,4 +232,7 @@ void MicroComputerModuleDeviceScriptCmderPaser::initialize(ICmdParser* cancmder, m_deviceManager = deviceManager; cancmder->regCMD("dumpreg", "dumpreg (mid)", 1, [this](int32_t paramN, const char* paraV[], ICmdParserACK* ack) { do_dumpreg(paramN, paraV, ack); }); + deviceManager->regOnRegValChangeEvent([this](int32_t moduleid, int32_t event_id, int32_t eventval) { // + ZLOGI(TAG, "onRegValChangeEvent(%d,%d,%d)", moduleid, event_id, eventval); + }); }