T-EMU  2
The Terma Emulator
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
MilStd1553.h
Go to the documentation of this file.
1 //===------------------------------------------------------------*- C++ -*-===//
2 //
3 // T-EMU: The Terma Emulator
4 // (c) Terma 2015
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
61 #ifndef TEMU_MIL_STD_1553_H
62 #define TEMU_MIL_STD_1553_H
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 #include "temu-c/Support/Objsys.h"
69 
70 #include <assert.h>
71 #include <stdbool.h>
72 #include <stdint.h>
73 
74 typedef enum temu_Mil1553Error {
77  teME_SyncError = 1 << 1
79 
80 typedef enum temu_Mil1553MsgType {
86 
87 
88 typedef struct temu_Mil1553Msg {
89  unsigned WordCount; // Number of words including CmdStat
90  temu_Mil1553MsgType MsgTyp; // Pre-computed for convenience, otherwise we need
91  // to construct nasty state machines for every model
92  temu_Mil1553Error Err; // We can inject errors in every message. The
93  // interpretation is up to the model, but typically:
94  // parity error applies to command or status word if cmd
95  // or stat type message, and to the last data word, if the
96  // msg is a data transaction.
97  uint16_t CmdStat; // Is command or status word
98  uint16_t *Data; // Pointer to data block
100 
101 typedef struct temu_Mil1553DevIface {
102  void (*send)(void *Device, temu_Mil1553Msg *Msg);
104 
105 TEMU_IFACE_REFERENCE_TYPE(temu_Mil1553Dev);
106 
107 typedef struct {
108  uint64_t LastReportSentWords; // Number of words sent during last report
109  uint64_t SentWords; // Current number of sent words
111 
112 typedef struct temu_Mil1553BusIface {
113  void (*connect)(void *Bus, int Subaddr, temu_Mil1553DevIfaceRef Device);
114  void (*disconnect)(void *Bus, int Subaddr);
115  void (*reportStats)(void *Bus);
116  void (*send)(void *Bus, void *Sender, temu_Mil1553Msg *Msg);
117 
118  // Controls whether events should be issued at send calls
119  void (*enableSendEvents)(void *Bus);
120  void (*disableSendEvents)(void *Bus);
122 
123 TEMU_IFACE_REFERENCE_TYPE(temu_Mil1553Bus);
124 
125 
126 // Compute messeage transfer time in nano seconds
127 // 1553 bus is 1 Mb/sec and we have 16 + 4 bits per word.
128 #define TEMU_1553_NS_PER_WORD 20000
129 static inline uint64_t
130 temu_mil1553TransferTime(unsigned Words)
131 {
132  uint64_t Nanos = Words * TEMU_1553_NS_PER_WORD;
133  return Nanos;
134 }
135 
136 #define TEMU_1553_BITS_PER_WORD 20
137 static inline uint64_t
138 temu_mil1553BitCount(unsigned Words)
139 {
140  return Words * TEMU_1553_BITS_PER_WORD;
141 }
142 
143 // Conveniance functions to extract fields from the command word
144 
145 static inline uint16_t
146 temu_mil1553CmdRtAddr(uint16_t Cmd)
147 {
148  return (Cmd >> 11) & 0x1f;
149 }
150 
151 
152 static inline uint16_t
153 temu_mil1553CmdTR(uint16_t Cmd)
154 {
155  return (Cmd >> 10) & 1;
156 }
157 
158 static inline uint16_t
159 temu_mil1553CmdSubAddr(uint16_t Cmd)
160 {
161  return (Cmd >> 5) & 0x1f;
162 }
163 
164 // Return non-zero if the command is a mode command (sub addr == 0 or 0x1f)
165 
166 static inline int
167 temu_mil1553CmdIsModeCodeCmd(uint16_t Cmd)
168 {
169  if (temu_mil1553CmdSubAddr(Cmd) == 0x00
170  || temu_mil1553CmdSubAddr(Cmd) == 0x1f) {
171  return 1;
172  }
173 
174  return 0;
175 }
176 
177 
178 
179 
180 static inline uint16_t
181 temu_mil1553CmdTRModeCode(uint16_t Cmd)
182 {
183  return ((Cmd >> 5) & 0x20) | (Cmd & 0x1f);
184 }
185 
186 static inline uint16_t
187 temu_mil1553CmdCount(uint16_t Cmd)
188 {
189  return Cmd & 0x1f;
190 }
191 
192 
193 static inline uint16_t
194 temu_mil1553StatRtAddr(uint16_t Stat)
195 {
196  return (Stat >> 11) & 0x1f;
197 }
198 
199 
200 static inline uint16_t
201 temu_mil1553StatME(uint16_t Stat)
202 {
203  return (Stat >> 10) & 1;
204 }
205 
206 static inline uint16_t
207 temu_mil1553StatInst(uint16_t Stat)
208 {
209  return (Stat >> 9) & 1;
210 }
211 
212 static inline uint16_t
213 temu_mil1553StatSR(uint16_t Stat)
214 {
215  return (Stat >> 8) & 1;
216 }
217 
218 static inline uint16_t
219 temu_mil1553StatBC(uint16_t Stat)
220 {
221  return (Stat >> 4) & 1;
222 }
223 
224 
225 static inline uint16_t
226 temu_mil1553StatBusy(uint16_t Stat)
227 {
228  return (Stat >> 3) & 1;
229 }
230 
231 static inline uint16_t
232 temu_mil1553StatSubFlag(uint16_t Stat)
233 {
234  return (Stat >> 2) & 1;
235 }
236 
237 static inline uint16_t
238 temu_mil1553StatDynBusCtrl(uint16_t Stat)
239 {
240  return (Stat >> 1) & 1;
241 }
242 
243 static inline uint16_t
244 temu_mil1553StatTermFlag(uint16_t Stat)
245 {
246  return Stat & 1;
247 }
248 
249 // Useful constants, use temu_mil1553CmdTRMode() to extract bits,
250 // these constants are used for the optional mode control codes that
251 // are defined in the 1553 standard.
252 #define TEMU_1553_TR_BIT 0x20
253 
254 #define teM1553MC_DynamicBusControl (TEMU_1553_TR_BIT|0x00)
255 #define teM1553MC_Synchronize (TEMU_1553_TR_BIT|0x01)
256 #define teM1553MC_TransmitStatusWord (TEMU_1553_TR_BIT|0x02)
257 #define teM1553MC_InitiateSelfTest (TEMU_1553_TR_BIT|0x03)
258 #define teM1553MC_TransmitterShutdown (TEMU_1553_TR_BIT|0x04)
259 #define teM1553MC_OverrideTransmitter (TEMU_1553_TR_BIT|0x05)
260 #define teM1553MC_InhibitTerminalFlagBit (TEMU_1553_TR_BIT|0x06)
261 #define teM1553MC_OverrideInhibitTerminalFlagBit (TEMU_1553_TR_BIT|0x07)
262 #define teM1553MC_ResetRT (TEMU_1553_TR_BIT|0x08)
263 #define teM1553MC_TransmitVectorWord (TEMU_1553_TR_BIT|0x10)
264 #define teM1553MC_SynchronizeWithData (0x11)
265 #define teM1553MC_TransmitLastCommand (TEMU_1553_TR_BIT|0x12)
266 #define teM1553MC_TransmitBITWord (TEMU_1553_TR_BIT|0x13)
267 #define teM1553MC_SelectedTransmitter (0x14)
268 #define teM1553MC_OverrideSelectedTransmitter (0x15)
269 
270 
271 #ifdef __cplusplus
272 }
273 #endif
274 
275 
276 #endif /* ! TEMU_MIL_STD_1553_H */
temu_Mil1553Error
Definition: MilStd1553.h:74
TEMU_IFACE_REFERENCE_TYPE(temu_Mil1553Dev)
temu_Mil1553MsgType
Definition: MilStd1553.h:80
uint64_t LastReportSentWords
Definition: MilStd1553.h:108
struct temu_Mil1553DevIface temu_Mil1553DevIface
temu_Mil1553Error Err
Definition: MilStd1553.h:92
uint64_t SentWords
Definition: MilStd1553.h:109
uint16_t CmdStat
Definition: MilStd1553.h:97
struct temu_Mil1553BusIface temu_Mil1553BusIface
void(* disableSendEvents)(void *Bus)
Definition: MilStd1553.h:120
void(* enableSendEvents)(void *Bus)
Definition: MilStd1553.h:119
unsigned WordCount
Definition: MilStd1553.h:89
void(* reportStats)(void *Bus)
Definition: MilStd1553.h:115
temu_Mil1553MsgType MsgTyp
Definition: MilStd1553.h:90
struct temu_Mil1553Msg temu_Mil1553Msg
void(* send)(void *Bus, void *Sender, temu_Mil1553Msg *Msg)
Definition: MilStd1553.h:116
void(* send)(void *Device, temu_Mil1553Msg *Msg)
Definition: MilStd1553.h:102
void(* connect)(void *Bus, int Subaddr, temu_Mil1553DevIfaceRef Device)
Definition: MilStd1553.h:113
#define TEMU_1553_BITS_PER_WORD
Definition: MilStd1553.h:136
uint16_t * Data
Definition: MilStd1553.h:98
#define TEMU_1553_NS_PER_WORD
Definition: MilStd1553.h:128
void(* disconnect)(void *Bus, int Subaddr)
Definition: MilStd1553.h:114