File: hackrf_usb.c

package info (click to toggle)
hackrf 2024.02.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 36,692 kB
  • sloc: ansic: 56,310; xml: 3,424; perl: 2,730; python: 1,427; makefile: 598; asm: 514; vhdl: 319; sh: 179; awk: 20
file content (333 lines) | stat: -rw-r--r-- 9,060 bytes parent folder | download | duplicates (2)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * Copyright 2012-2022 Great Scott Gadgets <info@greatscottgadgets.com>
 * Copyright 2012 Jared Boone
 * Copyright 2013 Benjamin Vernoux
 *
 * This file is part of HackRF.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include <stddef.h>
#include <string.h>

#include <libopencm3/lpc43xx/ipc.h>
#include <libopencm3/lpc43xx/m4/nvic.h>
#include <libopencm3/lpc43xx/rgu.h>
#include <libopencm3/lpc43xx/timer.h>

#include <streaming.h>

#include "tuning.h"

#include "usb.h"
#include "usb_standard_request.h"

#include <rom_iap.h>
#include "usb_descriptor.h"

#include "usb_device.h"
#include "usb_endpoint.h"
#include "usb_api_board_info.h"
#include "usb_api_cpld.h"
#include "usb_api_register.h"
#include "usb_api_spiflash.h"
#include "usb_api_operacake.h"
#include "operacake.h"
#include "usb_api_sweep.h"
#include "usb_api_transceiver.h"
#include "usb_api_ui.h"
#include "usb_bulk_buffer.h"
#include "usb_api_m0_state.h"
#include "cpld_xc2c.h"
#include "portapack.h"
#include "hackrf_ui.h"
#include "platform_detect.h"
#include "clkin.h"

extern uint32_t __m0_start__;
extern uint32_t __m0_end__;
extern uint32_t __ram_m0_start__;
extern uint32_t _etext_ram, _text_ram, _etext_rom;

static usb_request_handler_fn vendor_request_handler[] = {
	NULL,
	usb_vendor_request_set_transceiver_mode,
	usb_vendor_request_write_max283x,
	usb_vendor_request_read_max283x,
	usb_vendor_request_write_si5351c,
	usb_vendor_request_read_si5351c,
	usb_vendor_request_set_sample_rate_frac,
	usb_vendor_request_set_baseband_filter_bandwidth,
#ifdef RAD1O
	NULL, // write_rffc5071 not used
	NULL, // read_rffc5071 not used
#else
	usb_vendor_request_write_rffc5071,
	usb_vendor_request_read_rffc5071,
#endif
	usb_vendor_request_erase_spiflash,
	usb_vendor_request_write_spiflash,
	usb_vendor_request_read_spiflash,
	NULL, // used to be write_cpld
	usb_vendor_request_read_board_id,
	usb_vendor_request_read_version_string,
	usb_vendor_request_set_freq,
	usb_vendor_request_set_amp_enable,
	usb_vendor_request_read_partid_serialno,
	usb_vendor_request_set_lna_gain,
	usb_vendor_request_set_vga_gain,
	usb_vendor_request_set_txvga_gain,
	NULL, // was set_if_freq
#ifdef HACKRF_ONE
	usb_vendor_request_set_antenna_enable,
#else
	NULL,
#endif
	usb_vendor_request_set_freq_explicit,
	usb_vendor_request_read_wcid, // USB_WCID_VENDOR_REQ
	usb_vendor_request_init_sweep,
	usb_vendor_request_operacake_get_boards,
	usb_vendor_request_operacake_set_ports,
	usb_vendor_request_set_hw_sync_mode,
	usb_vendor_request_reset,
	usb_vendor_request_operacake_set_ranges,
	usb_vendor_request_set_clkout_enable,
	usb_vendor_request_spiflash_status,
	usb_vendor_request_spiflash_clear_status,
	usb_vendor_request_operacake_gpio_test,
#ifdef HACKRF_ONE
	usb_vendor_request_cpld_checksum,
#else
	NULL,
#endif
	usb_vendor_request_set_ui_enable,
	usb_vendor_request_operacake_set_mode,
	usb_vendor_request_operacake_get_mode,
	usb_vendor_request_operacake_set_dwell_times,
	usb_vendor_request_get_m0_state,
	usb_vendor_request_set_tx_underrun_limit,
	usb_vendor_request_set_rx_overrun_limit,
	usb_vendor_request_get_clkin_status,
	usb_vendor_request_read_board_rev,
	usb_vendor_request_read_supported_platform,
	usb_vendor_request_set_leds,
	usb_vendor_request_user_config_set_bias_t_opts,
};

static const uint32_t vendor_request_handler_count =
	sizeof(vendor_request_handler) / sizeof(vendor_request_handler[0]);

usb_request_status_t usb_vendor_request(
	usb_endpoint_t* const endpoint,
	const usb_transfer_stage_t stage)
{
	usb_request_status_t status = USB_REQUEST_STATUS_STALL;

	if (endpoint->setup.request < vendor_request_handler_count) {
		usb_request_handler_fn handler =
			vendor_request_handler[endpoint->setup.request];
		if (handler) {
			status = handler(endpoint, stage);
		}
	}

	return status;
}

const usb_request_handlers_t usb_request_handlers = {
	.standard = usb_standard_request,
	.class = 0,
	.vendor = usb_vendor_request,
	.reserved = 0,
};

void usb_configuration_changed(usb_device_t* const device)
{
	/* Reset transceiver to idle state until other commands are received */
	request_transceiver_mode(TRANSCEIVER_MODE_OFF);
	if (device->configuration->number == 1) {
		// transceiver configuration
		led_on(LED1);
	} else {
		/* Configuration number equal 0 means usb bus reset. */
		led_off(LED1);
	}
	usb_endpoint_init(&usb_endpoint_bulk_in);
	usb_endpoint_init(&usb_endpoint_bulk_out);
}

void usb_set_descriptor_by_serial_number(void)
{
	iap_cmd_res_t iap_cmd_res;

	/* Read IAP Serial Number Identification */
	iap_cmd_res.cmd_param.command_code = IAP_CMD_READ_SERIAL_NO;
	iap_cmd_call(&iap_cmd_res);

	if (iap_cmd_res.status_res.status_ret == CMD_SUCCESS) {
		usb_descriptor_string_serial_number[0] =
			USB_DESCRIPTOR_STRING_SERIAL_BUF_LEN;
		usb_descriptor_string_serial_number[1] = USB_DESCRIPTOR_TYPE_STRING;

		/* 32 characters of serial number, convert to UTF-16LE */
		for (size_t i = 0; i < USB_DESCRIPTOR_STRING_SERIAL_LEN; i++) {
			const uint_fast8_t nibble =
				(iap_cmd_res.status_res.iap_result[i >> 3] >>
				 (28 - (i & 7) * 4)) &
				0xf;
			const char c =
				(nibble > 9) ? ('a' + nibble - 10) : ('0' + nibble);
			usb_descriptor_string_serial_number[2 + i * 2] = c;
			usb_descriptor_string_serial_number[3 + i * 2] = 0x00;
		}
	} else {
		usb_descriptor_string_serial_number[0] = 2;
		usb_descriptor_string_serial_number[1] = USB_DESCRIPTOR_TYPE_STRING;
	}
}

static bool cpld_jtag_sram_load(jtag_t* const jtag)
{
	cpld_jtag_take(jtag);
	cpld_xc2c64a_jtag_sram_write(jtag, &cpld_hackrf_program_sram);
	const bool success = cpld_xc2c64a_jtag_sram_verify(
		jtag,
		&cpld_hackrf_program_sram,
		&cpld_hackrf_verify);
	cpld_jtag_release(jtag);
	return success;
}

static void m0_rom_to_ram()
{
	uint32_t* dest = &__ram_m0_start__;

	// Calculate the base address of ROM
	uint32_t base = (uint32_t) (&_etext_rom - (&_etext_ram - &_text_ram));

	// M0 image location, relative to the start of ROM
	uint32_t src = (uint32_t) &__m0_start__;

	uint32_t len = (uint32_t) &__m0_end__ - (uint32_t) src;
	memcpy(dest, (uint32_t*) (base + src), len);
}

int main(void)
{
	// Copy M0 image from ROM before SPIFI is disabled
	m0_rom_to_ram();

	detect_hardware_platform();
	pin_setup();
	enable_1v8_power();
#ifdef HACKRF_ONE
	// Set up mixer before enabling RF power, because its
	// GPO is used to control the antenna bias tee.
	mixer_setup(&mixer);
#endif
#if (defined HACKRF_ONE || defined RAD1O)
	enable_rf_power();
#endif
	cpu_clock_init();

	/* Wake the M0 */
	ipc_halt_m0();
	ipc_start_m0((uint32_t) &__ram_m0_start__);

	if (!cpld_jtag_sram_load(&jtag_cpld)) {
		halt_and_flash(6000000);
	}

#ifdef HACKRF_ONE
	portapack_init();
#endif

#ifndef DFU_MODE
	usb_set_descriptor_by_serial_number();
#endif

	usb_set_configuration_changed_cb(usb_configuration_changed);
	usb_peripheral_reset();

	usb_device_init(0, &usb_device);

	usb_queue_init(&usb_endpoint_control_out_queue);
	usb_queue_init(&usb_endpoint_control_in_queue);
	usb_queue_init(&usb_endpoint_bulk_out_queue);
	usb_queue_init(&usb_endpoint_bulk_in_queue);

	usb_endpoint_init(&usb_endpoint_control_out);
	usb_endpoint_init(&usb_endpoint_control_in);

	nvic_set_priority(NVIC_USB0_IRQ, 255);

	hackrf_ui()->init();

	usb_run(&usb_device);

	rf_path_init(&rf_path);

	bool operacake_allow_gpio;
	if (hackrf_ui()->operacake_gpio_compatible()) {
		operacake_allow_gpio = true;
	} else {
		operacake_allow_gpio = false;
	}
	operacake_init(operacake_allow_gpio);

	// FIXME: clock detection on r9 only works when calling init twice
	if (detected_platform() == BOARD_ID_HACKRF1_R9) {
		clkin_detect_init();
		clkin_detect_init();
	}

	while (true) {
		transceiver_request_t request;

		// Briefly disable USB interrupt so that we can
		// atomically retrieve both the transceiver mode
		// and the mode change sequence number. They are
		// changed together by request_transceiver_mode()
		// called from the USB ISR.

		nvic_disable_irq(NVIC_USB0_IRQ);
		request = transceiver_request;
		nvic_enable_irq(NVIC_USB0_IRQ);

		switch (request.mode) {
		case TRANSCEIVER_MODE_OFF:
			off_mode(request.seq);
			break;
		case TRANSCEIVER_MODE_RX:
			rx_mode(request.seq);
			break;
		case TRANSCEIVER_MODE_TX:
			tx_mode(request.seq);
			break;
		case TRANSCEIVER_MODE_RX_SWEEP:
			sweep_mode(request.seq);
			break;
		case TRANSCEIVER_MODE_CPLD_UPDATE:
			cpld_update();
			break;
		default:
			break;
		}
	}

	return 0;
}