Skip to content

Commit 26b562b

Browse files
committed
Implement write_then_read style API for threadsafe Wire.
1 parent e3e793f commit 26b562b

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include <Arduino_ThreadsafeIO.h>
6+
7+
/**************************************************************************************
8+
* CONSTANTS
9+
**************************************************************************************/
10+
11+
static byte constexpr LSM6DSOX_ADDRESS = 0x6A;
12+
static byte constexpr LSM6DSOX_WHO_AM_I_REG = 0x0F;
13+
14+
static size_t constexpr NUM_THREADS = 20;
15+
16+
/**************************************************************************************
17+
* FUNCTION DECLARATION
18+
**************************************************************************************/
19+
20+
byte lsm6dsox_read_reg(byte const reg_addr);
21+
void lsm6dsox_thread_func();
22+
23+
/**************************************************************************************
24+
* GLOBAL VARIABLES
25+
**************************************************************************************/
26+
27+
BusDevice lsm6dsox(Wire, LSM6DSOX_ADDRESS);
28+
29+
static char thread_name[NUM_THREADS][32];
30+
31+
/**************************************************************************************
32+
* SETUP/LOOP
33+
**************************************************************************************/
34+
35+
void setup()
36+
{
37+
Serial.begin(9600);
38+
while (!Serial) { }
39+
40+
/* Fire up some threads all accessing the LSM6DSOX */
41+
for(size_t i = 0; i < NUM_THREADS; i++)
42+
{
43+
snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i);
44+
rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]);
45+
t->start(lsm6dsox_thread_func);
46+
}
47+
}
48+
49+
void loop()
50+
{
51+
52+
}
53+
54+
/**************************************************************************************
55+
* FUNCTION DEFINITION
56+
**************************************************************************************/
57+
58+
byte lsm6dsox_read_reg(byte const reg_addr)
59+
{
60+
byte read_buf = 0;
61+
lsm6dsox.wire().write_then_read(&reg_addr, 1, &read_buf, 1);
62+
return read_buf;
63+
}
64+
65+
void lsm6dsox_thread_func()
66+
{
67+
for(;;)
68+
{
69+
/* Sleep between 5 and 500 ms */
70+
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
71+
/* Try to read some data from the LSM6DSOX. */
72+
byte const who_am_i = lsm6dsox_read_reg(LSM6DSOX_WHO_AM_I_REG);
73+
/* Print thread id and chip id value to serial. */
74+
char msg[64] = {0};
75+
snprintf(msg, sizeof(msg), "%s: LSM6DSOX[WHO_AM_I] = 0x%X", rtos::ThisThread::get_name(), who_am_i);
76+
Serial.println(msg);
77+
}
78+
}

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ block KEYWORD2
2424
unblock KEYWORD2
2525
prefix KEYWORD2
2626
suffix KEYWORD2
27+
wire KEYWORD2
28+
write_then_read KEYWORD2
2729

2830
#######################################
2931
# Constants (LITERAL1)

src/BusDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@ IoResponse BusDevice::transfer(IoRequest & req)
109109
{
110110
return _dev->transfer(req);
111111
}
112+
113+
WireBusDevice & BusDevice::wire()
114+
{
115+
return *reinterpret_cast<WireBusDevice *>(_dev.get());
116+
}

src/BusDevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace arduino
3838
}
3939

4040
class BusDevice;
41+
class WireBusDevice;
4142

4243
/**************************************************************************************
4344
* CLASS DECLARATION
@@ -79,6 +80,9 @@ class BusDevice
7980
IoResponse transfer(IoRequest & req);
8081

8182

83+
WireBusDevice & wire();
84+
85+
8286
private:
8387

8488
mbed::SharedPtr<BusDeviceBase> _dev;

src/wire/WireBusDevice.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ class WireBusDevice : public BusDeviceBase
5050
}
5151

5252

53+
bool write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop = false)
54+
{
55+
/* Copy the Wire parameters from the device and modify only those
56+
* which can be modified via the parameters of this function.
57+
*/
58+
bool const restart = !stop;
59+
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), restart, _config.stop());
60+
/* Fire off the IO request and await its response. */
61+
IoRequest req(write_buffer, write_len, read_buffer, read_len);
62+
IoResponse rsp = WireDispatcher::instance().dispatch(&req, &config);
63+
rsp->wait();
64+
/* TODO: Introduce error codes within the IoResponse and evaluate
65+
* them here.
66+
*/
67+
return true;
68+
}
69+
5370
private:
5471

5572
WireBusDeviceConfig _config;

src/wire/WireDispatcher.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class WireDispatcher
4343
static WireDispatcher & instance();
4444
static void destroy();
4545

46+
4647
IoResponse dispatch(IoRequest * req, WireBusDeviceConfig * config);
4748

49+
4850
private:
4951

5052
static WireDispatcher * _p_instance;

0 commit comments

Comments
 (0)