|
| 1 | +/************************************************************************************** |
| 2 | + * INCLUDE |
| 3 | + **************************************************************************************/ |
| 4 | + |
| 5 | +#include <Arduino_ThreadsafeIO.h> |
| 6 | + |
| 7 | +/************************************************************************************** |
| 8 | + * CONSTANTS |
| 9 | + **************************************************************************************/ |
| 10 | + |
| 11 | +static int const BMP388_CS_PIN = 2; |
| 12 | +static int const BMP388_INT_PIN = 6; |
| 13 | +static byte const BMP388_CHIP_ID_REG_ADDR = 0x00; |
| 14 | + |
| 15 | +static size_t constexpr NUM_THREADS = 20; |
| 16 | + |
| 17 | +/************************************************************************************** |
| 18 | + * FUNCTION DECLARATION |
| 19 | + **************************************************************************************/ |
| 20 | + |
| 21 | +byte bmp388_read_reg(byte const reg_addr); |
| 22 | +void bmp388_thread_func(); |
| 23 | + |
| 24 | +/************************************************************************************** |
| 25 | + * GLOBAL VARIABLES |
| 26 | + **************************************************************************************/ |
| 27 | + |
| 28 | +BusDevice bmp388(SPI, BMP388_CS_PIN, 1000000, MSBFIRST, SPI_MODE0); |
| 29 | + |
| 30 | +static char thread_name[NUM_THREADS][32]; |
| 31 | + |
| 32 | +/************************************************************************************** |
| 33 | + * SETUP/LOOP |
| 34 | + **************************************************************************************/ |
| 35 | + |
| 36 | +void setup() |
| 37 | +{ |
| 38 | + Serial.begin(9600); |
| 39 | + while (!Serial) { } |
| 40 | + |
| 41 | + pinMode(BMP388_CS_PIN, OUTPUT); |
| 42 | + digitalWrite(BMP388_CS_PIN, HIGH); |
| 43 | + |
| 44 | + for(size_t i = 0; i < NUM_THREADS; i++) |
| 45 | + { |
| 46 | + snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i); |
| 47 | + rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]); |
| 48 | + t->start(bmp388_thread_func); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void loop() |
| 53 | +{ |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +/************************************************************************************** |
| 58 | + * FUNCTION DEFINITION |
| 59 | + **************************************************************************************/ |
| 60 | + |
| 61 | +byte bmp388_read_reg(byte const reg_addr) |
| 62 | +{ |
| 63 | + /* REG_ADDR | DUMMY_BYTE | REG_VAL is on SDO */ |
| 64 | + byte read_write_buf[] = {static_cast<byte>(0x80 | reg_addr), 0, 0}; |
| 65 | + |
| 66 | + IoRequest req(read_write_buf, sizeof(read_write_buf), nullptr, 0); |
| 67 | + IoResponse rsp = bmp388.transfer(req); |
| 68 | + |
| 69 | + /* Do other stuff */ |
| 70 | + |
| 71 | + rsp->wait(); |
| 72 | + |
| 73 | + return read_write_buf[2]; |
| 74 | +} |
| 75 | + |
| 76 | +void bmp388_thread_func() |
| 77 | +{ |
| 78 | + for(;;) |
| 79 | + { |
| 80 | + /* Sleep between 5 and 500 ms */ |
| 81 | + rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500))); |
| 82 | + /* Try to read some data from the BMP3888. */ |
| 83 | + byte const chip_id = bmp388_read_reg(BMP388_CHIP_ID_REG_ADDR); |
| 84 | + /* Print thread id and chip id value to serial. */ |
| 85 | + char msg[64] = {0}; |
| 86 | + snprintf(msg, sizeof(msg), "%s: Chip ID = 0x%X", rtos::ThisThread::get_name(), chip_id); |
| 87 | + Serial.println(msg); |
| 88 | + } |
| 89 | +} |
0 commit comments