#define _DEFAULT_SOURCE #define _BSD_SOURCE #define _PCIDEV_MODEL_C #include #include #include #include #include #include #include #include #include #include #include #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; }