2019-01-23 18:41:12 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 Wolfgang Thaller.
|
|
|
|
|
|
|
|
This file is part of Retro68.
|
|
|
|
|
|
|
|
Retro68 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Retro68 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 Retro68. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-04-22 23:11:56 +00:00
|
|
|
#include "MacSerialStream.h"
|
|
|
|
|
|
|
|
#include <Serial.h>
|
|
|
|
#include <Devices.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-05-07 21:51:47 +00:00
|
|
|
MacSerialStream::MacSerialStream(int port, int baud)
|
|
|
|
: port(port), curBaud(baud)
|
2018-04-22 23:11:56 +00:00
|
|
|
{
|
2018-04-27 00:06:54 +00:00
|
|
|
open();
|
2018-04-22 23:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacSerialStream::close()
|
|
|
|
{
|
|
|
|
if(inRefNum == 0)
|
|
|
|
return;
|
2019-01-04 02:35:32 +00:00
|
|
|
SerSetBuf(inRefNum, NULL, 0);
|
|
|
|
|
|
|
|
CloseDriver(inRefNum);
|
|
|
|
CloseDriver(outRefNum);
|
2018-04-22 23:11:56 +00:00
|
|
|
inRefNum = outRefNum = 0;
|
|
|
|
}
|
|
|
|
|
2018-04-27 00:06:54 +00:00
|
|
|
void MacSerialStream::open()
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
OpenDriver(port ? "\p.BOut" : "\p.AOut", &outRefNum);
|
|
|
|
OpenDriver(port ? "\p.BIn" : "\p.AIn", &inRefNum);
|
|
|
|
SerSetBuf(inRefNum, inputBuffer, kInputBufferSize);
|
2018-04-27 00:06:54 +00:00
|
|
|
|
2019-01-04 02:35:32 +00:00
|
|
|
SerShk shk;
|
|
|
|
memset(&shk, 0, sizeof(shk));
|
|
|
|
shk.fCTS = true;
|
|
|
|
Control(outRefNum, kSERDHandshake, &shk);
|
2018-04-27 00:06:54 +00:00
|
|
|
|
|
|
|
setBaud(curBaud);
|
|
|
|
}
|
|
|
|
|
2018-04-22 23:11:56 +00:00
|
|
|
MacSerialStream::~MacSerialStream()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacSerialStream::write(const void* p, size_t n)
|
|
|
|
{
|
2019-01-04 02:35:32 +00:00
|
|
|
ParamBlockRec pb;
|
|
|
|
memset(&pb, 0, sizeof(pb));
|
|
|
|
pb.ioParam.ioRefNum = outRefNum;
|
|
|
|
pb.ioParam.ioBuffer = (Ptr)p;
|
|
|
|
pb.ioParam.ioReqCount = n;
|
2018-04-22 23:11:56 +00:00
|
|
|
OSErr err = PBWriteSync(&pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacSerialStream::idle()
|
|
|
|
{
|
|
|
|
long count = 0;
|
2019-01-04 02:35:32 +00:00
|
|
|
SerGetBuf(inRefNum, &count);
|
2018-04-22 23:11:56 +00:00
|
|
|
while(count > 0)
|
|
|
|
{
|
|
|
|
long count1 = count > kReadBufferSize ? kReadBufferSize : count;
|
|
|
|
ParamBlockRec pb;
|
|
|
|
memset(&pb, 0, sizeof(pb));
|
|
|
|
pb.ioParam.ioRefNum = inRefNum;
|
|
|
|
pb.ioParam.ioBuffer = (Ptr)&readBuffer;
|
|
|
|
pb.ioParam.ioReqCount = count1;
|
|
|
|
OSErr err = PBReadSync(&pb);
|
|
|
|
if(err)
|
|
|
|
return;
|
|
|
|
count -= count1;
|
|
|
|
|
|
|
|
notifyReceive((uint8_t*)readBuffer, count1);
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 20:06:32 +00:00
|
|
|
|
|
|
|
void MacSerialStream::setBaud(int baud)
|
|
|
|
{
|
2018-04-27 00:06:54 +00:00
|
|
|
curBaud = baud;
|
2018-04-24 20:06:32 +00:00
|
|
|
short baudval = 0;
|
|
|
|
switch(baud)
|
|
|
|
{
|
|
|
|
case 9600: baudval = baud9600; break;
|
|
|
|
case 14400: baudval = baud14400; break;
|
|
|
|
case 19200: baudval = baud19200; break;
|
|
|
|
case 28800: baudval = baud28800; break;
|
|
|
|
case 38400: baudval = baud38400; break;
|
|
|
|
case 57600: baudval = baud57600; break;
|
|
|
|
case 115200: baudval = 0; break;
|
|
|
|
case 230400: baudval = 0; break;
|
|
|
|
}
|
2019-01-04 02:35:32 +00:00
|
|
|
SerReset(outRefNum, baudval | data8 | noParity | stop10);
|
2018-04-24 20:06:32 +00:00
|
|
|
|
|
|
|
if(baud == 115200)
|
|
|
|
Control(outRefNum, kSERD115KBaud, nullptr);
|
|
|
|
else if(baud == 230400)
|
|
|
|
Control(outRefNum, kSERD230KBaud, nullptr);
|
|
|
|
}
|