summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 8e267bf46e1d2c2480370e81eaf87ea04444dd99 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <rcc/flash.h>
#include <rcc/rcc.h>
#include <rcc/crs.h>
#include <syscfg/syscfg.h>
#include <gpio/gpio.h>
#include <os/time.h>
#include <spi/spi.h>
#include <usb/usb.h>
#include <usb/descriptor.h>

enum tag_type_t {
	ISO15693,
	ISO18092,
};

auto dev_desc = device_desc(0x200, 0, 0, 0, 64, 0x1d50, 0x60f8, 0, 0, 0, 0, 1);
auto conf_desc = configuration_desc(2, 1, 0, 0xc0, 0,
	interface_desc(1, 0, 1, 0xff, 0x00, 0x00, 0,
		endpoint_desc(0x81, 0x02, 64, 0)
	),
	interface_desc(2, 0, 1, 0xff, 0x00, 0x00, 0,
		endpoint_desc(0x82, 0x03, 64, 0)
	)
);

desc_t dev_desc_p = {sizeof(dev_desc), (void*)&dev_desc};
desc_t conf_desc_p = {sizeof(conf_desc), (void*)&conf_desc};

USB_l0 usb(USB, dev_desc_p, conf_desc_p);

Pin nfc_sck     = GPIOB[3];
Pin nfc_miso    = GPIOB[4];
Pin nfc_mosi    = GPIOB[5];
Pin nfc_ss      = GPIOB[6];
Pin nfc_irq_out = GPIOB[7];
Pin nfc_irq_in  = GPIOC[13];

Pin button      = GPIOA[3];
Pin led         = GPIOA[4];

PinArray kp_rows = GPIOB.array(12, 15);
PinArray kp_cols = GPIOA.array(8, 10); 

class CR95HF {
	private:
		SPI_t& spi;
		Pin& ss;
	
	public:
		CR95HF(SPI_t& spi_bus, Pin& p) : spi(spi_bus), ss(p) {}
		
		void send_cmd(uint8_t cmd, uint32_t len, uint8_t* buf) {
			ss.off();
			
			spi.transfer_byte(0);
			spi.transfer_byte(cmd);
			spi.transfer_byte(len);
			
			while(len--) {
				spi.transfer_byte(*buf++);
			}
			
			ss.on();
		}
		
		uint32_t get_response(uint32_t maxlen, uint8_t* buf) {
			uint32_t len = 0;
			
			while(nfc_irq_out.get());
			
			ss.off();
			
			spi.transfer_byte(2);
			*buf++ = spi.transfer_byte();
			len++;
			
			uint32_t datalen = spi.transfer_byte();
			*buf++ = datalen;
			len++;
			
			while(len < datalen + 2) {
				*buf++ = spi.transfer_byte();
				len++;
			}
			
			ss.on();
			
			return len;
		}
};

CR95HF cr95hf(SPI1, nfc_ss);

class USB_NFC : public USB_class_driver {
	private:
		USB_generic& usb;
		
	public:
		USB_NFC(USB_generic& usbd) : usb(usbd) {
			usb.register_driver(this);
		}
		
	protected:
		virtual void handle_set_configuration(uint8_t configuration) {
			if(configuration) {
				usb.hw_conf_ep(0x81, EPType::Bulk, 64);
			}
		}
};

USB_NFC usb_nfc(usb);

class USB_keypad : public USB_class_driver {
	private:
		USB_generic& usb;
		
	public:
		USB_keypad(USB_generic& usbd) : usb(usbd) {
			usb.register_driver(this);
		}
		
	protected:
		virtual void handle_set_configuration(uint8_t configuration) {
			if(configuration) {
				usb.hw_conf_ep(0x82, EPType::Interrupt, 64);
			}
		}
};

USB_keypad usb_keypad(usb);

int main() {
	rcc_init();
	
	// Initialize system timer.
	STK.LOAD = 32000000 / 8 / 1000; // 1000 Hz.
	STK.VAL = 0;
	STK.CTRL = 0x03;
	
	// Enable CRS.
	RCC.enable(RCC.CRS);
	CRS.enable();
	
	RCC.enable(RCC.GPIOA);
	RCC.enable(RCC.GPIOB);
	RCC.enable(RCC.GPIOC);
	
	led.set_mode(Pin::Output);
	
	RCC.enable(RCC.USB);
	
	usb.init();
	
	Time::sleep(10);
	USB.reg.BCDR |= 1 << 15;
	
	RCC.enable(RCC.SPI1);
	
	nfc_irq_out.set_mode(Pin::Input);
	nfc_irq_in.on();
	nfc_irq_in.set_mode(Pin::Output);
	
	nfc_ss.on();
	nfc_ss.set_mode(Pin::Output);
	nfc_sck.set_speed(Pin::High);
	nfc_sck.set_mode(Pin::AF);
	nfc_miso.set_mode(Pin::AF);
	nfc_mosi.set_mode(Pin::AF);
	
	SPI1.reg.CR1 = (1 << 9) | (1 << 8) | (1 << 6) | (4 << 3) | (1 << 2) | (0 << 1) | (0 << 0); // SSM, SSI, SPE, 36/32MHz, MSTR, CPOL=0, CPHA=0
	SPI1.reg.CR2 = (1 << 12) | (7 << 8); // FRXTH, DS = 8bit
	
	Time::sleep(1);
	nfc_irq_in.off();
	Time::sleep(1);
	nfc_irq_in.on();
	Time::sleep(1);
	
	uint8_t buf[64];
	bool cmd_sent = false;
	uint32_t nfc_delay_until = 0;
	uint8_t bulk_buf[8];
	tag_type_t active_type = ISO15693;
	
	Time::sleep(1000);
	
	
	kp_cols.set_mode(Pin::Input);
	kp_cols.set_pull(Pin::PullUp);
	
	kp_rows.set_type(Pin::OpenDrain);
	kp_rows.set_mode(Pin::Output);
	
	uint32_t kp_last = 0;
	
	while(1) {
		if(Time::time() < nfc_delay_until) {
			// Just do nothing.
		} else if(!cmd_sent) {
			led.off();
			
			if(active_type == ISO15693) {
				cr95hf.send_cmd(0x02, 2, (uint8_t*)"\x01\x05"); // Select ISO 15693
				cr95hf.get_response(64, buf);
				
				cr95hf.send_cmd(0x04, 3, (uint8_t*)"\x26\x01\x00"); // INVENTORY
				
			} else if(active_type == ISO18092) {
				cr95hf.send_cmd(0x02, 2, (uint8_t*)"\x04\x51"); // Select ISO 18092
				cr95hf.get_response(64, buf);
				
				cr95hf.send_cmd(0x04, 5, (uint8_t*)"\x00\xff\xff\x00\x00"); // REQC
			}
			
			cmd_sent = true;

		} else if(!nfc_irq_out.get()) {
			cr95hf.get_response(64, buf);
			cmd_sent = false;
			
			if(buf[0] == 128 && buf[14] == 0) {
				led.on();
				
				uint8_t* id_buf = bulk_buf;
				
				if(active_type == ISO15693) {
					uint8_t* id_raw = &buf[11];
					for(uint32_t i = 0; i < 8; i++) {
						*id_buf++ = *id_raw--;
					}
				
				} else if(active_type == ISO18092) {
					uint8_t* id_raw = &buf[3];
					for(uint32_t i = 0; i < 8; i++) {
						*id_buf++ = *id_raw++;
					}
				}
				
				if(usb.ep_ready(1)) {
					usb.write(1, (uint32_t*)bulk_buf, sizeof(bulk_buf));
				}
				
				nfc_delay_until = Time::time() + 5000; // Valid for five seconds.
			} else {
				nfc_delay_until = Time::time() + 100; // Retry in 100ms.
				
				if(active_type == ISO15693) {
					active_type = ISO18092;
				} else {
					active_type = ISO15693;
				}
			}
		}
		
		uint32_t kp_state = 0;
		
		for(int i = 0; i < 4; i++) {
			kp_rows.set(~(1 << i));
			asm volatile("dmb");
			asm volatile("nop");
			asm volatile("nop");
			asm volatile("nop");
			asm volatile("nop");
			kp_state |= ((~kp_cols.get()) & 0x7) << (i * 3);
		}
		
		if(kp_state != kp_last && usb.ep_ready(2)) {
			usb.write(2, &kp_state, 2);
			kp_last = kp_state;
		}
		
		
		usb.process();
	}
}