GPIB_AVR
Data Structures | Defines | Functions | Variables
gpib.c File Reference

This file contains the implementation of the GPIB functionality. More...

#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "uart.h"
#include "gpib.h"
#include "timer16.h"
#include "debugshell.h"

Data Structures

struct  gpib_controller_t
 Controller object data. More...

Defines

#define WITH_TIMEOUT
 if set, WITH_TIMEOUT means that waiting loops are interrupted by timeout.
#define release_bit(d, p, b)   d &= ~_BV(b); p |= _BV(b);
 Open Collector bit handling.
#define assign_bit(d, p, b)   p &= ~_BV(b); d |= _BV(b); p &= ~_BV(b);
 Open Collector bit handling.

Functions

uchar _gpib_write (uchar *bytes, int length, uchar attention)
 Write byte array to the bus.
void delay_ms (unsigned short ms)
 Some basic delay function.
void gpib_init (void)
 Init GPIB pins and variables.
uchar gpib_receive (uchar *_byte)
 Receive a character from GPIB Bus.
void queryPartners ()
 Query all active partners on bus.
void gpib_controller_assign (uchar address)
 Assign bus to me.
void gpib_controller_release (void)
 Release bus.
uchar gpib_write (uchar *bytes, int length)
 Write GPIB string to bus.
uchar gpib_cmd (uchar *bytes, int length)
 Write GPIB command to bus.
void gpib_info (void)
 print some useful info about bus state (for example value of handshake pins)
uchar gpib_serial_poll (void)
 execute serial polling
void gpib_set_partner (uchar address)
 Set device to be controlled.
uchar gpib_get_partner (void)
 Get device currently controlled.
uchar gpib_get_address (void)
 Get controller address.

Variables

gpib_controller_t controller
 controller object.
uchar cmd_buf [100]
 buffer used for sending commands and print out strings

Detailed Description

This file contains the implementation of the GPIB functionality.


Define Documentation

#define assign_bit (   d,
  p,
 
)    p &= ~_BV(b); d |= _BV(b); p &= ~_BV(b);

Open Collector bit handling.

assign: means set to LOW. If not open collector, we would use: bit is output, set bit to 1 #define assign_bit(p,b) p &= ~_BV(b) bit to input, switch pullup on bit is unknown, set bit to 0, bit to output, set bit to 0

Referenced by _gpib_write(), gpib_controller_assign(), gpib_controller_release(), gpib_init(), and gpib_receive().

#define release_bit (   d,
  p,
 
)    d &= ~_BV(b); p |= _BV(b);

Open Collector bit handling.

release : means set to HIGH. If not open collector, we would use: bit is output, set bit to 1 #define release_bit(p,b) p |= _BV(b) bit is output, set bit to 0

Referenced by _gpib_write(), gpib_controller_assign(), gpib_controller_release(), gpib_init(), and gpib_receive().

#define WITH_TIMEOUT

if set, WITH_TIMEOUT means that waiting loops are interrupted by timeout.

if not set, code stays in waiting loop maybe forever.


Function Documentation

uchar _gpib_write ( uchar bytes,
int  length,
uchar  attention 
)

Write byte array to the bus.

Precondition: Device must be allowed to talk. For a controller, this means all other devices have been set to be listeners.

Parameters:
bytesbyte array containing bytes to be send
lengthlength of valid bytes in byte array or zero. for binary data, lenght must be defined. For strings, length can be set to zero. Then the length of the string is calculated internally.
attentionattention tur means assign ATN signal line during write.

References assign_bit, debugshell(), G_ATN, G_DAV, G_EOI, G_NDAC, G_NRFD, release_bit, s, gpib_controller_t::talks, and uart_puts().

Referenced by gpib_cmd(), and gpib_write().

void delay_ms ( unsigned short  ms)

Some basic delay function.

Referenced by gpib_controller_assign(), and gpib_controller_release().

uchar gpib_cmd ( uchar bytes,
int  length 
)

Write GPIB command to bus.

See _gpib_write() for further information.

References _gpib_write().

Referenced by gpib_controller_assign(), gpib_serial_poll(), and main().

void gpib_controller_assign ( uchar  address)

Assign bus to me.

Initialization of the controller part.

Warning:
This function is not fully generic. I assume that two devices with address 0x01 and 0x02 are on the bus and fill the partners-array according to that assumption. You have to change the initialization of the partner array according to your environment. The partners-array is used by gpib_serial_poll() for looping over all existing devices.
Parameters:
addressthe address to be used by the controller. Usually 0x00.

get all partners on bus by querying them

References assign_bit, cmd_buf, DEFAULT_PARTNER_ADDRESS, delay_ms(), G_CMD_DCL, G_IFC, G_REN, gpib_cmd(), gpib_controller_t::myaddress, gpib_controller_t::partneraddress, queryPartners(), release_bit, and gpib_controller_t::talks.

Referenced by main().

void gpib_controller_release ( void  )

Release bus.

References assign_bit, delay_ms(), G_IFC, G_REN, and release_bit.

uchar gpib_get_address ( void  )

Get controller address.

Returns:
address of controller.

References gpib_controller_t::myaddress.

Referenced by main().

uchar gpib_get_partner ( void  )

Get device currently controlled.

Returns:
address Address of device.

References gpib_controller_t::partneraddress.

Referenced by main().

void gpib_info ( void  )

print some useful info about bus state (for example value of handshake pins)

References buf, G_ATN, G_DAV, G_EOI, G_IFC, G_NDAC, G_NRFD, G_REN, G_SRQ, and uart_puts().

Referenced by debugshell().

void gpib_init ( void  )

Init GPIB pins and variables.

All signal lines not related to the controller part are initialized with useful values. (The controller part initialization is done on gpib_controller_assert())

References assign_bit, G_ATN, G_DAV, G_EOI, G_IFC, G_NDAC, G_NRFD, G_REN, G_SRQ, and release_bit.

Referenced by main().

uchar gpib_receive ( uchar _byte)

Receive a character from GPIB Bus.

Does busy waiting until timeout value is reached. Then, the debugshell function is entered for user handling of the problem.

Parameters:
_bytePointer to single character; the function stores herein the character read. When errors occur during the function, the content of the parameter (i.e. *_byte) is undefined.
Returns:
On any error, 0xff is returned. in this case, the value of parameter *_byte is undefined. Otherwise the value of the EOI signal line during read is returned. If EOI was assigned, a 0x01 is returned. If EOI was not assigned, a 0x00 is returned. Assignment of EOI means that the talker is sending the last character for this transmission.

References assign_bit, debugshell(), G_DAV, G_EOI, G_NDAC, G_NRFD, release_bit, s, gpib_controller_t::talks, and uart_puts().

Referenced by gpib_serial_poll(), and main().

uchar gpib_serial_poll ( void  )

execute serial polling

We return the talker address of the device that created the SRQ.

References address2TalkerAddress, cmd_buf, G_CMD_SPD, G_CMD_SPE, G_CMD_UNL, G_CMD_UNT, gpib_cmd(), gpib_receive(), gpib_controller_t::partners, TalkerAddress2Address, and uart_puts().

Referenced by main().

void gpib_set_partner ( uchar  address)

Set device to be controlled.

Parameters:
addressAddress of device.

References gpib_controller_t::partneraddress.

Referenced by main().

uchar gpib_write ( uchar bytes,
int  length 
)

Write GPIB string to bus.

See _gpib_write() for further information.

References _gpib_write().

Referenced by main().

void queryPartners ( )

Query all active partners on bus.

This function should query all partners on the bus by executing a serial poll after take over of the bus as a controller. The partners found (answering) are stored in an array "partners".

TODO: the function does not a query for now, but just assumes that there are two devices with addresses 0x01 and 0x02. This is very limited but currently ok for me.

References gpib_controller_t::partners.

Referenced by gpib_controller_assign().


Variable Documentation

uchar cmd_buf[100]

buffer used for sending commands and print out strings

Referenced by gpib_controller_assign(), gpib_serial_poll(), and main().

controller object.

Not to be accessed outside gpib.c