Skip to content. | Skip to navigation

Personal tools

lcu/io.cpp

lcu/io.cpp

lcu/io.cpp

/////////////////////////////////////////////////////////////////////////////
// IO.CPP
//
//  Last updated: 4/24/2005
//
//  Part of the source code for the Remote M3 Program
//
//  This file contains numerous low level I/O routines.
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// SystemReset - Software based system reset function
void SystemReset()
// Perform a hardware reset on the system.
{
  //outp(0x64,0xFE);
}

/////////////////////////////////////////////////////////////////////////////
// InitPorts - Function to configure the serial port parameters and install
// the serial interrupt driver
void InitPorts()
{
  InitComPort(Host,0xFF60,9600,"1N");  // Port 0, COMB, M3 cell DGHs
  InitComPort(Guest,0xFF70,19200,"1N"); // Port 1, COMA, console/terminal
}

/////////////////////////////////////////////////////////////////////////////
// RespondToHost - Transmit a string to the upstream host computer on COMB:
void RespondToHost(char* message)
// Send a message to the Host computer (upstream).
// The string "message" is transmitted.
// This routine automatically adds the correct Response and
// Address characters to the beginning of the string.
{
  char line[255]; // String buffer

  line[0] = Response;
  line[1] = Address;
  strcpy(&line[2],message);
  SerialBufPutString(Host,line);
  //EDSLog.Add(820,"RespondToHost(%d): Sent to host \"%s\"",__LINE__,line);
}

/////////////////////////////////////////////////////////////////////////////
// VerifyChecksum: Verifies that the checksum in a string is correct
int VerifyChecksum(char* s)
// Returns a zero if the string's checksum is OK, and a 1 if not.
// Note that this routine expects a full message, i.e.
//  ":JdataCC" where data is the body of the message, and CC is the
//  checksum.
{
  int i,Checksum;
  char check[10];

  // Start checksum calculation with M3 address
  Checksum = Address;
  // Binary not all subsequent characters
  for (i=2;i<strlen(s)-2;i++)
    Checksum ^= s[i];
  // Reduce checksum to 1 byte
  Checksum &= 255;
  sprintf(check,"%02X",Checksum);
  if ((s[strlen(s)-2] == check[0]) && (s[strlen(s)-1] == check[1]))
    return(0);
  else
    return(1);
}

/////////////////////////////////////////////////////////////////////////////
// RespondToHostWithChecksum: Add a hex checksum + CR to string and send
void RespondToHostWithChecksum(char* s)
// Calculates the Shec-checksum for a string, and sends the
// string to the Host computer
{
  int i,Checksum;
  char s2[120];

  // Start checksum calculation with M3 address
  Checksum = Address;
  // Binary not all subsequent characters
  for (i=0;i<strlen(s);i++)
    Checksum ^= s[i];
  // Reduce checksum to 1 byte
  Checksum &= 255;
  // Append checksum and a carriage return to the string given to us
  sprintf(s2,"%s%02X\r",s,Checksum);
  // Send total message to host
  RespondToHost(s2);
}

/*
/////////////////////////////////////////////////////////////////////////////
// COMTerminal: opens a simple terminal with a com port
void COMTerminal(int Port)
// This routine acts as a simple terminal to a com port.
// It displays received characters, and transmits typed characters.
// Hitting ESC ends the terminal session.
{
  char c; // Holds received and typed characters

  CloseVideo();
  cprintf("Terminal on COM%d: (ESC to end)\r\n",Port+1);
  do
  {
    // Check for incoming serial characters
    if (SerialCharReady(Port))
    {
      c = SerialGetChar(Port);
      cprintf("%c",c);
      if (c == 13)
        cprintf("\n");
    }
    c = 0;
    // Check for outgoing characters
    if (kbhit())
    {
      c = getch();
      if (c != 27)
      {
        cprintf("%c",c);
        if (c == '\r')
        cprintf("\n");
        // Transmit the typed character
        SerialPutChar(Port,c);
      }
    }
    // Quit on an <ESC>
  } while (c != 27);
  InitVideo();
  InitDisplay();
}
*/

/////////////////////////////////////////////////////////////////////////////
// InitHardwareIO - Initialize low-level I/O interfaces
void InitHardwareIO()
// Initialize 82C55 digital I/O chip and the MAX197 A/D chip
{
  int i;

  // Init digital IO chip according to:
  // Port A   Port B   Port C   Port C     Control Byte
  // 7-0      7-0      7-4      3-0        (CTRL55)
  // Out      In       Out      Out        0x82
  Init_IO(0x82,0x0,0x0);

  // Init DAS system according to
  // acqmod = 0 ---> internally controlled acquisition
  // acqmod = 1 ---> externally controlled acquisition
  // RNG    = 0 ---> 5V range
  // RNG    = 1 ---> 10V range
  // BIP    = 0 ---> + RNG range (unipolar)
  // BIP    = 1 ---> +/- RNG range (bipolar)
  ConfigADC(0,0,1);

  // Set COMA (Port 0) for RS-422 communications
  // Clear RTS1 or P1.4 (0xFF56h) to transmit from COMA
  // To receive you just need to put jumper W2 in 1-2 (2-3 is for RS232)
  //outp(0xFF56,0xEF);
  //outp(0xFF54,0xEF);

  for (i=0;i<4;i++)
    AnalogHistoryCount[i] = 0;
}

/////////////////////////////////////////////////////////////////////////////
// CloseHardwareIO - Shut down all low-level I/O interfaces
void CloseHardwareIO()
{
  // Set COMA (Port 0) for RS-232 communications
  // Set RTS1 or P1.4 (0xFF56h) to transmit from COMA
  // To receive you just need to put jumper W2 in 2-3 for RS232.
  //outp(0xFF56,0xFF);
  //outp(0xFF54,0xFF);
}

/////////////////////////////////////////////////////////////////////////////
// ReadAllAnalog - Query the Maxim MAX127 DAS for updated values
void ReadAllAnalog()
// Read all analog channels and store in the global analog variables
{
  int i;
  int Channel;  // Channel of new analog data being read
  double ftemp;

  for(Channel=0;Channel<4;Channel++)
  {
    // AnalogReadCount readings are taken and averaged before
    // recording a new value.

    AnalogHistory[Channel][AnalogHistoryCount[Channel]] = AnalogRd(Channel);
    AnalogHistoryCount[Channel]++;
    if (AnalogHistoryCount[Channel] >= AnalogReadCount)
    {
      ftemp = 0.0;
      for (i=0;i<AnalogReadCount;i++)
        ftemp += AnalogHistory[Channel][i];
      ftemp /= AnalogReadCount;
      /////////////////////////////////////////////////////////////////////
      // 2005/01
      // We're dividing by 2048 for now, which means values in [-2047-2048],
      // but we use unipolar mode so samples really are between [0-4095]
      //AnalogData[Channel] = ftemp * 5.0/4095.0;  //+.005;
      AnalogData[Channel] = ftemp * 5.0/2048.0;  //+.005;
      AnalogHistoryCount[Channel] = 0;
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// SetDigBit - Set a bit on the 82C55 digital I/O chip
void SetDigBit(int bit)
// "bit" is from 0 to 23
{
  write_bit(bit,1);
}

/////////////////////////////////////////////////////////////////////////////
// ClrDigBit - Clear a bit on the 82C55 digital I/O chip
void ClrDigBit(int bit)
// "bit" is from 0 to 23
{
  write_bit(bit,0);
}

/////////////////////////////////////////////////////////////////////////////
// GetDigBit - Get a bit on the 82C55 digital I/O chip
int GetDigBit(int bit)
// "bit" is from 0 to 23
// Returns a 0 or 1 if bit "bit" is cleared or set
{
  return(read_bit(bit));
}

/////////////////////////////////////////////////////////////////////////////
// SetStepperPhase - Set a steppers motor phase before motor current-on
void SetStepperPhase(int StepBit,int DirBit,int MonBit,int MonSteps)
// StepBit,DirBit,MonBit: the digital I/O bits for Step, Direction, and
// phase for the stepper whose phase needs to be set.  MonSteps is the
// number of steps to send to the driver before enabling current to the
// motor, a number from -9 to 9
{
  int count = 0;

  // Step forward
  SetDigBit(DirBit);
  // Wait for the Moni bit to come on
  while (GetDigBit(MonBit) && (count <= 10))
  {
    SetDigBit(StepBit);
    Delay(1);
    ClrDigBit(StepBit);
    Delay(1);
    count++;
  }

  // We don't seem to be communicating with the stepper driver
  if (GetDigBit(MonBit))
    EDSLog.Add(20,"SetStepperPhase: %s bit not responding",DigIONames[MonBit]);

  // We're at the Moni-on phase, now step to our last recorded phase

  // Set direction
  if (MonSteps < 0)
    ClrDigBit(DirBit);

  // Go
  MonSteps = abs(MonSteps);
  while (MonSteps > 0)
  {
    SetDigBit(StepBit);
    Delay(1);
    ClrDigBit(StepBit);
    Delay(1);
    MonSteps--;
  }
}

/////////////////////////////////////////////////////////////////////////////
// StepperPowerOn - Turn power on to steppers after setting motor phases
int StepperPowerOn()
// Turn on power to Tilt Steppers.  This routine
// sets the motor phases correctly (with current to the motors off) before
// enabling current to the motors to avoid power-on jerks.
// If StepperPowerOn returns a 1, call StepperPowerOnStage2 one second later.
{
  // Don't turn on power if power is already on.
  if (!StepperPowerFlag)
  {
    // Turn current off to the steppers we're enabling
    // Tilt Current off
    ClrDigBit(3);
    // Power on
    SetDigBit(4);
    StepperPowerFlag = 1;
    return(1);
	}
  // Keep track of how many processes are using the stepper power
  // (ie, if the M3 Turret and Tilts are both active, and one asks
	// to turn the power off, don't, until the other has also asked
	// to turn power off.
  StepperPowerFlag++;
  return(0);
}

/////////////////////////////////////////////////////////////////////////////
// StepperPowerOnStage2 - Set motor phases
void StepperPowerOnStage2()
{
	int i;

  // Prepare to apply current to the three Tilt steppers
  // Set the stepper drivers to the correct phases
  for (i=0;i<=2;i++)
    SetStepperPhase(TBitStep[i],TBitDir[i],TBitMon[i],Tilt.MonStep[i]);
  // Tilt Current On
  SetDigBit(3);
}

/////////////////////////////////////////////////////////////////////////////
// StepperPowerOff - Turn off power Tilt Steppers
void StepperPowerOff(int MotionOK)
// If MotionOK = 0, power will not be turned off if
// Tilts are moving.
{
  if (!MotionOK &&
      (Tilt.Moving[0] || Tilt.Moving[1] || Tilt.Moving[2]))
    return;
  // Power off if everyone's done with stepper power
  if (StepperPowerFlag > 0)
	  StepperPowerFlag--;
  if (StepperPowerFlag == 0)
  ClrDigBit(4); // Power off
}

/////////////////////////////////////////////////////////////////////////////
// StepperPowerOffNow - Turn off power to Tilt Steppers
void StepperPowerOffNow()
{
  // Power off regardless if anyone's using the steppers
  StepperPowerFlag = 0;
  ClrDigBit(4); // Power off
}

Generated by GNU Enscript 1.6.5.2.
Document Actions