-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWire.ino
92 lines (71 loc) · 2.95 KB
/
Wire.ino
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
/* This example demonstrates how multiple threads can communicate
* with a single Wire client device using the BusDevice abstraction
* for Wire. In a similar way multiple threads can interface
* with different client devices on the same Wire bus.
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <Arduino_Threads.h>
/**************************************************************************************
* CONSTANTS
**************************************************************************************/
static byte constexpr LSM6DSOX_ADDRESS = 0x6A;
static byte constexpr LSM6DSOX_WHO_AM_I_REG = 0x0F;
static size_t constexpr NUM_THREADS = 20;
/**************************************************************************************
* FUNCTION DECLARATION
**************************************************************************************/
byte lsm6dsox_read_reg(byte const reg_addr);
void lsm6dsox_thread_func();
/**************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************/
BusDevice lsm6dsox(Wire, LSM6DSOX_ADDRESS);
static char thread_name[NUM_THREADS][32];
/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/
void setup()
{
/* Fire up some threads all accessing the LSM6DSOX */
for(size_t i = 0; i < NUM_THREADS; i++)
{
snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i);
rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]);
t->start(lsm6dsox_thread_func);
}
}
void loop()
{
}
/**************************************************************************************
* FUNCTION DEFINITION
**************************************************************************************/
byte lsm6dsox_read_reg(byte const reg_addr)
{
/* As we need only 1 byte large write/read buffers for this IO transaction
* the buffers are not arrays but rather simple variables.
*/
byte write_buf = reg_addr;
byte read_buf = 0;
IoRequest req(write_buf, read_buf);
IoResponse rsp = transferAndWait(lsm6dsox, req);
return read_buf;
}
void lsm6dsox_thread_func()
{
Serial.begin(9600);
while(!Serial) { }
for(;;)
{
/* Sleep between 5 and 500 ms */
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
/* Try to read some data from the LSM6DSOX. */
byte const who_am_i = lsm6dsox_read_reg(LSM6DSOX_WHO_AM_I_REG);
/* Print thread ID and chip ID value to serial. */
char msg[64] = {0};
snprintf(msg, sizeof(msg), "%s: LSM6DSOX[WHO_AM_I] = 0x%X", rtos::ThisThread::get_name(), who_am_i);
Serial.println(msg);
}
}