From bfdaec3c3756b6b6bc7c391b16e482ee584d5de5 Mon Sep 17 00:00:00 2001 From: Manfred Steiner Date: Wed, 17 Jul 2024 17:58:33 +0200 Subject: [PATCH] nano-644 SW2 simulation --- examples/simuc/src/main.cpp | 87 ++++++++++++++++++---------- examples/simuc/src/simavr/simavr.cpp | 54 +++++++++++++++-- examples/simuc/src/simavr/simavr.h | 4 +- 3 files changed, 109 insertions(+), 36 deletions(-) diff --git a/examples/simuc/src/main.cpp b/examples/simuc/src/main.cpp index 0b98f1c..6123098 100644 --- a/examples/simuc/src/main.cpp +++ b/examples/simuc/src/main.cpp @@ -31,6 +31,13 @@ void printHelp () { printf(" simuc --board arduino a.out\n\n"); } +void printAvailableCommands (struct StartParameters ¶ms) { + printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n"); + if (params.board == BoardNano644) { + printf(" 1 (SW2 not pressed), 2 (SW2 pressed)\n"); + } +} + struct SimUcInit { int argc; char *argv[]; @@ -216,7 +223,11 @@ int main (int argc, char **argv) { } printf("----------------------------------------------------------------------\n"); printf("available commands:\n"); - printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n"); + printAvailableCommands(params); + // printf(" b (break), c (continue), s (stack), r (reset), p (power), q (quit)\n"); + // if (params.board == BoardNano644) { + // printf(" 1 (SW2 not pressed), 2 (SW2 pressed)\n"); + // } printf("----------------------------------------------------------------------\n"); printf("init done - press key to start\n"); char c = getchar(); @@ -231,6 +242,12 @@ int main (int argc, char **argv) { case 'r': start(CommandReset, NULL); break; case 'p': start(CommandPower, NULL); break; case 'q': return 0; + case '1': case '2': { + if (params.board == BoardNano644) { + + break; + } + } default: start(ReadyForNewCommand, NULL); break; } @@ -250,41 +267,55 @@ int main (int argc, char **argv) { // } if (getline(&line, &size, stdin) > 0) { - const char *commands[] = { "quit", "break", "continue", "stack", "reset", "power" }; try { - int foundIndex = -1; - int foundCnt = 0; - size_t length = strlen(line) - 1; - if (length > 0 && size >= (length + 1)) { - line[length] = 0; - for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) { - const char *cmd = commands[i]; - size_t max = strlen(cmd); - bool ok = true; - for (size_t j = 0; j < length; j++) { - if (j >= max || line[j] != cmd[j]) { - ok = false; - break; + EnumSimAvrCommand simCmd = ReadyForNewCommand; + + if (strcmp(line, "1\n") == 0) { + simCmd = CommandSW2Released; + + } else if (strcmp(line, "2\n") == 0) { + simCmd = CommandSW2Pressed; + + } else { + const char *commands[] = { "quit", "break", "continue", "stack", "reset", "power" }; + int foundIndex = -1; + int foundCnt = 0; + size_t length = strlen(line) - 1; + if (length > 0 && size >= (length + 1)) { + line[length] = 0; + for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) { + const char *cmd = commands[i]; + size_t max = strlen(cmd); + bool ok = true; + for (size_t j = 0; j < length; j++) { + if (j >= max || line[j] != cmd[j]) { + ok = false; + break; + } + } + if (ok) { + foundCnt++; + foundIndex = i; } } - if (ok) { - foundCnt++; - foundIndex = i; - } + } + + // printf("foundCnt=%d foundIndex=%d command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : ""); + if (foundCnt == 1 || length == 0) { + simCmd = (EnumSimAvrCommand)(foundCnt > 0 ? foundIndex + 2 : 1); } } - // printf("foundCnt=%d foundIndex=%d command=%s\n", foundCnt, foundIndex, foundIndex >= 0 ? commands[foundIndex] : ""); - if (foundCnt == 1 || length == 0) { - EnumSimAvrCommand cmd = (EnumSimAvrCommand)(foundCnt > 0 ? foundIndex + 2 : 1); + + if (simCmd != ReadyForNewCommand) { struct SimAvrStatus status = getStatus(); - if (cmd == CommandStatus && status.state == StateStopped) { + if (simCmd == CommandStatus && status.state == StateStopped) { // ignore status command, because status was already printed // only line feed visible on screen } else { - setCommand(cmd, NULL); + setCommand(simCmd, NULL); while (1) { struct SimAvrEvent ev = waitForEvent(); - if (ev.event == EventShutdown || (cmd == CommandQuit && ev.event == EventCommandExecuted)) { + if (ev.event == EventShutdown || (simCmd == CommandQuit && ev.event == EventCommandExecuted)) { quit = 1; break; } @@ -295,10 +326,8 @@ int main (int argc, char **argv) { } } else { - printf("invalid command, valid commands are for status and:"); - for (long unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) { - printf(" %s", commands[i]); - } + printf("invalid command, valid commands are for status and:\n"); + printAvailableCommands(params); printf("\n"); continue; } diff --git a/examples/simuc/src/simavr/simavr.cpp b/examples/simuc/src/simavr/simavr.cpp index 2b90430..29d2d0e 100644 --- a/examples/simuc/src/simavr/simavr.cpp +++ b/examples/simuc/src/simavr/simavr.cpp @@ -17,6 +17,7 @@ #include #include #include +#include SimAvr::SimAvr () { @@ -517,9 +518,12 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) { } case BoardNano644: { static int8_t led[3] = { -1, -1, -1 }; // pin floating + static int8_t sw2 = -1; static uint8_t mask[3] = { 0x04, 0x08, 0x10 }; // Bitmask for LED Red, Yellow, Green int nextLed[3] = { -1, -1, -1 }; + int nextSw2 = -1; char cLed[3]; + uint8_t pinc = avr->data[0x26]; uint8_t ddrc = avr->data[0x27]; uint8_t portc = avr->data[0x28]; int change = 0; @@ -531,12 +535,27 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) { } if (!onlyOnChange || nextLed[i] != led[i]) { change++; - rv = true; } } + + if ((ddrc & 0x20) == 0 && ((portc & 0x20) == 0x20)) { + nextSw2 = (pinc & 0x20) == 0 ? 1 : 0; // 1 == pressed + } else { + nextSw2 = -1; + } + if (!onlyOnChange || nextSw2 != sw2) { + change++; + } + int l = 0; if (change) { - l += snprintf(s, size, "LED RGB = "); + rv = true; + switch (nextSw2) { + case 0: l += snprintf(&s[l], size - l, "SW2 ."); break; + case 1: l += snprintf(&s[l], size - l, "SW2 X"); break; + default: l += snprintf(&s[l], size - l, "SW2 ?"); break; + } + l += snprintf(&s[l], size - l, " LED RGB = "); for (int i = 0; i < 3; i++) { switch (nextLed[i]) { case -1: cLed[i] = '-'; break; @@ -546,16 +565,21 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) { } l += snprintf(&s[l], size - l, "%c", cLed[i]); } + for (int i = 0; i < 3; i++) { if (nextLed[i] != led[i]) { led[i] = nextLed[i]; switch (i) { - case 0: l += snprintf(&s[l], l, " -> Red = %c", cLed[i]); break; - case 1: l += snprintf(&s[l], l, " -> Yellow = %c", cLed[i]); break; - case 2: l += snprintf(&s[l], l, " -> Green = %c", cLed[i]); break; + case 0: l += snprintf(&s[l], size - l, " -> Red = %c", cLed[i]); break; + case 1: l += snprintf(&s[l], size - l, " -> Yellow = %c", cLed[i]); break; + case 2: l += snprintf(&s[l], size - l, " -> Green = %c", cLed[i]); break; } } } + if (nextSw2 != sw2) { + l += snprintf(&s[l], size - l, " SW2 %s", nextSw2 ? "pressed" : "not pressed"); + sw2 = nextSw2; + } } break; } @@ -649,7 +673,7 @@ void SimAvr::avrRun () { long cnt = 0; int lastAvrState = -1; _avr_sp_set(avr, 0); - char led[80]; + char led[120]; while (1) { try { if (sprintfLedStatus(led, sizeof(led), true)) { @@ -897,6 +921,24 @@ void SimAvr::avrRun () { break; } + case CommandSW2Released: { + if (startParameters->board == BoardNano644) { + avr_ioport_external_t io_ext = { 'C', 0x20, 0x20 }; // PC5 ... SW2 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + nextCommand = ReadyForNewCommand; + break; + } + + case CommandSW2Pressed: { + if (startParameters->board == BoardNano644) { + avr_ioport_external_t io_ext = { 'C', 0x20, 0x0 }; // PC5 ... SW2 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + nextCommand = ReadyForNewCommand; + break; + } + default: { nextCommand = ReadyForNewCommand; break; diff --git a/examples/simuc/src/simavr/simavr.h b/examples/simuc/src/simavr/simavr.h index 7c993e7..999beb9 100644 --- a/examples/simuc/src/simavr/simavr.h +++ b/examples/simuc/src/simavr/simavr.h @@ -64,7 +64,9 @@ typedef enum { CommandContinue, CommandStack, CommandReset, - CommandPower + CommandPower, + CommandSW2Released, + CommandSW2Pressed, } EnumSimAvrCommand; struct SimAvrStatus { -- 2.39.5