Page 1 of 1
Abstract I/O Block Device - EEPROM
Posted: Fri Oct 25, 2013 1:22 pm
by Noname
Hello
I'm having some difficulties to understand the implementation of the abstract I/O Block Device (IO_BLOCK.h) in the MMC_SPI or SDC Driver (BaseBlockDevice).
I do really not feel comfortable with C, or in general with any none object oriented language, so I feel a little bit lost

.
I imagine that the key of this is the vmt structure but I don't undersdtand how those methods are linked to the _base_block_device_methods in IO_BLOCK.h.
I'm asking it because I need to develop a BaseBlockDevice SPI/I2C EEPROM driver (well I'll try at least....

)
Thanks
Re: Abstract I/O Block Device - EEPROM
Posted: Fri Oct 25, 2013 2:27 pm
by Giovanni
Hi,
It is like a class in C++ or Java, it is the ancestor of block device drivers, you need to extend that class in order to create your class, MMC_SPI and SDC do exactly that.
You don't even need to fully understand those macros, just use the same declarations you can see in those drivers and assign your own name to the class.
Giovanni
Re: Abstract I/O Block Device - EEPROM
Posted: Fri Oct 25, 2013 4:07 pm
by Noname
Thank you for your answer.
Giovanni wrote:Hi,
It is like a class in C++ or Java, it is the ancestor of block device drivers, you need to extend that class in order to create your class, MMC_SPI and SDC do exactly that.
That's what I was searching for, but how is it done in C????
I understand that those methods in the MMC_SPI driver (for instance) implement the ones defined in the abstract "class" io_block.h
Code: Select all
/**
* @brief Virtual methods table.
*/
static const struct MMCDriverVMT mmc_vmt = {
(bool_t (*)(void *))mmc_lld_is_card_inserted,
(bool_t (*)(void *))mmc_lld_is_write_protected,
(bool_t (*)(void *))mmcConnect,
(bool_t (*)(void *))mmcDisconnect,
mmc_read,
mmc_write,
(bool_t (*)(void *))mmcSync,
(bool_t (*)(void *, BlockDeviceInfo *))mmcGetInfo
};
Code: Select all
/**
* @brief @p BaseBlockDevice specific methods.
*/
#define _base_block_device_methods
bool_t (*is_inserted)(void *instance); \
bool_t (*is_protected)(void *instance); \
bool_t (*connect)(void *instance); \
bool_t (*disconnect)(void *instance); \
...
But they use different method names, or prefixes. Example:
connect in the abstract "class" becomes
mmcConnect in the
MMC_SPI driver or
sdcConnect in the SDC driver
How is that possible?
(Sorry for my noob questions

)
Re: Abstract I/O Block Device - EEPROM
Posted: Fri Oct 25, 2013 4:34 pm
by Giovanni
Hi,
In io_block.h there are macros for accessing descendant objects in an abstract way:
Code: Select all
/**
* @name Macro Functions (BaseBlockDevice)
* @{
*/
/**
* @brief Returns the driver state.
* @note Can be called in ISR context.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The driver state.
*
* @special
*/
#define blkGetDriverState(ip) ((ip)->state)
/**
* @brief Determines if the device is transferring data.
* @note Can be called in ISR context.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The driver state.
* @retval FALSE the device is not transferring data.
* @retval TRUE the device not transferring data.
*
* @special
*/
#define blkIsTransferring(ip) ((((ip)->state) == BLK_CONNECTING) || \
(((ip)->state) == BLK_DISCONNECTING) || \
(((ip)->state) == BLK_READING) || \
(((ip)->state) == BLK_WRITING))
/**
* @brief Returns the media insertion status.
* @note On some implementations this function can only be called if the
* device is not transferring data.
* The function @p blkIsTransferring() should be used before calling
* this function.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The media state.
* @retval FALSE media not inserted.
* @retval TRUE media inserted.
*
* @api
*/
#define blkIsInserted(ip) ((ip)->vmt->is_inserted(ip))
/**
* @brief Returns the media write protection status.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The media state.
* @retval FALSE writable media.
* @retval TRUE non writable media.
*
* @api
*/
#define blkIsWriteProtected(ip) ((ip)->vmt->is_protected(ip))
/**
* @brief Performs the initialization procedure on the block device.
* @details This function should be performed before I/O operations can be
* attempted on the block device and after insertion has been
* confirmed using @p blkIsInserted().
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkConnect(ip) ((ip)->vmt->connect(ip))
/**
* @brief Terminates operations on the block device.
* @details This operation safely terminates operations on the block device.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkDisconnect(ip) ((ip)->vmt->disconnect(ip))
/**
* @brief Reads one or more blocks.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
* @param[in] startblk first block to read
* @param[out] buf pointer to the read buffer
* @param[in] n number of blocks to read
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkRead(ip, startblk, buf, n) \
((ip)->vmt->read(ip, startblk, buf, n))
/**
* @brief Writes one or more blocks.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
* @param[in] startblk first block to write
* @param[out] buf pointer to the write buffer
* @param[in] n number of blocks to write
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkWrite(ip, startblk, buf, n) \
((ip)->vmt->write(ip, startblk, buf, n))
/**
* @brief Ensures write synchronization.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkSync(ip) ((ip)->vmt->sync(ip))
/**
* @brief Returns a media information structure.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
* @param[out] bdip pointer to a @p BlockDeviceInfo structure
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkGetInfo(ip, bdip) ((ip)->vmt->get_info(ip, bdip))
/** @} */
In mmc_spi.h you can see how the base class is extended in a new class named MMCDriver:
Code: Select all
/**
* @brief @p MMCDriver specific methods.
*/
#define _mmc_driver_methods \
_mmcsd_block_device_methods
/**
* @extends MMCSDBlockDeviceVMT
*
* @brief @p MMCDriver virtual methods table.
*/
struct MMCDriverVMT {
_mmc_driver_methods
};
/**
* @extends MMCSDBlockDevice
*
* @brief Structure representing a MMC/SD over SPI driver.
*/
typedef struct {
/**
* @brief Virtual Methods Table.
*/
const struct MMCDriverVMT *vmt;
_mmcsd_block_device_data
/**
* @brief Current configuration data.
*/
const MMCConfig *config;
/***
* @brief Addresses use blocks instead of bytes.
*/
bool_t block_addresses;
} MMCDriver;
It is just few lines of code, replace MMCDriver with MYDriver or some other name, the fields in the structure are the field that you ADD to the base class.
You can add more methods by appending them to the _xxx_driver_methods macro.
Object oriented programming is not matter of language it is a methodology that you can use also writing ASM code.
Giovanni
Re: Abstract I/O Block Device - EEPROM
Posted: Sat Oct 26, 2013 1:39 pm
by Noname
Ok thanks for all the infos

Re: Abstract I/O Block Device - EEPROM
Posted: Sun Nov 24, 2013 4:06 pm
by Noname
Hello
I have some issues :-/. I don't know why but I can't get rid of that error:
eeprom_testsuite.c:134: undefined reference to `eepromObjectInit'
eeprom_testsuite.c:135: undefined reference to `eepromStart'
But I added the header files that contains the prototypes to the makefile (INCDIR) and source file also (CSRC).

Re: Abstract I/O Block Device - EEPROM
Posted: Sun Nov 24, 2013 4:24 pm
by Giovanni
Do you include the header file in testsuite.c?
Without code it is hard to give advice.
Giovanni
Re: Abstract I/O Block Device - EEPROM
Posted: Sun Nov 24, 2013 4:28 pm
by Noname
Yes :/
Code: Select all
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "eeprom_i2c.h"
#include "eeprom_testsuite.h"
The .o files ar also generated

Re: Abstract I/O Block Device - EEPROM
Posted: Mon Nov 25, 2013 6:42 pm
by Noname
Sorry
I discovered the problem.
I forgot to rename the SPI suffix with I2C.
Code: Select all
#if HAL_USE_EEPROM_SPI || defined(__DOXYGEN__)
Re: Abstract I/O Block Device - EEPROM
Posted: Mon Nov 25, 2013 8:47 pm
by Giovanni
It is always something like this

I should write an article with all the stupid errors I do, title: "don't do this".
Giovanni