Merge pull request #325 from dominicgs/device_selection_options

Code clean up
This commit is contained in:
Michael Ossmann
2017-02-01 16:32:22 -07:00
committed by GitHub
3 changed files with 12 additions and 217 deletions

View File

@ -1,142 +0,0 @@
/*
* Copyright 2013 Jared Boone <jared@sharebrained.com>
*
* 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 <sgpio_dma.h>
#include <libopencm3/lpc43xx/creg.h>
#include <libopencm3/lpc43xx/gpdma.h>
#include <libopencm3/lpc43xx/scu.h>
#include <libopencm3/lpc43xx/sgpio.h>
#include <sgpio.h>
#include <gpdma.h>
void sgpio_dma_configure_lli(
gpdma_lli_t* const lli,
const size_t lli_count,
const bool direction_transmit,
void* const buffer,
const size_t transfer_bytes
) {
const size_t bytes_per_word = 4;
const size_t transfer_words = (transfer_bytes + bytes_per_word - 1) / bytes_per_word;
gpdma_lli_create_loop(lli, lli_count);
for(size_t i=0; i<lli_count; i++) {
void* const peripheral_address = (void*)&SGPIO_REG_SS(0);
void* const memory_address = buffer + (transfer_words * bytes_per_word * i);
const uint_fast8_t source_master = direction_transmit ? 1 : 0;
const uint_fast8_t destination_master = direction_transmit ? 0 : 1;
const uint_fast8_t lli_fetch_master = direction_transmit ? 0 : 1;
lli[i].csrcaddr = direction_transmit ? memory_address : peripheral_address;
lli[i].cdestaddr = direction_transmit ? peripheral_address : memory_address;
lli[i].clli = (lli[i].clli & ~GPDMA_CLLI_LM_MASK) | GPDMA_CLLI_LM(lli_fetch_master);
lli[i].ccontrol =
GPDMA_CCONTROL_TRANSFERSIZE(transfer_words) |
GPDMA_CCONTROL_SBSIZE(0) |
GPDMA_CCONTROL_DBSIZE(0) |
GPDMA_CCONTROL_SWIDTH(2) |
GPDMA_CCONTROL_DWIDTH(2) |
GPDMA_CCONTROL_S(source_master) |
GPDMA_CCONTROL_D(destination_master) |
GPDMA_CCONTROL_SI(direction_transmit ? 1 : 0) |
GPDMA_CCONTROL_DI(direction_transmit ? 0 : 1) |
GPDMA_CCONTROL_PROT1(0) |
GPDMA_CCONTROL_PROT2(0) |
GPDMA_CCONTROL_PROT3(0) |
GPDMA_CCONTROL_I(0)
;
}
}
static void sgpio_dma_enable(const uint_fast8_t channel, const gpdma_lli_t* const lli, const bool direction_transmit) {
gpdma_channel_disable(channel);
gpdma_channel_interrupt_tc_clear(channel);
gpdma_channel_interrupt_error_clear(channel);
GPDMA_CSRCADDR(channel) = (uint32_t)lli->csrcaddr;
GPDMA_CDESTADDR(channel) = (uint32_t)lli->cdestaddr;
GPDMA_CLLI(channel) = (uint32_t)lli->clli;
GPDMA_CCONTROL(channel) = lli->ccontrol;
/* 1: Memory -> Peripheral
* 2: Peripheral -> Memory */
const uint_fast8_t flowcntrl = direction_transmit ? 1 : 2;
GPDMA_CCONFIG(channel) =
GPDMA_CCONFIG_E(0) |
GPDMA_CCONFIG_SRCPERIPHERAL(0) |
GPDMA_CCONFIG_DESTPERIPHERAL(0) |
GPDMA_CCONFIG_FLOWCNTRL(flowcntrl) |
GPDMA_CCONFIG_IE(1) |
GPDMA_CCONFIG_ITC(1) |
GPDMA_CCONFIG_L(0) |
GPDMA_CCONFIG_H(0)
;
gpdma_channel_enable(channel);
}
void sgpio_dma_init() {
/* DMA peripheral/source 0, option 2 (SGPIO14) -- BREQ */
CREG_DMAMUX &= ~(CREG_DMAMUX_DMAMUXPER0_MASK);
CREG_DMAMUX |= CREG_DMAMUX_DMAMUXPER0(0x2);
// Disable sync, maybe it is causing max speed (10MT/sec) glitches?
//GPDMA_DMACSYNC = (1 << 0);
//GPDMA_SYNC = GPDMA_SYNC_DMACSYNC(0xFFFF); // TODO: Don't do this, I'm just going nuts here.
gpdma_controller_enable();
}
static const uint_fast8_t dma_channel_sgpio = 0;
void sgpio_dma_rx_start(const gpdma_lli_t* const start_lli) {
sgpio_dma_enable(dma_channel_sgpio, start_lli, false);
}
void sgpio_dma_tx_start(const gpdma_lli_t* const start_lli) {
sgpio_dma_enable(dma_channel_sgpio, start_lli, true);
}
void sgpio_dma_irq_tc_acknowledge() {
gpdma_channel_interrupt_tc_clear(dma_channel_sgpio);
}
void sgpio_dma_stop() {
gpdma_channel_disable(dma_channel_sgpio);
}
size_t sgpio_dma_current_transfer_index(
const gpdma_lli_t* const lli,
const size_t lli_count
) {
const uint32_t next_lli = GPDMA_CLLI(dma_channel_sgpio);
for(size_t i=0; i<lli_count; i++) {
if( lli[i].clli == next_lli ) {
return i;
}
}
return 0;
}

View File

@ -1,48 +0,0 @@
/*
* Copyright 2013 Jared Boone <jared@sharebrained.com>
*
* 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.
*/
#ifndef __SGPIO_DMA_H__
#define __SGPIO_DMA_H__
#include <stddef.h>
#include <libopencm3/lpc43xx/gpdma.h>
void sgpio_dma_configure_lli(
gpdma_lli_t* const lli,
const size_t lli_count,
const bool direction_transmit,
void* const buffer,
const size_t transfer_bytes
);
void sgpio_dma_init();
void sgpio_dma_rx_start(const gpdma_lli_t* const start_lli);
void sgpio_dma_tx_start(const gpdma_lli_t* const start_lli);
void sgpio_dma_irq_tc_acknowledge();
void sgpio_dma_stop();
size_t sgpio_dma_current_transfer_index(
const gpdma_lli_t* const lli,
const size_t lli_count
);
#endif/*__SGPIO_DMA_H__*/

View File

@ -24,7 +24,7 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSI
#include "hackrf.h"
#include <stdlib.h>
#include <string.h>
#include <libusb.h>
#ifdef _WIN32
@ -98,17 +98,19 @@ typedef enum {
HACKRF_HW_SYNC_MODE_ON = 1,
} hackrf_hw_sync_mode;
#define TRANSFER_COUNT 4
#define TRANSFER_BUFFER_SIZE 262144
struct hackrf_device {
libusb_device_handle* usb_device;
struct libusb_transfer** transfers;
hackrf_sample_block_cb_fn callback;
volatile bool transfer_thread_started; /* volatile shared between threads (read only) */
pthread_t transfer_thread;
uint32_t transfer_count;
uint32_t buffer_size;
volatile bool streaming; /* volatile shared between threads (read only) */
void* rx_ctx;
void* tx_ctx;
unsigned char buffer[TRANSFER_COUNT * TRANSFER_BUFFER_SIZE];
};
typedef struct {
@ -155,7 +157,7 @@ static int cancel_transfers(hackrf_device* device)
if( device->transfers != NULL )
{
for(transfer_index=0; transfer_index<device->transfer_count; transfer_index++)
for(transfer_index=0; transfer_index<TRANSFER_COUNT; transfer_index++)
{
if( device->transfers[transfer_index] != NULL )
{
@ -175,7 +177,7 @@ static int free_transfers(hackrf_device* device)
if( device->transfers != NULL )
{
// libusb_close() should free all transfers referenced from this array.
for(transfer_index=0; transfer_index<device->transfer_count; transfer_index++)
for(transfer_index=0; transfer_index<TRANSFER_COUNT; transfer_index++)
{
if( device->transfers[transfer_index] != NULL )
{
@ -194,13 +196,13 @@ static int allocate_transfers(hackrf_device* const device)
if( device->transfers == NULL )
{
uint32_t transfer_index;
device->transfers = (struct libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer));
device->transfers = (struct libusb_transfer**) calloc(TRANSFER_COUNT, sizeof(struct libusb_transfer));
if( device->transfers == NULL )
{
return HACKRF_ERROR_NO_MEM;
}
for(transfer_index=0; transfer_index<device->transfer_count; transfer_index++)
for(transfer_index=0; transfer_index<TRANSFER_COUNT; transfer_index++)
{
device->transfers[transfer_index] = libusb_alloc_transfer(0);
if( device->transfers[transfer_index] == NULL )
@ -212,8 +214,8 @@ static int allocate_transfers(hackrf_device* const device)
device->transfers[transfer_index],
device->usb_device,
0,
(unsigned char*)malloc(device->buffer_size),
device->buffer_size,
&device->buffer[transfer_index * TRANSFER_BUFFER_SIZE],
TRANSFER_BUFFER_SIZE,
NULL,
device,
0
@ -239,7 +241,7 @@ static int prepare_transfers(
uint32_t transfer_index;
if( device->transfers != NULL )
{
for(transfer_index=0; transfer_index<device->transfer_count; transfer_index++)
for(transfer_index=0; transfer_index<TRANSFER_COUNT; transfer_index++)
{
device->transfers[transfer_index]->endpoint = endpoint_address;
device->transfers[transfer_index]->callback = callback;
@ -355,9 +357,6 @@ int ADDCALL hackrf_exit(void)
return HACKRF_SUCCESS;
}
#include <stdio.h>
#include <string.h>
hackrf_device_list_t* ADDCALL hackrf_device_list()
{
ssize_t i;
@ -442,8 +441,6 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number)
char serial_number[64];
int serial_number_length;
printf("Number of USB devices: %ld\n", list_length);
if( desired_serial_number ) {
/* If a shorter serial number is specified, only match against the suffix.
* Should probably complain if the match is not unique, currently doesn't.
@ -461,7 +458,6 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number)
if((device_descriptor.idProduct == hackrf_one_usb_pid) ||
(device_descriptor.idProduct == hackrf_jawbreaker_usb_pid) ||
(device_descriptor.idProduct == rad1o_usb_pid)) {
printf("USB device %4x:%4x:", device_descriptor.idVendor, device_descriptor.idProduct);
if( desired_serial_number != NULL ) {
const uint_fast8_t serial_descriptor_index = device_descriptor.iSerialNumber;
@ -473,23 +469,18 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number)
serial_number_length = libusb_get_string_descriptor_ascii(usb_device, serial_descriptor_index, (unsigned char*)serial_number, sizeof(serial_number));
if( serial_number_length == 32 ) {
serial_number[32] = 0;
printf(" %s", serial_number);
if( strncmp(serial_number + 32-match_len, desired_serial_number, match_len) == 0 ) {
printf(" match\n");
break;
} else {
printf(" skip\n");
libusb_close(usb_device);
usb_device = NULL;
}
} else {
printf(" wrong length of serial number: %d\n", serial_number_length);
libusb_close(usb_device);
usb_device = NULL;
}
}
} else {
printf(" default\n");
libusb_open(devices[i], &usb_device);
break;
}
@ -537,12 +528,6 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
lib_device->transfers = NULL;
lib_device->callback = NULL;
lib_device->transfer_thread_started = false;
/*
lib_device->transfer_count = 1024;
lib_device->buffer_size = 16384;
*/
lib_device->transfer_count = 4;
lib_device->buffer_size = 262144; /* 1048576; */
lib_device->streaming = false;
do_exit = false;