//serial_epoc.cpp
//EPOC serial comms for icdprog C program

/*  
    icdprog - an open source PIC programmer for use with the Microchip ICD(1)
    Copyright (C) 2004  Andrew Ferry.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/



#include <e32base.h>
#include <e32test.h>
#include <e32svr.h>
#include <c32comm.h>
#include <f32file.h>

#include "serial.h"
#include <string.h>
#include <stdio.h>

RCommServ server; 
RComm commPort;

void udelay(unsigned usec) {
  User::After(usec);
}

void dtr_set()
{
  commPort.SetSignals (KSignalDTR, 0);
}

void dtr_clear()
{
  commPort.SetSignals (0,KSignalDTR);
}

void rts_set()
{
  commPort.SetSignals (KSignalRTS, 0);
}

void rts_clear()
{
  commPort.SetSignals (0, KSignalRTS);
}

int icd_read(char* s) {
  TBuf8 <1> buf;
  TRequestStatus stat = KRequestPending;
  TTimeIntervalMicroSeconds32 tout=10000000;
  commPort.Read (stat, tout, buf);
  User::WaitForRequest(stat);
  if (buf.Length()==1)
    *s=(char) buf[0];
    if (stat.Int() == KErrNone) return 1;
  return stat.Int() ;
}

int icd_write(void* data, unsigned n_bytes) {
  TBuf8 < 128 > buf;
  TRequestStatus stat = KRequestPending;
  buf.Copy((TUint8*)data);
  commPort.Write (stat, buf,n_bytes);
  User::WaitForRequest(stat);
  return stat.Int ();
}

int serial_open(char *port)
{
  //Load pdd and ldd
  TInt r = User::LoadPhysicalDevice (_L("EUART1"));
  if (r != KErrNone && r != KErrAlreadyExists){
    printf("Error opening serial port: %d\n",r);
    return -1;
  }
  
  r = User::LoadLogicalDevice (_L("ECOMM"));
  if (r != KErrNone && r != KErrAlreadyExists) {
    printf("Error opening serial port: %d\n",r);
    return -1;
  }
  
  // Start the comms server process.
  r = StartC32 ();
  if (r != KErrNone && r != KErrAlreadyExists) {
    printf("Error opening serial port: %d\n",r);
    return -1;
  }
  
  // Connect to the comm server
  r= server.Connect ();
  if (r!= KErrNone) {
    printf("Error opening serial port: %d\n",r);
    return -1;
  }
  
  //Load CSY Module
  r=server.LoadCommModule (_L("ECUART"));
  if (r!=KErrNone) {
    printf("Error opening serial port: %d\n",r);
    return -1;
  }
  
  r = commPort.Open (server, _L("COMM::0"), ECommExclusive);
  if (r!=KErrNone) {
    printf("Error opening serial port: %d\n",r);
    return -1;
  }
  
  TCommConfig portSettings;
  commPort.Config (portSettings);
  portSettings ().iRate = EBps57600;
  portSettings ().iParity = EParityNone;
  portSettings ().iDataBits = EData8;
  portSettings ().iStopBits = EStop1;
  portSettings ().iHandshake = KConfigFailDSR;  // for no handshaking
  portSettings ().iTerminator[0] = 10;
  portSettings ().iTerminatorCount = 0

  r = commPort.SetConfig (portSettings);
  if (r != 0) {
    printf("Error setting serial port parameters: %d\n",r);
    return -1;
  }
  
  TInt curlenth = commPort.ReceiveBufferLength ();
  commPort.SetReceiveBufferLength (128);
  curlenth = commPort.ReceiveBufferLength ();
     
    return 0;
}

void serial_close()
{
  commPort.Close ();
}