• AVR Freaks

Hot!NVM read example not working on PIC32MZ2064DAG169

Author
hermallorn
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2016/08/30 01:55:52
  • Location: 0
  • Status: offline
2020/07/02 07:42:29 (permalink)
0

NVM read example not working on PIC32MZ2064DAG169

I am using the MEBII expansion board equiped with a PIC32MZ2064DAG169
 
We need to implemente a last minute log inside the flash memory of the pic because of some bugs that occured during deployement... I thought i would give it a go with NVM but the example provided by harmony does not work.
 
Simple test code generated with harmony 2.06.
I generated the application code for NVM read and write
I only activated the FS (SYS_FS_MEDIA_TYPE_NVM), FAT, create FAT12, FAT file Systems
I activated NVM driver (enabled erase-write, register with FS)
 
The write code example works fine.
In the read code example, the FileOpen return an invalid handle.
"FileHandle = SYS_FS_FileOpen(appNVMReadPath, SYS_FS_FILE_OPEN_READ);"
because of this code in sys_fs.c:
"fileStatus = disk->fsFunctions->open((uintptr_t)&fileObj->nativeFSFileObj, (const char *)pathWithDiskNo, mode);"
that returns fileStatus = 4
 
Does anyone has a clue? Thanks. Full code below
 
// Section: Included Files 
// *****************************************************************************
// *****************************************************************************

#include "app.h"
#include "bsp.h"

// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    This structure should be initialized by the APP_Initialize function.
    
    Application strings and buffers are be defined outside this structure.
*/

APP_DATA appData;

static char appNVMWritePath[] = "/mnt/myDrive1/filename.txt";
static uint8_t __attribute__ ((aligned (16))) appNVMWriteData[] = "Hello World";

static char appNVMReadPath[] = "/mnt/myDrive1/filename.txt";
static uint8_t __attribute__ ((aligned (16))) appNVMReadData[20];

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************

/* TODO: Add any necessary callback functions.
*/

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************

/* state machine for the File System */
static void APP_NVM_WRITE_Task(void)
{
    static SYS_FS_ERROR FsError;
    static uint8_t HandleAttempts;
    static uint32_t TotalBytesWritten;
    static SYS_FS_HANDLE FileHandle;

    switch (appData.nvmWriteStateMachine)
    {
        default:
        case APP_NVM_WRITE_STATE_START:
            HandleAttempts = 0;
            appData.nvmWriteStateMachine = APP_NVM_WRITE_STATE_OPEN;
            break;

        case APP_NVM_WRITE_STATE_OPEN:
            /* open the file to write - create file if it does not exist */
            FileHandle = SYS_FS_FileOpen(appNVMWritePath, SYS_FS_FILE_OPEN_WRITE_PLUS);
            test = FileHandle;//WRITE
            if (FileHandle == SYS_FS_HANDLE_INVALID)
            {
                /* try 10 times to get the handle while auto-mount happens */
                if (++HandleAttempts > 10)
                {
                    FsError = SYS_FS_Error();
                    appData.nvmWriteStateMachine = APP_NVM_WRITE_STATE_ERROR;
                }
            }
            else
            {
                TotalBytesWritten = 0;
                appData.nvmWriteStateMachine = APP_NVM_WRITE_STATE_FILEOPS;
            }
            break;

        case APP_NVM_WRITE_STATE_FILEOPS:
            {
                size_t BytesWritten = SYS_FS_FileWrite(FileHandle, (const void *)appNVMWriteData,
                    sizeof(appNVMWriteData) - TotalBytesWritten);
                if (BytesWritten == (size_t)-1)
                {
                    /* error */
                    FsError = SYS_FS_Error();
                    appData.nvmWriteStateMachine = APP_NVM_WRITE_STATE_ERROR;
                    return;
                }
                else
                {
                    TotalBytesWritten += BytesWritten;
                    /* have we written all the bytes now? */
                    if (TotalBytesWritten >= sizeof(appNVMWriteData))
                    {
                        SYS_FS_FileClose(FileHandle);
                        appData.nvmWriteStateMachine = APP_NVM_WRITE_STATE_DONE;
                    }
                }
            }
            break;

        case APP_NVM_WRITE_STATE_DONE:
            (void)FsError;
            BSP_LEDOn(BSP_LED_2);
            break;

        /* if we get to this state - something went wrong - check FsError for cause */
        case APP_NVM_WRITE_STATE_ERROR:
            break;
    }
}

/* state machine for the File System */
static void APP_NVM_READ_Task(void)
{
    static SYS_FS_ERROR FsError;
    static uint8_t HandleAttempts;
    static uint32_t TotalBytesRead;
    static SYS_FS_HANDLE FileHandle;

    switch (appData.nvmReadStateMachine)
    {
        default:
        case APP_NVM_READ_STATE_START:
            HandleAttempts = 0;
            appData.nvmReadStateMachine = APP_NVM_READ_STATE_OPEN;
            break;

        case APP_NVM_READ_STATE_OPEN:
            /* open the file to read */
            FileHandle = SYS_FS_FileOpen(appNVMReadPath, SYS_FS_FILE_OPEN_READ);<<DOES WORK
            if (FileHandle == SYS_FS_HANDLE_INVALID)
            {
                /* try 10 times to get the handle while auto-mount happens */
                if (++HandleAttempts > 10)
                {
                    FsError = SYS_FS_Error();
                    appData.nvmReadStateMachine = APP_NVM_READ_STATE_ERROR;
                }
            }
            else
            {
                TotalBytesRead = 0;
                appData.nvmReadStateMachine = APP_NVM_READ_STATE_FILEOPS;
            }
            break;

        case APP_NVM_READ_STATE_FILEOPS:
            {
                size_t BytesRead = SYS_FS_FileRead(FileHandle, appNVMReadData,
                    sizeof(appNVMReadData) - TotalBytesRead);
                if (BytesRead == (size_t)-1)
                {
                    /* error */
                    FsError = SYS_FS_Error();
                    appData.nvmReadStateMachine = APP_NVM_READ_STATE_ERROR;
                    return;
                }
                else
                {
                    TotalBytesRead += BytesRead;
                    /* have we gathered all the bytes now? */
                    if (TotalBytesRead >= sizeof(appNVMReadData))
                    {
                        SYS_FS_FileClose(FileHandle);
                        appData.nvmReadStateMachine = APP_NVM_READ_STATE_DONE;
                    }
                }
            }
            break;

        case APP_NVM_READ_STATE_DONE:
            BSP_LEDOn(BSP_LED_D6);
            break;

        /* if we get to this state - something went wrong - check FsError for cause */
        case APP_NVM_READ_STATE_ERROR:
            BSP_LEDOn(BSP_LED_D7);
            (void)FsError;
            break;
    }
}


/* TODO: Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
  Function:
    void APP_Initialize ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;

    
    /* Place the File System NVM state machine in its initial state */
    appData.nvmWriteStateMachine = APP_NVM_WRITE_STATE_START;

    /* Place the File System NVM state machine in its initial state */
    appData.nvmReadStateMachine = APP_NVM_READ_STATE_START;
    
    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
}


/******************************************************************************
  Function:
    void APP_Tasks ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Tasks ( void )
{

    /* Check the application's current state. */
    switch ( appData.state )
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            bool appInitialized = true;
       
        
            if (appInitialized)
            {
            
                appData.state = APP_STATE_SERVICE_TASKS;
            }
            break;
        }

        case APP_STATE_SERVICE_TASKS:
        {
        
            if (BSP_LED_ACTIVE_LOW == BSP_LEDStateGet(BSP_LED_2))
            {
            /* run the state machine for servicing the NVM file system */
                APP_NVM_WRITE_Task();
            }
            else
            {
                /* run the state machine for servicing the NVM file system */
                APP_NVM_READ_Task();
            }
            break;
        }

        /* TODO: implement your application state machine.*/
        

        /* The default state should never be executed. */
        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}

/*******************************************************************************
 End of File
 */

post edited by hermallorn - 2020/07/02 07:44:22
#1

1 Reply Related Threads

    optimus_jack
    Senior Member
    • Total Posts : 100
    • Reward points : 0
    • Joined: 2017/02/16 03:02:47
    • Location: 0
    • Status: offline
    Re: NVM read example not working on PIC32MZ2064DAG169 2020/07/02 20:10:24 (permalink)
    0
    Hi,
     
    Instead of trying to open a file with some counter, if you are using Auto mount you can use the SYS_FS_Eventhandler mechanism where SYS_FS_MOUNT event will be sent to callback registered.
     
    Also make sure File close is executed after the write is completed, if the maximum simultaneous file access is set to only 1.
     
    I would prefer you to use state machine to wait for write to complete and then continue to read instead of state of GPIO pin.
    #2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5