ON 20081016@10:11:58 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/index.htm#39737.4249652778 Isaac Marino Bavaresco[IMB-yahoo-J86] Code: /techref/member/IMB-yahoo-J86/hi-techmemcpy.htm
ON 20081016@10:21:28 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/hi-techmemcpy.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\hi-techmemcpy.htm&version=0 ON 20081016@12:47:03 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/hi-techmemcpy.htm#39737.5326736111 Isaac Marino Bavaresco[IMB-yahoo-J86] SaysThis routine is a substitution for the one in the Hi-Tech PICC compiler library.
The original has some limitations that I wanted to overcome:
1 - The data destination must always be in bank 0 or bank 1.
2 - If the data source is in program memory (retlw table) it must be 256 entries long or less, and it must not cross a 256 instruction address boundary.
My version can copy data to any RAM bank, from any RAM bank or a retlw table, without any size or alignment restriction. Besides, it is over twice as fast as the original.
There are draw-backs tough:
1 - The parameter 'Dst' is declared 'const void *', when it should be declared 'void *', but a non-const pointer can point only to banks 0 and 1 or to banks 2 and 3, never to all banks. It's no problem if the programmer is a bit careful.
2 - the function returns 'void', where it should return 'void *'. I did this to save some RAM and execution time (I personally never used the return from memcpy, as it is the same destination pointer I already have). If one needs it the right way, it is easy to fix.
Note:
If a pointer to program memory is passed to 'Dst', the function returns without doing nothing.
;============================================================================== ; Copyright (c) 2008, 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 ;============================================================================== INDF equ 0 PCL equ 2 STATUS equ 3 FSR equ 4 PCLATH equ 10 ;=============================================================================== _memcpy$Dst set ?_memcpy+0 _memcpy$DstBnk set ?_memcpy+1 _memcpy$Src set ?_memcpy+2 _memcpy$SrcBnk set ?_memcpy+3 _memcpy$Len set ?_memcpy+4 ;=============================================================================== ; void memcpy( const void *Dst, const void *Src, unsigned char Len ) psect text0,local,class=CODE,delta=2 global _memcpy signat _memcpy,0x3078 fnsize _memcpy,0,5 global ?_memcpy _memcpy: clrf STATUS ;--------------------------------------------------------------- btfss _memcpy$DstBnk,7 ; if( !( Dst & 0x8000 )) DstInROM: return ; return; incf _memcpy$Len,f ;--------------------------------------------------------------- DstInRAM: btfsc _memcpy$SrcBnk,7 ; if( Src & 0x8000 ) goto SrcInRAMTest ;--------------------------------------------------------------- SrcInROM: btfsc _memcpy$DstBnk,0 bsf STATUS,7 movf _memcpy$Dst,w movwf FSR goto SrcInROMTest ;--------------------------------------------------------------- SrcInROMLoop: call LookUp movwf INDF pagesel $ incfsz _memcpy$Src,f goto $+2 incf _memcpy$SrcBnk,f incf FSR,f SrcInROMTest: decfsz _memcpy$Len,f goto SrcInROMLoop return ;--------------------------------------------------------------- LookUp: movf _memcpy$SrcBnk,w movwf PCLATH movf _memcpy$Src,w movwf PCL ;--------------------------------------------------------------- SrcInRAM: bcf STATUS,7 btfsc _memcpy$SrcBnk,0 bsf STATUS,7 movf _memcpy$Src,w movwf FSR movf INDF,w movwf btemp+0 bcf STATUS,7 btfsc _memcpy$DstBnk,0 bsf STATUS,7 movf _memcpy$Dst,w movwf FSR movf btemp+0,w movwf INDF incf _memcpy$Src,f incf _memcpy$Dst,f SrcInRAMTest: decfsz _memcpy$Len,f goto SrcInRAM return ;=============================================================================== psect temp,ovrld,class=BANK0,space=1 btemp ds 1 ;===============================================================================ON 20081017@8:25:33 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/index.htm#39738.8510532407 Isaac Marino Bavaresco[IMB-yahoo-J86] Code: /techref/member/IMB-yahoo-J86/heap-mgmt.htm
Heap management for PICS, mostly PIC18 and FreeRTOS (www.freertos.org)ON 20081017@8:28:53 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8533796296 Isaac Marino Bavaresco[IMB-yahoo-J86] Says
This if file "malloc.c". Works with Microchip MPLAB-C18, Hi-Tech PICC and Hi-Tech PICC-18.
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #if defined __18CXX // PIC18 family with Microchip MPLAB-C18 compiler #includeON 20081017@8:30:02 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8541782407 Isaac Marino Bavaresco[IMB-yahoo-J86] Says#define INT_MASK 0xc0 #elif defined _PIC14 // PIC16 family with Hi-Tech PICC compiler #include #define INT_MASK 0x80 #elif defined _PIC18 // PIC18 family with Hi-Tech PICC-18 compiler #include #define INT_MASK 0xc0 #else // defined _PIC18 #error "Architecture not supported!" #endif // defined __18CXX //============================================================================== #include #include #include //============================================================================== #if USING_FREE_RTOS == 1 void vTaskSuspendAll ( void ); signed char xTaskResumeAll ( void ); #define DISABLE_INTERRUPTS() vTaskSuspendAll() #define RESTORE_INTERRUPTS() xTaskResumeAll() #else // USING_FREE_RTOS == 1 #define DISABLE_INTERRUPTS() {Aux = INTCON & INT_MASK; INTCON &= ~INT_MASK;} #define RESTORE_INTERRUPTS() {INTCON |= Aux;} #endif // USING_FREE_RTOS == 1 //============================================================================== void RAM *malloc( S_CLASS sizeram_t Length ) { struct _AllocBlock RAM *p, *q; #if USING_FREE_RTOS != 1 unsigned char Aux; #endif // USING_FREE_RTOS != 1 // We will not allocate 0 bytes. if( Length == 0 ) // The allocation failed. return NULL; // // The length should always be even. (Not for 14 or 16 bit (program word) PICs). // Length = ( Length + 1 ) & -2; #if defined __18CXX || defined _PIC18 // The length should be at least 2. (Not for 14 bit (program word) PICs) if( Length < sizeof __freelist.Next ) Length = sizeof __freelist.Next; #endif // defined __18CXX || defined _PIC18 DISABLE_INTERRUPTS(); // Then we iterate through the free list to find a suitable block. for( p = __freelist.Next, q = &__freelist; p != NULL && p->Length < Length; q = p, p = p->Next ) ; // Empty statement // If p is NULL is because there is no suitable block. if( p == NULL ) { RESTORE_INTERRUPTS(); // The allocation failed. return NULL; } // If the block length is not enough to be splitted, we allocate the whole block. if( p->Length < Length + sizeof( struct _AllocBlock )) { // Remove the block from the free list. q->Next = p->Next; } // We have to split the block. else { // Make q point to the begining of the new block. q = (struct _AllocBlock RAM *)( (unsigned char RAM *)p + p->Length - Length ); // Reduce the original block length. p->Length -= Length + sizeof __freelist.Length; // Set the new block length. p = q; p->Length = Length; } RESTORE_INTERRUPTS(); // Return the address of the data area of the block. return (unsigned char RAM *)p + sizeof __freelist.Length; } //==============================================================================
This if file "free.c". Works with Microchip MPLAB-C18, Hi-Tech PICC and Hi-Tech PICC-18.
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #if defined __18CXX // PIC18 family with Microchip MPLAB-C18 compiler #includeON 20081017@8:36:31 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm# Isaac Marino Bavaresco[IMB-yahoo-J86] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\heap-mgmt.htm&version=2 ON 20081017@8:38:49 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8602777778 Isaac Marino Bavaresco[IMB-yahoo-J86] Says#define INT_MASK 0xc0 #elif defined _PIC14 // PIC16 family with Hi-Tech PICC compiler #include #define INT_MASK 0x80 #elif defined _PIC18 // PIC18 family with Hi-Tech PICC-18 compiler #include #define INT_MASK 0xc0 #else // defined _PIC18 #error "Architecture not supported!" #endif // defined __18CXX //============================================================================== #include #include #include //============================================================================== #if USING_FREE_RTOS == 1 void vTaskSuspendAll ( void ); signed char xTaskResumeAll ( void ); #define DISABLE_INTERRUPTS() vTaskSuspendAll() #define RESTORE_INTERRUPTS() xTaskResumeAll() #else // USING_FREE_RTOS == 1 #define DISABLE_INTERRUPTS() {Aux = INTCON & INT_MASK; INTCON &= ~INT_MASK;} #define RESTORE_INTERRUPTS() {INTCON |= Aux;} #endif // USING_FREE_RTOS == 1 //============================================================================== void free( S_CLASS void RAM *r ) { struct _AllocBlock RAM *p, *q; #if USING_FREE_RTOS != 1 unsigned char Aux; #endif // USING_FREE_RTOS != 1 // We will not free a NULL pointer if( r == NULL ) // Finished return; // Make 'r' point to the true begining of the allocated block r = (unsigned char RAM *)r - sizeof __freelist.Length; DISABLE_INTERRUPTS(); // Then we iterate through the free list to find the point where the block will be inserted for( p = __freelist.Next, q = &__freelist; p != NULL && p < (struct _AllocBlock RAM *)r; q = p, p = p->Next ) ; // Empty statement // Make the block point to its successor in the free list (( struct _AllocBlock RAM *)r)->Next = p; // Make the predecessor block point to the block being freed q->Next = r; // If the block is contiguous to its successor, then we merge them if( (ptrdiffram_t)r + ((struct _AllocBlock RAM *)r)->Length + sizeof __freelist.Length == (ptrdiffram_t)p ) { // Make the block point to the successor of its successor ((struct _AllocBlock RAM *)r)->Next = p->Next; // Extend the block to the end of its successor ((struct _AllocBlock RAM *)r)->Length += p->Length + sizeof __freelist.Length; } // If the predecessor block is contiguous to this block, then we merge them if( (ptrdiffram_t)q + ((struct _AllocBlock RAM *)q)->Length + sizeof __freelist.Length == (ptrdiffram_t)r ) { // Make the predecessor block point to the current block's successor ((struct _AllocBlock RAM *)q)->Next = ((struct _AllocBlock RAM *)r)->Next; // Extend the predecessor block to the end of the current block ((struct _AllocBlock RAM *)q)->Length += ((struct _AllocBlock RAM *)r)->Length + sizeof __freelist.Length; } RESTORE_INTERRUPTS(); } //==============================================================================
This file is "freelist.c".
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #includeON 20081017@8:42:08 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8625925926 Isaac Marino Bavaresco[IMB-yahoo-J86] Says//============================================================================== // Head of the free blocks list in the heap. struct _AllocBlock RAM __freelist; //==============================================================================
This file is "__heap.c".
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #includeON 20081017@8:44:48 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm# Isaac Marino Bavaresco[IMB-yahoo-J86] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\heap-mgmt.htm&version=5 ON 20081017@8:48:04 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.866712963 Isaac Marino Bavaresco[IMB-yahoo-J86] Says//============================================================================== #if defined __18CXX // PIC18 family with Microchip MPLAB-C18 compiler // For using more than 256 bytes, the linker-script must be changed. unsigned char RAM __heap[256]; #elif defined _PIC14 // PIC16 family with Hi-Tech PICC compiler // Works for PIC16F876A and PIC16F877A, using all of bank2 and bank3 (this is the // maximum one can get from a PIC16). // Please note that with these definitions, the heap is composed of two non- // contiguous areas. // For other PIC16 uC, change the definitions. unsigned char RAM __heap[94]; unsigned char bank3 __heap2[96]; #elif defined _PIC18 // PIC18 family with Hi-Tech PICC-18 compiler // Hi-Tech PICC-18 is smart, you may use almost all of the RAM memory. unsigned char RAM __heap[3000]; #else // defined _PIC18 #error "Architecture not supported!" #endif // defined __18CXX //============================================================================== void heapinit( void ) { #if defined __18CXX || defined _PIC18 ((struct _AllocBlock RAM *)__heap)->Length = sizeof __heap - sizeof __freelist.Length; ((struct _AllocBlock RAM *)__heap)->Next = NULL; #elif defined _PIC14 __heap[0] = sizeof __heap - sizeof __freelist.Length; __heap[1] = (sizeram_t)__heap2; __heap2[0] = sizeof __heap2 - sizeof __freelist.Length; __heap2[1] = NULL; #endif // defined _PIC14 __freelist.Length = 0; __freelist.Next = (struct _AllocBlock RAM *)__heap; } //==============================================================================
This file is "__reclaim_stack.c". Useful only when used with PIC18 and FreeRTOS (www.freertos.org).
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #includeON 20081017@8:50:49 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8686226852 Isaac Marino Bavaresco[IMB-yahoo-J86] Says//============================================================================== // Annex the original system stack to the heap. // // Should be called only once, inside a thread, after the call to the functions // heapinit() and vTaskStartScheduler(), when the original stack is no more in use. // // This function works only for the combination PIC18 microcontrollers + // MPLAB-C18 compiler + FreeRTOS (www.freertos.org). // TIP: If you make a careful linker-script, you may join the stack to the heap // as one contiguous block, thus optimizing the available memory. // Define the stack just before the heap, with nothing between them. // When you call '__reclaim_stack', it calls 'free' passing the stack as a block // to free, and because 'malloc' allocates from the end of the heap, the beginning // of the heap contains free memory, and 'free' joins the stack to this free memory. // before after // // | | | | // +---------------------+ +---------------------+ // | | | | lower // | stack | | | addresses // | | | | // +---------------------+ | free heap | // | | | | // | free heap | | | // | | | | // + - - - - - - - - - - + + - - - - - - - - - - + // | | | | // | used heap | | used heap | higher // | | | | addresses // +---------------------+ +---------------------+ // | | | | // The standard library implementation of __reclaim_stack() do nothing. // The user should implement it, because there is no way to know the stack size // at run time or when building the library. /****************************************************************************** * Just uncomment the #define and the two lines inside the function. * * Don't forget to set the macro STACK_SIZE to the correct value. * ******************************************************************************/ // Be careful, this definition should agree with the linker-script //#define STACK_SIZE 0x100 void __reclaim_stack( void ) { //((struct _AllocBlock RAM *)&_stack)->Length = STACK_SIZE - sizeof( sizeram_t ); //free( &((struct _AllocBlock RAM *)&_stack)->Next ); } //==============================================================================
This is file "alloc.h".
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #ifndef __ALLOC_H__ #define __ALLOC_H__ //============================================================================== #includeON 20081017@8:52:22 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8696875 Isaac Marino Bavaresco[IMB-yahoo-J86] Says#if defined __18CXX // PIC18 family with Microchip MPLAB-C18 compiler #define RAM far ram #define S_CLASS auto #elif defined _PIC14 // PIC16 family with Hi-Tech PICC compiler #define RAM bank2 #define S_CLASS typedef unsigned char sizeram_t; typedef unsigned char ptrdiffram_t; #elif defined _PIC18 // PIC18 family with Hi-Tech PICC-18 compiler #define RAM #define S_CLASS typedef unsigned short sizeram_t; typedef unsigned short ptrdiffram_t; #else // defined _PIC18 #error "Architecture not supported!" #endif // defined __18CXX /*=========================================================================*//** \brief Initializes the heap. Must be called before any call to other functions that use the heap. *//*==========================================================================*/ void heapinit( void ); /*=========================================================================*//** \brief Allocates a block of RAM memory in the heap. \param len Length of the block in bytes. \return In case of success returns a pointer to the allocated block, in case of failure, returns ::NULL. *//*==========================================================================*/ void RAM *malloc( S_CLASS sizeram_t len ); /*=========================================================================*//** \brief Frees a block of RAM memory previously allocated by the function ::malloc. \param r Pointer to the block to free. *//*==========================================================================*/ void free( S_CLASS void RAM *r ); //############################################################################## #if defined __18CXX //############################################################################## /*=========================================================================*//** \brief Annex the original system stack to the heap. Should be called only once, inside a thread, after the call to the functions heapinit() and vTaskStartScheduler(), when the original stack is no more in use. This function works only for the combination PIC18 microcontrollers + MPLAB-C18 compiler + FreeRTOS (www.freertos.org). *//*==========================================================================*/ void __reclaim_stack( void ); //############################################################################## #endif // defined __18CXX //############################################################################## //============================================================================== #endif // __ALLOC_H__ //==============================================================================
This is file "heap.h".
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== #ifndef __HEAP_H__ #define __HEAP_H__ //============================================================================== #includeON 20081017@8:53:57 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.870787037 Isaac Marino Bavaresco[IMB-yahoo-J86] Says/*=========================================================================*//** \brief Control structure for controlling the free blocks list in the heap. *//*==========================================================================*/ typedef struct _AllocBlock { /// Length of the block. sizeram_t Length; /// Pointer to the next free block. struct _AllocBlock RAM *Next; } AllocBlock; /*=========================================================================*//** \brief Head of the free blocks list in the heap. *//*==========================================================================*/ extern struct _AllocBlock RAM __freelist; //============================================================================== #endif // __HEAP_H__ //==============================================================================
This is file "main.c". Useful for demonstration purpose only.
//============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== /* This file is just an example of how to use "Heap Management For Small Microcontrollers". */ //============================================================================== #includeON 20081017@8:57:45 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8734375 Isaac Marino Bavaresco[IMB-yahoo-J86] Says//============================================================================== char RAM *v[64]; void main( void ) { char i; char RAM *p, *q, *r; heapinit(); while( 1 ) { for( i = 0; i < sizeof v / sizeof v[0]; i++ ) v[i] = malloc( 20 ); p = malloc( 93 ); q = malloc( 93 ); r = malloc( 1 ); for( i = 0; i < sizeof v / sizeof v[0]; i += 2 ) free( v[i] ); for( i = 1; i < sizeof v / sizeof v[0]; i += 2 ) free( v[i] ); free( q ); free( p ); free( r ); } } //============================================================================== void vTaskSuspendAll( void ) { } //============================================================================== signed char xTaskResumeAll( void ) { return 0; } //==============================================================================
This is file "UsingFreeRTOS.h".
#if 0 //============================================================================== // This file is part of "Heap Management For Small Microcontrollers". // v1.03 (2008-10-17) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, 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. */ //============================================================================== // // This file is included from both ".c" files and ".asm" files. // //============================================================================== #endif #define USING_FREE_RTOS 0ON 20081017@9:02:02 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.876412037 Isaac Marino Bavaresco[IMB-yahoo-J86] Says
This is file "malloc.asm", it is an optional substitution for the file "malloc.c". It is much more optimized, but works only for PIC18 with Microchip MPLAB-C18.
;=============================================================================== ; This file is part of "Heap Management For Small Microcontrollers". ; v1.03 (2008-10-17) ; isaacbavaresco@yahoo.com.br ;=============================================================================== ; Copyright (c) 2007-2008, 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. ;=============================================================================== #includeON 20081017@9:03:05 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm#39738.8771296296 Isaac Marino Bavaresco[IMB-yahoo-J86] Says;=============================================================================== radix decimal ;=============================================================================== #include ;=============================================================================== #if USING_FREE_RTOS == 1 ;------------------------------------------------------------------------------- extern vTaskSuspendAll extern xTaskResumeAll #define LOCALS_SIZE 4 DISABLE_INTERRUPTS macro call vTaskSuspendAll endm RESTORE_INTERRUPTS macro call xTaskResumeAll endm ;------------------------------------------------------------------------------- #else ; USING_FREE_RTOS == 1 ;------------------------------------------------------------------------------- #define LOCALS_SIZE 5 DISABLE_INTERRUPTS macro ; Aux = INTCON & 0xc0; movlw 0xc0 andwf INTCON,w,ACCESS movwf [Aux+0] ; INTCON &= 0x3f; movlw 0x3f andwf INTCON,f,ACCESS endm RESTORE_INTERRUPTS macro ; INTCON |= Aux; movf [Aux+0],w iorwf INTCON,f,ACCESS endm ;------------------------------------------------------------------------------- #endif ; USING_FREE_RTOS == 1 ;=============================================================================== ALLOC code ;=============================================================================== #define Length 0 #define p 4 #define q 6 #define Aux 8 extern __freelist extern vTaskSuspendAll extern xTaskResumeAll ;=============================================================================== global malloc ; void ram *malloc( sizeram_t Length ) malloc: movff FSR2L,POSTINC1 movff FSR2H,POSTINC1 movff FSR1L,FSR2L movff FSR1H,FSR2H subfsr 2,4 ; { ; struct _AllocBlock ram *p,*q; ; #if USING_FREE_RTOS != 1 ; unsigned char Aux; ; #endif // USING_FREE_RTOS != 1 addfsr 1,LOCALS_SIZE ; // We will not allocate 0 bytes. ; if( Length == 0 ) movf [Length+0],w iorwf [Length+1],w bnz NotZero ; // The allocation failed. ; return NULL; clrf PRODL,ACCESS clrf PRODH,ACCESS bra Ret NotZero: ; // // The length should always be even. (Not for 14 or 16 bit (program word) PICs). ; // Length = ( Length + 1 ) & -2; ; ; infsnz [Length+0],f ; incf [Length+1],f ; bcf [Length+0],0 ; #if defined __18CXX || defined _PIC18 ; // The length should be at least 2. (Not for 14 bit (program word) PICs) ; if( Length < sizeof __freelist.Next ) ; Length = sizeof __freelist.Next; ; #endif // defined __18CXX || defined _PIC18 movlw 2 subwf [Length+0],w movlw 0 subwfb [Length+1],w bc TwoOrMore movlw 2 movwf [Length+0] TwoOrMore: ; DISABLE_INTERRUPTS(); DISABLE_INTERRUPTS ; // Then we iterate trough the free list to find a suitable block. ; for( q = &__freelist,p = __freelist.Next; p != NULL && p->Length < Length; q = p, p = p->Next ) ; ; // Empty statement movlw low __freelist ; for( q = &__freelist, movwf [q+0] movlw high __freelist movwf [q+1] movff __freelist+2,FSR0L ; p = __freelist.Next; movff __freelist+3,FSR0H Loop: movf FSR0L,w,ACCESS ; p != NULL iorwf FSR0H,w,ACCESS bz Fail movf [Length+0],w ; && p->Length < Length; subwf POSTINC0,w,ACCESS movf [Length+1],w subwfb POSTDEC0,w,ACCESS bc Found movf FSR0L,w,ACCESS ; q = p, movwf [q+0] movf FSR0H,w,ACCESS movwf [q+1] addfsr 0,2 ; p = p->Next ); movf POSTINC0,w,ACCESS movff INDF0,FSR0H movwf FSR0L,ACCESS bra Loop ; // If p is NULL is because there is no suitable block. ; if( p == NULL ) ; { ; RESTORE_INTERRUPTS(); ; // The allocation failed. ; return NULL; ; } #if USING_FREE_RTOS == 1 Fail: clrf [p+0] clrf [p+1] #else ; USING_FREE_RTOS == 1 Fail: clrf PRODL,ACCESS clrf PRODH,ACCESS #endif ; USING_FREE_RTOS == 1 bra Epilog ; // If the block length is not enough to be splitted, we allocate the whole block. ; if( p->Length < Length + sizeof( struct _AllocBlock )) ; { Found: movf FSR0L,w movwf [p+0] movf FSR0H,w movwf [p+1] movlw 0x04 addwf [Length+0],w movwf PRODL,ACCESS movlw 0x00 addwfc [Length+1],w movwf PRODH,ACCESS movf PRODL,w,ACCESS subwf POSTINC0,w,ACCESS movf PRODH,w,ACCESS subwfb POSTDEC0,w,ACCESS bc SplitBlock ; // Remove the block from the free list. ; q->Next = p->Next; AllocateWhole: addfsr 0,2 movff POSTINC0,PRODL movff POSTDEC0,PRODH movsf [q+0],FSR0L movsf [q+1],FSR0H addfsr 0,2 movff PRODL,POSTINC0 movff PRODH,POSTDEC0 bra Finish ; } ; // We have to split the block. ; else ; { ; // Make p point to the begining of the new block. ; pTemp = (struct _AllocBlock ram *)( (unsigned char ram *)p + p->Length - Length ); SplitBlock: movf POSTINC0,w,ACCESS addwf [p+0],f movf POSTDEC0,w,ACCESS addwfc [p+1],f movf [Length+0],w subwf [p+0],f movf [Length+1],w subwfb [p+1],f ; // Reduce the original block length. ; p->Length -= Length + sizeof __freelist.Length; movf [Length+0],w subwf POSTINC0,f,ACCESS movf [Length+1],w subwfb POSTDEC0,f,ACCESS movlw 0x02 subwf POSTINC0,f,ACCESS movlw 0x00 subwfb POSTDEC0,f,ACCESS ; // Set the new block length. ; p = pTemp; movsf [p+0],FSR0L movsf [p+1],FSR0H ; p->Length = Length; movsf [Length+0],POSTINC0 movsf [Length+1],POSTDEC0 ; } ; ; RESTORE_INTERRUPTS(); ; // return the address of the data area of the block. ; return (unsigned char RAM *)p + sizeof __freelist.Length; ; } #if USING_FREE_RTOS == 1 Finish: movlw 0x02 addwf [p+0],f movlw 0x00 addwfc [p+1],f Epilog: RESTORE_INTERRUPTS movsf [p+0],PRODL movsf [p+1],PRODH #else ; USING_FREE_RTOS == 1 Finish: movlw 0x02 addwf [p+0],w movwf PRODL,ACCESS movlw 0x00 addwfc [p+1],w movwf PRODH,ACCESS Epilog: RESTORE_INTERRUPTS #endif ; USING_FREE_RTOS == 1 Ret: subfsr 1,LOCALS_SIZE + 1 movff POSTDEC1,FSR2H movff INDF1,FSR2L return 0 ;=============================================================================== end ;===============================================================================
This is file "free.asm", it is an optional substitution for the file "free.c". It is much more optimized, but works only for PIC18 with Microchip MPLAB-C18.
;=============================================================================== ; This file is part of "Heap Management For Small Microcontrollers". ; v1.03 (2008-10-17) ; isaacbavaresco@yahoo.com.br ;=============================================================================== ; Copyright (c) 2007-2008, 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. ;=============================================================================== #includeON 20081017@9:07:57 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm# Isaac Marino Bavaresco[IMB-yahoo-J86] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\heap-mgmt.htm&version=13 ON 20081020@6:54:29 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap-mgmt.htm# Isaac Marino Bavaresco[IMB-yahoo-J86] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\heap-mgmt.htm&version=14 ON 20081020@9:16:41 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/malloc.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\malloc.c.htm&version=0 ON 20081020@9:25:56 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/malloc.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\malloc.c.htm&version=1 ON 20081020@9:26:22 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/malloc.asm.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\malloc.asm.htm&version=0 ON 20081020@9:26:53 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/free.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\free.c.htm&version=0 ON 20081020@9:28:58 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/free.asm.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\free.asm.htm&version=0 ON 20081020@9:29:25 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/__freelist.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\__freelist.c.htm&version=0 ON 20081020@9:29:46 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/__heap.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\__heap.c.htm&version=0 ON 20081020@9:31:41 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/__reclaim_stack.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\__reclaim_stack.c.htm&version=0 ON 20081020@11:19:34 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/heap.h.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\heap.h.htm&version=0 ON 20081020@11:20:00 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/main.c.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\main.c.htm&version=0 ON 20081020@11:20:21 AM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/UsingFreeRTOS.h.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\UsingFreeRTOS.h.htm&version=0 ON 20081020@3:19:31 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/malloc.asm.htm# Isaac Marino Bavaresco[IMB-yahoo-J86] edited the page. Difference: http://www.piclist.com/techref/diff.asp?url=H:\techref\member\IMB-yahoo-J86\malloc.asm.htm&version=1 ON 20081021@6:23:29 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/index.htm#39742.7662962963 Isaac Marino Bavaresco[IMB-yahoo-J86] Code: /techref/member/IMB-yahoo-J86/SimpleRTOS.htm;=============================================================================== radix decimal ;=============================================================================== #include ;=============================================================================== #if USING_FREE_RTOS == 1 ;------------------------------------------------------------------------------- extern vTaskSuspendAll extern xTaskResumeAll #define LOCALS_SIZE 6 DISABLE_INTERRUPTS macro call vTaskSuspendAll endm RESTORE_INTERRUPTS macro call xTaskResumeAll endm ;------------------------------------------------------------------------------- #else ; USING_FREE_RTOS == 1 ;------------------------------------------------------------------------------- #define LOCALS_SIZE 7 DISABLE_INTERRUPTS macro ; Aux = INTCON & 0xc0; movlw 0xc0 andwf INTCON,w,ACCESS movwf [Aux+0] ; INTCON &= 0x3f; movlw 0x3f andwf INTCON,f,ACCESS endm RESTORE_INTERRUPTS macro ; INTCON |= Aux; movf [Aux+0],w iorwf INTCON,f,ACCESS endm ;------------------------------------------------------------------------------- #endif ; USING_FREE_RTOS == 1 ;=============================================================================== ALLOC code ;=============================================================================== #define r 0 #define p 4 #define q 6 #define Aux2 8 #define Aux 10 extern __freelist ;=============================================================================== global free ;15: void free( void ram *r ) free: movff FSR2L, POSTINC1 movff FSR2H, POSTINC1 movff FSR1L,FSR2L movff FSR1H,FSR2H subfsr 2,4 ;16: { ;17: struct _AllocBlock ram *p, *q; ; unsigned short Aux2; ; unsigned char Aux; addfsr 1,LOCALS_SIZE ;18: ;19: if( r == NULL ) movf [r+0],w iorwf [r+1],w ;20: return; bnz NotNULL bra Finish ;21: ;22: r = (unsigned char *)r - 2; NotNULL: movlw 0x02 subwf [r+0],f movlw 0 subwfb [r+1],f DISABLE_INTERRUPTS ;23: ;24: ;25: for( p = __freelist.Next, q = &__freelist; p != NULL && p < (struct _AllocBlock ram *)r; q = p, p = p->Next ) movlw low __freelist movwf [q+0] movlw high __freelist movwf [q+1] movff __freelist+2,FSR0L ; p = __freelist.Next; movff __freelist+3,FSR0H Loop: movf FSR0L,w,ACCESS ; p != NULL iorwf FSR0H,w,ACCESS bz xa630 movf [r+0],w ; && p < (struct _AllocBlock ram *)r; subwf FSR0L,w,ACCESS movf [r+1],w subwfb FSR0H,w,ACCESS bc xa630 movf FSR0L,w,ACCESS ; q = p; movwf [q+0] movf FSR0H,w,ACCESS movwf [q+1] addfsr 0,2 ; p = p->Next ); movf POSTINC0,w,ACCESS movff INDF0,FSR0H movwf FSR0L,ACCESS bra Loop ;26: ; /* Empty statement */ xa630: movf FSR0L,w movwf [p+0] movf FSR0H,w movwf [p+1] ;29: q->Next = r; movsf [q+0],FSR0L movsf [q+1],FSR0H addfsr 0,2 movsf [r+0],POSTINC0 movsf [r+1],POSTDEC0 ;28: ((struct _AllocBlock ram *)r)->Next = p; movsf [r+0],FSR0L movsf [r+1],FSR0H addfsr 0,2 movsf [p+0],POSTINC0 movsf [p+1],POSTDEC0 ;31: if( (unsigned short)r + ((struct _AllocBlock ram *)r)->Length + 2 == (unsigned short)p ) ;movsf [r+0],FSR0L ;movsf [r+1],FSR0H subfsr 0,2 movf POSTINC0,w,ACCESS addwf [r+0],w movwf PRODL,ACCESS movf POSTDEC0,w,ACCESS addwfc [r+1],w movwf PRODH,ACCESS movlw 0x02 addwf PRODL,w,ACCESS xorwf [p+0],w bnz xa68e movlw 0x00 addwfc PRODH,w,ACCESS xorwf [p+1],w bnz xa68e ;32: { ;34: ((struct _AllocBlock ram *)r)->Length += p->Length + 2; ;33: ((struct _AllocBlock ram *)r)->Next = p->Next; movsf [p+0],FSR0L ; FSR0 = p; movsf [p+1],FSR0H movlw 0x02 ; Aux2 = p->Length + 2; addwf POSTINC0,w,ACCESS movwf [Aux2+0] movlw 0x00 addwfc POSTINC0,w,ACCESS movwf [Aux2+1] movff POSTINC0,PRODL ; PROD = p->Next; movff POSTINC0,PRODH movsf [r+0],FSR0L ; FSR = r; movsf [r+1],FSR0H movf [Aux2+0],w ; r->Length += Aux2; addwf POSTINC0,f,ACCESS movf [Aux2+1],w addwfc POSTINC0,f,ACCESS movff PRODL,POSTINC0 ; r->Next = PROD; movff PRODH,POSTINC0 ;35: } ;36: ;37: if( (unsigned short)q + ((struct _AllocBlock ram *)q)->Length + 2 == (unsigned short)r ) xa68e: movsf [q+0],FSR0L ; FSR0 = q; movsf [q+1],FSR0H movf POSTINC0,w,ACCESS ; PROD = q->Length; addwf [q+0],w movwf PRODL,ACCESS movf POSTDEC0,w,ACCESS addwfc [q+1],w movwf PRODH,ACCESS movlw 0x02 ; if( PROD + 2 == r ) addwf PRODL,w,ACCESS ; { xorwf [r+0],w bnz Epilog movlw 0x00 addwfc PRODH,w,ACCESS xorwf [r+1],w bnz Epilog ;38: { ;40: ((struct _AllocBlock ram *)q)->Length += ((struct _AllocBlock ram *)r)->Length + 2; ;39: ((struct _AllocBlock ram *)q)->Next = ((struct _AllocBlock ram *)r)->Next; movsf [r+0],FSR0L ; FSR0 = r; movsf [r+1],FSR0H movlw 0x02 ; Aux2 = r->Length + 2; addwf POSTINC0,w,ACCESS movwf [Aux2+0] movlw 0x00 addwfc POSTINC0,w,ACCESS movwf [Aux2+1] movff POSTINC0,PRODL ; PROD = r->Next; movff POSTINC0,PRODH movsf [q+0],FSR0L ; FSR0 = q; movsf [q+1],FSR0H movf [Aux2+0],w ; q->Length += Aux2; addwf POSTINC0,f,ACCESS movf [Aux2+1],w addwfc POSTINC0,f,ACCESS movff PRODL,POSTINC0 ; q->Next = PROD; movff PRODH,POSTINC0 ;41: } ;42: } Epilog: RESTORE_INTERRUPTS Finish: subfsr 1,LOCALS_SIZE + 1 movff POSTDEC1,FSR2H movff INDF1,FSR2L return 0 ;=============================================================================== end ;===============================================================================
Very simple RTOS for Microchip(R) Baseline and Midrange uCsON 20081021@6:30:58 PM at page: http://www.piclist.com/techref/member/IMB-yahoo-J86/SimpleRTOS.htm#39742.7714699074 Isaac Marino Bavaresco[IMB-yahoo-J86] Says