PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
app_flashled.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 flash an LED on RB15. Flashing LED is generated
32  * by an <em>user task</em>.
33  *
34  * \note Demonstrates task wait/yields and ESOS application code organization
35  */
36 
37 // INCLUDEs go here (First include the main esos.h file)
38 // After that, the user can include what they need
39 #include "esos.h"
40 #ifdef __linux
41 #include "esos_pc.h"
42 #include "esos_pc_stdio.h"
43 
44 // INCLUDE these so that printf() and our PC hacks work
45 #include <stdio.h>
46 #include <sys/select.h>
47 #include <termios.h>
48 #include <unistd.h>
49 #else
50 #include "esos_pic24.h"
51 #include "esos_pic24_rs232.h"
52 #endif
53 
54 // DEFINEs go here
55 #ifndef __linux
56 #define CONFIG_LED1() CONFIG_RB15_AS_DIG_OUTPUT()
57 #define LED1 _LATB15
58 #else
59 #define CONFIG_LED1() printf("Called CONFIG_LED1()\n");
60 uint8_t LED1 = TRUE; // LED1 is initially "on"
61 #endif
62 
63 // PROTOTYPEs go here
64 
65 // GLOBALs go here
66 // Generally, the user-created semaphores will be defined/allocated here
67 static uint8_t psz_CRNL[3]= {0x0D, 0x0A, 0};
68 
69 
70 #ifdef __linux
71 /*
72  * Simulate the timer ISR found on a MCU
73  * The PC doesn't have a timer ISR, so this task will periodically
74  * call the timer services callback instead.
75  * USED ONLY FOR DEVELOPMENT AND TESTING ON PC.
76  * Real MCU hardware doesn't need this task
77  */
78 ESOS_USER_TASK( __simulated_isr ) {
80  while (TRUE) {
81  // call the ESOS timer services callback just like a real H/W ISR would
82  __esos_tmrSvcsExecute();
84 
85  } // endof while(TRUE)
86  ESOS_TASK_END();
87 } // end child_task
88 #endif
89 
90 /************************************************************************
91  * User supplied functions
92  ************************************************************************
93  */
94 
95 /*
96  * An ESOS task to mimic the heartbeat LED found
97  * in the PIC24 support library code used in Chapters 8-13.
98  *
99  * Toggle LED1, wait 250ms, repeat forever.
100  *
101  * \note Since this heartbeat is performed in an ESOS task,
102  * a flashing LED indicates that the ESOS scheduler is still
103  * running properly. If the LED quits flashing, the ESOS
104  * scheduler is no longer rotating through the runnable task
105  * list. The most likely reason is that some task has ceased
106  * "yielding" the processor, and is caught in some deadlock
107  * or otherwise infinite loop.
108  * \hideinitializer
109  */
110 ESOS_USER_TASK(heartbeat_LED) {
111  ESOS_TASK_BEGIN();
112  while (TRUE) {
113  LED1 = !LED1;
114 
115 #ifdef __linux
116  if (LED1) {
117  printf("\a");
118  fflush(stdout);
119  }
120 #endif
121 
122  ESOS_TASK_WAIT_TICKS( 500 );
123  } // endof while(TRUE)
124  ESOS_TASK_END();
125 } // end heartbeat_LED task
126 
127 /****************************************************
128  * user_init()
129  ****************************************************
130  */
131 void user_init(void) {
132 
133  // Call the hardware-provided routines to print the
134  // HELLO_MSG to the screen. Must use this call because
135  // the ESOS communications subsystems is not yet fully
136  // initialized, since this call is in user_init()
137  //
138  // In general, users should call hardware-specific
139  // function like this.
140 
141  __esos_unsafe_PutString( HELLO_MSG );
142 
143 #ifdef __linux
144  // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off
145  // the ESOS S/W timers when they expire
146  esos_RegisterTask( __simulated_isr );
147 #endif
148 
149  // configure our hardware as needed by the tasks
150  CONFIG_LED1();
151 
152  // user_init() should register at least one user task
153  esos_RegisterTask(heartbeat_LED);
154 
155 } // end user_init()