EZR32 Wonder Gecko Software Documentation  ezr32wg-doc-5.1.2
mx25flash_spi.c
1 /*
2  * COPYRIGHT (c) 2010-2015 MACRONIX INTERNATIONAL CO., LTD
3  * SPI Flash Low Level Driver (LLD) Sample Code
4  *
5  * SPI interface command set
6  *
7  * $Id: MX25_CMD.c,v 1.31 2015/03/24 01:06:33 mxclldb1 Exp $
8  */
9 
10 #include "mx25flash_spi.h"
11 #include "em_gpio.h"
12 #include "em_usart.h"
13 #include "em_cmu.h"
14 
15 /* Local functions */
16 
17 /* Basic functions */
18 void CS_High( void );
19 void CS_Low( void );
20 void InsertDummyCycle( uint8_t dummy_cycle );
21 void SendByte( uint8_t byte_value, uint8_t transfer_type );
22 uint8_t GetByte( uint8_t transfer_type );
23 
24 /* Utility functions */
25 void Wait_Flash_WarmUp( void );
26 void Initial_Spi( void );
27 bool WaitFlashReady( uint32_t ExpectTime );
28 bool WaitRYBYReady( uint32_t ExpectTime );
29 bool IsFlashBusy( void );
30 bool IsFlashQIO( void );
31 bool IsFlash4Byte( void );
32 void SendFlashAddr( uint32_t flash_address, uint8_t io_mode, bool addr_4byte_mode );
33 uint8_t GetDummyCycle( uint32_t default_cycle );
34 
35 
36 void MX25_init( void )
37 {
39 
41  CMU_ClockEnable( MX25_USART_CLK, true );
42 
43  init.msbf = true;
44  init.baudrate = 8000000;
45  USART_InitSync( MX25_USART, &init );
46 
47  /* IO config */
48  GPIO_PinModeSet( MX25_PORT_MOSI, MX25_PIN_MOSI, gpioModePushPull, 1 );
49  GPIO_PinModeSet( MX25_PORT_MISO, MX25_PIN_MISO, gpioModeInput, 0 );
50  GPIO_PinModeSet( MX25_PORT_SCLK, MX25_PIN_SCLK, gpioModePushPull, 1 );
51  GPIO_PinModeSet( MX25_PORT_CS, MX25_PIN_CS, gpioModePushPull, 1 );
52 
53  MX25_USART->ROUTELOC0 = ( USART_ROUTELOC0_RXLOC_LOC11 | USART_ROUTELOC0_TXLOC_LOC11 | USART_ROUTELOC0_CLKLOC_LOC11 );
54  MX25_USART->ROUTEPEN = ( USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_CLKPEN );
55 
56  /* Wait for flash warm-up */
57  Initial_Spi();
58 }
59 
60 
61 /*
62  --Common functions
63  */
64 
65 /*
66  * Function: Wait_Flash_WarmUp
67  * Arguments: None.
68  * Description: Wait some time until flash read / write enable.
69  * Return Message: None.
70  */
71 void Wait_Flash_WarmUp()
72 {
73  volatile int time_cnt = FlashFullAccessTime;
74  while( time_cnt > 0 )
75  {
76  time_cnt--;
77  }
78 }
79 
80 /*
81  * Function: Initial_Spi
82  * Arguments: None
83  * Description: Initial spi flash state and wait flash warm-up
84  * (enable read/write).
85  * Return Message: None
86  */
87 void Initial_Spi()
88 {
89  // Wait flash warm-up
90  Wait_Flash_WarmUp();
91 }
92 
93 /*
94  * Function: CS_Low, CS_High
95  * Arguments: None.
96  * Description: Chip select go low / high.
97  * Return Message: None.
98  */
99 void CS_Low()
100 {
101  GPIO_PinOutClear( MX25_PORT_CS, MX25_PIN_CS );
102 }
103 
104 void CS_High()
105 {
106  GPIO_PinOutSet( MX25_PORT_CS, MX25_PIN_CS );
107 }
108 
109 /*
110  * Function: InsertDummyCycle
111  * Arguments: dummy_cycle, number of dummy clock cycle
112  * Description: Insert dummy cycle of SCLK
113  * Return Message: None.
114  */
115 void InsertDummyCycle( uint8_t dummy_cycle )
116 {
117  int i;
118 
119  for( i = 0; i < dummy_cycle/8; i++ )
120  {
121  USART_SpiTransfer( MX25_USART, 0xff );
122  }
123 }
124 
125 /*
126  * Function: SendByte
127  * Arguments: byte_value, data transfer to flash
128  * transfer_type, select different type of I/O mode.
129  * Seven mode:
130  * SIO, single IO
131  * DIO, dual IO
132  * QIO, quad IO
133  * PIO, parallel
134  * DTSIO, double transfer rate SIO
135  * DTDIO, double transfer rate DIO
136  * DTQIO, double transfer rate QIO
137  * Description: Send one byte data to flash
138  * Return Message: None.
139  */
140 void SendByte( uint8_t byte_value, uint8_t transfer_type )
141 {
142  switch( transfer_type )
143  {
144 #ifdef SIO
145  case SIO: // Single I/O
146  USART_SpiTransfer( MX25_USART, byte_value );
147  break;
148 #endif
149 #ifdef DIO
150  case DIO: // Dual I/O
151  //--- IO mode not supported! ---//
152  break;
153 #endif
154 #ifdef QIO
155  case QIO: // Quad I/O
156  //--- IO mode not supported! ---//
157  break;
158 #endif
159 #ifdef PIO
160  case PIO: // Parallel I/O
161  //--- IO mode not supported! ---//
162  break;
163 #endif
164 #ifdef DTSIO
165  case DTSIO: // Double transfer rate Single I/O
166  //--- IO mode not supported! ---//
167  break;
168 #endif
169 #ifdef DTDIO
170  case DTDIO: // Double transfer rate Dual I/O
171  //--- IO mode not supported! ---//
172  break;
173 #endif
174 #ifdef DTQIO
175  case DTQIO: // Double transfer rate Quad I/O
176  //--- IO mode not supported! ---//
177  break;
178 #endif
179  default:
180  break;
181  }
182 }
183 
184 
185 /*
186  * Function: GetByte
187  * Arguments: byte_value, data receive from flash
188  * transfer_type, select different type of I/O mode.
189  * Seven mode:
190  * SIO, single IO
191  * DIO, dual IO
192  * QIO, quad IO
193  * PIO, parallel IO
194  * DTSIO, double transfer rate SIO
195  * DTDIO, double transfer rate DIO
196  * DTQIO, double transfer rate QIO
197  * Description: Get one byte data to flash
198  * Return Message: 8 bit data
199  */
200 uint8_t GetByte( uint8_t transfer_type )
201 {
202  uint8_t data_buf = 0;
203 
204  switch( transfer_type )
205  {
206 #ifdef SIO
207  case SIO: // Single I/O
208  //--- insert your code here for single IO receive. ---//
209  data_buf = USART_SpiTransfer( MX25_USART, 0xff );
210  break;
211 #endif
212 #ifdef DIO
213  case DIO: // Dual I/O
214  //--- IO mode not supported! ---//
215  break;
216 #endif
217 #ifdef QIO
218  case QIO: // Quad I/O
219  //--- IO mode not supported! ---//
220  break;
221 #endif
222 #ifdef PIO
223  case PIO: // Parallel I/O
224  //--- IO mode not supported! ---//
225  break;
226 #endif
227 #ifdef DTSIO
228  case DTSIO: // Double transfer rate Single I/O
229  //--- IO mode not supported! ---//
230  break;
231 #endif
232 #ifdef DTDIO
233  case DTDIO: // Double transfer rate Dual I/O
234  //--- IO mode not supported! ---//
235  break;
236 #endif
237 #ifdef DTQIO
238  case DTQIO: // Double transfer rate Qual I/O
239  //--- IO mode not supported! ---//
240 #endif
241  default:
242  break;
243  }
244 
245  return data_buf;
246 }
247 
248 /*
249  * Function: WaitFlashReady
250  * Arguments: ExpectTime, expected time-out value of flash operations.
251  * No use at non-synchronous IO mode.
252  * Description: Synchronous IO:
253  * If flash is ready return TRUE.
254  * If flash is time-out return FALSE.
255  * Non-synchronous IO:
256  * Always return TRUE
257  * Return Message: TRUE, FALSE
258  */
259 bool WaitFlashReady( uint32_t ExpectTime )
260 {
261 #ifndef NON_SYNCHRONOUS_IO
262  volatile uint32_t temp = 0;
263  while( IsFlashBusy() )
264  {
265  if( temp > ExpectTime )
266  {
267  return FALSE;
268  }
269  temp = temp + 1;
270  }
271  return TRUE;
272 #else
273  return TRUE;
274 #endif
275 }
276 
277 /*
278  * Function: WaitRYBYReady
279  * Arguments: ExpectTime, expected time-out value of flash operations.
280  * No use at non-synchronous IO mode.
281  * Description: Synchronous IO:
282  * If flash is ready return TRUE.
283  * If flash is time-out return FALSE.
284  * Non-synchronous IO:
285  * Always return TRUE
286  * Return Message: TRUE, FALSE
287  */
288 bool WaitRYBYReady( uint32_t ExpectTime )
289 {
290 #ifndef NON_SYNCHRONOUS_IO
291  uint32_t temp = 0;
292 #ifdef GPIO_SPI
293  while( SO == 0 )
294 #else
295  while( GPIO_PinInGet(MX25_PORT_MISO, MX25_PIN_MISO) == 0 );
296 #endif
297  {
298  if( temp > ExpectTime )
299  {
300  return FALSE;
301  }
302  temp = temp + 1;
303  }
304  return TRUE;
305 
306 #else
307  return TRUE;
308 #endif
309 }
310 
311 /*
312  * Function: IsFlashBusy
313  * Arguments: None.
314  * Description: Check status register WIP bit.
315  * If WIP bit = 1: return TRUE ( Busy )
316  * = 0: return FALSE ( Ready ).
317  * Return Message: TRUE, FALSE
318  */
319 bool IsFlashBusy( void )
320 {
321  uint8_t gDataBuffer;
322 
323  MX25_RDSR( &gDataBuffer );
324  if( (gDataBuffer & FLASH_WIP_MASK) == FLASH_WIP_MASK )
325  return TRUE;
326  else
327  return FALSE;
328 }
329 
330 /*
331  * Function: IsFlashQIO
332  * Arguments: None.
333  * Description: If flash QE bit = 1: return TRUE
334  * = 0: return FALSE.
335  * Return Message: TRUE, FALSE
336  */
337 bool IsFlashQIO( void )
338 {
339 #ifdef FLASH_NO_QE_BIT
340  return TRUE;
341 #else
342  uint8_t gDataBuffer;
343  MX25_RDSR( &gDataBuffer );
344  if( (gDataBuffer & FLASH_QE_MASK) == FLASH_QE_MASK )
345  return TRUE;
346  else
347  return FALSE;
348 #endif
349 }
350 /*
351  * Function: IsFlash4Byte
352  * Arguments: None
353  * Description: Check flash address is 3-byte or 4-byte.
354  * If flash 4BYTE bit = 1: return TRUE
355  * = 0: return FALSE.
356  * Return Message: TRUE, FALSE
357  */
358 bool IsFlash4Byte( void )
359 {
360 #ifdef FLASH_CMD_RDSCUR
361  #ifdef FLASH_4BYTE_ONLY
362  return TRUE;
363  #elif FLASH_3BYTE_ONLY
364  return FALSE;
365  #else
366  uint8_t gDataBuffer;
367  MX25_RDSCUR( &gDataBuffer );
368  if( (gDataBuffer & FLASH_4BYTE_MASK) == FLASH_4BYTE_MASK )
369  return TRUE;
370  else
371  return FALSE;
372  #endif
373 #else
374  return FALSE;
375 #endif
376 }
377 /*
378  * Function: SendFlashAddr
379  * Arguments: flash_address, 32 bit flash memory address
380  * io_mode, I/O mode to transfer address
381  * addr_4byte_mode,
382  * Description: Send flash address with 3-byte or 4-byte mode.
383  * Return Message: None
384  */
385 void SendFlashAddr( uint32_t flash_address, uint8_t io_mode, bool addr_4byte_mode )
386 {
387  /* Check flash is 3-byte or 4-byte mode.
388  4-byte mode: Send 4-byte address (A31-A0)
389  3-byte mode: Send 3-byte address (A23-A0) */
390  if( addr_4byte_mode == TRUE ){
391  SendByte( (flash_address >> 24), io_mode ); // A31-A24
392  }
393  /* A23-A0 */
394  SendByte( (flash_address >> 16), io_mode );
395  SendByte( (flash_address >> 8), io_mode );
396  SendByte( (flash_address), io_mode );
397 }
398 
399 /*
400  * Function: GetDummyCycle
401  * Arguments: default_cycle, default dummy cycle
402  * fsptr, pointer of flash status structure
403  * Description: Get dummy cycle for different condition
404  * default_cycle: Byte3 | Byte2 | Byte1 | Byte0
405  * DC 1 bit: x 1 x 0
406  * DC 2 bit: 11 10 01 00
407  * Note: the variable dummy cycle only support
408  in some product.
409  * Return Message: Dummy cycle value
410  */
411 uint8_t GetDummyCycle( uint32_t default_cycle )
412 {
413 #ifdef FLASH_CMD_RDCR
414  uint8_t gDataBuffer;
415  uint8_t dummy_cycle = default_cycle;
416  MX25_RDCR( &gDataBuffer );
417  #ifdef SUPPORT_CR_DC
418  // product support 1-bit dummy cycle configuration
419  if( (gDataBuffer & FLASH_DC_MASK) == FLASH_DC_MASK )
420  dummy_cycle = default_cycle >> 16;
421  else
422  dummy_cycle = default_cycle;
423  #elif SUPPORT_CR_DC_2bit
424  // product support 2-bit dummy cycle configuration
425  switch( gDataBuffer & FLASH_DC_2BIT_MASK ){
426  case 0x00:
427  dummy_cycle = default_cycle;
428  break;
429  case 0x40:
430  dummy_cycle = default_cycle >> 8;
431  break;
432  case 0x80:
433  dummy_cycle = default_cycle >> 16;
434  break;
435  case 0xC0:
436  dummy_cycle = default_cycle >> 24;
437  break;
438  }
439  #else
440  // configuration register not support dummy configuration
441  dummy_cycle = default_cycle;
442  #endif
443  return dummy_cycle;
444 #else
445  // default case: return default dummy cycle
446  return default_cycle;
447 #endif
448 }
449 
450 
451 
452 /*
453  * ID Command
454  */
455 
456 /*
457  * Function: MX25_RDID
458  * Arguments: Identification, 32 bit buffer to store id
459  * Description: The RDID instruction is to read the manufacturer ID
460  * of 1-byte and followed by Device ID of 2-byte.
461  * Return Message: FlashOperationSuccess
462  */
463 ReturnMsg MX25_RDID( uint32_t *Identification )
464 {
465  uint32_t temp;
466  uint8_t gDataBuffer[3];
467 
468  // Chip select go low to start a flash command
469  CS_Low();
470 
471  // Send command
472  SendByte( FLASH_CMD_RDID, SIO );
473 
474  // Get manufacturer identification, device identification
475  gDataBuffer[0] = GetByte( SIO );
476  gDataBuffer[1] = GetByte( SIO );
477  gDataBuffer[2] = GetByte( SIO );
478 
479  // Chip select go high to end a command
480  CS_High();
481 
482  // Store identification
483  temp = gDataBuffer[0];
484  temp = (temp << 8) | gDataBuffer[1];
485  *Identification = (temp << 8) | gDataBuffer[2];
486 
487  return FlashOperationSuccess;
488 }
489 
490 /*
491  * Function: MX25_RES
492  * Arguments: ElectricIdentification, 8 bit buffer to store electric id
493  * Description: The RES instruction is to read the Device
494  * electric identification of 1-byte.
495  * Return Message: FlashOperationSuccess
496  */
497 ReturnMsg MX25_RES( uint8_t *ElectricIdentification )
498 {
499 
500  // Chip select go low to start a flash command
501  CS_Low();
502 
503  // Send flash command and insert dummy cycle
504  SendByte( FLASH_CMD_RES, SIO );
505  InsertDummyCycle( 24 );
506 
507  // Get electric identification
508  *ElectricIdentification = GetByte( SIO );
509 
510  // Chip select go high to end a flash command
511  CS_High();
512 
513  return FlashOperationSuccess;
514 }
515 
516 /*
517  * Function: MX25_REMS
518  * Arguments: REMS_Identification, 16 bit buffer to store id
519  * fsptr, pointer of flash status structure
520  * Description: The REMS instruction is to read the Device
521  * manufacturer ID and electric ID of 1-byte.
522  * Return Message: FlashOperationSuccess
523  */
524 ReturnMsg MX25_REMS( uint16_t *REMS_Identification, FlashStatus *fsptr )
525 {
526  uint8_t gDataBuffer[2];
527 
528  // Chip select go low to start a flash command
529  CS_Low();
530 
531  // Send flash command and insert dummy cycle ( if need )
532  // ArrangeOpt = 0x00 will output the manufacturer's ID first
533  // = 0x01 will output electric ID first
534  SendByte( FLASH_CMD_REMS, SIO );
535  InsertDummyCycle( 16 );
536  SendByte( fsptr->ArrangeOpt, SIO );
537 
538  // Get ID
539  gDataBuffer[0] = GetByte( SIO );
540  gDataBuffer[1] = GetByte( SIO );
541 
542  // Store identification informaion
543  *REMS_Identification = (gDataBuffer[0] << 8) | gDataBuffer[1];
544 
545  // Chip select go high to end a flash command
546  CS_High();
547 
548  return FlashOperationSuccess;
549 }
550 
551 
552 /*
553  * Register Command
554  */
555 
556 /*
557  * Function: MX25_RDSR
558  * Arguments: StatusReg, 8 bit buffer to store status register value
559  * Description: The RDSR instruction is for reading Status Register Bits.
560  * Return Message: FlashOperationSuccess
561  */
562 ReturnMsg MX25_RDSR( uint8_t *StatusReg )
563 {
564  uint8_t gDataBuffer;
565 
566  // Chip select go low to start a flash command
567  CS_Low();
568 
569  // Send command
570  SendByte( FLASH_CMD_RDSR, SIO );
571  gDataBuffer = GetByte( SIO );
572 
573  // Chip select go high to end a flash command
574  CS_High();
575 
576  *StatusReg = gDataBuffer;
577 
578  return FlashOperationSuccess;
579 }
580 
581 /*
582  * Function: MX25_WRSR
583  * Arguments: UpdateValue, 8/16 bit status register value to updata
584  * Description: The WRSR instruction is for changing the values of
585  * Status Register Bits (and configuration register)
586  * Return Message: FlashIsBusy, FlashTimeOut, FlashOperationSuccess
587  */
588 #ifdef SUPPORT_WRSR_CR
589 ReturnMsg MX25_WRSR( uint16_t UpdateValue )
590 #else
591 ReturnMsg MX25_WRSR( uint8_t UpdateValue )
592 #endif
593 {
594  // Check flash is busy or not
595  if( IsFlashBusy() ) return FlashIsBusy;
596 
597  // Setting Write Enable Latch bit
598  MX25_WREN();
599 
600  // Chip select go low to start a flash command
601  CS_Low();
602 
603  // Send command and update value
604  SendByte( FLASH_CMD_WRSR, SIO );
605  SendByte( UpdateValue, SIO );
606 #ifdef SUPPORT_WRSR_CR
607  SendByte( UpdateValue >> 8, SIO ); // write configuration register
608 #endif
609 
610  // Chip select go high to end a flash command
611  CS_High();
612 
613 
614  if( WaitFlashReady( WriteStatusRegCycleTime ) )
615  return FlashOperationSuccess;
616  else
617  return FlashTimeOut;
618 
619 }
620 
621 /*
622  * Function: MX25_RDSCUR
623  * Arguments: SecurityReg, 8 bit buffer to store security register value
624  * Description: The RDSCUR instruction is for reading the value of
625  * Security Register bits.
626  * Return Message: FlashOperationSuccess
627  */
628 ReturnMsg MX25_RDSCUR( uint8_t *SecurityReg )
629 {
630  uint8_t gDataBuffer;
631 
632  // Chip select go low to start a flash command
633  CS_Low();
634 
635  //Send command
636  SendByte( FLASH_CMD_RDSCUR, SIO );
637  gDataBuffer = GetByte( SIO );
638 
639  // Chip select go high to end a flash command
640  CS_High();
641 
642  *SecurityReg = gDataBuffer;
643 
644  return FlashOperationSuccess;
645 
646 }
647 
648 /*
649  * Function: MX25_WRSCUR
650  * Arguments: None.
651  * Description: The WRSCUR instruction is for changing the values of
652  * Security Register Bits.
653  * Return Message: FlashIsBusy, FlashOperationSuccess, FlashWriteRegFailed,
654  * FlashTimeOut
655  */
656 ReturnMsg MX25_WRSCUR( void )
657 {
658  uint8_t gDataBuffer;
659 
660  // Check flash is busy or not
661  if( IsFlashBusy() ) return FlashIsBusy;
662 
663  // Setting Write Enable Latch bit
664  MX25_WREN();
665 
666  // Chip select go low to start a flash command
667  CS_Low();
668 
669  // Write WRSCUR command
670  SendByte( FLASH_CMD_WRSCUR, SIO );
671 
672  // Chip select go high to end a flash command
673  CS_High();
674 
675  if( WaitFlashReady( WriteSecuRegCycleTime ) ){
676 
677  MX25_RDSCUR( &gDataBuffer );
678 
679  // Check security register LDSO bit
680  if( (gDataBuffer & FLASH_LDSO_MASK) == FLASH_LDSO_MASK )
681  return FlashOperationSuccess;
682  else
683  return FlashWriteRegFailed;
684  }
685  else
686  return FlashTimeOut;
687 
688 }
689 
690 /*
691  * Function: MX25_RDCR
692  * Arguments: ConfigReg, 8 bit buffer to store Configuration register value
693  * Description: The RDCR instruction is for reading Configuration Register Bits.
694  * Return Message: FlashOperationSuccess
695  */
696 ReturnMsg MX25_RDCR( uint8_t *ConfigReg )
697 {
698  uint8_t gDataBuffer;
699 
700  // Chip select go low to start a flash command
701  CS_Low();
702 
703  // Send command
704  SendByte( FLASH_CMD_RDCR, SIO );
705  gDataBuffer = GetByte( SIO );
706 
707  // Chip select go high to end a flash command
708  CS_High();
709 
710  *ConfigReg = gDataBuffer;
711 
712  return FlashOperationSuccess;
713 }
714 
715 
716 /*
717  * Read Command
718  */
719 
720 /*
721  * Function: MX25_READ
722  * Arguments: flash_address, 32 bit flash memory address
723  * target_address, buffer address to store returned data
724  * byte_length, length of returned data in byte unit
725  * Description: The READ instruction is for reading data out.
726  * Return Message: FlashAddressInvalid, FlashOperationSuccess
727  */
728 ReturnMsg MX25_READ( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
729 {
730  uint32_t index;
731  uint8_t addr_4byte_mode;
732 
733  // Check flash address
734  if( flash_address > FlashSize ) return FlashAddressInvalid;
735 
736  // Check 3-byte or 4-byte mode
737  if( IsFlash4Byte() )
738  addr_4byte_mode = TRUE; // 4-byte mode
739  else
740  addr_4byte_mode = FALSE; // 3-byte mode
741 
742  // Chip select go low to start a flash command
743  CS_Low();
744 
745  // Write READ command and address
746  SendByte( FLASH_CMD_READ, SIO );
747  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
748 
749  // Set a loop to read data into buffer
750  for( index=0; index < byte_length; index++ )
751  {
752  // Read data one byte at a time
753  *(target_address + index) = GetByte( SIO );
754  }
755 
756  // Chip select go high to end a flash command
757  CS_High();
758 
759  return FlashOperationSuccess;
760 }
761 
762 /*
763  * Function: MX25_2READ
764  * Arguments: flash_address, 32 bit flash memory address
765  * target_address, buffer address to store returned data
766  * byte_length, length of returned data in byte unit
767  * Description: The 2READ instruction enable double throughput of Serial
768  * Flash in read mode
769  * Return Message: FlashAddressInvalid, FlashOperationSuccess
770  */
771 ReturnMsg MX25_2READ( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
772 {
773  uint32_t index;
774  uint8_t addr_4byte_mode;
775  uint8_t dc;
776 
777  // Check flash address
778  if( flash_address > FlashSize ) return FlashAddressInvalid;
779 
780  // Check 3-byte or 4-byte mode
781  if( IsFlash4Byte() )
782  addr_4byte_mode = TRUE; // 4-byte mode
783  else
784  addr_4byte_mode = FALSE; // 3-byte mode
785 
786  dc = GetDummyCycle( DUMMY_CONF_2READ );
787 
788  // Chip select go low to start a flash command
789  CS_Low();
790 
791  // Write 2-I/O Read command and address
792  SendByte( FLASH_CMD_2READ, SIO );
793  SendFlashAddr( flash_address, DIO, addr_4byte_mode );
794  InsertDummyCycle( dc ); // Wait 4 dummy cycle
795 
796  // Set a loop to read data into data buffer
797  for( index=0; index < byte_length; index++ )
798  {
799  *(target_address + index) = GetByte( DIO );
800  }
801 
802  // Chip select go high to end a flash command
803  CS_High();
804 
805  return FlashOperationSuccess;
806 }
807 
808 /*
809  * Function: MX25_4READ
810  * Arguments: flash_address, 32 bit flash memory address
811  * target_address, buffer address to store returned data
812  * byte_length, length of returned data in byte unit
813  * Description: The 4READ instruction enable quad throughput of
814  * Serial Flash in read mode.
815  * Return Message: FlashAddressInvalid, FlashQuadNotEnable, FlashOperationSuccess
816  */
817 ReturnMsg MX25_4READ( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
818 {
819  uint32_t index=0;
820  uint8_t addr_4byte_mode;
821  uint8_t dc;
822 
823 
824  // Check flash address
825  if( flash_address > FlashSize ) return FlashAddressInvalid;
826 
827 #ifndef NO_QE_BIT
828  // Check QE bit
829  if( IsFlashQIO() != TRUE ) return FlashQuadNotEnable;
830 #endif
831 
832  // Check 3-byte or 4-byte mode
833  if( IsFlash4Byte() )
834  addr_4byte_mode = TRUE; // 4-byte mode
835  else
836  addr_4byte_mode = FALSE; // 3-byte mode
837 
838  // get dummy cycle number
839  dc = GetDummyCycle( DUMMY_CONF_4READ );
840 
841  // Chip select go low to start a flash command
842  CS_Low();
843 
844  // Write 4-I/O Read Array command
845  SendByte( FLASH_CMD_4READ, SIO );
846  SendFlashAddr( flash_address, QIO, addr_4byte_mode );
847  InsertDummyCycle ( dc ); // Wait 6 or 8 dummy cycle
848 
849  // Set a loop to read data into flash's buffer
850  for( index=0; index < byte_length; index=index + 1 )
851  {
852  *(target_address + index) = GetByte( QIO );
853  }
854 
855  // Chip select go high to end a flash command
856  CS_High();
857 
858  return FlashOperationSuccess;
859 }
860 
861 
862 /*
863  * Function: MX25_DREAD
864  * Arguments: flash_address, 32 bit flash memory address
865  * target_address, buffer address to store returned data
866  * byte_length, length of returned data in byte unit
867  * Description: The DREAD instruction enable double throughput of Serial
868  * Flash in read mode
869  * Return Message: FlashAddressInvalid, FlashOperationSuccess
870  */
871 ReturnMsg MX25_DREAD( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
872 {
873  uint32_t index;
874  uint8_t addr_4byte_mode;
875  uint8_t dc;
876 
877  // Check flash address
878  if( flash_address > FlashSize ) return FlashAddressInvalid;
879 
880  // Check 3-byte or 4-byte mode
881  if( IsFlash4Byte() )
882  addr_4byte_mode = TRUE; // 4-byte mode
883  else
884  addr_4byte_mode = FALSE; // 3-byte mode
885 
886  // get dummy cycle number
887  dc = GetDummyCycle( DUMMY_CONF_DREAD );
888 
889  // Chip select go low to start a flash command
890  CS_Low();
891 
892  // Write 2-I/O Read command and address
893  SendByte( FLASH_CMD_DREAD, SIO );
894  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
895  InsertDummyCycle( dc ); // Wait 8 dummy cycle
896 
897  // Set a loop to read data into data buffer
898  for( index=0; index < byte_length; index++ )
899  {
900  *(target_address + index) = GetByte( DIO );
901  }
902 
903  // Chip select go high to end a flash command
904  CS_High();
905 
906  return FlashOperationSuccess;
907 }
908 
909 /*
910  * Function: MX25_QREAD
911  * Arguments: flash_address, 32 bit flash memory address
912  * target_address, buffer address to store returned data
913  * byte_length, length of returned data in byte unit
914  * Description: The QREAD instruction enable quad throughput of
915  * Serial Flash in read mode.
916  * Return Message: FlashAddressInvalid, FlashQuadNotEnable, FlashOperationSuccess
917  */
918 ReturnMsg MX25_QREAD( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
919 {
920  uint32_t index=0;
921  uint8_t addr_4byte_mode;
922  uint8_t dc;
923 
924  // Check flash address
925  if( flash_address > FlashSize ) return FlashAddressInvalid;
926 
927  // Check QE bit
928  if( IsFlashQIO() != TRUE ) return FlashQuadNotEnable;
929 
930  // Check 3-byte or 4-byte mode
931  if( IsFlash4Byte() )
932  addr_4byte_mode = TRUE; // 4-byte mode
933  else
934  addr_4byte_mode = FALSE; // 3-byte mode
935 
936  // get dummy cycle number
937  dc = GetDummyCycle( DUMMY_CONF_QREAD );
938 
939  // Chip select go low to start a flash command
940  CS_Low();
941 
942  // Write 4-I/O Read Array command
943  SendByte( FLASH_CMD_QREAD, SIO );
944  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
945  InsertDummyCycle ( dc ); // Wait 8 dummy cycle
946 
947  // Set a loop to read data into flash's buffer
948  for( index=0; index < byte_length; index=index+1)
949  {
950  *(target_address + index) = GetByte( QIO );
951  }
952 
953  // Chip select go high to end a flash command
954  CS_High();
955 
956  return FlashOperationSuccess;
957 }
958 
959 /*
960  * Function: MX25_FASTREAD
961  * Arguments: flash_address, 32 bit flash memory address
962  * target_address, buffer address to store returned data
963  * byte_length, length of returned data in byte unit
964  * Description: The FASTREAD instruction is for quickly reading data out.
965  * Return Message: FlashAddressInvalid, FlashOperationSuccess
966  */
967 ReturnMsg MX25_FASTREAD( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
968 {
969  uint32_t index;
970  uint8_t addr_4byte_mode;
971  uint8_t dc;
972 
973  // Check flash address
974  if( flash_address > FlashSize ) return FlashAddressInvalid;
975 
976  // Check 3-byte or 4-byte mode
977  if( IsFlash4Byte() )
978  addr_4byte_mode = TRUE; // 4-byte mode
979  else
980  addr_4byte_mode = FALSE; // 3-byte mode
981 
982  dc = GetDummyCycle( DUMMY_CONF_FASTREAD );
983 
984  // Chip select go low to start a flash command
985  CS_Low();
986 
987  // Write Fast Read command, address and dummy cycle
988  SendByte( FLASH_CMD_FASTREAD, SIO );
989  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
990  InsertDummyCycle ( dc ); // Wait dummy cycle
991 
992  // Set a loop to read data into data buffer
993  for( index=0; index < byte_length; index++ )
994  {
995  *(target_address + index) = GetByte( SIO );
996  }
997 
998  // Chip select go high to end a flash command
999  CS_High();
1000 
1001  return FlashOperationSuccess;
1002 }
1003 
1004 
1005 /*
1006  * Function: MX25_RDSFDP
1007  * Arguments: flash_address, 32 bit flash memory address
1008  * target_address, buffer address to store returned data
1009  * byte_length, length of returned data in byte unit
1010  * Description: RDSFDP can retrieve the operating characteristics, structure
1011  * and vendor-specified information such as identifying information,
1012  * memory size, operating voltages and timinginformation of device
1013  * Return Message: FlashAddressInvalid, FlashOperationSuccess
1014  */
1015 ReturnMsg MX25_RDSFDP( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
1016 {
1017  uint32_t index;
1018  uint8_t addr_4byte_mode;
1019 
1020  // Check flash address
1021  if( flash_address > FlashSize ) return FlashAddressInvalid;
1022 
1023  // Check 3-byte or 4-byte mode
1024  if( IsFlash4Byte() )
1025  addr_4byte_mode = TRUE; // 4-byte mode
1026  else
1027  addr_4byte_mode = FALSE; // 3-byte mode
1028 
1029  // Chip select go low to start a flash command
1030  CS_Low();
1031 
1032  // Write Read SFDP command
1033  SendByte( FLASH_CMD_RDSFDP, SIO );
1034  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1035  InsertDummyCycle ( 8 ); // Insert dummy cycle
1036 
1037  // Set a loop to read data into data buffer
1038  for( index=0; index < byte_length; index++ )
1039  {
1040  *(target_address + index) = GetByte( SIO );
1041  }
1042 
1043  // Chip select go high to end a flash command
1044  CS_High();
1045 
1046  return FlashOperationSuccess;
1047 }
1048 /*
1049  * Program Command
1050  */
1051 
1052 /*
1053  * Function: MX25_WREN
1054  * Arguments: None.
1055  * Description: The WREN instruction is for setting
1056  * Write Enable Latch (WEL) bit.
1057  * Return Message: FlashOperationSuccess
1058  */
1059 ReturnMsg MX25_WREN( void )
1060 {
1061  // Chip select go low to start a flash command
1062  CS_Low();
1063 
1064  // Write Enable command = 0x06, Setting Write Enable Latch Bit
1065  SendByte( FLASH_CMD_WREN, SIO );
1066 
1067  // Chip select go high to end a flash command
1068  CS_High();
1069 
1070  return FlashOperationSuccess;
1071 }
1072 
1073 /*
1074  * Function: MX25_WRDI
1075  * Arguments: None.
1076  * Description: The WRDI instruction is to reset
1077  * Write Enable Latch (WEL) bit.
1078  * Return Message: FlashOperationSuccess
1079  */
1080 ReturnMsg MX25_WRDI( void )
1081 {
1082  // Chip select go low to start a flash command
1083  CS_Low();
1084 
1085  // Write Disable command = 0x04, resets Write Enable Latch Bit
1086  SendByte( FLASH_CMD_WRDI, SIO );
1087 
1088  CS_High();
1089 
1090  return FlashOperationSuccess;
1091 }
1092 
1093 
1094 /*
1095  * Function: MX25_PP
1096  * Arguments: flash_address, 32 bit flash memory address
1097  * source_address, buffer address of source data to program
1098  * byte_length, byte length of data to programm
1099  * Description: The PP instruction is for programming
1100  * the memory to be "0".
1101  * The device only accept the last 256 byte ( or 32 byte ) to program.
1102  * If the page address ( flash_address[7:0] ) reach 0xFF, it will
1103  * program next at 0x00 of the same page.
1104  * Some products have smaller page size ( 32 byte )
1105  * Return Message: FlashAddressInvalid, FlashIsBusy, FlashOperationSuccess,
1106  * FlashTimeOut
1107  */
1108 ReturnMsg MX25_PP( uint32_t flash_address, uint8_t *source_address, uint32_t byte_length )
1109 {
1110  uint32_t index;
1111  uint8_t addr_4byte_mode;
1112 
1113  // Check flash address
1114  if( flash_address > FlashSize ) return FlashAddressInvalid;
1115 
1116  // Check flash is busy or not
1117  if( IsFlashBusy() ) return FlashIsBusy;
1118 
1119  // Check 3-byte or 4-byte mode
1120  if( IsFlash4Byte() )
1121  addr_4byte_mode = TRUE; // 4-byte mode
1122  else
1123  addr_4byte_mode = FALSE; // 3-byte mode
1124 
1125  // Setting Write Enable Latch bit
1126  MX25_WREN();
1127 
1128  // Chip select go low to start a flash command
1129  CS_Low();
1130 
1131  // Write Page Program command
1132  SendByte( FLASH_CMD_PP, SIO );
1133  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1134 
1135  // Set a loop to down load whole page data into flash's buffer
1136  // Note: only last 256 byte ( or 32 byte ) will be programmed
1137  for( index=0; index < byte_length; index++ )
1138  {
1139  SendByte( *(source_address + index), SIO );
1140  }
1141 
1142  // Chip select go high to end a flash command
1143  CS_High();
1144 
1145  if( WaitFlashReady( PageProgramCycleTime ) )
1146  return FlashOperationSuccess;
1147  else
1148  return FlashTimeOut;
1149 }
1150 
1151 
1152 /*
1153  * Function: MX25_4PP
1154  * Arguments: flash_address, 32 bit flash memory address
1155  * source_address, buffer address of source data to program
1156  * byte_length, byte length of data to programm
1157  * Description: The 4PP instruction is for programming
1158  * the memory to be "0".
1159  * The device only accept the last 256 byte ( one page ) to program.
1160  * If the page address ( flash_address[7:0] ) reach 0xFF, it will
1161  * program next at 0x00 of the same page.
1162  * The different between QPP and 4PP is the IO number during sending address
1163  * Return Message: FlashQuadNotEnable, FlashAddressInvalid, FlashIsBusy,
1164  * FlashOperationSuccess, FlashTimeOut
1165  */
1166 ReturnMsg MX25_4PP( uint32_t flash_address, uint8_t *source_address, uint32_t byte_length )
1167 {
1168  uint32_t index;
1169  uint8_t addr_4byte_mode;
1170 
1171  // Check QE bit
1172  if( !IsFlashQIO() ) return FlashQuadNotEnable;
1173 
1174  // Check flash address
1175  if( flash_address > FlashSize ) return FlashAddressInvalid;
1176 
1177  // Check flash is busy or not
1178  if( IsFlashBusy() ) return FlashIsBusy;
1179 
1180  // Check 3-byte or 4-byte mode
1181  if( IsFlash4Byte() )
1182  addr_4byte_mode = TRUE; // 4-byte mode
1183  else
1184  addr_4byte_mode = FALSE; // 3-byte mode
1185 
1186  // Setting Write Enable Latch bit
1187  MX25_WREN();
1188 
1189  // Chip select go low to start a flash command
1190  CS_Low();
1191 
1192  // Write 4-I/O Page Program command
1193  SendByte( FLASH_CMD_4PP, SIO );
1194  SendFlashAddr( flash_address, QIO, addr_4byte_mode );
1195 
1196  // Send source data to flash.
1197  for( index=0; index < byte_length; index++ )
1198  {
1199  SendByte( *(source_address + index), QIO );
1200  }
1201 
1202  // Chip select go high to end a flash command
1203  CS_High();
1204 
1205  if( WaitFlashReady( PageProgramCycleTime ) )
1206  return FlashOperationSuccess;
1207  else
1208  return FlashTimeOut;
1209 }
1210 
1211 /*
1212  * Erase Command
1213  */
1214 
1215 /*
1216  * Function: MX25_SE
1217  * Arguments: flash_address, 32 bit flash memory address
1218  * Description: The SE instruction is for erasing the data
1219  * of the chosen sector (4KB) to be "1".
1220  * Return Message: FlashAddressInvalid, FlashIsBusy, FlashOperationSuccess,
1221  * FlashTimeOut
1222  */
1223 ReturnMsg MX25_SE( uint32_t flash_address )
1224 {
1225  uint8_t addr_4byte_mode;
1226 
1227  // Check flash address
1228  if( flash_address > FlashSize ) return FlashAddressInvalid;
1229 
1230  // Check flash is busy or not
1231  if( IsFlashBusy() ) return FlashIsBusy;
1232 
1233  // Check 3-byte or 4-byte mode
1234  if( IsFlash4Byte() )
1235  addr_4byte_mode = TRUE; // 4-byte mode
1236  else
1237  addr_4byte_mode = FALSE; // 3-byte mode
1238 
1239  // Setting Write Enable Latch bit
1240  MX25_WREN();
1241 
1242  // Chip select go low to start a flash command
1243  CS_Low();
1244 
1245  //Write Sector Erase command = 0x20;
1246  SendByte( FLASH_CMD_SE, SIO );
1247  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1248 
1249  // Chip select go high to end a flash command
1250  CS_High();
1251 
1252  if( WaitFlashReady( SectorEraseCycleTime ) )
1253  return FlashOperationSuccess;
1254  else
1255  return FlashTimeOut;
1256 }
1257 
1258 /*
1259  * Function: MX25_BE32K
1260  * Arguments: flash_address, 32 bit flash memory address
1261  * Description: The BE32K instruction is for erasing the data
1262  * of the chosen sector (32KB) to be "1".
1263  * Return Message: FlashAddressInvalid, FlashIsBusy, FlashOperationSuccess,
1264  * FlashTimeOut
1265  */
1266 ReturnMsg MX25_BE32K( uint32_t flash_address )
1267 {
1268  uint8_t addr_4byte_mode;
1269 
1270  // Check flash address
1271  if( flash_address > FlashSize ) return FlashAddressInvalid;
1272 
1273  // Check flash is busy or not
1274  if( IsFlashBusy() ) return FlashIsBusy;
1275 
1276  // Check 3-byte or 4-byte mode
1277  if( IsFlash4Byte() )
1278  addr_4byte_mode = TRUE; // 4-byte mode
1279  else
1280  addr_4byte_mode = FALSE; // 3-byte mode
1281 
1282  // Setting Write Enable Latch bit
1283  MX25_WREN();
1284 
1285  // Chip select go low to start a flash command
1286  CS_Low();
1287 
1288  //Write Block Erase32KB command;
1289  SendByte( FLASH_CMD_BE32K, SIO );
1290  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1291 
1292  // Chip select go high to end a flash command
1293  CS_High();
1294 
1295  if( WaitFlashReady( BlockErase32KCycleTime ) )
1296  return FlashOperationSuccess;
1297  else
1298  return FlashTimeOut;
1299 }
1300 
1301 /*
1302  * Function: MX25_BE
1303  * Arguments: flash_address, 32 bit flash memory address
1304  * Description: The BE instruction is for erasing the data
1305  * of the chosen sector (64KB) to be "1".
1306  * Return Message: FlashAddressInvalid, FlashIsBusy, FlashOperationSuccess,
1307  * FlashTimeOut
1308  */
1309 ReturnMsg MX25_BE( uint32_t flash_address )
1310 {
1311  uint8_t addr_4byte_mode;
1312 
1313  // Check flash address
1314  if( flash_address > FlashSize ) return FlashAddressInvalid;
1315 
1316  // Check flash is busy or not
1317  if( IsFlashBusy() ) return FlashIsBusy;
1318 
1319  // Check 3-byte or 4-byte mode
1320  if( IsFlash4Byte() )
1321  addr_4byte_mode = TRUE; // 4-byte mode
1322  else
1323  addr_4byte_mode = FALSE; // 3-byte mode
1324 
1325  // Setting Write Enable Latch bit
1326  MX25_WREN();
1327 
1328  // Chip select go low to start a flash command
1329  CS_Low();
1330 
1331  //Write Block Erase command = 0xD8;
1332  SendByte( FLASH_CMD_BE, SIO );
1333  SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1334 
1335  // Chip select go high to end a flash command
1336  CS_High();
1337 
1338  if( WaitFlashReady( BlockEraseCycleTime ) )
1339  return FlashOperationSuccess;
1340  else
1341  return FlashTimeOut;
1342 }
1343 
1344 /*
1345  * Function: MX25_CE
1346  * Arguments: None.
1347  * Description: The CE instruction is for erasing the data
1348  * of the whole chip to be "1".
1349  * Return Message: FlashIsBusy, FlashOperationSuccess, FlashTimeOut
1350  */
1351 ReturnMsg MX25_CE( void )
1352 {
1353  // Check flash is busy or not
1354  if( IsFlashBusy() ) return FlashIsBusy;
1355 
1356  // Setting Write Enable Latch bit
1357  MX25_WREN();
1358 
1359  // Chip select go low to start a flash command
1360  CS_Low();
1361 
1362  //Write Chip Erase command = 0x60;
1363  SendByte( FLASH_CMD_CE, SIO );
1364 
1365  // Chip select go high to end a flash command
1366  CS_High();
1367 
1368  if( WaitFlashReady( ChipEraseCycleTime ) )
1369  return FlashOperationSuccess;
1370  else
1371  return FlashTimeOut;
1372 }
1373 
1374 
1375 /*
1376  * Mode setting Command
1377  */
1378 
1379 /*
1380  * Function: MX25_DP
1381  * Arguments: None.
1382  * Description: The DP instruction is for setting the
1383  * device on the minimizing the power consumption.
1384  * Return Message: FlashOperationSuccess
1385  */
1386 ReturnMsg MX25_DP( void )
1387 {
1388  // Chip select go low to start a flash command
1389  CS_Low();
1390 
1391  // Deep Power Down Mode command
1392  SendByte( FLASH_CMD_DP, SIO );
1393 
1394  // Chip select go high to end a flash command
1395  CS_High();
1396 
1397  return FlashOperationSuccess;
1398 }
1399 
1400 
1401 /*
1402  * Function: MX25_ENSO
1403  * Arguments: None.
1404  * Description: The ENSO instruction is for entering the secured OTP mode.
1405  * Return Message: FlashOperationSuccess
1406  */
1407 ReturnMsg MX25_ENSO( void )
1408 {
1409  // Chip select go low to start a flash command
1410  CS_Low();
1411 
1412  // Write ENSO command
1413  SendByte( FLASH_CMD_ENSO, SIO );
1414 
1415  // Chip select go high to end a flash command
1416  CS_High();
1417 
1418  return FlashOperationSuccess;
1419 }
1420 
1421 /*
1422  * Function: MX25_EXSO
1423  * Arguments: None.
1424  * Description: The EXSO instruction is for exiting the secured OTP mode.
1425  * Return Message: FlashOperationSuccess
1426  */
1427 ReturnMsg MX25_EXSO( void )
1428 {
1429  // Chip select go low to start a flash command
1430  CS_Low();
1431 
1432  // Write EXSO command = 0xC1
1433  SendByte( FLASH_CMD_EXSO, SIO );
1434 
1435  // Chip select go high to end a flash command
1436  CS_High();
1437 
1438  return FlashOperationSuccess;
1439 }
1440 
1441 
1442 /*
1443  * Function: MX25_SBL
1444  * Arguments: burstconfig, burst length configuration
1445  * Description: To set the Burst length
1446  * Return Message: FlashOperationSuccess
1447  */
1448 ReturnMsg MX25_SBL( uint8_t burstconfig )
1449 {
1450  // Chip select go low to start a flash command
1451  CS_Low();
1452 
1453  // Send SBL command and config data
1454  SendByte( FLASH_CMD_SBL, SIO );
1455  SendByte( burstconfig, SIO );
1456 
1457  // Chip select go high to end a flash command
1458  CS_High();
1459 
1460  return FlashOperationSuccess;
1461 }
1462 
1463 
1464 /*
1465  * Reset setting Command
1466  */
1467 
1468 /*
1469  * Function: MX25_RSTEN
1470  * Arguments: None.
1471  * Description: Enable RST command
1472  * Return Message: FlashOperationSuccess
1473  */
1474 ReturnMsg MX25_RSTEN( void )
1475 {
1476  // Chip select go low to start a flash command
1477  CS_Low();
1478 
1479  // Write RSTEN command
1480  SendByte( FLASH_CMD_RSTEN, SIO );
1481 
1482  // Chip select go high to end a flash command
1483  CS_High();
1484 
1485  return FlashOperationSuccess;
1486 }
1487 
1488 /*
1489  * Function: MX25_RST
1490  * Arguments: fsptr, pointer of flash status structure
1491  * Description: The RST instruction is used as a system (software) reset that
1492  * puts the device in normal operating Ready mode.
1493  * Return Message: FlashOperationSuccess
1494  */
1495 ReturnMsg MX25_RST( FlashStatus *fsptr )
1496 {
1497  // Chip select go low to start a flash command
1498  CS_Low();
1499 
1500  // Write RST command = 0x99
1501  SendByte( FLASH_CMD_RST, SIO );
1502 
1503  // Chip select go high to end a flash command
1504  CS_High();
1505 
1506  // Reset current state
1507  fsptr->ArrangeOpt = FALSE;
1508  fsptr->ModeReg = 0x00;
1509 
1510  return FlashOperationSuccess;
1511 }
1512 
1513 /*
1514  * Security Command
1515  */
1516 
1517 
1518 /*
1519  * Suspend/Resume Command
1520  */
1521 
1522 /*
1523  * Function: MX25_PGM_ERS_S
1524  * Arguments: None
1525  * Description: The PGM_ERS_S suspend Sector-Erase, Block-Erase or
1526  * Page-Program operations and conduct other operations.
1527  * Return Message: FlashOperationSuccess
1528  */
1529 ReturnMsg MX25_PGM_ERS_S( void )
1530 {
1531 
1532  // Chip select go low to start a flash command
1533  CS_Low();
1534 
1535  // Send program/erase suspend command
1536  SendByte( FLASH_CMD_PGM_ERS_S, SIO );
1537 
1538  // Chip select go high to end a flash command
1539  CS_High();
1540 
1541  return FlashOperationSuccess;
1542 }
1543 
1544 /*
1545  * Function: MX25_PGM_ERS_R
1546  * Arguments: None
1547  * Description: The PGM_ERS_R resume Sector-Erase, Block-Erase or
1548  * Page-Program operations.
1549  * Return Message: FlashOperationSuccess
1550  */
1551 ReturnMsg MX25_PGM_ERS_R( void )
1552 {
1553 
1554  // Chip select go low to start a flash command
1555  CS_Low();
1556 
1557  // Send resume command
1558  SendByte( FLASH_CMD_PGM_ERS_R, SIO );
1559 
1560  // Chip select go high to end a flash command
1561  CS_High();
1562 
1563  return FlashOperationSuccess;
1564 }
1565 
1566 
1567 /*
1568  * Function: MX25_NOP
1569  * Arguments: None.
1570  * Description: The NOP instruction is null operation of flash.
1571  * Return Message: FlashOperationSuccess
1572  */
1573 ReturnMsg MX25_NOP( void )
1574 {
1575  // Chip select go low to start a flash command
1576  CS_Low();
1577 
1578  // Write NOP command = 0x00
1579  SendByte( FLASH_CMD_NOP, SIO );
1580 
1581  // Chip select go high to end a flash command
1582  CS_High();
1583 
1584  return FlashOperationSuccess;
1585 }
1586 
Clock management unit (CMU) API.
void USART_InitSync(USART_TypeDef *usart, const USART_InitSync_TypeDef *init)
Init USART for synchronous mode.
Definition: em_usart.c:640
Universal synchronous/asynchronous receiver/transmitter (USART/UART) peripheral API.
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
Definition: em_gpio.c:269
General Purpose IO (GPIO) peripheral API.
__STATIC_INLINE void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out register to 1.
Definition: em_gpio.h:856
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Definition: em_cmu.c:1453
uint8_t USART_SpiTransfer(USART_TypeDef *usart, uint8_t data)
Perform one 8 bit frame SPI transfer.
Definition: em_usart.c:1050
__STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out port register to 0.
Definition: em_gpio.h:811
#define USART_INITSYNC_DEFAULT
Definition: em_usart.h:484
__STATIC_INLINE unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port, unsigned int pin)
Read the pad value for a single pin in a GPIO port.
Definition: em_gpio.h:781