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.c
1#include "CException.h"
2
3#pragma GCC diagnostic push
4#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
5volatile CEXCEPTION_FRAME_T CExceptionFrames[CEXCEPTION_NUM_ID] = {{0}};
6#pragma GCC diagnostic pop
7
8//------------------------------------------------------------------------------------------
9// Throw
10//------------------------------------------------------------------------------------------
11void Throw(CEXCEPTION_T ExceptionID) {
12 unsigned int MY_ID = CEXCEPTION_GET_ID;
13 CExceptionFrames[MY_ID].Exception = ExceptionID;
14 if (CExceptionFrames[MY_ID].pFrame) {
15 longjmp(*CExceptionFrames[MY_ID].pFrame, 1);
16 }
17 CEXCEPTION_NO_CATCH_HANDLER(ExceptionID);
18}
19
20//------------------------------------------------------------------------------------------
21// Explanation of what it's all for:
22//------------------------------------------------------------------------------------------
23/*
24#define Try
25 { <- give
26us some local scope. most compilers are happy with this jmp_buf *PrevFrame,
27NewFrame; <- prev frame points to the last try
28block's frame. new frame gets created on stack for this Try block unsigned int
29MY_ID = CEXCEPTION_GET_ID; <- look up this task's id for
30use in frame array. always 0 if single-tasking PrevFrame =
31CExceptionFrames[CEXCEPTION_GET_ID].pFrame; <- set pointer to point at
32old frame (which array is currently pointing at) CExceptionFrames[MY_ID].pFrame
33= &NewFrame; <- set array to point at my new frame instead,
34now CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <-
35initialize my exception id to be NONE if (setjmp(NewFrame) == 0) { <- do setjmp.
36it returns 1 if longjump called, otherwise 0 if (&PrevFrame) <- this is here to
37force proper scoping. it requires braces or a single line to be but after Try,
38otherwise won't compile. This is always true at this point.
39
40#define Catch(e)
41 else { } <- this
42also forces proper scoping. Without this they could stick their own 'else' in
43and it would get ugly CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- no
44errors happened, so just set the exception id to NONE (in case it was corrupted)
45 }
46 else <- an
47exception occurred { e = CExceptionFrames[MY_ID].Exception; e=e;} <- assign the
48caught exception id to the variable passed in. CExceptionFrames[MY_ID].pFrame =
49PrevFrame; <- make the pointer in the array point at the
50previous frame again, as if NewFrame never existed. } <- finish off that local
51scope we created to have our own variables if
52(CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE) <- start the
53actual 'catch' processing if we have an exception id saved away
54 */