licensing and training services.\r
*/\r
\r
+/*\r
+ * This file defines the button push task and ISR as described at the top of\r
+ * main.c. The ISR is called from a wrapper function defined in ButtonISR.s26.\r
+ */\r
+\r
+/* Kernel includes. */\r
#include "FreeRTOS.h"\r
#include "task.h"\r
#include "semphr.h"\r
\r
-static xSemaphoreHandle xButtonSemaphore;\r
+/* The LED output used by the button push task. */\r
+#define butLED1 P7_bit.no7\r
+\r
+/* A short delay used for button debouncing. */\r
+#define butDEBOUNCE_DELAY ( 200 / portTICK_RATE_MS )\r
\r
-#define LED01 P7_bit.no7\r
+/* The semaphore used to synchronise the button push task with the interrupt. */\r
+static xSemaphoreHandle xButtonSemaphore;\r
\r
+/*\r
+ * The definition of the button task itself. See the comments at the top of\r
+ * main.c.\r
+ */\r
void vButtonTask( void *pvParameters )\r
{\r
+ /* Ensure the semaphore is created before it gets used. */\r
vSemaphoreCreateBinary( xButtonSemaphore );\r
\r
for( ;; )\r
{\r
+ /* Block on the semaphore to wait for an interrupt event. The semaphore\r
+ is 'given' from vButtonISRHandler() below. Using portMAX_DELAY as the\r
+ block time will cause the task to block indefinitely provided\r
+ INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */\r
xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );\r
- LED01 = !LED01;\r
+\r
+ /* The button must have been pushed for this line to be executed.\r
+ Simply toggle the LED. */\r
+ butLED1 = !butLED1;\r
\r
- vTaskDelay( 200 / portTICK_RATE_MS );\r
+ /* Wait a short time then clear any pending button pushes as a crude\r
+ method of debouncing the switch. xSemaphoreTake() uses a block time of\r
+ zero this time so it returns immediately rather than waiting for the\r
+ interrupt to occur. */\r
+ vTaskDelay( butDEBOUNCE_DELAY );\r
xSemaphoreTake( xButtonSemaphore, 0 );\r
}\r
}\r
/*-----------------------------------------------------------*/\r
\r
+/*\r
+ * The C portion of the interrupt handler. Interrupts are triggered by pushing\r
+ * the button on the target board. This interrupt can cause a context switch\r
+ * so has an assembly file wrapper defined within ButtonISR.s26.\r
+ */\r
void vButtonISRHandler( void )\r
{\r
short sHigherPriorityTaskWoken = pdFALSE;\r
\r
+ /* 'Give' the semaphore to unblock the button task. */\r
xSemaphoreGiveFromISR( xButtonSemaphore, &sHigherPriorityTaskWoken );\r
\r
+ /* If giving the semaphore unblocked a task, and the unblocked task has a\r
+ priority that is higher than the currently running task, then\r
+ sHigherPriorityTaskWoken will have been set to pdTRUE. Passing a pdTRUE\r
+ value to portYIELD_FROM_ISR() will cause this interrupt to return directly\r
+ to the higher priority unblocked task. */\r
portYIELD_FROM_ISR( sHigherPriorityTaskWoken );\r
}\r
/*-----------------------------------------------------------*/\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 "semtest.h"\r
#include "GenQTest.h"\r
#define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )\r
#define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )\r
\r
-#define LED00 P7_bit.no6\r
-#define LED01 P7_bit.no7\r
+/* The LED toggled by the check task. */\r
+#define mainLED_0 P7_bit.no6\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
+/* 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 ( 0x345678 )\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
+/*-----------------------------------------------------------*/\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
+ /* Creates all the tasks, then starts the scheduler. */\r
+\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
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
vStartSemaphoreTasks(mainSEMTEST_PRIORITY);\r
vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );\r
vStartDynamicPriorityTasks();\r
vCreateBlockTimeTasks();\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 tasks defined within this file. */\r
- xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, (void*)0x12345678, mainCHECK_TASK_PRIORITY, NULL );\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 '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
-\r
+ /* Finally start the scheduler running. */\r
vTaskStartScheduler();\r
\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
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( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
xToggleRate = mainERROR_TOGGLE_PERIOD;\r
}\r
\r
- /* Toggle the LED. */\r
- LED00 = !LED00;\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
\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