1 Reply Latest reply on Aug 28, 2019 2:34 PM by BushraH_91

    reg erase the sectors which is already programmed - S25FL256S

    ViV_4156156

      Hi,

          i need to erase the sector which is already programmed . the erase command seems to work. I need some guidance in order to save

      or backup/create a copy of already present data say if i need to write same sector in different address location.

      say i have written from 0 to 40 now i need to write from 40 - 80. It would be of great help if it is explained out here.

       

       

      void spi_flash_secure_region_program (uint16_t address, uint8_t *p_data_bytes, uint16_t size)
      {
      static uint16_t number_of_pages, size_of_data, last_data_block_size, counter;

      int i = 0;

      uint16_t startSctrAddr = address & 0x0000FFFF;
      uint16_t stopSctrAddr = (address + size) & 0x0000FFFF;

      int numSctrs = 1;

      if (startSctrAddr != stopSctrAddr)  // Data to be written over sector boundary?
      numSctrs += 1;

      for (i = 0; i < numSctrs; i++)
      {
        spi_flash_secure_region_erase(startSctrAddr + (i * FLASH_SECURE_REGION_SIZE_4K)); // FLASH_SECURE_REGION_SIZE_4K = 0x400
      }

      size_of_data = size;

      if(size_of_data <= 1024)
      {
        if (size_of_data % 256)
        {
         number_of_pages = (size_of_data / 256) + 1;
         last_data_block_size = size_of_data % 256;
        }
        else
        {
         number_of_pages = (size_of_data / 256);
         last_data_block_size = 256;
        }

        address &= 0x0000FFFF; // address must be aligned to 256 byte !

        for ( counter = 0; counter < number_of_pages - 1; counter++ )
        {
         spi_flash_secure_region_page_program ((address + 256*counter) ,(p_data_bytes +256*counter), 256);
        }
        // last page
        spi_flash_secure_region_page_program ((address + 256*(number_of_pages - 1)) ,(p_data_bytes +256*(number_of_pages - 1)), last_data_block_size);
      }

      }

       

      void spi_flash_program (uint32_t address, uint8_t *p_data_bytes, uint32_t size)
      {
      static uint32_t number_of_pages, size_of_data, last_data_block_size, counter;
      int i = 0;

      uint32_t startSctrAddr = address & 0xff000;
      uint32_t stopSctrAddr = (address + size) & 0xff000;

      int numSctrs = 1;

      if (startSctrAddr != stopSctrAddr)  // Data to be written over sector boundary?
      numSctrs += 1;

      for (i = 0; i < numSctrs; i++)
      {
          spi_flash_sector_erase(startSctrAddr + (i * FLASH_SCTR_SIZE_4K)); // FLASH_SCTR_SIZE_4K - 0x1000
      }

      size_of_data = size;

      if (size_of_data % 256)
      {
        number_of_pages = (size_of_data / 256) + 1;
        last_data_block_size = size_of_data % 256;
      }
      else
      {
        number_of_pages = (size_of_data / 256);
        last_data_block_size = 256;
      }


      address &= 0x00FFFF00; // address must be aligned to 256 byte !

       

      for ( counter = 0; counter < number_of_pages - 1; counter++ )
      {
        spi_flash_page_program ((address + 256*counter) ,(p_data_bytes +256*counter), 256);
      }
      // last page
      spi_flash_page_program ((address + 256*(number_of_pages - 1)) ,(p_data_bytes +256*(number_of_pages - 1)), last_data_block_size);

      }

       

      void spi_flash_sector_erase(uint32_t address)
      {
      uint8_t status, dummy_var;
      uint8_t addr0, addr1, addr2,addr3;
      (void)(dummy_var);
      addr0 = (uint8_t) (address & 0x000000FF);
      addr1 = (uint8_t) ((address & 0x0000FF00) >> 8);
      addr2 = (uint8_t) ((address & 0x00FF0000) >> 16);
      addr3 = (uint8_t) ((address & 0xFF000000) >> 24);

      ALLOW_TRANSFER; // if previous command has set EOQ

      PUSH_ENQ(WREN);   // write enable command (must be done before every write !!!)
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      ALLOW_TRANSFER; // from this point all following write to PUSHR will be sent

      PUSH(SE); // Sector erase command
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var); // discard one RxFIFO item

      PUSH(addr3); // most-significant byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      PUSH(addr2); // most-significant byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      PUSH(addr1); // middle byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      PUSH_ENQ(addr0); // least-significant byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      do
      {
        status = spi_flash_read_status_register();
      }
      while (status & WIP); // wait here until bulk erase is done
      }

      void spi_flash_secure_region_erase(uint16_t address)
      {
      uint8_t status, dummy_var;
      uint8_t addr0, addr1, addr2,addr3;
      (void)(dummy_var);
      addr0 = (uint8_t) (address & 0x000000FF);
      addr1 = (uint8_t) ((address & 0x0000FF00) >> 8);
      addr2 = 0;

      ALLOW_TRANSFER; // if previous command has set EOQ

      PUSH_ENQ(WREN);   // write enable command (must be done before every write !!!)
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      ALLOW_TRANSFER; // from this point all following write to PUSHR will be sent

      PUSH(SECRE); // Secured region erase command
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var); // discard one RxFIFO item

      PUSH(addr2); // most-significant byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      PUSH(addr1); // middle byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      PUSH_ENQ(addr0); // least-significant byte of address
      WAIT_UNTIL_RxFIFO_IS_NOT_EMPTY;
      POP(dummy_var);

      do
      {
        status = spi_flash_read_status_register();
      }
      while (status & WIP); // wait here until sector erase is done
      }

       

       

       

      Share

      Show less

      Actions Hide new activity