Add direct-access methods to the base Device class, so that there's a common way of talking to drivers regardless of which of the specialised classes they derive from.

Make the Device destructor public and virtual, so that arbitrary devices can be deleted. Likewise for classes that derive from it.

Make Device::init public so that arbitrary devices can be initialised after being returned by factories.
This commit is contained in:
px4dev 2013-08-04 19:48:20 -07:00
parent 567f621754
commit 4b4f33e6a4
4 changed files with 82 additions and 23 deletions

View File

@ -223,5 +223,22 @@ interrupt(int irq, void *context)
return OK;
}
int
Device::read(unsigned offset, void *data, unsigned count)
{
return -ENODEV;
}
} // namespace device
int
Device::write(unsigned offset, void *data, unsigned count)
{
return -ENODEV;
}
int
Device::ioctl(unsigned operation, unsigned &arg)
{
return -ENODEV;
}
} // namespace device

View File

@ -68,11 +68,62 @@ namespace device __EXPORT
class __EXPORT Device
{
public:
/**
* Destructor.
*
* Public so that anonymous devices can be destroyed.
*/
virtual ~Device();
/**
* Interrupt handler.
*/
virtual void interrupt(void *ctx); /**< interrupt handler */
/*
* Direct access methods.
*/
/**
* Initialise the driver and make it ready for use.
*
* @return OK if the driver initialized OK, negative errno otherwise;
*/
virtual int init();
/**
* Read directly from the device.
*
* The actual size of each unit quantity is device-specific.
*
* @param offset The device address at which to start reading
* @param data The buffer into which the read values should be placed.
* @param count The number of items to read.
* @return The number of items read on success, negative errno otherwise.
*/
virtual int read(unsigned address, void *data, unsigned count);
/**
* Write directly to the device.
*
* The actual size of each unit quantity is device-specific.
*
* @param address The device address at which to start writing.
* @param data The buffer from which values should be read.
* @param count The number of items to write.
* @return The number of items written on success, negative errno otherwise.
*/
virtual int write(unsigned address, void *data, unsigned count);
/**
* Perform a device-specific operation.
*
* @param operation The operation to perform.
* @param arg An argument to the operation.
* @return Negative errno on error, OK or positive value on success.
*/
virtual int ioctl(unsigned operation, unsigned &arg);
protected:
const char *_name; /**< driver name */
bool _debug_enabled; /**< if true, debug messages are printed */
@ -85,14 +136,6 @@ protected:
*/
Device(const char *name,
int irq = 0);
~Device();
/**
* Initialise the driver and make it ready for use.
*
* @return OK if the driver initialised OK.
*/
virtual int init();
/**
* Enable the device interrupt
@ -189,7 +232,7 @@ public:
/**
* Destructor
*/
~CDev();
virtual ~CDev();
virtual int init();
@ -282,6 +325,7 @@ public:
* Test whether the device is currently open.
*
* This can be used to avoid tearing down a device that is still active.
* Note - not virtual, cannot be overridden by a subclass.
*
* @return True if the device is currently open.
*/
@ -396,9 +440,9 @@ public:
const char *devname,
uint32_t base,
int irq = 0);
~PIO();
virtual ~PIO();
int init();
virtual int init();
protected:
@ -407,7 +451,7 @@ protected:
*
* @param offset Register offset in bytes from the base address.
*/
uint32_t reg(uint32_t offset) {
uint32_t reg(uint32_t offset) {
return *(volatile uint32_t *)(_base + offset);
}
@ -444,4 +488,4 @@ private:
} // namespace device
#endif /* _DEVICE_DEVICE_H */
#endif /* _DEVICE_DEVICE_H */

View File

@ -55,13 +55,11 @@ class __EXPORT I2C : public CDev
public:
/**
* Get the address
*/
uint16_t get_address() {
return _address;
}
/**
* Get the address
*/
int16_t get_address() { return _address; }
protected:
/**
* The number of times a read or write operation will be retried on
@ -90,7 +88,7 @@ protected:
uint16_t address,
uint32_t frequency,
int irq = 0);
~I2C();
virtual ~I2C();
virtual int init();

View File

@ -71,7 +71,7 @@ protected:
enum spi_mode_e mode,
uint32_t frequency,
int irq = 0);
~SPI();
virtual ~SPI();
virtual int init();