Dear All,
For my case, I am using W25X16 SPI Flash in SSI1 Module of LM4F.
I wanna transmit one byte (0xAB) to spi flash, and the register address of flash is 0x000000. And after transmission, I also want to receive the data I sent before.
But unlucky, I can not receive the data I send. The code as below, plz help me to check it :
Thank you in advance.
void Timer0_ISR( void ) ;
void Init_SPI( void ) ;
void SendByte( unsigned char data ) ;
void ReceiveByte( void ) ;
#define W25X16_CS_0 GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, 0x00 ) ;
#define W25X16_CS_1 GPIOPinWrite( GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3 ) ;
volatile char GetData = 0 ;
volatile long Cnt32_1 = 0, ReadData = 0 ;
//================================================================//
void SendByte( unsigned char data )
{
unsigned long NullData ;
SSIDataPut( SSI1_BASE, data ) ;
SSIDataGet( SSI1_BASE, &NullData ) ;
}
//----------------------------------------------------------------//
void ReceiveByte( void )
{
SSIDataPut( SSI1_BASE, 0xFF ) ;
SSIDataGet( SSI1_BASE, &ReadData ) ;
}
//----------------------------------------------------------------//
void Init_SPI( void )
{
// Using Winbond SPI Flash W25X16BV ( 2M Bytes )
// PF0 (RX) --> DO ( MISO)
// PF1 (TX) --> DIO (MOSI)
// PF2 (Clk) --> CLK
// PF3 (Fss) --> /CS
//========================<Initial>========================//
SysCtlPeripheralEnable( SYSCTL_PERIPH_SSI1 ) ;
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOF ) ;
GPIOPinConfigure( GPIO_PF0_SSI1RX ) ;
GPIOPinConfigure( GPIO_PF1_SSI1TX ) ;
GPIOPinConfigure( GPIO_PF2_SSI1CLK ) ;
GPIOPinTypeSSI( GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 ) ;
GPIODirModeSet( GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_DIR_MODE_OUT ) ;
GPIOPadConfigSet( GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD ) ;
SSIConfigSetExpClk( SSI1_BASE, SysCtlClockGet( ), SSI_FRF_MOTO_MODE_0, SSI_MODE_MASTER, 1000000, 8 ) ;
SSIEnable( SSI1_BASE ) ;
W25X16_CS_1 ;
//========================<Write>========================//
// Step 1.Program Enable
W25X16_CS_0 ;
SendByte( 0x06 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
W25X16_CS_1 ;
// Step 2.Sector Erase
W25X16_CS_0 ;
SendByte( 0x20 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
W25X16_CS_1 ;
do
{
W25X16_CS_0 ;
SendByte( 0x50 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
ReceiveByte( ) ;
W25X16_CS_1 ;
}
while( ReadData == 0x03 ) ;
// Step 3.Program Enable
W25X16_CS_0 ;
SendByte( 0x06 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
W25X16_CS_1 ;
// Step 4.Page Program
W25X16_CS_0 ;
SendByte( 0x02 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0xAB ) ;
while( SSIBusy( SSI1_BASE ) ) { }
W25X16_CS_1 ;
do
{
W25X16_CS_0 ;
SendByte( 0x50 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
ReceiveByte( ) ;
W25X16_CS_1 ;
}
while( ReadData == 0x03 ) ;
//========================<Read>========================//
W25X16_CS_0 ;
SendByte( 0x0B ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0x00 ) ;
while( SSIBusy( SSI1_BASE ) ) { }
SendByte( 0xFF ) ; // Make 8-clock
while( SSIBusy( SSI1_BASE ) ) { }
ReceiveByte( ) ;
while( SSIBusy( SSI1_BASE ) ) { }
W25X16_CS_1 ;
GetData = ReadData ; // Receive the data I sent before.
}
//----------------------------------------------------------------//
void Timer0_ISR( void )
{
TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT ) ;
TimerLoadSet(TIMER0_BASE, TIMER_A, 80000);
Init_SPI( ) ;
}
//================================================================//
void main( void )
{
// Init Clk
SysCtlClockSet( SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN ) ;
// Init Timer
SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 ) ;
TimerConfigure( TIMER0_BASE, TIMER_CFG_PERIODIC ) ;
TimerLoadSet(TIMER0_BASE, TIMER_A, 80000);
TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0_ISR ) ;
TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT ) ;
IntEnable(INT_TIMER0A);
TimerEnable( TIMER0_BASE, TIMER_A ) ;
while( 1 )
{
;
}
}