/******************************************************************************
* void erase_data_flash(unsigned char start_block, unsigned char end_block)
*
* Erases data flash from start_block to end_block, inclusive.  It locks upon
* error.
*
******************************************************************************/
static void erase_data_flash(unsigned char start_block, unsigned char end_block)
{
  uint8_t ret = 0;
  uint8_t loop_counter = 0, block_count = BLOCK_DB0 + start_block;
  uint32_t address = 0x00000000;

  /* Initialise a for loop to erase each of the data flash blocks */
  for (loop_counter = start_block; loop_counter < end_block + 1; loop_counter++)
  {
    /* Fetch beginning address of DF block */
    address = g_flash_BlockAddresses[BLOCK_DB0 + loop_counter];

    /* Erase data flash block */
    ret |= R_FlashErase(block_count);

    /* Check if data flash area is blank */
    ret |= R_FlashDataAreaBlankCheck(address,BLANK_CHECK_ENTIRE_BLOCK);

    /* Increment block counter */
    block_count++;
  }

  /* Check Blank Checking */
  ret |= R_FlashDataAreaBlankCheck(address,BLANK_CHECK_ENTIRE_BLOCK);

  /* Halt in while loop when flash API errors detected */
  while (ret);
}
/*******************************************************************************
* Outline      : Erase_FlashData
* Description  : This function enters a for loop, and erases a block of data
*         flash memory each iteration until all blocks have been erased.
* Argument     : none
* Return value : none
*******************************************************************************/
void Erase_FlashData(void)
{
  /* Declare flash API error flag */
  uint8_t ret = 0;
  
  /* Declare loop count and block count variables */
  uint8_t loop_counter = 0, block_count = BLOCK_DB0;
  
  uint32_t address = 0x00000000;
  
  /* Initialise a for loop to erase each of the data flash blocks */
  for(loop_counter = 0; loop_counter < DF_NUM_BLOCKS; loop_counter++)
    {
        /* Fetch beginning address of DF block */  
        address = block_addresses[BLOCK_DB0 + loop_counter];
        
    /* Erase data flash block */
        ret |=   R_FlashErase
        (
          block_count
        );  
    
    /* Check if data flash area is blank */
        ret |=  R_FlashDataAreaBlankCheck
        (
          address,
          BLANK_CHECK_ENTIRE_BLOCK
        );
                
    /* Increment block counter */
    block_count++;
  }
   
   /* Check Blank Checking */
     ret |= R_FlashDataAreaBlankCheck
       (
        address,
        BLANK_CHECK_ENTIRE_BLOCK
      );
   
   /* Halt in while loop when flash API errors detected */
   //--while(ret);
}