A core-dump implementation for PIC18 devices with on-chip Ethernet controller.

by Isaac Marino Bavaresco

This is file "core-dump-sample.c".

//==============================================================================
/*
 Copyright (c) 2008-2010, Isaac Marino Bavaresco
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Neither the name of the author nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//==============================================================================
// isaacbavaresco@yahoo.com.br
//==============================================================================
#include <p18cxxx.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include "genericpointer.h"
#include "MAC.h"
//==============================================================================
unsigned char   ResetFlags;
//==============================================================================
#define BUFFERSIZE  512

#define min(a,b) ((a)<(b)?(a):(b))
//==============================================================================
// Check the reset cause and write the core-dump if needed.
void CheckReset( void )
    {
    unsigned char   *p;
    int             Handle;
    int             i;

    // If the reset was not normal...
    if((( ResetFlags & 0xe8 ) != 0x28u ) || (( ResetFlags & 0x03 ) == 0x02u ))
        {
        // Remove the back-up of the old core-dump (if any).
        remove( "core.bak" );
        // Rename the file with the previous core-dump to ".bak".
        rename( "core", "core.bak" ); 
        // Create a new file for the current core-dump...
        if(( Handle = open( "core", O_WRONLY | O_CREAT )) != -1 )
            {
            // ... succeeded.

            // Allocate a RAM buffer to transfer the data from the Ethernet buffer
            // to the file...
            if(( p = (unsigned char *)malloc( BUFFERSIZE )) != NULL )
                {
                // ... succeeded.

                // Temporarily initialize the pointers to the Ethernet buffer.
                ERXST   = 0;
                ERXND   = 6 * 1024;
                ERDPT   = 0;

                // Write all the core dump to the file...
                for( i = 6 + 93 + 4096; i > 0; i -= BUFFERSIZE )
                    {
                    // Read one chunk from the Ethernet buffer.
                    // This function is supplied by Micochip's TCP/IP stack.
                    MACGetArray( p, BUFFERSIZE );
                    // Write the chunk to the file.
                    write( Handle, MAKE_GEN_PTR_RAM_T( p, unsigned char ), min( i, BUFFERSIZE ));
                    }
                // Free the RAM buffer.
                free( p );
                }
            // Close the file.
            close( Handle );
            }
        // Signal that a core-dump was generated.
        printf( "\fCore dump: %02X", ResetFlags );
        // Wait until the user press a key.
        getch();
        }
    }
//==============================================================================
void main( void )
    {
    // Turn-on the PLL.
    OSCTUNE     = 0x40;

    // Save the reset flags.
    ResetFlags  = RCON & 0x3f | STKPTR & 0xc0;

    // Clear the reset flags in the SFRs.
    RCON        = 0x3f;
    STKPTR      = 0x00;

    // @@@TODO: Initialize the file system.

    // Save the core-dump if there is any.
    CheckReset();

    // @@@TODO: Initialize the Ethernet Controller and TCP/IP stack.

    while( 1 )
        {
        ClrWdt();
        // @@@TODO: The rest of the application.
        }
    }
//==============================================================================