Commit ad4d4456 authored by Lucas Russo's avatar Lucas Russo

sm_io/protocols/*: use operation pointer instead of enum name

This will make it easier to declare a new operation
and use it. In the previous way, we had to manually
add the operation to the enumeration.
parent 3e640e0f
...@@ -19,15 +19,6 @@ extern "C" { ...@@ -19,15 +19,6 @@ extern "C" {
#define SMPR_WB_REG_2_BYTE 4 /* 32-bit word */ #define SMPR_WB_REG_2_BYTE 4 /* 32-bit word */
#define SMPR_WB_REG_2_BIT (SMPR_WB_REG_2_BYTE*SMPR_BYTE_2_BIT) #define SMPR_WB_REG_2_BIT (SMPR_WB_REG_2_BYTE*SMPR_BYTE_2_BIT)
typedef enum {
SMPR_SPI = 0,
SMPR_I2C,
SMPR_BSMP,
SMPR_1WIRE,
SMPR_GPIO,
SMPR_BYPASS
} smpr_type_e;
/* Open protocol */ /* Open protocol */
typedef int (*proto_open_fp) (smpr_t *self, uint64_t base, void *args); typedef int (*proto_open_fp) (smpr_t *self, uint64_t base, void *args);
/* Release protocol */ /* Release protocol */
...@@ -50,6 +41,7 @@ typedef ssize_t (*proto_read_dma_fp) (smpr_t *self, uint64_t offs, size_t size, ...@@ -50,6 +41,7 @@ typedef ssize_t (*proto_read_dma_fp) (smpr_t *self, uint64_t offs, size_t size,
typedef ssize_t (*proto_write_dma_fp) (smpr_t *self, uint64_t offs, size_t size, const uint32_t *data, uint32_t flags); typedef ssize_t (*proto_write_dma_fp) (smpr_t *self, uint64_t offs, size_t size, const uint32_t *data, uint32_t flags);
typedef struct { typedef struct {
const char *proto_name; /* Protocol name */
proto_open_fp proto_open; /* Open protocol */ proto_open_fp proto_open; /* Open protocol */
proto_release_fp proto_release; /* Release protocol */ proto_release_fp proto_release; /* Release protocol */
proto_read_16_fp proto_read_16; /* Read 16-bit data */ proto_read_16_fp proto_read_16; /* Read 16-bit data */
...@@ -71,7 +63,8 @@ typedef struct { ...@@ -71,7 +63,8 @@ typedef struct {
/***************** Our methods *****************/ /***************** Our methods *****************/
/* Creates a new instance of the Low-level I/O */ /* Creates a new instance of the Low-level I/O */
smpr_t * smpr_new (char *name, smio_t *parent, smpr_type_e type, int verbose); smpr_t * smpr_new (char *name, smio_t *parent, const smpr_proto_ops_t *reg_ops,
int verbose);
/* Destroy an instance of the Low-level I/O */ /* Destroy an instance of the Low-level I/O */
smpr_err_e smpr_destroy (smpr_t **self_p); smpr_err_e smpr_destroy (smpr_t **self_p);
/* Register Specific Protocol operations to smpr instance */ /* Register Specific Protocol operations to smpr instance */
...@@ -82,6 +75,8 @@ void *smpr_get_handler (smpr_t *self); ...@@ -82,6 +75,8 @@ void *smpr_get_handler (smpr_t *self);
void *smpr_unset_handler (smpr_t *self); void *smpr_unset_handler (smpr_t *self);
/* Get parent handler */ /* Get parent handler */
smio_t *smpr_get_parent (smpr_t *self); smio_t *smpr_get_parent (smpr_t *self);
/* Get protocol name */
const char *smpr_get_ops_name (smpr_t *self);
/************************************************************/ /************************************************************/
/***************** Thsafe generic methods API ***************/ /***************** Thsafe generic methods API ***************/
......
...@@ -483,6 +483,7 @@ err_packet_header: ...@@ -483,6 +483,7 @@ err_packet_header:
} }
const smpr_proto_ops_t smpr_proto_ops_bsmp = { const smpr_proto_ops_t smpr_proto_ops_bsmp = {
.proto_name = "BSMP", /* Protocol name */
.proto_open = bsmp_open, /* Open device */ .proto_open = bsmp_open, /* Open device */
.proto_release = bsmp_release, /* Release device */ .proto_release = bsmp_release, /* Release device */
.proto_read_16 = NULL, /* Read 16-bit data */ .proto_read_16 = NULL, /* Read 16-bit data */
......
...@@ -652,6 +652,7 @@ err_exit: ...@@ -652,6 +652,7 @@ err_exit:
#endif #endif
const smpr_proto_ops_t smpr_proto_ops_i2c = { const smpr_proto_ops_t smpr_proto_ops_i2c = {
.proto_name = "I2C", /* Protocol name */
.proto_open = i2c_open, /* Open device */ .proto_open = i2c_open, /* Open device */
.proto_release = i2c_release, /* Release device */ .proto_release = i2c_release, /* Release device */
.proto_read_16 = i2c_read_16, /* Read 16-bit data */ .proto_read_16 = i2c_read_16, /* Read 16-bit data */
......
...@@ -423,8 +423,8 @@ static ssize_t _spi_read_write_generic (smpr_t *self, uint8_t *data, ...@@ -423,8 +423,8 @@ static ssize_t _spi_read_write_generic (smpr_t *self, uint8_t *data,
/* Read data from RX regsiters */ /* Read data from RX regsiters */
uint32_t i; uint32_t i;
uint8_t data_read[SPI_PROTO_REG_RXTX_NUM * SMPR_WB_REG_2_BYTE] = {0}; uint8_t data_read[SPI_PROTO_REG_RXTX_NUM * SMPR_WB_REG_2_BYTE] = {0};
/* If we are using Bidirectional SPI, the receved data is located on base address /* If we are using Bidirectional SPI, the receved data is located on base address
* SPI_PROTO_REG_RX0. Otherwise, the data is on a different register * SPI_PROTO_REG_RX0. Otherwise, the data is on a different register
* SPI_PROTO_REG_RX0_SINGLE */ * SPI_PROTO_REG_RX0_SINGLE */
uint32_t read_base_addr = (spi_proto->bidir) ? SPI_PROTO_REG_RX0 : SPI_PROTO_REG_RX0_SINGLE; uint32_t read_base_addr = (spi_proto->bidir) ? SPI_PROTO_REG_RX0 : SPI_PROTO_REG_RX0_SINGLE;
/* We read 32-bit at a time */ /* We read 32-bit at a time */
...@@ -454,6 +454,7 @@ err_proto_handler: ...@@ -454,6 +454,7 @@ err_proto_handler:
} }
const smpr_proto_ops_t smpr_proto_ops_spi = { const smpr_proto_ops_t smpr_proto_ops_spi = {
.proto_name = "SPI", /* Protocol name */
.proto_open = spi_open, /* Open device */ .proto_open = spi_open, /* Open device */
.proto_release = spi_release, /* Release device */ .proto_release = spi_release, /* Release device */
.proto_read_16 = spi_read_16, /* Read 16-bit data */ .proto_read_16 = spi_read_16, /* Read 16-bit data */
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
smpr_err_str (err_type)) smpr_err_str (err_type))
struct _smpr_t { struct _smpr_t {
smpr_type_e type; /* Protocol type (SPI, I2C, 1-wire, GPIO, Bypass) */
void *proto_handler; /* Generic pointer to a protocol handler. This void *proto_handler; /* Generic pointer to a protocol handler. This
must be cast to a specific type by the must be cast to a specific type by the
specific protocol functions */ specific protocol functions */
...@@ -45,12 +44,13 @@ struct _smpr_t { ...@@ -45,12 +44,13 @@ struct _smpr_t {
const smpr_proto_ops_t *ops; const smpr_proto_ops_t *ops;
}; };
static smpr_err_e _smpr_register_proto_ops (smpr_type_e type, static smpr_err_e _smpr_register_proto_ops (const smpr_proto_ops_t **ops,
const smpr_proto_ops_t **ops); const smpr_proto_ops_t *reg_ops);
static smpr_err_e _smpr_unregister_proto_ops (const smpr_proto_ops_t **ops); static smpr_err_e _smpr_unregister_proto_ops (const smpr_proto_ops_t **ops);
/* Creates a new instance of the Low-level I/O */ /* Creates a new instance of the Low-level I/O */
smpr_t * smpr_new (char *name, smio_t *parent, smpr_type_e type, int verbose) smpr_t * smpr_new (char *name, smio_t *parent, const smpr_proto_ops_t *reg_ops,
int verbose)
{ {
assert (name); assert (name);
assert (parent); assert (parent);
...@@ -58,8 +58,7 @@ smpr_t * smpr_new (char *name, smio_t *parent, smpr_type_e type, int verbose) ...@@ -58,8 +58,7 @@ smpr_t * smpr_new (char *name, smio_t *parent, smpr_type_e type, int verbose)
smpr_t *self = (smpr_t *) zmalloc (sizeof *self); smpr_t *self = (smpr_t *) zmalloc (sizeof *self);
ASSERT_ALLOC(self, err_self_alloc); ASSERT_ALLOC(self, err_self_alloc);
/* Initialize Protocol type */ /* Initialize Protocol */
self->type = type;
self->proto_handler = NULL; /* This is set by the specific protocol functions */ self->proto_handler = NULL; /* This is set by the specific protocol functions */
self->name = strdup (name); self->name = strdup (name);
ASSERT_ALLOC(self->name, err_name_alloc); ASSERT_ALLOC(self->name, err_name_alloc);
...@@ -71,7 +70,7 @@ smpr_t * smpr_new (char *name, smio_t *parent, smpr_type_e type, int verbose) ...@@ -71,7 +70,7 @@ smpr_t * smpr_new (char *name, smio_t *parent, smpr_type_e type, int verbose)
err_parent_null); err_parent_null);
/* Attach protocol operation to instance of smpr */ /* Attach protocol operation to instance of smpr */
smpr_err_e err = _smpr_register_proto_ops (type, &self->ops); smpr_err_e err = _smpr_register_proto_ops (&self->ops, reg_ops);
ASSERT_TEST(err == SMPR_SUCCESS, "Could not register SMPR operation", ASSERT_TEST(err == SMPR_SUCCESS, "Could not register SMPR operation",
err_register_ops); err_register_ops);
...@@ -148,41 +147,23 @@ smio_t *smpr_get_parent (smpr_t *self) ...@@ -148,41 +147,23 @@ smio_t *smpr_get_parent (smpr_t *self)
return self->parent; return self->parent;
} }
const char *smpr_get_ops_name (smpr_t *self)
{
assert (self);
if (self->ops == NULL) {
return NULL;
}
return self->ops->proto_name;
}
/**************** Helper Functions ***************/ /**************** Helper Functions ***************/
/* Register Specific Protocol operations to smpr instance. Helper function */ /* Register Specific Protocol operations to smpr instance. Helper function */
static smpr_err_e _smpr_register_proto_ops (smpr_type_e type, const smpr_proto_ops_t **ops) static smpr_err_e _smpr_register_proto_ops (const smpr_proto_ops_t **ops,
const smpr_proto_ops_t *reg_ops)
{ {
switch (type) { *ops = reg_ops;
case SMPR_SPI:
*ops = &smpr_proto_ops_spi;
break;
case SMPR_I2C:
*ops = &smpr_proto_ops_i2c;
break;
case SMPR_BSMP:
*ops = &smpr_proto_ops_bsmp;
break;
/*case SMPR_1WIRE:
*ops = &smpr_proto_ops_1wire;
break;
case SMPR_GPIO:
*ops = &smpr_proto_ops_gpio;
break;
case SMPR_BYPASS:
*ops = &smpr_proto_ops_bypass;
break;*/
default:
*ops = NULL;
return SMPR_ERR_INV_FUNC_PARAM;
}
DBE_DEBUG (DBG_SM_PR | DBG_LVL_INFO, "[sm_pr] Proto ops set\n"); DBE_DEBUG (DBG_SM_PR | DBG_LVL_INFO, "[sm_pr] Proto ops set\n");
return SMPR_SUCCESS; return SMPR_SUCCESS;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment