MATH_DATA & .tmp_data save/restore for FreeRTOS

by Isaac Marino Bavaresco

This is file "port.c.diff". It contains the differences between the file "FreeRTOS\Source\portable\MPLAB\PIC18F\port.c" (versions 5.2.0 and v5.3.0) and my modifications that solve the problem with saving/restoring the sections "MATH_DATA" and ".tmpdata".

This patch file may be applied using TortoiseMerge, which comes with TortoiseSVN.

--- port.c	sex mai 29 15:28:15 2009
+++ port.c	sex mai 29 15:27:57 2009
@@ -142,6 +142,13 @@
  */
 static void prvLowInterrupt( void );
 
+extern unsigned char ram *	GetMATHDATAStrt	( void );
+extern unsigned char		GetMATHDATALen	( void );
+extern unsigned char ram *	GetTMPDATAStrt	( void );
+extern unsigned char		GetTMPDATALen	( void );
+extern void					SaveTMP_MATH	( void );
+extern void					RestoreTMP_MATH	( void );
+
 /* 
  * Macro that pushes all the registers that make up the context of a task onto
  * the stack, then saves the new top of stack into the TCB.
@@ -198,31 +205,7 @@
 		MOVFF	PRODL, PREINC1													\
 		MOVFF	PCLATU, PREINC1													\
 		MOVFF	PCLATH, PREINC1													\
-		/* Store the .tempdata and MATH_DATA areas as described above. */		\
-		CLRF	FSR0L, 0														\
-		CLRF	FSR0H, 0														\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	POSTINC0, PREINC1												\
-		MOVFF	INDF0, PREINC1													\
-		MOVFF	FSR0L, PREINC1													\
-		MOVFF	FSR0H, PREINC1													\
+																				\
 		/* Store the hardware stack pointer in a temp register before we		\
 		modify it. */															\
 		MOVFF	STKPTR, FSR0L													\
@@ -243,6 +226,9 @@
 		/* Store the number of addresses on the hardware stack (from the		\
 		temporary register). */													\
 		MOVFF	FSR0L, PREINC1													\
+																				\
+		call	SaveTMP_MATH,0													\
+																				\
 		MOVF	PREINC1, 1, 0													\
 	_endasm																		\
 																				\
@@ -275,6 +261,9 @@
 		/* How many return addresses are there on the hardware stack?  Discard	\
 		the first byte as we are pointing to the next free space. */			\
 		MOVFF	POSTDEC1, FSR0L													\
+																				\
+		call	RestoreTMP_MATH,0												\
+																				\
 		MOVFF	POSTDEC1, FSR0L													\
 	_endasm																		\
 																				\
@@ -295,29 +284,6 @@
 	}																			\
 																				\
 	_asm																		\
-		/* Restore the .tmpdata and MATH_DATA memory. */						\
-		MOVFF	POSTDEC1, FSR0H													\
-		MOVFF	POSTDEC1, FSR0L													\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, POSTDEC0												\
-		MOVFF	POSTDEC1, INDF0													\
 		/* Restore the other registers forming the tasks context. */			\
 		MOVFF	POSTDEC1, PCLATH												\
 		MOVFF	POSTDEC1, PCLATU												\
@@ -363,6 +329,11 @@
 }
 /*-----------------------------------------------------------*/
 
+void FinishTask( void )
+	{
+	vTaskDelete( NULL );
+	}
+
 /* 
  * See header file for description. 
  */
@@ -370,18 +341,8 @@
 {
 unsigned portLONG ulAddress;
 unsigned portCHAR ucBlock;
+portSTACK_TYPE ram *p;
 
-	/* Place a few bytes of known values on the bottom of the stack. 
-	This is just useful for debugging. */
-
-	*pxTopOfStack = 0x11;
-	pxTopOfStack++;
-	*pxTopOfStack = 0x22;
-	pxTopOfStack++;
-	*pxTopOfStack = 0x33;
-	pxTopOfStack++;
-
-
 	/* Simulate how the stack would look after a call to vPortYield() generated
 	by the compiler. 
 
@@ -453,24 +414,29 @@
 	*pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* PCLATH. */
 	pxTopOfStack++;
 
-	/* Next the .tmpdata and MATH_DATA sections. */
-	for( ucBlock = 0; ucBlock <= portCOMPILER_MANAGED_MEMORY_SIZE; ucBlock++ )
-	{
-		*pxTopOfStack = ( portSTACK_TYPE ) ucBlock;
-		*pxTopOfStack++;
-	}
+	/* The only function return address so far is the address of the 
+	task. */
+	ulAddress = ( unsigned portLONG ) pxCode;
 
-	/* Store the top of the global data section. */
-	*pxTopOfStack = ( portSTACK_TYPE ) portCOMPILER_MANAGED_MEMORY_SIZE; /* Low. */
+	/* TOS low. */
+	*pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );
 	pxTopOfStack++;
+	ulAddress >>= 8;
 
-	*pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* High. */
+	/* TOS high. */
+	*pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );
 	pxTopOfStack++;
+	ulAddress >>= 8;
 
-	/* The only function return address so far is the address of the 
-	task. */
-	ulAddress = ( unsigned portLONG ) pxCode;
+	/* TOS even higher. */
+	*pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );
+	pxTopOfStack++;
 
+#if			0
+	//--------------------------------------------------------------------------
+	/* . */
+	ulAddress = ( unsigned portLONG ) FinishTask;
+
 	/* TOS low. */
 	*pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );
 	pxTopOfStack++;
@@ -484,12 +450,24 @@
 	/* TOS even higher. */
 	*pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );
 	pxTopOfStack++;
+	//--------------------------------------------------------------------------
 
 	/* Store the number of return addresses on the hardware stack - so far only
 	the address of the task entry point. */
+	*pxTopOfStack = ( portSTACK_TYPE ) 2;
+#else	//	0
 	*pxTopOfStack = ( portSTACK_TYPE ) 1;
+#endif	//	0
 	pxTopOfStack++;
 
+	/* Next the .tmpdata section. */
+	for( ucBlock = GetTMPDATALen(), p = GetTMPDATAStrt(); ucBlock; ucBlock--, pxTopOfStack++, p++ )
+		*pxTopOfStack = *p;
+
+	/* Next the MATH_DATA section. */
+	for( ucBlock = GetMATHDATALen(), p = GetMATHDATAStrt(); ucBlock; ucBlock--, pxTopOfStack++, p++ )
+		*pxTopOfStack = *p;
+
 	return pxTopOfStack;
 }
 /*-----------------------------------------------------------*/