Newer
Older
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
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdint.h>
#include <unistd.h>
#include "mraa.hpp"
using namespace mraa;
using namespace std;
#pragma region Definitions
#define USER_CTRL 0x6A
#define WHO_AM_I 0x75
#define AM_I_TRUE true
#pragma endregion
int main(int argc, char **argv)
{
// set up the mraa library
mraa_init();
// set the SPI channels
Spi* spi = new Spi(0);
// set the frequency ( 10Mhz )
spi->frequency(1000000);
// set the mode
spi->mode(MRAA_SPI_MODE1);
// ensure MSB is sent first
spi->lsbmode(false);
usleep(1000000);
// This disables I2C from the start
spi->write_word(0x10 | USER_CTRL);
usleep(1000000);
while (AM_I_TRUE)
{
uint8_t result = spi->write_word(WHO_AM_I);
cout << "I should be 0x71, I am " << hex << result << endl;
usleep(1000000);
}
while (!AM_I_TRUE)
{
uint16_t gyroX = spi->write_word(0x43) | spi->write_word(0x44);
uint16_t gyroY = spi->write_word(0x45) | spi->write_word(0x46);
uint16_t gyroZ = spi->write_word(0x47) | spi->write_word(0x48);
cout << "X: " << hex << gyroX << " Y: " << hex << gyroY << " Z: " << hex << gyroZ << endl;
usleep(1000000);
}
delete spi;
}