From d310ac45522fbcaa74c2d42a7c6890417963a58a Mon Sep 17 00:00:00 2001
From: Richard Barry <ribarry@amazon.com>
Date: Tue, 28 Jan 2014 17:41:28 +0000
Subject: [PATCH] Add very basic serial CLI to the Zynq demo - needs a lot of
 tidy up yet!

---
 .../CORTEX_A9_Zynq_ZC702/RTOSDemo/.cproject   |   1 +
 .../CORTEX_A9_Zynq_ZC702/RTOSDemo/.project    |  10 +
 .../CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main.c  |   3 +-
 .../RTOSDemo/src/main_full.c                  |  13 +-
 .../RTOSDemo/src/serial.c                     | 226 ++++++++++++------
 5 files changed, 168 insertions(+), 85 deletions(-)

diff --git a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.cproject b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.cproject
index f3fd43ab2..bf4326f6c 100644
--- a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.cproject
+++ b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.cproject
@@ -32,6 +32,7 @@
 								</option>
 								<option id="xilinx.gnu.compiler.dircategory.includes.1516519458" name="Include Paths" superClass="xilinx.gnu.compiler.dircategory.includes" valueType="includePath">
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/FreeRTOS_Source/include}&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/FreeRTOS-Plus-CLI}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/Standard_Demo_Tasks/include}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/FreeRTOS_Source/portable/GCC/ARM_CA9}&quot;"/>
diff --git a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.project b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.project
index dc244b284..15d9fcbe6 100644
--- a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.project
+++ b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/.project
@@ -24,6 +24,11 @@
 		<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
 	</natures>
 	<linkedResources>
+		<link>
+			<name>src/FreeRTOS-Plus-CLI</name>
+			<type>2</type>
+			<locationURI>FREERTOS_ROOT/FreeRTOS-Plus/Source/FreeRTOS-Plus-CLI</locationURI>
+		</link>
 		<link>
 			<name>src/FreeRTOS_Source</name>
 			<type>2</type>
@@ -34,6 +39,11 @@
 			<type>2</type>
 			<locationURI>FREERTOS_ROOT/FreeRTOS/Demo/Common</locationURI>
 		</link>
+		<link>
+			<name>src/UARTCommandConsole.c</name>
+			<type>1</type>
+			<locationURI>FREERTOS_ROOT/FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/UARTCommandConsole.c</locationURI>
+		</link>
 	</linkedResources>
 	<filteredResources>
 		<filter>
diff --git a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main.c b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main.c
index 68164e7be..f2aff13ab 100644
--- a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main.c
+++ b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main.c
@@ -128,6 +128,8 @@ void vApplicationIdleHook( void );
 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName );
 void vApplicationTickHook( void );
 
+XScuGic xInterruptController;
+
 /*-----------------------------------------------------------*/
 
 int main( void )
@@ -155,7 +157,6 @@ static void prvSetupHardware( void )
 {
 BaseType_t xStatus;
 XScuGic_Config *pxGICConfig;
-XScuGic xInterruptController;
 
 	/* Ensure no interrupts execute while the scheduler is in an inconsistent
 	state.  Interrupts are automatically enabled when the scheduler is
diff --git a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main_full.c b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main_full.c
index 55464a8c3..081e0f5a2 100644
--- a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main_full.c
+++ b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main_full.c
@@ -228,6 +228,11 @@ extern void vRegTest2Implementation( void );
 extern void vRegisterSampleCLICommands( void );
 extern void vRegisterFileSystemCLICommands( void );
 
+/*
+ * The task that manages the FreeRTOS+CLI input and output.
+ */
+extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
+
 /*-----------------------------------------------------------*/
 
 /* The following two variables are used to communicate the status of the
@@ -240,10 +245,6 @@ volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
 
 void main_full( void )
 {
-	/* The baud rate setting here has no effect, hence it is set to 0 to
-	make that obvious. */
-//	xSerialPortInitMinimal( 0, mainUART_QUEUE_LENGTHS );
-
 	/* Start all the other standard demo/test tasks.  The have not particular
 	functionality, but do demonstrate how to use the FreeRTOS API and test the
 	kernel port. */
@@ -260,7 +261,7 @@ void main_full( void )
 
 	/* Start the tasks that implements the command console on the UART, as
 	described above. */
-//	vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
+	vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
 
 	/* Register the standard CLI commands. */
 //	vRegisterSampleCLICommands();
@@ -294,7 +295,7 @@ void main_full( void )
 	for( ;; );
 }
 /*-----------------------------------------------------------*/
-#error Fails when the tick hook is used
+
 static void prvCheckTask( void *pvParameters )
 {
 portTickType xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
diff --git a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/serial.c b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/serial.c
index 371230a74..53e7fcdb1 100644
--- a/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/serial.c
+++ b/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/serial.c
@@ -1,82 +1,83 @@
 /*
-    FreeRTOS V8.0.0:rc2 - Copyright (C) 2014 Real Time Engineers Ltd.
-    All rights reserved
-
-    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
-
-    ***************************************************************************
-     *                                                                       *
-     *    FreeRTOS provides completely free yet professionally developed,    *
-     *    robust, strictly quality controlled, supported, and cross          *
-     *    platform software that has become a de facto standard.             *
-     *                                                                       *
-     *    Help yourself get started quickly and support the FreeRTOS         *
-     *    project by purchasing a FreeRTOS tutorial book, reference          *
-     *    manual, or both from: http://www.FreeRTOS.org/Documentation        *
-     *                                                                       *
-     *    Thank you!                                                         *
-     *                                                                       *
-    ***************************************************************************
-
-    This file is part of the FreeRTOS distribution.
-
-    FreeRTOS is free software; you can redistribute it and/or modify it under
-    the terms of the GNU General Public License (version 2) as published by the
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
-
-    >>! NOTE: The modification to the GPL is included to allow you to distribute
-    >>! a combined work that includes FreeRTOS without being obliged to provide
-    >>! the source code for proprietary components outside of the FreeRTOS
-    >>! kernel.
-
-    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
-    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-    FOR A PARTICULAR PURPOSE.  Full license text is available from the following
-    link: http://www.freertos.org/a00114.html
-
-    1 tab == 4 spaces!
-
-    ***************************************************************************
-     *                                                                       *
-     *    Having a problem?  Start by reading the FAQ "My application does   *
-     *    not run, what could be wrong?"                                     *
-     *                                                                       *
-     *    http://www.FreeRTOS.org/FAQHelp.html                               *
-     *                                                                       *
-    ***************************************************************************
-
-    http://www.FreeRTOS.org - Documentation, books, training, latest versions,
-    license and Real Time Engineers Ltd. contact details.
-
-    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
-    including FreeRTOS+Trace - an indispensable productivity tool, a DOS
-    compatible FAT file system, and our tiny thread aware UDP/IP stack.
-
-    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
-    Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS
-    licenses offer ticketed support, indemnification and middleware.
-
-    http://www.SafeRTOS.com - High Integrity Systems also provide a safety
-    engineered and independently SIL3 certified version for use in safety and
-    mission critical applications that require provable dependability.
-
-    1 tab == 4 spaces!
-*/
+ FreeRTOS V8.0.0:rc2 - Copyright (C) 2014 Real Time Engineers Ltd.
+ All rights reserved
+
+ VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
+
+ ***************************************************************************
+ *                                                                       *
+ *    FreeRTOS provides completely free yet professionally developed,    *
+ *    robust, strictly quality controlled, supported, and cross          *
+ *    platform software that has become a de facto standard.             *
+ *                                                                       *
+ *    Help yourself get started quickly and support the FreeRTOS         *
+ *    project by purchasing a FreeRTOS tutorial book, reference          *
+ *    manual, or both from: http://www.FreeRTOS.org/Documentation        *
+ *                                                                       *
+ *    Thank you!                                                         *
+ *                                                                       *
+ ***************************************************************************
+
+ This file is part of the FreeRTOS distribution.
+
+ FreeRTOS is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License (version 2) as published by the
+ Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
+
+ >>! NOTE: The modification to the GPL is included to allow you to distribute
+ >>! a combined work that includes FreeRTOS without being obliged to provide
+ >>! the source code for proprietary components outside of the FreeRTOS
+ >>! kernel.
+
+ FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE.  Full license text is available from the following
+ link: http://www.freertos.org/a00114.html
+
+ 1 tab == 4 spaces!
+
+ ***************************************************************************
+ *                                                                       *
+ *    Having a problem?  Start by reading the FAQ "My application does   *
+ *    not run, what could be wrong?"                                     *
+ *                                                                       *
+ *    http://www.FreeRTOS.org/FAQHelp.html                               *
+ *                                                                       *
+ ***************************************************************************
+
+ http://www.FreeRTOS.org - Documentation, books, training, latest versions,
+ license and Real Time Engineers Ltd. contact details.
+
+ http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
+ including FreeRTOS+Trace - an indispensable productivity tool, a DOS
+ compatible FAT file system, and our tiny thread aware UDP/IP stack.
+
+ http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
+ Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS
+ licenses offer ticketed support, indemnification and middleware.
+
+ http://www.SafeRTOS.com - High Integrity Systems also provide a safety
+ engineered and independently SIL3 certified version for use in safety and
+ mission critical applications that require provable dependability.
+
+ 1 tab == 4 spaces!
+ */
 
 /*
-	BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART2.
-
-	***Note*** This example uses queues to send each character into an interrupt
-	service routine and out of an interrupt service routine individually.  This
-	is done to demonstrate queues being used in an interrupt, and to deliberately
-	load the system to test the FreeRTOS port.  It is *NOT* meant to be an
-	example of an efficient implementation.  An efficient implementation should
-	use the DMA, and only use FreeRTOS API functions when enough has been
-	received to warrant a task being unblocked to process the data.
-*/
+ BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART2.
+
+ ***Note*** This example uses queues to send each character into an interrupt
+ service routine and out of an interrupt service routine individually.  This
+ is done to demonstrate queues being used in an interrupt, and to deliberately
+ load the system to test the FreeRTOS port.  It is *NOT* meant to be an
+ example of an efficient implementation.  An efficient implementation should
+ use the DMA, and only use FreeRTOS API functions when enough has been
+ received to warrant a task being unblocked to process the data.
+ */
 
 /* Scheduler includes. */
 #include "FreeRTOS.h"
+#include "task.h"
 #include "queue.h"
 #include "semphr.h"
 #include "comtest2.h"
@@ -84,35 +85,104 @@
 /* Demo application includes. */
 #include "serial.h"
 
+/* Xilinx includes. */
+#include "xuartps.h"
+#include "xscugic.h"
+#include "xil_exception.h"
+
 /*-----------------------------------------------------------*/
 
+static XUartPs xUARTInstance;
+extern XScuGic xInterruptController;
+
+/*-----------------------------------------------------------*/
+
+static void prvISRHandler( void *pvUnused, uint32_t ulEvent, uint32_t ulUnused2 );
+
 /*
  * See the serial2.h header file.
  */
-xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
+xComPortHandle xSerialPortInitMinimal( uint32_t ulWantedBaud, UBaseType_t uxQueueLength )
 {
-	return NULL;
+BaseType_t xStatus;
+XUartPs_Config *pxConfig;
+
+	/* Look up the UART configuration then initialise the dirver. */
+	pxConfig = XUartPs_LookupConfig( XPAR_XUARTPS_0_DEVICE_ID );
+	configASSERT( pxConfig );
+
+	xStatus = XUartPs_CfgInitialize( &xUARTInstance, pxConfig, pxConfig->BaseAddress );
+	configASSERT( xStatus == XST_SUCCESS );
+
+	XUartPs_SetBaudRate( &xUARTInstance, ulWantedBaud );
+
+	XUartPs_SetOperMode( &xUARTInstance, XUARTPS_OPER_MODE_NORMAL );
+
+	return 0;
 }
 /*-----------------------------------------------------------*/
 
-signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
+BaseType_t xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
 {
-	return 0;
+TickType_t xTimeOnEntering;
+const TickType_t xDelay = 10UL / portTICK_PERIOD_MS;
+BaseType_t xReturn = 0;
+
+	xTimeOnEntering = xTaskGetTickCount();
+
+	do
+	{
+		/* Only wanting to receive one key press at a time. */
+		if( XUartPs_Recv( &xUARTInstance, pcRxedChar, sizeof( pcRxedChar ) ) != 0 )
+		{
+			xReturn = 1;
+			break;
+		}
+		else
+		{
+			vTaskDelay( xDelay );
+		}
+	} while( ( xTaskGetTickCount() - xTimeOnEntering ) <= xBlockTime );
+
+	return xReturn;
 }
 /*-----------------------------------------------------------*/
 
 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
 {
+static const xTxDelay = 10UL / portTICK_PERIOD_MS;
+uint32_t ulBytesSent = 0UL;
+
+	( void ) pxPort;
+
+	while( ulBytesSent < usStringLength )
+	{
+		ulBytesSent += XUartPs_Send( &xUARTInstance, pcString + ulBytesSent, usStringLength - ulBytesSent );
+
+		while( XUartPs_IsSending( &xUARTInstance ) )
+		{
+			vTaskDelay( xTxDelay );
+		}
+	}
 }
 /*-----------------------------------------------------------*/
 
 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
 {
+static const xTxDelay = 10UL / portTICK_PERIOD_MS;
+
+	XUartPs_Send( &xUARTInstance, &cOutChar, sizeof( cOutChar ) );
+
+	while( XUartPs_IsSending( &xUARTInstance ) )
+	{
+		vTaskDelay( xTxDelay );
+	}
+
 	return 0;
 }
 /*-----------------------------------------------------------*/
 
-void vSerialClose( xComPortHandle xPort )
+void vSerialClose(xComPortHandle xPort)
 {
 	/* Not supported as not required by the demo application. */
 }