diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2023-05-25 22:41:04 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2023-05-25 22:41:04 +0200 |
commit | 6f4af841f6fdd099b97d071ae64c8be60f809456 (patch) | |
tree | d4f9a18b38e1ce3cfc0a5336215d5ce3afe830d2 /registers.c | |
download | pcidev-6f4af841f6fdd099b97d071ae64c8be60f809456.tar.gz pcidev-6f4af841f6fdd099b97d071ae64c8be60f809456.tar.bz2 pcidev-6f4af841f6fdd099b97d071ae64c8be60f809456.tar.xz pcidev-6f4af841f6fdd099b97d071ae64c8be60f809456.zip |
A sample event engine for pcitool (not requiring any PCIe hardware). Initial (barely tested and intended only as an example) release
Diffstat (limited to 'registers.c')
-rw-r--r-- | registers.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/registers.c b/registers.c new file mode 100644 index 0000000..1c7ce2d --- /dev/null +++ b/registers.c @@ -0,0 +1,103 @@ +#define _DEFAULT_SOURCE +#define _BSD_SOURCE +#define _PCIDEV_MODEL_C +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> +#include <unistd.h> +#include <assert.h> + +#include <pcilib.h> +#include <pcilib/tools.h> +#include <pcilib/error.h> +#include <pcilib/locking.h> +#include <pcilib/model.h> +#include <pcilib/datacpy.h> + +#include "registers.h" +#include "private.h" + + + +typedef struct pcidev_register_context_s pcidev_register_context_t; + +struct pcidev_register_context_s { + pcilib_register_bank_context_t bank_ctx; /**< the bank context associated with the software registers */ + pcilib_lock_t *lock; /**< the lock to serialize access through GPIO */ +}; + + +pcilib_register_bank_context_t* pcidev_regfile_open(pcilib_t *ctx, pcilib_register_bank_t bank, const char* model, const void *args) { + pcidev_register_context_t *bank_ctx; + + bank_ctx = calloc(1, sizeof(pcidev_register_context_t)); + if (!bank_ctx) { + pcilib_error("Memory allocation for bank context has failed"); + return NULL; + } + + bank_ctx->lock = pcilib_get_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, "registers"); + if (!bank_ctx->lock) { + pcidev_regfile_close(ctx, (pcilib_register_bank_context_t*)bank_ctx); + pcilib_error("Failed to initialize a lock to protect CMOSIS register bank"); + return NULL; + } + + srandom(1); + + return (pcilib_register_bank_context_t*)bank_ctx; +} + +void pcidev_regfile_close(pcilib_t *ctx, pcilib_register_bank_context_t *reg_bank_ctx) { + pcidev_register_context_t *bank_ctx = (pcidev_register_context_t*)reg_bank_ctx; + + if (bank_ctx->lock) + pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, bank_ctx->lock); + free(bank_ctx); +} + + +int pcidev_register_read(pcilib_t *ctx, pcilib_register_bank_context_t *reg_bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t *value) { + int err; + uint32_t val; + + pcidev_register_context_t *bank_ctx = (pcidev_register_context_t*)reg_bank_ctx; + const pcilib_register_bank_description_t *bank = reg_bank_ctx->bank; + + +retry: + err = pcilib_lock(bank_ctx->lock); + if (err) { + pcilib_error("Error (%i) obtaining a lock to serialize access to registers ", err); + return err; + } + + val = random(); + + pcilib_unlock(bank_ctx->lock); + + *value = val; + + return 0; +} + +int pcidev_register_write(pcilib_t *ctx, pcilib_register_bank_context_t *reg_bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t value) { + int err; + + pcidev_register_context_t *bank_ctx = (pcidev_register_context_t*)reg_bank_ctx; + const pcilib_register_bank_description_t *bank = reg_bank_ctx->bank; + +retry: + err = pcilib_lock(bank_ctx->lock); + if (err) { + pcilib_error("Error (%i) obtaining a lock to serialize access to CMOSIS registers ", err); + return err; + } + + pcilib_unlock(bank_ctx->lock); + + return 0; +} + + + |