Page MenuHomec4science

main.cpp
No OneTemporary

File Metadata

Created
Fri, Nov 29, 08:18

main.cpp

/**
* \file main.cpp
*
* \date Oct 23, 2012
* \author Alexandre Tuleu
*/
#include <libsbcp-tool/UIElements.h>
#include <libsbcp-tool/argstream.h>
#include <libsbcp/utils/Config.h>
#include <vector>
sbcp::Data PerformRawInstructionOnRegister(sbcp::Device::Ptr dev,
sbcp::InstructionID instruction,
sbcp::Register::Address adr,
sbcp::Data * data);
sbcp::Data PerformRawInstructionOnRegister(sbcp::Device::Ptr dev,
sbcp::InstructionID instruction,
sbcp::Register::Address adr,
sbcp::Data * data){
sbcp::LazyWorkflow & w = dev->GetBus().Lazy();
sbcp::Byte * out;
out = w.Prepare(dev,instruction,data != 0 ? 3 : 1);
out[0] = adr;
if(data){
sbcp::FillBytesFromData(*data,&(out[1]));
}
const sbcp::Byte * res(NULL);
sbcp::PayloadSize resSize;
w.Submit(&res,resSize);
w.Release();
if(resSize == 2){
sbcp::Data result;
sbcp::FillDataFromBytes(res,result);
return result;
}
return 0x0000;
}
int main(int argc, char ** argv){
sbcp::HexaByte klass,id,reg;
sbcp::Data regVal(0x0000);
std::vector<unsigned int> regValues;
bool write(false),unlock(false),nonInteractif(false);
std::string busName(sbcp::BusesConfigSection::DEFAULT_BUS_NAME);
argstream as(argc,argv);
as >> option('w',"write",write,"perform a write operation instead of a read (you need to specify value)")
>> option('u',"unlock",unlock,"unlock registers first")
>> option('s',"non-interactive",nonInteractif,"script mode, throw exception instead to ask")
>> parameter('c',"class",klass,"Class of the device",false)
>> parameter('i',"id",id,"Id of the device",false)
>> parameter('r',"register",reg,"Register to operate on",false)
>> parameter('v', "value", regVal, "Value to set on write",false)
>> parameter('b',"bus",busName,"Name of the bus to use",false)
>> help();
as.defaultErrorHandling();
FillHexaByteIfNeeded("class",nonInteractif,klass);
FillHexaByteIfNeeded("id",nonInteractif,id);
FillHexaByteIfNeeded("reg",nonInteractif,reg);
sbcp::Config config;
config.LoadAllFiles();
std::shared_ptr<sbcp::Bus> bus = config.OpenBusWithFrame(busName,1,16);
sbcp::Device::Ptr dev = bus->OpenGenericDevice(klass,id);
if(!dev){
std::ostringstream os;
os << "Could not find device " << klass << ":" << id << ".";
throw std::runtime_error(os.str());
}
sbcp::Data oldRegValue = PerformRawInstructionOnRegister(dev,
sbcp::Device::GET_REGISTERS,
reg,
(sbcp::Data *)NULL);
if(nonInteractif){
std::cout << std::hex << oldRegValue << std::endl;
} else {
std::cout << "Device " << klass << ":" << id << " register " << reg
<< " has value 0x" << std::hex << oldRegValue << std::endl;
}
if(write){
if(unlock){
PerformRawInstructionOnRegister(dev,sbcp::Device::UNLOCK_REGISTER,
reg,
(sbcp::Data *)NULL);
}
PerformRawInstructionOnRegister(dev,
sbcp::Device::SET_REGISTERS,
reg,&regVal);
sbcp::Data newValue = PerformRawInstructionOnRegister(dev,
sbcp::Device::GET_REGISTERS,
reg,
(sbcp::Data *)NULL);
if(nonInteractif){
std::cout << std::hex << newValue << std::endl;
} else {
std::cout << "Device " << klass << ":" << id << " register" << reg
<< " set to 0x" << std::hex << newValue << std::endl;
}
}
return 0;
}

Event Timeline