Hello Everyone,
I am trying to interface winbond SPI Flash w25q16 with STM32F103RC using CubeMX.
After doing some research i chosen these two links as for my reference.
Here is the link i am following:
https://electronics.stackexchange.com/questions/51229/how-do-i-write-to-spi-flash-memory
from Jeff Walther
and this link for sample program
https://github.com/mhollands/Winbond-W25Q32JV/blob/master/main.cpp
Here is my code:
And this is the main
SPI init
I am comparing the value after reading and blinking the LED. But i am not getting any result. I don't know where i am going wrong.
Any help will be appreciated
Thanks in advance.
I am trying to interface winbond SPI Flash w25q16 with STM32F103RC using CubeMX.
After doing some research i chosen these two links as for my reference.
Here is the link i am following:
https://electronics.stackexchange.com/questions/51229/how-do-i-write-to-spi-flash-memory
from Jeff Walther
and this link for sample program
https://github.com/mhollands/Winbond-W25Q32JV/blob/master/main.cpp
Here is my code:
C:
uint8_tCheck_Status_Flag(){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);//CS Low
HAL_Delay(10);
uint8_t txBuff[1],rxBuff[1];
txBuff[0]=0x05;//command for checking status
HAL_SPI_Transmit(&hspi1, txBuff,1,100);//send command
txBuff[0]=0x00;
HAL_SPI_Transmit(&hspi1, txBuff,1,100);//send dummy value
HAL_SPI_Receive(&hspi1, rxBuff,1,100);//receive status
return rxBuff[0];}
voidWrite_Enable(){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);//CS Low
HAL_Delay(10);
uint8_t txBuff[1],rxBuff[1];
txBuff[0]=0x06;//command for write enable
HAL_SPI_Transmit(&hspi1, txBuff,1,100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);//CS High
HAL_Delay(10);
while((Check_Status_Flag()&0x02)!=0x00);}
voidSector_Erase(uint8_t addr1,uint8_t addr2,uint8_t addr3){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);//CS Low
HAL_Delay(10);
uint8_t txBuff[4],rxBuff[4];
txBuff[0]=0x20;//command for sector erase
txBuff[1]= addr1;//MSB of the address
txBuff[2]= addr2;//middle address
txBuff[3]= addr3;//LSB of the address
HAL_SPI_Transmit(&hspi1, txBuff,4,100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);//CS High
HAL_Delay(10);
while((Check_Status_Flag()&0x01)!=0x00);}
voidWrite_Data(uint8_t addr1,uint8_t addr2,uint8_t addr3,uint8_t data){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);//CS Low
HAL_Delay(10);
uint8_t txBuff[5],rxBuff[5];
txBuff[0]=0x02;//command for write data
txBuff[1]= addr1;//MSB of the address
txBuff[2]= addr2;//middle address
txBuff[3]= addr3;//LSB of the address
txBuff[4]= data;
HAL_SPI_Transmit(&hspi1, txBuff,5,100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);//CS High
HAL_Delay(10);
while((Check_Status_Flag()&0x01)!=0x00);}
voidRead_Data(uint8_t addr1,uint8_t addr2,uint8_t addr3){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);//CS Low
HAL_Delay(10);
uint8_t txBuff[5],rxBuff[5];
txBuff[0]=0x03;//command for read data
txBuff[1]= addr1;//MSB of the address
txBuff[2]= addr2;//middle address
txBuff[3]= addr3;//LSB of the address
HAL_SPI_Transmit(&hspi1, txBuff,4,100);
HAL_SPI_Receive(&hspi1, rxBuff,4,100);//receiving 4 garbage
txBuff[0]=0x00;
HAL_SPI_Transmit(&hspi1, txBuff,1,100);//transmitting dummy
HAL_SPI_Receive(&hspi1, rxBuff,1,100);//receiving the data uint8_t data = rxBuff[0];//storing the data
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);//CS High
HAL_Delay(10);
if (rxBuff[0] == 0x11)
//Blink LED
while((Check_Status_Flag()&0x01)!=0x00);}
Code:
int main(void){
HAL_Init();SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);//CS High
HAL_Delay(10);Write_Enable();Sector_Erase(0x00,0x10,0x00);Write_Enable();Write_Data(0x00,0x00,0x01,0x11);Read_Data(0x00,0x00,0x01);
while(1){
}
}
C:
staticvoid MX_SPI1_Init(void){
/* SPI1 parameter configuration*/
hspi1.Instance= SPI1;
hspi1.Init.Mode= SPI_MODE_MASTER;
hspi1.Init.Direction= SPI_DIRECTION_2LINES;
hspi1.Init.DataSize= SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity= SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase= SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler= SPI_BAUDRATEPRESCALER_64;
hspi1.Init.FirstBit= SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode= SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation= SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial=10;if(HAL_SPI_Init(&hspi1)!= HAL_OK){_Error_Handler(__FILE__, __LINE__);}
}
I am comparing the value after reading and blinking the LED. But i am not getting any result. I don't know where i am going wrong.
Any help will be appreciated
Thanks in advance.