PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
app_uppercase.c
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 #define ESOS_USE_IRQS
31 
32 // INCLUDEs go here (First include the main esos.h file)
33 // After that, the user can include what they need
34 #include "esos.h"
35 #include "esos_pc.h"
36 #include "esos_pc_stdio.h"
37 
38 
39 // DEFINEs go here
40 
41 /*
42  * PROTOTYPEs go here
43  *
44  */
45 
46 // GLOBALs go here
47 // Generally, the user-created semaphores will be defined/allocated here
48 volatile uint32_t u32_T2Count;
49 volatile uint32_t u32_T3Count;
50 static uint8_t psz_T2Is[]="T2 is ";
51 static uint8_t psz_T3Is[]="T3 is ";
52 static uint8_t psz_Enabled[]="enabled.";
53 static uint8_t psz_Disabled[]="disabled.";
54 static uint8_t psz_CRNL[3]= {0x0D, 0x0A, 0};
55 
56 struct stTask* pst_MyTasks[3];
57 
58 /************************************************************************
59  * User supplied functions
60  ************************************************************************
61  */
62 
63 /******************************************************************************
64  * Function: upper_case
65  *
66  * PreConditions: This ESOS task has been registered with ESOS properly
67  *
68  * Input: None
69  *
70  * Output: None
71  *
72  * Side Effects:
73  *
74  * Overview: A task to read the data buffers, convert them to upper case
75  * and send the data right back out.
76  *
77  * Notes:
78  *
79  *****************************************************************************/
80 ESOS_USER_TASK( upper_case ) {
81  static uint8_t u8_Char;
82 
83  ESOS_TASK_BEGIN(pstTask);
84  while (TRUE) {
87  ESOS_TASK_WAIT_ON_GET_UINT8( u8_Char );
89  if ((u8_Char >= 'a') && (u8_Char <= 'z') )
90  u8_Char = u8_Char - 'a' + 'A';
95  } // endof while(TRUE)
96  ESOS_TASK_END(pstTask);
97 }
98 
99 /******************************************************************************
100  * Function: echo
101  *
102  * PreConditions: This ESOS task has been registered with ESOS properly
103  *
104  * Input: None
105  *
106  * Output: None
107  *
108  * Side Effects:
109  *
110  * Overview: A task to read the data buffers, and echo it write back out
111  *
112  * Notes:
113  *
114  *****************************************************************************/
115 ESOS_USER_TASK( echo ) {
116  static uint8_t u8_Char;
117 
118  ESOS_TASK_BEGIN(pstTask);
119  while (TRUE) {
122  ESOS_TASK_WAIT_ON_GET_UINT8( u8_Char );
128  } // endof while(TRUE)
129  ESOS_TASK_END(pstTask);
130 }
131 
132 // user task to talk to child task RX/TX tasks --- bypasses the
133 // ESOS comm system. Requires that you "disable" the
134 // ESOS comm system task in the main ESOS code.
135 //
136 // USED ONLY FOR DEVELOPMENT AND TESTING!
137 ESOS_USER_TASK( child_echo_buffers ) {
138 
139 #define LOCAL_BUFFER_LEN 16
140 
141  static uint8_t u8_i;
142  static uint8_t au8_Char[LOCAL_BUFFER_LEN+4];
143 
144  ESOS_TASK_BEGIN(pstTask);
145  while (TRUE) {
148  ESOS_TASK_WAIT_ON_GET_U8BUFFER( &au8_Char[0], LOCAL_BUFFER_LEN);
150  for (u8_i=0; u8_i<LOCAL_BUFFER_LEN; u8_i++) {
151  if ((au8_Char[u8_i] >= 'A') && (au8_Char[u8_i] <= 'Z') )
152  au8_Char[u8_i] = au8_Char[u8_i] - 'A' + 'a';
153  } // end for
154 
157  ESOS_TASK_WAIT_ON_SEND_U8BUFFER( &au8_Char[0], LOCAL_BUFFER_LEN );
159  } // endof while(TRUE)
160  ESOS_TASK_END(pstTask);
161 } // end child_task
162 
163 // user task to transmit a 32bit random number
164 //
165 // USED ONLY FOR DEVELOPMENT AND TESTING!
166 ESOS_USER_TASK( random_xmit ) {
167  static uint32_t u32_RandomNumber;
168  static uint32_t u32_count;
169  UINT32 U32_Temp;
170 
171  ESOS_TASK_BEGIN(pstTask);
172  u32_RandomNumber = 0;
173  while (TRUE) {
174  u32_RandomNumber = 245 + (esos_GetRandomUint32() & 0xFF);
175  U32_Temp._uint32_t = u32_RandomNumber;
178 #if FALSE
183 #else
185 #endif
186  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
188 
189  ESOS_TASK_WAIT_TICKS( pstTask, u32_RandomNumber );
190  } // endof while(TRUE)
191  ESOS_TASK_END(pstTask);
192 } // end child_task
193 
194 
195 // user task to transmit a 32bit random number
196 //
197 // USED ONLY FOR DEVELOPMENT AND TESTING!
198 ESOS_USER_TASK( random_1 ) {
199  static uint32_t u32_RandomNumber;
200  static uint8_t u8_RandomNumber;
201  static uint8_t u8_Count;
202  UINT32 U32_Temp;
203 
204  ESOS_TASK_BEGIN(pstTask);
205  while (TRUE) {
206  u8_Count = 0x13;
207  while (u8_Count--) {
208  u32_RandomNumber = esos_GetRandomUint32();
209  U32_Temp._uint32_t = u32_RandomNumber;
210  u8_RandomNumber = U32_Temp.u8LoLsb ^ U32_Temp.u8LoMsb ^ U32_Temp.u8HiLsb ^ U32_Temp.u8HiMsb;
217  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
219  ESOS_TASK_WAIT_TICKS(pstTask, 10+4*u8_RandomNumber );
220  } // endof while (u8_Count)
221  ESOS_TASK_SLEEP( pstTask );
222  } // endof while(TRUE)
223  ESOS_TASK_END(pstTask);
224 } // end child_task
225 
226 // user task to transmit a 32bit random number
227 //
228 // USED ONLY FOR DEVELOPMENT AND TESTING!
229 ESOS_USER_TASK( random_2 ) {
230  static uint32_t u32_RandomNumber;
231  static uint8_t u8_RandomNumber;
232  static uint8_t u8_Count;
233  UINT32 U32_Temp;
234 
235  ESOS_TASK_BEGIN(pstTask);
236  //while(TRUE) {
237  u8_Count = 0x16 ;
238  while (u8_Count--) {
239  u32_RandomNumber = esos_GetRandomUint32();
240  U32_Temp._uint32_t = u32_RandomNumber;
241  u8_RandomNumber = U32_Temp.u8LoLsb ^ U32_Temp.u8LoMsb ^ U32_Temp.u8HiLsb ^ U32_Temp.u8HiMsb;
248  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
250  ESOS_TASK_WAIT_TICKS(pstTask, 10+4*u8_RandomNumber );
251 
252  } // endof while (u8_Count)
253  // uncomment the line below to have the task SLEEP when finished,
254  // else, it will just die
255  //ESOS_TASK_SLEEP( pstTask );
256  //ESOS_TASK_SLEEP( pstTask );
257  //} // endof while(TRUE)
258  ESOS_TASK_END(pstTask);
259 } // end child_task
260 
261 // user task to transmit a 32bit random number
262 //
263 // USED ONLY FOR DEVELOPMENT AND TESTING!
264 ESOS_USER_TASK( random_3 ) {
265  static uint32_t u32_RandomNumber;
266  static uint8_t u8_RandomNumber;
267  static uint8_t u8_Count;
268  UINT32 U32_Temp;
269 
270  ESOS_TASK_BEGIN(pstTask);
271  while (TRUE) {
272  u8_Count = 0x11;
273  while (u8_Count--) {
274  u32_RandomNumber = esos_GetRandomUint32();
275  U32_Temp._uint32_t = u32_RandomNumber;
276  u8_RandomNumber = U32_Temp.u8LoLsb;
283  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
285  ESOS_TASK_WAIT_TICKS(pstTask, 10+4*u8_RandomNumber );
286  } // endof while (u8_Count)
287  ESOS_TASK_SLEEP( pstTask );
288  } // endof while(TRUE)
289  ESOS_TASK_END(pstTask);
290 } // end child_task
291 
292 
293 /******************************************************************************
294  * Function: task_ctrl
295  *
296  * PreConditions: This ESOS task has been registered with ESOS properly
297  *
298  * Input: None
299  *
300  * Output: None
301  *
302  * Side Effects:
303  *
304  * Overview: A task to sleep, wake, kill, restart, and re-register
305  * a few other tasks
306  *
307  * Notes:
308  *
309  *****************************************************************************/
310 ESOS_USER_TASK( task_ctrl ) {
311  static uint8_t u8_Char;
312  static uint8_t u8_i;
313 
314  ESOS_TASK_BEGIN(pstTask);
315  while (TRUE) {
318  ESOS_TASK_WAIT_ON_GET_UINT8( u8_Char );
320 
321  // do NOT wait for permission... Just seize control of
322  // the output communications system. Release it when
323  // we are done so an errant task can be overcome.
324  //ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM();
326 
327  if ( u8_Char == '1') {
328  ESOS_TASK_WAIT_ON_SEND_STRING( "Registering Task 1" );
329  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
330  pst_MyTasks[0] = esos_RegisterTask( random_1 );
331  } else if (u8_Char == '2') {
332  ESOS_TASK_WAIT_ON_SEND_STRING( "Registering Task 2" );
333  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
334  pst_MyTasks[1] = esos_RegisterTask( random_2 );
335  } else if (u8_Char == '3') {
336  ESOS_TASK_WAIT_ON_SEND_STRING( "Registering Task 3" );
337  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
338  pst_MyTasks[2] = esos_RegisterTask( random_3 );
339  } else if (u8_Char == 'Q') {
340  ESOS_TASK_WAIT_ON_SEND_STRING( "Killing Task 1" );
341  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
342  ESOS_TASK_KILL(pst_MyTasks[0] );
343  } else if (u8_Char == 'W') {
344  ESOS_TASK_WAIT_ON_SEND_STRING( "Killing Task 2" );
345  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
346  ESOS_TASK_KILL(pst_MyTasks[1] );
347  } else if (u8_Char == 'E') {
348  ESOS_TASK_WAIT_ON_SEND_STRING( "Killing Task 3" );
349  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
350  ESOS_TASK_KILL(pst_MyTasks[2] );
351  } else if (u8_Char == 'A') {
352  ESOS_TASK_WAIT_ON_SEND_STRING( "Restarting Task 1" );
353  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
354  ESOS_TASK_INIT( pst_MyTasks[0] );
355  } else if (u8_Char == 'S') {
356  ESOS_TASK_WAIT_ON_SEND_STRING( "Restarting Task 2" );
357  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
358  ESOS_TASK_INIT( pst_MyTasks[1] );
359  } else if (u8_Char == 'D') {
360  ESOS_TASK_WAIT_ON_SEND_STRING( "Restarting Task 3" );
361  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
362  ESOS_TASK_INIT( pst_MyTasks[2] );
363  } else if (u8_Char == 'Z') {
364  ESOS_TASK_WAIT_ON_SEND_STRING( "Waking Task 1" );
365  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
366  ESOS_TASK_WAKE(pst_MyTasks[0] );
367  } else if (u8_Char == 'X') {
368  ESOS_TASK_WAIT_ON_SEND_STRING( "Waking Task 2" );
369  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
370  ESOS_TASK_WAKE(pst_MyTasks[1] );
371  } else if (u8_Char == 'C') {
372  ESOS_TASK_WAIT_ON_SEND_STRING( "Waking Task 3" );
373  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
374  ESOS_TASK_WAKE(pst_MyTasks[2] );
375  } else if (u8_Char == ' ') {
376  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
377  ESOS_TASK_WAIT_ON_SEND_STRING( "Status:" );
378  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
379  ESOS_TASK_WAIT_ON_SEND_STRING( "=======" );
380  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
381  for (u8_i=0; u8_i<3; u8_i++) {
385  if (ESOS_TASK_IS_SLEEPING(pst_MyTasks[u8_i])) {
386  ESOS_TASK_WAIT_ON_SEND_STRING( "sleeping." );
387  }
388  if (ESOS_TASK_IS_KILLED(pst_MyTasks[u8_i])) {
389  ESOS_TASK_WAIT_ON_SEND_STRING( "killed." );
390  }
391  if (__ESOS_TASK_IS_CALLED(pst_MyTasks[u8_i])) {
392  ESOS_TASK_WAIT_ON_SEND_STRING( "called." );
393  }
394  if (ESOS_TASK_IS_ENDED(pst_MyTasks[u8_i])) {
395  ESOS_TASK_WAIT_ON_SEND_STRING( "ended." );
396  }
397  if (ESOS_TASK_IS_WAITING(pst_MyTasks[u8_i])) {
398  ESOS_TASK_WAIT_ON_SEND_STRING( "waiting." );
399  }
400  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
401  } // endof for()
402  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
403  } else {
404  ESOS_TASK_WAIT_ON_SEND_UINT8( u8_Char );
405  }
407  }
408  ESOS_TASK_END(pstTask);
409 }
410 
411 /******************************************************************************
412  * Function: watchdog
413  *
414  * PreConditions: This ESOS task has been registered with ESOS properly
415  *
416  * Input: None
417  *
418  * Output: None
419  *
420  * Side Effects:
421  *
422  * Overview: A task to alert me when tasks are no longer located where
423  * they should be
424  *
425  * Notes:
426  *
427  *****************************************************************************/
428 ESOS_USER_TASK( watchdog ) {
429  static uint8_t u8_i;
430 
431  u8_i = FALSE;
432  ESOS_TASK_BEGIN(pstTask);
433  while (TRUE) {
434  if (pst_MyTasks[0]->pfn != random_1 ) u8_i |= BIT0;
435  if (pst_MyTasks[1]->pfn != random_2 ) u8_i |= BIT1;
436  if (pst_MyTasks[2]->pfn != random_3 ) u8_i |= BIT2;
437  if (u8_i) {
440 
441  if ( u8_i && BIT0 ) {
442  ESOS_TASK_WAIT_ON_SEND_STRING( "pstTasks[0] is now " );
443  ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( (uint32_t) pst_MyTasks[0]->pfn );
444  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
445  }
446  if ( u8_i && BIT1 ) {
447  ESOS_TASK_WAIT_ON_SEND_STRING( "pstTasks[1] is now" );
448  ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( (uint32_t) pst_MyTasks[1]->pfn );
449  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
450  }
451  if ( u8_i && BIT2 ) {
452  ESOS_TASK_WAIT_ON_SEND_STRING( "pstTasks[2] is now" );
453  ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( (uint32_t) pst_MyTasks[2]->pfn );
454  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
455  }
457  } //end of if(u8_i)
458  ESOS_TASK_YIELD( pstTask );
459  } //endof while(TRUE)
460  ESOS_TASK_END(pstTask);
461 } // endof watchdog() task
462 
463 
464 /******************************************************************************
465  * Function: watchdog
466  *
467  * PreConditions: This ESOS task has been registered with ESOS properly
468  *
469  * Input: None
470  *
471  * Output: None
472  *
473  * Side Effects:
474  *
475  * Overview: A task to alert me when tasks are no longer located where
476  * they should be
477  *
478  * Notes:
479  *
480  *****************************************************************************/
481 ESOS_USER_TASK( init_print ) {
482 
483  ESOS_TASK_BEGIN(pstTask);
486  ESOS_TASK_WAIT_ON_SEND_STRING( "pstTasks[0] is " );
487  ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( (uint32_t) pst_MyTasks[0]->pfn );
488  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
489  ESOS_TASK_WAIT_ON_SEND_STRING( "pstTasks[1] is " );
490  ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( (uint32_t) pst_MyTasks[1]->pfn );
491  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
492  ESOS_TASK_WAIT_ON_SEND_STRING( "pstTasks[2] is " );
493  ESOS_TASK_WAIT_ON_SEND_UINT32_AS_HEX_STRING( (uint32_t) pst_MyTasks[2]->pfn );
494  ESOS_TASK_WAIT_ON_SEND_STRING( psz_CRNL );
496  ESOS_TASK_END(pstTask);
497 
498 } // endof init_print() task
499 
500 
501 
502 
503 /******************************************************************************
504  * Function: void user_init(void)
505  *
506  * PreCondition: None
507  *
508  * Input: None
509  *
510  * Output: None
511  *
512  * Side Effects: None
513  *
514  * Overview: user_init is a centralized initialization routine where
515  * the user can setup their application. It is called
516  * automagically by ES_OS during the operating system
517  * initialization.
518  *
519  * Note: The user should set up any state machines and init
520  * all application variables. They can also turn on
521  * any needed peripherals here.
522  *
523  * The user SHALL NOT mess with the interrupt hardware
524  * directly!!! The ES_OS must be aware of the interrupts
525  * and provides osXXXXXXX functions for the user to use.
526  * Using these ES_OS-provided functions, the user may
527  * (and probably should) initialize, register, and enable
528  * interrupts in this routine.
529  *
530  * Furthermore, the user should register AT LEAST one
531  * user application task here (via esos_RegisterTask) or
532  * the ES_OS scheduler will have nothing to schedule
533  * to run when this function returns.
534  *
535  *****************************************************************************/
536 void user_init(void) {
537  uint16_t* pu16_ptr;
538  uint16_t u16_junk;
539 
540  __esos_hw_PutString( HELLO_MSG );
541 
542  /*
543  * Now, let's get down and dirty with ESOS and our user tasks
544  *
545  * Once tasks are registered, they will start executing in
546  * the ESOS scheduler.
547  */
548 
549 
550  // here are several combinations of tasks that should work together
551 #if 0
552  esos_RegisterTask( upper_case );
553 #endif
554 
555 #if 0
556  esos_RegisterTask( child_echo_buffers );
557 #endif
558 
559 #if 1
560  esos_RegisterTask( random_xmit );
561 #endif
562 
563 #if 0
564  esos_RegisterTask( check_timers );
565 #endif
566 
567 #if 0
568  esos_RegisterTask( child_echo_buffers );
569  esos_RegisterTask( random_xmit );
570 #endif
571 
572 #if 0
573  esos_RegisterTask( random_1 );
574  esos_RegisterTask( random_2 );
575  esos_RegisterTask( random_3 );
576 #endif
577 
578 #if 0
579  esos_RegisterTask( echo );
580  esos_RegisterTask( random_1 );
581  esos_RegisterTask( random_2 );
582  esos_RegisterTask( random_3 );
583 #endif
584 
585 #if 0
586  esos_RegisterTask( child_echo_buffers );
587  esos_RegisterTask( random_1 );
588  esos_RegisterTask( random_2 );
589  esos_RegisterTask( random_3 );
590 #endif
591 
592 #if 0
593  esos_RegisterTask( upper_case );
594  esos_RegisterTask( random_1 );
595  esos_RegisterTask( random_2 );
596  esos_RegisterTask( random_3 );
597 #endif
598 
599 #if 0
600  pst_MyTasks[0] = esos_RegisterTask( random_1 );
601  pst_MyTasks[1] = esos_RegisterTask( random_2 );
602  pst_MyTasks[2] = esos_RegisterTask( random_3 );
603  esos_RegisterTask( task_ctrl );
604 #endif
605 
606 #if 0
607  pst_MyTasks[0] = esos_RegisterTask( random_1 );
608  pst_MyTasks[1] = esos_RegisterTask( random_2 );
609  pst_MyTasks[2] = esos_RegisterTask( random_3 );
610  esos_RegisterTask( task_ctrl );
611  esos_RegisterTask( init_print );
612  esos_RegisterTask( watchdog );
613 #endif
614 
615 
616 } // end user_init()