Initial commit
This commit is contained in:
commit
074d7c8114
7 changed files with 252 additions and 0 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
10
.vscode/extensions.json
vendored
Normal file
10
.vscode/extensions.json
vendored
Normal file
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
37
include/README
Normal file
37
include/README
Normal file
|
|
@ -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
|
||||
46
lib/README
Normal file
46
lib/README
Normal file
|
|
@ -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 <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
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
|
||||
19
platformio.ini
Normal file
19
platformio.ini
Normal file
|
|
@ -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
|
||||
124
src/main.cpp
Normal file
124
src/main.cpp
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
#include <Arduino.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
// 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 <bits> <half_us>"
|
||||
// Example: "READ 25 5"
|
||||
if (!cmd.startsWith("READ ")) {
|
||||
Serial.println("ERR unknown command. Use: READ <bits> <half_us>");
|
||||
return;
|
||||
}
|
||||
|
||||
int firstSpace = cmd.indexOf(' ');
|
||||
int secondSpace = cmd.indexOf(' ', firstSpace + 1);
|
||||
if (secondSpace < 0) {
|
||||
Serial.println("ERR usage: READ <bits> <half_us>");
|
||||
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 <bits> <half_us>");
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
test/README
Normal file
11
test/README
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue