---------------------------------------------------------------------
!! Add below code in app_drv_memory.h !!
---------------------------------------------------------------------
    // APP_DRV_MEMORY_STATE_SERVICE_TASKS,
    APP_DRV_MEMORY_STATE_WAIT_ATTACH,
    APP_DRV_MEMORY_STATE_READ,
    APP_DRV_MEMORY_STATE_READ_WAIT_COMPLETE,
    APP_DRV_MEMORY_STATE_WRITE,
    APP_DRV_MEMORY_STATE_WRITE_WAIT_COMPLETE,
    APP_DRV_MEMORY_STATE_ERROR,
    APP_DRV_MEMORY_STATE_IDLE,

---------------------------------------------------------------------


---------------------------------------------------------------------
!! Add below code in app_drv_memory.c !!
---------------------------------------------------------------------
    switch ( app_drv_memoryData.state )
    {
    case APP_DRV_MEMORY_STATE_INIT:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_WAIT_ATTACH;
        break;

    case APP_DRV_MEMORY_STATE_WAIT_ATTACH:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_READ;
        break;

    case APP_DRV_MEMORY_STATE_READ:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_READ_WAIT_COMPLETE;
        break;

    case APP_DRV_MEMORY_STATE_READ_WAIT_COMPLETE:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_WRITE;
        break;

    case APP_DRV_MEMORY_STATE_WRITE:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_WRITE_WAIT_COMPLETE;
        break;

    case APP_DRV_MEMORY_STATE_WRITE_WAIT_COMPLETE:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_IDLE;
        break;

    case APP_DRV_MEMORY_STATE_ERROR:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_IDLE;
        break;

    case APP_DRV_MEMORY_STATE_IDLE:
        break;
    }

---------------------------------------------------------------------
#include "app_drv_usart1.h"
#include "driver/memory/drv_memory.h"
#include <stdio.h>
#include <string.h>

---------------------------------------------------------------------
#define NVM_BLOCK_START                 0
#define NVM_BUFFER_SIZE                 256
#define NVM_GEOMETRY_TABLE_READ_ENTRY   (0)
#define NVM_GEOMETRY_TABLE_WRITE_ENTRY  (1)
#define NVM_GEOMETRY_TABLE_ERASE_ENTRY  (2)
SYS_MEDIA_GEOMETRY *MemoryGeometry = NULL;
int MemoryReadBlocks;
int MemoryWriteBlocks;
int MemoryEraseBlocks;

DRV_HANDLE MemoryHandle;
DRV_MEMORY_COMMAND_HANDLE MemoryReadHandle;
DRV_MEMORY_COMMAND_HANDLE MemoryWriteHandle;
volatile int MemoryWriteComplete = false;
volatile int MemoryReadComplete = false;
char MemoryWriteBuf[NVM_BUFFER_SIZE];
char MemoryReadBuf[NVM_BUFFER_SIZE];
int MemoryResetCount = 1;

---------------------------------------------------------------------
void MemoryTransferHandler( SYS_MEDIA_BLOCK_EVENT event, SYS_MEDIA_BLOCK_COMMAND_HANDLE commandHandle, uintptr_t context )
{
    switch( event )
    {
    case SYS_MEDIA_EVENT_BLOCK_COMMAND_COMPLETE:
        if     (commandHandle == MemoryReadHandle)
        { MemoryReadComplete = true; }
        else if(commandHandle == MemoryWriteHandle)
        { MemoryWriteComplete = true; }
        break;

    case SYS_MEDIA_EVENT_BLOCK_COMMAND_ERROR:
        app_drv_memoryData.state = APP_DRV_MEMORY_STATE_ERROR;
        break;
    }
}

---------------------------------------------------------------------
        MemoryHandle = DRV_MEMORY_Open(DRV_MEMORY_INDEX_0, DRV_IO_INTENT_READWRITE);
        if(MemoryHandle == DRV_HANDLE_INVALID)
        {
            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_ERROR;
            break;
        }

        DRV_MEMORY_TransferHandlerSet(MemoryHandle, MemoryTransferHandler, 0);

---------------------------------------------------------------------
        USART1printf("DRV_MEMORY error!\r\n");

---------------------------------------------------------------------
        if( DRV_MEMORY_IsAttached(MemoryHandle) == false )
            break;
		
        USART1printf("\r\nNVM attached!\r\n");
		
        MemoryGeometry = DRV_MEMORY_GeometryGet(MemoryHandle);
        if (MemoryGeometry == NULL)
        {
            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_ERROR;
            break;
        }
        MemoryReadBlocks  = (NVM_BUFFER_SIZE / MemoryGeometry->geometryTable[NVM_GEOMETRY_TABLE_READ_ENTRY].blockSize);
        MemoryWriteBlocks = (NVM_BUFFER_SIZE / MemoryGeometry->geometryTable[NVM_GEOMETRY_TABLE_WRITE_ENTRY].blockSize);
        MemoryEraseBlocks = (NVM_BUFFER_SIZE / MemoryGeometry->geometryTable[NVM_GEOMETRY_TABLE_ERASE_ENTRY].blockSize);

---------------------------------------------------------------------
        DRV_MEMORY_AsyncRead(MemoryHandle, &MemoryReadHandle, MemoryReadBuf, NVM_BLOCK_START, MemoryReadBlocks);
        if(MemoryReadHandle == DRV_MEMORY_COMMAND_HANDLE_INVALID)
        {
            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_ERROR;
            break;
        }

---------------------------------------------------------------------
        if(MemoryReadComplete == true)
        {
            MemoryReadComplete = false;

            if( !strncmp((char*)MemoryReadBuf, "Count NVM: ", 11) )
            {
                USART1printf("%s\r\n", MemoryReadBuf);
                sscanf(MemoryReadBuf, "Count NVM: %d", &MemoryResetCount);
                MemoryResetCount = (MemoryResetCount+1)%10;
            }
            else
            {
                USART1printf("Geometry NVM: Buff(%d), R(%d), W(%d), E(%d)\r\n",
                                  NVM_BUFFER_SIZE, MemoryReadBlocks, MemoryWriteBlocks, MemoryEraseBlocks);
            }

            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_WRITE;				
        }

---------------------------------------------------------------------
        sprintf( MemoryWriteBuf, "Count NVM: %d", MemoryResetCount);
        DRV_MEMORY_AsyncEraseWrite(MemoryHandle, &MemoryWriteHandle, MemoryWriteBuf, NVM_BLOCK_START, MemoryWriteBlocks);

        if(MemoryWriteHandle == DRV_MEMORY_COMMAND_HANDLE_INVALID)
        {
            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_ERROR;
        }
        else
        {
            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_WRITE_WAIT_COMPLETE;
        }

---------------------------------------------------------------------
        if(MemoryWriteComplete == true)
        {
            MemoryWriteComplete = false;

            DRV_MEMORY_Close( MemoryHandle );

            app_drv_memoryData.state = APP_DRV_MEMORY_STATE_IDLE;
        }

---------------------------------------------------------------------
