ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
CException.h
1#ifndef _CEXCEPTION_H
2#define _CEXCEPTION_H
3
4#include <setjmp.h>
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10// To Use CException, you have a number of options:
11// 1. Just include it and run with the defaults
12// 2. Define any of the following symbols at the command line to override them
13// 3. Include a header file before CException.h everywhere which defines any of
14// these
15// 4. Create an Exception.h in your path, and just define
16// EXCEPTION_USE_CONFIG_FILE first
17
18#ifdef CEXCEPTION_USE_CONFIG_FILE
19#include "CExceptionConfig.h"
20#endif
21
22// This is the value to assign when there isn't an exception
23#ifndef CEXCEPTION_NONE
24#define CEXCEPTION_NONE (0x5A5A5A5A)
25#endif
26
27// This is number of exception stacks to keep track of (one per task)
28#ifndef CEXCEPTION_NUM_ID
29#define CEXCEPTION_NUM_ID (1) // there is only the one stack by default
30#endif
31
32// This is the method of getting the current exception stack index (0 if only
33// one stack)
34#ifndef CEXCEPTION_GET_ID
35#define CEXCEPTION_GET_ID \
36 (0) // use the first index always because there is only one anyway
37#endif
38
39// The type to use to store the exception values.
40#ifndef CEXCEPTION_T
41#define CEXCEPTION_T unsigned int
42#endif
43
44// This is an optional special handler for when there is no global Catch
45#ifndef CEXCEPTION_NO_CATCH_HANDLER
46#define CEXCEPTION_NO_CATCH_HANDLER(id)
47#endif
48
49// These hooks allow you to inject custom code into places, particularly useful
50// for saving and restoring additional state
51#ifndef CEXCEPTION_HOOK_START_TRY
52#define CEXCEPTION_HOOK_START_TRY
53#endif
54#ifndef CEXCEPTION_HOOK_HAPPY_TRY
55#define CEXCEPTION_HOOK_HAPPY_TRY
56#endif
57#ifndef CEXCEPTION_HOOK_AFTER_TRY
58#define CEXCEPTION_HOOK_AFTER_TRY
59#endif
60#ifndef CEXCEPTION_HOOK_START_CATCH
61#define CEXCEPTION_HOOK_START_CATCH
62#endif
63
64// exception frame structures
65typedef struct {
66 jmp_buf *pFrame;
67 CEXCEPTION_T volatile Exception;
69
70// actual root frame storage (only one if single-tasking)
71extern volatile CEXCEPTION_FRAME_T CExceptionFrames[];
72
73// Try (see C file for explanation)
74#define Try \
75 { \
76 jmp_buf *PrevFrame, NewFrame; \
77 unsigned int MY_ID = CEXCEPTION_GET_ID; \
78 PrevFrame = CExceptionFrames[MY_ID].pFrame; \
79 CExceptionFrames[MY_ID].pFrame = (jmp_buf *)(&NewFrame); \
80 CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \
81 CEXCEPTION_HOOK_START_TRY; \
82 if (setjmp(NewFrame) == 0) { \
83 if (1)
84
85// Catch (see C file for explanation)
86#define Catch(e) \
87 else { \
88 } \
89 CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \
90 CEXCEPTION_HOOK_HAPPY_TRY; \
91 } \
92 else { \
93 e = CExceptionFrames[MY_ID].Exception; \
94 (void)e; \
95 CEXCEPTION_HOOK_START_CATCH; \
96 } \
97 CExceptionFrames[MY_ID].pFrame = PrevFrame; \
98 CEXCEPTION_HOOK_AFTER_TRY; \
99 } \
100 if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE)
101
102// Throw an Error
103void Throw(CEXCEPTION_T ExceptionID);
104
105// Just exit the Try block and skip the Catch.
106#define ExitTry() Throw(CEXCEPTION_NONE)
107
108#ifdef __cplusplus
109} // extern "C"
110#endif
111
112#endif // _CEXCEPTION_H