Sample SPI drivers for a number of the Adesto Technologies flash devices.
standardflash.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2018 Adesto Technologies Corporation, Inc
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted (subject to the limitations in the disclaimer below) provided
8  * that the following conditions are met:
9  *
10  * o Redistributions of source code must retain the above copyright notice, this list
11  * of conditions and the following disclaimer.
12  *
13  * o Redistributions in binary form must reproduce the above copyright notice, this
14  * list of conditions and the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  *
17  * o Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
42 #include <standardflash.h>
43 
44 #if (PARTNO == AT25SF641) || \
45  (PARTNO == AT25SF321) || \
46  (PARTNO == AT25SF161) || \
47  (PARTNO == AT25SF081) || \
48  (PARTNO == AT25SF041) || \
49  (PARTNO == AT25SL128A) || \
50  (PARTNO == AT25SL641) || \
51  (PARTNO == AT25SL321) || \
52  (PARTNO == AT25DL081) || \
53  (PARTNO == AT25DL161) || \
54  (PARTNO == AT25DF081A) || \
55  (PARTNO == AT25DF321A) || \
56  (PARTNO == AT25DF641A) || \
57  (PARTNO == AT25QL128A) || \
58  (PARTNO == AT25QL641) || \
59  (PARTNO == AT25QL321) || \
60  (PARTNO == AT25QF641) || \
61  (ALL == 1)
62 
68 static uint8_t MCU_SPI_MODE = 0;
69 
70 static bool DISPLAY_OUTPUT = 0;
71 
72 static void debugOn() {DISPLAY_OUTPUT = 1;};
73 static void debugOff() {DISPLAY_OUTPUT = 0;};
74 
76 
78 {
79  uint8_t SRArray[2] = {0, 0};
80  do
81  {
82 #if (PARTNO == AT25DL081) || \
83  (PARTNO == AT25DL161) || \
84  (PARTNO == AT25DF081A) || \
85  (PARTNO == AT25DF321A) || \
86  (PARTNO == AT25DF641A)
87  standardflashReadSR(SRArray);
88  SPI_Delay(10);
89  }
90  while(SRArray[1] & (1<<0));
91 #else
92  SRArray[0] = standardflashReadSRB1();
93  SPI_Delay(10);
94  }
95  while(SRArray[0] & (1<<0));
96 #endif
97 }
98 
100 {
101  uint8_t SRArray[2] = {0, 0};
102  // Read both status register bytes.
103 #if (PARTNO == AT25DL081) || \
104  (PARTNO == AT25DL161) || \
105  (PARTNO == AT25DF081A) || \
106  (PARTNO == AT25DF321A) || \
107  (PARTNO == AT25DF641A)
108  standardflashReadSR(SRArray);
109 #else
110  SRArray[0] = standardflashReadSRB1();
111  SRArray[1] = standardflashReadSRB2();
112 #endif
113  // Store a 1 in the QE bit.
114  SRArray[1] |= (1 << 1);
115  // Altering the device data, so send over a write enable first.
117  // Store the command and new SRB values. NOTE this needs to be done after
118  // the write enable since the internal buffer used is a shared resource.
119 #if (PARTNO == AT25DL081) || \
120  (PARTNO == AT25DL161) || \
121  (PARTNO == AT25DF081A) || \
122  (PARTNO == AT25DF321A) || \
123  (PARTNO == AT25DF641A)
124  standardflashWriteSRB1(SRArray[0]);
125  standardflashWriteSRB2(SRArray[1]);
126 #else
127  standardflashWriteSR(SRArray, 2);
128 #endif
129  if(DISPLAY_OUTPUT)
130  {
132  }
133 }
134 
136 {
137  uint8_t SRArray[2] = {0, 0};
138  // Read both status register bytes.
139 #if (PARTNO == AT25DL081) || \
140  (PARTNO == AT25DL161) || \
141  (PARTNO == AT25DF081A) || \
142  (PARTNO == AT25DF321A) || \
143  (PARTNO == AT25DF641A)
144  standardflashReadSR(SRArray);
145 #else
146  SRArray[0] = standardflashReadSRB1();
147  SRArray[1] = standardflashReadSRB2();
148 #endif
149  // Store a 0 in the QE bit.
150  SRArray[1] &= ~(1 << 1);
151  // Altering the device data, so send over a write enable first.
153  // Store the command and new SRB values. NOTE this needs to be done after
154  // the write enable since the internal buffer used is a shared resource.
155 #if (PARTNO == AT25DL081) || \
156  (PARTNO == AT25DL161) || \
157  (PARTNO == AT25DF081A) || \
158  (PARTNO == AT25DF321A) || \
159  (PARTNO == AT25DF641A)
160  standardflashWriteSRB1(SRArray[0]);
161  standardflashWriteSRB2(SRArray[1]);
162 #else
163  standardflashWriteSR(SRArray, 2);
164 #endif
165  if(DISPLAY_OUTPUT)
166  {
168  }
169 }
170 
172 {
174  if(MCU_SPI_MODE == SPI)
176  else
178  if(DISPLAY_OUTPUT)
179  {
181  }
182 }
183 
185 {
187  if(MCU_SPI_MODE == SPI)
189  else
191  if(DISPLAY_OUTPUT)
192  {
194  }
195 }
196 
197 void standardflashReadArrayLowFreq(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
198 {
200  SPI_Exchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 0);
201  if(DISPLAY_OUTPUT)
202  {
203  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
204  }
205 }
206 
207 void standardflashReadArrayHighFreq(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
208 {
210  if(MCU_SPI_MODE == SPI)
211  SPI_Exchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 1);
212  else
213  SPI_QuadExchange(0, txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 2);
214  if(DISPLAY_OUTPUT)
215  {
216  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
217  }
218 }
219 
220 void standardflashBytePageProgram(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
221 {
223  // Offset the data bytes by 4; opcode+address takes up the first 4 bytes of a transmission.
224  uint32_t totalBytes = txNumBytes + 4;
225 
226  for(uint32_t j = 0; j < txNumBytes; j++)
227  {
228  txStandardflashInternalBuffer[j+4] = txBuffer[j];
229  }
230 
231  if(MCU_SPI_MODE == SPI)
232  SPI_Exchange(txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
233  else
234  SPI_QuadExchange(0, txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
235  if(DISPLAY_OUTPUT)
236  {
237  printSPIExchange(txStandardflashInternalBuffer, totalBytes, NULL, 0);
238  }
239 }
240 
241 void standardflashBlockErase4K(uint32_t address)
242 {
244  if(MCU_SPI_MODE == SPI)
246  else
248  if(DISPLAY_OUTPUT)
249  {
251  }
252 }
253 
254 void standardflashBlockErase32K(uint32_t address)
255 {
257  if(MCU_SPI_MODE == SPI)
259  else
261  if(DISPLAY_OUTPUT)
262  {
264  }
265 }
266 
267 void standardflashBlockErase64K(uint32_t address)
268 {
270  if(MCU_SPI_MODE == SPI)
272  else
274  if(DISPLAY_OUTPUT)
275  {
277  }
278 }
279 
281 {
283  if(MCU_SPI_MODE == SPI)
285  else
287  if(DISPLAY_OUTPUT)
288  {
290  }
291 }
292 
294 {
296  if(MCU_SPI_MODE == SPI)
298  else
300  if(DISPLAY_OUTPUT)
301  {
303  }
304 }
305 
307 {
309  if(MCU_SPI_MODE == SPI)
311  else
313  if(DISPLAY_OUTPUT)
314  {
316  }
317 }
318 
320 {
322  if(MCU_SPI_MODE == SPI)
324  else
326  if(DISPLAY_OUTPUT)
327  {
329  }
330 }
331 
332 void standardflashReadID(uint8_t *rxBuffer)
333 {
335  if(MCU_SPI_MODE == SPI)
336  SPI_Exchange(txStandardflashInternalBuffer, 1, rxBuffer, 2, 3);
337  else
338  SPI_QuadExchange(0, txStandardflashInternalBuffer, 1, rxBuffer, 2, 3);
339  if(DISPLAY_OUTPUT)
340  {
342  }
343 }
344 
345 void standardflashReadMID(uint8_t *rxBuffer)
346 {
348  if(MCU_SPI_MODE == SPI)
349  SPI_Exchange(txStandardflashInternalBuffer, 1, rxBuffer, 3, 0);
350  else
351  SPI_QuadExchange(0, txStandardflashInternalBuffer, 1, rxBuffer, 3, 0);
352  if(DISPLAY_OUTPUT)
353  {
355  }
356 }
357 #endif
358 #if (PARTNO == AT25SF641) || \
359  (PARTNO == AT25SF321) || \
360  (PARTNO == AT25SF161) || \
361  (PARTNO == AT25SF081) || \
362  (PARTNO == AT25SF041) || \
363  (PARTNO == AT25SL128A) || \
364  (PARTNO == AT25SL641) || \
365  (PARTNO == AT25SL321) || \
366  (PARTNO == AT25QL128A) || \
367  (PARTNO == AT25QL641) || \
368  (PARTNO == AT25QL321) || \
369  (PARTNO == AT25QF641) || \
370  (ALL == 1)
371 
372 void standardflashWriteSR(uint8_t *txBuffer, uint8_t txNumBytes)
373 {
375  txStandardflashInternalBuffer[1] = txBuffer[0];
376  if(txNumBytes > 1)
377  {
378  txStandardflashInternalBuffer[2] = txBuffer[1];
379  }
380  if(MCU_SPI_MODE == SPI)
381  SPI_Exchange(txStandardflashInternalBuffer, txNumBytes+1, NULL, 0, 0);
382  else
383  SPI_QuadExchange(0, txStandardflashInternalBuffer, txNumBytes+1, NULL, 0, 0);
384  if(DISPLAY_OUTPUT)
385  {
386  printSPIExchange(txStandardflashInternalBuffer, txNumBytes+1, NULL, 0);
387  }
388 }
389 #endif
390 
391 #if (PARTNO == AT25SF641) || \
392  (PARTNO == AT25SL128A) || \
393  (PARTNO == AT25SL641) || \
394  (PARTNO == AT25SL321) || \
395  (PARTNO == AT25QL128A) || \
396  (PARTNO == AT25QL641) || \
397  (PARTNO == AT25QL321) || \
398  (PARTNO == AT25QF641) || \
399  (PARTNO == AT25DL081) || \
400  (PARTNO == AT25DL161) || \
401  (PARTNO == AT25DF081A) || \
402  (PARTNO == AT25DF321A) || \
403  (PARTNO == AT25DF641A) || \
404  (ALL == 1)
405 
406 void standardflashWriteSRB1(uint8_t regVal)
407 {
409  txStandardflashInternalBuffer[1] = regVal;
410  if(MCU_SPI_MODE == SPI)
412  else
414  if(DISPLAY_OUTPUT)
415  {
417  }
418 }
419 
420 void standardflashWriteSRB2(uint8_t regVal)
421 {
423  txStandardflashInternalBuffer[1] = regVal;
424  if(MCU_SPI_MODE == SPI)
426  else
428  if(DISPLAY_OUTPUT)
429  {
431  }
432 }
433 #endif
434 
435 #if (PARTNO == AT25SF641) || \
436  (PARTNO == AT25SF321) || \
437  (PARTNO == AT25SF161) || \
438  (PARTNO == AT25SF081) || \
439  (PARTNO == AT25SF041) || \
440  (PARTNO == AT25SL128A) || \
441  (PARTNO == AT25SL641) || \
442  (PARTNO == AT25SL321) || \
443  (PARTNO == AT25QL128A) || \
444  (PARTNO == AT25QL641) || \
445  (PARTNO == AT25QL321) || \
446  (PARTNO == AT25QF641) || \
447  (ALL == 1)
449 {
451  if(MCU_SPI_MODE == SPI)
453  else
455  if(DISPLAY_OUTPUT)
456  {
458  }
459 }
460 
462 {
463  uint8_t regVal = 0;
465  if(MCU_SPI_MODE == SPI)
466  SPI_Exchange(txStandardflashInternalBuffer, 1, &regVal, 1, 0);
467  else
468  SPI_QuadExchange(0, txStandardflashInternalBuffer, 1, &regVal, 1, 0);
469  if(DISPLAY_OUTPUT)
470  {
472  }
473  return regVal;
474 }
475 
477 {
478  uint8_t regVal = 0;
480  if(MCU_SPI_MODE == SPI)
481  SPI_Exchange(txStandardflashInternalBuffer, 1, &regVal, 1, 0);
482  else
483  SPI_QuadExchange(0, txStandardflashInternalBuffer, 1, &regVal, 1, 0);
484  if(DISPLAY_OUTPUT)
485  {
487  }
488  return regVal;
489 }
490 
491 void standardflashDualOutputRead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
492 {
494  SPI_DualExchange(4, txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 1);
495  if(DISPLAY_OUTPUT)
496  {
497  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
498  }
499 }
500 
501 void standardflashDualIORead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes, uint8_t readMode, uint8_t modeByteValue)
502 {
503  // Set to an unused number to ensure a transmission does not take place with invalid input.
504  uint8_t transmissionSelect = 0x02;
505  switch(readMode)
506  {
507  // Single Dual Read
508  case 0:
511  transmissionSelect = 0x00;
512  break;
513  // Start Continuous Dual Read
514  case 1:
516  txStandardflashInternalBuffer[4] = modeByteValue;
517  transmissionSelect = 0x00;
518  break;
519  // Continue Continuous Dual Read
520  case 2:
521  txStandardflashInternalBuffer[0] = (uint8_t) (address >> 16);
522  txStandardflashInternalBuffer[1] = (uint8_t) (address >> 8);
523  txStandardflashInternalBuffer[2] = (uint8_t) address;
524  txStandardflashInternalBuffer[3] = modeByteValue;
525  transmissionSelect = 0x01;
526  break;
527  // End Continuous Dual Read
528  case 3:
529  txStandardflashInternalBuffer[0] = (uint8_t) (address >> 16);
530  txStandardflashInternalBuffer[1] = (uint8_t) (address >> 8);
531  txStandardflashInternalBuffer[2] = (uint8_t) address;
533  transmissionSelect = 0x01;
534  break;
535  default:
536  printf("Error with standardflashDualIORead.\n");
537  printf("\t- Invalid dualReadMode value. Expected 0-3, received %d.\n", readMode);
538  printf("\tSee documentation for operating modes.\n");
539  break;
540  }
541 
542  if(transmissionSelect == 0x00)
543  {
544  SPI_DualExchange(1, txStandardflashInternalBuffer, 5, rxBuffer, rxNumBytes, 0);
545  if(DISPLAY_OUTPUT)
546  {
547  printSPIExchange(txStandardflashInternalBuffer, 5, rxBuffer, rxNumBytes);
548  }
549  }
550  else if(transmissionSelect == 0x01)
551  {
552  SPI_DualExchange(0, txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 0);
553  if(DISPLAY_OUTPUT)
554  {
555  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
556  }
557  }
558 }
559 
560 void standardflashQuadOutputRead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
561 {
563  SPI_QuadExchange(4, txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 1);
564  if(DISPLAY_OUTPUT)
565  {
566  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
567  }
568 }
569 
570 void standardflashQuadIORead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes, uint8_t readMode, uint8_t modeByteValue)
571 {
572  // Set to an unused number to ensure a transmission does not take place with invalid input.
573  uint8_t transmissionSelect = 0x02;
574  switch(readMode)
575  {
576  // Single Quad Read
577  case 0:
580  transmissionSelect = 0x00;
581  break;
582  // Start Continuous Quad Read
583  case 1:
585  txStandardflashInternalBuffer[4] = modeByteValue;
586  transmissionSelect = 0x00;
587  break;
588  // Continue Continuous Quad Read
589  case 2:
590  txStandardflashInternalBuffer[0] = (uint8_t) (address >> 16);
591  txStandardflashInternalBuffer[1] = (uint8_t) (address >> 8);
592  txStandardflashInternalBuffer[2] = (uint8_t) address;
593  txStandardflashInternalBuffer[3] = modeByteValue;
594  transmissionSelect = 0x01;
595  break;
596  // End Continuous Quad Read
597  case 3:
598  txStandardflashInternalBuffer[0] = (uint8_t) (address >> 16);
599  txStandardflashInternalBuffer[1] = (uint8_t) (address >> 8);
600  txStandardflashInternalBuffer[2] = (uint8_t) address;
601  txStandardflashInternalBuffer[3] = modeByteValue;
602  transmissionSelect = 0x01;
603  break;
604  default:
605  printf("Error with standardflashQuadIORead.\n");
606  printf("\t- Invalid quadReadMode value. Expected 0-3, received %d.\n", readMode);
607  printf("\tSee documentation for operating modes.\n");
608  break;
609  }
610 
611  if(transmissionSelect == 0x00)
612  {
613  if(MCU_SPI_MODE == SPI)
614  SPI_QuadExchange(1, txStandardflashInternalBuffer, 5, rxBuffer, rxNumBytes, 2);
615  else
616  SPI_QuadExchange(0, txStandardflashInternalBuffer, 5, rxBuffer, rxNumBytes, 1);
617  if(DISPLAY_OUTPUT)
618  {
619  printSPIExchange(txStandardflashInternalBuffer, 5, rxBuffer, rxNumBytes);
620  }
621  }
622  else if(transmissionSelect == 0x01)
623  {
624  if(MCU_SPI_MODE == SPI)
625  SPI_QuadExchange(0, txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 2);
626  else
627  SPI_QuadExchange(0, txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 1);
628  if(DISPLAY_OUTPUT)
629  {
630  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
631  }
632  }
633 }
634 
636 {
640  if(DISPLAY_OUTPUT)
641  {
643  }
644 }
645 
647 {
649  if(MCU_SPI_MODE == SPI)
651  else
653  if(DISPLAY_OUTPUT)
654  {
656  }
657 }
658 
660 {
663  if(DISPLAY_OUTPUT)
664  {
666  }
667 }
668 
669 void standardflashProgramSecurityRegisters(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
670 {
672  // Offset the data bytes by 4; opcode+address takes up the first 4 bytes of a transmission.
673  uint32_t totalBytes = txNumBytes + 4;
674 
675  for(uint32_t j = 0; j < txNumBytes; j++)
676  {
677  txStandardflashInternalBuffer[j+4] = txBuffer[j];
678  }
679 
680  SPI_Exchange(txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
681  if(DISPLAY_OUTPUT)
682  {
683  printSPIExchange(txStandardflashInternalBuffer, totalBytes, NULL, 0);
684  }
685 }
686 
687 void standardflashReadSecurityRegisters(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
688 {
690  SPI_Exchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes, 1);
691  if(DISPLAY_OUTPUT)
692  {
693  printSPIExchange(txStandardflashInternalBuffer, 4, rxBuffer, rxNumBytes);
694  }
695 }
696 
697 void standardflashResumeFromDPDReadID(uint8_t *rxBuffer)
698 {
700  if(MCU_SPI_MODE == SPI)
701  SPI_Exchange(txStandardflashInternalBuffer, 1, rxBuffer, 1, 3);
702  else
703  SPI_QuadExchange(0, txStandardflashInternalBuffer, 1, rxBuffer, 1, 3);
704  if(DISPLAY_OUTPUT)
705  {
707  }
708 }
709 
710 void standardflashQuadPageProgram(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes, uint8_t mode)
711 {
713  // Offset the data bytes by 4; opcode+address takes up the first 4 bytes of a transmission.
714  uint32_t totalBytes = txNumBytes + 4;
715 
716  for(uint32_t j = 0; j < txNumBytes; j++)
717  {
718  txStandardflashInternalBuffer[j+4] = txBuffer[j];
719  }
720 
721  if(MCU_SPI_MODE == SPI)
722  SPI_QuadExchange(1, txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
723  else
724  SPI_QuadExchange(0, txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
725  if(DISPLAY_OUTPUT)
726  {
727  printSPIExchange(txStandardflashInternalBuffer, totalBytes, NULL, 0);
728  }
729 }
730 #endif
731 
732 
733 #if (PARTNO == AT25SF641) || \
734  (PARTNO == AT25SF321) || \
735  (PARTNO == AT25SF161) || \
736  (PARTNO == AT25SL128A) || \
737  (PARTNO == AT25SL641) || \
738  (PARTNO == AT25SL321) || \
739  (PARTNO == AT25QL128A) || \
740  (PARTNO == AT25QL641) || \
741  (PARTNO == AT25QL321) || \
742  (PARTNO == AT25QF641) || \
743  (ALL == 1)
744 
746 {
748  if(MCU_SPI_MODE == SPI)
750  else
752  if(DISPLAY_OUTPUT)
753  {
755  }
756 }
757 
759 {
761  if(MCU_SPI_MODE == SPI)
763  else
765  if(DISPLAY_OUTPUT)
766  {
768  }
769 }
770 
771 #endif
772 
773 
774 #if (PARTNO == AT25SF641) || \
775  (PARTNO == AT25SL128A) || \
776  (PARTNO == AT25SL641) || \
777  (PARTNO == AT25SL321) || \
778  (PARTNO == AT25QL128A) || \
779  (PARTNO == AT25QL641) || \
780  (PARTNO == AT25QL321) || \
781  (PARTNO == AT25QF641) || \
782  (ALL == 1)
783 
785 {
788  if(DISPLAY_OUTPUT)
789  {
791  }
792  // Set the MCU_SPI_MODE byte to QPI.
793  MCU_SPI_MODE = QPI;
794 }
795 
797 {
800  if(DISPLAY_OUTPUT)
801  {
803  }
804  // Set the MCU_SPI_MODE byte to SPI.
805  MCU_SPI_MODE = SPI;
806 }
807 
809 {
811  if(MCU_SPI_MODE == SPI)
813  else
815  if(DISPLAY_OUTPUT)
816  {
818  }
819 }
820 
822 {
824  if(MCU_SPI_MODE == SPI)
826  else
828  if(DISPLAY_OUTPUT)
829  {
831  }
832 }
833 
835 {
837  if(MCU_SPI_MODE == SPI)
839  else
841  if(DISPLAY_OUTPUT)
842  {
844  }
845 }
846 
848 {
850  if(MCU_SPI_MODE == SPI)
852  else
854  if(DISPLAY_OUTPUT)
855  {
857  }
858 }
859 
860 #endif
861 
862 
863 #if (PARTNO == AT25DL081) || \
864  (PARTNO == AT25DL161) || \
865  (PARTNO == AT25DF081A) || \
866  (PARTNO == AT25DF321A) || \
867  (PARTNO == AT25DF641A) || \
868  (ALL == 1)
869 
870 void standardflashReadSR(uint8_t *rxBuffer)
871 {
873  if(MCU_SPI_MODE == SPI)
874  SPI_Exchange(txStandardflashInternalBuffer, 1, rxBuffer, 2, 0);
875  else
876  SPI_QuadExchange(0, txStandardflashInternalBuffer, 1, rxBuffer, 2, 0);
877  if(DISPLAY_OUTPUT)
878  {
880  }
881 }
882 
883 void standardflashDualInputBytePageProgram(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
884 {
886  // Offset the data bytes by 4; opcode+address takes up the first 4 bytes of a transmission.
887  uint32_t totalBytes = txNumBytes + 4;
888 
889  for(uint32_t j = 0; j < txNumBytes; j++)
890  {
891  txStandardflashInternalBuffer[j+4] = txBuffer[j];
892  }
893 
894  SPI_DualExchange(4, txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
895  if(DISPLAY_OUTPUT)
896  {
897  printSPIExchange(txStandardflashInternalBuffer, totalBytes, NULL, 0);
898  }
899 }
900 
902 {
905  if(DISPLAY_OUTPUT)
906  {
908  }
909 }
910 
912 {
915  if(DISPLAY_OUTPUT)
916  {
918  }
919 }
920 
921 void standardflashProtectSector(uint32_t address)
922 {
925  if(DISPLAY_OUTPUT)
926  {
928  }
929 }
930 void standardflashUnprotectSector(uint32_t address)
931 {
934  if(DISPLAY_OUTPUT)
935  {
937  }
938 }
939 
940 uint8_t standardflashReadSectorProtectionReg(uint32_t address)
941 {
942  uint8_t registerVal = 0;
944  SPI_Exchange(txStandardflashInternalBuffer, 4, &registerVal, 1, 0);
945  if(DISPLAY_OUTPUT)
946  {
947  printSPIExchange(txStandardflashInternalBuffer, 4, &registerVal, 1);
948  }
949  return registerVal;
950 }
951 
953 {
957  if(DISPLAY_OUTPUT)
958  {
960  }
961 }
962 
963 uint8_t standardflashReadLockdownReg(uint32_t address)
964 {
965  uint8_t registerVal = 0;
967  SPI_Exchange(txStandardflashInternalBuffer, 4, &registerVal, 1, 1);
968  if(DISPLAY_OUTPUT)
969  {
970  printSPIExchange(txStandardflashInternalBuffer, 4, &registerVal, 1);
971  }
972  return registerVal;
973 }
974 
975 void standardflashProgramOTPReg(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
976 {
978  // Offset the data bytes by 4; opcode+address takes up the first 4 bytes of a transmission.
979  uint32_t totalBytes = txNumBytes + 4;
980 
981  for(uint32_t j = 0; j < txNumBytes; j++)
982  {
983  txStandardflashInternalBuffer[j+4] = txBuffer[j];
984  }
985 
986  SPI_Exchange(txStandardflashInternalBuffer, totalBytes, NULL, 0, 0);
987  if(DISPLAY_OUTPUT)
988  {
989  printSPIExchange(txStandardflashInternalBuffer, totalBytes, NULL, 0);
990  }
991 }
992 #endif
void standardflashWriteDisable()
OPCODE: 0x04 Sends opcode to disable writing.
#define CMD_STANDARDFLASH_READ_ID
Definition: cmd_defs.h:351
void standardflashReadSR(uint8_t *rxBuffer)
OPCODE: 0x05 Reads the value in the device status register (byte 1 and 2).
#define CMD_STANDARDFLASH_ENABLE_QPI
Definition: cmd_defs.h:430
void standardflashResumeFromDPD()
OPCODE: 0xAB Wake the device from deep power down mode. See dataflashDPD().
void standardflashEnableQPI()
OPCODE: 0x38 Switches the device from SPI mode to QPI mode, and sets a variable to quad mode...
uint8_t standardflashReadLockdownReg(uint32_t address)
OPCODE: 0x35 Reads the sector lockdown register for address address.
void standardflashEraseProgramSuspend()
OPCODE: 0x75 Suspends an erase or program mid operation.
void SPI_QuadExchange(uint8_t standardSPINumBytes, uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
Sends and receives bytes based on the function parameters. MOSI can used for the opcode and address...
Definition: spi_driver.c:406
void printSPIExchange(uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes)
Prints the byte array in hexadecimal with a formatted output. Indicates what bytes were sent...
#define CMD_STANDARDFLASH_READ_SECURITY_REG_PAGE
Definition: cmd_defs.h:403
void standardflashWriteEnableVolatileSR()
OPCODE: 0x50 Sends opcode to enable writing for the volatile status register.
#define CMD_STANDARDFLASH_EXIT_SECURED_OTP
Definition: cmd_defs.h:435
#define CMD_STANDARDFLASH_WRITE_DISABLE
Definition: cmd_defs.h:340
void standardflashProgramOTPReg(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
OPCODE: 0x9B Programs &#39;txNumBytes&#39; bytes of data to the OTP register starting at the address indica...
#define CMD_STANDARDFLASH_DUAL_IO_READ
Definition: cmd_defs.h:396
void standardflashContinuousReadModeDualReset()
OPCODE: 0xFFFF Resets the dual continuous read mode that was entered with standardflashDualIORead()...
void standardflashResumeFromDPDReadID(uint8_t *rxBuffer)
OPCODE: 0xAB Wake the device from deep power down mode and read the 1 byte ID. See standardflashDPD...
#define CMD_STANDARDFLASH_READ_SECT_PROT_REG
Definition: cmd_defs.h:449
#define CMD_STANDARDFLASH_UNPROTECT_SECTOR
Definition: cmd_defs.h:448
#define QPI
Definition: spi_driver.h:111
#define CMD_STANDARDFLASH_WRITE_SRB2
Definition: cmd_defs.h:376
void standardflashDPD()
OPCODE: 0xB9 Put the device into deep power down mode. Call dataflashResumeFromDPD() to wake up...
void standardflashQuadIORead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes, uint8_t readMode, uint8_t modeByteValue)
OPCODE: 0xEB Reads &#39;txNumBytes&#39; bytes of data in Quad IO Mode starting at &#39;address&#39; using SI...
void standardflashDualIORead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes, uint8_t readMode, uint8_t modeByteValue)
OPCODE: 0xBB Reads &#39;txNumBytes&#39; bytes of data in Dual IO Mode starting at &#39;address&#39; using both SI a...
uint8_t standardflashReadSectorProtectionReg(uint32_t address)
OPCODE: 0x3C Reads the sector protection register for address address.
#define CMD_STANDARDFLASH_QUAD_PAGE_PROGRAM
Definition: cmd_defs.h:405
void standardflashEraseProgramResume()
OPCODE: 0x7A Resumes a previously suspended erase or program.
void standardflashReadArrayLowFreq(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
OPCODE: 0x03 Reads rxNumBytes starting from location &#39;address&#39; and stores the data in the byte arra...
#define CMD_STANDARDFLASH_READ_LOCKDOWN_REG
Definition: cmd_defs.h:451
#define CMD_STANDARDFLASH_READ_SR
Definition: cmd_defs.h:443
#define CMD_STANDARDFLASH_PROGRAM_ERASE_RESUME
Definition: cmd_defs.h:446
#define SPI
Definition: spi_driver.h:110
uint8_t standardflashReadSRB1()
OPCODE: 0x05 Reads the value in the device status register (byte 1).
#define CMD_STANDARDFLASH_ENTER_SECURED_OTP
Definition: cmd_defs.h:434
void standardflashSetQEBit()
: Sets the QE bit in status register byte 2. Does not modify any other bits. This is accomplished by ...
Definition: standardflash.c:99
#define CMD_STANDARDFLASH_READ_ARRAY_HF
Definition: cmd_defs.h:342
void standardflashProgramEraseResume()
OPCODE: 0xD0 Resumes a previously suspended erase or program.
#define CMD_STANDARDFLASH_BYTE_PAGE_PROGRAM
Definition: cmd_defs.h:343
void standardflashEnterSecureOTP()
OPCODE: 0xB1 Enter secured OTP region.
#define CMD_STANDARDFLASH_CHIP_ERASE1
Definition: cmd_defs.h:347
void standardflashChipErase2()
OPCODE: 0xC7 Erases the entire chip by setting all bits. standardflashChipErase1() is functionally ...
void standardflashBlockErase32K(uint32_t address)
OPCODE: 0x52 Erases a block of data (4 KBytes) starting from page address &#39;address.&#39;.
void standardflashDualInputBytePageProgram(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
OPCODE: 0xA2 Programs &#39;txNumBytes&#39; bytes of data starting at the address indicated by address using...
void standardflashEraseSecurityRegister(uint32_t address)
OPCODE: 0x44 Sends opcode to erase the security registers.
void standardflashQuadPageProgram(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes, uint8_t mode)
OPCODE: 0x33 Programs &#39;txNumBytes&#39; bytes of data starting at the address indicated by address using...
void standardflashBlockErase64K(uint32_t address)
OPCODE: 0xD8 Erases a block of data (4 KBytes) starting from page address &#39;address.&#39;.
#define CMD_STANDARDFLASH_DUAL_OUTPUT_READ
Definition: cmd_defs.h:395
void standardflashUnprotectSector(uint32_t address)
OPCODE: 0x39 Un-protects a sector containing the address address.
#define CMD_STANDARDFLASH_BLOCK_ERASE_32K
Definition: cmd_defs.h:345
#define CMD_STANDARDFLASH_READ_SRB1
Definition: cmd_defs.h:393
void standardflashContinuousReadModeQuadReset()
OPCODE: 0xFF Resets the quad continuous read mode that was entered with standardflashQuadIORead() w...
#define CMD_STANDARDFLASH_CHIP_ERASE2
Definition: cmd_defs.h:348
void standardflashFreezeLockdownState()
OPCODE: 0x34 Freezes the current state of the sector lockdown registers.
#define MAXIMUM_TX_BYTES
Definition: cmd_defs.h:46
void standardflashWriteSRB1(uint8_t regVal)
void standardflashReadMID(uint8_t *rxBuffer)
OPCODE: 0x9F Reads the manufacturer ID and stores the data in rxBuffer.
void standardflashReadID(uint8_t *rxBuffer)
OPCODE: 0x90 Legace Command: Reads the manufacturer ID and stores the data in rxBuffer.
void standardflashReadSecurityRegisters(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
OPCODE: 0x48 Reads the contents of the security registers and stores it in the rxBuffer.
void standardflashWriteSRB2(uint8_t regVal)
#define CMD_STANDARDFLASH_PROTECT_SECTOR
Definition: cmd_defs.h:447
#define CMD_STANDARDFLASH_DUAL_BYTE_PAGE_PROGRAM
Definition: cmd_defs.h:444
void standardflashExitSecuredOTP()
OPCODE: 0xC1 Exit secured OTP region.
void load4BytesToTxBuffer(uint8_t *txBuffer, uint8_t opcode, uint32_t address)
Loads 1 byte of opcode followed by 3 address bytes into the txBuffer. The data is stored at the first...
uint8_t txStandardflashInternalBuffer[MAXIMUM_TX_BYTES]
Definition: standardflash.c:73
#define CMD_STANDARDFLASH_PROGRAM_ERASE_SUSPEND
Definition: cmd_defs.h:445
void standardflashEnableReset()
OPCODE: 0x66 Enable software reset of the flash device.
#define CMD_STANDARDFLASH_DISABLE_QPI
Definition: cmd_defs.h:431
#define CMD_STANDARDFLASH_RESUME_FROM_DPD
Definition: cmd_defs.h:350
void standardflashClearQEBit()
: Clears the QE bit in status register byte 2. Does not modify any other bits. This is accomplished b...
void standardflashQuadOutputRead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
OPCODE: 0x6B Reads &#39;txNumBytes&#39; bytes of data in Quad Output Mode starting at &#39;address&#39; using SI...
void SPI_DualExchange(uint8_t standardSPINumBytes, uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
Sends and receives bytes based on the function parameters. MOSI is used for the opcode and address...
Definition: spi_driver.c:355
void standardflashChipErase1()
OPCODE: 0x60 Erases the entire chip by setting all bits. standardflashChipErase2() is functionally ...
void standardflashReadArrayHighFreq(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
OPCODE: 0x0B Reads rxNumBytes starting from location &#39;address&#39; and stores the data in the byte arra...
void standardflashWriteSR(uint8_t *txBuffer, uint8_t txNumBytes)
OPCODE: 0x01 Writes the value in data to the status register byte 1, or bytes 1 and 2...
#define CMD_STANDARDFLASH_WE_FOR_VOLATILE_SR
Definition: cmd_defs.h:391
void standardflashWaitOnReady()
: Wait while Read/Busy Status bit in SRB is 1 (device is busy). This is accomplished by reading the f...
Definition: standardflash.c:77
void standardflashWriteEnable()
OPCODE: 0x06 Sends opcode to enable writing.
#define CMD_STANDARDFLASH_WRITE_SR
Definition: cmd_defs.h:392
#define CMD_STANDARDFLASH_ENABLE_RESET
Definition: cmd_defs.h:432
void SPI_Delay(uint32_t delayTime)
Performs a delayTime number of NOPs.
Definition: spi_driver.c:140
void standardflashProgramSecurityRegisters(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
OPCODE: 0x42 Programs the security registers with the contents of txBuffer.
#define CMD_STANDARDFLASH_READ_SRB2
Definition: cmd_defs.h:394
#define CMD_STANDARDFLASH_ERASE_PROGRAM_RESUME
Definition: cmd_defs.h:419
#define CMD_STANDARDFLASH_PROGRAM_OTP_REG
Definition: cmd_defs.h:452
#define CMD_STANDARDFLASH_QUAD_IO_READ
Definition: cmd_defs.h:398
#define CMD_STANDARDFLASH_READ_ARRAY_LF
Definition: cmd_defs.h:341
#define CMD_STANDARDFLASH_CONT_READ_MODE_RST_DUAL
Definition: cmd_defs.h:399
#define CMD_STANDARDFLASH_WRITE_SRB1
Definition: cmd_defs.h:375
#define CMD_STANDARDFLASH_CONT_READ_MODE_RST_QUAD
Definition: cmd_defs.h:400
void standardflashProtectSector(uint32_t address)
OPCODE: 0x36 Protects a sector containing the address address.
#define CMD_STANDARDFLASH_FREEZE_LOCKDOWN_STATE
Definition: cmd_defs.h:450
#define CMD_STANDARDFLASH_BLOCK_ERASE_4K
Definition: cmd_defs.h:344
#define CMD_STANDARDFLASH_READ_MID
Definition: cmd_defs.h:352
void standardflashReset()
OPCODE: 0x99 Software reset the flash device.
#define CMD_STANDARDFLASH_RESET
Definition: cmd_defs.h:433
void standardflashProgramEraseSuspend()
OPCODE: 0xB0 Suspends an erase or program mid operation.
Declarations of Standardflash functions.
void standardflashDualOutputRead(uint32_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
OPCODE: 0x3B Reads &#39;txNumBytes&#39; bytes of data in Dual Output Mode starting at &#39;address&#39; using both ...
#define CMD_STANDARDFLASH_QUAD_OUTPUT_READ
Definition: cmd_defs.h:397
uint8_t standardflashReadSRB2()
OPCODE: 0x35 Reads the value in the device status register (byte 2).
void standardflashBlockErase4K(uint32_t address)
OPCODE: 0x20 Erases a block of data (4 KBytes) starting from page address &#39;address.&#39;.
#define CMD_STANDARDFLASH_ERASE_SECURTIY_REG_PAGE
Definition: cmd_defs.h:401
void standardflashBytePageProgram(uint32_t address, uint8_t *txBuffer, uint32_t txNumBytes)
OPCODE: 0x02 Programs &#39;txNumBytes&#39; bytes of data starting at the address indicated by address...
void standardflashDisableQPI()
OPCODE: 0xFF Switches the device from QPI mode to SPI mode, and sets a variable to spi mode...
#define CMD_STANDARDFLASH_BLOCK_ERASE_64K
Definition: cmd_defs.h:346
#define CMD_STANDARDFLASH_ERASE_PROGRAM_SUSPEND
Definition: cmd_defs.h:418
void SPI_Exchange(uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
Sends and receives bytes based on the function parameters. MISO and MOSI fill their standard SPI role...
Definition: spi_driver.c:325
#define CMD_STANDARDFLASH_PROGRAM_SECURITY_REG_PAGE
Definition: cmd_defs.h:402
#define CMD_STANDARDFLASH_WRITE_ENABLE
Standardflash Devices and Opcodes AT25SF041 AT25SF081 AT25SF161 AT25SF321 AT25SF641 AT25SL321 AT25SL6...
Definition: cmd_defs.h:339
#define CMD_STANDARDFLASH_DEEP_POWER_DOWN
Definition: cmd_defs.h:349