10 #include "mx25flash_spi.h"
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 );
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 );
36 void MX25_init(
void )
71 void Wait_Flash_WarmUp()
73 volatile int time_cnt = FlashFullAccessTime;
115 void InsertDummyCycle( uint8_t dummy_cycle )
119 for( i = 0; i < dummy_cycle/8; i++ )
140 void SendByte( uint8_t byte_value, uint8_t transfer_type )
142 switch( transfer_type )
200 uint8_t GetByte( uint8_t transfer_type )
202 uint8_t data_buf = 0;
204 switch( transfer_type )
259 bool WaitFlashReady( uint32_t ExpectTime )
261 #ifndef NON_SYNCHRONOUS_IO
262 volatile uint32_t temp = 0;
263 while( IsFlashBusy() )
265 if( temp > ExpectTime )
288 bool WaitRYBYReady( uint32_t ExpectTime )
290 #ifndef NON_SYNCHRONOUS_IO
298 if( temp > ExpectTime )
319 bool IsFlashBusy(
void )
323 MX25_RDSR( &gDataBuffer );
324 if( (gDataBuffer & FLASH_WIP_MASK) == FLASH_WIP_MASK )
337 bool IsFlashQIO(
void )
339 #ifdef FLASH_NO_QE_BIT
343 MX25_RDSR( &gDataBuffer );
344 if( (gDataBuffer & FLASH_QE_MASK) == FLASH_QE_MASK )
358 bool IsFlash4Byte(
void )
360 #ifdef FLASH_CMD_RDSCUR
361 #ifdef FLASH_4BYTE_ONLY
363 #elif FLASH_3BYTE_ONLY
367 MX25_RDSCUR( &gDataBuffer );
368 if( (gDataBuffer & FLASH_4BYTE_MASK) == FLASH_4BYTE_MASK )
385 void SendFlashAddr( uint32_t flash_address, uint8_t io_mode,
bool addr_4byte_mode )
390 if( addr_4byte_mode == TRUE ){
391 SendByte( (flash_address >> 24), io_mode );
394 SendByte( (flash_address >> 16), io_mode );
395 SendByte( (flash_address >> 8), io_mode );
396 SendByte( (flash_address), io_mode );
411 uint8_t GetDummyCycle( uint32_t default_cycle )
413 #ifdef FLASH_CMD_RDCR
415 uint8_t dummy_cycle = default_cycle;
416 MX25_RDCR( &gDataBuffer );
419 if( (gDataBuffer & FLASH_DC_MASK) == FLASH_DC_MASK )
420 dummy_cycle = default_cycle >> 16;
422 dummy_cycle = default_cycle;
423 #elif SUPPORT_CR_DC_2bit
425 switch( gDataBuffer & FLASH_DC_2BIT_MASK ){
427 dummy_cycle = default_cycle;
430 dummy_cycle = default_cycle >> 8;
433 dummy_cycle = default_cycle >> 16;
436 dummy_cycle = default_cycle >> 24;
441 dummy_cycle = default_cycle;
446 return default_cycle;
463 ReturnMsg MX25_RDID( uint32_t *Identification )
466 uint8_t gDataBuffer[3];
472 SendByte( FLASH_CMD_RDID, SIO );
475 gDataBuffer[0] = GetByte( SIO );
476 gDataBuffer[1] = GetByte( SIO );
477 gDataBuffer[2] = GetByte( SIO );
483 temp = gDataBuffer[0];
484 temp = (temp << 8) | gDataBuffer[1];
485 *Identification = (temp << 8) | gDataBuffer[2];
487 return FlashOperationSuccess;
497 ReturnMsg MX25_RES( uint8_t *ElectricIdentification )
504 SendByte( FLASH_CMD_RES, SIO );
505 InsertDummyCycle( 24 );
508 *ElectricIdentification = GetByte( SIO );
513 return FlashOperationSuccess;
524 ReturnMsg MX25_REMS( uint16_t *REMS_Identification,
FlashStatus *fsptr )
526 uint8_t gDataBuffer[2];
534 SendByte( FLASH_CMD_REMS, SIO );
535 InsertDummyCycle( 16 );
536 SendByte( fsptr->ArrangeOpt, SIO );
539 gDataBuffer[0] = GetByte( SIO );
540 gDataBuffer[1] = GetByte( SIO );
543 *REMS_Identification = (gDataBuffer[0] << 8) | gDataBuffer[1];
548 return FlashOperationSuccess;
562 ReturnMsg MX25_RDSR( uint8_t *StatusReg )
570 SendByte( FLASH_CMD_RDSR, SIO );
571 gDataBuffer = GetByte( SIO );
576 *StatusReg = gDataBuffer;
578 return FlashOperationSuccess;
588 #ifdef SUPPORT_WRSR_CR
589 ReturnMsg MX25_WRSR( uint16_t UpdateValue )
591 ReturnMsg MX25_WRSR( uint8_t UpdateValue )
595 if( IsFlashBusy() )
return FlashIsBusy;
604 SendByte( FLASH_CMD_WRSR, SIO );
605 SendByte( UpdateValue, SIO );
606 #ifdef SUPPORT_WRSR_CR
607 SendByte( UpdateValue >> 8, SIO );
614 if( WaitFlashReady( WriteStatusRegCycleTime ) )
615 return FlashOperationSuccess;
628 ReturnMsg MX25_RDSCUR( uint8_t *SecurityReg )
636 SendByte( FLASH_CMD_RDSCUR, SIO );
637 gDataBuffer = GetByte( SIO );
642 *SecurityReg = gDataBuffer;
644 return FlashOperationSuccess;
656 ReturnMsg MX25_WRSCUR(
void )
661 if( IsFlashBusy() )
return FlashIsBusy;
670 SendByte( FLASH_CMD_WRSCUR, SIO );
675 if( WaitFlashReady( WriteSecuRegCycleTime ) ){
677 MX25_RDSCUR( &gDataBuffer );
680 if( (gDataBuffer & FLASH_LDSO_MASK) == FLASH_LDSO_MASK )
681 return FlashOperationSuccess;
683 return FlashWriteRegFailed;
696 ReturnMsg MX25_RDCR( uint8_t *ConfigReg )
704 SendByte( FLASH_CMD_RDCR, SIO );
705 gDataBuffer = GetByte( SIO );
710 *ConfigReg = gDataBuffer;
712 return FlashOperationSuccess;
728 ReturnMsg MX25_READ( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
731 uint8_t addr_4byte_mode;
734 if( flash_address > FlashSize )
return FlashAddressInvalid;
738 addr_4byte_mode = TRUE;
740 addr_4byte_mode = FALSE;
746 SendByte( FLASH_CMD_READ, SIO );
747 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
750 for( index=0; index < byte_length; index++ )
753 *(target_address + index) = GetByte( SIO );
759 return FlashOperationSuccess;
771 ReturnMsg MX25_2READ( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
774 uint8_t addr_4byte_mode;
778 if( flash_address > FlashSize )
return FlashAddressInvalid;
782 addr_4byte_mode = TRUE;
784 addr_4byte_mode = FALSE;
786 dc = GetDummyCycle( DUMMY_CONF_2READ );
792 SendByte( FLASH_CMD_2READ, SIO );
793 SendFlashAddr( flash_address, DIO, addr_4byte_mode );
794 InsertDummyCycle( dc );
797 for( index=0; index < byte_length; index++ )
799 *(target_address + index) = GetByte( DIO );
805 return FlashOperationSuccess;
817 ReturnMsg MX25_4READ( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
820 uint8_t addr_4byte_mode;
825 if( flash_address > FlashSize )
return FlashAddressInvalid;
829 if( IsFlashQIO() != TRUE )
return FlashQuadNotEnable;
834 addr_4byte_mode = TRUE;
836 addr_4byte_mode = FALSE;
839 dc = GetDummyCycle( DUMMY_CONF_4READ );
845 SendByte( FLASH_CMD_4READ, SIO );
846 SendFlashAddr( flash_address, QIO, addr_4byte_mode );
847 InsertDummyCycle ( dc );
850 for( index=0; index < byte_length; index=index + 1 )
852 *(target_address + index) = GetByte( QIO );
858 return FlashOperationSuccess;
871 ReturnMsg MX25_DREAD( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
874 uint8_t addr_4byte_mode;
878 if( flash_address > FlashSize )
return FlashAddressInvalid;
882 addr_4byte_mode = TRUE;
884 addr_4byte_mode = FALSE;
887 dc = GetDummyCycle( DUMMY_CONF_DREAD );
893 SendByte( FLASH_CMD_DREAD, SIO );
894 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
895 InsertDummyCycle( dc );
898 for( index=0; index < byte_length; index++ )
900 *(target_address + index) = GetByte( DIO );
906 return FlashOperationSuccess;
918 ReturnMsg MX25_QREAD( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
921 uint8_t addr_4byte_mode;
925 if( flash_address > FlashSize )
return FlashAddressInvalid;
928 if( IsFlashQIO() != TRUE )
return FlashQuadNotEnable;
932 addr_4byte_mode = TRUE;
934 addr_4byte_mode = FALSE;
937 dc = GetDummyCycle( DUMMY_CONF_QREAD );
943 SendByte( FLASH_CMD_QREAD, SIO );
944 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
945 InsertDummyCycle ( dc );
948 for( index=0; index < byte_length; index=index+1)
950 *(target_address + index) = GetByte( QIO );
956 return FlashOperationSuccess;
967 ReturnMsg MX25_FASTREAD( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
970 uint8_t addr_4byte_mode;
974 if( flash_address > FlashSize )
return FlashAddressInvalid;
978 addr_4byte_mode = TRUE;
980 addr_4byte_mode = FALSE;
982 dc = GetDummyCycle( DUMMY_CONF_FASTREAD );
988 SendByte( FLASH_CMD_FASTREAD, SIO );
989 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
990 InsertDummyCycle ( dc );
993 for( index=0; index < byte_length; index++ )
995 *(target_address + index) = GetByte( SIO );
1001 return FlashOperationSuccess;
1015 ReturnMsg MX25_RDSFDP( uint32_t flash_address, uint8_t *target_address, uint32_t byte_length )
1018 uint8_t addr_4byte_mode;
1021 if( flash_address > FlashSize )
return FlashAddressInvalid;
1024 if( IsFlash4Byte() )
1025 addr_4byte_mode = TRUE;
1027 addr_4byte_mode = FALSE;
1033 SendByte( FLASH_CMD_RDSFDP, SIO );
1034 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1035 InsertDummyCycle ( 8 );
1038 for( index=0; index < byte_length; index++ )
1040 *(target_address + index) = GetByte( SIO );
1046 return FlashOperationSuccess;
1059 ReturnMsg MX25_WREN(
void )
1065 SendByte( FLASH_CMD_WREN, SIO );
1070 return FlashOperationSuccess;
1080 ReturnMsg MX25_WRDI(
void )
1086 SendByte( FLASH_CMD_WRDI, SIO );
1090 return FlashOperationSuccess;
1108 ReturnMsg MX25_PP( uint32_t flash_address, uint8_t *source_address, uint32_t byte_length )
1111 uint8_t addr_4byte_mode;
1114 if( flash_address > FlashSize )
return FlashAddressInvalid;
1117 if( IsFlashBusy() )
return FlashIsBusy;
1120 if( IsFlash4Byte() )
1121 addr_4byte_mode = TRUE;
1123 addr_4byte_mode = FALSE;
1132 SendByte( FLASH_CMD_PP, SIO );
1133 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1137 for( index=0; index < byte_length; index++ )
1139 SendByte( *(source_address + index), SIO );
1145 if( WaitFlashReady( PageProgramCycleTime ) )
1146 return FlashOperationSuccess;
1148 return FlashTimeOut;
1166 ReturnMsg MX25_4PP( uint32_t flash_address, uint8_t *source_address, uint32_t byte_length )
1169 uint8_t addr_4byte_mode;
1172 if( !IsFlashQIO() )
return FlashQuadNotEnable;
1175 if( flash_address > FlashSize )
return FlashAddressInvalid;
1178 if( IsFlashBusy() )
return FlashIsBusy;
1181 if( IsFlash4Byte() )
1182 addr_4byte_mode = TRUE;
1184 addr_4byte_mode = FALSE;
1193 SendByte( FLASH_CMD_4PP, SIO );
1194 SendFlashAddr( flash_address, QIO, addr_4byte_mode );
1197 for( index=0; index < byte_length; index++ )
1199 SendByte( *(source_address + index), QIO );
1205 if( WaitFlashReady( PageProgramCycleTime ) )
1206 return FlashOperationSuccess;
1208 return FlashTimeOut;
1223 ReturnMsg MX25_SE( uint32_t flash_address )
1225 uint8_t addr_4byte_mode;
1228 if( flash_address > FlashSize )
return FlashAddressInvalid;
1231 if( IsFlashBusy() )
return FlashIsBusy;
1234 if( IsFlash4Byte() )
1235 addr_4byte_mode = TRUE;
1237 addr_4byte_mode = FALSE;
1246 SendByte( FLASH_CMD_SE, SIO );
1247 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1252 if( WaitFlashReady( SectorEraseCycleTime ) )
1253 return FlashOperationSuccess;
1255 return FlashTimeOut;
1266 ReturnMsg MX25_BE32K( uint32_t flash_address )
1268 uint8_t addr_4byte_mode;
1271 if( flash_address > FlashSize )
return FlashAddressInvalid;
1274 if( IsFlashBusy() )
return FlashIsBusy;
1277 if( IsFlash4Byte() )
1278 addr_4byte_mode = TRUE;
1280 addr_4byte_mode = FALSE;
1289 SendByte( FLASH_CMD_BE32K, SIO );
1290 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1295 if( WaitFlashReady( BlockErase32KCycleTime ) )
1296 return FlashOperationSuccess;
1298 return FlashTimeOut;
1309 ReturnMsg MX25_BE( uint32_t flash_address )
1311 uint8_t addr_4byte_mode;
1314 if( flash_address > FlashSize )
return FlashAddressInvalid;
1317 if( IsFlashBusy() )
return FlashIsBusy;
1320 if( IsFlash4Byte() )
1321 addr_4byte_mode = TRUE;
1323 addr_4byte_mode = FALSE;
1332 SendByte( FLASH_CMD_BE, SIO );
1333 SendFlashAddr( flash_address, SIO, addr_4byte_mode );
1338 if( WaitFlashReady( BlockEraseCycleTime ) )
1339 return FlashOperationSuccess;
1341 return FlashTimeOut;
1351 ReturnMsg MX25_CE(
void )
1354 if( IsFlashBusy() )
return FlashIsBusy;
1363 SendByte( FLASH_CMD_CE, SIO );
1368 if( WaitFlashReady( ChipEraseCycleTime ) )
1369 return FlashOperationSuccess;
1371 return FlashTimeOut;
1386 ReturnMsg MX25_DP(
void )
1392 SendByte( FLASH_CMD_DP, SIO );
1397 return FlashOperationSuccess;
1407 ReturnMsg MX25_ENSO(
void )
1413 SendByte( FLASH_CMD_ENSO, SIO );
1418 return FlashOperationSuccess;
1427 ReturnMsg MX25_EXSO(
void )
1433 SendByte( FLASH_CMD_EXSO, SIO );
1438 return FlashOperationSuccess;
1448 ReturnMsg MX25_SBL( uint8_t burstconfig )
1454 SendByte( FLASH_CMD_SBL, SIO );
1455 SendByte( burstconfig, SIO );
1460 return FlashOperationSuccess;
1474 ReturnMsg MX25_RSTEN(
void )
1480 SendByte( FLASH_CMD_RSTEN, SIO );
1485 return FlashOperationSuccess;
1501 SendByte( FLASH_CMD_RST, SIO );
1507 fsptr->ArrangeOpt = FALSE;
1508 fsptr->ModeReg = 0x00;
1510 return FlashOperationSuccess;
1529 ReturnMsg MX25_PGM_ERS_S(
void )
1536 SendByte( FLASH_CMD_PGM_ERS_S, SIO );
1541 return FlashOperationSuccess;
1551 ReturnMsg MX25_PGM_ERS_R(
void )
1558 SendByte( FLASH_CMD_PGM_ERS_R, SIO );
1563 return FlashOperationSuccess;
1573 ReturnMsg MX25_NOP(
void )
1579 SendByte( FLASH_CMD_NOP, SIO );
1584 return FlashOperationSuccess;
Clock management unit (CMU) API.
void USART_InitSync(USART_TypeDef *usart, const USART_InitSync_TypeDef *init)
Init USART for synchronous mode.
#define USART_ROUTEPEN_TXPEN
Universal synchronous/asynchronous receiver/transmitter (USART/UART) peripheral API.
#define USART_ROUTELOC0_TXLOC_LOC11
#define USART_ROUTELOC0_RXLOC_LOC11
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
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.
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
#define USART_ROUTELOC0_CLKLOC_LOC11
uint8_t USART_SpiTransfer(USART_TypeDef *usart, uint8_t data)
Perform one 8 bit frame SPI transfer.
#define USART_ROUTEPEN_RXPEN
#define USART_ROUTEPEN_CLKPEN
__STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out port register to 0.
#define USART_INITSYNC_DEFAULT
__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.