]> git.sur5r.net Git - freertos/blobdiff - Demo/NEC_78K0R_IAR/main.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / NEC_78K0R_IAR / main.c
index 6824ee6583a4a22803a9a83f4b364782ca8830eb..1ea81127ca79a06eca545326aa44baa4fd6615bc 100644 (file)
@@ -1,52 +1,79 @@
 /*\r
-       FreeRTOS.org V5.1.1 - Copyright (C) 2003-2009 Richard Barry.\r
+    FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
 \r
-       This file is part of the FreeRTOS.org distribution.\r
+    This file is part of the FreeRTOS distribution.\r
 \r
-       FreeRTOS.org is free software; you can redistribute it and/or modify\r
-       it under the terms of the GNU General Public License as published by\r
-       the Free Software Foundation; either version 2 of the License, or\r
-       (at your option) any later version.\r
+    FreeRTOS is free software; you can redistribute it and/or modify it    under\r
+    the terms of the GNU General Public License (version 2) as published by the\r
+    Free Software Foundation and modified by the FreeRTOS exception.\r
+    **NOTE** The exception to the GPL is included to allow you to distribute a\r
+    combined work that includes FreeRTOS without being obliged to provide the\r
+    source code for proprietary components outside of the FreeRTOS kernel.\r
+    Alternative commercial license and support terms are also available upon\r
+    request.  See the licensing section of http://www.FreeRTOS.org for full\r
+    license details.\r
 \r
-       FreeRTOS.org is distributed in the hope that it will be useful,\r
-       but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-       GNU General Public License for more details.\r
+    FreeRTOS is distributed in the hope that it will be useful,    but WITHOUT\r
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+    more details.\r
 \r
-       You should have received a copy of the GNU General Public License\r
-       along with FreeRTOS.org; if not, write to the Free Software\r
-       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+    You should have received a copy of the GNU General Public License along\r
+    with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
+    Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
 \r
-       A special exception to the GPL can be applied should you wish to distribute\r
-       a combined work that includes FreeRTOS.org, without being obliged to provide\r
-       the source code for any proprietary components.  See the licensing section\r
-       of http://www.FreeRTOS.org for full details of how and when the exception\r
-       can be applied.\r
 \r
-    ***************************************************************************\r
     ***************************************************************************\r
     *                                                                         *\r
-    * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
-    * and even write all or part of your application on your behalf.          *\r
-    * See http://www.OpenRTOS.com for details of the services we provide to   *\r
-    * expedite your project.                                                  *\r
+    * The FreeRTOS eBook and reference manual are available to purchase for a *\r
+    * small fee. Help yourself get started quickly while also helping the     *\r
+    * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
     *                                                                         *\r
-    ***************************************************************************\r
     ***************************************************************************\r
 \r
-       Please ensure to read the configuration and relevant port sections of the\r
-       online documentation.\r
+    1 tab == 4 spaces!\r
 \r
-       http://www.FreeRTOS.org - Documentation, latest information, license and\r
-       contact details.\r
+    Please ensure to read the configuration and relevant port sections of the\r
+    online documentation.\r
 \r
-       http://www.SafeRTOS.com - A version that is certified for use in safety\r
-       critical systems.\r
+    http://www.FreeRTOS.org - Documentation, latest information, license and\r
+    contact details.\r
 \r
-       http://www.OpenRTOS.com - Commercial support, development, porting,\r
-       licensing and training services.\r
+    http://www.SafeRTOS.com - A version that is certified for use in safety\r
+    critical systems.\r
+\r
+    http://www.OpenRTOS.com - Commercial support, development, porting,\r
+    licensing and training services.\r
 */\r
 \r
+/*\r
+ * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
+ * documentation provides more details of the standard demo application tasks.\r
+ * In addition to the standard demo tasks, the following tasks and tests are\r
+ * defined and/or created within this file:\r
+ *\r
+ * "Check" task -  This only executes every three seconds but has a high priority\r
+ * to ensure it gets processor time.  Its main function is to check that all the\r
+ * standard demo tasks are still operational.  If everything is running as\r
+ * expected then the check task will toggle an LED every 3 seconds.  An error\r
+ * being discovered in any task will cause the toggle rate to increase to 500ms.\r
+ *\r
+ * "Reg test" tasks - These fill the registers with known values, then check\r
+ * that each register still contains its expected value.  Each task uses\r
+ * different values.  The tasks run with very low priority so get preempted very\r
+ * frequently.  A register containing an unexpected value is indicative of an\r
+ * error in the context switching mechanism.\r
+ *\r
+ *\r
+ * Also in addition to the standard demo tasks is a button push task.  This is\r
+ * a very basic task that is included as an example of how to write an interrupt\r
+ * service routine that interacts with a task.  The button on the target board\r
+ * is used to generate an interrupt that 'gives' a semaphore in order to unblock\r
+ * a task.  In doing so the task is synchronised with the interrupt.  Each time\r
+ * the task unblocks it simply toggles an LED before entering the Blocked state\r
+ * again to wait for the next button push.\r
+ */\r
+\r
 /* Standard includes. */\r
 #include <stdlib.h>\r
 #include <string.h>\r
 #include "FreeRTOS.h"\r
 #include "task.h"\r
 \r
-/* Demo file headers. */\r
-#include "int78K0R.h"\r
+/* Standard demo file headers. */\r
 #include "PollQ.h"\r
-#include "LED.h"\r
-#include "print.h"\r
 #include "semtest.h"\r
+#include "GenQTest.h"\r
+#include "dynamic.h"\r
+#include "blocktim.h"\r
 \r
 /*\r
  * Priority definitions for most of the tasks in the demo application.  Some\r
 #define mainCHECK_TASK_PRIORITY        ( tskIDLE_PRIORITY + 2 )\r
 #define mainQUEUE_POLL_PRIORITY        ( tskIDLE_PRIORITY + 1 )\r
 #define mainSEMTEST_PRIORITY    ( tskIDLE_PRIORITY + 1 )\r
-#define mainLED_TOGGLE_PRIORITY ( tskIDLE_PRIORITY + 1 )\r
+#define mainBUTTON_PRIORITY            ( configMAX_PRIORITIES - 1 )\r
+#define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )\r
 \r
 /* The period between executions of the check task. */\r
 #define mainNO_ERROR_TOGGLE_PERIOD     ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
 #define mainERROR_TOGGLE_PERIOD                ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
 \r
-/*\r
- * 78K0R/Kx3 Option Byte Definition\r
- * watchdog disabled, LVI enabled, OCD interface enabled\r
- */\r
-__root __far const unsigned portCHAR OptionByte[OPT_BYTES_SIZE] @ 0x00C0 =\r
-{\r
-       WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED\r
-};\r
+/* The LED toggled by the check task. */\r
+#define mainLED_0   P7_bit.no6\r
 \r
-/* Security Byte Definition */\r
-__root __far const unsigned portCHAR SecuIDCode[SECU_ID_SIZE]   @ 0x00C4 =\r
-{\r
-       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff\r
-};\r
+/* A value that is passed in as the parameter to the 'check' task.  This is done\r
+purely to check that the parameter passing mechanism is functioning correctly. */\r
+#define mainCHECK_PARAMETER_VALUE      ( 0x5678 )\r
+\r
+/*-----------------------------------------------------------*/\r
 \r
-/* The task function for the "Check" task. */\r
+/*\r
+ * The function that defines the 'check' task as described at the top of this\r
+ * file.\r
+ */\r
 static void vErrorChecks( void *pvParameters );\r
 \r
 \r
-/* 78K0R/Kx3 low level init Initialization of the System Clock */\r
+/*\r
+ * This function is called from the C startup routine to setup the processor -\r
+ * in particular the clock source.\r
+ */\r
 int __low_level_init(void);\r
 \r
+/*\r
+ * Functions that define the RegTest tasks as described at the top of this file.\r
+ */\r
 extern void vRegTest1( void *pvParameters );\r
 extern void vRegTest2( void *pvParameters );\r
 \r
+/*\r
+ * Function that defines the button push task as described at the top of this\r
+ * file.\r
+ */\r
+extern void vButtonTask( void *pvParameters );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* If an error is discovered by one of the RegTest tasks then this flag is set\r
+to pdFAIL.  The 'check' task then inspects this flag to detect errors within\r
+the RegTest tasks. */\r
 static short sRegTestStatus = pdPASS;\r
 \r
-portSHORT main( void )\r
+/* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface\r
+enabled. */\r
+__root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =\r
+{\r
+       WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED\r
+};\r
+\r
+/* Security byte definition */\r
+__root __far const unsigned portCHAR SecuIDCode[]  @ 0x00C4 =\r
+{\r
+       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff\r
+};\r
+\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+short main( void )\r
 {\r
-       /* Create the standard demo tasks. */\r
-       vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
-       vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
-       vStartSemaphoreTasks(mainSEMTEST_PRIORITY);\r
+       /* Creates all the tasks, then starts the scheduler. */\r
 \r
-       /* Create the tasks defined within this file. */\r
-       xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, (void*)0x12345678, mainCHECK_TASK_PRIORITY, NULL );\r
+       /* First create the 'standard demo' tasks.  These are used to demonstrate\r
+       API functions being used and also to test the kernel port.  More information\r
+       is provided on the FreeRTOS.org WEB site. */\r
+       vStartDynamicPriorityTasks();\r
 \r
+       /* Create the RegTest tasks as described at the top of this file. */\r
        xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
        xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );      \r
+       \r
+       /* Create the button push task as described at the top of this file. */\r
+       xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );                \r
+       \r
+       /* Create the 'check' task as described at the top of this file. */\r
+       xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );\r
 \r
-       /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
-        * as 1 in FreeRTOSconfig.h.  To use the cooperative scheduler define\r
-        * configUSE_PREEMPTION as 0.\r
-        */\r
+       #ifdef __IAR_78K0R_Kx3__\r
+       {\r
+               /* The Kx3 has enough RAM to create more of the standard demo tasks. */\r
+               vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
+               vStartSemaphoreTasks(mainSEMTEST_PRIORITY);\r
+               vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );\r
+               vCreateBlockTimeTasks();\r
+       }\r
+       #endif\r
+       \r
+       /* Finally start the scheduler running. */\r
        vTaskStartScheduler();\r
 \r
-       return 0;\r
+       /* If this line is reached then vTaskStartScheduler() returned because there\r
+       was insufficient heap memory remaining for the idle task to be created. */\r
+       for( ;; );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -129,156 +202,169 @@ static void vErrorChecks( void *pvParameters )
 {\r
 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;\r
 \r
-       /* The pointer will only actually be either 3 or 2 bytes, depending on the\r
-       memory model. */\r
-       if( pvParameters != ( void * ) 0x12345678 )\r
+       /* Ensure the parameter was passed in as expected.  This is just a test of\r
+       the kernel port, the parameter is not actually used for anything.  The\r
+       pointer will only actually be either 3 or 2 bytes, depending on the memory\r
+       model. */\r
+       if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )\r
        {\r
                xToggleRate = mainERROR_TOGGLE_PERIOD;\r
        }\r
 \r
+       /* Initialise xLastWakeTime before it is used.  After this point it is not\r
+       written to directly. */\r
        xLastWakeTime = xTaskGetTickCount();\r
 \r
        /* Cycle for ever, delaying then checking all the other tasks are still\r
        operating without error. */\r
        for( ;; )\r
        {\r
+               /* Wait until it is time to check all the other tasks again. */\r
                vTaskDelayUntil( &xLastWakeTime, xToggleRate );\r
 \r
-               if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
-               {\r
-                       xToggleRate = mainERROR_TOGGLE_PERIOD;\r
-               }\r
-\r
-               if( xArePollingQueuesStillRunning() != pdTRUE)\r
+               if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
                {\r
                        xToggleRate = mainERROR_TOGGLE_PERIOD;\r
                }\r
 \r
-               if( xAreSemaphoreTasksStillRunning() != pdTRUE)\r
+               if( sRegTestStatus != pdPASS )\r
                {\r
                        xToggleRate = mainERROR_TOGGLE_PERIOD;\r
                }\r
 \r
-               if( sRegTestStatus != pdPASS )\r
+               #ifdef __IAR_78K0R_Kx3__\r
                {\r
-                       xToggleRate = mainERROR_TOGGLE_PERIOD;\r
+                       /* Only the Kx3 runs all the tasks. */\r
+                       if( xArePollingQueuesStillRunning() != pdTRUE)\r
+                       {\r
+                               xToggleRate = mainERROR_TOGGLE_PERIOD;\r
+                       }\r
+               \r
+                       if( xAreSemaphoreTasksStillRunning() != pdTRUE)\r
+                       {\r
+                               xToggleRate = mainERROR_TOGGLE_PERIOD;\r
+                       }\r
+                       \r
+                       if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
+                       {\r
+                               xToggleRate = mainERROR_TOGGLE_PERIOD;\r
+                       }       \r
+               \r
+                       if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
+                       {\r
+                               xToggleRate = mainERROR_TOGGLE_PERIOD;\r
+                       }                       \r
                }\r
-\r
-               /* Toggle the LED. */\r
-               LED01 = !LED01;\r
+               #endif\r
+               \r
+               /* Toggle the LED.  The toggle rate will depend on whether or not an\r
+               error has been found in any tasks. */\r
+               mainLED_0 = !mainLED_0;\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 int __low_level_init(void)\r
 {\r
-unsigned portCHAR resetflag = RESF;\r
+unsigned portCHAR ucResetFlag = RESF;\r
 \r
        portDISABLE_INTERRUPTS();\r
 \r
-       /*\r
-        * Clock Configuration:\r
-        * In this port, to use the internal high speed clock source of the microcontroller\r
-        * define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To use an external\r
-        * clock define configCLOCK_SOURCE as 0.\r
-        */\r
+       /* Clock Configuration:\r
+       In this port, to use the internal high speed clock source of the microcontroller\r
+       define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To use an external\r
+       clock define configCLOCK_SOURCE as 0. */\r
        #if configCLOCK_SOURCE == 1\r
        {\r
-               /*\r
-                * Set XT1 and XT2 in Input Port Mode\r
-                * Set X1  and X2  in Input Port Mode\r
-                * High speed oszillation frequency 2MHz <= fMX <= 10MHz\r
-                */\r
+               /* Set XT1 and XT2 in Input Port Mode\r
+                  Set X1  and X2  in Input Port Mode\r
+                  High speed oscillator frequency 2MHz <= fMX <= 10MHz */\r
                CMC = 0x00;\r
 \r
-               /* X1 external oszillation stopped */\r
+               /* X1 external oszillation stopped. */\r
                MSTOP = 1;\r
 \r
-               /* enable internal high speed oszillation */\r
+               /* Enable internal high speed oszillation. */\r
                HIOSTOP = 0;\r
                MCM0 = 0;\r
 \r
-               /* stop internal subsystem clock */\r
+               /* Stop internal subsystem clock. */\r
                XTSTOP = 1;\r
 \r
-               /* Set clock speed */\r
+               /* Set clock speed. */\r
                CSS = 0;\r
                CKC &= (unsigned portCHAR)~0x07;\r
                CKC |= 0x00;\r
        }\r
        #else\r
        {\r
-               /*\r
-                * XT1 and XT2 pin in input port mode\r
-                * X1  and X2  pin in crystal resonator mode\r
-                * High speed oszillation frequency 10MHz < fMX <= 20MHz\r
-                */\r
+               /* XT1 and XT2 pin in input port mode\r
+                  X1  and X2  pin in crystal resonator mode\r
+                  High speed oszillation frequency 10MHz < fMX <= 20MHz */\r
                CMC   = 0x41;\r
                \r
-               /* Set oscillation stabilization time */\r
+               /* Set oscillation stabilization time. */\r
                OSTS  = 0x07;\r
                \r
-               /* Set speed mode: fMX > 10MHz for Flash memory high speed operation */\r
+               /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */\r
                OSMC  = 0x01;\r
                \r
-               /*\r
-                * Start up X1 oscillator operation\r
-                * Internal high-speed oscillator operating\r
-                */\r
+               /* Start up X1 oscillator operation\r
+                  Internal high-speed oscillator operating. */\r
                MSTOP = 0;\r
                \r
-               /* Check oscillation stabilization time status */\r
+               /* Check oscillation stabilization time status. */\r
                while(OSTC < 0x07)\r
                {\r
-                       /* wait until X1 clock stabilization time */\r
+                       /* Wait until X1 clock stabilization time. */\r
                        portNOP();\r
                }\r
                \r
-               /* Switch CPU clock to X1 oscillator */\r
+               /* Switch CPU clock to X1 oscillator. */\r
                MCM0 = 1;\r
                while(MCS != 1)\r
                {\r
-                       /* wait until CPU and peripherals operate with fX1 clock */\r
+                       /* Wait until CPU and peripherals operate with fX1 clock. */\r
                        portNOP();\r
                }\r
 \r
-               /* Stop the internal high-speed oscillator operation */\r
+               /* Stop the internal high-speed oscillator operation. */\r
                HIOSTOP = 1;\r
                \r
-               /* Stop the XT1 oscillator operation */\r
+               /* Stop the XT1 oscillator operation. */\r
                XTSTOP  = 1;\r
                \r
-               /*\r
-                * operating frequency f = fx\r
-                * Change clock generator setting, if necessary\r
-                */\r
+               /* Operating frequency f = fx\r
+                  Change clock generator setting, if necessary. */\r
                CKC &= 0xF8;\r
 \r
-               /* From here onwards the X1 oscillator is supplied to the CPU */\r
+               /* From here onwards the X1 oscillator is supplied to the CPU. */\r
        }\r
        #endif\r
        \r
-       /* LED Port Initialization - set Port Register */\r
+       /* LED port initialization - set port register. */\r
        P7  = 0x80;\r
        \r
-       /* Set Port Mode Register */\r
+       /* Set port mode register. */\r
        PM7 = 0x3F;\r
        \r
-       /* Switch Pin Initialization - enable pull-up resistor */\r
+       /* Switch pin initialization - enable pull-up resistor. */\r
        PU12_bit.no0  = 1;\r
+\r
+       /* INTP0 is used by the button on the target board. */\r
        \r
-       /* INTP0 disable */\r
+       /* INTP0 disable. */\r
        PMK0 = 1;                       \r
        \r
-       /* INTP0 IF clear */\r
+       /* INTP0 IF clear. */\r
        PIF0 = 0;                       \r
        EGN0_bit.no0  = 1;\r
        \r
-       /* INTP0 priority low */\r
+       /* INTP0 priority low. */\r
        PPR10 = 0;\r
        PPR00 = 1;\r
        \r
-       /* enable ext. INTP0 interrupt */\r
+       /* Enable ext. INTP0 interrupt */\r
        PMK0  = 0;      \r
 \r
        return pdTRUE;\r
@@ -287,6 +373,20 @@ unsigned portCHAR resetflag = RESF;
 \r
 void vRegTestError( void )\r
 {\r
+       /* Called by the RegTest tasks if an error is found.  lRegTestStatus is\r
+       inspected by the check task. */\r
        sRegTestStatus = pdFAIL;\r
+\r
+       /* Do not return from here as the reg test tasks clobber all registers so\r
+       function calls may not function correctly. */\r
+       for( ;; );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vApplicationStackOverflowHook( void )\r
+{\r
+       /* This will get called if an overflow is detected in the stack of a task.\r
+       Inspect pxCurrentTCB to see which was the offending task. */\r
        for( ;; );\r
 }\r
+\r