PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
esos_skel.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 similar to the "Echo" program \ref echo.c
32  * in Chapter 8. (See Figure 8.6 in the text.)
33  * This version of "echo" program which waits for UART1 RX character
34  * and echos it back to UART1 TX with all lowercase letters converted
35  * to uppercase.
36  * Use this program to test your UART connection under ESOS
37  *
38  * Application also has a flashing LED on RB15. Flashing LED is generated
39  * by an <em>user task</em> that runs simultaneously with the echo task above.
40  *
41  * \note Demonstrates multitasking in ESOS
42  */
43 
44 
45 // INCLUDEs go here (First include the main esos.h file)
46 // After that, the user can include what they need
47 #include "esos.h"
48 #ifdef __linux
49 #include "esos_pc.h"
50 #include "esos_pc_stdio.h"
51 
52 // INCLUDE these so that printf() and our PC hacks work
53 #include <stdio.h>
54 #include <sys/select.h>
55 #include <termios.h>
56 #include <unistd.h>
57 #else
58 #include "esos_pic24.h"
59 #include "esos_pic24_rs232.h"
60 #endif
61 
62 // DEFINEs go here
63 #ifndef __linux
64 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT()
65 #define LED1 _LATB15
66 #else
67 #define CONFIG_LED1() printf("called CONFIG_LED1()\n");
68 uint8_t LED1 = TRUE; // LED1 is initially "on"
69 #endif
70 
71 // PROTOTYPEs go here
72 
73 // GLOBALs go here
74 // Generally, the user-created semaphores will be defined/allocated here
75 static uint8_t psz_CRNL[3]= {0x0D, 0x0A, 0};
76 
77 
78 #ifdef __linux
79 /*
80  * Simulate the timer ISR found on a MCU
81  * The PC doesn't have a timer ISR, so this task will periodically
82  * call the timer services callback instead.
83  * USED ONLY FOR DEVELOPMENT AND TESTING ON PC.
84  * Real MCU hardware doesn't need this task
85  */
86 ESOS_USER_TASK( __simulated_isr ) {
88  while (TRUE) {
89  // call the ESOS timer services callback just like a real H/W ISR would
90  __esos_tmrSvcsExecute();
92 
93  } // endof while(TRUE)
94  ESOS_TASK_END();
95 } // end child_task
96 #endif
97 
98 /************************************************************************
99  * User supplied functions
100  ************************************************************************
101  */
102 
103 /**
104  * An ESOS task to mimic the heartbeat LED found
105  * in the PIC24 support library code used in Chapters 8-13.
106  *
107  * Toggle LED1, wait 250ms, repeat forever.
108  *
109  * \note Since this heartbeat is performed in an ESOS task,
110  * a flashing LED indicates that the ESOS scheduler is still
111  * running properly. If the LED quits flashing, the ESOS
112  * scheduler is no longer rotating through the runnable task
113  * list. The most likely reason is that some task has ceased
114  * "yielding" the processor, and is caught in some deadlock
115  * or otherwise infinite loop.
116  * \hideinitializer
117  */
118 ESOS_USER_TASK(heartbeat_LED) {
119  ESOS_TASK_BEGIN();
120  while (TRUE) {
121  LED1 = !LED1;
122 #ifdef __linux
123  if (LED1) {
124  printf("\a");
125  fflush(stdout);
126  }
127 #endif
128  ESOS_TASK_WAIT_TICKS( 500 );
129  } // endof while(TRUE)
130  ESOS_TASK_END();
131 } // end upper_case()
132 
133 /**
134  * Read a single character from the "in" stream, convert lowercase
135  * letters to uppercase, and send those chars back out the "out" stream
136  */
137 ESOS_USER_TASK(upper_case) {
138  static uint8_t u8_char;
139 
140  ESOS_TASK_BEGIN();
141  while (TRUE) {
145  if ((u8_char >= 'a') & (u8_char <= 'z')) {
146  u8_char = u8_char - 'a' + 'A';
147  }
151  } // endof while(TRUE)
152  ESOS_TASK_END();
153 } // end upper_case()
154 
155 /****************************************************
156  * user_init()
157  ****************************************************
158  */
159 void user_init(void) {
160 
161  // Call the hardware-provided routines to print the
162  // HELLO_MSG to the screen. Must use this call because
163  // the ESOS communications subsystems is not yet fully
164  // initialized, since this call is in user_init()
165  //
166  // In general, users should call hardware-specific
167  // function like this.
168  __esos_unsafe_PutString( HELLO_MSG );
169 
170 #ifdef __linux
171  // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off
172  // the ESOS S/W timers when they expire
173  esos_RegisterTask( __simulated_isr );
174 #endif
175 
176  // configure our hardware to support to support our application
177  CONFIG_LED1();
178 
179  // user_init() should register at least one user task
180  esos_RegisterTask(heartbeat_LED);
181  esos_RegisterTask(upper_case);
182 
183 } // end user_init()