2019-03-06 05:50 AM
It's the first time I'm trying to use the SPI protocol with Flash memory.
Write Program function code:
void Flash_Write_Data(void)
{
uint8_t Write_Enable = 0x06;
uint8_t Page_Program = 0x02;
uint32_t Address = 0x00000000;
uint8_t addrLow = Address & 0xFF;
uint8_t addrMiddle = (Address >> 8);
uint8_t addrHigh = (Address >> 16);
uint8_t Address_buffer[3] = {addrHigh,addrMiddle, addrLow};
uint8_t txData[10] = {0xAB, 0x04, 0x06, 0xC7, 0x04, 0x90, 0x00, 0x00, 0x00, 0x00};
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_RESET); // CS to low
HAL_SPI_Transmit(&hspi2,&Write_Enable,1,1000); // Write Enable Command
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_SET); // CS to high
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_RESET); // CS to low
HAL_SPI_Transmit(&hspi2,&Page_Program,1,1000);// Page Program Command
HAL_SPI_Transmit(&hspi2,Address_buffer,3,1000); // Write Address ( The first address of flash module is 0x00000000 )
HAL_SPI_Transmit(&hspi2,&txData[0],1,1000); // Write 1 byte
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_SET); // CS to high
}
Read Program function Code:
void Flash_Read_Data (void)
{
uint8_t Read_Data = 0x03;
uint32_t Address = 0x00000000;
uint8_t addrLow = Address & 0xFF;
uint8_t addrMiddle = (Address >> 8);
uint8_t addrHigh = (Address >> 16);
uint8_t dummy_Data = 0;
uint8_t Address_buffer[3] = {addrHigh,addrMiddle, addrLow};
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_RESET); // CS low
HAL_SPI_Transmit(&hspi2,&Read_Data,1,1000); // Read Command
HAL_SPI_Transmit(&hspi2,Address_buffer,3,1000); // Write Address ( The first address of flash module is 0x00000000 )
// HAL_SPI_TransmitReceive(&hspi2,&dummy_Data,RxData1,1,1000); // Read 1 byte
HAL_SPI_Receive(&hspi2,RxData1,1,10000); // Read 1 byte
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_SET); // CS to high
}
Erase Program function Code:
void Flash_Erase_Chip(void)
{
uint8_t Write_Enable = 0x06;
uint8_t Erase_Chip = 0xC7;
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_RESET); // CS to low
HAL_SPI_Transmit(&hspi2,&Write_Enable,1,1000); // Write Enable Command
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_SET); // CS to high
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_RESET); // CS to low
HAL_SPI_Transmit(&hspi2,&Erase_Chip,1,1000); // Erase Chip Command
HAL_GPIO_WritePin(ASIF_GPIO_Port,ASIF_Pin,GPIO_PIN_SET); // CS to high
}
Main function code:
HAL_GPIO_WritePin(ASIF_GPIO_Port, ASIF_Pin, GPIO_PIN_SET); //CE#
HAL_GPIO_WritePin(WP_GPIO_Port, WP_Pin, GPIO_PIN_SET); //Write Protection Pin
HAL_GPIO_WritePin(HR_GPIO_Port, HR_Pin, GPIO_PIN_SET); //Hold REset Pin
Flash_Erase_Chip();
HAL_Delay(3); //3ms Delay
Flash_Write_Data();
HAL_Delay(1); //1ms Delay
Flash_Read_Data();
After sending the Read Command 0x3h the MISO slave pin is in High Impedance state always (No Response) using logic analyser.
I'm in the impression that write data is working perfectly please let me know if not.
Help me to resolve the issue. Thanks in advance.
Murali
2019-03-06 06:28 AM
Should probably look at what the CS signal is doing, I suspect HAL_SPI_Transmit() exits before the data transfers, people usually use the TransmitReceive version as it dwells until the data coming back gets clocked in. See HAL SPI example code in the Nucleo BSP files.
2019-03-06 10:11 PM
Thanks for the quick and helpful reply.
The following logic analyser image shows that CE# pin is low until the 8th bit last clock SCLK for HAL_SPI_Receive(&hspi2,RxData1,1,10000) function call.
Also I've checked with HAL_SPI_TransmitReceive(&hspi2,&dummy_Data,RxData1,1,1000) method. In this method i'm sending dummy_Data as 3 but still no response from MISO pin.
Let me know if anything else i missed unknowingly.