Friday, November 21, 2008

    An example PIC32 assert() function implementation

    screenshot
    I wrote a simple assert() implementation that you can customize for your application. This implementation writes the assert message to a simple array for inspection in the watch window. You should be able to easily customize the assertion failure behavior for your application.

    #pragma config FPLLODIV = DIV_1, FPLLMUL = MUL_18
    #pragma config FPLLIDIV = DIV_2, FWDTEN = OFF
    #pragma config FCKSM = CSDCMD, FPBDIV = DIV_8
    #pragma config OSCIOFNC = OFF, POSCMOD = HS
    #pragma config IESO = OFF, FSOSCEN = OFF, FNOSC = PRIPLL
    #pragma config CP = OFF, BWP = OFF, PWP = OFF
    #include <p32xxxx.h>
    #include <plib.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib..h>

    #ifndef __DEBUG
    #define assert(ignore) ((void)0)
    #else
    #undef assert
    #undef __myassert
    #define assert(expression) \
    ((void)((expression) ? 0 : \
    (__myassert (#expression, __FILE__, \
    __LINE__), 0)))

    #define __myassert(expression, file, line) \
    __myassfail("Failed assertion `%s' at line %d of `%s'.", \
    expression, line, file)

    static void
    __myassfail(const char *format,...)
    {
    va_list arg;
    static char mystderr[0x80];
    va_start(arg, format);
    (void)vsprintf(&mystderr[0], format, arg);
    while(1);
    va_end(arg);
    }
    #endif

    int
    main (void)
    {
    SYSTEMConfig(80000000, SYS_CFG_ALL);
    int x = 1;
    int y = 2;

    assert((1+3)==(x+y));
    return 0;
    }

    Comments?

    Microchip licenses this software to you solely for use with Microchip products. Microchip and its licensors retain all right, title and interest in and to the software. All rights reserved.

    This software and any accompanying information is for suggestion only. It shall not be deemed to modify Microchip’s standard warranty for its products. It is your responsibility to ensure that this software meets your requirements.

    SOFTWARE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP OR ITS LICENSORS BE LIABLE FOR ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. The aggregate and cumulative liability of Microchip and its licensors for damages related to the use of the software will in no event exceed the amount you paid Microchip for the software.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE TERMS. If you do not accept these terms, you must remove the software from your system.

    No comments: