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