Showing posts with label C32. Show all posts
    Showing posts with label C32. Show all posts

    Friday, July 22, 2011

    Known issues with MPLAB C Compiler for PIC32 MCUs v2.00

    Microchip TCP/IP Stack compatibility

    Older versions of the TCP/IP stack had references to macros referencing debug freeze bits such as _SPI2CON_FRZ_MASK. The macros for these freeze bits should never have appeared in the processor header files and application code should not have been using them. The definitions were removed for the C32 v2.00 release. The July 2011 release of the Microchip Application Library with TCP/IP Stack version 5.36.2 removes references to these macros to correct compatibility with MPLAB C32 v2.00. Download the update here: http://www.microchip.com/mal

    Peripheral Library - UART2

    The UART peripheral library functions do not work correctly for the UART2 module on the PIC32MX3 and PIC32MX4 devices. The device-support include files pic32mx/include/proc/p32mx3.h and pic32mx/include/proc/p32mx4*.h contain an incorrect base address for the UART2 peripheral.

    #define _UART2_BASE_ADDRESS 0xBF806800
    should be
    #define _UART2_BASE_ADDRESS 0xBF806200
    for the PICMX3 and PIC32MX4 devices.

    This causes the peripheral library functions for the UART2 to access unimplemented SFR addresses rather than the correct UART2 SFR addresses.

    One workaround is to correct the _UART2_BASE_ADDRESS address value in your pic32mx/include/proc/pic32mx*.h header file and add a copy of the [C:\Program Files\Microchip\mplabc32\v2.00\pic32-libs\peripheral\uart\source\uart_lib.c] source file to your project.

    Uninitialized data clearing

    Uninitialized global variables should be cleared to zero on startup. There is an issue with the v2.00 startup code where variables allocated in the "common" section are not correctly zeroed on startup. One workaround is to disable support for commons by adding the compiler option "-fno-common" to the alternate settings field for your project. Another workaround is to modify the C startup code as described in this web forum post. http://www.microchip.com/forums/fb.ashx?m=588408

    Exception when using a Custom Linker Script

    The default startup code is initializing the bus matrix as if ram functions exist in a project, even when no such ram functions exist. This causes a problem whenever the end address of the kseg1_data_mem region is not 2k aligned in a custom linker script, since the bus matrix registers must be 2k aligned. To workaround the issue for now, add the following line to your custom linker script: _ramfunc_begin = 0;
    This causes the startup code to skip over the bus-matrix initialization code.

    Getting Support

    For technical support, please visit either the community web forum or http://www.microchip.com/support. To report a bug, please visit this forum post for instructions. Thanks!

    Thursday, June 09, 2011

    Working on an MPLAB C32 documentation update

    I've started work on a much needed update to the MPLAB C Compiler for PIC32 MCUs documentation. I think that I already know what sections need improvement and which sections need to be added, but your suggestions will definitely help. Hit me up on Twitter with your suggestions or leave them in the comments here.

    Tuesday, July 13, 2010

    C32 Open Discussion group at MASTERs in Phoenix

    For those of you attending the Worldwide MASTERs Conference in Phoenix, Arizona on August 25 - 28, please join the compiler team for an open discussion on the MPLAB C Compiler for PIC32 MCUs. We will have an open question and answer session followed by a discussion about the future of the compiler. We would really like to get your feedback and suggestions regarding new features and improvements.

    The discussion will take place on Thursday, August 26 at 7 PM. The room number is to be determined.

    Please join us! I'd really love to have a good turnout with a quality discussion.

    P.S. There is also a C30 discussion taking place on Wednesday, August 25 at 7 PM.

    Thursday, April 09, 2009

    Save PIC32 code (and maybe data) space by providing exit() in MPLAB C32

    The default exit() function, provided with the MPLAB C Compiler for PIC32 MCUs and called by the default startup code, does a lot of clean up that your application might not require. It does things like flushing I/O buffers. This could be completely unnecessary if your application never exits from your main function().

    You might want to consider providing your own exit() function to override the default implementation. This can save both code space and data space.

    If you're not using standard I/O, this stub implementation can save about 1 KB of code space and even some data space. Just add it to your project.
    void exit(int status)
    {
    while(1);
    }
    Even if you are using standard I/O, this stub could still save you a little space.

    Friday, March 06, 2009

    MPLAB C Compiler for PIC32 MCUs v1.05 Released

    New Features in v1.05

    Student Edition renamed Evaluation Version -- The student edition has been renamed the valuation version. This release also introduces a new academic version.

    Improved 16- and 32-bit FFT functions in the DSP Library -- The FFT functions have been significantly improved and optimized for the PIC32 instruction set. See the 32-bit Language Tools Libraries document (DS51685) for more information. Please visit Microchip's website for the revision C of this document (DS51685C).

    Binary Constants -- A sequence of binary digits preceded by 0b or 0B (the numeral '0' followed by the letter 'b' or 'B') is taken to be a binary integer. The binary digits consist of the numerals '0' and '1'. Note that this binary-constant syntax may not be accepted by other C compilers.

    Linker Memory-Usage Report -- This optional, informational report allows you to easily determine the program- and data-memory usage of your application. It allows you to see how much space is used by the project being linked and how much space is available on the target device. The memory-usage report appears on stdout and in the optional map file.

    • Using this feature with MPLAB IDE 8.20 and earlier -- On the linker's tab of the project build options, select to Use Alternate Settings. Add the --report-mem option to the Alternate Settings field. The memory-usage report will appear in the output window after a project build. (This option will appear as a checkbox in later MPLAB IDE releases.)
    • Using this feature on the command prompt -- Add the --report-mem option to the linker's command line. If you are calling the linker via the compilation driver, use the driver's -Wl option to pass the --report-mem option to the linker. The memory-usage report will appear on stdout.

    Improved malloc implementation -- This release contains a new malloc/free/realloc implementation based on the implementation written by Doug Lea and released to the public domain.

    • This is not the fastest, most space-conserving, most portable, or most tunable malloc ever written. However it is among the fastest while also being among the most space-conserving, portable and tunable. Consistent balance across these factors results in a good general-purpose allocator.
    • For a high-level description, see http://g.oswego.edu/dl/html/malloc.html
    • This implementation is also provided with the Newlib C Library.

    Instruction Macros -- The p32xxxx.h include file now contains a Nop() macro that issues a superscalar SSNOP instruction.

    gettimeofday() weak stub -- The time.h clock() and time() functions require a user-provided gettimeofday() helper function. The compiler's library now provides a weak stub implementation that allows the project link to complete without error, but you should provide your own implementation appropriate for your application.

    Microchip DSP Compatibility Wrapper functions -- This release provides new DSP wrapper functions intended to ease the migration from the Microchip DSP library for dsPIC DSCs to the general DSP library for PIC32 MCUs. See the 32-Bit Language Tools Libraries (DS51685C) document for more information on these new wrapper functions.

    Updated peripheral-library documentation -- The 32-bit peripheral library document is now distributed as a compiled help file (*.chm) rather than a PDF file.

    Support for PWP configuration bits -- The config pragma now supports enabling the Program Write Protect bits. See the PIC32MX Config Settings help file for information on the setting name and available values for each device.

    Visit http://www.microchip.com/c32

    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.

    Monday, June 09, 2008

    Using C32's Multilib when building a project from MPLAB IDE

    Hopefully, most MPLAB C Compiler for PIC32 MCUs users know that the toolchain supports multilibs, target libraries built under a permutated set of options. To take advantage of these libraries when building a project under MPLAB IDE, be sure to set the library-selection options under the linker's build options as shown in the MPLAB IDE 8.10 screenshot below.


    If you don't set these options, you will get the default unoptimized libraries. In many (if not most) cases, you'll want an optimized version of these libraries.

    You can read more about multilibs in the MPLAB C32 User's Guide.

    Invoking MADD instruction from C

    Generally, C programmers don't have to worry about the assembly instructions that their C compiler generates. We have, however, received a few messages asking if the MPLAB C Compiler for PIC32 (aka MPLAB C32) supports the MADD instruction. Yes, it does. The compiler tries to avoid using the HI/LO registers, but the code below shows a sequence that invokes the MADD instruction (as of C32 1.02).

    EDIT1: You'll need to build with at least the -O1 level of optimization.
    int dp (int a[], int b[], int n);

    int ary1[] = {1,2,3,4,5};
    int ary2[] = {6,7,8,9,10};
    volatile int testval;

    int main(void)
    {
    testval = dp (ary1, ary2, 5);
    while(1);
    }

    int dp (int a[], int b[], int n)
    {
    /*
    * move a3,a0
    */

    int i;
    long long acc = (long long) a[0] * b[0];
    /*
    * lw v1,0(a1)
    * lw a0,0(a0)
    */

    for (i = 1; i < n; i++)
    /*
    * li t0,1
    * slt v0,t0,a2
    * beqz v0,9d00005c
    * mult a0,v1
    * addiu a1,a1,4
    * addiu a0,a3,4
    * addiu a2,a2,-1
    */

    acc += (long long) a[i] * b[i];
    /*
    * lw t1,0(a0)
    * lw a3,0(a1)
    * addiu a2,a2,-1
    * madd t1,a3 <--- MADD instruction
    * addiu a0,a0,4
    * bnez a2,9d000040
    * addiu a1,a1,4
    */

    return (acc >> 31);
    /*
    * mflo t3
    * mfhi t2
    * srl a1,t3,0x1f
    * sll a2,t2,0x1
    */
    }
    /*
    * jr ra
    * or v0,a1,a2
    */

    Monday, April 21, 2008

    MPLAB C Compiler for PIC32 MCUs v1.02 now available for download

    Microchip PIC32 users will be happy to know that version 1.02 of the MPLAB C Compiler for PIC32 MCUs (aka MPLAB C32) is now available for download. There's some new devices supported and a few bug fixes. Read the release notes for further details. Get it while it's hot. http://www.microchip.com/c32