Skip to content

Instantly share code, notes, and snippets.

@itrobotics
Last active February 7, 2022 15:43
Show Gist options
  • Save itrobotics/94a0bd1b1f1c04f3b791 to your computer and use it in GitHub Desktop.
Save itrobotics/94a0bd1b1f1c04f3b791 to your computer and use it in GitHub Desktop.
void SendByte( uint8 byte_value )
{
uint16 i;
uint8 cycle_cnt;
cycle_cnt = 8;
for( i= 0; i < cycle_cnt; i++ )
{
if ( (byte_value & IO_MASK) == 0x80 ){
SI = 1;
}
else{
SI = 0;
}
SCLK = 0;
byte_value = byte_value << 1;
SCLK = 1;
}
}
uint8 GetByte( )
{
uint16 i;
uint8 cycle_cnt;
uint8 data_buf;
data_buf = 0;
cycle_cnt = 8;
for( i= 0; i < cycle_cnt; i++ )
{
SCLK = 0;
if ( SO == 1 ){
data_buf = (data_buf | (0x80 >> i));
}
SCLK = 1;
}
return data_buf;
}
ReturnMsg CMD_PP( uint32 flash_address, uint8 *source_address, uint32 byte_length )
{
uint32 index;
uint8 addr_4byte_mode;
if( flash_address > FlashSize ) return FlashAddressInvalid;
// Check flash is busy or not
if( IsFlashBusy() ) return FlashIsBusy;
// Setting Write Enable Latch bit
CMD_WREN();
CS_Low();
// Write Page Program command
SendByte( FLASH_CMD_PP, SIO );
SendFlashAddr( flash_address, SIO);
// Set a loop to down load whole page data into flash's buffer
// Note: only last 256 byte will be programmed
for( index=0; index < byte_length; index++ )
{
SendByte( *(source_address + index), SIO );
}
CS_High();
if( WaitFlashReady( PageProgramCycleTime ) )
return FlashOperationSuccess;
else
return FlashTimeOut;
}
ReturnMsg CMD_RDID( uint32 *Identification )
{
uint32 temp;
uint8 gDataBuffer[3];
// Chip select go low to start a flash command
CS_Low();
// Send command
#define FLASH_CMD_RDID 0x9F //RDID (Read Identification)
SendByte( FLASH_CMD_RDID);
// Get manufacturer identification, device identification
gDataBuffer[0] = GetByte( );
gDataBuffer[1] = GetByte( );
gDataBuffer[2] = GetByte( );
// Chip select go high to end a command
CS_High();
// Store identification
temp = gDataBuffer[0];
temp = (temp << 8) | gDataBuffer[1];
*Identification = (temp << 8) | gDataBuffer[2];
// Flash ID:C22013
return FlashOperationSuccess;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment