File: 0010-W25Q80BV-Separate-structures-into-algorithm-and-hard.patch

package info (click to toggle)
hackrf 2015.07.2-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 69,764 kB
  • ctags: 9,327
  • sloc: ansic: 13,907; python: 696; vhdl: 218; sh: 32; makefile: 15
file content (352 lines) | stat: -rw-r--r-- 11,159 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
From 5a77d196ea548e702c61adaad257189e60620045 Mon Sep 17 00:00:00 2001
From: Jared Boone <jboone@earfeast.com>
Date: Tue, 4 Nov 2014 17:38:43 -0800
Subject: [PATCH 10/68] W25Q80BV: Separate structures into algorithm and
 hardware access layers.

Also extract #define constants for W25Q80BV into driver structure. Driver could eventually extend to other devices.
---
 firmware/common/hackrf_core.h          |  2 +-
 firmware/common/w25q80bv.c             | 84 ++++++++++++++++++----------------
 firmware/common/w25q80bv.h             | 13 ++++--
 firmware/common/w25q80bv_drv.c         | 16 +++----
 firmware/common/w25q80bv_drv.h         | 10 ++--
 firmware/hackrf_usb/usb_api_spiflash.c | 19 ++++----
 6 files changed, 77 insertions(+), 67 deletions(-)

--- a/firmware/common/hackrf_core.h
+++ b/firmware/common/hackrf_core.h
@@ -36,7 +36,7 @@
 
 #include "max2837_drv.h"
 #include "rffc5071_drv.h"
-#include "w25q80bv_drv.h"
+#include "w25q80bv.h"
 
 /* hardware identification number */
 #define BOARD_ID_JELLYBEAN  0
--- a/firmware/common/w25q80bv.c
+++ b/firmware/common/w25q80bv.c
@@ -51,7 +51,11 @@
 {
 	uint8_t device_id;
 
-	w25q80bv_spi_init(drv);
+	drv->page_len = 256U;
+	drv->num_pages = 4096U;
+	drv->num_bytes = 1048576U;
+
+	w25q80bv_spi_init(drv->hw);
 
 	device_id = 0;
 	while(device_id != W25Q80BV_DEVICE_ID_RES)
@@ -64,10 +68,10 @@
 {
 	uint8_t value;
 
-	w25q80bv_spi_select(drv);
-	w25q80bv_spi_transfer(drv, W25Q80BV_READ_STATUS1);
-	value = w25q80bv_spi_transfer(drv, 0xFF);
-	w25q80bv_spi_unselect(drv);
+	w25q80bv_spi_select(drv->hw);
+	w25q80bv_spi_transfer(drv->hw, W25Q80BV_READ_STATUS1);
+	value = w25q80bv_spi_transfer(drv->hw, 0xFF);
+	w25q80bv_spi_unselect(drv->hw);
 
 	return value;
 }
@@ -77,17 +81,17 @@
 {
 	uint8_t value;
 
-	w25q80bv_spi_select(drv);
-	w25q80bv_spi_transfer(drv, W25Q80BV_DEVICE_ID);
+	w25q80bv_spi_select(drv->hw);
+	w25q80bv_spi_transfer(drv->hw, W25Q80BV_DEVICE_ID);
 
 	/* Read 3 dummy bytes */
-	value = w25q80bv_spi_transfer(drv, 0xFF);
-	value = w25q80bv_spi_transfer(drv, 0xFF);
-	value = w25q80bv_spi_transfer(drv, 0xFF);
+	value = w25q80bv_spi_transfer(drv->hw, 0xFF);
+	value = w25q80bv_spi_transfer(drv->hw, 0xFF);
+	value = w25q80bv_spi_transfer(drv->hw, 0xFF);
 	/* Read Device ID shall return 0x13 for W25Q80BV */
-	value = w25q80bv_spi_transfer(drv, 0xFF);
+	value = w25q80bv_spi_transfer(drv->hw, 0xFF);
 	
-	w25q80bv_spi_unselect(drv);
+	w25q80bv_spi_unselect(drv->hw);
 
 	return value;
 }
@@ -97,21 +101,21 @@
 	int i;
 	uint8_t value;
 
-	w25q80bv_spi_select(drv);
-	w25q80bv_spi_transfer(drv, W25Q80BV_UNIQUE_ID);
+	w25q80bv_spi_select(drv->hw);
+	w25q80bv_spi_transfer(drv->hw, W25Q80BV_UNIQUE_ID);
 
 	/* Read 4 dummy bytes */
 	for(i=0; i<4; i++)
-		value = w25q80bv_spi_transfer(drv, 0xFF);
+		value = w25q80bv_spi_transfer(drv->hw, 0xFF);
 
 	/* Read Unique ID 64bits (8*8) */
 	for(i=0; i<8; i++)
 	{
-		value = w25q80bv_spi_transfer(drv, 0xFF);
+		value = w25q80bv_spi_transfer(drv->hw, 0xFF);
 		unique_id->id_8b[i]  = value;
 	}
 
-	w25q80bv_spi_unselect(drv);
+	w25q80bv_spi_unselect(drv->hw);
 }
 
 void w25q80bv_wait_while_busy(w25q80bv_driver_t* const drv)
@@ -122,9 +126,9 @@
 void w25q80bv_write_enable(w25q80bv_driver_t* const drv)
 {
 	w25q80bv_wait_while_busy(drv);
-	w25q80bv_spi_select(drv);
-	w25q80bv_spi_transfer(drv, W25Q80BV_WRITE_ENABLE);
-	w25q80bv_spi_unselect(drv);
+	w25q80bv_spi_select(drv->hw);
+	w25q80bv_spi_transfer(drv->hw, W25Q80BV_WRITE_ENABLE);
+	w25q80bv_spi_unselect(drv->hw);
 }
 
 void w25q80bv_chip_erase(w25q80bv_driver_t* const drv)
@@ -139,9 +143,9 @@
 
 	w25q80bv_write_enable(drv);
 	w25q80bv_wait_while_busy(drv);
-	w25q80bv_spi_select(drv);
-	w25q80bv_spi_transfer(drv, W25Q80BV_CHIP_ERASE);
-	w25q80bv_spi_unselect(drv);
+	w25q80bv_spi_select(drv->hw);
+	w25q80bv_spi_transfer(drv->hw, W25Q80BV_CHIP_ERASE);
+	w25q80bv_spi_unselect(drv->hw);
 }
 
 /* write up a 256 byte page or partial page */
@@ -150,24 +154,24 @@
 	int i;
 
 	/* do nothing if asked to write beyond a page boundary */
-	if (((addr & 0xFF) + len) > W25Q80BV_PAGE_LEN)
+	if (((addr & 0xFF) + len) > drv->page_len)
 		return;
 
 	/* do nothing if we would overflow the flash */
-	if (addr > (W25Q80BV_NUM_BYTES - len))
+	if (addr > (drv->num_bytes - len))
 		return;
 
 	w25q80bv_write_enable(drv);
 	w25q80bv_wait_while_busy(drv);
 
-	w25q80bv_spi_select(drv);
-	w25q80bv_spi_transfer(drv, W25Q80BV_PAGE_PROGRAM);
-	w25q80bv_spi_transfer(drv, (addr & 0xFF0000) >> 16);
-	w25q80bv_spi_transfer(drv, (addr & 0xFF00) >> 8);
-	w25q80bv_spi_transfer(drv, addr & 0xFF);
+	w25q80bv_spi_select(drv->hw);
+	w25q80bv_spi_transfer(drv->hw, W25Q80BV_PAGE_PROGRAM);
+	w25q80bv_spi_transfer(drv->hw, (addr & 0xFF0000) >> 16);
+	w25q80bv_spi_transfer(drv->hw, (addr & 0xFF00) >> 8);
+	w25q80bv_spi_transfer(drv->hw, addr & 0xFF);
 	for (i = 0; i < len; i++)
-		w25q80bv_spi_transfer(drv, data[i]);
-	w25q80bv_spi_unselect(drv);
+		w25q80bv_spi_transfer(drv->hw, data[i]);
+	w25q80bv_spi_unselect(drv->hw);
 }
 
 /* write an arbitrary number of bytes */
@@ -183,12 +187,12 @@
 	}	
 	
 	/* do nothing if we would overflow the flash */
-	if ((len > W25Q80BV_NUM_BYTES) || (addr > W25Q80BV_NUM_BYTES)
-			|| ((addr + len) > W25Q80BV_NUM_BYTES))
+	if ((len > drv->num_bytes) || (addr > drv->num_bytes)
+			|| ((addr + len) > drv->num_bytes))
 		return;
 
 	/* handle start not at page boundary */
-	first_block_len = W25Q80BV_PAGE_LEN - (addr % W25Q80BV_PAGE_LEN);
+	first_block_len = drv->page_len - (addr % drv->page_len);
 	if (len < first_block_len)
 		first_block_len = len;
 	if (first_block_len) {
@@ -199,11 +203,11 @@
 	}
 
 	/* one page at a time on boundaries */
-	while (len >= W25Q80BV_PAGE_LEN) {
-		w25q80bv_page_program(drv, addr, W25Q80BV_PAGE_LEN, data);
-		addr += W25Q80BV_PAGE_LEN;
-		data += W25Q80BV_PAGE_LEN;
-		len -= W25Q80BV_PAGE_LEN;
+	while (len >= drv->page_len) {
+		w25q80bv_page_program(drv, addr, drv->page_len, data);
+		addr += drv->page_len;
+		data += drv->page_len;
+		len -= drv->page_len;
 	}
 
 	/* handle end not at page boundary */
--- a/firmware/common/w25q80bv.h
+++ b/firmware/common/w25q80bv.h
@@ -24,11 +24,9 @@
 #ifndef __W25Q80BV_H__
 #define __W25Q80BV_H__
 
-#include "w25q80bv_drv.h"
+#include <stddef.h>
 
-#define W25Q80BV_PAGE_LEN     256U
-#define W25Q80BV_NUM_PAGES    4096U
-#define W25Q80BV_NUM_BYTES    1048576U
+#include "w25q80bv_drv.h"
 
 typedef union
 {
@@ -37,6 +35,13 @@
 	uint8_t id_8b[8]; /* 8*8bits 64bits Unique ID */
 } w25q80bv_unique_id_t;
 
+typedef struct {
+	w25q80bv_hw_t* hw;
+	size_t page_len;
+	size_t num_pages;
+	size_t num_bytes;
+} w25q80bv_driver_t;
+
 void w25q80bv_setup(w25q80bv_driver_t* const drv);
 void w25q80bv_chip_erase(w25q80bv_driver_t* const drv);
 void w25q80bv_program(w25q80bv_driver_t* const drv, uint32_t addr, uint32_t len, const uint8_t* data);
--- a/firmware/common/w25q80bv_drv.c
+++ b/firmware/common/w25q80bv_drv.c
@@ -30,7 +30,7 @@
 #include <libopencm3/lpc43xx/gpio.h>
 #include <libopencm3/lpc43xx/rgu.h>
 
-void w25q80bv_spi_init(w25q80bv_driver_t* const drv) {
+void w25q80bv_spi_init(w25q80bv_hw_t* const hw) {
 	const uint8_t serial_clock_rate = 2;
 	const uint8_t clock_prescale_rate = 2;
 
@@ -57,7 +57,7 @@
 
 	/* drive SSEL, HOLD, and WP pins high */
 	gpio_set(PORT_FLASH, (PIN_FLASH_HOLD | PIN_FLASH_WP));
-	w25q80bv_spi_unselect(drv);
+	w25q80bv_spi_unselect(hw);
 
 	/* Set GPIO pins as outputs. */
 	GPIO1_DIR |= (PIN_FLASH_HOLD | PIN_FLASH_WP);
@@ -75,17 +75,17 @@
 			SSP_SLAVE_OUT_ENABLE);
 }
 
-void w25q80bv_spi_select(w25q80bv_driver_t* const drv) {
-	(void)drv;
+void w25q80bv_spi_select(w25q80bv_hw_t* const hw) {
+	(void)hw;
 	gpio_clear(PORT_SSP0_SSEL, PIN_SSP0_SSEL);
 }
 
-void w25q80bv_spi_unselect(w25q80bv_driver_t* const drv) {
-	(void)drv;
+void w25q80bv_spi_unselect(w25q80bv_hw_t* const hw) {
+	(void)hw;
 	gpio_set(PORT_SSP0_SSEL, PIN_SSP0_SSEL);
 }
 
-uint16_t w25q80bv_spi_transfer(w25q80bv_driver_t* const drv, const uint16_t tx_data) {
-	(void)drv;
+uint16_t w25q80bv_spi_transfer(w25q80bv_hw_t* const hw, const uint16_t tx_data) {
+	(void)hw;
 	return ssp_transfer(SSP0_NUM, tx_data);
 }
--- a/firmware/common/w25q80bv_drv.h
+++ b/firmware/common/w25q80bv_drv.h
@@ -28,12 +28,12 @@
 
 typedef struct {
 	/* Empty for now */
-} w25q80bv_driver_t;
+} w25q80bv_hw_t;
 
-void w25q80bv_spi_init(w25q80bv_driver_t* const drv);
+void w25q80bv_spi_init(w25q80bv_hw_t* const hw);
 
-void w25q80bv_spi_select(w25q80bv_driver_t* const drv);
-uint16_t w25q80bv_spi_transfer(w25q80bv_driver_t* const drv, const uint16_t tx_data);
-void w25q80bv_spi_unselect(w25q80bv_driver_t* const drv);
+void w25q80bv_spi_select(w25q80bv_hw_t* const hw);
+uint16_t w25q80bv_spi_transfer(w25q80bv_hw_t* const hw, const uint16_t tx_data);
+void w25q80bv_spi_unselect(w25q80bv_hw_t* const hw);
 
 #endif//__W25Q80BV_DRV_H__
--- a/firmware/hackrf_usb/usb_api_spiflash.c
+++ b/firmware/hackrf_usb/usb_api_spiflash.c
@@ -30,7 +30,8 @@
 
 #include <w25q80bv.h>
 
-uint8_t spiflash_buffer[W25Q80BV_PAGE_LEN];
+/* Buffer size == spi_flash.page_len */
+uint8_t spiflash_buffer[256U];
 
 usb_request_status_t usb_vendor_request_erase_spiflash(
 		usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage)
@@ -58,8 +59,8 @@
 	if (stage == USB_TRANSFER_STAGE_SETUP) {
 		addr = (endpoint->setup.value << 16) | endpoint->setup.index;
 		len = endpoint->setup.length;
-		if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
-				|| ((addr + len) > W25Q80BV_NUM_BYTES)) {
+		if ((len > spi_flash.page_len) || (addr > spi_flash.num_bytes)
+				|| ((addr + len) > spi_flash.num_bytes)) {
 			return USB_REQUEST_STATUS_STALL;
 		} else {
 			usb_transfer_schedule_block(endpoint->out, &spiflash_buffer[0], len,
@@ -71,8 +72,8 @@
 		addr = (endpoint->setup.value << 16) | endpoint->setup.index;
 		len = endpoint->setup.length;
 		/* This check is redundant but makes me feel better. */
-		if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
-				|| ((addr + len) > W25Q80BV_NUM_BYTES)) {
+		if ((len > spi_flash.page_len) || (addr > spi_flash.num_bytes)
+				|| ((addr + len) > spi_flash.num_bytes)) {
 			return USB_REQUEST_STATUS_STALL;
 		} else {
 			w25q80bv_program(&spi_flash, addr, len, &spiflash_buffer[0]);
@@ -97,8 +98,8 @@
 	{
 		addr = (endpoint->setup.value << 16) | endpoint->setup.index;
 		len = endpoint->setup.length;
-		if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
-			    || ((addr + len) > W25Q80BV_NUM_BYTES)) {
+		if ((len > spi_flash.page_len) || (addr > spi_flash.num_bytes)
+			    || ((addr + len) > spi_flash.num_bytes)) {
 			return USB_REQUEST_STATUS_STALL;
 		} else {
 			/* TODO flush SPIFI "cache" before to read the SPIFI memory */
@@ -116,8 +117,8 @@
 			addr = (endpoint->setup.value << 16) | endpoint->setup.index;
 			len = endpoint->setup.length;
 			/* This check is redundant but makes me feel better. */
-			if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
-					|| ((addr + len) > W25Q80BV_NUM_BYTES)) 
+			if ((len > spi_flash.page_len) || (addr > spi_flash.num_bytes)
+					|| ((addr + len) > spi_flash.num_bytes)) 
 			{
 				return USB_REQUEST_STATUS_STALL;
 			} else