/**
  ******************************************************************************
  * @file     Flash_user.c
  * @author   SINOMCU-AE
  * @brief    Flash user function
  *    
  *          This file provides functions flash one page write and read test. 
  *     
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */
  
#include "stdio.h"
#include "Flash_user.h"

#define MAIN_FLASH_PAGE_MAX        (31)                    // MS32F031 32K(page 0-31)
#define PAGE_ADD_CAL_NUM(address)  (address >> 10)         // page address calculation page NUM

uint8_t g_falsh_test_write_buffer[TEST_FLASH_BUFF_LEN] ={"MS32F031,FlashTest."};
uint8_t g_falsh_test_read_buffer[TEST_FLASH_BUFF_LEN]  ={0};

/**
  * @brief  test flash one page write and read.
  * @param  None
  * @retval opration result:SUCCESS,ERROR
  */
ErrorStatus Flash_Write_Read_test( void ) {
  ErrorStatus state =ERROR;
  uint32_t page_address = FLASH_RW_TEST_PAGE_ADD;
  uint16_t write_read_len = TEST_FLASH_BUFF_LEN;
  uint8_t page_num = MAIN_FLASH_PAGE_MAX;  
  uint16_t count=0;
    
  page_num = PAGE_ADD_CAL_NUM(page_address);               // page num for PageErase function
  state = MS32_FLASH_PageErase(page_num);
  if(state == ERROR) {
    printf("\r\n**Error:Flash page erase error,page address:0x0%7X",page_address);
  }
  
  printf("\r\n--Inf: Write Data:%s",g_falsh_test_write_buffer);  
  state = MS32_FLASH_Write(page_address, g_falsh_test_write_buffer, write_read_len); 
  if(state == ERROR) {
    printf("\r\n**Error:Flash page write error,page address:0x0%7X",page_address);
  }

  state = MS32_FLASH_Read(page_address, g_falsh_test_read_buffer, write_read_len); 
  if(state == ERROR) {
    printf("\r\n**Error:Flash page read error,page address :0x0%7X",page_address);
  }  
  printf("\r\n--Inf: Read Data :%s",g_falsh_test_read_buffer);
  
  state = SUCCESS;
  for(count=0; count < write_read_len ; count++) {         // compare read back with write
    if( g_falsh_test_read_buffer[count] != g_falsh_test_read_buffer[count]) {
      state = ERROR;
      //break;
    }
  }
  
  if( state == ERROR ){
    printf("\r\n**Error:Flash Write Read Error!");
  }
  else {
    printf("\r\n--Inf: Flash write read successful.");  
  }
  
  return state;  
}

/******************************** END OF FILE *********************************/
