summaryrefslogtreecommitdiff
path: root/src/app/loratest/main.cc
blob: 5b03425038e3483c4eac45c572e4abf6f006cf45 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "arch.h"
#include "driver/gpio.h"
#include "driver/stdout.h"

class SerialOutput : public OutputStream {
	private:
		SerialOutput(const SerialOutput &copy);

	public:
		SerialOutput () {}
		void setup();

		virtual void put(char c) override;
};

/*
 * Baud rate calculation according to datasheet:
 * N := f_{BRCLK} / Baudrate = F_CPU / 115200 in our case
 * if N <= 16: OS16 = 0, UCBR0 = int(N)
 * if N > 16: OS16 = 1, UCBR0 = int(N/16), UCBRF0 = int(((n/16) - int(n/16)) * 16) = int(N)%16
 * Set UCBRS0 according to table 21-4
 */

void SerialOutput::setup()
{
	UCA1CTLW0 |= UCSWRST;
#if F_CPU == 16000000UL
	// 16M / 9600 == 1666.6667 -> UCOS16 = 1, UCBR0 = 104, UCBRF0 = 2, UCBRS0 = 0xd6 ("0.6667")
	UCA1CTLW0 = UCSWRST | UCSSEL__SMCLK;
	UCA1MCTLW = UCOS16 | (2<<4) | 0xd600;
	UCA1BR0 = 104;
#elif F_CPU == 8000000UL
	// 8M / 9600 == 833.3333 -> UCOS16 = 1, UCBR0 = 52, UCBRF0 = 1, UCBRS0 = 0x49 ("0.3335")
	UCA1CTLW0 = UCSWRST | UCSSEL__SMCLK;
	UCA1MCTLW = UCOS16 | (1<<4) | 0x4900;
	UCA1BR0 = 52;
#else
#error Unsupported F_CPU
#endif

	UCA1IRCTL = 0;
	UCA1ABCTL = 0;

	P2REN &= ~(BIT5 | BIT6);
	P2SEL0 &= ~(BIT5 | BIT6);
	P2SEL1 |= BIT5 | BIT6;

	UCA1CTLW0 &= ~UCSWRST;

	//UCA1IE |= UCRXIE;
}

void SerialOutput::put(char c)
{
	while (!(UCA1IFG & UCTXIFG));
	UCA1TXBUF = c;

	if (c == '\n') {
		put('\r');
	}
}

SerialOutput sout;

class SerialInput {
	private:
		SerialInput(const SerialInput &copy);
		char buffer[64];
		unsigned char write_pos, read_pos;

	public:
		SerialInput() : write_pos(0), read_pos(0) {}
		void setup();
		bool hasKey();
		char getKey();

		inline void addKey(char key) {
			buffer[write_pos++] = key;
			write_pos %= 64;
		}
};

void SerialInput::setup()
{
	UCA1IE |= UCRXIE;
}

bool SerialInput::hasKey()
{
	if (write_pos != read_pos) {
		return true;
	}
	return false;
}

char SerialInput::getKey()
{
	char ret = buffer[read_pos++];
	read_pos %= 64;
	return ret;
}

SerialInput sin;

__attribute__((interrupt(USCI_A1_VECTOR))) __attribute__((wakeup)) void handle_stdin()
{
	if (UCA1IFG & UCRXIFG) {
		sin.addKey(UCA1RXBUF);
	}
}

void loop(void)
{
	static unsigned int i = 0;
	gpio.led_off(0);
	// does not work on MSP430 3V3
	// works when connected to MSP430 5V0, AUX should be disconnected in that case
	// RX seems to work best without intermittent TX
	sout << "Hello! My value is " << i << endl;
	if (sin.hasKey()) {
		gpio.led_on(0);
		kout << "RX: ";
		while (sin.hasKey()) {
			kout << sin.getKey();
		}
		kout << endl;
	}
	i++;
}

int main(void)
{
	arch.setup();
	gpio.setup();
	kout.setup();
	sout.setup();
	sin.setup();

	gpio.led_on(1);

	// AUX
	gpio.input(GPIO::p4_2);

	// set module to sleep mode

	// M0
	gpio.output(GPIO::p2_4, 1);

	// M1
	gpio.output(GPIO::p4_3, 1);

	arch.delay_ms(100);
	sout << "\xc2\x00\x00\x1a\x17\x47";
	arch.delay_ms(50);
	gpio.write(GPIO::p2_4, 0);
	gpio.write(GPIO::p4_3, 0);
	arch.delay_ms(10);

	gpio.led_off(1);
	gpio.led_on(0);

	arch.idle_loop();

	return 0;
}