view gpib.h @ 0:6dfcd8e5b8df

first release
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Tue, 25 Mar 2014 20:31:00 +0000
parents
children 1a405bda2ffe
line wrap: on
line source
1 /* #------------------------------------------------------------------------#
2 | |
3 | gpib.h |
4 | |
5 | USB-GPIB GPIB processing and state machine. |
6 | |
7 | Copyright 2014, Frank A. Kingswood, www.kingswood-consulting.co.uk |
8 | |
9 #------------------------------------------------------------------------# */
11 #ifndef GPIB_H_
12 #define GPIB_H_
14 #include <avr/io.h>
16 class GPIBDriver
17 {
18 public:
19 static const bool SAFER=1;
21 static const unsigned PORTD_EOI=0,
22 PORTD_NRFD=1,
23 PORTD_NDAC=2,
24 PORTD_SRQ=3,
25 PORTD_TEENSY_N_ARDUINO=4,
26 PORTD_LED_MP=5, // /LED on Arduino, serial on Teensy
27 PORTD_LED_T=6, // LED on Teensy
28 PORTD_DAV=7;
30 // By happy coincidence the bits here are unused on port D
31 static const unsigned PORTF_ATN=4,
32 PORTF_IFC=5,
33 PORTF_REN=6,
34 PORTF_DIO8=7;
36 // Data pins for MicroPro
37 static const unsigned PORTC_DIO8=6;
38 static const unsigned PORTE_DIO1=6;
40 public:
41 GPIBDriver();
42 static void LED(bool);
44 /* ..... public data ........................................................ */
46 static uint16_t MyAddress;
47 static uint16_t DeviceAddress;
48 static uint16_t Timeout;
49 static bool MicroPro;
51 /* ..... high level commands ................................................ */
53 static void Initialize();
54 static void Float();
55 static void Controller();
57 int16_t Command(const int16_t *command); // UNIM
59 /* ..... byte level commands ................................................ */
61 static uint8_t Out(uint8_t byte);
62 static int16_t In();
63 static void Report(const char *msg=0);
65 static uint8_t Lines();
66 #define set_bit(port_,bit_) \
67 do \
68 { static const uint8_t port=_SFR_IO_ADDR(port_); \
69 static const uint8_t bit=bit_; \
70 __asm__ __volatile__ ("sbi %0,%1"::"I"(port),"I"(bit):"memory"); \
71 } while(0)
73 #define clear_bit(port_,bit_) \
74 do \
75 { static const uint8_t port=_SFR_IO_ADDR(port_); \
76 static const uint8_t bit=bit_; \
77 __asm__ __volatile__ ("cbi %0,%1"::"I"(port),"I"(bit):"memory"); \
78 } while(0)
80 #define test_bit_set(port_,bit_) \
81 ({ static const uint8_t port=_SFR_IO_ADDR(port_); \
82 static const uint8_t bit=bit_; \
83 uint8_t value; \
84 __asm__ __volatile__ ( \
85 " clr %0 \n" \
86 " sbic %1,%2 \n" \
87 " inc %0 \n" \
88 :"=r"(value) \
89 :"I"(port),"I"(bit) \
90 :"memory"); \
91 0xff&value; \
92 })
94 #define test_bit_clear(port_,bit_) \
95 ({ static const uint8_t port=_SFR_IO_ADDR(port_); \
96 static const uint8_t bit=bit_; \
97 uint8_t value; \
98 __asm__ __volatile__ ( \
99 " clr %0 \n" \
100 " sbis %1,%2 \n" \
101 " inc %0 \n" \
102 :"=r"(value) \
103 :"I"(port),"I"(bit) \
104 :"memory"); \
105 0xff&value; \
106 })
109 /* ..... bit level commands ................................................. */
111 static void FloatData();
112 static uint8_t Data();
113 static void Data(uint8_t byte);
115 // Who is allowed to drive the signals
116 //
117 // Control Output
118 // --------- ---------
119 // NRFD Listener
120 // NDAC Listener
121 // DAV Talker
122 //
123 // Control Output
124 // --------- ---------
125 // ATN Controller
126 // IFC Controller
127 // REN Controller
128 // SRQ Any
129 // EOI Talker and Controller
131 inline static void ATN(bool value)
132 {
133 if(!value)
134 { if(SAFER) clear_bit(DDRF,PORTF_ATN);
135 set_bit(PORTF,PORTF_ATN);
136 }
137 else
138 { clear_bit(PORTF,PORTF_ATN);
139 if(SAFER) set_bit(DDRF,PORTF_ATN);
140 }
141 }
142 inline static void IFC(bool value)
143 {
144 if(!value)
145 { if(SAFER) clear_bit(DDRF,PORTF_IFC);
146 set_bit(PORTF,PORTF_IFC);
147 }
148 else
149 { clear_bit(PORTF,PORTF_IFC);
150 if(SAFER) set_bit(DDRF,PORTF_IFC);
151 }
152 }
153 inline static void REN(bool value)
154 {
155 if(!value)
156 { if(SAFER) clear_bit(DDRF,PORTF_REN);
157 set_bit(PORTF,PORTF_REN);
158 }
159 else
160 { clear_bit(PORTF,PORTF_REN);
161 if(SAFER) set_bit(DDRF,PORTF_REN);
162 }
163 }
165 // shared pins, must not actively drive high
166 inline static void EOI(bool value)
167 {
168 if(!value)
169 { clear_bit(DDRD,PORTD_EOI);
170 set_bit(PORTD,PORTD_EOI);
171 }
172 else
173 { clear_bit(PORTD,PORTD_EOI);
174 set_bit(DDRD,PORTD_EOI);
175 }
176 }
177 inline static void NDAC(bool value)
178 {
179 if(!value)
180 { clear_bit(DDRD,PORTD_NDAC);
181 set_bit(PORTD,PORTD_NDAC);
182 }
183 else
184 { clear_bit(PORTD,PORTD_NDAC);
185 set_bit(DDRD,PORTD_NDAC);
186 }
187 }
188 inline static void NRFD(bool value)
189 {
190 if(!value)
191 { clear_bit(DDRD,PORTD_NRFD);
192 set_bit(PORTD,PORTD_NRFD);
193 }
194 else
195 { clear_bit(PORTD,PORTD_NRFD);
196 set_bit(DDRD,PORTD_NRFD);
197 }
198 }
199 inline static void SRQ(bool value)
200 {
201 if(!value)
202 { clear_bit(DDRD,PORTD_SRQ);
203 set_bit(PORTD,PORTD_SRQ);
204 }
205 else
206 { clear_bit(PORTD,PORTD_SRQ);
207 set_bit(DDRD,PORTD_SRQ);
208 }
209 }
210 inline static void DAV(bool value)
211 {
212 if(!value)
213 { clear_bit(DDRD,PORTD_DAV);
214 set_bit(PORTD,PORTD_DAV);
215 }
216 else
217 { clear_bit(PORTD,PORTD_DAV);
218 set_bit(DDRD,PORTD_DAV);
219 }
220 }
222 inline static uint8_t EOI()
223 {
224 return test_bit_clear(PIND,PORTD_EOI);
225 }
226 inline static uint8_t NDAC()
227 {
228 return test_bit_clear(PIND,PORTD_NDAC);
229 }
230 inline static uint8_t NRFD()
231 {
232 return test_bit_clear(PIND,PORTD_NRFD);
233 }
234 inline static uint8_t SRQ()
235 {
236 return test_bit_clear(PIND,PORTD_SRQ);
237 }
238 inline static uint8_t DAV()
239 {
240 return test_bit_clear(PIND,PORTD_DAV);
241 }
242 inline static int ATN()
243 {
244 return test_bit_clear(PINF,PORTF_ATN);
245 }
246 inline static uint8_t IFC()
247 {
248 return test_bit_clear(PINF,PORTF_IFC);
249 }
250 inline static uint8_t REN()
251 {
252 return test_bit_clear(PINF,PORTF_REN);
253 }
254 };
256 extern const GPIBDriver GPIB;
258 // #define GPIB_S_DAT 1
259 // #define GPIB_R_DAT 2
260 // #define GPIB_R_SRQ 3
261 // #define RD_AD_CMD 4
262 // #define SET_IO_CMD 5
263 // #define RESET_CMD 6
264 //
265 // #define FLASH_SECTION_READ 0x01
266 // #define FLASH_SECTION_WRITE 0x02
267 //
268 // #define TIMEOUT 100000
269 // #define true 1
270 // #define false 0
271 // //
272 // // GPIB receive finite-state-machine states
273 // //
274 // #define GPIB_RX_START 0
275 // #define GPIB_RX_ACCEPT 1
276 // #define GPIB_RX_WAIT_DAV 2
277 // #define GPIB_RX_DAV_LOW 3
278 // #define GPIB_RX_WAIT_DAV_HIGH 4
279 // #define GPIB_RX_DAV_HIGH 5
280 // #define GPIB_RX_EOI 6
281 // #define GPIB_RX_FINISH 9
282 // #define GPIB_RX_DAV_TIMEOUT 10
283 // #define GPIB_RX_DONE 99
284 //
285 // //
286 // // GPIB send finite-state-machine states
287 // //
288 // #define GPIB_TX_START 0
289 // #define GPIB_TX_CHECK 1
290 // #define GPIB_TX_PUT_DATA 2
291 // #define GPIB_TX_WAIT_FOR_NRFD 3
292 // #define GPIB_TX_SET_DAV_LOW 4
293 // #define GPIB_TX_WAIT_FOR_NDAC 5
294 // #define GPIB_TX_SET_DAV_HIGH 6
295 // #define GPIB_TX_FINISH 9
296 // #define GPIB_TX_ERROR 98
297 // #define GPIB_TX_DONE 99
298 //
299 // #define GPIB_MAX_TX_LEN 64 // Max length of transmit GPIB string
300 // #define GPIB_MAX_RX_LEN 128 // Max length of receive GPIB string
301 // #define GPIB_MAX_CMD_LEN 32 // Max length of GPIB incoming command
302 //
303 // extern unsigned char gpib_buff[GPIB_MAX_RX_LEN];
304 // extern unsigned char gpib_ptr;
305 // extern char gpib_tx_buff[GPIB_MAX_TX_LEN];
306 // extern char gpib_tx_ptr;
307 // extern char gpib_cmd_buff[GPIB_MAX_CMD_LEN];
308 // extern unsigned char gpib_cmd_ptr;
309 // extern unsigned char listening;
310 // extern unsigned char in_command;
311 //
312 // void gpib_init(void);
313 // void sendgpib (void);
314 // int readgpib(void);
315 // void gpib_ren(unsigned char state);
316 // void gpib_ifc(void);
318 #endif /* GPIB_H_ */
320 /* ----- EOF gpib.h ----- */