/**
 * @file xmc1_flash.c
 * @date 2019-05-04
 *
 * @cond
 *****************************************************************************
 * XMClib v2.2.0 - XMC Peripheral Driver Library
 *
 * Copyright (c) 2015-2020, Infineon Technologies AG
 * All rights reserved.
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * To improve the quality of the software, users are encouraged to share
 * modifications, enhancements or bug fixes with Infineon Technologies AG
 * at XMCSupport@infineon.com.
 *****************************************************************************
 *
 * Change History
 * --------------
 *
 * 2015-02-10:
 *     - Initial <br>
 *
 * 2015-06-20:
 *     - Removed definition of GetDriverVersion API
 *
 * 2015-10-14:
 *     - Fixed defect in API XMC_FLASH_ErasePages, related to the errata NVM_CM.001
 *     - NVM ROM user routine XMC1000_NvmErasePage(address) used for erase page.
 *
 * 2019-05-04:
 *     - Changed XMC_FLASH_ErasePage() and XMC_FLASH_ProgramVerifyPage() to return status of operation
 *     - Changed XMC_FLASH_ErasePages(), XMC_FLASH_EraseSector() and XMC_FLASH_ProgramPage() to return status of operation
 *
 * @endcond
 *
 */

#include "xmc_flash.h"

/*********************************************************************************************************************
 * MACROS
 ********************************************************************************************************************/
#if UC_FAMILY == XMC1

/*********************************************************************************************************************
 * ENUMS
 ********************************************************************************************************************/
/* FLASH programming / erase options */
typedef enum FLASH_ACTION
{
  FLASH_ACTION_IDLE                        =  (uint32_t)0x00,
  FLASH_ACTION_ONESHOT_WRITE_VERIFY        = ((uint32_t)0x51 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_ONESHOT_WRITE               = ((uint32_t)0x91 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_CONTINUOUS_WRITE_VERIFY     = ((uint32_t)0x61 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_CONTINUOUS_WRITE            = ((uint32_t)0xa1 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_ONESHOT_PAGE_ERASE          = ((uint32_t)0x92 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_CONTINUOUS_PAGE_ERASE       = ((uint32_t)0xa2 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_ONESHOT_VERIFY_ONLY         = ((uint32_t)0xd0 << NVM_NVMPROG_ACTION_Pos),
  FLASH_ACTION_CONTINUOUS_VERIFY_ONLY      = ((uint32_t)0xe0 << NVM_NVMPROG_ACTION_Pos)
} FLASH_ACTION_t;


/*********************************************************************************************************************
 * API IMPLEMENTATION
 ********************************************************************************************************************/

/*
 * This API shall clear the ECC and VERIFICATION error status.
 */
void XMC_FLASH_ClearStatus(void)
{
  NVM->NVMPROG |= (uint16_t)((uint16_t)NVM_NVMPROG_RSTVERR_Msk | (uint16_t)NVM_NVMPROG_RSTECC_Msk);
}

/*
 * This API shall return the status of NVM.
 */
uint32_t XMC_FLASH_GetStatus(void)
{
  return NVM->NVMSTATUS;
}

/*
 * This API shall enable the the flash interrupt event.
 */
void XMC_FLASH_EnableEvent(const uint32_t event_msk)
{
  NVM->NVMCONF |= (uint16_t)event_msk;
}

/*
 * This API shall disable the the flash interrupt event.
 */
void XMC_FLASH_DisableEvent(const uint32_t event_msk)
{
  NVM->NVMCONF &= (uint16_t)(~(uint16_t)event_msk);
}

int32_t XMC_FLASH_ErasePage(uint32_t *address)
{
  return XMC1000_NvmErasePage(address);
}

int32_t XMC_FLASH_ProgramVerifyPage(uint32_t *address, const uint32_t *data)
{
  return XMC1000_NvmProgVerify(data, address);
}

/* Write blocks of data into flash*/
void XMC_FLASH_WriteBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks, bool verify)
{
  uint32_t word;
  uint32_t block;

  XMC_ASSERT("XMC_FLASH_WriteBlocks: Starting address not aligned to Block",
             ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)

  /* Configure the continuous Write option command and reset the NVM error / verification status*/
  NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
  NVM->NVMPROG |= (uint16_t)(NVM_NVMPROG_RSTVERR_Msk | NVM_NVMPROG_RSTECC_Msk);

  if (verify == true)
  {
    NVM->NVMPROG |= (uint16_t)FLASH_ACTION_CONTINUOUS_WRITE_VERIFY;
  }
  else
  {
    NVM->NVMPROG |= (uint16_t)FLASH_ACTION_CONTINUOUS_WRITE;
  }

  for (block = 0U; block < num_blocks; ++block)
  {
    for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
    {
      *address = *data;
      data++;
      address++;
    }

    while (XMC_FLASH_IsBusy() == true)
    {
    }
  }

  /* Stop continuous write operation */
  NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
}

/* Erase flash pages */
int32_t XMC_FLASH_ErasePages(uint32_t *address, uint32_t num_pages)
{
  uint32_t page;

  XMC_ASSERT("XMC_FLASH_ErasePages: Starting address not aligned to Page",
             ((uint32_t)address & FLASH_PAGE_ADDR_MASK) == 0U)

  int32_t status = NVM_E_FAIL;
  for (page = 0U; page < num_pages; ++page)
  {
    status = XMC1000_NvmErasePage(address);
    if (status != NVM_PASS)
    {
      return status;
    }

    /* Increment the page address for the next erase */
    address += XMC_FLASH_WORDS_PER_PAGE;
  }

  return status;

}

/* Write multiple data blocks and verify the written data */
void XMC_FLASH_VerifyBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks)
{
  uint32_t word;
  uint32_t block;

  XMC_ASSERT("XMC_FLASH_VerifyBlocks: Starting address not aligned to Block",
             ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)

  /* Configure the Continuous write with verify option command and reset the NVM error / verification status*/
  NVM->NVMPROG &= (uint16_t)~NVM_NVMPROG_ACTION_Msk;
  NVM->NVMPROG |= (uint16_t)((uint16_t)NVM_NVMPROG_RSTVERR_Msk |
                             (uint16_t)NVM_NVMPROG_RSTECC_Msk |
                             (uint16_t)FLASH_ACTION_CONTINUOUS_VERIFY_ONLY);

  for (block = 0U; block < num_blocks; ++block)
  {
    for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
    {
      *address = *data;
      data++;
      address++;
    }

    while (XMC_FLASH_IsBusy() == true)
    {
    }
  }

  /* Stop continuous verify operation */
  NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
}

/* Read data blocks from flash */
void XMC_FLASH_ReadBlocks(uint32_t *address, uint32_t *data, uint32_t num_blocks)
{
  uint32_t word;
  uint32_t block;

  XMC_ASSERT("XMC_FLASH_ReadBlocks: Starting address not aligned to Block",
             ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)

  for (block = 0U; block < num_blocks; ++block)
  {
    for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
    {
      *data = *address;
      data++;
      address++;
    }
  }
}

/* Erase single sector */
int32_t XMC_FLASH_EraseSector(uint32_t *address)
{
  XMC_ASSERT("XMC_FLASH_EraseSector: Starting address not aligned to Sector",
             ((uint32_t)address & FLASH_SECTOR_ADDR_MASK) == 0U)
  return XMC_FLASH_ErasePages(address, XMC_FLASH_PAGES_PER_SECTOR);
}

/* Program single page */
int32_t XMC_FLASH_ProgramPage(uint32_t *address, const uint32_t *data)
{
  return XMC_FLASH_ProgramVerifyPage(address, data);
}

#endif
