PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
app_childtask.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 /** \file
31  * ESOS application program to recreate demonstrate child tasks in ESOS.
32  * Application is a fancier version of the "Echo" program \ref echo.c
33  * in Chapter 8. (See Figure 8.6 in the text.)
34  *
35  * In this version, the user selected a number 0-9 which is the increment
36  * to add each character in the "echo". The application will echo until
37  * an exclamation point "!" is sent. Then, the user is prompted for a
38  * new increment and the process repeats.
39  *
40  * Application also has a flashing LED on RB15. Flashing LED is generated
41  * by an <em>software timer</em> calling a user-provided callback function.
42  *
43  * \note Demonstrates child tasks (with input argument) and ESOS software timers
44  */
45 
46 
47 // INCLUDEs go here (First include the main esos.h file)
48 // After that, the user can include what they need
49 #include "esos.h"
50 #ifdef __linux
51 #include "esos_pc.h"
52 #include "esos_pc_stdio.h"
53 
54 // INCLUDE these so that printf() and our PC hacks work
55 #include <stdio.h>
56 #include <sys/select.h>
57 #include <termios.h>
58 #include <unistd.h>
59 #else
60 #include "esos_pic24.h"
61 #include "esos_pic24_rs232.h"
62 #endif
63 
64 // DEFINEs go here
65 #ifndef __linux
66 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT()
67 #define LED1 _LATB15
68 #else
69 #define CONFIG_LED1() printf("called CONFIG_LED1()\n");
70 uint8_t LED1 = TRUE; // LED1 is initially "on"
71 #endif
72 
73 // PROTOTYPEs go here
74 
75 // GLOBALs go here
76 // Generally, the user-created semaphores will be defined/allocated here
77 char psz_CRNL[3]= {0x0D, 0x0A, 0};
78 char psz_prompt[] = "Enter number 0-9 for echo increment: ";
79 char psz_done[9]= {' ','D','O','N','E','!',0x0D, 0x0A, 0};
80 
81 #ifdef __linux
82 /*
83  * Simulate the timer ISR found on a MCU
84  * The PC doesn't have a timer ISR, so this task will periodically
85  * call the timer services callback instead.
86  * USED ONLY FOR DEVELOPMENT AND TESTING ON PC.
87  * Real MCU hardware doesn't need this task
88  */
89 ESOS_USER_TASK( __simulated_isr ) {
91  while (TRUE) {
92  // call the ESOS timer services callback just like a real H/W ISR would
93  __esos_tmrSvcsExecute();
95 
96  } // endof while(TRUE)
97  ESOS_TASK_END();
98 } // end child_task
99 #endif
100 
101 /************************************************************************
102  * User supplied functions
103  ************************************************************************
104  */
105 
106 /*
107  * An ESOS software timer callback function strobe the heartbeat LED.
108  *
109  * Toggles LED1 everytime the callback is called. Exact period is
110  * determined by application when this timer callback function is
111  * registered with ESOS. See \ref esos_RegisterTimer
112  * Application can change timer period on-the-fly with \ref esos_ChangeTimerPeriod
113  *
114  * \note Since this heartbeat is performed in an ESOS software
115  * timer callabck, a flashing LED indicates that the ESOS system
116  * tick ISR is being called properly. If the LED quits flashing,
117  * then the ESOS system tick has ceased working. This probably indicates
118  * some catastrophic failure of the system. However, the cause could
119  * be poorly-behaved user code that is manipulating the hardware registers
120  * with the timer or interrupt enables directly. ESOS provides functions
121  * to change state of interrupts and user code should never modify the
122  * hardware used by ESOS to implement the system tick.
123  * \hideinitializer
124  */
125 
126 // user-created timer callback
127 ESOS_USER_TIMER( swTimerLED ) {
128  LED1 = !LED1;
129 #ifdef __linux
130  if (LED1) {
131  printf("\a");
132  fflush(stdout);
133  }
134 #endif
135 } //endof swTimerLED
136 
137 /*
138  * Child task to echo incoming characters back with an
139  * arbitrary increment. Will continue echoing characters
140  * until user sends an exclamation point "!"
141  */
142 ESOS_CHILD_TASK(echo_child, uint8_t u8_in) {
143  static uint8_t u8_char;
144  ESOS_TASK_BEGIN();
145  do {
150  ESOS_TASK_WAIT_ON_SEND_UINT8(u8_char+u8_in);
152  } while (u8_char != '!');
156  ESOS_TASK_END();
157 } // end echo_child()
158 
159 
160 /*
161  * Task to prompt user for the desired echo increment
162  * that is between 0-9. After getting increment, this
163  * task will spawn a child task to do the actual
164  * echo operation.
165  */
166 ESOS_USER_TASK(prompter) {
167  static uint8_t u8_char;
168  static ESOS_TASK_HANDLE th_child;
169 
170  ESOS_TASK_BEGIN();
171  while (TRUE) {
173  ESOS_TASK_WAIT_ON_SEND_STRING(psz_prompt);
175  do {
177  } while ((u8_char < '0') | (u8_char > '9'));
181  ESOS_ALLOCATE_CHILD_TASK(th_child);
182  ESOS_TASK_SPAWN_AND_WAIT(th_child, echo_child, u8_char-'0');
183  }
184  ESOS_TASK_END();
185 } // end prompter()
186 
187 /****************************************************
188  * user_init()
189  ****************************************************
190  */
191 void user_init(void) {
192 
193  // Call the hardware-provided routines to print the
194  // HELLO_MSG to the screen. Must use this call because
195  // the ESOS communications subsystems is not yet fully
196  // initialized, since this call is in user_init()
197  //
198  // In general, users should call hardware-specific
199  // function like this.
200  __esos_unsafe_PutString( HELLO_MSG );
201 
202 #ifdef __linux
203  // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off
204  // the ESOS S/W timers when they expire
205  esos_RegisterTask( __simulated_isr );
206 #endif
207 
208  // configure our hardware to support to support our application
209  CONFIG_LED1();
210 
211  // user_init() should register at least one user task
212  esos_RegisterTask(prompter);
213 
214  // register our callback function with ESOS to create a software timer
215  esos_RegisterTimer( swTimerLED, 250);
216 
217 } // end user_init()