As i was beginning with SPI FLASH from winbond W25Q32FV with STM32F103RCT6 CORTEX M3. I am facing a real issue of understanding how things should work.
I am using CUBEMX. First i have selected RCC as crystal/ceramic resonator and configured my clock to 72MHz. Then i configured SPI1 as FULL DUPLEX MASTER. There i got only 3 Pin (PA5 - SCK, PA6- MISO, PA7 - MOSI) so i configured CS pin as GPIO OUTPUT on PA2.
Now to write to flash? What is the first thing I need to do? What are the steps i need to follow?
As long as i refereed to the datasheet first i need to Enable Write(0x06). Then i need to send Page Program(0x02) and then i need to send 24 bit address. Then i need to send at least 1 byte of data. All these procedure will happen when CS is low and then after sending all this CS will be high.
Then i am disabling Write enable, i.e write Disable(0x04).
After then i am trying to read data from that address, So, I send Read Data(0x03) and 24bit address. Then Recieve data in buffer.
Here is the sample code:
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(10);
SPI_TX_BUFF[0] = 0x06;
SPI_TX_BUFF[1] = 0x02;
SPI_TX_BUFF[2] = 0x00;
SPI_TX_BUFF[3] = 0x00;
SPI_TX_BUFF[4] = 0x01;
SPI_TX_BUFF[5] = 0x11;
HAL_SPI_Transmit(&hspi1, SPI_TX_BUFF, 6, 50);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(10);
SPI_TX_BUFF[0] = 0x04;
HAL_SPI_Transmit(&hspi1, SPI_TX_BUFF, 1, 50);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(10);
SPI_TX_BUFF[0] = 0x03;
SPI_TX_BUFF[1] = 0x00;
SPI_TX_BUFF[2] = 0x00;
SPI_TX_BUFF[3] = 0x01;
HAL_SPI_Transmit(&hspi1, SPI_TX_BUFF, 4, 50);
HAL_SPI_Receive(&hspi1, SPI_RX_BUFF, 1, 50);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(100);
This code is not working. More over after flashing the code i can't even enter the debug mode. It says NO TARGET CONNECTED. I know i am doing something massively wrong and need a little guidance. I just need to know what are the steps involved to successfully initiate, write and read from spi flash.
Like I am confused with few stuffs
- Here i am directly sending the Write enable as my first command. Here should i need to send the id first? I mean how to initiate and let know the MCU that he is having a flash connected to the spi pins.
2.How to send 24 bit address? What is the starting address i begin with to write data in flash?
3.When is flash a simple blinky. The MCU works fine but when i flash this code why the alert NO TARGET CONNECTED. Then i have to press reset and erase everything.
Any help will be appreciated.
Thank you in advance.