summaryrefslogtreecommitdiff
path: root/drivers/l3gd20.h
blob: c7e8935a48d5a0033428f8c5f5e4952deb1279af (plain)
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
#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