From f034bc82ca5c17647f0a1944943c293ae7aacb61 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sun, 9 Nov 2014 10:50:32 -0800 Subject: [PATCH] RFFC5071: Further work abstracting SPI details out of driver. --- firmware/common/hackrf_core.c | 11 ++++++++- firmware/common/rffc5071.c | 7 +++--- firmware/common/rffc5071.h | 11 ++++++++- firmware/common/rffc5071_spi.c | 10 +++++++- firmware/common/rffc5071_spi.h | 12 ++------- firmware/common/spi.c | 34 +++++++++++++++++++++++++ firmware/common/spi.h | 45 ++++++++++++++++++++++++++++++++++ firmware/hackrf-common.cmake | 1 + 8 files changed, 114 insertions(+), 17 deletions(-) create mode 100644 firmware/common/spi.c create mode 100644 firmware/common/spi.h diff --git a/firmware/common/hackrf_core.c b/firmware/common/hackrf_core.c index 93bf8512..651579bf 100644 --- a/firmware/common/hackrf_core.c +++ b/firmware/common/hackrf_core.c @@ -25,6 +25,7 @@ #include "si5351c.h" #include "max2837.h" #include "rffc5071.h" +#include "rffc5071_spi.h" #include "sgpio.h" #include "rf_path.h" #include @@ -37,9 +38,17 @@ max2837_driver_t max2837; -rffc5071_driver_t rffc5072; +spi_t rffc5071_spi = { + .init = rffc5071_spi_init, + .transfer = rffc5071_spi_transfer, + .transfer_gather = rffc5071_spi_transfer_gather, +}; w25q80bv_driver_t spi_flash; +rffc5071_driver_t rffc5072 = { + .spi = &rffc5071_spi, +}; + void delay(uint32_t duration) { diff --git a/firmware/common/rffc5071.c b/firmware/common/rffc5071.c index a5e4e46e..e9c1b72c 100644 --- a/firmware/common/rffc5071.c +++ b/firmware/common/rffc5071.c @@ -34,7 +34,6 @@ #include #include #include "rffc5071.h" -#include "rffc5071_spi.h" #include "rffc5071_regs.def" // private register def macros #include "hackrf_core.h" @@ -91,7 +90,7 @@ void rffc5071_setup(rffc5071_driver_t* const drv) { rffc5071_init(drv); - rffc5071_spi_init(drv->spi); + spi_init(drv->spi); /* initial setup */ /* put zeros in freq contol registers */ @@ -124,7 +123,7 @@ static uint16_t rffc5071_spi_read(rffc5071_driver_t* const drv, uint8_t r) { (void)drv; uint16_t data[] = { 0x80 | (r & 0x7f), 0xffff }; - rffc5071_spi_transfer(drv->spi, data, 2); + spi_transfer(drv->spi, data, 2); return data[1]; } @@ -132,7 +131,7 @@ static void rffc5071_spi_write(rffc5071_driver_t* const drv, uint8_t r, uint16_t (void)drv; uint16_t data[] = { 0x00 | (r & 0x7f), v }; - rffc5071_spi_transfer(drv->spi, data, 2); + spi_transfer(drv->spi, data, 2); } uint16_t rffc5071_reg_read(rffc5071_driver_t* const drv, uint8_t r) diff --git a/firmware/common/rffc5071.h b/firmware/common/rffc5071.h index c180ae84..18caf69f 100644 --- a/firmware/common/rffc5071.h +++ b/firmware/common/rffc5071.h @@ -25,7 +25,16 @@ #include -#include "rffc5071_spi.h" +#include "spi.h" + +/* 31 registers, each containing 16 bits of data. */ +#define RFFC5071_NUM_REGS 31 + +typedef struct { + spi_t* const spi; + uint16_t regs[RFFC5071_NUM_REGS]; + uint32_t regs_dirty; +} rffc5071_driver_t; /* Initialize chip. Call _setup() externally, as it calls _init(). */ extern void rffc5071_init(rffc5071_driver_t* const drv); diff --git a/firmware/common/rffc5071_spi.c b/firmware/common/rffc5071_spi.c index 45f709c8..1711c513 100644 --- a/firmware/common/rffc5071_spi.c +++ b/firmware/common/rffc5071_spi.c @@ -138,11 +138,13 @@ static uint32_t rffc5071_spi_exchange_word(spi_t* const spi, const uint32_t data * next 7 bits are register address, * next 16 bits are register value. */ -void rffc5071_spi_transfer(spi_t* const spi, uint16_t* const data, const size_t count) { +void rffc5071_spi_transfer(spi_t* const spi, void* const _data, const size_t count) { if( count != 2 ) { return; } + uint16_t* const data = _data; + const bool direction_read = (data[0] >> 7) & 1; /* @@ -171,3 +173,9 @@ void rffc5071_spi_transfer(spi_t* const spi, uint16_t* const data, const size_t */ rffc5071_spi_sck(spi); } + +void rffc5071_spi_transfer_gather(spi_t* const spi, const spi_transfer_t* const transfer, const size_t count) { + if( count == 1 ) { + rffc5071_spi_transfer(spi, transfer[0].data, transfer[0].count); + } +} diff --git a/firmware/common/rffc5071_spi.h b/firmware/common/rffc5071_spi.h index 7c3ef5fc..d20fe152 100644 --- a/firmware/common/rffc5071_spi.h +++ b/firmware/common/rffc5071_spi.h @@ -25,16 +25,8 @@ #include "spi.h" -/* 31 registers, each containing 16 bits of data. */ -#define RFFC5071_NUM_REGS 31 - -typedef struct { - spi_t* const spi; - uint16_t regs[RFFC5071_NUM_REGS]; - uint32_t regs_dirty; -} rffc5071_driver_t; - void rffc5071_spi_init(spi_t* const spi); -void rffc5071_spi_transfer(spi_t* const spi, uint16_t* const data, const size_t count); +void rffc5071_spi_transfer(spi_t* const spi, void* const data, const size_t count); +void rffc5071_spi_transfer_gather(spi_t* const spi, const spi_transfer_t* const transfer, const size_t count); #endif // __RFFC5071_SPI_H diff --git a/firmware/common/spi.c b/firmware/common/spi.c new file mode 100644 index 00000000..f743c0bc --- /dev/null +++ b/firmware/common/spi.c @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc. + * + * 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 "spi.h" + +void spi_init(spi_t* const spi) { + spi->init(spi); +} + +void spi_transfer(spi_t* const spi, void* const data, const size_t count) { + spi->transfer(spi, data, count); +} + +void spi_transfer_gather(spi_t* const spi, const spi_transfer_t* const transfers, const size_t count) { + spi->transfer_gather(spi, transfers, count); +} diff --git a/firmware/common/spi.h b/firmware/common/spi.h new file mode 100644 index 00000000..cad5f268 --- /dev/null +++ b/firmware/common/spi.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc. + * + * 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 __SPI_H__ +#define __SPI_H__ + +#include + +typedef struct { + void* const data; + const size_t count; +} spi_transfer_t; + +struct spi_t; +typedef struct spi_t spi_t; + +struct spi_t { + void (*init)(spi_t* const spi); + void (*transfer)(spi_t* const spi, void* const data, const size_t count); + void (*transfer_gather)(spi_t* const spi, const spi_transfer_t* const transfers, const size_t count); +}; + +void spi_init(spi_t* const spi); +void spi_transfer(spi_t* const spi, void* const data, const size_t count); +void spi_transfer_gather(spi_t* const spi, const spi_transfer_t* const transfers, const size_t count); + +#endif/*__SPI_H__*/ diff --git a/firmware/hackrf-common.cmake b/firmware/hackrf-common.cmake index 3ea5a212..0973d7f0 100644 --- a/firmware/hackrf-common.cmake +++ b/firmware/hackrf-common.cmake @@ -139,6 +139,7 @@ macro(DeclareTargets) ${PATH_HACKRF_FIRMWARE_COMMON}/max5864.c ${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071.c ${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071_spi.c + ${PATH_HACKRF_FIRMWARE_COMMON}/spi.c m0_bin.s )