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.

    Thursday, November 20, 2008

    What are PIC32 synthesized/macro instructions?

    The MPLAB Assembler for PIC32 MCUs also supports a number of synthesized/macro instructions intended to make writing assembly code easier. The LI (load immediate) instruction is an example of a synthetic macro instruction. The assembler generates two machine instructions to load a 32-bit constant value into a register from this single synthetic instruction.
    The assembler synthesizes instructions for
    • A 32-bit Load Immediate
    • A load from a memory location
    • A GP-relative load or store
    • An extended branch conditional
    • A two-operand form of some three-operand instructions
    • An unaligned load/store instruction
    Assembly directives, such as .set noat, .set nomacro, and .set noreorder, disable these normally helpful features for cases where you require full control over the generated code. See this previous post.

    Wednesday, November 19, 2008

    Writing a PIC32 wrapper function for malloc() and other system functions

    When debugging your MPLAB C Compiler for PIC32 MCUs project, have you ever wanted to wrap a system library function call in another function? You can with the linker's --wrap option.

    --wrap symbol
    Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol. This can be used to provide a wrapper for a system function. The wrapper function should be called __wrap_symbol. If it wishes to call the system function, it should call __real_symbol.
    Here is a trivial example:
    #include <stddef.h>
    #include <stdlib.h>
    extern void *__real_malloc (size_t);
    void *
    __wrap_malloc (int c)
    {
    printf ("malloc called with %ld\n", c);
    return __real_malloc (c);
    }
    If you link other code with this file using --wrap malloc, then all calls to malloc will call the function __wrap_malloc instead. The call to __real_malloc in __wrap_malloc will call the real malloc function. You may wish to provide a __real_malloc function as well, so that links without the --wrap option will succeed. If you do this, you should not put the definition of __real_malloc in the same file as __wrap_malloc; if you do, the assembler may resolve the call before the linker has a chance to wrap it to malloc.

    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.

    Tuesday, November 18, 2008

    PIC32 Assembler Directives that Control Code Generation

    When inspecting MPLAB Assembler for PIC32 MCUs (pic32-as) source code, you may have noticed some new assembler directives such as .set noat and .set nomacro. These directives are specific to PIC32 MCUs and affect the way that pic32-as assembles your code.

    The following excerpt from the upcoming MPLAB Assembler, Linker, and Utilities for PIC32 MCUs User’s Guide describes these new directives.

    .set noat

    When synthesizing some address formats, pic32-as may require a scratch register. By default, the assembler will quietly use the at ($1) register, which is reserved as an assembler temporary by convention. In some cases, we don't want the compiler to use that register. The .set noat directive prevents the assembler from quietly using the at register.

    .set at

    Allow the assembler to quietly use the at ($1) register.

    .set noautoextend

    By default, MIPS16 instructions are automatically extended to 32 bits when necessary. The directive .set noautoextend will turn this off. When .set noautoextend is in effect, any 32-bit instruction must be explicitly extended with the .e modifier (e.g., `li.e $4,1000'). The directive .set autoextend may be used to once again automatically extend instructions when necessary.

    .set autoextend

    Enable auto-extension of MIPS16 instructions to 32 bits.

    .set nomacro

    The assembler supports synthesized instructions, an instruction mnemonic that synthesizes into multiple machine instructions. For instance, the sleu instruction assembles into an sltu instruction and an xori instruction. The .set nomacro directive causes the assembler to emit a warming message when an instruction expands into more than one machine instruction.

    .set macro

    Suppress warnings for synthesized instructions.

    .set mips16e

    Assemble with the MIPS16e ISA extension.

    .set nomips16e

    Do not assemble with the MIPS16e ISA extension.

    .set noreorder

    By default, the assembler attempts to fill a branch or delay slot automatically by reordering the instructions around it. This feature can be very useful.

    Occasionally, you'll want to retain precise control over your instruction ordering. Use the .set noreorder directive to tell the assembler to suppress this feature until it encounters a .set reorder directive.

    .set reorder

    Allow the assembler to reorder instructions to fill a branch or delay slot.

    Beta testers wanted for COD removal from MPASM Assembler

    For our next MPASM release in late January, one of the new features is the removal of the COD file format.

    This feature will affect only those who build and debug with absolute assembly files (not using the MPLINK object linker). Removing the COD file output eliminates the limitations of requiring the project files to be within a 64-character path length and not being able to debug above 0xFFFF locations. This change should be transparent to everyone.

    We would like some users of MPASM who are not using the linker to try out this new feature and provide feedback.

    If you are willing to try this out, send an e-mail to devtools ‘at’ microchip.com.

    Thursday, November 13, 2008

    Xbox 360 NXE Coming November 19

    See all the new features of the 360's new UI as 1UP News Editor Phillip Kollar and GameVideos Associate Producer David Ellis take it for a spin.

    Xbox 360 'New Xbox Experience Walkthrough'

    Help Us Populate the Source Code Library


    Do you have Microchip source code to share with the embedded design community? Why not upload it to the new Embedded.com Source Code Library?

    Tuesday, November 04, 2008

    Microchip Enhances Mid-range 8-bit PIC Microcontroller Core

    Building upon the success of Microchip's popular Mid-range core, the enhanced core provides numerous technical improvements, including more program and data memory; a deeper/enhanced hardware stack; additional reset methods; 14 additional programming instructions, including "C" efficiency optimizations resulting in code size reductions; increased peripheral support; reduced interrupt latency, and other enhancements.

    Recognizing the demand for increased performance and peripherals within the 8-bit MCU market, Microchip continues to invest in its 8-bit PIC(R) MCU line to provide a broad product portfolio that meets the needs of its existing and future customers. The enhanced core builds upon the best elements of the existing Mid-range core and provides additional performance, while maintaining compatibility with existing Mid-range products for true product migration. The enhancements provide users with a boost of performance of up to 50% and code-size reductions of up to 40% for various algorithms and functions. Microchip's Mid-range 8-bit PIC MCUs continue their wide market acceptance and gain further momentum into applications where MCUs have been historically void, thereby enabling designers to differentiate their products in the marketplace.
    Read the full press release on MarketWatch.com
    More info here

    Monday, November 03, 2008

    Microsoft previews Windows 7 Taskbar with Peak

    One of the biggest UI improvements coming in Windows 7 is the taskbar with its new Peak feature.

    [Gizmodo via Lifehacker via NeoWin]