amiro-os / components / eeprom / eeprom.cpp @ 2af9778e
History | View | Annotate | Download (1.703 KB)
1 |
#include <ch.hpp> |
---|---|
2 |
#include <hal.h> |
3 |
|
4 |
#include <amiro/eeprom/eeprom.hpp> |
5 |
|
6 |
namespace amiro {
|
7 |
|
8 |
#if HAL_USE_I2C
|
9 |
EEPROM:: |
10 |
EEPROM(const struct BaseFileStreamVMT* vmt, size_t size, uint8_t page_size, uint16_t max_t_wr, uint8_t sla, I2CDriver* i2c_driver) : |
11 |
error(EEPROM_ERROR_NONE), |
12 |
position(0x00u),
|
13 |
size(size), |
14 |
page_size(page_size), |
15 |
max_t_wr(max_t_wr), |
16 |
i2c_driver(i2c_driver) { |
17 |
|
18 |
#if CH_USE_MUTEXES
|
19 |
chMtxInit(&this->mutex);
|
20 |
#elif CH_USE_SEMAPHORES
|
21 |
chSemInit(&this->semaphore, 1); |
22 |
#endif
|
23 |
|
24 |
this->bfs.vmt = vmt;
|
25 |
this->i2c_txparams.addr = sla;
|
26 |
|
27 |
} |
28 |
#endif /* HAL_USE_I2C */ |
29 |
|
30 |
#if HAL_USE_SPI
|
31 |
EEPROM:: |
32 |
EEPROM(const struct BaseFileStreamVMT* vmt, size_t size, uint8_t page_size, uint16_t max_t_wr, HWSPIDriver* spi_driver) : |
33 |
error(EEPROM_ERROR_NONE), |
34 |
position(0x00u),
|
35 |
size(size), |
36 |
page_size(page_size), |
37 |
max_t_wr(max_t_wr), |
38 |
spi_driver(spi_driver) { |
39 |
|
40 |
#if CH_USE_MUTEXES
|
41 |
chMtxInit(&this->mutex);
|
42 |
#elif CH_USE_SEMAPHORES
|
43 |
chSemInit(&this->semaphore, 1); |
44 |
#endif
|
45 |
|
46 |
this->bfs.vmt = vmt;
|
47 |
|
48 |
} |
49 |
#endif /* HAL_USE_SPI */ |
50 |
|
51 |
EEPROM:: |
52 |
~EEPROM() { |
53 |
|
54 |
} |
55 |
|
56 |
void
|
57 |
EEPROM:: |
58 |
acquire() { |
59 |
|
60 |
#if CH_USE_MUTEXES
|
61 |
chMtxLock(&this->mutex);
|
62 |
#elif CH_USE_SEMAPHORES
|
63 |
chSemWait(&this->semaphore);
|
64 |
#endif
|
65 |
|
66 |
} |
67 |
|
68 |
void
|
69 |
EEPROM:: |
70 |
release() { |
71 |
|
72 |
#if CH_USE_MUTEXES
|
73 |
chMtxUnlock(); |
74 |
#elif CH_USE_SEMAPHORES
|
75 |
chSemSignal(&this->semaphore);
|
76 |
#endif
|
77 |
|
78 |
} |
79 |
|
80 |
int
|
81 |
EEPROM:: |
82 |
geterror(void* instance) {
|
83 |
|
84 |
return (int) ((EEPROM*) (instance))->error; |
85 |
|
86 |
} |
87 |
|
88 |
fileoffset_t |
89 |
EEPROM:: |
90 |
getsize(void *instance) {
|
91 |
|
92 |
return ((EEPROM*) (instance))->size;
|
93 |
|
94 |
} |
95 |
|
96 |
fileoffset_t |
97 |
EEPROM:: |
98 |
getposition(void* instance) {
|
99 |
|
100 |
return ((EEPROM*) (instance))->position;
|
101 |
|
102 |
|
103 |
} |
104 |
|
105 |
fileoffset_t |
106 |
EEPROM:: |
107 |
lseek(void *instance, fileoffset_t offset) {
|
108 |
|
109 |
((EEPROM*) (instance))->position = offset; |
110 |
return FILE_OK;
|
111 |
|
112 |
} |
113 |
|
114 |
}; |