1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#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;
}
|