PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pic24_util.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 // Documentation for this file. If the \file tag isn't present,
31 // this file won't be documented.
32 /** \file
33  * This file contains miscellaneous functions that
34  * that do not fall under any particular category.
35  * See pic24_util.c for details on how
36  * these functions were implemented.
37  */
38 
39 #pragma once
40 
41 #include <stdint.h>
42 #include "pic24_chip.h"
43 #include "pic24_libconfig.h"
44 
45 /** \name Bitfield to struct conversion
46  * <a name="bitfieldMacros">Given</a> a bitfield struct representing an SFR,
47  * convert this to a word (uint16_t) or to the
48  * high and low bytes (uint8_t) of that word.
49  * Note: though this looks ugly, it introduces
50  * no unexpected compiler overhead at -O1. See
51  * \ref u16_INTTREGlast for an example.
52  */
53 //@{
54 /// Convert a bitfield to a word (uint16_t).
55 #define BITS2WORD(sfrBitfield) ( *((uint16_t*) &sfrBitfield) )
56 /// Return the low byte (as a uint8_t) of a bitfield.
57 #define BITS2BYTEL(sfrBitfield) ( ((uint8_t*) &sfrBitfield)[0] )
58 /// Return the high byte (as a uint8_t) of a bitfield.
59 #define BITS2BYTEH(sfrBitfield) ( ((uint8_t*) &sfrBitfield)[1] )
60 //@}
61 
62 /** Report an error on reset via \ref reportError,
63  * also printing the file and
64  * line number where this macro was called via
65  * a call to \ref ERROR_FILE_LINE.
66  * \param msg Error message to report
67  */
68 #define REPORT_ERROR(msg) reportError(ERROR_FILE_LINE(msg))
69 
70 #ifdef SIM
71 # define HELLO_MSG "****************************************************\n" \
72  "* SIMULATION MODE - DO NOT RUN ON A REAL PIC! *\n" \
73  "****************************************************\n" \
74  "\n" __FILE__ ", built on " __DATE__ " at " __TIME__ "\n"
75 // Output a warning during compilation also
76 # warning "***********************************************"
77 # warning "* SIMULATION MODE - DO NOT RUN ON A REAL PIC! *"
78 # warning "***********************************************"
79 #else
80 # define HELLO_MSG "\n" __FILE__ ", built on " __DATE__ " at " __TIME__ "\n"
81 #endif
82 
83 
84 void reportError(const char* szErrorMessage);
85 uint32_t readProgramMemory(uint32_t u32_address);
86 void checkDeviceAndRevision(void);
87 void checkOscOption(void);
88 void printResetCause(void);
89 void configPinsForLowPower(void);
90 void configBasic(const char* psz_helloMsg);
91 uint16_t compute_brg(uint32_t u32_fcy, uint16_t u16_brgh, uint32_t u32_baudrate);
92 #ifndef _NOFLOAT
93 uint32_t roundFloatToUint32(float f_x);
94 uint16_t roundFloatToUint16(float f_x);
95 #endif
96 
97 extern _PERSISTENT const char* sz_lastTimeoutError;
98 
99 #if USE_HEARTBEAT
100 extern uint32_t u32_heartbeatCount;
101 #endif
102 
103 void configHeartbeat(void);
104 void doHeartbeat(void);
105 void toggleHeartbeat(void);
106 
107 // Copied from http://embeddedgurus.com/stack-overflow/2011/03/the-n_elements-macro/.
108 #define N_ELEMENTS(X) (sizeof(X)/sizeof(*(X)))