Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regmap api added #1693

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/regmap/include/regmap/regmapplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ namespace regmap {

class RegisterMapTool;
class JsonFormatedElement;
class RegMap_API;

class SCOPY_REGMAP_EXPORT RegmapPlugin : public QObject, public PluginBase
{
friend class RegMap_API;
Q_OBJECT
SCOPY_PLUGIN;

Expand Down Expand Up @@ -59,6 +61,8 @@ public Q_SLOTS:
struct iio_device *getIioDevice(iio_context *ctx, const char *dev_name);
bool isBufferCapable(iio_device *dev);
RegisterMapTool *registerMapTool;
void InitApi();
RegMap_API *api;
};
} // namespace regmap
} // namespace scopy
Expand Down
56 changes: 56 additions & 0 deletions plugins/regmap/js/regMapAutomatedTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//This import path is relative to where scopy is build
evaluateFile("../js/scopycommon.js")
evaluateFile("../plugins/regmap/js/regMapFunctions.js")


//Not working with EMU
function deviceTest() {
connect()
switchToRegMap()
readRegister("0x00")
regmap.write("0x00", "0x01")
regmap.write("0x00", "0x00")
}

function deviceTestEMU(path) {
connect()
switchToRegMap()
regmap.readInterval("0x00", "0x07")
regmap.registerDump(path)
regmap.writeFromFile(path)
}

function chageDevicesTest() {
connect()
switchToRegMap()
switchDevice("cf-ad9361-dds-core-lpc")
readRegister("0x00")
regmap.write("0x00", "0x01")
}

//Not working with EMU
function writeRegisterTest() {
connect()
switchToRegMap()
readRegister("0x00")
regmap.write("0x00", "0x01")
let readResult = readRegister("0x00")
if (readResult != "0x01") {
printToConsole("Failed to write")
exit(1)
}
}

function writeBitField(addr, val) {
connect()
switchToRegMap()
regmap.writeBitField(addr, val)
}

deviceTest()
deviceTestEMU("")
chageDevicesTest()
writeRegisterTest()
writeBitField("0x01", "0x01")
disconnect()
scopy.exit()
26 changes: 26 additions & 0 deletions plugins/regmap/js/regMapFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
evaluateFile("../js/scopycommon.js")

function switchToRegMap() {
let toolSwitched = scopy.switchTool("Register map")
if (!toolSwitched) {
printToConsole("Failed to switch to Register Map")
exit(1)
}
}

function readRegister(reg) {
let result = regmap.readRegister(reg)
if (result.length === 0) {
printToConsole("Failed to read register " + reg)
exit(1)
}
return result
}

function switchDevice(devName) {
let result = regmap.setDevice(devName)
if (!result) {
printToConsole("Failed to change device " + devName)
exit(1)
}
}
2 changes: 2 additions & 0 deletions plugins/regmap/src/deviceregistermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ bool DeviceRegisterMap::hasTemplate()
return false;
}

bool DeviceRegisterMap::getAutoread() { return autoread; }

void DeviceRegisterMap::initSettings()
{
QObject::connect(this, &DeviceRegisterMap::requestRead, registerMapValues, &RegisterMapValues::requestRead);
Expand Down
2 changes: 2 additions & 0 deletions plugins/regmap/src/deviceregistermap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RegisterMapTable;

class SCOPY_REGMAP_EXPORT DeviceRegisterMap : public QWidget
{
friend class RegMap_API;
Q_OBJECT
public:
explicit DeviceRegisterMap(RegisterMapTemplate *registerMapTemplate = nullptr,
Expand All @@ -34,6 +35,7 @@ class SCOPY_REGMAP_EXPORT DeviceRegisterMap : public QWidget
void toggleAutoread(bool toggled);
void applyFilters(QString filter);
bool hasTemplate();
bool getAutoread();

private:
ToolTemplate *tool;
Expand Down
1 change: 1 addition & 0 deletions plugins/regmap/src/recyclerview/registermaptable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RecyclerView;

class RegisterMapTable : public IRecyclerViewAdapter
{
friend class RegMap_API;
Q_OBJECT
public:
RegisterMapTable(QMap<uint32_t, RegisterModel *> *registerModels, QWidget *parent);
Expand Down
1 change: 1 addition & 0 deletions plugins/regmap/src/registercontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AddressPicker;

class SCOPY_REGMAP_EXPORT RegisterController : public QWidget
{
friend class RegMap_API;
Q_OBJECT
public:
explicit RegisterController(QWidget *parent = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion plugins/regmap/src/registermapsettingsmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace regmap {
class RegisterMapSettingsMenu : public QWidget
{
friend class RegmapStyleHelper;

friend class RegMap_API;
Q_OBJECT

public:
Expand Down
1 change: 1 addition & 0 deletions plugins/regmap/src/registermaptool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SearchBarWidget;

class SCOPY_REGMAP_EXPORT RegisterMapTool : public QWidget
{
friend class RegMap_API;
Q_OBJECT
public:
explicit RegisterMapTool(QWidget *parent = nullptr);
Expand Down
177 changes: 177 additions & 0 deletions plugins/regmap/src/regmap_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#include "regmap_api.h"

using namespace scopy::regmap;

Q_LOGGING_CATEGORY(CAT_REGMAP_API, "RegMap_API")

RegMap_API::RegMap_API(RegmapPlugin *regMapPlugin)
: ApiObject()
, m_regMapPlugin(regMapPlugin)
{}

RegMap_API::~RegMap_API() {}

void RegMap_API::write(QString addr, QString val)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
uint32_t address = Utils::convertQStringToUint32(addr);
uint32_t value = Utils::convertQStringToUint32(val);
Q_EMIT devRegMap->registerMapValues->requestWrite(address, value);
}

void RegMap_API::writeBitField(QString addr, QString val)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
uint32_t address = Utils::convertQStringToUint32(addr);
devRegMap->registerController->registerChanged(address);
Q_EMIT devRegMap->registerDetailedWidget->bitFieldValueChanged(val);
write(addr, devRegMap->registerController->regValue->text());
}

QStringList RegMap_API::getAvailableDevicesName()
{
auto devices = m_regMapPlugin->registerMapTool->deviceList;
return devices->keys();
}

bool RegMap_API::setDevice(QString device)
{
m_regMapPlugin->registerMapTool->updateActiveRegisterMap(device);
QString currentRegMap = m_regMapPlugin->registerMapTool->activeRegisterMap;
if(currentRegMap == device) {
return true;
}
qWarning(CAT_REGMAP_API) << "Device " << device << " was not set";
return false;
}

QList<QString> RegMap_API::search(QString searchParam)
{
QList<uint32_t> resultIndexes;
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
resultIndexes = Search::searchForRegisters(devRegMap->registerMapTemplate->getRegisterList(), searchParam);
QList<QString> resultList;

for(int i = 0; i < resultIndexes.size(); i++) {
resultList.append(QString::number(resultIndexes[i]));
}
return resultList;
}

void RegMap_API::readInterval(QString startAddr, QString stopAddr)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
uint32_t start = Utils::convertQStringToUint32(startAddr);
uint32_t stop = Utils::convertQStringToUint32(stopAddr);
for(int i = start; i <= stop; i++) {
Q_EMIT devRegMap->requestRead(i);
}
}

bool RegMap_API::enableAutoread(bool enable)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
devRegMap->toggleAutoread(enable);
if(devRegMap->getAutoread() == enable) {
return true;
}
qWarning(CAT_REGMAP_API) << "Autoread was not changed to " << enable;
return false;
}

bool RegMap_API::isAutoreadEnabled()
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
return devRegMap->getAutoread();
}

void RegMap_API::registerDump(QString filePath)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
Q_EMIT devRegMap->registerMapValues->registerDump(filePath);
}

void RegMap_API::setPath(QString filePath) { m_regMapPlugin->registerMapTool->settings->filePath->setText(filePath); }

void RegMap_API::writeFromFile(QString filePath)
{
setPath(filePath);
Q_EMIT m_regMapPlugin->registerMapTool->settings->writeListOfValuesButton->clicked();
}

QString RegMap_API::readRegister(QString addr)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
Q_EMIT devRegMap->requestRead(Utils::convertQStringToUint32(addr));
return devRegMap->registerController->regValue->text();
}

QString RegMap_API::getValueOfRegister(QString addr)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
uint32_t address = Utils::convertQStringToUint32(addr);
QMap<uint32_t, uint32_t> *values = devRegMap->registerMapValues->getRegisterReadValues();
if(values->keys().contains(address)) {
return Utils::convertToHexa(values->value(address), 16);
}
qWarning(CAT_REGMAP_API) << "Value not read";
return "";
}

DeviceRegisterMap *RegMap_API::getActiveDevRegMap()
{
return m_regMapPlugin->registerMapTool->deviceList->value(m_regMapPlugin->registerMapTool->activeRegisterMap);
}

QStringList RegMap_API::getRegisterInfo(QString addr)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
QStringList regInfo;
RegisterModel *regModel =
devRegMap->registerMapTableWidget->registerModels->value(Utils::convertQStringToUint32(addr));
if(regModel != nullptr) {
regInfo.append("Name:" + regModel->getName());
regInfo.append("Address:" + Utils::convertToHexa(regModel->getAddress(), 16));
regInfo.append("Description:" + regModel->getDescription());
regInfo.append("Notes:" + regModel->getNotes());
}
return regInfo;
}

QStringList RegMap_API::getRegisterBitFieldsInfo(QString addr)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
QStringList bitFieldsDetails;
RegisterModel *regModel =
devRegMap->registerMapTableWidget->registerModels->value(Utils::convertQStringToUint32(addr));
QVector<BitFieldModel *> *bitField = regModel->getBitFields();

for(BitFieldModel *bf : *bitField) {
bitFieldsDetails.append("Name:" + bf->getName());
bitFieldsDetails.append("Description:" + bf->getDescription());
bitFieldsDetails.append("Notes:" + bf->getNotes());
bitFieldsDetails.append("Default value:" + Utils::convertToHexa(bf->getDefaultValue(), 16));
bitFieldsDetails.append(";");
}
return bitFieldsDetails;
}

QStringList RegMap_API::getBitFieldInfo(QString addr, QString bitName)
{
DeviceRegisterMap *devRegMap = getActiveDevRegMap();
QStringList bitDetails;
RegisterModel *regModel =
devRegMap->registerMapTableWidget->registerModels->value(Utils::convertQStringToUint32(addr));
QVector<BitFieldModel *> *bitField = regModel->getBitFields();

for(BitFieldModel *bf : *bitField) {
if(bf->getName() == bitName) {
bitDetails.append("Name:" + bf->getName());
bitDetails.append("Description:" + bf->getDescription());
bitDetails.append("Notes:" + bf->getNotes());
bitDetails.append("Default value:" + Utils::convertToHexa(bf->getDefaultValue(), 16));
break;
}
}
return bitDetails;
}
54 changes: 54 additions & 0 deletions plugins/regmap/src/regmap_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef REGMAP_API_H
#define REGMAP_API_H

#include "scopy-regmap_export.h"

#include <regmapplugin.h>
#include <deviceregistermap.hpp>
#include <registermaptool.hpp>
#include <register/registermodel.hpp>
#include <registermapvalues.hpp>
#include <recyclerview/registermaptable.hpp>
#include <utils.hpp>
#include <register/bitfield/bitfieldmodel.hpp>
#include <readwrite/fileregisterreadstrategy.hpp>
#include <registermapsettingsmenu.hpp>
#include <registercontroller.hpp>
#include <register/registerdetailedwidget.hpp>
#include <qlineedit.h>
#include "search.hpp"

namespace scopy::regmap {

class SCOPY_REGMAP_EXPORT RegMap_API : public ApiObject
{

Q_OBJECT

public:
explicit RegMap_API(RegmapPlugin *regMapPlugin);
~RegMap_API();

Q_INVOKABLE void write(QString addr, QString val);
Q_INVOKABLE void writeBitField(QString addr, QString val);
Q_INVOKABLE QStringList getAvailableDevicesName();
Q_INVOKABLE bool setDevice(QString device);
Q_INVOKABLE QList<QString> search(QString searchParam);
Q_INVOKABLE void readInterval(QString startAddr, QString stopAddr);
Q_INVOKABLE bool enableAutoread(bool enable);
Q_INVOKABLE bool isAutoreadEnabled();
Q_INVOKABLE void registerDump(QString filePath);
Q_INVOKABLE void setPath(QString filePath);
Q_INVOKABLE void writeFromFile(QString filePath);
Q_INVOKABLE QString readRegister(QString addr);
Q_INVOKABLE QString getValueOfRegister(QString addr);
Q_INVOKABLE QStringList getRegisterInfo(QString addr);
Q_INVOKABLE QStringList getRegisterBitFieldsInfo(QString addr);
Q_INVOKABLE QStringList getBitFieldInfo(QString addr, QString bitName);

private:
RegmapPlugin *m_regMapPlugin;
DeviceRegisterMap *getActiveDevRegMap();
};
} // namespace scopy::regmap
#endif // REGMAP_API_H
Loading
Loading