Skip to main content
sheng yang
Senior
November 5, 2024
Solved

after enable external flash mapped, couldn't write to external flash.

  • November 5, 2024
  • 5 replies
  • 1165 views

I made a board with ST32H7B0ZBT6, and add a external flash with W25Q128JVS。I found after I enable mapped function, I couldn't write data to external flash any more.

the code with enable mapped func as below:

 

uint8_t W25Qxx_OSPI_Init(void)
{
 OSPI_RegularCmdTypeDef s_command;
 uint16_t w25qxx_id;
 uint8_t value = W25QxJV_FSR_QE;

 if (W25Qxx_OSPI_ResetMemory() != OSPI_OK)
 {
 return OSPI_NOT_SUPPORTED;
 }
 if (W25Qxx_OSPI_WriteEnable() != OSPI_OK)
 {
 return OSPI_ERROR;
 }

 s_command.OperationType = HAL_OSPI_OPTYPE_COMMON_CFG;
 s_command.FlashId = HAL_OSPI_FLASH_ID_1;
 s_command.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE;
 s_command.Instruction = WRITE_STATUS_REG2_CMD;
 s_command.AddressMode = HAL_OSPI_ADDRESS_NONE;
 s_command.AlternateBytesMode = HAL_OSPI_ALTERNATE_BYTES_NONE;
 s_command.DataMode = HAL_OSPI_DATA_1_LINE;
 s_command.DummyCycles = 0;
 s_command.NbData = 1;
 s_command.DataDtrMode = HAL_OSPI_DATA_DTR_DISABLE;
 s_command.SIOOMode = HAL_OSPI_SIOO_INST_EVERY_CMD;
 s_command.InstructionSize = HAL_OSPI_INSTRUCTION_8_BITS;
 s_command.InstructionDtrMode = HAL_OSPI_INSTRUCTION_DTR_DISABLE;
 s_command.AddressDtrMode = HAL_OSPI_ADDRESS_DTR_DISABLE;
 s_command.DQSMode = HAL_OSPI_DQS_DISABLE;

 if (HAL_OSPI_Command(&hospi1, &s_command, HAL_OSPI_TIMEOUT_DEFAULT_VALUE)
 != HAL_OK)
 {
 return OSPI_ERROR;
 }
#if 0
 OSPI_MemoryMappedTypeDef cfg;
 cfg.TimeOutActivation = HAL_OSPI_TIMEOUT_COUNTER_DISABLE;
 if (OSPI_OK != HAL_OSPI_MemoryMapped(&hospi1, &cfg))
 {
 return OSPI_ERROR;
 }
#endif
 if (HAL_OSPI_Transmit(&hospi1, &value, HAL_OSPI_TIMEOUT_DEFAULT_VALUE)
 != HAL_OK)
 {
 return OSPI_ERROR;
 }
 if (W25Qxx_OSPI_AutoPollingMemReady(W25QxJV_SUBSECTOR_ERASE_MAX_TIME) != OSPI_OK)
 {
 return OSPI_ERROR;
 }
 w25qxx_id=W25Qxx_OSPI_FLASH_ReadDeviceID();
 if ( w25qxx_id != W25Qxx_ID)
 return OSPI_ERROR;

#if (W25Q256 == W25Qxx_ID)
 if (W25Qxx_OSPI_Addr_Mode_Init() != OSPI_OK)
 {
 return OSPI_ERROR;
 }
#endif

 return OSPI_OK;
}

 

If I enable these code:

 

#if 0
 OSPI_MemoryMappedTypeDef cfg;
 cfg.TimeOutActivation = HAL_OSPI_TIMEOUT_COUNTER_DISABLE;
 if (OSPI_OK != HAL_OSPI_MemoryMapped(&hospi1, &cfg))
 {
 return OSPI_ERROR;
 }
#endif

 

I will not able to write data to external flash with spi interface. Is there something was wrong?

This topic has been closed for replies.
Best answer by Tesla DeLorean

I think that's explictly what I said, yes.

This is the primary way to exit Memory Mapped Mode, and so the HAL_OSPI commands don't return with ERROR or BUSY indicators. Look at what errors the HAL functions return, perhaps even look a the HAL source code.

The MCU is sending commands to the memory device in Memory Mapped Mode, and it can't do this concurently with you sending slow write/erase commands, which need to complete and come ready.

5 replies

mƎALLEm
ST Technical Moderator
November 5, 2024

Hello @sheng yang ,


@sheng yang wrote:

I couldn't write data to external flash any more.

 

 This statement is confusing. Do you mean that you was able to write data but it stopped working?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
sheng yang
Senior
November 5, 2024

I mean, if I don't mapped the external nor flash, then I can write data to it by HAL_OSPI_xxx releate functions. But if I mapped external flash to memory area 0X90000000 - xxxx, then I couldn't write data to the flash any more.

Tesla DeLorean
Guru
November 5, 2024

Correct,  you can't send commands to the memory whilst in Memory Mapped mode, you will need to use the HAL_OSPI_Abort() to regain control, and then go back into Memory Mapped when finished 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
sheng yang
Senior
November 5, 2024

Did you mean that I must call

HAL_StatusTypeDef HAL_OSPI_Abort(OSPI_HandleTypeDef *hospi)

function when I want to write data to flash by this function as below

uint8_t W25Qxx_OSPI_WritePage(uint8_t *pData, uint32_t WriteAddr, uint32_t Size)
{
 OSPI_RegularCmdTypeDef s_command;

 if (W25Qxx_OSPI_WriteEnable() != OSPI_OK)
 {
 return OSPI_ERROR;
 }

 s_command.OperationType = HAL_OSPI_OPTYPE_COMMON_CFG;
 s_command.FlashId = HAL_OSPI_FLASH_ID_1;
 s_command.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE;
 s_command.Instruction = QUAD_INPUT_PAGE_PROG_CMD;
 s_command.AddressMode = HAL_OSPI_ADDRESS_1_LINE;
#if W25Qxx_ID==W25Q256
 s_command.AddressSize = HAL_OSPI_ADDRESS_32_BITS;
#else
 s_command.AddressSize = HAL_OSPI_ADDRESS_24_BITS;
#endif
 s_command.AlternateBytesMode = HAL_OSPI_ALTERNATE_BYTES_NONE;
 s_command.DataMode = HAL_OSPI_DATA_4_LINES;
 s_command.DummyCycles = 0;
 s_command.DataDtrMode = HAL_OSPI_DATA_DTR_DISABLE;
 s_command.SIOOMode = HAL_OSPI_SIOO_INST_ONLY_FIRST_CMD;
 s_command.InstructionSize = HAL_OSPI_INSTRUCTION_8_BITS;
 s_command.InstructionDtrMode = HAL_OSPI_INSTRUCTION_DTR_DISABLE;
 s_command.AddressDtrMode = HAL_OSPI_ADDRESS_DTR_DISABLE;
 s_command.DQSMode = HAL_OSPI_DQS_DISABLE;
 s_command.Address = WriteAddr;
 s_command.NbData = Size;

 if (HAL_OSPI_Command(&hospi1, &s_command, HAL_OSPI_TIMEOUT_DEFAULT_VALUE)
 != HAL_OK)
 {
 return OSPI_ERROR;
 }

 if (HAL_OSPI_Transmit(&hospi1, pData, HAL_OSPI_TIMEOUT_DEFAULT_VALUE)
 != HAL_OK)
 {
 return OSPI_ERROR;
 }

 if (W25Qxx_OSPI_AutoPollingMemReady(HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != OSPI_OK)
 {
 return OSPI_ERROR;
 }

 return OSPI_OK;
}

when I mapped the external flash before ?

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
November 5, 2024

I think that's explictly what I said, yes.

This is the primary way to exit Memory Mapped Mode, and so the HAL_OSPI commands don't return with ERROR or BUSY indicators. Look at what errors the HAL functions return, perhaps even look a the HAL source code.

The MCU is sending commands to the memory device in Memory Mapped Mode, and it can't do this concurently with you sending slow write/erase commands, which need to complete and come ready.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..