summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2012-04-22 23:20:26 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2012-04-22 23:20:26 +0200
commit55d20ed5c237c6e3864af472cdb350dced7913a0 (patch)
tree8720d441b3bf77143c1d53a1b2368a7d4f7be064 /drivers
parent63a58dcd75b7fb553dc79539da7582c2f521dbbb (diff)
Added L3GD20 driver.
Diffstat (limited to 'drivers')
-rw-r--r--drivers/l3gd20.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/drivers/l3gd20.h b/drivers/l3gd20.h
new file mode 100644
index 0000000..c7e8935
--- /dev/null
+++ b/drivers/l3gd20.h
@@ -0,0 +1,40 @@
+#ifndef L3GD20_H
+#define L3GD20_H
+
+#include "pin.h"
+#include "spi.h"
+
+class L3GD20 {
+ private:
+ Pin& cs;
+ SPI_t& spi;
+
+ public:
+ int16_t x, y, z;
+
+ L3GD20(Pin& cs_pin, SPI_t& spi_bus) : cs(cs_pin), spi(spi_bus) {}
+
+ void init() {
+ spi.reg.CR1 = 0x37f;
+ cs.off();
+ spi.reg.DR = 0x40 | 0x20; while(!(spi.reg.SR & 0x01)); (void)spi.reg.DR;
+ spi.reg.DR = 0x0f; while(!(spi.reg.SR & 0x01)); (void)spi.reg.DR;
+ cs.on();
+ }
+
+ void update() {
+ spi.reg.CR1 = 0x37f;
+ cs.off();
+ spi.reg.DR = 0xc0 | 0x28; while(!(spi.reg.SR & 0x01)); (void)spi.reg.DR;
+ spi.reg.DR = 0; while(!(spi.reg.SR & 0x01)); x = spi.reg.DR;
+ spi.reg.DR = 0; while(!(spi.reg.SR & 0x01)); x |= spi.reg.DR << 8;
+ spi.reg.DR = 0; while(!(spi.reg.SR & 0x01)); y = spi.reg.DR;
+ spi.reg.DR = 0; while(!(spi.reg.SR & 0x01)); y |= spi.reg.DR << 8;
+ spi.reg.DR = 0; while(!(spi.reg.SR & 0x01)); z = spi.reg.DR;
+ spi.reg.DR = 0; while(!(spi.reg.SR & 0x01)); z |= spi.reg.DR << 8;
+ cs.on();
+ }
+};
+
+
+#endif