OpenCores
URL https://opencores.org/ocsvn/gecko3/gecko3/trunk

Subversion Repositories gecko3

[/] [gecko3/] [trunk/] [GECKO3COM/] [gecko3com-fw/] [firmware/] [include/] [usb_tmc.h] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nussgipfel
/* GECKO3COM
2
 *
3
 * Copyright (C) 2008 by
4
 *   ___    ____  _   _
5
 *  (  _`\ (  __)( ) ( )
6
 *  | (_) )| (_  | |_| |   Berne University of Applied Sciences
7
 *  |  _ <'|  _) |  _  |   School of Engineering and
8
 *  | (_) )| |   | | | |   Information Technology
9
 *  (____/'(_)   (_) (_)
10
 *
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation, either version 3 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
 
25
/*********************************************************************/
26
/** \file     usb_tmc.h
27
 *********************************************************************
28
 * \brief     header file for the USB Test and Measurement Class (TMC)
29
 *            functions.
30
 *
31
 *            According to USBTMC Specification Revision 1.0 and
32
 *            USBTMC-USB488 Subclass Specification Revision 1.0.
33
 *
34
 * \author    Christoph Zimmermann bfh.ch, Eldo José dos Santos
35
 * \date      2009-02-04
36
 *
37
*/
38
 
39
#ifndef _USBTMC_H_
40
#define _USBTMC_H_
41
 
42
/** length of the buffer to hold the response messages to be sent to the host */
43
#define TMC_RESPONSE_QUEUE_LENGTH 256  
44
 
45
/*****************************************************************************/
46
/* definitions, data structures and related stuff
47
 * according to USBTMC standard */
48
 
49
#define USB_TMC_HEADER_SIZE 12  /**< length of a USBTMC header */
50
 
51
 
52
/* USBTMC MsgID. Values, Ref.: Table 2 */
53
#define DEV_DEP_MSG_OUT              1   /**< device dependent command message */
54
#define REQUEST_DEV_DEP_MSG_IN       2   /**< command message that requests the device to send a USBTMC response */
55
#define DEV_DEP_MSG_IN               2   /**< response message to the REQUEST_DEV_DEP_MSG_IN */
56
#define VENDOR_SPECIFIC_OUT          126 /**< vendor specific command message */
57
#define REQUEST_VENDOR_SPECIFIC_IN   127 /**< command message that requests the device to send a vendor specific USBTMC response */
58
#define VENDOR_SPECIFIC_IN           127 /**< response message to the REQUEST_VENDOR_SPECIFIC_IN */
59
 
60
 
61
/* format of bmRequestType byte */
62
#define bmRT_TMC_OUT   0x21             
63
#define bmRT_TMC_IN    0xA1
64
 
65
 
66
/* USBTMC commands (bRequest values, Ref.: Table 15) */
67
#define INITIATE_ABORT_BULK_OUT      1
68
#define CHECK_ABORT_BULK_OUT_STATUS  2
69
#define INITIATE_ABORT_BULK_IN       3
70
#define CHECK_ABORT_BULK_IN_STATUS   4
71
#define INITIATE_CLEAR               5
72
#define CHECK_CLEAR_STATUS           6
73
#define GET_CAPABILITIES             7
74
#define INDICATOR_PULSE              64
75
 
76
/* USBTMC USB488 Subclass commands (bRequest values, Ref.: Table 9) */
77
#define READ_STATUS_BYTE             128
78
#define REN_CONTROL                  160
79
#define GO_TO_LOCAL                  161
80
#define LOCAL_LOCKOUT                162
81
 
82
/* bmTransfer attributes */
83
#define bmTA_EOM       0x01  /**< bmTransfer Attribute: End of Message */
84
#define bmTA_TERMCHAR  0x02  /**< bmTransfer Attribute: Terminate transfer with Terminate Character */
85
 
86
/** \brief status values according to USBTMC specificaton, Ref.: Table 16 */
87
typedef enum {
88
        TMC_STATUS_SUCCESS                   = 0x01,
89
        TMC_STATUS_PENDING                   = 0x02,
90
 
91
        /* USB488 defined USBTMC status values */
92
        TMC_STATUS_INTERRUPT_IN_BUSY         = 0x20,
93
 
94
        TMC_STATUS_FAILED                    = 0x80,
95
        TMC_STATUS_TRANSFER_NOT_IN_PROGRESS  = 0x81,
96
        TMC_STATUS_SPLIT_NOT_IN_PROGRESS     = 0x82,
97
        TMC_STATUS_SPLIT_IN_PROGRESS         = 0x83,
98
} TMC_Status;
99
 
100
/* defines for the device capablilities, Ref.: Table 37 and Table 8 USB488 */
101
#define HAS_INDICATOR_PULSE 0x04
102
#define TALK_ONLY       0x02
103
#define LISTEN_ONLY     0x01
104
#define TERMCHAR_BULKIN 0x01
105
#define IS_488_2        0x04
106
#define ACCEPTS_LOCAL_LOCKOUT 0x02
107
#define TRIGGER         0x01
108
#define SCPI_COMPILIANT 0x08
109
#define SR1_CAPABLE     0x04
110
#define RL1_CAPABLE     0x02
111
#define DT1_CAPABLE     0x01
112
 
113
/** \brief Structure to handle get_capabilities command.
114
 *
115
 *  contains all required parameters for a correct device specification */
116
typedef struct {
117
  uint8_t USBTMC_status;
118
  uint8_t reserved0;
119
  uint8_t bcdUSBTMC_lsb;
120
  uint8_t bcdUSBTMC_msb;
121
  uint8_t TMCInterface;
122
  uint8_t TMCDevice;
123
  uint8_t reserved1[6];
124
  /* place here USB488 subclass capabilities */
125
  uint8_t bcdUSB488_lsb;
126
  uint8_t bcdUSB488_msb;
127
  uint8_t USB488Interface;
128
  uint8_t USB488Device;
129
  uint8_t reserved2[8];
130
} USB_TMC_Capabilities;
131
 
132
/* Structure to handle Bulk-OUT Header for a DEV_DEP_MSG
133
 *
134
 * Ref.: Table 3 or Bulk-IN Header for a DEV_DEP_MSG, Ref.: Table 9 */
135
typedef struct
136
{
137
        uint32_t TransferSize;
138
        int8_t   bmTransferAttributes;
139
        int8_t   Reserved[3];
140
} DEV_DEP_MSG_OUT_Header, /**< \brief Bulk-OUT Header for a DEV_DEP_MSG, Ref.: Table 3 */
141
  DEV_DEP_MSG_IN_Header;  /**< \brief Bulk-IN Header for a DEV_DEP_MSG, Ref.: Table 9 */
142
 
143
/** \brief Structure to handle USBTMC device dependent message IN requests.
144
 *
145
 *  Ref.: Table 4 */
146
typedef struct
147
{
148
        uint32_t TransferSize;
149
        int8_t   bmTransferAttributes;
150
        int8_t   TermChar;
151
        int8_t   Reserved[2];
152
} REQUEST_DEV_DEP_MSG_IN_Header; /* Ref.: Table 4 */
153
 
154
/* \brief structure to handle vendor specific IN/OUT or REQUESTS according
155
 *  to USBTMC */
156
typedef struct
157
{
158
        uint32_t TransferSize;
159
        int8_t   Reserved[4];
160
} VENDOR_SPECIFIC_OUT_Header, /**< \brief structure to handle vendor specific
161
                               *IN/OUT or REQUESTS according to Ref.: Table 5 */
162
  REQUEST_VENDOR_SPECIFIC_IN_Header, /**< \brief structure to handle vendor
163
                                      * specific IN/OUT or REQUESTS according
164
                                      * Ref.: Table 6 */
165
  VENDOR_SPECIFIC_IN_Header; /**< \brief structure to handle vendor specific
166
                              * IN/OUT or REQUESTS according Ref.: Table 10 */
167
 
168
/** \brief general Header structure for USBTMC bulk messages. The MsgID value
169
 *  determines the type of the msg_specific part of these structure */
170
typedef struct _tHeader
171
{
172
        int8_t MsgID; /**< The MsgID value determines the type of the
173
                       * msg_specific part of these structure */
174
        int8_t bTag;
175
        int8_t bTagInverse;
176
        int8_t Reserved;
177
        int8_t msg_specific[8];
178
 
179
} BulkOUT_Header, /**< \brief According to USBTMC Ref.: Table 1 */
180
  BulkIN_Header,  /**< \brief According to USBTMC Ref.: Table 8 */
181
  tHeader; /**< \brief general Header structure for USBTMC bulk messages. */
182
 
183
 
184
/*****************************************************************************/
185
/* IEEE488 related stuff */
186
 
187
#define bmPOWER_ON                0x80
188
#define bmUSER_REQUEST            0x40
189
#define bmCOMMAND_ERROR           0x20
190
#define bmEXECUTION_ERROR         0x10
191
#define bmDEVICEDEPENDENT_ERROR   0x08
192
#define bmQUERY_ERROR             0x04
193
#define bmREQUEST_CONTROL         0x02
194
#define bmOPERATION_COMPLETE      0x01
195
 
196
/** \brief struct that contains all registers needed for the IEEE488.2
197
 *  compiliant status reporting */
198
typedef struct {
199
        uint8_t EventStatusRegister;
200
        uint8_t EventStatusEnable;
201
        uint8_t StatusByteRegister;
202
        uint8_t ServiceRequestEnable;
203
        uint8_t OPC_Received;
204
} IEEE488_status_registers;
205
 
206
 
207
/*****************************************************************************/
208
/* internal definitions and typedef's needed for this implementation */
209
 
210
#define NEWTRANSFER 1
211
 
212
/** \brief Internal state of tmc system.
213
 *
214
 *  used by this implementation, no reference in standard */
215
typedef enum {
216
        TMC_STATE_IDLE = 0x01,
217
        TMC_STATE_OUT_TRANSFER = 0x02,
218
        TMC_STATE_IN_TRANSFER = 0x03,
219
        TMC_STATE_ABORTING_OUT = 0x04,
220
        TMC_STATE_ABORTING_IN = 0x05,
221
        TMC_STATE_HALT = 0x06,
222
} TMC_State;
223
 
224
/** \brief struct to hold all usb tmc transfer relevant information */
225
typedef struct {
226
  uint8_t bTag; /**< contains the bTag value of the currently active transfer */
227
  uint32_t transfer_size;
228
  uint8_t new_transfer; /**< flag to signal the start of a new transfer, else 0 */
229
  uint32_t nbytes_rxd; /**< contains the number of bytes received in active tmc OUT transfer */
230
  uint32_t nbytes_txd; /**< contains the number of bytes transmitted in active tmc IN transfer */
231
 
232
} TMC_Transfer_Info;
233
 
234
 
235
/** \brief response queue to hold the data for requests */
236
typedef struct {
237
  unsigned char buf[TMC_RESPONSE_QUEUE_LENGTH]; /**< message buffer */
238
  uint16_t length;  /**< length of message. is 0 when no message is available */
239
} TMC_Response_Queue;
240
 
241
 
242
/* Global variables */
243
extern volatile static TMC_Status usb_tmc_status;       /**< Global variable contains the status of the last tmc operation. normally USB_TMC_SUCCESS */
244
extern volatile TMC_State usb_tmc_state;         /**< Global variable contains the state of the tmc system. Example USB_TMC_IDLE or USB_TMC_IN_TRANSFER */
245
extern volatile idata TMC_Transfer_Info usb_tmc_transfer; /** Global struct to hold all usb tmc transfer relevant information */
246
extern volatile idata IEEE488_status_registers ieee488_status;   /**< Struct that contains all status and enable registers for the IEEE488 status reporting capabilities */
247
 
248
 
249
/** Makro to check if this setup package is a USBTMC request */
250
#define usb_tmc_request() ((wIndexL == USB_TMC_INTERFACE && \
251
                            (bRequestType & bmRT_RECIP_INTERFACE) == bmRT_RECIP_INTERFACE) \
252
                           ||                                           \
253
                           (wIndexL == USB_TMC_EP_OUT && \
254
                            (bRequestType & bmRT_RECIP_ENDPOINT) == bmRT_RECIP_ENDPOINT) \
255
                           ||                                           \
256
                           (wIndexL == (bmRT_DIR_IN | USB_TMC_EP_IN) && \
257
                            (bRequestType & bmRT_RECIP_ENDPOINT) == bmRT_RECIP_ENDPOINT) \
258
                          )
259
 
260
/** \brief general function to handle the TMC requests.
261
 *
262
 *  Parses the TMC header to provide needed information to the
263
 *  following message parser.
264
 *  \return returns non-zero if it handled the command successfully.
265
 */
266
uint8_t usb_handle_tmc_packet (void);
267
 
268
 
269
/** \brief clears all global variables to known states. sets the POWER_ON bit */
270
void init_usb_tmc();
271
 
272
/** set the mav bit (Message available or "Queue not empty") in the IEEE488 status structure */
273
#define IEEE488_set_mav() (ieee488_status.StatusByteRegister |= 0x10)
274
 
275
/** clear the mav bit (Message available or "Queue not empty") in the IEEE488 status structure */
276
#define IEEE488_clear_mav() (ieee488_status.StatusByteRegister &= 0xEF)
277
 
278
/** \brief evalutates the status IEEE488 status byte. represents the current
279
 *         device state
280
 *
281
 * executes the process to evaluate the current state of the status byte
282
 * according to the IEEE488.2 standard status reporting capabilities.
283
 * Reference: IEC60488-2:2004 figure 4-1
284
 *
285
 * \param[in] *status pointer to a IEEE488_status_registers struct.
286
 *            Status byte in the struct is updated
287
 * \return return value is the current value of the status byte
288
 */
289
uint8_t IEEE488_status_query(idata IEEE488_status_registers *status);
290
 
291
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.