From 1b2e5a4715486ecc57996a7c506aaaaf124832b5 Mon Sep 17 00:00:00 2001 From: Manfred Steiner Date: Thu, 18 Jul 2024 16:23:13 +0200 Subject: [PATCH] switches for sure board --- examples/simuc/.vscode/c_cpp_properties.json | 2 +- examples/simuc/.vscode/launch.json | 2 +- examples/simuc/.vscode/settings.json | 99 +----------------- examples/simuc/src/main.cpp | 38 ++++--- examples/simuc/src/simavr/simavr.cpp | 100 ++++++++++++++++++- examples/simuc/src/simavr/simavr.h | 2 + 6 files changed, 128 insertions(+), 115 deletions(-) diff --git a/examples/simuc/.vscode/c_cpp_properties.json b/examples/simuc/.vscode/c_cpp_properties.json index ba80ed4..e6ad2ab 100644 --- a/examples/simuc/.vscode/c_cpp_properties.json +++ b/examples/simuc/.vscode/c_cpp_properties.json @@ -7,7 +7,7 @@ "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "c++17", - "intelliSenseMode": "clang-x64" + "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 diff --git a/examples/simuc/.vscode/launch.json b/examples/simuc/.vscode/launch.json index 4c40c7b..447fd73 100644 --- a/examples/simuc/.vscode/launch.json +++ b/examples/simuc/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/dist/simuc", - "args": [], + "args": [ "--board", "sure", "/work/4ahme/sx-la1/g2/turnus3/2_timer/dist/atmega16.elf"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], diff --git a/examples/simuc/.vscode/settings.json b/examples/simuc/.vscode/settings.json index 4c1291f..263487d 100644 --- a/examples/simuc/.vscode/settings.json +++ b/examples/simuc/.vscode/settings.json @@ -1,99 +1,6 @@ { - "files.associations": { - "cstdio": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdlib": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "list": "cpp", - "map": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "random": "cpp", - "ratio": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "future": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "semaphore": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp", - "variant": "cpp", - "iom324.h": "c", - "sim_megax4.h": "c", - "sim_core_declare.h": "c", - "iom328p.h": "c", - "iom16.h": "c", - "sim_avr.h": "c" - }, - "cSpell.words": [ - "aref", - "atmega", - "avcc", - "avrsim", - "ddra", - "ddrb", - "eeprom", - "eesize", - "evws", - "fusesize", - "ioend", - "lfcrlf", - "lockbits", - "megaavr", - "microcontroller", - "Microcontrollers", - "millis", - "mmcu", - "picocom", - "porta", - "PORTB", - "ramend", - "simavr", - "simuc", - "slavename", - "SRAM", - "sreg", - "UART" + "cSpell.words": [], + "cSpell.ignorePaths": [ + "**/*.json", "**/*.c", "**/*.h", "**/Makefile" ] } \ No newline at end of file diff --git a/examples/simuc/src/main.cpp b/examples/simuc/src/main.cpp index 6123098..cf4abba 100644 --- a/examples/simuc/src/main.cpp +++ b/examples/simuc/src/main.cpp @@ -35,6 +35,8 @@ 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"); + } else if (params.board == BoardSure) { + printf(" 1 (SW1 not pressed), 2 (SW1 pressed), 3 (SW2 not pressed), 4 (SW2 pressed)\n"); } } @@ -224,10 +226,6 @@ int main (int argc, char **argv) { printf("----------------------------------------------------------------------\n"); printf("available commands:\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(); @@ -270,13 +268,30 @@ int main (int argc, char **argv) { try { EnumSimAvrCommand simCmd = ReadyForNewCommand; - if (strcmp(line, "1\n") == 0) { - simCmd = CommandSW2Released; - - } else if (strcmp(line, "2\n") == 0) { - simCmd = CommandSW2Pressed; - - } else { + switch (params.board) { + case BoardNano644: { + if (strcmp(line, "1\n") == 0) { + simCmd = CommandSW2Released; + } else if (strcmp(line, "2\n") == 0) { + simCmd = CommandSW2Pressed; + } + break; + } + case BoardSure: { + if (strcmp(line, "1\n") == 0) { + simCmd = CommandSW1Released; + } else if (strcmp(line, "2\n") == 0) { + simCmd = CommandSW1Pressed; + } else if (strcmp(line, "3\n") == 0) { + simCmd = CommandSW2Released; + } else if (strcmp(line, "4\n") == 0) { + simCmd = CommandSW2Pressed; + } + break; + } + default: break; + } + if (simCmd != ReadyForNewCommand) { const char *commands[] = { "quit", "break", "continue", "stack", "reset", "power" }; int foundIndex = -1; int foundCnt = 0; @@ -337,7 +352,6 @@ int main (int argc, char **argv) { } } - } // while (1) { // struct SimAvrEvent ev = waitForEvent(); diff --git a/examples/simuc/src/simavr/simavr.cpp b/examples/simuc/src/simavr/simavr.cpp index 29d2d0e..6dceb8c 100644 --- a/examples/simuc/src/simavr/simavr.cpp +++ b/examples/simuc/src/simavr/simavr.cpp @@ -587,10 +587,15 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) { case BoardSure: { static int8_t led[4] = { -1, -1, -1, -1 }; // pin floating static uint8_t mask[4] = { 0x08, 0x04, 0x02, 0x01 }; // Bitmask for LED PA3, PA2, PA1, PA0 + static int8_t sw[2] = { -1, -1 }; + static uint8_t swMask[2] = { 0x80, 0x40 }; int nextLed[4] = { -1, -1, -1, -1 }; + int nextSw[2] = { -1, -1 }; char cLed[4]; uint8_t ddra = avr->data[0x3a]; uint8_t porta = avr->data[0x3b]; + uint8_t ddrc = avr->data[0x34]; + uint8_t pinc = avr->data[0x33]; int change = 0; for (int i = 0; i < 4; i++) { if (ddra & mask[i]) { @@ -603,9 +608,32 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) { rv = true; } } + + for (int i = 0; i < 2; i++) { + if ((ddrc & swMask[i]) == 0) { + nextSw[i] = (pinc & swMask[i]) == 0 ? 1 : 0; + } else { + nextSw[i] = -1; + } + if (!onlyOnChange || nextSw[i] != sw[i]) { + change++; + rv = true; + } + } + int l = 0; if (change) { - l += snprintf(s, size, "LED PA3:0 = "); + char c[2]; + for (int i = 0; i < 2; i++){ + switch (nextSw[i]) { + case 0: c[i] = '.';break; + case 1: c[i] = 'X'; break; + default: c[i] = '?'; break; + } + } + l += snprintf(&s[l], size - l, "SW1:2 = %c%c", c[0], c[1]); + + l += snprintf(&s[l], size - l, " LED PA3:0 = "); for (int i = 0; i < 4; i++) { switch (nextLed[i]) { case -1: cLed[i] = '-'; break; @@ -627,6 +655,12 @@ bool SimAvr::sprintfLedStatus (char *s, size_t size, bool onlyOnChange) { } } + for (int i = 0; i < 2; i++) { + if (sw[i] != nextSw[i]) { + sw[i] = nextSw[i]; + l += snprintf(&s[l], size - l, " SW%d %s", i + 1, nextSw[i] ? "pressed" : "not pressed"); + } + } } break; } @@ -921,10 +955,54 @@ void SimAvr::avrRun () { break; } + case CommandSW1Released: { + if (startParameters->board == BoardSure) { + uint8_t ddrc = avr->data[0x34]; // Atmega16 + uint8_t pinc1 = avr->data[0x33]; + if ((ddrc & 0x80) == 0) { // external pullup on board + avr_ioport_external_t io_ext = { 'C', 0x80, 0x80 }; // PC7 ... SW1 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + uint8_t pinc2 = avr->data[0x33]; + printf("SW1 released, DDRC = 0x%02x, PINC 0x%02x -> 0x%02x\n", ddrc, pinc1, pinc2); + } + nextCommand = ReadyForNewCommand; + break; + } + + case CommandSW1Pressed: { + if (startParameters->board == BoardSure) { + uint8_t ddrc = avr->data[0x34]; // Atmega16 + uint8_t pinc1 = avr->data[0x33]; + if ( (ddrc & 0x80) == 0) { // external pullup on board + avr_ioport_external_t io_ext = { 'C', 0x80, 0x00 }; // PC7 ... SW1 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + uint8_t pinc2 = avr->data[0x33]; + printf("SW1 pressed, DDRC = 0x%02x, PINC 0x%02x -> 0x%02x\n", ddrc, pinc1, pinc2); + + } + nextCommand = ReadyForNewCommand; + 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); + uint8_t ddrc = avr->data[0x27]; // Atmega644P + uint8_t portc = avr->data[0x28]; // Atmega644P + if ( (ddrc & 0x20) == 0 && (portc & 0x20) == 0x20) { // internal pullup needed + avr_ioport_external_t io_ext = { 'C', 0x20, 0x20 }; // PC5 ... SW2 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + } else if (startParameters->board == BoardSure) { + uint8_t ddrc = avr->data[0x34]; // Atmega16 + if ( (ddrc & 0x40) == 0) { // external pullup on board + avr_ioport_external_t io_ext = { 'C', 0x40, 0x40 }; // PC6 ... SW2 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } } nextCommand = ReadyForNewCommand; break; @@ -932,8 +1010,20 @@ void SimAvr::avrRun () { 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); + uint8_t ddrc = avr->data[0x27]; // Atmega644P + uint8_t portc = avr->data[0x28]; // Atmega644P + if ( (ddrc & 0x20) == 0 && (portc & 0x20) == 0x20) { // internal pullup needed + // vr_ioport_external_t p; + avr_ioport_external_t io_ext = { 'C', 0x20, 0x0 }; // PC5 ... SW2 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + } else if (startParameters->board == BoardSure) { + uint8_t ddrc = avr->data[0x34]; // Atmega16 + if ( (ddrc & 0x80) == 0) { // external pullup on board + avr_ioport_external_t io_ext = { 'C', 0x40, 0x00 }; // PC6 ... SW2 + avr_ioctl(avr, AVR_IOCTL_IOPORT_SET_EXTERNAL('C'), &io_ext); + } + } nextCommand = ReadyForNewCommand; break; diff --git a/examples/simuc/src/simavr/simavr.h b/examples/simuc/src/simavr/simavr.h index 999beb9..d4ee0d0 100644 --- a/examples/simuc/src/simavr/simavr.h +++ b/examples/simuc/src/simavr/simavr.h @@ -65,6 +65,8 @@ typedef enum { CommandStack, CommandReset, CommandPower, + CommandSW1Released, + CommandSW1Pressed, CommandSW2Released, CommandSW2Pressed, } EnumSimAvrCommand; -- 2.39.5