PIC24 Support Libraries
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 // This macro suppresses "unused parameter" warnings.
63 #define UNUSED(x) (void)(x)
64 
65 
66 /** Report an error on reset via \ref reportError,
67  * also printing the file and
68  * line number where this macro was called via
69  * a call to \ref ERROR_FILE_LINE.
70  * \param msg Error message to report
71  */
72 #define REPORT_ERROR(msg) reportError(ERROR_FILE_LINE(msg))
73 
74 #ifdef SIM
75 # define HELLO_MSG "****************************************************\n" \
76  "* SIMULATION MODE - DO NOT RUN ON A REAL PIC! *\n" \
77  "****************************************************\n" \
78  "\n" __FILE__ ", built on " __DATE__ " at " __TIME__ "\n"
79 // Output a warning during compilation also
80 # warning "***********************************************"
81 # warning "* SIMULATION MODE - DO NOT RUN ON A REAL PIC! *"
82 # warning "***********************************************"
83 #else
84 # define HELLO_MSG "\n" __FILE__ ", built on " __DATE__ " at " __TIME__ "\n"
85 #endif
86 
87 
88 void reportError(const char* szErrorMessage);
89 uint32_t readProgramMemory(uint32_t u32_address);
90 void checkDeviceAndRevision(void);
91 void checkOscOption(void);
92 void printResetCause(void);
93 void configPinsForLowPower(void);
94 void configBasic(const char* psz_helloMsg);
95 uint16_t compute_brg(uint32_t u32_fcy, uint16_t u16_brgh, uint32_t u32_baudrate);
96 #ifndef _NOFLOAT
97 uint32_t roundFloatToUint32(float f_x);
98 uint16_t roundFloatToUint16(float f_x);
99 #endif
100 
101 extern _PERSISTENT const char* sz_lastTimeoutError;
102 
103 #if USE_HEARTBEAT
104 extern uint32_t u32_heartbeatCount;
105 #endif
106 
107 void configHeartbeat(void);
108 void doHeartbeat(void);
109 void toggleHeartbeat(void);
110 
111 // Copied from http://embeddedgurus.com/stack-overflow/2011/03/the-n_elements-macro/.
112 #define N_ELEMENTS(X) (sizeof(X)/sizeof(*(X)))
void reportError(const char *szErrorMessage)
Definition: pic24_util.c:183
void checkDeviceAndRevision(void)
Definition: pic24_util.c:208
void printResetCause(void)
Definition: pic24_util.c:329
void configHeartbeat(void)
Definition: pic24_util.c:89
_PERSISTENT const char * sz_lastTimeoutError
Definition: pic24_util.c:134
uint32_t readProgramMemory(uint32_t u32_address)
Definition: pic24_util.c:196
void checkOscOption(void)
Definition: pic24_util.c:280
uint16_t roundFloatToUint16(float f_x)
Definition: pic24_util.c:460
uint16_t compute_brg(uint32_t u32_fcy, uint16_t u16_brgh, uint32_t u32_baudrate)
Definition: pic24_util.c:474
uint32_t roundFloatToUint32(float f_x)
Definition: pic24_util.c:448
void toggleHeartbeat(void)
Definition: pic24_util.c:116
void doHeartbeat(void)
Definition: pic24_util.c:104
uint32_t u32_heartbeatCount
Definition: pic24_util.c:71
void configBasic(const char *psz_helloMsg)
Definition: pic24_util.c:434