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
|
From f034bc82ca5c17647f0a1944943c293ae7aacb61 Mon Sep 17 00:00:00 2001
From: Jared Boone <jboone@earfeast.com>
Date: Sun, 9 Nov 2014 10:50:32 -0800
Subject: [PATCH 20/68] 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
--- 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 <libopencm3/lpc43xx/i2c.h>
@@ -41,9 +42,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)
{
--- a/firmware/common/rffc5071.c
+++ b/firmware/common/rffc5071.c
@@ -34,7 +34,6 @@
#include <stdint.h>
#include <string.h>
#include "rffc5071.h"
-#include "rffc5071_spi.h"
#include "rffc5071_regs.def" // private register def macros
#include "hackrf_core.h"
@@ -91,7 +90,7 @@
{
rffc5071_init(drv);
- rffc5071_spi_init(drv->spi);
+ spi_init(drv->spi);
/* initial setup */
/* put zeros in freq contol registers */
@@ -124,7 +123,7 @@
(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 @@
(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)
--- a/firmware/common/rffc5071.h
+++ b/firmware/common/rffc5071.h
@@ -25,7 +25,16 @@
#include <stdint.h>
-#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);
--- a/firmware/common/rffc5071_spi.c
+++ b/firmware/common/rffc5071_spi.c
@@ -138,11 +138,13 @@
* 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 @@
*/
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);
+ }
+}
--- 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
--- /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);
+}
--- /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 <stddef.h>
+
+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__*/
--- a/firmware/hackrf-common.cmake
+++ b/firmware/hackrf-common.cmake
@@ -140,6 +140,7 @@
${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
)
|