FreeEMS  0.2.0-SNAPSHOT-282-g9efc524
commsCore.h
Go to the documentation of this file.
1 /* FreeEMS - the open source engine management system
2  *
3  * Copyright 2008-2013 Fred Cooke
4  *
5  * This file is part of the FreeEMS project.
6  *
7  * FreeEMS software is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FreeEMS software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
19  *
20  * We ask that if you make any changes to this file you email them upstream to
21  * us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
22  *
23  * Thank you for choosing FreeEMS to run your engine!
24  */
25 
26 
27 /** @file
28  *
29  * @ingroup allHeaders
30  */
31 
32 
33 /* Header file multiple inclusion protection courtesy eclipse Header Template */
34 /* and http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/ C pre processor manual */
35 #ifndef FILE_COMMS_CORE_H_SEEN
36 #define FILE_COMMS_CORE_H_SEEN
37 
38 
39 #ifdef EXTERN
40 #warning "EXTERN already defined by another header, please sort it out!"
41 #undef EXTERN /* If fail on warning is off, remove the definition such that we can redefine correctly. */
42 #endif
43 
44 
45 #ifdef COMMSCORE_C
46 #define EXTERN
47 #else
48 #define EXTERN extern
49 #endif
50 
51 
52 // Buffer size minus the overhead of a maximal packet header
53 #define TX_MAX_PAYLOAD_SIZE (TX_BUFFER_SIZE - 32)
54 
55 
56 /* Function declarations */
57 /* This function accesses paged flash and thus must be in linear space. Set explicitly to text. */
58 void decodePacketAndRespond(void) TEXT;
59 
60 void resetReceiveState(unsigned char) FPAGE_FE;
61 void finaliseAndSend(unsigned short) FPAGE_FE;
62 
63 unsigned short populateBasicDatalog(void) FPAGE_FE;
64 
65 
66 /* Global variables for TX (one set per interface) */
70 
71 
72 /* Buffer use and source IDs/flags */
73 EXTERN unsigned char TXBufferInUseFlags;
75 
76 
77 // Shared serial comms stuff TODO move this to a commsCommon.h header
78 #define START_BYTE 0xAA
79 #define ESCAPE_BYTE 0xBB
80 #define STOP_BYTE 0xCC
81 
82 #define ESCAPED_START_BYTE 0x55
83 #define ESCAPED_ESCAPE_BYTE 0x44
84 #define ESCAPED_STOP_BYTE 0x33
85 
86 
87 /* Masks for TXBufferInUseFlags and RXBufferContentSourceID */
88 #define COM_SET_SCI0_INTERFACE_ID BIT0
89 #define COM_SET_CAN0_INTERFACE_ID BIT1
90 #define COM_SET_SPARE2_INTERFACE_ID BIT2
91 #define COM_SET_SPARE3_INTERFACE_ID BIT3
92 #define COM_SET_SPARE4_INTERFACE_ID BIT4
93 #define COM_SET_SPARE5_INTERFACE_ID BIT5
94 #define COM_SET_SPARE6_INTERFACE_ID BIT6
95 #define COM_SET_SPARE7_INTERFACE_ID BIT7
96 #define COM_CLEAR_SCI0_INTERFACE_ID NBIT0
97 #define COM_CLEAR_CAN0_INTERFACE_ID NBIT1
98 #define COM_CLEAR_SPARE2_INTERFACE_ID NBIT2
99 #define COM_CLEAR_SPARE3_INTERFACE_ID NBIT3
100 #define COM_CLEAR_SPARE4_INTERFACE_ID NBIT4
101 #define COM_CLEAR_SPARE5_INTERFACE_ID NBIT5
102 #define COM_CLEAR_SPARE6_INTERFACE_ID NBIT6
103 #define COM_CLEAR_SPARE7_INTERFACE_ID NBIT7
104 #define CLEAR_ALL_SOURCE_ID_FLAGS ZEROS
105 
106 
107 /* Global variables for RX (one set for all) */
108 EXTERN unsigned char RXStateFlags;
110 EXTERN unsigned short RXCalculatedPayloadLength; // why global??
111 /* Masks for SCIRXStateFlags */
112 //#define RX_BUFFER_IN_USE BIT0
113 #define RX_READY_TO_PROCESS BIT1
114 #define RX_SCI_ESCAPED_NEXT BIT2
115 //#define RX_SCI_INSIDE_PACKET BIT3
116 //#define RX_BUFFER_NOT_IN_USE NBIT0
117 #define RX_CLEAR_READY_TO_PROCESS NBIT1
118 #define RX_SCI_NOT_ESCAPED_NEXT NBIT2
119 //#define RX_SCI_NOT_INSIDE_PACKET NBIT3
120 
121 /* Global CAN specific variables */
122 // TODO can id filters routing etc
123 
124 
125 /* Comms Control Enable and Disable Masks */
126 /* SCI masks */
127 #define SCICR2_RX_ENABLE BIT2
128 #define SCICR2_TX_ENABLE BIT3
129 #define SCICR2_RX_ISR_ENABLE BIT5
130 #define SCICR2_TX_ISR_ENABLE BIT7
131 #define SCICR2_RX_DISABLE NBIT2
132 #define SCICR2_TX_DISABLE NBIT3
133 #define SCICR2_RX_ISR_DISABLE NBIT5
134 #define SCICR2_TX_ISR_DISABLE NBIT7
135 /* CAN masks */
136 // TODO probably 8 of these too
137 // TODO probably 8 of these too
138 // TODO probably 8 of these too
139 // TODO probably 8 of these too
140 // TODO probably 8 of these too
141 // TODO probably 8 of these too
142 // TODO probably 8 of these too
143 // TODO probably 8 of these too
144 
145 
146 /* Header components */
147 EXTERN unsigned char RXHeaderFlags;
148 EXTERN unsigned char* TXHeaderFlags;
149 EXTERN unsigned short RXHeaderPayloadID; /// TODO @todo why global?
151 
152 
153 /* Header flag masks */
154 /* Always has flags (obviously) */
155 /* Always has payload ID so no flag */
156 /* Always has checksum, so no flag */
157 #define HEADER_HAS_LENGTH BIT0
158 #define HEADER_IS_NACK BIT1
159 #define HEADER_HAS_SEQUENCE BIT2
160 #define HEADER_RESERVED_E BIT3
161 #define HEADER_RESERVED_D BIT4
162 #define HEADER_RESERVED_C BIT5
163 #define HEADER_RESERVED_B BIT6
164 #define HEADER_RESERVED_A BIT7
165 
166 
167 #define asyncDatalogOff 0x00 ///< Logs by polling only, reduces CPU load a little but gives much lower data rate
168 #define asyncDatalogBasic 0x01 ///< Good old default log, always contains the normal stuff, good for most people, most of the time
169 #define asyncDatalogScratchPad 0x02 ///< User log of anything, any subset of a block allowed, start offset, size, content pointers/flags
170 #define asyncDatalogStructs 0x03 ///< Key structs, or subsets of them, or chunk to chunk, streamed, more efficient than scratch pad and bigger if needed
171 #define asyncDatalogPosition 0x04 ///< Record a buffer of position information, send when full: http://forum.diyefi.org/viewtopic.php?f=8&t=1339
172 #define asyncDatalogBlockBytes 0x05 ///< Populate a large block with bytes as fast as possible, send when full
173 #define asyncDatalogBlockWords 0x06 ///< Populate a large block with bytes as fast as possible, send when full
174 #define asyncDatalogBlockLongs 0x07 ///< Populate a large block with bytes as fast as possible, send when full
175 #define asyncDatalogStreamByte 0x08 ///< Send out a single byte as often as possible, ~1kHz with occasional ~3ms gaps from math running instead
176 #define asyncDatalogStreamWord 0x09 ///< Send out a single word as often as possible, ~1kHz with occasional ~3ms gaps from math running instead
177 #define asyncDatalogStreamLong 0x0A ///< Send out a single long as often as possible, ~1kHz with occasional ~3ms gaps from math running instead
178 #define asyncDatalogLastType asyncDatalogStreamLong
179 
180 
181 // Stuff that should only be found in this file.
182 #include "packetTypes.h"
183 #include "unitTestIDs.h"
184 
185 
186 #undef EXTERN
187 
188 
189 #else
190  /* let us know if we are being untidy with headers */
191  #warning "Header file COMMS_CORE_H seen before, sort it out!"
192 /* end of the wrapper ifdef from the very top */
193 #endif