PIC24 Support Libraries
dataXfer.h
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 #pragma once
31 
32 /** \file
33  * \brief Routines which implement the \ref dataXfer "uC comm protocol".
34  */
35 
36 #include "dataXferImpl.h"
37 
38 // Specify that this is C code for proper C++ linking
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /// Number of for loop iterations which equal a timeout for the microcontroller.
44 /// # loops = instructions/sec * loops/instruction * seconds
45 #define RECEIVE_TIMEOUT (FCY * 1/20 * 0.1)
46 
47 /// Send one character to the uC, escaping if necessary.
48 /// \param c Character to send.
49 void
50 outCharXfer(char c);
51 
52 
53 /// A system-dependent macro to output one character.
54 #ifndef OUT_CHAR
55 void outChar(uint8_t c);
56 # define OUT_CHAR(c) outChar(c)
57 #else
58 // Hack: a command line of -DOUT_CHAR(c)=testOutChar(c) doesn't work. Just -DOUT_CHAR then
59 // redefine it here.
60 # undef OUT_CHAR
61 # define UNIT_TESTS
62 void testOutChar(uint8_t c);
63 # define OUT_CHAR(c) testOutChar(c)
64 #endif
65 
66 /// Initialize the data transfer system. This must be called before calling
67 /// any of the data transfer functions in the library.
68 void
69 initDataXfer();
70 
71 #if defined(MICROCONTROLLER) || defined(__DOXYGEN__) || defined(UNIT_TESTS)
72 /** Specify a variable to be sent or received. <b>uC only.</b>
73  * \param u_varIndex A value from 0-\ref NUM_XFER_VARS, unique for each var
74  * \param pv_data A pointer to the data to be sent
75  * \param u_size Size of the data in bytes; must be from 1 to 256.
76  * \param b_isWriteable True if the PC is allowed to change this
77  * variable; false otherwise. This does *NOT*
78  * restrict the microcontroller to read-only access to this
79  * variable.
80  * \param psz_format printf format string to use in displaying the
81  * Variable.
82  * \param psz_name Name of this variable, typically the same as used
83  * in the code
84  * \param psz_desc Description of this variable.
85  */
86 void
87 specifyVar(uint u_varIndex, volatile void* pv_data, uint u_size,
88  BOOL b_isWriteable, char* psz_format, char* psz_name,
89  char* psz_desc);
90 
91 /** For simplicity, define a macro that specifies a variable with
92  * default names. <b>uC only.</b>
93  * \param u_varIndex A value from 0-\ref NUM_XFER_VARS, unique for each var
94  * \param data A pointer to the data to be sent
95  * \param isWriteable True if the PC is allowed to change this
96  * variable; false otherwise. This does *NOT*
97  * restrict the microcontroller to read-only access to this
98  * variable.
99  * \param format printf format string to use in displaying the
100  * variable
101  * \param desc Description of this variable.
102  */
103 #define SPECIFY_VAR(u_varIndex, data, isWriteable, format, desc) \
104 specifyVar(u_varIndex, &data, sizeof(data), isWriteable, format, #data, desc)
105 #endif
106 
107 /// Send an already-specified variable; \see specifyVar.
108 /// \param u_varIndex The index of the variable to send; must be from 0 to
109 /// \ref NUM_XFER_VARS.
110 void
111 sendVar(uint u_varIndex);
112 
113 #if !defined(MICROCONTROLLER) || defined(__DOXYGEN__)
114 /** Return a string with the data stored in the given variable formatted using
115  * the format string contained in the variable. <b>PC only.</b>
116  * \param u_varIndex The index of the variable to send; must be from 0 to
117  * \ref NUM_XFER_VARS.
118  * \param psz_buf Buffer to contain the formatted string.
119  * \param s_len Length of the buffer.
120  * \return On success, the total number of characters written is returned.
121  * This count does not include the additional null-character
122  * automatically appended at the end of the string.
123  * On failure, a negative number is returned.
124  */
125 int
126 formatVar(uint u_varIndex, char* psz_buf, size_t s_len);
127 #endif
128 
129 
130 #if defined(MICROCONTROLLER) || defined(__DOXYGEN__)
131 /** Receive a character or a variable. Any errors that occur are
132  * reported via outString. <b>uC only.</b>
133  * \param c Pointer to space for storing the received character, if
134  * a character was received (see return value).
135  * \return An index to the variable, if a variable was received, or
136  * \ref CHAR_RECEIVED_INDEX if a character was received.
137  */
138 uint
139 receiveVar(char* c);
140 
141 /** Receive a character, while transparently processing any
142  * variables sent by the PC. Variables are assigned, but
143  * this simple interface does not report these assignments
144  * to its caller. This provides a simple wrapper around \ref receiveVar,
145  * which also reports on received variables. <b>uC only.</b>
146  * \return The character received.
147  */
148 char
149 inCharXfer();
150 #else
151 /** Receive a character or a variable. Any errors that occur are
152 * reported via outString. <b>PC only.</b>
153 * \param c_in Character just received, which will be processed.
154 * \param pc_out Pointer to space for storing the received character, if
155 * a character was received (see pu16_index).
156 * \param pu_index A pointer to the index to the variable, if a variable was received;
157 * \ref CHAR_RECEIVED_INDEX if a character was received.
158 * \param u64_timeMs Current system time, in ms.
159 * \param psz_error NULL if no error occurred; otherwise, a pointer to the
160 * error string.
161 * \return True if a character or variable was received. True also implies that
162 * pu_index is valid.
163 */
164 BOOL receiveVar(char c_in, char* pc_out, uint* pu_index,
165  uint64_t u64_timeMs, const char** psz_error);
166 #endif
167 
168 
169 #ifdef __cplusplus
170 }
171 #endif
void specifyVar(uint u_varIndex, volatile void *pv_data, uint u_size, BOOL b_isWriteable, char *psz_format, char *psz_name, char *psz_desc)
Definition: dataXfer.c:36
unsigned int uint
An abbreviation for an unsigned integer.
Definition: dataXferImpl.h:190
void outCharXfer(char c)
Definition: dataXfer.c:27
void outChar(uint8_t c)
A system-dependent macro to output one character.
Definition: pic24_serial.c:58
BOOL
Definition: all_generic.h:402
uint receiveVar(char *c)
char inCharXfer()
int formatVar(uint u_varIndex, char *psz_buf, size_t s_len)
Definition: dataXfer.c:132
void initDataXfer()
Definition: dataXfer.c:17
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194
Routines which implement the uC comm protocol.
void sendVar(uint u_varIndex)
Definition: dataXfer.c:88