STM/STM32_LIBRARIES/tm_stm32_qspiflash.c
uint8_t BSP_QSPI_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size) {
	QSPI_CommandTypeDef s_command;
	uint32_t end_addr, current_size, current_addr;

	/* Calculation of the size between the write address and the end of the page */
	current_addr = 0;

	while (current_addr <= WriteAddr) {
		current_addr += N25Q128A_PAGE_SIZE;
	}
	current_size = current_addr - WriteAddr;

	/* Check if the size of the data is less than the remaining place in the page */
	if (current_size > Size) {
		current_size = Size;
	}

	/* Initialize the address variables */
	current_addr = WriteAddr;
	end_addr = WriteAddr + Size;

	/* Initialize the program command */
	s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
	s_command.Instruction       = EXT_QUAD_IN_FAST_PROG_CMD;
	s_command.AddressMode       = QSPI_ADDRESS_4_LINES;
	s_command.AddressSize       = QSPI_ADDRESS_24_BITS;
	s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
	s_command.DataMode          = QSPI_DATA_4_LINES;
	s_command.DummyCycles       = 0;
	s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
	s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
	s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

	/* Perform the write page by page */
	do {
		s_command.Address = current_addr;
		s_command.NbData  = current_size;

		/* Enable write operations */
		if (QSPI_WriteEnable(&QSPIHandle) != QSPI_OK) {
			return QSPI_ERROR;
		}

		/* Configure the command */
		if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
			return QSPI_ERROR;
		}

		/* Transmission of the data */
		if (HAL_QSPI_Transmit(&QSPIHandle, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
			return QSPI_ERROR;
		}

		/* Configure automatic polling mode to wait for end of program */
		if (QSPI_AutoPollingMemReady(&QSPIHandle, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK) {
			return QSPI_ERROR;
		}

		/* Update the address and size variables for next page programming */
		current_addr += current_size;
		pData += current_size;
		current_size = ((current_addr + N25Q128A_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : N25Q128A_PAGE_SIZE;
	} while (current_addr < end_addr);

	return QSPI_OK;
}
/**
  * @brief  Writes an amount of data to the QSPI memory.
  * @param  pData: Pointer to data to be written
  * @param  WriteAddr: Write start address
  * @param  Size: Size of data to write
  * @retval QSPI memory status
  */
@brief  Writes an amount of data to the QSPI memory.
@param  pData: Pointer to data to be written
@param  WriteAddr: Write start address
@param  Size: Size of data to write
@retval QSPI memory status
@brief Writes an amount of data to the QSPI memory . @param pData : Pointer to data to be written @param WriteAddr : Write start address @param Size : Size of data to write @retval QSPI memory status
uint8_t BSP_QSPI_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size) {
	QSPI_CommandTypeDef s_command;
	uint32_t end_addr, current_size, current_addr;
	current_addr = 0;
	while (current_addr <= WriteAddr) {
		current_addr += N25Q128A_PAGE_SIZE;
	}
	current_size = current_addr - WriteAddr;
	if (current_size > Size) {
		current_size = Size;
	}
	current_addr = WriteAddr;
	end_addr = WriteAddr + Size;
	s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
	s_command.Instruction       = EXT_QUAD_IN_FAST_PROG_CMD;
	s_command.AddressMode       = QSPI_ADDRESS_4_LINES;
	s_command.AddressSize       = QSPI_ADDRESS_24_BITS;
	s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
	s_command.DataMode          = QSPI_DATA_4_LINES;
	s_command.DummyCycles       = 0;
	s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
	s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
	s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;
	do {
		s_command.Address = current_addr;
		s_command.NbData  = current_size;
		if (QSPI_WriteEnable(&QSPIHandle) != QSPI_OK) {
			return QSPI_ERROR;
		}
		if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
			return QSPI_ERROR;
		}
		if (HAL_QSPI_Transmit(&QSPIHandle, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
			return QSPI_ERROR;
		}
		if (QSPI_AutoPollingMemReady(&QSPIHandle, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK) {
			return QSPI_ERROR;
		}
		current_addr += current_size;
		pData += current_size;
		current_size = ((current_addr + N25Q128A_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : N25Q128A_PAGE_SIZE;
	} while (current_addr < end_addr);
	return QSPI_OK;
}
uint8_t BSP_QSPI_Write ( uint8_t * pData , uint32_t WriteAddr , uint32_t Size ) { QSPI_CommandTypeDef s_command ; uint32_t end_addr , current_size , current_addr ; current_addr = 0 ; while ( current_addr <= WriteAddr ) { current_addr += N25Q128A_PAGE_SIZE ; } current_size = current_addr - WriteAddr ; if ( current_size > Size ) { current_size = Size ; } current_addr = WriteAddr ; end_addr = WriteAddr + Size ; s_command . InstructionMode = QSPI_INSTRUCTION_1_LINE ; s_command . Instruction = EXT_QUAD_IN_FAST_PROG_CMD ; s_command . AddressMode = QSPI_ADDRESS_4_LINES ; s_command . AddressSize = QSPI_ADDRESS_24_BITS ; s_command . AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE ; s_command . DataMode = QSPI_DATA_4_LINES ; s_command . DummyCycles = 0 ; s_command . DdrMode = QSPI_DDR_MODE_DISABLE ; s_command . DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY ; s_command . SIOOMode = QSPI_SIOO_INST_EVERY_CMD ; do { s_command . Address = current_addr ; s_command . NbData = current_size ; if ( QSPI_WriteEnable ( & QSPIHandle ) != QSPI_OK ) { return QSPI_ERROR ; } if ( HAL_QSPI_Command ( & QSPIHandle , & s_command , HAL_QPSI_TIMEOUT_DEFAULT_VALUE ) != HAL_OK ) { return QSPI_ERROR ; } if ( HAL_QSPI_Transmit ( & QSPIHandle , pData , HAL_QPSI_TIMEOUT_DEFAULT_VALUE ) != HAL_OK ) { return QSPI_ERROR ; } if ( QSPI_AutoPollingMemReady ( & QSPIHandle , HAL_QPSI_TIMEOUT_DEFAULT_VALUE ) != QSPI_OK ) { return QSPI_ERROR ; } current_addr += current_size ; pData += current_size ; current_size = ( ( current_addr + N25Q128A_PAGE_SIZE ) > end_addr ) ? ( end_addr - current_addr ) : N25Q128A_PAGE_SIZE ; } while ( current_addr < end_addr ) ; return QSPI_OK ; }
@brief  Writes an amount of data to the QSPI memory.
@brief Writes an amount of data to the QSPI memory .
/* Calculation of the size between the write address and the end of the page */ /* Check if the size of the data is less than the remaining place in the page */ /* Initialize the address variables */ /* Initialize the program command */ /* Perform the write page by page */ /* Enable write operations */ /* Configure the command */ /* Transmission of the data */ /* Configure automatic polling mode to wait for end of program */ /* Update the address and size variables for next page programming */