From 074d7c811462faeed1465949aa1d97ad8ded6df5 Mon Sep 17 00:00:00 2001 From: Luca van Straaten Date: Tue, 28 Apr 2026 20:48:26 +0200 Subject: [PATCH] Initial commit --- .gitignore | 5 ++ .vscode/extensions.json | 10 ++++ include/README | 37 ++++++++++++ lib/README | 46 +++++++++++++++ platformio.ini | 19 ++++++ src/main.cpp | 124 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++++ 7 files changed, 252 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..2380d46 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:rp2040zero] +platform = https://github.com/maxgerhardt/platform-raspberrypi.git +board = waveshare_rp2040_zero +framework = arduino +board_build.core = earlephilhower +upload_protocol = picotool +monitor_speed = 115200 +lib_deps = + adafruit/Adafruit NeoPixel @ ^1.12.0 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a439d58 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,124 @@ +#include +#include + +// CLOCK module (TX) +const uint8_t TX_DI = 0; +const uint8_t TX_DE = 1; +const uint8_t TX_RE = 2; + +// DATA module (RX) +const uint8_t RX_DI = 26; +const uint8_t RX_DE = 27; +const uint8_t RX_RE = 28; +const uint8_t RX_RO = 29; + +const uint8_t LED_PIN = 16; +Adafruit_NeoPixel led(1, LED_PIN, NEO_GRB + NEO_KHZ800); + +void setStatus(uint8_t r, uint8_t g, uint8_t b) { + led.setPixelColor(0, led.Color(r, g, b)); + led.show(); +} + +// Read N bits from SSI encoder. +// SSI convention: encoder latches on first falling edge, +// shifts new bit on rising edge, master samples on falling edge. +uint64_t ssi_read(uint8_t bits, uint16_t half_us) { + uint64_t value = 0; + + // Frame start: clock idles HIGH, drop to LOW = latch trigger + digitalWrite(TX_DI, LOW); + delayMicroseconds(half_us); + + for (uint8_t i = 0; i < bits; i++) { + digitalWrite(TX_DI, HIGH); // encoder shifts next bit out + delayMicroseconds(half_us); + digitalWrite(TX_DI, LOW); // sample on falling edge + value = (value << 1) | (digitalRead(RX_RO) ? 1 : 0); + delayMicroseconds(half_us); + } + + // Return to idle and wait monoflop time + digitalWrite(TX_DI, HIGH); + delayMicroseconds(30); + + return value; +} + +void handleCommand(const String& cmd) { + // Format: "READ " + // Example: "READ 25 5" + if (!cmd.startsWith("READ ")) { + Serial.println("ERR unknown command. Use: READ "); + return; + } + + int firstSpace = cmd.indexOf(' '); + int secondSpace = cmd.indexOf(' ', firstSpace + 1); + if (secondSpace < 0) { + Serial.println("ERR usage: READ "); + return; + } + + int bits = cmd.substring(firstSpace + 1, secondSpace).toInt(); + int halfUs = cmd.substring(secondSpace + 1).toInt(); + + if (bits < 1 || bits > 64) { + Serial.println("ERR bits must be 1..64"); + return; + } + if (halfUs < 1 || halfUs > 10000) { + Serial.println("ERR half_us must be 1..10000"); + return; + } + + setStatus(0, 0, 16); // blue = reading + uint64_t raw = ssi_read(bits, halfUs); + setStatus(0, 16, 0); // green = idle + + // Print as hex and decimal + // (Pico printf supports %llX for 64-bit) + Serial.printf("OK bits=%d half_us=%d hex=0x%llX dec=%llu\n", + bits, halfUs, raw, raw); +} + +void setup() { + Serial.begin(115200); + led.begin(); + setStatus(8, 8, 0); + + pinMode(TX_DI, OUTPUT); + pinMode(TX_DE, OUTPUT); pinMode(TX_RE, OUTPUT); + pinMode(RX_DE, OUTPUT); pinMode(RX_RE, OUTPUT); + pinMode(RX_DI, OUTPUT); + pinMode(RX_RO, INPUT); + + digitalWrite(TX_DE, HIGH); + digitalWrite(TX_RE, HIGH); + digitalWrite(RX_DE, LOW); + digitalWrite(RX_RE, LOW); + digitalWrite(RX_DI, LOW); + digitalWrite(TX_DI, HIGH); // SSI idle HIGH + + delay(200); + while (!Serial && millis() < 3000) { delay(10); } + Serial.println("\nSSI bridge ready"); + Serial.println("Send: READ "); + Serial.println("Example: READ 25 5"); + setStatus(0, 16, 0); +} + +void loop() { + static String buf; + while (Serial.available()) { + char c = Serial.read(); + if (c == '\n' || c == '\r') { + if (buf.length() > 0) { + handleCommand(buf); + buf = ""; + } + } else if (buf.length() < 64) { + buf += c; + } + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html