summaryrefslogtreecommitdiff
path: root/drivers/gps.h
blob: 2fcbf6d8113c3a3a3fc957cd1a91ecb91d1e167f (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef GPS_H
#define GPS_H

#include <rcc/rcc.h>
#include <usart/usart.h>
#include <interrupt/interrupt.h>
#include <os/thread.h>

#include <os/pool.h>

struct GPSMsg {
	unsigned int n;
	uint8_t buf[128];
	
	GPSMsg() : n(0) {}
};

class GPS {
	friend void interrupt<Interrupt::USART3>();
	
	private:
		static GPS* self;
		
		void irq();
		
		Pool<GPSMsg, 4> msg_pool;
		
		P<GPSMsg> incomplete_msg;
		volatile P<GPSMsg> complete_msg;
		
		volatile bool complete;
	
	public:
		GPS() {
			self = this;
		}
		
		void enable() {
			RCC.enable(RCC.USART3);
			USART3.set_baudrate(4800);
			USART3.enable();
			
			Interrupt::enable(Interrupt::USART3);
		}
		
		P<GPSMsg> read() {
			while(!complete) {
				Thread::yield();
			}
			
			complete = false;
			
			P<GPSMsg> msg = const_cast<P<GPSMsg>&>(complete_msg);
			
			const_cast<P<GPSMsg>&>(complete_msg).reset();
			
			return msg;
		}
};

#endif