From 56dc0dd9b42647378efab6d8ce6b570a9f47546b Mon Sep 17 00:00:00 2001
From: Gaurav Aggarwal <aggarg@amazon.com>
Date: Tue, 7 Aug 2018 07:21:07 +0000
Subject: [PATCH] Merge bug fixes from Cadence

---
 .../Tensilica_Simulator_Xplorer_XCC/FreeRTOSConfig.h     | 6 ++++--
 FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/port.c    | 8 +++++---
 .../Source/portable/ThirdParty/XCC/Xtensa/portmacro.h    | 9 +++++++--
 .../Source/portable/ThirdParty/XCC/Xtensa/xtensa_rtos.h  | 4 ++--
 4 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/FreeRTOS/Demo/Tensilica_Simulator_Xplorer_XCC/FreeRTOSConfig.h b/FreeRTOS/Demo/Tensilica_Simulator_Xplorer_XCC/FreeRTOSConfig.h
index 75a8fa7c8..397c404a4 100644
--- a/FreeRTOS/Demo/Tensilica_Simulator_Xplorer_XCC/FreeRTOSConfig.h
+++ b/FreeRTOS/Demo/Tensilica_Simulator_Xplorer_XCC/FreeRTOSConfig.h
@@ -112,18 +112,20 @@
 /* Minimal stack size. This may need to be increased for your application */
 /* NOTE: The FreeRTOS demos may not work reliably with stack size < 4KB.  */
 /* The Xtensa-specific examples should be fine with XT_STACK_MIN_SIZE.    */
+/* NOTE: the size is defined in terms of StackType_t units not bytes.     */
 #if !(defined XT_STACK_MIN_SIZE)
 #error XT_STACK_MIN_SIZE not defined, did you include xtensa_config.h ?
 #endif
 
 #ifdef SMALL_TEST
-#define configMINIMAL_STACK_SIZE		(XT_STACK_MIN_SIZE)
+#define configMINIMAL_STACK_SIZE		(XT_STACK_MIN_SIZE / sizeof(StackType_t))
 #else
-#define configMINIMAL_STACK_SIZE		(XT_STACK_MIN_SIZE > 4096 ? XT_STACK_MIN_SIZE : 4096)
+#define configMINIMAL_STACK_SIZE		(XT_STACK_MIN_SIZE > 1024 ? XT_STACK_MIN_SIZE : 1024)
 #endif
 
 /* The Xtensa port uses a separate interrupt stack. Adjust the stack size */
 /* to suit the needs of your specific application.                        */
+/* NOTE: the size is defined in bytes.                                    */
 #ifndef configISR_STACK_SIZE
 #define configISR_STACK_SIZE			2048
 #endif
diff --git a/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/port.c b/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/port.c
index 6bda80423..ec09bf72c 100644
--- a/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/port.c
+++ b/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/port.c
@@ -105,6 +105,8 @@ extern void _frxt_tick_timer_init(void);
 
 /* Defined in xtensa_context.S */
 extern void _xt_coproc_init(void);
+
+
 /*-----------------------------------------------------------*/
 
 /* We require the address of the pxCurrentTCB variable, but don't want to know
@@ -120,7 +122,7 @@ unsigned port_interruptNesting = 0;  // Interrupt nesting level
 // User exception dispatcher when exiting
 void _xt_user_exit(void);
 
-/*
+/* 
  * Stack initialization
  */
 #if portUSING_MPU_WRAPPERS
@@ -223,7 +225,7 @@ BaseType_t xPortStartScheduler( void )
 BaseType_t xPortSysTickHandler( void )
 {
 	BaseType_t ret;
-	unsigned interruptMask;
+	uint32_t interruptMask;
 
 	portbenchmarkIntLatency();
 
@@ -258,4 +260,4 @@ void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMOR
 	#endif
 }
 #endif
-/*-----------------------------------------------------------*/
+
diff --git a/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/portmacro.h b/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/portmacro.h
index 82e783dc4..31832c2e0 100644
--- a/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/portmacro.h
+++ b/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/portmacro.h
@@ -99,7 +99,7 @@ extern "C" {
 #define portDOUBLE		double
 #define portLONG		int32_t
 #define portSHORT		int16_t
-#define portSTACK_TYPE	uint8_t
+#define portSTACK_TYPE	uint32_t
 #define portBASE_TYPE	int
 
 typedef portSTACK_TYPE			StackType_t;
@@ -138,6 +138,7 @@ static inline unsigned portENTER_CRITICAL_NESTED() { unsigned state = XTOS_SET_I
 // These FreeRTOS versions are similar to the nested versions above
 #define portSET_INTERRUPT_MASK_FROM_ISR()            portENTER_CRITICAL_NESTED()
 #define portCLEAR_INTERRUPT_MASK_FROM_ISR(state)     portEXIT_CRITICAL_NESTED(state)
+
 /*-----------------------------------------------------------*/
 
 /* Architecture specifics. */
@@ -154,7 +155,11 @@ static inline unsigned portENTER_CRITICAL_NESTED() { unsigned state = XTOS_SET_I
 void vPortYield( void );
 void _frxt_setup_switch( void );
 #define portYIELD()       vPortYield()
-#define portYIELD_FROM_ISR( x )		if( ( x ) != 0 ) { _frxt_setup_switch(); }
+#define portYIELD_FROM_ISR( xHigherPriorityTaskWoken )	\
+	if ( ( xHigherPriorityTaskWoken ) != 0 ) {	\
+		_frxt_setup_switch();			\
+	}
+
 /*-----------------------------------------------------------*/
 
 /* Task function macros as described on the FreeRTOS.org WEB site. */
diff --git a/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/xtensa_rtos.h b/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/xtensa_rtos.h
index 2cb8b6b47..f47357fa5 100644
--- a/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/xtensa_rtos.h
+++ b/FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/xtensa_rtos.h
@@ -226,8 +226,8 @@ Xtensa Port Version.
 
 *******************************************************************************/
 
-#define XTENSA_PORT_VERSION             1.6
-#define XTENSA_PORT_VERSION_STRING      "1.6"
+#define XTENSA_PORT_VERSION             1.7
+#define XTENSA_PORT_VERSION_STRING      "1.7"
 
 #endif /* XTENSA_RTOS_H */