PIC24 Support Libraries
all_generic.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  * \brief Embedded Systems Operating System (ESOS)
34  * Definitions to make ESOS code more generic and portable.
35  * \note This file is to be included in ESOS builds on all platforms.
36 */
37 
38 #pragma once
39 
40 #include <stdint.h>
41 #ifdef __linux
42 //#warning Compiling for 32-bit Linux
43 #include "pc_generic.h"
44 #define HELLO_MSG "\n" __FILE__ ", built on " __DATE__ " at " __TIME__ "\n"
45 #else
46 //#warning Compiling for Microchip PIC24 microcontrollers
47 #include "pic24_unions.h"
48 #endif
49 
50 
51 /**
52  * Union structure to hold an uint8 (byte) to provide access to
53  * 8-bit data with different "viewpoints" (or casts, if you will).
54  * \note Works on machines that store their data little-endian.
55  * If you use big-endian, the members will have to be reordered!
56  * \note Prefix for all UINT8 structs is <em>U8_</em>
57  */
58 typedef union _UINT8 {
59  /** uint8 viewed as an uint8 */
61  /** uint8 viewed as an uint8 */
63  struct {
64  unsigned LSN: 4; /** Least significant nibble of the uint8 */
65  unsigned MSN: 4; /** Most signficant nibble of the uint8 */
66  };
67  struct {
68  /** bit 0 (the LSb) of the uint8 */
69 unsigned b0:
70  1;
71  /** bit 1 of the uint8 */
72 unsigned b1:
73  1;
74  /** bit 2 of the uint8 */
75 unsigned b2:
76  1;
77  /** bit 3 of the uint8 */
78 unsigned b3:
79  1;
80  /** bit 4 of the uint8 */
81 unsigned b4:
82  1;
83  /** bit 5 of the uint8 */
84 unsigned b5:
85  1;
86  /** bit 6 of the uint8 */
87 unsigned b6:
88  1;
89  /** bit 7 (MSb) of the uint8 */
90 unsigned b7:
91  1;
92  };
93 } UINT8;
94 
95 /**
96  * Union structure to hold uint16s to provide access to
97  * 16-bit data with different "viewpoints" (or casts, if you will).
98  * \note Works on machines that store their
99  * data little-endian. If you use big-endian, the members will
100  * have to be reordered!
101  * \note Prefix for all UINT16 structs is <em>U16_</em>
102  * \code
103  * uint8_t u8_a, u8_b;
104  * UINT16 U16_y;
105  *
106  * U16_y._uint16 = 0xF00D;
107  * u8_b = U16_y.u8Bytes[0]; // u8_b equals 0x0D
108  * u8_a = U16_y.u8MSb; // u8_a equals 0xF0
109  * while (U16_y.b7); // falls through since bit 7 is 0 (FALSE)
110  * \endcode
111  */
112 typedef union _UINT16 {
113  /** uint16 viewed as an uint16 */
114  uint16_t _uint16;
115  /** uint16 viewed as an uint16 */
116  uint16_t u16;
117  /** int16 viewed as an int16*/
118  int16_t int16;
119  /** int16 viewed as an int16*/
120  int16_t i16;
121  struct {
122  /** LSB (uint8) of the uint16 */
124  /** MSB (uint8) of the uint16 */
126  };
127  struct {
128  unsigned LSBLSN: 4;
129  unsigned LSBMSN: 4;
130  unsigned MSBLSN: 4;
131  unsigned MSBMSN: 4;
132  };
133  struct {
134  /** The uint16 viewed as an array of two (2) uint8s */
135  uint8_t u8Bytes[2];
136  };
137  struct {
138  /** bit 0 (the LSb) of the uint16 */
139 unsigned b0:
140  1;
141  /** bit 1 of the uint16 */
142 unsigned b1:
143  1;
144  /** bit 2 of the uint16 */
145 unsigned b2:
146  1;
147  /** bit 3 of the uint16 */
148 unsigned b3:
149  1;
150  /** bit 4 of the uint16 */
151 unsigned b4:
152  1;
153  /** bit 5 of the uint16 */
154 unsigned b5:
155  1;
156  /** bit 6 of the uint16 */
157 unsigned b6:
158  1;
159  /** bit 7 of the uint16 */
160 unsigned b7:
161  1;
162  /** bit 8 of the uint16 */
163 unsigned b8:
164  1;
165  /** bit 9 of the uint16 */
166 unsigned b9:
167  1;
168  /** bit 10 of the uint16 */
169 unsigned b10:
170  1;
171  /** bit 11 of the uint16 */
172 unsigned b11:
173  1;
174  /** bit 12 of the uint16 */
175 unsigned b12:
176  1;
177  /** bit 13 of the uint16 */
178 unsigned b13:
179  1;
180  /** bit 14 of the uint16 */
181 unsigned b14:
182  1;
183  /** bit 15 (the MSb) of the uint16 */
184 unsigned b15:
185  1;
186  };
187 } UINT16;
188 
189 /**
190  * Returns the Least-Significant-Byte (LSB)
191  * \param a UINT16 structure holding a 16-bit value
192  * \returns LSB of the 16-bit value in the UINT16 struct
193  * \hideinitializer
194  */
195 #define LSB(a) ((a).u8Bytes[0])
196 /**
197  * Returns the Most-Significant-Byte (MSB)
198  * \param a UINT16 structure holding a 16-bit value
199  * \returns MSB of the 16-bit value in the UINT16 struct
200  * \hideinitializer
201  */
202 #define MSB(a) ((a).u8Bytes[1])
203 
204 /**
205  * Union structure to hold an uint32 to provide access to
206  * 32-bit data with different "viewpoints" (or casts, if you will).
207  * \note Works on machines that store their
208  * data little-endian. If you use big-endian, the members will
209  * have to be reordered!
210  * \note Prefix for all UINT32 structs is <em>U32_</em>
211  * \code
212  * UINT32 U32_x;
213  * uint16 u16_a;
214  * uint8 u8_b;
215  * UINT16 U16_y;
216  *
217  * U32_x._uint32 = 0xDEADBEEF;
218  * u16_a = U32_x.u16LoWord; // u16_a equals 0xBEEF
219  * u8_b = U32_x.u8Bytes[0]; // u8_b equals 0xEF
220  * u16_a = U32_x.u16Words[1]; // u16_a equals 0xDEAD
221  * U16_y._uint16 = U32_x.u16LoWord; // where's the "BEEF"?
222  * u8_b = U16_y.uMSb; // u8_b equals 0xBE
223  * while (U32_x.b31); // infinite loop
224  * \endcode
225  */
226 typedef union _UINT32 {
227  /** uint32 viewed as an uint32 */
228  uint32_t _uint32;
229  /** uint32 viewed as an uint32 */
230  uint32_t u32;
231  struct {
232  /** The LSB of the least-signficant uint16 in the 32-bit data */
234  /** The MSB of the least-signficant uint16 in the 32-bit data */
236  /** The LSB of the most-signficant uint16 in the 32-bit data */
238  /** The MSB of the most-signficant uint16 in the 32-bit data */
240  };
241  struct {
242  /** The least-significant uint16 in the 32-bit data */
243  uint16_t u16LoWord;
244  /** The most-significant uint16 in the 32-bit data */
245  uint16_t u16HiWord;
246  };
247  struct {
248  /** The uint32 viewed as an array of two (2) uint16s */
249  uint16_t u16Words[2];
250  };
251  struct {
252  /** The uint32 viewed as an array of four (4) uint8s */
253  uint8_t u8Bytes[4];
254  };
255  struct {
256 unsigned b0:
257  1;
258  /** bit 1 (the LSb) of the uint32 */
259 unsigned b1:
260  1;
261  /** bit 2 of the uint32 */
262 unsigned b2:
263  1;
264  /** bit 3 of the uint32 */
265 unsigned b3:
266  1;
267  /** bit 4 of the uint32 */
268 unsigned b4:
269  1;
270  /** bit 5 of the uint32 */
271 unsigned b5:
272  1;
273  /** bit 6 of the uint32 */
274 unsigned b6:
275  1;
276  /** bit 7 of the uint32 */
277 unsigned b7:
278  1;
279  /** bit 8 of the uint32 */
280 unsigned b8:
281  1;
282  /** bit 9 of the uint32 */
283 unsigned b9:
284  1;
285  /** bit 10 of the uint32 */
286 unsigned b10:
287  1;
288  /** bit 11 of the uint32 */
289 unsigned b11:
290  1;
291  /** bit 12 of the uint32 */
292 unsigned b12:
293  1;
294  /** bit 13 of the uint32 */
295 unsigned b13:
296  1;
297  /** bit 14 of the uint32 */
298 unsigned b14:
299  1;
300  /** bit 15 of the uint32 */
301 unsigned b15:
302  1;
303  /** bit 16 of the uint32 */
304 unsigned b16:
305  1;
306  /** bit 17 of the uint32 */
307 unsigned b17:
308  1;
309  /** bit 18 of the uint32 */
310 unsigned b18:
311  1;
312  /** bit 19 of the uint32 */
313 unsigned b19:
314  1;
315  /** bit 20 of the uint32 */
316 unsigned b20:
317  1;
318  /** bit 21 of the uint32 */
319 unsigned b21:
320  1;
321  /** bit 22 of the uint32 */
322 unsigned b22:
323  1;
324  /** bit 23 of the uint32 */
325 unsigned b23:
326  1;
327  /** bit 24 of the uint32 */
328 unsigned b24:
329  1;
330  /** bit 25 of the uint32 */
331 unsigned b25:
332  1;
333  /** bit 26 of the uint32 */
334 unsigned b26:
335  1;
336  /** bit 27 of the uint32 */
337 unsigned b27:
338  1;
339  /** bit 28 of the uint32 */
340 unsigned b28:
341  1;
342  /** bit 29 of the uint32 */
343 unsigned b29:
344  1;
345  /** bit 30 of the uint32 */
346 unsigned b30:
347  1;
348  /** bit 31 (MSb) of the uint32 */
349 unsigned b31:
350  1;
351  };
352 } UINT32;
353 
354 // Now, some macros to help navigate these structures.....
355 /**
356  * Returns the Least-Significant-WORD (uint16) of a UINT32 structure
357  * \param a UINT32 structure holding a 32-bit value
358  * \returns LSW of the 32-bit value in the UINT32 struct
359  * \hideinitializer
360  */
361 #define LOWER_WORD(a) ((a).u16Words[0])
362 /**
363  * Returns the Most-Significant-WORD (uint16) of a UINT32 structure
364  * \param a UINT32 structure holding a 32-bit value
365  * \returns MSW of the 32-bit value in the UINT32 struct
366  * \hideinitializer
367  */
368 #define UPPER_WORD(a) ((a).u16Words[1])
369 /**
370  * Returns the Least-Significant-BYTE (uint8) of a UINT32 structure
371  * \param a UINT32 structure holding a 32-bit value
372  * \returns Byte 0 (LSB) of the 32-bit value in the UINT32 struct
373  * \hideinitializer
374  */
375 #define LOWER_LSB(a) ((a).u8Bytes[0])
376 /**
377  * Returns the 2nd most least-Significant-BYTE (uint8) of a UINT32 structure
378  * \param a UINT32 structure holding a 32-bit value
379  * \returns Byte 1 of the 32-bit value in the UINT32 struct
380  * \hideinitializer
381  */
382 #define LOWER_MSB(a) ((a).u8Bytes[1])
383 /**
384  * Returns the 3nd most least-Significant-BYTE (uint8) of a UINT32 structure
385  * \param a UINT32 structure holding a 32-bit value
386  * \returns Byte 2 of the 32-bit value in the UINT32 struct
387  * \hideinitializer
388  */
389 #define UPPER_LSB(a) ((a).u8Bytes[2])
390 /**
391  * Returns the Most-Significant-BYTE (uint8) of a UINT32 structure
392  * \param a UINT32 structure holding a 32-bit value
393  * \returns Byte 3 (MSB) of the 32-bit value in the UINT32 struct
394  * \hideinitializer
395  */
396 #define UPPER_MSB(a) ((a).u8Bytes[3])
397 
398 /**
399  * A single bit quantity. Takes on the value TRUE or FALSE.
400  * \hideinitializer
401  */
402 typedef enum _BOOL {
403  /** False, not true, off, zero */
404  FALSE = 0,
405  /** True, not false, on, one */
407 } BOOL;
408 
409 /** another way to say \ref TRUE */
410 #define OK TRUE
411 /** another way to say \ref FALSE */
412 #define FAIL FALSE
413 
414 #define ON TRUE
415 #define OFF FALSE
416 
417 #define HIGH TRUE
418 #define LOW FALSE
419 
420 #define ENABLE TRUE
421 #define DISABLE FALSE
422 
423 /** An uninitialized pointer */
424 #define NULLPTR 0
425 /** An unitialized index value */
426 #define NULLIDX 0xFF
427 /** Mask to represent bit 0 (the LSb) */
428 #define BIT0 0x0001
429 /** Mask to represent bit 1 */
430 #define BIT1 0x0002
431 /** Mask to represent bit 2 */
432 #define BIT2 0x0004
433 /** Mask to represent bit 3 */
434 #define BIT3 0x0008
435 /** Mask to represent bit 4 */
436 #define BIT4 0x0010
437 /** Mask to represent bit 5 */
438 #define BIT5 0x0020
439 /** Mask to represent bit 6 */
440 #define BIT6 0x0040
441 /** Mask to represent bit 7 (the MSb of a uint8) */
442 #define BIT7 0x0080
443 /** Mask to represent bit 8 (the LSb of the most-significant byte in an uint8)*/
444 #define BIT8 0x0100
445 /** Mask to represent bit 9 */
446 #define BIT9 0x0200
447 /** Mask to represent bit 10 */
448 #define BIT10 0x0400
449 /** Mask to represent bit 11 */
450 #define BIT11 0x0800
451 /** Mask to represent bit 12 */
452 #define BIT12 0x1000
453 /** Mask to represent bit 13 */
454 #define BIT13 0x2000
455 /** Mask to represent bit 14 */
456 #define BIT14 0x4000
457 /** Mask to represent bit 15 (the MSb of an uint16) */
458 #define BIT15 0x8000
459 
460 /**
461  * Sets the bits of a variable according to a mask
462  * \param var variable containing the bits to "set"
463  * \param mask varable with <em>ONE</em>s in the locations where you want to <em>set</em> bits
464  * \hideinitializer
465  */
466 #define BIT_SET_MASK(var, mask) ((var) |= (mask))
467 /**
468  * Clear the bits of a variable according to a mask
469  * \param var variable containing the bits to "clear"
470  * \param mask varable with <em>ONE</em>s in the locations where you want to <em>clear</em> bits
471  * \hideinitializer
472  */
473 #define BIT_CLEAR_MASK(var, mask) ((var) &= (~(mask)))
474 /**
475  * Toggle the bits of a variable according to a mask
476  * \param var variable containing the bits to "toggle"
477  * \param mask varable with <em>ONE</em>s in the locations where you want to <em>toggle</em> bits
478  * \hideinitializer
479  */
480 #define BIT_TOGGLE_MASK(var, mask) ((var) ^= (mask))
481 /**
482  * Determine if one or more bits are set in a variable
483  * \param var variable containing the bits to test
484  * \param mask varable with <em>ONE</em>s in the locations where you want to check for <em>set</em> bits
485  * \retval TRUE if one or more of the mask bits are set in the variable
486  * \retval FALSE if none of the mask bits are set
487  * \note Does <em>NOT</em> test to see if you variable has ALL mask bits set
488  * \hideinitializer
489  */
490 #define IS_BIT_SET_MASK(var, mask) (((var) & (mask)))
491 /**
492  * Determine if one or more bits are cleared in a variable
493  * \param var variable containing the bits to test
494  * \param mask varable with <em>ONE</em>s in the locations where you want to check for <em>cleared</em> bits
495  * \retval TRUE if one or more of the mask bits are cleared in the variable
496  * \retval FALSE if none of the mask bits are cleared
497  * \note Does <em>NOT</em> test to see if you variable has ALL mask bits cleared
498  * \hideinitializer
499  */
500 #define IS_BIT_CLEAR_MASK(var, mask) ((~(var) & (mask)))
501 
502 /**
503  * Sets a single bit in a variable
504  * \param var variable
505  * \param bitnum Number (0-?) of the bit that should be <em>set</em>
506  * \hideinitializer
507  */
508 #define BIT_SET(var, bitnum) ((var) |= (1 << (bitnum)))
509 /**
510  * Clears a single bit in a variable
511  * \param var variable
512  * \param bitnum Number (0-?) of the bit that should be <em>cleared</em>
513  * \hideinitializer
514  */
515 #define BIT_CLEAR(var, bitnum) ((var) &= (~(1 << (bitnum))))
516 /**
517  * Toggle a single bit in a variable
518  * \param var variable
519  * \param bitnum Number (0-?) of the bit that should be <em>toggleed</em>
520  * \hideinitializer
521  */
522 #define BIT_TOGGLE(var, bitnum) ((var) ^= (1 << (bitnum)))
523 /**
524  * Determine if a bit is set in a variable
525  * \param var variable containing the bits to test
526  * \param bitnum Number (0-?) of the bit to test for being <em>set</em>
527  * \retval TRUE if the bit is set
528  * \retval FALSE otherwise
529  * \hideinitializer
530  */
531 #define IS_BIT_SET(var, bitnum) ((var) & (1 << (bitnum)))
532 /**
533  * Determine if a bit is cleared in a variable
534  * \param var variable containing the bits to test
535  * \param bitnum Number (0-?) of the bit to test for being <em>cleared</em>
536  * \retval TRUE if the bit is clear
537  * \retval FALSE otherwise
538  * \hideinitializer
539  */
540 #define IS_BIT_CLEAR(var, bitnum) (~(var) & ((1 << (bitnum))))
uint8_t u8HiMsb
Definition: all_generic.h:239
uint8_t u8Msb
Definition: all_generic.h:125
uint16_t _uint16
Definition: all_generic.h:114
uint8_t u8LoMsb
Definition: all_generic.h:235
BOOL
Definition: all_generic.h:402
int16_t i16
Definition: all_generic.h:120
uint32_t u32
Definition: all_generic.h:230
uint8_t u8HiLsb
Definition: all_generic.h:237
uint8_t _uint8
Definition: all_generic.h:60
uint8_t u8LoLsb
Definition: all_generic.h:233
uint16_t u16
Definition: all_generic.h:116
uint8_t u8
Definition: all_generic.h:62
unsigned MSN
Definition: all_generic.h:65
uint32_t _uint32
Definition: all_generic.h:228
uint8_t u8Lsb
Definition: all_generic.h:123
uint16_t u16HiWord
Definition: all_generic.h:245
uint16_t u16LoWord
Definition: all_generic.h:243
int16_t int16
Definition: all_generic.h:118
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194