Skip to content

Commit

Permalink
Overhaul flow control, logging, and build
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodger Combs committed Feb 28, 2017
1 parent b49b858 commit ff0de47
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@
*.su

serial

build-*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CFLAGS += -std=c11 -g

all: serial arduino
serial: serial.c
serial: serial.c config.h

.PHONY: arduino upload clean clean-arduino

Expand Down
40 changes: 40 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2017 Rodger Combs
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#define RATE 57600
#define BRATE B57600

#define HEADER_SIZE 0x400

#define INPUT_SIZE 4
#define INPUT_BATCH 8

#define BUF_COUNT 0x80
#define MS_TIMEOUT 10

#define START_BYTE 0xFF
#define FINISH_BYTE 0xFE

#define REQ_BYTE 0x00
#define ACK_BYTE 0x01
3 changes: 3 additions & 0 deletions serial-n64/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CXXFLAGS += -O3

include ../arduino-mk/Arduino.mk
39 changes: 27 additions & 12 deletions serial-n64/serial-n64.ino
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ void writeString(const char *string)
writeByte(*string++);
}

void writeInt(int i, int radix)
{
char localBuf[32];
itoa(i, localBuf, radix);
writeString(localBuf);
}

void writeLine(const char *line)
{
writeString(line);
Expand All @@ -83,7 +90,7 @@ byte readByte()

void setup()
{
Serial.begin(115200);
Serial.begin(RATE);

noInterrupts();

Expand Down Expand Up @@ -233,18 +240,22 @@ inner_loop:
}

const char* sendMsg = NULL;
int pendingAcks = 0;

static void readSerial(bool timeout)
{
int ms = millis();
int currentMs = ms;
digitalWrite(13, HIGH);
if (sendMsg) {
writeLine(sendMsg);
sendMsg = NULL;
}
while (buf->numElements(buf) <= (BUF_COUNT - INPUT_BATCH) && (!timeout || ((currentMs < ms + MS_TIMEOUT)) || buf->isEmpty(buf)))
{
if (sendMsg) {
writeLine(sendMsg);
sendMsg = NULL;
}
for (int i = 0; i < pendingAcks; i++)
writeByte(ACK_BYTE);
pendingAcks = 0;
int cmd;
writeByte(REQ_BYTE);
while ((cmd = readByteNow()) < 0x80) {
Expand All @@ -253,20 +264,22 @@ static void readSerial(bool timeout)
}
if (cmd == START_BYTE) {
for (int i = 0; i < INPUT_BATCH && (!timeout || ((currentMs < ms + MS_TIMEOUT) && N64_QUERY)); i++) {
char localBuf[INPUT_SIZE + 2] = {0};
for (int j = 0; j < INPUT_SIZE + 1; j++) {
byte localBuf[INPUT_SIZE + 2] = {0};
for (int j = 0; j < (INPUT_SIZE * 8 + 8 + 6) / 7; j++) {
int byte;
while ((byte = readByteNow()) == -1) {
if (timeout && !N64_QUERY)
goto skip;
}
localBuf[(j * 7) / 8] |= byte >> (8 - (j % 7));
localBuf[((j * 7) + 7) / 8] = byte << ((j % 7) + 1);
localBuf[(j * 7) / 8] |= byte >> (7 - (j % 7));
localBuf[((j * 7) + 7) / 8] = byte << ((j % 7) + 1);
}
if ((localBuf[0] ^ localBuf[1] ^ localBuf[2] ^ localBuf[3]) != localBuf[4])
if ((localBuf[0] ^ localBuf[1] ^ localBuf[2] ^ localBuf[3]) != localBuf[4]) {
sendMsg = "Check failed!";
break;
}
buf->add(buf, localBuf);
writeByte(ACK_BYTE);
pendingAcks++;
currentMs = millis();
}
} else if (cmd == FINISH_BYTE) {
Expand Down Expand Up @@ -399,7 +412,9 @@ void loop()
break;

default:
writeLine("Unknown command!");
writeString("Unknown command: ");
writeInt(n64_command, 16);
writeByte('\n');
break;

}
Expand Down
6 changes: 3 additions & 3 deletions serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ void write_7bit(int fd, const char *in, int len)
unsigned char check = 0;
for (int i = 0; i < len; i++) {
out[(i * 8) / 7] |= in[i] >> 1 + (i % 7);
out[(i * 8) / 7 + 1] = (in[i] << 7 - (i % 7)) & 0x7F;
out[(i * 8) / 7 + 1] = (in[i] << 6 - (i % 7)) & 0x7F;
check ^= in[i];
}
out[(len * 8) / 7] |= check >> 1 + (len % 7);
out[(len * 8) / 7 + 1] = (check << 7 - (len % 7)) & 0x7F;
out[(len * 8) / 7 + 1] = (check << 6 - (len % 7)) & 0x7F;
write(fd, out, ((len + 1) * 8 + 6) / 7);
}

Expand All @@ -75,7 +75,7 @@ int main(int argc, const char** argv)

printf("Total size: %i\n", size);

speed_t baud = B115200;
speed_t baud = BRATE;

struct termios settings;
tcgetattr(outfd, &settings);
Expand Down

0 comments on commit ff0de47

Please sign in to comment.