summaryrefslogtreecommitdiff
path: root/src/modem.h
blob: 4040c92718fda815b3d6968e346aa65f66bbaf21 (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
62
63
64
65
66
67
68
69
70
71
72
73
/* Name: modem.h
 * Author: Jari Tulilahti
 * Copyright: 2014 Rakettitiede Oy
 * License: LGPLv3, see COPYING, and COPYING.LESSER -files for more info
 */

#ifndef MODEM_H_
#define MODEM_H_

#include <avr/interrupt.h>
#include <stdlib.h>

/* Modem ring buffer size must be power of 2 */
#define MODEM_BUFFER_SIZE	8

/* Modem defines */
#define MODEM_SYNC_LEN		42
#define MODEM_TIMER		TCNT1L
#define MODEM_PCINT		PCINT24
#define MODEM_PCMSK		PCMSK3
#define MODEM_PCIE		PCIE3
#define MODEM_PIN		PA0
#define MODEM_DDR		DDRA

/**
 * Receive-only modem. Sets up a pin change interrupt on the modem pin
 * and receives bytes using a simple protocol. Does not detect or correct
 * transmission errors. Exposes a global modem object for convenience.
 */
class Modem {
	private:
		uint8_t buffer_head;
		uint8_t buffer_tail;
		uint8_t buffer[MODEM_BUFFER_SIZE];
		void buffer_put(const uint8_t c);
	public:
		Modem() {};

		/**
		 * Checks if there are unprocessed bytes in the modem receive buffer.
		 * @return number of unprocessed bytes
		 */
		uint8_t buffer_available(void);

		/**
		 * Get next byte from modem receive buffer.
		 * @return next unprocessed byte (0 if the buffer is empty)
		 */
		uint8_t buffer_get(void);

		/**
		 * Enable the modem. Turns on the input voltage divider on MODEM_PIN
		 * and enables the receive interrupt (MODEM_PCINT).
		 */
		void enable(void);

		/**
		 * Disable the modem. Disables the receive interrupt and turns off
		 * the input voltage divider on MODEM_PIN.
		 */
		void disable(void);

		/**
		 * Called by the pin change interrupt service routine whenever the
		 * modem pin is toggled. Detects sync pulses, receives bits and
		 * stores complete bytes in the buffer.
		 *
		 * Do not call this function yourself.
		 */
		void receive(void);
};

#endif /* MODEM_H_ */