summaryrefslogtreecommitdiff
path: root/hal/i2c.h
blob: 3d58056c8ad3f6017aa64c9f08cfc4c894022283 (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 I2C_H
#define I2C_H

#include <stdint.h>

#include "interrupt.h"

class I2C {
	friend void interrupt<Interrupt::I2C1_EV>();
	friend void interrupt<Interrupt::I2C1_ER>();
	
	private:
		static I2C* self;
		
		volatile uint8_t addr;
		volatile uint8_t writing;
		volatile uint8_t reading;
		volatile uint8_t* write_p;
		volatile uint8_t* read_p;
		
		volatile bool busy;
		
		void irq_ev();
		void irq_er();
		void handle_error();
	
	public:
		I2C() {
			self = this;
		}
		
		void enable();
		
		void write_reg(uint8_t addr_, uint8_t reg, uint8_t data);
		void read_reg(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf);
};



#endif