Skip to content
Snippets Groups Projects
Commit c265c62b authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Solved some bugs in the read_reg() function.

Added an exception class.
Open issues: the port is not properly configured.
             errors on read and write operations are not detected.
parent 688999a7
No related branches found
No related tags found
No related merge requests found
# edit the following line to add all the source code files of the library
SET(sources usb_i2c.cpp)
SET(sources usb_i2c.cpp usb_i2c_exceptions.cpp)
# edit the following line to add all the header files of the library
SET(headers usb_i2c.h)
SET(headers usb_i2c.h usb_i2c_exceptions.h)
# edit the following line to find the necessary packages
FIND_PACKAGE(iriutils REQUIRED)
......
#include "usb_i2c.h"
#include "eventexceptions.h"
#include "usb_i2c_exceptions.h"
#include <string.h>
#include <iostream>
CUSBI2C::CUSBI2C(const std::string &adapter_id)
{
......@@ -9,6 +11,7 @@ CUSBI2C::CUSBI2C(const std::string &adapter_id)
if(adapter_id.size()==0)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Invalid adapter identifier.");
}
else
{
......@@ -47,6 +50,7 @@ unsigned char CUSBI2C::get_revision(void)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -64,6 +68,7 @@ void CUSBI2C::config_gpio(gpio_pins pin, gpio_types type)
case gpio1: if(type!=digital_input)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"GPIO1 can only be configured as a digital input.");
}
else
{
......@@ -116,6 +121,7 @@ void CUSBI2C::set_gpio(gpio_pins pin, bool value)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -127,6 +133,7 @@ void CUSBI2C::set_gpio(gpio_pins pin, bool value)
else
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The desired GPIO is not configured as a digital output");
}
}
......@@ -143,6 +150,7 @@ bool CUSBI2C::get_gpio(gpio_pins pin)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -157,6 +165,7 @@ bool CUSBI2C::get_gpio(gpio_pins pin)
else
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The desired GPIO is not configured as a digital input");
}
return false;
......@@ -188,6 +197,7 @@ short int CUSBI2C::get_analog(gpio_pins pin)
else
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The desired GPIO is not configured as an analog input");
}
return analog_value;
......@@ -204,6 +214,7 @@ void CUSBI2C::turn_led_on(void)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -215,6 +226,7 @@ void CUSBI2C::turn_led_on(void)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -235,6 +247,7 @@ void CUSBI2C::turn_led_off(void)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -246,6 +259,7 @@ void CUSBI2C::turn_led_off(void)
if((written=this->comm_dev->write(cmd,4))!=4)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -265,12 +279,14 @@ int CUSBI2C::write(unsigned char dev_id, unsigned char *data, int len)
if(len>1)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"At least one byte must be send in a write operation");
}
else
{
if(data==NULL)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Invalid data input buffer");
}
else
{
......@@ -280,6 +296,7 @@ int CUSBI2C::write(unsigned char dev_id, unsigned char *data, int len)
if((written=this->comm_dev->write(cmd,3))!=3)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -289,9 +306,11 @@ int CUSBI2C::write(unsigned char dev_id, unsigned char *data, int len)
if(status==0x00)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The write operation did not succed");
}
}catch(CEventTimeoutException &e){
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The write operation did not complete in the allowed time");
}
}
}
......@@ -308,6 +327,7 @@ int CUSBI2C::read(unsigned char dev_id, unsigned char *data, int len)
if(data==NULL)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Invalid data input buffer");
}
else
{
......@@ -319,6 +339,7 @@ int CUSBI2C::read(unsigned char dev_id, unsigned char *data, int len)
if((written=this->comm_dev->write(cmd,2))!=2)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
}
else
......@@ -328,6 +349,7 @@ int CUSBI2C::read(unsigned char dev_id, unsigned char *data, int len)
if((written=this->comm_dev->write(cmd,3))!=3)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
}
try{
......@@ -338,6 +360,7 @@ int CUSBI2C::read(unsigned char dev_id, unsigned char *data, int len)
}while(total_read<len);
}catch(CEventTimeoutException &e){
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The read operation did not complete in the allowed time");
}
}
......@@ -350,7 +373,8 @@ int CUSBI2C::write_reg(unsigned char dev_id, short int address, unsigned char *d
std::list<std::string> events;
unsigned char status;
unsigned char *cmd;
events.push_back(this->rx_event_id);
do{
if((len-total_written)>64) num=64;
else num=len-total_written;
......@@ -378,6 +402,7 @@ int CUSBI2C::write_reg(unsigned char dev_id, short int address, unsigned char *d
if((written=this->comm_dev->write(cmd,num))!=num)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -387,9 +412,11 @@ int CUSBI2C::write_reg(unsigned char dev_id, short int address, unsigned char *d
if(status==0x00)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The write operation did not succed");
}
}catch(CEventTimeoutException &e){
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The write operation did not complete in the allowed time");
}
}
delete[] cmd;
......@@ -402,33 +429,37 @@ int CUSBI2C::write_reg(unsigned char dev_id, short int address, unsigned char *d
int CUSBI2C::read_reg(unsigned char dev_id, short int address, unsigned char *data, int len)
{
int written=0,read=0,total_read=0,num=0,total_num=0;
int written=0,read=0,total_read=0,num=0,total_num=0,num_cmd;
std::list<std::string> events;
unsigned char cmd[5];
events.push_back(this->rx_event_id);
do{
if((len-total_read)>60) num=60;
else num=len-total_read;
if(address<256)
{
cmd[0]=0x55;
cmd[1]=dev_id<<1;
cmd[1]=(dev_id<<1)+1;
cmd[2]=address;
cmd[3]=num;
written=this->comm_dev->write(cmd,4)!=4;
written=this->comm_dev->write(cmd,4);
num_cmd=4;
}
else
{
cmd[0]=0x56;
cmd[1]=dev_id<<1;
cmd[1]=(dev_id<<1)+1;
cmd[2]=address/256;
cmd[3]=address%256;
cmd[4]=num;
written=this->comm_dev->write(cmd,5)!=5;
written=this->comm_dev->write(cmd,5);
num_cmd=5;
}
if(written!=num)
if(written!=num_cmd)
{
/* handle exceptions */
throw CUSBI2CException(_HERE_,"Error while sending the command to the converter");
}
else
{
......@@ -440,6 +471,7 @@ int CUSBI2C::read_reg(unsigned char dev_id, short int address, unsigned char *da
}while(total_num<num);
}catch(CEventTimeoutException &e){
/* handle exceptions */
throw CUSBI2CException(_HERE_,"The write operation did not complete in the allowed time");
}
}
total_read+=num;
......
#include "usb_i2c_exceptions.h"
#include <string.h>
#include <stdio.h>
const std::string usb_i2c_exception_msg="[CUSBI2C class] - ";
CUSBI2CException::CUSBI2CException(const std::string& where, const std::string& error_msg):CException(where,usb_i2c_exception_msg)
{
this->error_msg+=error_msg;
}
#ifndef USB_I2C_EXCEPTIONS
#define USB_I2C_EXCEPTIONS
#include "exceptions.h"
class CUSBI2CException : public CException
{
public:
CUSBI2CException(const std::string& where,const std::string& error_msg);
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment