Commit 5127b1d7 authored by Lucas Russo's avatar Lucas Russo

sm_io/protocols/*: restrict ops pointer access to inner class

In this way, the only option to get the operations
table for a particular "protocol" is to instantiate
the appropriate class and use the *_get_ops ()
function.

With this, we can be safe that the user will always
pass the pointer ops that is inside the "protocol"
structure (along with its options).
parent f64c4681
......@@ -130,12 +130,10 @@ typedef struct _zmq_server_args_t zmq_server_args_t;
#include "sm_pr_err.h"
#include "sm_pr.h"
#include "sm_pr_spi.h"
#include "sm_pr_spi_obj.h"
#include "hw/wb_spi_regs.h"
#include "protocols/smpr_spi_regs.h"
#include "protocols/sm_pr_spi_defaults.h"
#include "sm_pr_i2c.h"
#include "sm_pr_i2c_obj.h"
#include "hw/wb_i2c_regs.h"
#include "protocols/smpr_i2c_regs.h"
#include "protocols/sm_pr_i2c_defaults.h"
......
......@@ -24,8 +24,28 @@ typedef enum {
I2C_MODE_REP_START, /* Repetitive start mode */
} i2c_mode_e;
/* For use by llio_t general structure */
extern const smpr_proto_ops_t smpr_proto_ops_i2c;
/* Creates a new instance of the proto_i2c */
smpr_i2c_t *smpr_i2c_new (uint32_t rep_start, uint32_t trans_size, uint32_t addr);
/* Destroy an instance of the i2c */
smpr_err_e smpr_i2c_destroy (smpr_i2c_t **self_p);
/* Set I2C rep_start parameter */
smpr_err_e smpr_i2c_set_rep_start (smpr_i2c_t *self, uint32_t rep_start);
/* Get I2C rep_start parameter */
uint32_t smpr_i2c_get_rep_start (smpr_i2c_t *self);
/* Set I2C trans_size parameter */
smpr_err_e smpr_i2c_set_trans_size (smpr_i2c_t *self, uint32_t trans_size);
/* Get I2C trans_size parameter */
uint32_t smpr_i2c_get_trans_size (smpr_i2c_t *self);
/* Set I2C addr parameter */
smpr_err_e smpr_i2c_set_addr (smpr_i2c_t *self, uint32_t addr);
/* Get I2C addr parameter */
uint32_t smpr_i2c_get_addr (smpr_i2c_t *self);
/* Get I2C operations */
const smpr_proto_ops_t *smpr_i2c_get_ops (smpr_i2c_t *self);
#ifdef __cplusplus
}
......
/*
* Copyright (C) 2016 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU GPL, version 3 or any later version.
*/
#ifndef _SM_PR_I2C_OBJ_H_
#define _SM_PR_I2C_OBJ_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Creates a new instance of the proto_i2c */
smpr_i2c_t *smpr_i2c_new (uint32_t rep_start, uint32_t trans_size, uint32_t addr);
/* Destroy an instance of the i2c */
smpr_err_e smpr_i2c_destroy (smpr_i2c_t **self_p);
/* Set I2C rep_start parameter */
smpr_err_e smpr_i2c_set_rep_start (smpr_i2c_t *self, uint32_t rep_start);
/* Get I2C rep_start parameter */
uint32_t smpr_i2c_get_rep_start (smpr_i2c_t *self);
/* Set I2C trans_size parameter */
smpr_err_e smpr_i2c_set_trans_size (smpr_i2c_t *self, uint32_t trans_size);
/* Get I2C trans_size parameter */
uint32_t smpr_i2c_get_trans_size (smpr_i2c_t *self);
/* Set I2C addr parameter */
smpr_err_e smpr_i2c_set_addr (smpr_i2c_t *self, uint32_t addr);
/* Get I2C addr parameter */
uint32_t smpr_i2c_get_addr (smpr_i2c_t *self);
/* Get SPI CHARLEN parameter */
const smpr_proto_ops_t *smpr_i2c_get_ops (smpr_i2c_t *self);
#ifdef __cplusplus
}
#endif
#endif
......@@ -26,8 +26,25 @@ typedef enum {
SPI_MODE_WRITE_READ
} spi_mode_e;
/* For use by llio_t general structure */
extern const smpr_proto_ops_t smpr_proto_ops_spi;
/************************* Our Methods **************************/
/* Creates a new instance of the proto_spi */
smpr_spi_t *smpr_spi_new (uint32_t ss, uint32_t charlen);
/* Destroy an instance of the spi */
smpr_err_e smpr_spi_destroy (smpr_spi_t **self_p);
/* Set SPI SS parameter */
smpr_err_e smpr_spi_set_ss (smpr_spi_t *self, uint32_t ss);
/* Get SPI SS parameter */
uint32_t smpr_spi_get_ss (smpr_spi_t *self);
/* Set SPI CHARLEN parameter */
smpr_err_e smpr_spi_set_charlen (smpr_spi_t *self, uint32_t charlen);
/* Get SPI CHARLEN parameter */
uint32_t smpr_spi_get_charlen (smpr_spi_t *self);
/* Get SPI operations */
const smpr_proto_ops_t *smpr_spi_get_ops (smpr_spi_t *self);
#ifdef __cplusplus
}
......
/*
* Copyright (C) 2016 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU GPL, version 3 or any later version.
*/
#ifndef _SM_PR_SPI_OBJ_H_
#define _SM_PR_SPI_OBJ_H_
#ifdef __cplusplus
extern "C" {
#endif
/* Creates a new instance of the proto_spi */
smpr_spi_t *smpr_spi_new (uint32_t ss, uint32_t charlen);
/* Destroy an instance of the spi */
smpr_err_e smpr_spi_destroy (smpr_spi_t **self_p);
/* Set SPI SS parameter */
smpr_err_e smpr_spi_set_ss (smpr_spi_t *self, uint32_t ss);
/* Get SPI SS parameter */
uint32_t smpr_spi_get_ss (smpr_spi_t *self);
/* Set SPI CHARLEN parameter */
smpr_err_e smpr_spi_set_charlen (smpr_spi_t *self, uint32_t charlen);
/* Get SPI CHARLEN parameter */
uint32_t smpr_spi_get_charlen (smpr_spi_t *self);
/* Get SPI CHARLEN parameter */
const smpr_proto_ops_t *smpr_spi_get_ops (smpr_spi_t *self);
#ifdef __cplusplus
}
#endif
#endif
sm_io_protocols_ops_DIR = $(SRC_DIR)/sm_io/protocols/ops
sm_io_protocols_ops_OBJS = $(sm_io_protocols_ops_DIR)/sm_pr_spi.o \
$(sm_io_protocols_ops_DIR)/sm_pr_spi_obj.o \
$(sm_io_protocols_ops_DIR)/sm_pr_i2c.o \
$(sm_io_protocols_ops_DIR)/sm_pr_i2c_obj.o \
$(sm_io_protocols_ops_DIR)/sm_pr_bsmp.o
# $(sm_io_protocols_ops_DIR)/sm_pr_bypass.o \
# $(sm_io_protocols_ops_DIR)/sm_pr_1w.o \
......
......@@ -49,6 +49,15 @@ typedef struct {
i2c_mode_e mode; /* I2C mode */
} smpr_proto_i2c_t;
/* Protocol object specification */
struct _smpr_i2c_t {
/* Must be located first */
smpr_proto_ops_t proto_ops; /* I2C protocol operations */
uint32_t rep_start; /* I2C repetitive start */
uint32_t trans_size; /* I2C transaction size */
uint32_t addr; /* I2C transaction size */
};
static smpr_err_e _i2c_init (smpr_t *self);
static ssize_t _i2c_check_transfer (smpr_t *self, bool ack_check);
static smpr_err_e _i2c_set_mode (smpr_t *self);
......@@ -92,7 +101,7 @@ static smpr_err_e smpr_proto_i2c_destroy (smpr_proto_i2c_t **self_p)
/************ smpr_proto_ops_i2c Implementation **********/
/* Open I2C protocol */
int i2c_open (smpr_t *self, uint64_t base, void *args)
static int i2c_open (smpr_t *self, uint64_t base, void *args)
{
assert (self);
......@@ -143,7 +152,7 @@ err_proto_handler_alloc:
}
/* Release I2C protocol device */
int i2c_release (smpr_t *self)
static int i2c_release (smpr_t *self)
{
assert (self);
......@@ -163,7 +172,7 @@ err_proto_handler_unset:
}
/* Read 16-bit data from I2C */
ssize_t i2c_read_16 (smpr_t *self, uint64_t offs, uint16_t *data)
static ssize_t i2c_read_16 (smpr_t *self, uint64_t offs, uint16_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -172,14 +181,14 @@ ssize_t i2c_read_16 (smpr_t *self, uint64_t offs, uint16_t *data)
}
/* Write 16-bit data to PCIe device */
ssize_t i2c_write_16 (smpr_t *self, uint64_t offs, const uint16_t *data)
static ssize_t i2c_write_16 (smpr_t *self, uint64_t offs, const uint16_t *data)
{
(void) offs;
return _i2c_write_generic (self, (uint8_t *) data, sizeof(*data));
}
/* Read 32-bit data from I2C */
ssize_t i2c_read_32 (smpr_t *self, uint64_t offs, uint32_t *data)
static ssize_t i2c_read_32 (smpr_t *self, uint64_t offs, uint32_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -188,14 +197,14 @@ ssize_t i2c_read_32 (smpr_t *self, uint64_t offs, uint32_t *data)
}
/* Write 32-bit data to PCIe device */
ssize_t i2c_write_32 (smpr_t *self, uint64_t offs, const uint32_t *data)
static ssize_t i2c_write_32 (smpr_t *self, uint64_t offs, const uint32_t *data)
{
(void) offs;
return _i2c_write_generic (self, (uint8_t *) data, sizeof(*data));
}
/* Read 64-bit data from I2C */
ssize_t i2c_read_64 (smpr_t *self, uint64_t offs, uint64_t *data)
static ssize_t i2c_read_64 (smpr_t *self, uint64_t offs, uint64_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -204,14 +213,14 @@ ssize_t i2c_read_64 (smpr_t *self, uint64_t offs, uint64_t *data)
}
/* Write 64-bit data to PCIe device */
ssize_t i2c_write_64 (smpr_t *self, uint64_t offs, const uint64_t *data)
static ssize_t i2c_write_64 (smpr_t *self, uint64_t offs, const uint64_t *data)
{
(void) offs;
return _i2c_write_generic (self, (uint8_t *) data, sizeof(*data));
}
/* Read data block from PCIe device, size in bytes */
ssize_t i2c_read_block (smpr_t *self, uint64_t offs, size_t size, uint32_t *data)
static ssize_t i2c_read_block (smpr_t *self, uint64_t offs, size_t size, uint32_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -220,7 +229,7 @@ ssize_t i2c_read_block (smpr_t *self, uint64_t offs, size_t size, uint32_t *data
}
/* Write data block from PCIe device, size in bytes */
ssize_t i2c_write_block (smpr_t *self, uint64_t offs, size_t size, const uint32_t *data)
static ssize_t i2c_write_block (smpr_t *self, uint64_t offs, size_t size, const uint32_t *data)
{
(void) offs;
return _i2c_write_generic (self, (uint8_t *) data, size);
......@@ -655,7 +664,7 @@ err_exit:
}
#endif
const smpr_proto_ops_t smpr_proto_ops_i2c = {
static const smpr_proto_ops_t smpr_proto_ops_i2c = {
.proto_name = "I2C", /* Protocol name */
.proto_open = i2c_open, /* Open device */
.proto_release = i2c_release, /* Release device */
......@@ -674,3 +683,88 @@ const smpr_proto_ops_t smpr_proto_ops_i2c = {
.proto_write_dma = NULL /* Write arbitrary block size data via DMA,
parameter size in bytes */
};
/************ Our methods implementation **********/
/* Creates a new instance of the proto_i2c */
smpr_i2c_t *smpr_i2c_new (uint32_t rep_start, uint32_t trans_size, uint32_t addr)
{
smpr_i2c_t *self = (smpr_i2c_t *) zmalloc (sizeof *self);
ASSERT_ALLOC (self, err_smpr_i2c_alloc);
/* copy I2C operations */
self->proto_ops = smpr_proto_ops_i2c;
self->rep_start = rep_start;
self->trans_size = trans_size;
self->addr = addr;
return self;
err_smpr_i2c_alloc:
return NULL;
}
/* Destroy an instance of the i2c */
smpr_err_e smpr_i2c_destroy (smpr_i2c_t **self_p)
{
assert (self_p);
if (*self_p) {
smpr_i2c_t *self = *self_p;
free (self);
self_p = NULL;
}
return SMPR_SUCCESS;
}
smpr_err_e smpr_i2c_set_rep_start (smpr_i2c_t *self, uint32_t rep_start)
{
assert (self);
self->rep_start = rep_start;
return SMPR_SUCCESS;
}
uint32_t smpr_i2c_get_rep_start (smpr_i2c_t *self)
{
assert (self);
return self->rep_start;
}
smpr_err_e smpr_i2c_set_trans_size (smpr_i2c_t *self, uint32_t trans_size)
{
assert (self);
self->trans_size = trans_size;
return SMPR_SUCCESS;
}
uint32_t smpr_i2c_get_trans_size (smpr_i2c_t *self)
{
assert (self);
return self->trans_size;
}
smpr_err_e smpr_i2c_set_addr (smpr_i2c_t *self, uint32_t addr)
{
assert (self);
self->addr = addr;
return SMPR_SUCCESS;
}
uint32_t smpr_i2c_get_addr (smpr_i2c_t *self)
{
assert (self);
return self->addr;
}
const smpr_proto_ops_t *smpr_i2c_get_ops (smpr_i2c_t *self)
{
assert (self);
return &self->proto_ops;
}
/*
* Copyright (C) 2016 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU GPL, version 3 or any later version.
*/
#include "bpm_server.h"
/* Undef ASSERT_ALLOC to avoid conflicting with other ASSERT_ALLOC */
#ifdef ASSERT_TEST
#undef ASSERT_TEST
#endif
#define ASSERT_TEST(test_boolean, err_str, err_goto_label, /* err_core */ ...) \
ASSERT_HAL_TEST(test_boolean, SM_PR, "[sm_pr:i2c_obj]", \
err_str, err_goto_label, /* err_core */ __VA_ARGS__)
#ifdef ASSERT_ALLOC
#undef ASSERT_ALLOC
#endif
#define ASSERT_ALLOC(ptr, err_goto_label, /* err_core */ ...) \
ASSERT_HAL_ALLOC(ptr, SM_PR, "[sm_pr:i2c_obj]", \
smpr_err_str(SMPR_ERR_ALLOC), \
err_goto_label, /* err_core */ __VA_ARGS__)
#ifdef CHECK_ERR
#undef CHECK_ERR
#endif
#define CHECK_ERR(err, err_type) \
CHECK_HAL_ERR(err, SM_PR, "[sm_pr:i2c_obj]", \
smpr_err_str (err_type))
/* Protocol object specification */
struct _smpr_i2c_t {
/* Must be located first */
smpr_proto_ops_t proto_ops; /* I2C protocol operations */
uint32_t rep_start; /* I2C repetitive start */
uint32_t trans_size; /* I2C transaction size */
uint32_t addr; /* I2C transaction size */
};
/************ Our methods implementation **********/
/* Creates a new instance of the proto_i2c */
smpr_i2c_t *smpr_i2c_new (uint32_t rep_start, uint32_t trans_size, uint32_t addr)
{
smpr_i2c_t *self = (smpr_i2c_t *) zmalloc (sizeof *self);
ASSERT_ALLOC (self, err_smpr_i2c_alloc);
/* copy I2C operations */
self->proto_ops = smpr_proto_ops_i2c;
self->rep_start = rep_start;
self->trans_size = trans_size;
self->addr = addr;
return self;
err_smpr_i2c_alloc:
return NULL;
}
/* Destroy an instance of the i2c */
smpr_err_e smpr_i2c_destroy (smpr_i2c_t **self_p)
{
assert (self_p);
if (*self_p) {
smpr_i2c_t *self = *self_p;
free (self);
self_p = NULL;
}
return SMPR_SUCCESS;
}
smpr_err_e smpr_i2c_set_rep_start (smpr_i2c_t *self, uint32_t rep_start)
{
assert (self);
self->rep_start = rep_start;
return SMPR_SUCCESS;
}
uint32_t smpr_i2c_get_rep_start (smpr_i2c_t *self)
{
assert (self);
return self->rep_start;
}
smpr_err_e smpr_i2c_set_trans_size (smpr_i2c_t *self, uint32_t trans_size)
{
assert (self);
self->trans_size = trans_size;
return SMPR_SUCCESS;
}
uint32_t smpr_i2c_get_trans_size (smpr_i2c_t *self)
{
assert (self);
return self->trans_size;
}
smpr_err_e smpr_i2c_set_addr (smpr_i2c_t *self, uint32_t addr)
{
assert (self);
self->addr = addr;
return SMPR_SUCCESS;
}
uint32_t smpr_i2c_get_addr (smpr_i2c_t *self)
{
assert (self);
return self->addr;
}
const smpr_proto_ops_t *smpr_i2c_get_ops (smpr_i2c_t *self)
{
assert (self);
return &self->proto_ops;
}
......@@ -51,6 +51,15 @@ typedef struct {
bool bidir; /* SPI bidirectional control enable */
} smpr_proto_spi_t;
/* Protocol object specification */
struct _smpr_spi_t {
/* Must be located first */
smpr_proto_ops_t proto_ops; /* SPI protocol operations */
uint32_t ss; /* SPI Slave Select */
uint32_t charlen; /* SPI character length:
0 is 128-bit data word, 1 is 1 bit, 2 is 2-bit and so on */
};
static smpr_err_e _spi_init (smpr_t *self);
static ssize_t _spi_read_write_generic (smpr_t *self, uint8_t *data,
size_t size, spi_mode_e mode);
......@@ -89,7 +98,7 @@ static smpr_err_e smpr_proto_spi_destroy (smpr_proto_spi_t **self_p)
/************ smpr_proto_ops_spi Implementation **********/
/* Open SPI protocol */
int spi_open (smpr_t *self, uint64_t base, void *args)
static int spi_open (smpr_t *self, uint64_t base, void *args)
{
assert (self);
......@@ -142,7 +151,7 @@ err_proto_handler_alloc:
}
/* Release SPI protocol device */
int spi_release (smpr_t *self)
static int spi_release (smpr_t *self)
{
assert (self);
......@@ -163,7 +172,7 @@ err_proto_handler_unset:
}
/* Read 16-bit data from SPI */
ssize_t spi_read_16 (smpr_t *self, uint64_t offs, uint16_t *data)
static ssize_t spi_read_16 (smpr_t *self, uint64_t offs, uint16_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -173,7 +182,7 @@ ssize_t spi_read_16 (smpr_t *self, uint64_t offs, uint16_t *data)
}
/* Write 16-bit data to SPI device */
ssize_t spi_write_16 (smpr_t *self, uint64_t offs, const uint16_t *data)
static ssize_t spi_write_16 (smpr_t *self, uint64_t offs, const uint16_t *data)
{
(void) offs;
return _spi_read_write_generic (self, (uint8_t *) data, sizeof(*data),
......@@ -181,7 +190,7 @@ ssize_t spi_write_16 (smpr_t *self, uint64_t offs, const uint16_t *data)
}
/* Read 32-bit data from SPI */
ssize_t spi_read_32 (smpr_t *self, uint64_t offs, uint32_t *data)
static ssize_t spi_read_32 (smpr_t *self, uint64_t offs, uint32_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -191,7 +200,7 @@ ssize_t spi_read_32 (smpr_t *self, uint64_t offs, uint32_t *data)
}
/* Write 32-bit data to SPI device */
ssize_t spi_write_32 (smpr_t *self, uint64_t offs, const uint32_t *data)
static ssize_t spi_write_32 (smpr_t *self, uint64_t offs, const uint32_t *data)
{
(void) offs;
return _spi_read_write_generic (self, (uint8_t *) data, sizeof(*data),
......@@ -199,7 +208,7 @@ ssize_t spi_write_32 (smpr_t *self, uint64_t offs, const uint32_t *data)
}
/* Read 64-bit data from SPI */
ssize_t spi_read_64 (smpr_t *self, uint64_t offs, uint64_t *data)
static ssize_t spi_read_64 (smpr_t *self, uint64_t offs, uint64_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -209,7 +218,7 @@ ssize_t spi_read_64 (smpr_t *self, uint64_t offs, uint64_t *data)
}
/* Write 64-bit data to SPI device */
ssize_t spi_write_64 (smpr_t *self, uint64_t offs, const uint64_t *data)
static ssize_t spi_write_64 (smpr_t *self, uint64_t offs, const uint64_t *data)
{
(void) offs;
return _spi_read_write_generic (self, (uint8_t *) data, sizeof(*data),
......@@ -217,7 +226,7 @@ ssize_t spi_write_64 (smpr_t *self, uint64_t offs, const uint64_t *data)
}
/* Read data block from SPI device, size in bytes */
ssize_t spi_read_block (smpr_t *self, uint64_t offs, size_t size, uint32_t *data)
static ssize_t spi_read_block (smpr_t *self, uint64_t offs, size_t size, uint32_t *data)
{
(void) offs;
/* We want to request a read command from some off-FPGA chip. So, we
......@@ -227,7 +236,7 @@ ssize_t spi_read_block (smpr_t *self, uint64_t offs, size_t size, uint32_t *data
}
/* Write data block from SPI device, size in bytes */
ssize_t spi_write_block (smpr_t *self, uint64_t offs, size_t size, const uint32_t *data)
static ssize_t spi_write_block (smpr_t *self, uint64_t offs, size_t size, const uint32_t *data)
{
(void) offs;
return _spi_read_write_generic (self, (uint8_t *) data, size,
......@@ -452,7 +461,7 @@ err_proto_handler:
return err;
}
const smpr_proto_ops_t smpr_proto_ops_spi = {
static const smpr_proto_ops_t smpr_proto_ops_spi = {
.proto_name = "SPI", /* Protocol name */
.proto_open = spi_open, /* Open device */
.proto_release = spi_release, /* Release device */
......@@ -471,3 +480,72 @@ const smpr_proto_ops_t smpr_proto_ops_spi = {
.proto_write_dma = NULL /* Write arbitrary block size data via DMA,
parameter size in bytes */
};
/************ Our methods implementation **********/
/* Creates a new instance of the proto_spi */
smpr_spi_t *smpr_spi_new (uint32_t ss, uint32_t charlen)
{
smpr_spi_t *self = (smpr_spi_t *) zmalloc (sizeof *self);
ASSERT_ALLOC (self, err_smpr_spi_alloc);
/* copy SPI operations */
self->proto_ops = smpr_proto_ops_spi;
self->ss = ss;
self->charlen = charlen;
return self;
err_smpr_spi_alloc:
return NULL;
}
/* Destroy an instance of the spi */
smpr_err_e smpr_spi_destroy (smpr_spi_t **self_p)
{
assert (self_p);
if (*self_p) {
smpr_spi_t *self = *self_p;
free (self);
self_p = NULL;
}
return SMPR_SUCCESS;
}
smpr_err_e smpr_spi_set_ss (smpr_spi_t *self, uint32_t ss)
{
assert (self);
self->ss = ss;
return SMPR_SUCCESS;
}
uint32_t smpr_spi_get_ss (smpr_spi_t *self)
{
assert (self);
return self->ss;
}
smpr_err_e smpr_spi_set_charlen (smpr_spi_t *self, uint32_t charlen)
{
assert (self);
self->charlen = charlen;
return SMPR_SUCCESS;
}
uint32_t smpr_spi_get_charlen (smpr_spi_t *self)
{
assert (self);
return self->charlen;
}
const smpr_proto_ops_t *smpr_spi_get_ops (smpr_spi_t *self)
{
assert (self);
return &self->proto_ops;
}
/*
* Copyright (C) 2016 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU GPL, version 3 or any later version.
*/
#include "bpm_server.h"
/* Undef ASSERT_ALLOC to avoid conflicting with other ASSERT_ALLOC */
#ifdef ASSERT_TEST
#undef ASSERT_TEST
#endif
#define ASSERT_TEST(test_boolean, err_str, err_goto_label, /* err_core */ ...) \
ASSERT_HAL_TEST(test_boolean, SM_PR, "[sm_pr:spi_obj]", \
err_str, err_goto_label, /* err_core */ __VA_ARGS__)
#ifdef ASSERT_ALLOC
#undef ASSERT_ALLOC
#endif
#define ASSERT_ALLOC(ptr, err_goto_label, /* err_core */ ...) \
ASSERT_HAL_ALLOC(ptr, SM_PR, "[sm_pr:spi_obj]", \
smpr_err_str(SMPR_ERR_ALLOC), \
err_goto_label, /* err_core */ __VA_ARGS__)
#ifdef CHECK_ERR
#undef CHECK_ERR
#endif
#define CHECK_ERR(err, err_type) \
CHECK_HAL_ERR(err, SM_PR, "[sm_pr:spi_obj]", \
smpr_err_str (err_type))
/* Protocol object specification */
struct _smpr_spi_t {
/* Must be located first */
smpr_proto_ops_t proto_ops; /* SPI protocol operations */
uint32_t ss; /* SPI Slave Select */
uint32_t charlen; /* SPI character length:
0 is 128-bit data word, 1 is 1 bit, 2 is 2-bit and so on */
};
/************ Our methods implementation **********/
/* Creates a new instance of the proto_spi */
smpr_spi_t *smpr_spi_new (uint32_t ss, uint32_t charlen)
{
smpr_spi_t *self = (smpr_spi_t *) zmalloc (sizeof *self);
ASSERT_ALLOC (self, err_smpr_spi_alloc);
/* copy SPI operations */
self->proto_ops = smpr_proto_ops_spi;
self->ss = ss;
self->charlen = charlen;
return self;
err_smpr_spi_alloc:
return NULL;
}
/* Destroy an instance of the spi */
smpr_err_e smpr_spi_destroy (smpr_spi_t **self_p)
{
assert (self_p);
if (*self_p) {
smpr_spi_t *self = *self_p;
free (self);
self_p = NULL;
}
return SMPR_SUCCESS;
}
smpr_err_e smpr_spi_set_ss (smpr_spi_t *self, uint32_t ss)
{
assert (self);
self->ss = ss;
return SMPR_SUCCESS;
}
uint32_t smpr_spi_get_ss (smpr_spi_t *self)
{
assert (self);
return self->ss;
}
smpr_err_e smpr_spi_set_charlen (smpr_spi_t *self, uint32_t charlen)
{
assert (self);
self->charlen = charlen;
return SMPR_SUCCESS;
}
uint32_t smpr_spi_get_charlen (smpr_spi_t *self)
{
assert (self);
return self->charlen;
}
const smpr_proto_ops_t *smpr_spi_get_ops (smpr_spi_t *self)
{
assert (self);
return &self->proto_ops;
}
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