diff --git a/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp new file mode 100644 index 0000000..3677690 --- /dev/null +++ b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp @@ -0,0 +1,85 @@ +#include "m24lr64e_i2c_eeprom.hpp" + +#include +#include + +#include "sdk\components\zprotocols\errorcode\errorcode.hpp" + +#ifdef HAL_I2C_MODULE_ENABLED + +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) { + m_i2c_handle = i2c_handle; + ZASSERT(m_i2c_handle); + m_mutex.init(); + m_monitor_thread.init(TAG, 1024, osPriorityNormal); +} + +int32_t M24LR64E_I2CEEPROM::start_monitor_status(function cb) { + 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); + + 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) (status); + } + + osDelay(100); + } + }); +}; +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) { + zlock_guard guard(m_mutex); + if (sector_size > sizeof(ack.packet)) return err::kce_buffer_not_enough; + + uint16_t add = sector_index * sector_size; + if (add >= MAX_SIZE) return err::kce_param_out_of_range; + + if (add + sector_size > MAX_SIZE) { + ack.len = MAX_SIZE - add; + ack.is_end = 1; + } else { + ack.len = sector_size; + ack.is_end = 0; + } + HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, READ_ADD, add, I2C_MEMADD_SIZE_8BIT, ack.packet, ack.len, 30); + if (status != HAL_OK) { + printf("M24LR64E_I2CEEPROM::read error: %d\n", status); + return err::kce_device_offline; + } + return 0; +}; + +bool M24LR64E_I2CEEPROM::isonline() { + zlock_guard guard(m_mutex); + uint8_t data[2]; + HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, READ_ADD, 0, I2C_MEMADD_SIZE_8BIT, data, 2, 5); + if (status != HAL_OK) { + return false; + } + return true; +} + +#endif \ No newline at end of file diff --git a/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp new file mode 100644 index 0000000..eab2e50 --- /dev/null +++ b/components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp @@ -0,0 +1,28 @@ +#include "sdk\components\zprotocols\zcancmder\api\i_eeprom.hpp" +#include "sdk\os\zos.hpp" + +#ifdef HAL_I2C_MODULE_ENABLED +namespace iflytop { +using namespace std; +class M24LR64E_I2CEEPROM : public I_EEPROMModule { + I2C_HandleTypeDef* m_i2c_handle; + ZThread m_monitor_thread; + bool m_is_online_flag = false; + zmutex m_mutex; + + public: + M24LR64E_I2CEEPROM(){}; + ~M24LR64E_I2CEEPROM(){}; + + void initialize(I2C_HandleTypeDef* i2c_handle); + + 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; + + private: + bool isonline(); +}; +} // namespace iflytop + +#endif diff --git a/components/zcancmder/zcanreceiver.cpp b/components/zcancmder/zcanreceiver.cpp index cba0a07..ed7c58f 100644 --- a/components/zcancmder/zcanreceiver.cpp +++ b/components/zcancmder/zcanreceiver.cpp @@ -183,6 +183,13 @@ void ZCanCmder::sendExecStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, si memcpy(txheader->data, data, len); sendPacket(txbuff, sizeof(Cmdheader_t) + len); } +void ZCanCmder::sendStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len) { + Cmdheader_t *txheader = (Cmdheader_t *)txbuff; + memcpy(txheader, rxcmdheader, sizeof(Cmdheader_t)); + txheader->packetType = kpt_report; + memcpy(txheader->data, data, len); + sendPacket(txbuff, sizeof(Cmdheader_t) + len); +} void ZCanCmder::sendErrorAck(Cmdheader_t *cmdheader, uint16_t id, uint32_t errcode) { Cmdheader_t *txheader = (Cmdheader_t *)txbuff; diff --git a/components/zcancmder/zcanreceiver.hpp b/components/zcancmder/zcanreceiver.hpp index e9d84d6..7d9635e 100644 --- a/components/zcancmder/zcanreceiver.hpp +++ b/components/zcancmder/zcanreceiver.hpp @@ -104,6 +104,7 @@ class ZCanCmder : public ZCanIRQListener { void sendPacket(uint8_t *packet, size_t len); void sendExecStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); + void sendStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); void sendAck(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); void sendErrorAck(Cmdheader_t *cmdheader, uint16_t id, uint32_t errcode); bool sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems); diff --git a/components/zcancmder_module/zcan_eeprom_module.cpp b/components/zcancmder_module/zcan_eeprom_module.cpp new file mode 100644 index 0000000..19c8b38 --- /dev/null +++ b/components/zcancmder_module/zcan_eeprom_module.cpp @@ -0,0 +1,36 @@ +#include "zcan_eeprom_module.hpp" +using namespace iflytop; + +#define TAG "ZCanEepromModule" +#if 1 +void ZCanEepromModule::initialize(ZCanCmder* cancmder, int id, I_EEPROMModule* module) { + m_cancmder = cancmder; + m_id = id; + m_module = module; + m_lock.init(); + cancmder->registerListener(this); +} +void ZCanEepromModule::onRceivePacket(CanPacketRxBuffer* rxcmd) { // + zlock_guard l(m_lock); + + PROCESS_PACKET(kcmd_eeprom_start_monitor_status, m_id) { + errorcode = m_module->start_monitor_status([this, cmdheader](I_EEPROMModule::eeprom_status_t& status) { // + zlock_guard l(m_lock); + auto* report = (kcmd_eeprom_start_monitor_status_report_t*)m_txbuf; + static_assert(sizeof(*report) < sizeof(m_txbuf), "report size too large"); + + ZLOGI(TAG, "kcmd_eeprom_start_monitor_status exec_status:%d", status); + report->id = m_id; + report->status = status; + m_cancmder->sendStatusReport(cmdheader, (uint8_t*)report, sizeof(*report)); + }); + }; + END_PP(); + + PROCESS_PACKET(kcmd_eeprom_stop_monitor_status, m_id) { errorcode = m_module->stop_monitor_status(); }; + END_PP(); + + PROCESS_PACKET(kcmd_eeprom_read_block, m_id) { errorcode = m_module->read(cmd->sector_index, cmd->sector_size, ack->ack); }; + END_PP(); +} +#endif \ No newline at end of file diff --git a/components/zcancmder_module/zcan_eeprom_module.hpp b/components/zcancmder_module/zcan_eeprom_module.hpp new file mode 100644 index 0000000..56f7b43 --- /dev/null +++ b/components/zcancmder_module/zcan_eeprom_module.hpp @@ -0,0 +1,19 @@ +#pragma once +// #include "sdk/components/zprotocols/zcancmder/api/i_step_motor_ctrl_module.hpp" +#include "sdk/components/zprotocols/zcancmder/api/i_eeprom.hpp" +#include "sdk\components\zcancmder\zcanreceiver.hpp" +namespace iflytop { + +class ZCanEepromModule : public ZCanCmderListener { + ZCanCmder* m_cancmder = nullptr; + int m_id = 0; + I_EEPROMModule* m_module; + + uint8_t m_txbuf[300] = {0}; + zmutex m_lock; + + public: + void initialize(ZCanCmder* cancmder, int id, I_EEPROMModule* module); + virtual void onRceivePacket(CanPacketRxBuffer* rxcmd); +}; +} // namespace iflytop diff --git a/components/zprotocols/zcancmder b/components/zprotocols/zcancmder index 20e1197..fca927c 160000 --- a/components/zprotocols/zcancmder +++ b/components/zprotocols/zcancmder @@ -1 +1 @@ -Subproject commit 20e1197365b8384b7168085f75818d475d2add22 +Subproject commit fca927c9a222ea031e6516df2bdef9b7f1f300b7