Data Logger BLE Cellular Board
Loading...
Searching...
No Matches
w25q64jv.c
Go to the documentation of this file.
1/*!
2 * \file w25q64jv.c
3 *
4 * \brief W25Q64JW Mb Flash Memory (I2C) functions
5 *
6 * \copyright J Factor Embedded Technologies LLC. 2020
7 * \copyright TJM Embedded Software LLC. 2020
8 * \author T. J. Mulrooney
9 *
10 * \sa
11 * <a href="https://www.adestotech.com/wp-content/uploads/DS-45DB641E-027.pdf">W25Q64JW Datasheet</a>
12 *
13 */
14/*
15 * All Rights Reserved
16 * UNPUBLISHED, LICENSED SOFTWARE.
17 *
18 * CONFIDENTIAL AND PROPRIETARY INFORMATION
19 * WHICH IS THE PROPERTY OF J-Factor Embedded Technologies.
20 *
21 */
22
24#include <stdio.h>
25
26/*!
27 * \fn void readNVFlashxID(uint8_t *flashId)
28 *
29 * \brief reads the NV Flash ID over the SPI bus.
30 *
31 * \param flashId pointer to the buffer to receive the Flash ID bytes
32 *
33 */
34void readNVFlashxID(uint8_t *flashId) {
35 uint8_t i = 0;
36 int ret;
37
38 /* zero buffer and load the command */
39 for (i = 0; i < 4; i++)
40 buffer_tx[i] = 0;
41 buffer_tx[0] = 0x9F;
42 tx_bufs[0].len = 4;
43 rx_bufs[0].len = 4;
44
45 /* enable CS and send command */
46 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
47
48 /* shift bytes back to caller byte 0 is not valid data */
49 for (i = 0; i < 3; i++)
50 flashId[i] = buffer_rx[i + 1];
51}
52
53/*!
54 * \fn void readNVFlashxStatus(uint8_t *flashStatus)
55 *
56 * \brief reads the 3 NV Flash status registers over the SPI bus.
57 *
58 * \param flashStatus pointer to the buffer to receive the Flash Status bytes
59 *
60 */
61void readNVFlashxStatus(uint8_t *flashStatus) {
62 uint8_t i;
63 uint8_t status[3];
64 int ret;
65
66 for (i = 0; i < 3; i++)
67 status[i] = 0;
68
69 /* zero buffer and load the command */
70 for (i = 0; i < 2; i++)
71 buffer_tx[i] = 0;
72
73 /* get status register 1 */
74 buffer_tx[0] = 0x05;
75 tx_bufs[0].len = 2;
76 rx_bufs[0].len = 2;
77
78 /* enable CS and send command */
79 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
80
81 /* get requested status register */
82 status[0] = buffer_rx[1];
83
84 /* zero buffer and load the command */
85 for (i = 0; i < 2; i++)
86 buffer_tx[i] = 0;
87
88 /* get status register 2 */
89 buffer_tx[0] = 0x35;
90 tx_bufs[0].len = 2;
91 rx_bufs[0].len = 2;
92
93 /* enable CS and send command */
94 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
95
96 /* get requested status register */
97 status[1] = buffer_rx[1];
98
99 /* zero buffer and load the command */
100 for (i = 0; i < 2; i++)
101 buffer_tx[i] = 0;
102
103 /* get status register 2 */
104 buffer_tx[0] = 0x15;
105 tx_bufs[0].len = 2;
106 rx_bufs[0].len = 2;
107
108 /* enable CS and send command */
109 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
110
111 /* get requested status register */
112 status[2] = buffer_rx[1];
113
114 /* send back status registers */
115 for (i = 0; i < 3; i++)
116 flashStatus[i] = status[i];
117}
118
119#if 1
120/*!
121 * \fn void writeNVFlashxStatus(void)
122 *
123 * \brief writes NV Flash status register2 over the SPI bus.
124 *
125 * clears teh QSPI bit
126 */
128 uint8_t i;
129 int ret;
130 uint8_t flashStatus[3];
131
132 /* zero buffer and load the command */
133 for (i = 0; i < 2; i++)
134 buffer_tx[i] = 0;
135
136 buffer_tx[0] = 0x06; /* non-volatile write enable */
137 tx_bufs[0].len = 1;
138 rx_bufs[0].len = 1;
139
140 /* enable CS and send command */
141 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
142
143 /* set status register 1 */
144 // buffer_tx[0] = 0x01;
145 // buffer_tx[1] = 0x00; /* SRP = 1 */
146 // tx_bufs[0].len = 2;
147 // rx_bufs[0].len = 2;
148
149 /* enable CS and send command */
150 // ret = spi_transceive(spi, &spi_cfg, &tx, &rx);
151
152 /* set status register 2 */
153 buffer_tx[0] = 0x31;
154 buffer_tx[1] = 0x00; /* QE = 0 */
155 tx_bufs[0].len = 2;
156 rx_bufs[0].len = 2;
157
158 /* enable CS and send command */
159 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
160
161 readNVFlashxStatus(flashStatus);
162
163 buffer_tx[0] = 0x04; /* write disable */
164 tx_bufs[0].len = 1;
165 rx_bufs[0].len = 1;
166
167 /* enable CS and send command */
168 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
169}
170#endif
171
172/*!
173 * \fn void flashxErasePage(uint32_t address)
174 *
175 * \brief erases a 256 byte page over the SPI bus.
176 *
177 * \param address address of the page to be erased
178 *
179 */
180void flashxErasePage(uint32_t address) {
181 uint8_t flashStatusIn[3];
182 uint32_t address32 = address << 11;
183 int ret;
184
185 buffer_tx[0] = 0x06; /* write enable */
186 tx_bufs[0].len = 1;
187 rx_bufs[0].len = 1;
188
189 /* enable CS and send command */
190 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
191
192 buffer_tx[0] = 0x20; /* block 1 erase */
193 buffer_tx[1] = (address32 >> 16) & 0xFF;
194 buffer_tx[2] = (address32 >> 8) & 0xFF;
195 buffer_tx[3] = (address32 >> 0) & 0xFF;
196 tx_bufs[0].len = 4;
197 rx_bufs[0].len = 4;
198
199 /* enable CS and send command to read ID into buffer */
200 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
201
202 printf("Erasing sector (16 pages) %d. Please wait ...\r\n", address);
203
204 while (1) /* wait until flag goes away */
205 {
206 readNVFlashxStatus(flashStatusIn);
207 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
208 if ((flashStatusIn[0] & 0x01) == 0x00) {
209 break;
210 }
211 k_sleep(K_MSEC(1));
212 }
213
214 buffer_tx[0] = 0x04; /* write disable */
215 tx_bufs[0].len = 1;
216 rx_bufs[0].len = 1;
217
218 /* enable CS and send command */
219 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
220}
221
222/*!
223 * \fn void flashxEraseBlock1(uint32_t address)
224 *
225 * \brief erases a 2K block page over the SPI bus.
226 *
227 * \param address address of the block to be erased
228 *
229 */
230void flashxEraseBlock1(uint32_t address) {
231 uint8_t flashStatusIn[3];
232 uint32_t address32 = address << 7;
233 int ret;
234
235 buffer_tx[0] = 0x06; /* write enable */
236 tx_bufs[0].len = 1;
237 rx_bufs[0].len = 1;
238
239 /* enable CS and send command */
240 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
241
242 buffer_tx[0] = 0x52; /* block 1 erase */
243 buffer_tx[1] = (address32 >> 16) & 0xFF;
244 buffer_tx[2] = (address32 >> 8) & 0xFF;
245 buffer_tx[3] = (address32 >> 0) & 0xFF;
246 tx_bufs[0].len = 4;
247 rx_bufs[0].len = 4;
248
249 /* enable CS and send command to read ID into buffer */
250 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
251
252 printf("Erasing Block %d. Please wait ...\r\n", address);
253
254 while (1) /* wait until flag goes away */
255 {
256 readNVFlashxStatus(flashStatusIn);
257 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
258 if ((flashStatusIn[0] & 0x01) == 0x00) {
259 break;
260 }
261 k_sleep(K_MSEC(1));
262 }
263
264 buffer_tx[0] = 0x04; /* write disable */
265 tx_bufs[0].len = 1;
266 rx_bufs[0].len = 1;
267
268 /* enable CS and send command */
269 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
270}
271
272/*!
273 * \fn void flashxEraseBlock1(uint32_t address)
274 *
275 * \brief erases a 2K block page over the SPI bus.
276 *
277 * \param address address of the block to be erased
278 *
279 */
280void flashxEraseBlock2(uint32_t address) {
281 uint8_t flashStatusIn[3];
282 uint32_t address32 = address << 4;
283 int ret;
284
285 buffer_tx[0] = 0x06; /* write enable */
286 tx_bufs[0].len = 1;
287 rx_bufs[0].len = 1;
288
289 /* enable CS and send command */
290 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
291
292 buffer_tx[0] = 0xD8; /* block 1 erase */
293 buffer_tx[1] = (address32 >> 16) & 0xFF;
294 buffer_tx[2] = (address32 >> 8) & 0xFF;
295 buffer_tx[3] = (address32 >> 0) & 0xFF;
296 tx_bufs[0].len = 4;
297 rx_bufs[0].len = 4;
298
299 /* enable CS and send command to read ID into buffer */
300 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
301
302 printf("Erasing Block %d. Please wait ...\r\n", address);
303
304 while (1) /* wait until flag goes away */
305 {
306 readNVFlashxStatus(flashStatusIn);
307 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
308 if ((flashStatusIn[0] & 0x01) == 0x00) {
309 break;
310 }
311 k_sleep(K_MSEC(1));
312 }
313
314 buffer_tx[0] = 0x04; /* write disable */
315 tx_bufs[0].len = 1;
316 rx_bufs[0].len = 1;
317
318 /* enable CS and send command */
319 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
320}
321
322/*!
323 * \fn void flashxEraseSector(uint32_t address)
324 *
325 * \brief erases a 256K byte sector over the SPI bus.
326 *
327 * \param address address of the sector to be erased
328 *
329 */
330void flashxEraseSector(uint32_t address) {
331 uint8_t flashStatusIn[3];
332 uint32_t address32 = address << 12;
333 int ret;
334
335 buffer_tx[0] = 0x06; /* write enable */
336 tx_bufs[0].len = 1;
337 rx_bufs[0].len = 1;
338
339 /* enable CS and send command */
340 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
341
342 buffer_tx[0] = 0x20; /* 4K sector erase */
343 buffer_tx[1] = (address32 >> 16) & 0xFF;
344 buffer_tx[2] = (address32 >> 8) & 0xFF;
345 buffer_tx[3] = (address32 >> 0) & 0xFF;
346 tx_bufs[0].len = 4;
347 rx_bufs[0].len = 4;
348
349 /* enable CS and send command to read ID into buffer */
350 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
351
352 printf("Erasing Sector %d. Please wait ...\r\n", address);
353
354 while (1) /* wait until flag goes away */
355 {
356 readNVFlashxStatus(flashStatusIn);
357 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
358 if ((flashStatusIn[0] & 0x01) == 0x00) {
359 break;
360 }
361 k_sleep(K_MSEC(1));
362 }
363
364 buffer_tx[0] = 0x04; /* write disable */
365 tx_bufs[0].len = 1;
366 rx_bufs[0].len = 1;
367
368 /* enable CS and send command */
369 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
370 printf("Erasing Sector %d. Finished ...\r\n", address);
371}
372
373/*!
374 * \fn void flashxErase(void)
375 *
376 * \brief erases the entire 8 Mb over the SPI bus.
377 *
378 */
379void flashxErase(void) {
380 uint8_t flashStatusIn[3];
381 int ret;
382
383 buffer_tx[0] = 0x06; /* write enable */
384 tx_bufs[0].len = 1;
385 rx_bufs[0].len = 1;
386
387 /* enable CS and send command */
388 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
389
390 buffer_tx[0] = 0xC7; /* write enable */
391 tx_bufs[0].len = 1;
392 rx_bufs[0].len = 1;
393
394 /* enable CS and send command to read ID into buffer */
395 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
396
397 printf("Erasing entire device. Please wait ...\r\n");
398
399 while (1) /* wait until flag goes away */
400 {
401 readNVFlashxStatus(flashStatusIn);
402 // printf("Flash Status: 0x%02X 0x%02X %d\r\n",flashStatusIn[0],flashStatusIn[1],(flashStatusIn[0] & 0x80));
403 if ((flashStatusIn[0] & 0x01) == 0x00) {
404 break;
405 }
406 k_sleep(K_MSEC(1));
407 }
408 printf("Entire device erased\r\n");
409
410 buffer_tx[0] = 0x04; /* write disable */
411 tx_bufs[0].len = 1;
412 rx_bufs[0].len = 1;
413
414 /* enable CS and send command */
415 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
416}
417
418/*!
419 * \fn void flashxReadPage(uint32_t address, uint8_t *flashRead)
420 *
421 * \brief reads a 256 byte page over the SPI bus.
422 *
423 * \param address 24 bit memory address of page to read
424 * \param flashRead - address of data array to received bytes from NV memory
425 *
426 */
427void flashxReadPage(uint32_t address, uint8_t *flashRead) {
428 uint16_t i;
429 uint32_t address32;
430 int ret;
431 uint8_t readBuffer[256];
432
433 /* start first 64 byte read from page */
434 /* zero buffer and load the command */
435 address32 = (address << 8) + 0; /* set first address */
436 for (i = 0; i < 64 + 4; i++)
437 buffer_tx[i] = 0;
438 buffer_tx[0] = 0x03;
439 buffer_tx[1] = (address32 >> 16) & 0xFF;
440 buffer_tx[2] = (address32 >> 8) & 0xFF;
441 buffer_tx[3] = (address32 >> 0) & 0xFF;
442 tx_bufs[0].len = 64 + 4;
443 rx_bufs[0].len = 64 + 4;
444
445 // printf("Flash Page Read: 0x%08X\r\n",address);
446 // printf("%02X %02X %02X %02X\r\n",flashReadCmd[0],flashReadCmd[1],flashReadCmd[2],flashReadCmd[3]);
447
448 /* enable CS and send command */
449 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
450
451 /* put bytes in buffer ignore first 4 bytes */
452 for (i = 0; i < 64; i++)
453 readBuffer[i] = buffer_rx[i + 4];
454 /* end first 64 byte read from page */
455
456 /* start second 64 byte read from page */
457 /* zero buffer and load the command */
458 address32 = (address << 8) + 64; /* set first address */
459 for (i = 0; i < 64 + 4; i++)
460 buffer_tx[i] = 0;
461 buffer_tx[0] = 0x03;
462 buffer_tx[1] = (address32 >> 16) & 0xFF;
463 buffer_tx[2] = (address32 >> 8) & 0xFF;
464 buffer_tx[3] = (address32 >> 0) & 0xFF;
465 tx_bufs[0].len = 64 + 4;
466 rx_bufs[0].len = 64 + 4;
467
468 // printf("Flash Page Read: 0x%08X\r\n",address);
469 // printf("%02X %02X %02X %02X\r\n",flashReadCmd[0],flashReadCmd[1],fsheadCmd[2],flashReadCmd[3]);
470
471 /* enable CS and send command */
472 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
473
474 /* put bytes in buffer ignore first 4 bytes */
475 for (i = 0; i < 64; i++)
476 readBuffer[i + 64] = buffer_rx[i + 4];
477 /* end second 64 byte read from page */
478
479 /* start third 64 byte read from page */
480 /* zero buffer and load the command */
481 address32 = (address << 8) + 128; /* set first address */
482 for (i = 0; i < 64 + 4; i++)
483 buffer_tx[i] = 0;
484 buffer_tx[0] = 0x03;
485 buffer_tx[1] = (address32 >> 16) & 0xFF;
486 buffer_tx[2] = (address32 >> 8) & 0xFF;
487 buffer_tx[3] = (address32 >> 0) & 0xFF;
488 tx_bufs[0].len = 64 + 4;
489 rx_bufs[0].len = 64 + 4;
490
491 // printf("Flash Page Read: 0x%08X\r\n",address);
492 // printf("%02X %02X %02X %02X\r\n",flashReadCmd[0],flashReadCmd[1],flashReadCmd[2],flashReadCmd[3]);
493
494 /* enable CS and send command */
495 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
496
497 /* put bytes in buffer ignore first 4 bytes */
498 for (i = 0; i < 64; i++)
499 readBuffer[i + 128] = buffer_rx[i + 4];
500 /* end third 64 byte read from page */
501
502 /* start fourth 64 byte read from page */
503 /* zero buffer and load the command */
504 address32 = (address << 8) + 192; /* set first address */
505 for (i = 0; i < 64 + 4; i++)
506 buffer_tx[i] = 0;
507 buffer_tx[0] = 0x03;
508 buffer_tx[1] = (address32 >> 16) & 0xFF;
509 buffer_tx[2] = (address32 >> 8) & 0xFF;
510 buffer_tx[3] = (address32 >> 0) & 0xFF;
511 tx_bufs[0].len = 64 + 4;
512 rx_bufs[0].len = 64 + 4;
513
514 // printf("Flash Page Read: 0x%08X\r\n",address);
515 // printf("%02X %02X %02X %02X\r\n",flashReadCmd[0],flashReadCmd[1],flashReadCmd[2],flashReadCmd[3]);
516
517 /* enable CS and send command */
518 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
519
520 /* put bytes in buffer ignore first 4 bytes */
521 for (i = 0; i < 64; i++)
522 readBuffer[i + 192] = buffer_rx[i + 4];
523 /* end fourth 64 byte read from page */
524
525 /* shift bytes back to caller byte 0 is not valid data */
526 for (i = 0; i < 256; i++) {
527 flashRead[i] = readBuffer[i];
528 }
529}
530
531/**
532 * \fn void flashxWritePage(uint32_t page, uint32_t address, uint8_t *data, uint16_t count)
533 *
534 * \brief writes data to address in serial flash over the SPI bus.
535 *
536 * \param page 24 bit memory address of page to write
537 * \param address address within page to begin writing
538 * \param data address of data array to get bytes to store bytes in NV memory
539 * \param count number of bytes to write
540 *
541 */
542void flashxWritePage(uint32_t page, uint32_t address, uint8_t *data, uint16_t count) {
543 int i;
544 uint8_t flashStatusIn[2];
545 // uint8_t flashIn[256+4];
546 uint32_t address32 = page << 8;
547 uint8_t dataPage[256 + 4];
548 int ret;
549
550#if 0
551 printf("flashxWritePage page: %d length: %d\r\n",page,count);
552 for(i=0;i<count;i++)
553 {
554 if( (i & 0xf) == 0)
555 printf("\r\n %02X",i);
556 printf(" 0x%02X",data[i]);
557 }
558#endif
559
560#if 1
561 buffer_tx[0] = 0x06; /* write enable */
562 tx_bufs[0].len = 1;
563 rx_bufs[0].len = 1;
564
565 /* enable CS and send command */
566 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
567
568 // printf("page: %d address: %d data: %02X count: %d\r\n",page,address,data[0],count);
569 flashxReadPage(page, dataPage); /* read in page data */
570
571 // for(i=0;i<256;i++)
572 // printf("i: %d data: %02X\r\n",i,dataPage[i]);
573 for (i = 0; i < count; i++) /* move in new data */
574 dataPage[i + address] = data[i];
575 // for(i=0;i<256;i++)
576 // printf("i: %d data: %02X\r\n",i,dataPage[i]);
577
578 /* write first 64 byte write */
579 address32 = (page << 8) + 0;
580 buffer_tx[0] = 0x02; /* chip program */
581 buffer_tx[1] = (address32 >> 16) & 0xFF;
582 buffer_tx[2] = (address32 >> 8) & 0xFF;
583 buffer_tx[3] = (address32 >> 0) & 0xFF;
584 for (i = 0; i < 64; i++)
585 buffer_tx[i + 4] = dataPage[i];
586 tx_bufs[0].len = 64 + 4;
587 rx_bufs[0].len = 64 + 4;
588
589 /* enable CS and send command */
590 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
591
592 while (1) /* wait until flag goes away */
593 {
594 readNVFlashxStatus(flashStatusIn);
595 // PRINTF("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
596 if ((flashStatusIn[0] & 0x01) == 0x00) {
597 break;
598 }
599 k_sleep(K_MSEC(1));
600 }
601
602 buffer_tx[0] = 0x04; /* write disable */
603 tx_bufs[0].len = 1;
604 rx_bufs[0].len = 1;
605
606 /* enable CS and send command */
607 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
608
609 /* end of first 64 byte write */
610// myPrintkI("first 64 bytes written\r\n");
611#endif
612
613#if 1
614 buffer_tx[0] = 0x06; /* write enable */
615 tx_bufs[0].len = 1;
616 rx_bufs[0].len = 1;
617
618 /* enable CS and send command */
619 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
620
621 /* write second 64 byte write */
622 address32 = (page << 8) + 64;
623 buffer_tx[0] = 0x02; /* chip program */
624 buffer_tx[1] = (address32 >> 16) & 0xFF;
625 buffer_tx[2] = (address32 >> 8) & 0xFF;
626 buffer_tx[3] = (address32 >> 0) & 0xFF;
627 for (i = 0; i < 64; i++)
628 buffer_tx[i + 4] = dataPage[i + 64];
629 tx_bufs[0].len = 64 + 4;
630 rx_bufs[0].len = 64 + 4;
631
632 /* enable CS and send command */
633 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
634
635 while (1) /* wait until flag goes away */
636 {
637 readNVFlashxStatus(flashStatusIn);
638 // PRINTF("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
639 if ((flashStatusIn[0] & 0x01) == 0x00) {
640 break;
641 }
642 k_sleep(K_MSEC(1));
643 }
644
645 buffer_tx[0] = 0x04; /* write disable */
646 tx_bufs[0].len = 1;
647 rx_bufs[0].len = 1;
648
649 /* enable CS and send command */
650 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
651
652 /* end of second 64 byte write */
653// myPrintkI("second 64 bytes written\r\n");
654#endif
655
656#if 1
657 buffer_tx[0] = 0x06; /* write enable */
658 tx_bufs[0].len = 1;
659 rx_bufs[0].len = 1;
660
661 /* enable CS and send command */
662 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
663
664 /* write third 64 byte write */
665 address32 = (page << 8) + 128;
666 buffer_tx[0] = 0x02; /* chip program */
667 buffer_tx[1] = (address32 >> 16) & 0xFF;
668 buffer_tx[2] = (address32 >> 8) & 0xFF;
669 buffer_tx[3] = (address32 >> 0) & 0xFF;
670 for (i = 0; i < 64; i++)
671 buffer_tx[i + 4] = dataPage[i + 128];
672 tx_bufs[0].len = 64 + 4;
673 rx_bufs[0].len = 64 + 4;
674
675 /* enable CS and send command */
676 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
677
678 while (1) /* wait until flag goes away */
679 {
680 readNVFlashxStatus(flashStatusIn);
681 // PRINTF("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
682 if ((flashStatusIn[0] & 0x01) == 0x00) {
683 break;
684 }
685 k_sleep(K_MSEC(1));
686 }
687
688 buffer_tx[0] = 0x04; /* write disable */
689 tx_bufs[0].len = 1;
690 rx_bufs[0].len = 1;
691
692 /* enable CS and send command */
693 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
694
695 /* end of third 64 byte write */
696// myPrintkI("third 64 bytes written\r\n");
697#endif
698
699#if 1
700 buffer_tx[0] = 0x06; /* write enable */
701 tx_bufs[0].len = 1;
702 rx_bufs[0].len = 1;
703
704 /* enable CS and send command */
705 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
706
707 /* write fourth 64 byte write */
708 address32 = (page << 8) + 192;
709 buffer_tx[0] = 0x02; /* chip program */
710 buffer_tx[1] = (address32 >> 16) & 0xFF;
711 buffer_tx[2] = (address32 >> 8) & 0xFF;
712 buffer_tx[3] = (address32 >> 0) & 0xFF;
713 for (i = 0; i < 64; i++)
714 buffer_tx[i + 4] = dataPage[i + 192];
715 tx_bufs[0].len = 64 + 4;
716 rx_bufs[0].len = 64 + 4;
717
718 /* enable CS and send command */
719 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
720
721 while (1) /* wait until flag goes away */
722 {
723 readNVFlashxStatus(flashStatusIn);
724 // PRINTF("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
725 if ((flashStatusIn[0] & 0x01) == 0x00) {
726 break;
727 }
728 k_sleep(K_MSEC(1));
729 }
730
731 buffer_tx[0] = 0x04; /* write disable */
732 tx_bufs[0].len = 1;
733 rx_bufs[0].len = 1;
734
735 /* enable CS and send command */
736 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
737
738 /* end of fourth 64 byte write */
739// myPrintkI("fourth 64 bytes written\r\n");
740#endif
741}
742
743/*!
744 * \fn void flashxFillPage(uint32_t address, uint16_t data)
745 *
746 * \brief fills a 256 byte page with data over the SPI bus.
747 *
748 * \param address 24 bit memory address of page to write
749 * \param data byte to fill in page in NV memory
750 *
751 */
752void flashxFillPage(uint32_t address, uint16_t data) {
753 int i;
754 uint8_t flashStatusIn[2];
755 uint32_t address32 = address << 8;
756 int ret;
757
758#if 1
759 buffer_tx[0] = 0x06; /* write enable */
760 tx_bufs[0].len = 1;
761 rx_bufs[0].len = 1;
762
763 /* enable CS and send command */
764 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
765
766 /* fill first 64 bytes */
767 address32 = (address << 8) + 0;
768 buffer_tx[0] = 0x02; /* chip program */
769 buffer_tx[1] = (address32 >> 16) & 0xFF;
770 buffer_tx[2] = (address32 >> 8) & 0xFF;
771 buffer_tx[3] = (address32 >> 0) & 0xFF;
772 for (i = 0; i < 64; i++)
773 buffer_tx[i + 4] = data;
774 tx_bufs[0].len = 64 + 4;
775 rx_bufs[0].len = 64 + 4;
776
777 /* enable CS and send command */
778 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
779
780 while (1) /* wait until flag goes away */
781 {
782 readNVFlashxStatus(flashStatusIn);
783 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
784 if ((flashStatusIn[0] & 0x01) == 0x00) {
785 break;
786 }
787 // k_sleep(K_MSEC(1));
788 }
789 // myPrintkI("first 64 bytes written\r\n");
790
791 buffer_tx[0] = 0x04; /* write disable */
792 tx_bufs[0].len = 1;
793 rx_bufs[0].len = 1;
794
795 /* enable CS and send command */
796 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
797
798 /* end fill first 64 bytes */
799#endif
800
801#if 1
802 buffer_tx[0] = 0x06; /* write enable */
803 tx_bufs[0].len = 1;
804 rx_bufs[0].len = 1;
805
806 /* enable CS and send command */
807 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
808
809 /* fill second 64 bytes */
810 address32 = (address << 8) + 64;
811 buffer_tx[0] = 0x02; /* chip program */
812 buffer_tx[1] = (address32 >> 16) & 0xFF;
813 buffer_tx[2] = (address32 >> 8) & 0xFF;
814 buffer_tx[3] = (address32 >> 0) & 0xFF;
815 for (i = 0; i < 64; i++)
816 buffer_tx[i + 4] = data;
817 tx_bufs[0].len = 64 + 4;
818 rx_bufs[0].len = 64 + 4;
819
820 /* enable CS and send command */
821 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
822
823 while (1) /* wait until flag goes away */
824 {
825 readNVFlashxStatus(flashStatusIn);
826 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
827 if ((flashStatusIn[0] & 0x01) == 0x00) {
828 break;
829 }
830 // k_sleep(K_MSEC(1));
831 }
832 // myPrintkI("second 64 bytes written\r\n");
833
834 buffer_tx[0] = 0x04; /* write disable */
835 tx_bufs[0].len = 1;
836 rx_bufs[0].len = 1;
837
838 /* enable CS and send command */
839 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
840
841 /* end fill second 64 bytes */
842#endif
843
844#if 1
845 buffer_tx[0] = 0x06; /* write enable */
846 tx_bufs[0].len = 1;
847 rx_bufs[0].len = 1;
848
849 /* enable CS and send command */
850 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
851
852 /* fill third 64 bytes */
853 address32 = (address << 8) + 128;
854 buffer_tx[0] = 0x02; /* chip program */
855 buffer_tx[1] = (address32 >> 16) & 0xFF;
856 buffer_tx[2] = (address32 >> 8) & 0xFF;
857 buffer_tx[3] = (address32 >> 0) & 0xFF;
858 for (i = 0; i < 64; i++)
859 buffer_tx[i + 4] = data;
860 tx_bufs[0].len = 64 + 4;
861 rx_bufs[0].len = 64 + 4;
862
863 /* enable CS and send command */
864 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
865
866 while (1) /* wait until flag goes away */
867 {
868 readNVFlashxStatus(flashStatusIn);
869 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
870 if ((flashStatusIn[0] & 0x01) == 0x00) {
871 break;
872 }
873 // delay_ms(1);
874 }
875 // myPrintkI("third 64 bytes written\r\n");
876
877 buffer_tx[0] = 0x04; /* write disable */
878 tx_bufs[0].len = 1;
879 rx_bufs[0].len = 1;
880
881 /* enable CS and send command */
882 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
883
884 /* end fill second 64 bytes */
885#endif
886
887#if 1
888 buffer_tx[0] = 0x06; /* write enable */
889 tx_bufs[0].len = 1;
890 rx_bufs[0].len = 1;
891
892 /* enable CS and send command */
893 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
894
895 /* fill fourth 64 bytes */
896 address32 = (address << 8) + 192;
897 buffer_tx[0] = 0x02; /* chip program */
898 buffer_tx[1] = (address32 >> 16) & 0xFF;
899 buffer_tx[2] = (address32 >> 8) & 0xFF;
900 buffer_tx[3] = (address32 >> 0) & 0xFF;
901 for (i = 0; i < 64; i++)
902 buffer_tx[i + 4] = data;
903 tx_bufs[0].len = 64 + 4;
904 rx_bufs[0].len = 64 + 4;
905
906 /* enable CS and send command */
907 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
908
909 while (1) /* wait until flag goes away */
910 {
911 readNVFlashxStatus(flashStatusIn);
912 // printf("Flash Status: 0x%02X 0x%02X\r\n",flashStatusIn[0],flashStatusIn[1]);
913 if ((flashStatusIn[0] & 0x01) == 0x00) {
914 break;
915 }
916 // delay_ms(1);
917 }
918 /* end fill fourth 64 bytes */
919 // myPrintkI("fourth 64 bytes written\r\n");
920
921 buffer_tx[0] = 0x04; /* write disable */
922 tx_bufs[0].len = 1;
923 rx_bufs[0].len = 1;
924
925 /* enable CS and send command */
926 ret = spi_transceive(spi_dev, &spi_cfg1, &tx, &rx);
927#endif
928}
929
930/*!
931 * \fn void flashxClearParameters(void)
932 *
933 * \brief clears/initializes the flash parameters in RAM
934 *
935 * This is called on power up reset after the NV flash parameters have been erased \n
936 * from Page 0 1nd 1. only called if the Key is not set in the NV Flash Parameters.
937 *
938 */
940
941 // flashParameters.tempUnitsCentigrade = true;
942 // flashParameters.dateSet = false;
943 // flashParameters.timeZone = DEFAULT_TIMEZONE;
944 // flashParameters.alarmsEnabled = false;
945 // flashParameters.gpio09Mode = 1;
946 // flashParameters.lowPower = 0;
947 // flashParameters.gpsEnable = false;
948 // flashParameters.gpsExternal = false;
949 // jfet_dk_set_led(GPSENB,flashParameters.gpsEnable?1:0);
950 // flashParameters.modenPMEnable = false;
951 // jfet_dk_set_led(GPSENB,flashParameters.modenPMEnable?1:0);
952 // flashParameters.offsetAM2315 = 0;
953 // flashParameters.offsetAM2315Humidity = 0;
954 // flashParameters.offsetMSPPressure = 0;
955 // flashParameters.offsetRTD = 0;
956 // flashParameters.offsetThermistor = 0;
957 // flashParameters.sw1 = false;
958 // flashParameters.sw2 = false;
959 flashParameters.silent = false;
960 flashParameters.manualTrigger = true;
961 flashParameters.nouart0 = false;
962 flashParameters.ignoreBME688 = true;
964 strcpy(flashParameters.sensorID, "No ID");
965 strcpy(flashParameters.sensorName, "No Sensor Name");
966 strcpy(flashParameters.customer, "No Customer Name");
967 strcpy(flashParameters.location, "No Location Name");
968 strcpy(flashParameters.group, "No Group Name");
969 // strcpy(flashParameters.distributor,"No Distributor Name");
970 // strcpy(flashParameters.clientID,"x");
971 //// strcpy(flashParameters.edrx_param,CONFIG_LTE_EDRX_REQ_VALUE);
972 //// strcpy(flashParameters.ptw_param,CONFIG_LTE_PTW_VALUE);
973 // strcpy(flashParameters.ptw_param,"");
974 // strcpy(flashParameters.psm_param_rat,CONFIG_LTE_PSM_REQ_RAT);
975 // strcpy(flashParameters.psm_param_rptau,CONFIG_LTE_PSM_REQ_RPTAU);
976 // memset(&flashParameters.tempAlarms[0],0,sizeof(tempAlarmStruct) * MAX_NUMBER_TEMPERATURE_SENSORS);
977 // flashParameters.tempAlarmValid = DEFAULT_TEMPALARMVALID;
978 // flashParameters.tempAlarmCheck = DEFAULT_TEMPALARMCHECK;
979 // memset(&flashParameters.humidityAlarms[0],0,sizeof(humidityAlarmStruct) * MAX_NUMBER_HUMIDITY_SENSORS);
980 // flashParameters.humidityAlarmValid = DEFAULT_HUMIDITYALARMVALID;
981 // flashParameters.humidityAlarmCheck = DEFAULT_HUMIDITYALARMCHECK;
982 // memset(&flashParameters.pressureAlarms[0],0,sizeof(pressureAlarmStruct) * MAX_NUMBER_PRESSURE_SENSORS);
983 // flashParameters.pressureAlarmValid = DEFAULT_PRESSUREALARMVALID;
984 // flashParameters.pressureAlarmCheck = DEFAULT_PRESSUREALARMCHECK;
985 // memset(&flashParameters.ezoecAlarms[0],0,sizeof(ezoecAlarmStruct) * MAX_NUMBER_EZOEC_SENSORS);
986 // flashParameters.ezoecAlarmValid = DEFAULT_EZOECALARMVALID;
987 // flashParameters.ezoecAlarmCheck = DEFAULT_EZOECALARMCHECK;
988 // memset(&flashParameters.powerAlarms[0],0,sizeof(powerAlarmStruct) * MAX_NUMBER_POWER_SENSORS);
989 // flashParameters.powerAlarmValid = DEFAULT_POWERALARMVALID;
990 // flashParameters.powerAlarmCheck = DEFAULT_POWERALARMCHECK;
991 flashParameters.savedCount = 0;
992 flashParameters.nextSavedRead = 0;
993 flashParameters.nextSavedWrite = 0;
994 // #if 0
995 // flashParameters.tempAlarms[1].tempAlarm = 0xF;
996 // flashParameters.tempAlarms[1].lowTempCheck = (int16_t)(20.0 * 100);
997 // flashParameters.tempAlarms[1].lowTempAlarmValue = (int16_t)(13.67 * 100);
998 // flashParameters.tempAlarms[1].highTempCheck = (int16_t)(30.0 * 100);
999 // flashParameters.tempAlarms[1].highTempAlarmValue = (int16_t)(33.67 * 100);
1000 // struct tm time;
1001 // time_t curTime;
1002 // getRTCTime(&time);
1003 // curTime = mktime(&time);
1004 // flashParameters.tempAlarms[1].lowTempTime = curTime;
1005 // flashParameters.tempAlarms[1].lowTempTime = curTime;
1006 // flashParameters.tempAlarms[1].highTempTime = curTime;
1007 // flashParameters.tempAlarms[1].highTempTime = curTime;
1008 // #endif
1009 myPrintkI("Flash parameters reset to defaults\r\n");
1010}
1011
1012/*!
1013 * \fn void void displayFlashxParameters(void)
1014 *
1015 * \brief displays the NV flash parameters.
1016 *
1017 * This also first loads the RAM flash parameters from the NV Flash Parameters\n
1018 * so it reloads the RAM flash parameters.
1019 *
1020 */
1022 printf("FLASH Parameters\r\n");
1023#if 1
1024 uint8_t data0[256 + 8];
1025 int i;
1026 uint8_t *pFlashParameters = (uint8_t *)&flashParameters;
1027
1028 // if (flashParameters.silent)
1029 // return;
1030 printf("Flash Parameters Size: %d\r\n", sizeof(flashParametersStruct));
1031 if (sizeof(flashParametersStruct) <= 256) {
1033 for (i = 0; i < sizeof(flashParametersStruct); i++)
1034 pFlashParameters[i] = data0[i];
1035 } else if (sizeof(flashParametersStruct) <= 512) {
1037 for (i = 0; i < 256; i++)
1038 pFlashParameters[i] = data0[i];
1040 for (i = 0; i < sizeof(flashParametersStruct) - 256; i++)
1041 pFlashParameters[i + 256] = data0[i];
1042 } else if (sizeof(flashParametersStruct) <= 768) {
1044 for (i = 0; i < 256; i++)
1045 pFlashParameters[i] = data0[i];
1047 for (i = 0; i < 256; i++)
1048 pFlashParameters[i + 256] = data0[i];
1050 for (i = 0; i < sizeof(flashParametersStruct) - 512; i++)
1051 pFlashParameters[i + 512] = data0[i];
1052 } else {
1054 for (i = 0; i < 256; i++)
1055 pFlashParameters[i] = data0[i];
1057 for (i = 0; i < 256; i++)
1058 pFlashParameters[i + 256] = data0[i];
1060 for (i = 0; i < 256; i++)
1061 pFlashParameters[i + 512] = data0[i];
1063 for (i = 0; i < sizeof(flashParametersStruct) - 768; i++)
1064 pFlashParameters[i + 768] = data0[i];
1065 }
1067 printf(" Key: 0x%08X\r\n", (unsigned int)flashParameters.key);
1068 // printf(" Date Set: %s\r\n",flashParameters.dateSet?"True":"False");
1069 // printf(" Time Zone: %d\r\n",flashParameters.timeZone);
1070 if (flashParameters.dutyCycle < 7) {
1071 printf(" Duty Cycle: %d Setting\r\n", flashParameters.dutyCycle);
1072 } else {
1073 printf(" Duty Cycle: %d Minutes\r\n", flashParameters.dutyCycle - 7);
1074 }
1075 // printf(" SW1: %s\r\n",flashParameters.sw1?"True":"False");
1076 // printf(" SW2: %s\r\n",flashParameters.sw2?"True":"False");
1077 // printf(" GPIO09 Mode: %s\r\n",flashParameters.gpio09Mode?"Input":"Output");
1078 // if(flashParameters.lowPower > 2)
1079 // flashParameters.lowPower = 0;
1080 //// printf(" Low Power: %s\r\n",powerModes[flashParameters.lowPower]);
1081 //// printf(" Mode_nPM Enable: %s\r\n",flashParameters.modenPMEnable?"True":"False");
1082 //// printf(" GPS Enable: %s\r\n",flashParameters.gpsEnable?"True":"False");
1083 //// printf(" GPS External: %s\r\n",flashParameters.gpsExternal?"External":"Internal");
1084 //// printf(" GPS Fix Check: %ld seconds\r\n",(long int)flashParameters.gpsFixCheck);
1085 // printf(" Offset Am2315: %3.3f C\r\n",flashParameters.offsetAM2315 / 1000.0);
1086 // printf(" Offset Am2315 Humidity: %3.3f %%\r\n",flashParameters.offsetAM2315Humidity / 1000.0);
1087 // printf(" Offset MSP300 Pressure: %3.3f PSI\r\n",flashParameters.offsetMSPPressure / 1000.0);
1088 // printf(" Offset RTD: %3.3f C\r\n",flashParameters.offsetRTD / 1000.0);
1089 // printf(" Offset Thermistor: %3.3f C\r\n",flashParameters.offsetThermistor / 1000.0);
1090 printf(" Silent: %s\r\n", flashParameters.silent ? "True" : "False");
1091 printf(" Manual Trigger: %s\r\n", flashParameters.manualTrigger ? "True" : "False");
1092 printf(" Ignore BME688: %s\r\n", flashParameters.ignoreBME688 ? "True" : "False");
1093 printf(" No UART0: %s\r\n", flashParameters.nouart0 ? "True" : "False");
1094 printf(" Sensor ID: %s\r\n", flashParameters.sensorID);
1095 printf(" Sensor Name: %s\r\n", flashParameters.sensorName);
1096 printf(" Customer: %s\r\n", flashParameters.customer);
1097 printf(" Location: %s\r\n", flashParameters.location);
1098 printf(" Group: %s\r\n", flashParameters.group);
1099 // printf(" Distributor: %s\r\n",flashParameters.distributor);
1100 // printf(" Client ID: %s\r\n",flashParameters.clientID);
1101 // printf(" EDRX Parameter: %s\r\n",flashParameters.edrx_param);
1102 //// printf(" EDRX PTW: %s\r\n",flashParameters.ptw_param);
1103 // printf(" PSM Tau: %s\r\n",flashParameters.psm_param_rptau);
1104 // printf(" PSM AT: %s\r\n",flashParameters.psm_param_rat);
1105 printf(" BLE Packets Saved: %d\r\n", flashParameters.savedCount);
1106 printf(" Next Saved BLE Packet Read: %d 0x%X\r\n", flashParameters.nextSavedRead, flashParameters.nextSavedRead);
1107 printf(" Next Saved BLE Packet Write: %d 0x%X\r\n", flashParameters.nextSavedWrite, flashParameters.nextSavedWrite);
1108 } else {
1109 printf("Flash Parameter Key: 0x%08X Not Set\r\n", flashParameters.key);
1110 }
1111#endif
1112}
1113
1114/*!
1115 * \fn void setFlashxParameters(uint8_t display)
1116 *
1117 * \brief loads the NV Flash parameters from teh RAM flash parameters
1118 *
1119 * \param display true to display parameters after the write, false to not display after a write
1120 *
1121 */
1122void setFlashxParameters(uint8_t display) {
1123 // int i;
1124 uint16_t address = FLASHPARAMETERSTART;
1125 uint8_t *pFlashParameters = (uint8_t *)&flashParameters;
1126
1127 myPrintkI("Setting Flash Parameters\r\n");
1128 /* erase page */
1130 k_sleep(K_MSEC(200));
1131
1133 if (sizeof(flashParametersStruct) <= 256) {
1134 myPrintkI("Flash Parameters Size: %d\r\n", sizeof(flashParametersStruct));
1135 memcpy(tmpData, pFlashParameters, sizeof(flashParametersStruct));
1137 myPrintkI("Flash Parameters Written\r\n");
1138 } else if (sizeof(flashParametersStruct) <= 512) {
1139 memcpy(tmpData, pFlashParameters, 256);
1141 memcpy(tmpData, pFlashParameters + 256, sizeof(flashParametersStruct) - 256);
1142 flashxWritePage(FLASHPARAMETERSTART + 1, address, tmpData, (uint16_t)(sizeof(flashParametersStruct) - 256));
1143 } else if (sizeof(flashParametersStruct) <= 768) {
1144 memcpy(tmpData, pFlashParameters, 256);
1146 memcpy(tmpData, pFlashParameters + 256, 256);
1147 flashxWritePage(FLASHPARAMETERSTART + 1, address, tmpData, 256);
1148 memcpy(tmpData, pFlashParameters + 512, sizeof(flashParametersStruct) - 512);
1149 flashxWritePage(FLASHPARAMETERSTART + 2, address, tmpData, (uint16_t)(sizeof(flashParametersStruct) - 512));
1150 } else {
1151 memcpy(tmpData, pFlashParameters, 256);
1153 memcpy(tmpData, pFlashParameters + 256, 256);
1154 flashxWritePage(FLASHPARAMETERSTART + 1, address, tmpData, 256);
1155 memcpy(tmpData, pFlashParameters + 512, 256);
1156 flashxWritePage(FLASHPARAMETERSTART + 2, address, tmpData, 256);
1157 memcpy(tmpData, pFlashParameters + 768, sizeof(flashParametersStruct) - 768);
1158 flashxWritePage(FLASHPARAMETERSTART + 3, address, tmpData, (uint16_t)(sizeof(flashParametersStruct) - 512));
1159 }
1160 // for(i=0; i < sizeof(flashParametersStruct); i++)
1161 // PRINTF("%02X %02X\r\n",tmp[i],pFlashParameters[i]);
1162 // for(i=0; i < sizeof(flashParametersStruct); i++,address++)
1163 // {
1164 // PRINTF("i: %d %02X %02X\r\n",i,address,tmp[i]);
1165 // flashxWritePage(FLASHPARAMETERSTART, address,tmp,256);
1166 // }
1167 if (!flashParameters.silent)
1168 printf("Flash Parameters Set\r\n");
1169 // if (display)
1170 // {
1171 // displayFlashxParameters();
1172 // }
1173 // // publishSensor = true;
1174}
1175
1176/** \fn void getFlashparameters(void)
1177 * \brief Read the stored flash parameter block from serial flash.
1178 *
1179 * The flash parameter structure is stored in consecutive 256-byte pages.
1180 * This function reads only the number of pages needed for the current
1181 * sizeof(flashParametersStruct).
1182 */
1184 uint8_t data0[256 + 8];
1185 uint8_t *pFlashParameters = (uint8_t *)&flashParameters;
1186 int i;
1187
1188 /* Read the flash parameter pages based on the current structure size. */
1189 if (sizeof(flashParametersStruct) <= 256) {
1191 for (i = 0; i < sizeof(flashParametersStruct); i++)
1192 pFlashParameters[i] = data0[i];
1193 } else if (sizeof(flashParametersStruct) <= 512) {
1195 for (i = 0; i < 256; i++)
1196 pFlashParameters[i] = data0[i];
1198 for (i = 0; i < sizeof(flashParametersStruct) - 256; i++)
1199 pFlashParameters[i + 256] = data0[i];
1200 } else if (sizeof(flashParametersStruct) <= 768) {
1202 for (i = 0; i < 256; i++)
1203 pFlashParameters[i] = data0[i];
1205 for (i = 0; i < 256; i++)
1206 pFlashParameters[i + 256] = data0[i];
1208 for (i = 0; i < sizeof(flashParametersStruct) - 512; i++)
1209 pFlashParameters[i + 512] = data0[i];
1210 } else if (sizeof(flashParametersStruct) <= 1024) {
1212 for (i = 0; i < 256; i++)
1213 pFlashParameters[i] = data0[i];
1215 for (i = 0; i < 256; i++)
1216 pFlashParameters[i + 256] = data0[i];
1218 for (i = 0; i < 256; i++)
1219 pFlashParameters[i + 512] = data0[i];
1221 for (i = 0; i < sizeof(flashParametersStruct) - 768; i++)
1222 pFlashParameters[i + 768] = data0[i];
1223 } else {
1224 /* For parameter structures larger than 1024 bytes, read the next pages sequentially. */
1226 for (i = 0; i < 256; i++)
1227 pFlashParameters[i] = data0[i];
1229 for (i = 0; i < 256; i++)
1230 pFlashParameters[i + 256] = data0[i];
1232 for (i = 0; i < 256; i++)
1233 pFlashParameters[i + 512] = data0[i];
1235 for (i = 0; i < 256; i++)
1236 pFlashParameters[i + 768] = data0[i];
1238 for (i = 0; i < sizeof(flashParametersStruct) - 1024; i++)
1239 pFlashParameters[i + 1024] = data0[i];
1240 }
1241}
1242
1243/*!
1244 * \fn int initW25Q64JV( void)
1245 *
1246 * \brief Initiailizes the W25Q64JW.
1247 *
1248 * This will read the WHO_AM_I value to determine if a W25Q64JW is connected.\n
1249 * It will set SENSORTYPEW25Q64JW if a W25Q64JW is available.
1250 *
1251 *
1252 * \return TRUE if W25Q64JW connected, FALSE if W25Q64JW not connected.
1253 */
1254int initW25Q64JV(void) {
1255 uint8_t flashIDIn[5];
1256
1257 readNVFlashxID(flashIDIn);
1258 if ((flashIDIn[0] == W25Q64JW_WHO_AM_I_REG0) && (flashIDIn[1] == W25Q64JW_WHO_AM_I_REG1) && (flashIDIn[2] == W25Q64JW_WHO_AM_I_REG2)) {
1260 myPrintkI("W25Q64JV 64MB Flash Memory - Present\r\n");
1261 return TRUE;
1262 } else {
1263 myPrintkW("W25Q64JV 64MB Flash Memory - Not Present\r\n");
1264 return FALSE;
1265 }
1266}
W25Q64JV driver header file.
struct spi_config spi_cfg1
SPI configuration.
Definition globals.c:91
struct spi_buf tx_bufs[]
SPI TX buffer structure.
Definition globals.c:111
const struct spi_buf_set rx
SPI RX buffer structure.
Definition globals.c:127
uint8_t buffer_rx[SPI_BUF_SIZE+8]
SPI receive buffer0.
Definition globals.c:43
const struct device *const spi_dev
Definition globals.c:79
flashParametersStruct flashParameters
working valuses of flash parameters in RAM
Definition globals.c:35
uint32_t sensorTypePresentAll
Definition globals.c:30
int ret
Definition globals.c:41
uint8_t buffer_tx[SPI_BUF_SIZE+8]
SPI transmit buffer.
Definition globals.c:42
uint8_t tmpData[256]
Definition globals.c:36
struct spi_buf rx_bufs[]
SPI RX buffer structure.
Definition globals.c:118
const struct spi_buf_set tx
SPI TX buffer structure.
Definition globals.c:125
#define W25Q64JW_WHO_AM_I_REG0
#define W25Q64JW_WHO_AM_I_REG2
#define W25Q64JW_WHO_AM_I_REG1
#define FLASHPARAMETERSTART
Definition jfet_common.h:77
int myPrintkI(char *restrict fmt,...)
prints an information message to the UART
Definition main.c:2386
#define FLASHPARAMETERKEY
Definition jfet_common.h:79
#define DEFAULT_DUTYCYCLE
Definition jfet_common.h:44
#define TRUE
Definition jfet_common.h:42
#define FALSE
Definition jfet_common.h:41
@ SENSORTYPEW25Q64JV
Definition jfet_common.h:70
int myPrintkW(char *restrict fmt,...)
prints a warning message to the UART
Definition main.c:2353
Flash parameters structure.
Definition jfet_common.h:90
void displayFlashxParameters(void)
displays the NV flash parameters.
Definition w25q64jv.c:1021
void flashxClearParameters(void)
clears/initializes the flash parameters in RAM
Definition w25q64jv.c:939
int initW25Q64JV(void)
Initiailizes the W25Q64JW.
Definition w25q64jv.c:1254
void flashxWritePage(uint32_t page, uint32_t address, uint8_t *data, uint16_t count)
writes data to address in serial flash over the SPI bus.
Definition w25q64jv.c:542
void readNVFlashxID(uint8_t *flashId)
reads the NV Flash ID over the SPI bus.
Definition w25q64jv.c:34
void flashxEraseBlock1(uint32_t address)
erases a 2K block page over the SPI bus.
Definition w25q64jv.c:230
void flashxReadPage(uint32_t address, uint8_t *flashRead)
reads a 256 byte page over the SPI bus.
Definition w25q64jv.c:427
void flashxErasePage(uint32_t address)
erases a 256 byte page over the SPI bus.
Definition w25q64jv.c:180
void flashxEraseSector(uint32_t address)
erases a 256K byte sector over the SPI bus.
Definition w25q64jv.c:330
void flashxFillPage(uint32_t address, uint16_t data)
fills a 256 byte page with data over the SPI bus.
Definition w25q64jv.c:752
void setFlashxParameters(uint8_t display)
loads the NV Flash parameters from teh RAM flash parameters
Definition w25q64jv.c:1122
void flashxErase(void)
erases the entire 8 Mb over the SPI bus.
Definition w25q64jv.c:379
void readNVFlashxStatus(uint8_t *flashStatus)
reads the 3 NV Flash status registers over the SPI bus.
Definition w25q64jv.c:61
void writeNVFlashxStatus(void)
writes NV Flash status register2 over the SPI bus.
Definition w25q64jv.c:127
void getFlashparameters(void)
Read the stored flash parameter block from serial flash.
Definition w25q64jv.c:1183
void flashxEraseBlock2(uint32_t address)
Definition w25q64jv.c:280