Statistics
| Branch: | Tag: | Revision:

amiro-os / components / bus / i2c / VI2CDriver.cpp @ f1d13b04

History | View | Annotate | Download (1.496 KB)

1 58fe0e0b Thomas Schöpping
#include <chdebug.h>
2
#include <amiro/bus/i2c/I2CMultiplexer.hpp>
3
#include <amiro/bus/i2c/VI2CDriver.hpp>
4
5
namespace amiro {
6
7
VI2CDriver::
8
VI2CDriver(I2CMultiplexer *driver, const uint8_t bus_id) :
9
  driver(driver),
10
  id(bus_id) {
11
12
#if CH_USE_MUTEXES
13
  chMtxInit(&this->mutex);
14
#else
15
  chSemInit(&this->semaphore, 1);
16
#endif /* CH_USE_MUTEXES */
17
18
};
19
20
21
void
22
VI2CDriver::
23
start(const I2CConfig* config) {
24
25
  /* silently discard config */
26
  (void) config;
27
  /* ignore starts */
28
29
}
30
31
void
32
VI2CDriver::
33
stop() {
34
35
  /* ignore stops */
36
37
}
38
39
i2cflags_t
40
VI2CDriver::
41
getErrors() {
42
43
  return this->driver->getErrors();
44
45
}
46
47
msg_t
48
VI2CDriver::
49
masterTransmit(const I2CTxParams *params, systime_t timeout) {
50
51
  msg_t ret;
52
53
  this->driver->acquireBus();
54
55
  if (!(ret = this->driver->select(this->id))) {
56
    ret = this->driver->masterTransmit(params, timeout);
57
  }
58
59
  this->driver->deselect();
60
  this->driver->releaseBus();
61
62
  return ret;
63
}
64
65
msg_t
66
VI2CDriver::
67
masterReceive(const I2CRxParams *params, systime_t timeout) {
68
69
  msg_t ret;
70
71
  this->driver->acquireBus();
72
73
  if (!(ret = this->driver->select(this->id)))
74
    ret = this->driver->masterReceive(params, timeout);
75
76
  this->driver->deselect();
77
  this->driver->releaseBus();
78
79
  return ret;
80
81
}
82
83
84
void
85
VI2CDriver::
86
acquireBus() {
87
88
#if CH_USE_MUTEXES
89
  chMtxLock(&this->mutex);
90
#elif CH_USE_SEMAPHORES
91
  chSemWait(&this->semaphore);
92
#endif
93
94
}
95
96
void
97
VI2CDriver::
98
releaseBus() {
99
100
#if CH_USE_MUTEXES
101
  chMtxUnlock();
102
#elif CH_USE_SEMAPHORES
103
  chSemSignal(&this->semaphore);
104
#endif
105
106
}
107
108
}
109
/* amiro */