Newer
Older
BIRMM-GT200N / flash.c
/*
** File   : flash.c
**
** Copyright (C) 2013-2019 Gosuncn. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**      http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Author : lijiquan@gosuncn.cn
**
**  $Date: 2018/11/01 08:45:36GMT+08:00 $
**
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gsdk_api.h"
#include "flash.h"
#define DEBUG_LOG(fmt,...) printf("[LOGD]:Function:%s,Line:%d,"fmt,__func__,__LINE__,##__VA_ARGS__);

uint8_t Module_Flash_Read(uint32_t address, uint8_t *buffer, uint32_t length)
{
    gsdk_status_t status;
    gsdk_handle_t hflash;
	int i;
	
	for(i=0; i<5; i++)
	{
		status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash);
		if (status == GSDK_SUCCESS)
		{
			printf("Flash open success!\n");
			break;
		}
		else{
			printf("Flash open failed!\n");
		}
	}
	
	for(i=0; i<5; i++)
	{
		status = gsdk_flash_read(hflash, address, (uint8_t *)buffer, length);
		if (status == GSDK_SUCCESS)
		{
			printf("Flash read success!\n");
			break;
		}
		else{
			printf("Flash read failed!\n");
		}
	}
	
    gsdk_flash_close(hflash);
	if (status == GSDK_SUCCESS)
		return -1;
	else
		return 0;
}

uint8_t Module_Flash_Write(uint32_t address, uint8_t *data, uint32_t length)
{
    char buf[2560];
    gsdk_status_t status;
    gsdk_handle_t hflash;
		
    status = gsdk_flash_open(FLASH_TEST_BASE, FLASH_TEST_SIZE, &hflash);
    if (status != GSDK_SUCCESS)
        goto _fail;
	
    status = gsdk_flash_read(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560);
    if (status != GSDK_SUCCESS)
	{
        goto _fail_close;			
	}
			
	memcpy(buf + address, data, length);		
		
    status = gsdk_flash_erase(hflash, FLASH_TEST_BASE, FLASH_BLOCK_4K);
    if (status != GSDK_SUCCESS)
        goto _fail_close;
	
    status = gsdk_flash_write(hflash, FLASH_TEST_BASE, (uint8_t *)buf, 2560);
    if (status != GSDK_SUCCESS)
        goto _fail_close;

    gsdk_flash_close(hflash);
    return -1;

_fail_close:
	DEBUG_LOG("Flash test failed status is %d\r\n", status);
    gsdk_flash_close(hflash);
_fail:
    printf("[FLASH_DEMO] Flash test failed!\n");
    return 0;
}