Browse Source

add i2c eeprom

master
zhaohe 2 years ago
parent
commit
4a236836a9
  1. 34
      components/api/zi_eeprom.hpp
  2. 108
      components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp
  3. 48
      components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp
  4. 64
      components/sensors/i2ceeprom/m24lrxxe_i2c_eeprom.cpp
  5. 29
      components/sensors/i2ceeprom/m24lrxxe_i2c_eeprom.hpp
  6. 63
      components/sensors/i2ceeprom/p24c16_eeprom.cpp
  7. 28
      components/sensors/i2ceeprom/p24c16_eeprom.hpp

34
components/api/zi_eeprom.hpp

@ -0,0 +1,34 @@
#pragma once
#include <stdint.h>
#include <functional>
#include "sdk\components\zprotocols\zcancmder_v2\api\errorcode.hpp"
namespace iflytop {
using namespace std;
typedef enum {
kp24c16,
km24lrxxe,
} eeprom_type_t;
class ZI_EEPROM {
public:
virtual ~ZI_EEPROM() {}
public:
/**
* @brief
*
* @param add 4byteÔÆë
* @param val
* @param len 4µÄ±Êý
* @return int32_t
*/
virtual int32_t write(int32_t add, uint8_t* val, int32_t *len) = 0;
virtual int32_t read(int32_t add, uint8_t* val, int32_t *len) = 0;
virtual bool isOnline() = 0;
};
} // namespace iflytop

108
components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.cpp

@ -1,108 +0,0 @@
#include "m24lr64e_i2c_eeprom.hpp"
#include <stdio.h>
#include <string.h>
#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 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() {
ZLOGI(TAG, "start_monitor_status");
m_monitor_thread.start([this]() {
// eeprom_status_t status;
#if 1
while (!m_monitor_thread.getExitFlag()) {
bool is_online = isonline();
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::module_read_raw(int32_t index, uint8_t* data, int32_t* len) {
zlock_guard guard(m_mutex);
int32_t sector_size = 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) {
*len = MAX_SIZE - add;
} else {
*len = sector_size;
}
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);
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;
}
int32_t M24LR64E_I2CEEPROM::module_xxx_reg(int32_t param_id, bool read, int32_t& val) {
switch (param_id) {
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, m_is_online_flag);
}
}
}
int32_t M24LR64E_I2CEEPROM::read_status() {
if (!m_monitor_thread.isworking()) {
return 0;
}
return 1;
}
#endif

48
components/sensors/i2ceeprom/m24lr64e_i2c_eeprom.hpp

@ -1,48 +0,0 @@
#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 ZIModule {
I2C_HandleTypeDef* m_i2c_handle;
ZThread m_monitor_thread;
bool m_is_online_flag = false;
zmutex m_mutex;
ZIEventBusSender* m_event_bus_sender;
int32_t id = 0;
public:
M24LR64E_I2CEEPROM(){};
~M24LR64E_I2CEEPROM(){};
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_get_status(int32_t* status) {
*status = 1;
return 0;
}
virtual int32_t module_read_raw(int32_t index, uint8_t* data, int32_t* len);
private:
virtual int32_t module_xxx_reg(int32_t param_id, bool read, int32_t& param_value) override;
int32_t read_status();
private:
bool isonline();
void change_is_online_flag(bool state);
};
} // namespace iflytop
#endif

64
components/sensors/i2ceeprom/m24lrxxe_i2c_eeprom.cpp

@ -0,0 +1,64 @@
#include "m24lrxxe_i2c_eeprom.hpp"
using namespace iflytop;
#define DATA_ADD 0x53
#define CONFIG_ADD 0x57
static int32_t halstatustoerr(HAL_StatusTypeDef status) {
if (status == HAL_OK) {
return 0;
} else if (status == HAL_BUSY) {
return err::kdevice_is_busy;
} else if (status == HAL_TIMEOUT) {
return err::kovertime;
} else {
return err::kfail;
}
}
void M24LRXXE_I2C_EEPROM::initialize( I2C_HandleTypeDef* i2c_handle) { m_i2c_handle = i2c_handle; }
int32_t M24LRXXE_I2C_EEPROM::write(int32_t add, uint8_t* val, int32_t* len) {
ZASSERT(*len % 4 == 0);
ZASSERT(add % 4 == 0);
int32_t ecode = 0;
for (int32_t i = 0; i < *len; i += 4) {
ecode = write32(add + i, *(uint32_t*)&val[i]);
if (ecode != 0) return ecode;
}
return ecode;
}
int32_t M24LRXXE_I2C_EEPROM::read(int32_t add, uint8_t* val, int32_t* len) {
ZASSERT(*len % 4 == 0);
ZASSERT(add % 4 == 0);
int32_t ecode = 0;
for (int32_t i = 0; i < *len; i += 4) {
ecode = read32(add + i, (uint32_t*)&val[i]);
if (ecode != 0) return ecode;
}
return ecode;
}
bool M24LRXXE_I2C_EEPROM::isOnline() {
uint32_t val = 0;
int32_t ret = read32(0, &val);
if (ret != 0) {
return false;
}
return true;
}
int32_t M24LRXXE_I2C_EEPROM::write32(uint16_t add, uint32_t val) {
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(m_i2c_handle, DATA_ADD << 1, add, I2C_MEMADD_SIZE_16BIT, (uint8_t*)&val, 4, 10);
zos_delay(10);
return halstatustoerr(status);
}
int32_t M24LRXXE_I2C_EEPROM::read32(uint16_t add, uint32_t* val) {
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, DATA_ADD << 1, add, I2C_MEMADD_SIZE_16BIT, (uint8_t*)val, 4, 10);
return halstatustoerr(status);
}
#if 0
int32_t M24LRXXE_I2C_EEPROM::read_reg(int32_t add, uint32_t* regval) {
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, CONFIG_ADD << 1, add, I2C_MEMADD_SIZE_16BIT, (uint8_t*)regval, 4, 10);
return halstatustoerr(status);
}
#endif

29
components/sensors/i2ceeprom/m24lrxxe_i2c_eeprom.hpp

@ -0,0 +1,29 @@
#include "sdk\components\api\zi_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 M24LRXXE_I2C_EEPROM : public ZI_EEPROM {
I2C_HandleTypeDef* m_i2c_handle;
public:
M24LRXXE_I2C_EEPROM(){};
~M24LRXXE_I2C_EEPROM(){};
void initialize( I2C_HandleTypeDef* i2c_handle);
virtual int32_t write(int32_t add, uint8_t* val, int32_t *len) override;
virtual int32_t read(int32_t add, uint8_t* val, int32_t *len) override;
virtual bool isOnline() override;
private:
int32_t write32(uint16_t add, uint32_t val);
int32_t read32(uint16_t add, uint32_t* val);
// int32_t read_reg(int32_t add, uint32_t* regval);
};
} // namespace iflytop
#endif

63
components/sensors/i2ceeprom/p24c16_eeprom.cpp

@ -0,0 +1,63 @@
#include "p24c16_eeprom.hpp"
using namespace iflytop;
#define DATA_ADD 0x50
static int32_t halstatustoerr(HAL_StatusTypeDef status) {
if (status == HAL_OK) {
return 0;
} else if (status == HAL_BUSY) {
return err::kdevice_is_busy;
} else if (status == HAL_TIMEOUT) {
return err::kovertime;
} else {
return err::kfail;
}
}
void P24C16::initialize( I2C_HandleTypeDef* i2c_handle) { m_i2c_handle = i2c_handle; }
int32_t P24C16::write(int32_t add, uint8_t* val, int32_t* len) {
ZASSERT(*len % 4 == 0);
ZASSERT(add % 4 == 0);
int32_t ecode = 0;
for (int32_t i = 0; i < *len; i += 4) {
ecode = write32(add + i, *(uint32_t*)&val[i]);
if (ecode != 0) return ecode;
}
return ecode;
}
int32_t P24C16::read(int32_t add, uint8_t* val, int32_t* len) {
ZASSERT(*len % 4 == 0);
ZASSERT(add % 4 == 0);
int32_t ecode = 0;
for (int32_t i = 0; i < *len; i += 4) {
ecode = read32(add + i, (uint32_t*)&val[i]);
if (ecode != 0) return ecode;
}
return ecode;
}
bool P24C16::isOnline() {
uint32_t val = 0;
int32_t ret = read32(0, &val);
if (ret != 0) {
return false;
}
return true;
}
int32_t P24C16::write32(uint16_t add, uint32_t val) {
uint16_t deviceadd = DATA_ADD + (add >> 8);
uint16_t regadd = add & 0xff;
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(m_i2c_handle, deviceadd << 1, regadd, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&val, 4, 10);
zos_delay(10);
return halstatustoerr(status);
}
int32_t P24C16::read32(uint16_t add, uint32_t* val) {
uint16_t deviceadd = DATA_ADD + (add >> 8);
uint16_t regadd = add & 0xff;
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(m_i2c_handle, deviceadd << 1, regadd, I2C_MEMADD_SIZE_8BIT, (uint8_t*)val, 4, 10);
return halstatustoerr(status);
}

28
components/sensors/i2ceeprom/p24c16_eeprom.hpp

@ -0,0 +1,28 @@
#include "sdk\components\api\zi_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 P24C16 : public ZI_EEPROM {
I2C_HandleTypeDef* m_i2c_handle;
public:
P24C16(){};
~P24C16(){};
void initialize( I2C_HandleTypeDef* i2c_handle);
virtual int32_t write(int32_t add, uint8_t* val, int32_t* len) override;
virtual int32_t read(int32_t add, uint8_t* val, int32_t* len) override;
virtual bool isOnline() override;
private:
int32_t write32(uint16_t add, uint32_t val);
int32_t read32(uint16_t add, uint32_t* val);
// int32_t read_reg(int32_t add, uint32_t* regval);
};
} // namespace iflytop
#endif
Loading…
Cancel
Save