commit bb0fdeff66afd22b9fb3a41c8df7ed224ab38613 Author: matthias@rpi Date: Mon Oct 30 22:23:12 2023 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c928c4b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +spi diff --git a/.vimspector.json b/.vimspector.json new file mode 100644 index 0000000..509c996 --- /dev/null +++ b/.vimspector.json @@ -0,0 +1,22 @@ +{ + "configurations": { + "spi": { + "adapter": "vscode-cpptools", + "configuration": { + "name": "spi (cpp)", + "type": "cppdbg", + "request": "launch", + "externalConsole": true, + "logging": { + "engineLogging": true + }, + "stopOnEntry": true, + "stopAtEntry": true, + "debugOptions": [], + "MIMode": "gdb", + "cwd": "~/6502/spi/", + "program": "./spi" + } + } + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5e84830 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +CFLAGS = -g -std=c++20 -O2 -DBCM2835_HAVE_LIBCAP +LFLAGS = -lbcm2835 + +TARGET = spi + +default: $(TARGET) + +.PHONY = clean + +%: %.cpp + g++ $< -o $@ $(CFLAGS) $(LFLAGS) + +run: default + sudo ./$(TARGET) + + + +clean: + rm $(TARGET) diff --git a/spi.cpp b/spi.cpp new file mode 100644 index 0000000..255740e --- /dev/null +++ b/spi.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#define BCM2835 + + +int main(int argc, const char** argv) { + if (bcm2835_init() != 1) { + printf("Error: Could not initalise gpio libraray\n"); + return 1; + } + + if (bcm2835_spi_begin() != 1) { + printf("Error: Could not begin SPI mode. You might need this: https://www.airspayce.com/mikem/bcm2835/ \"Running as root\"\n"); + return 1; + } + + /* if (argc != 2) { */ + /* printf("Error: Expected exactly one argument (filename), got %d\n", argc); */ + /* return 1; */ + /* } */ + + bcm2835_spi_chipSelect(BCM2835_SPI_CS0); + /* bcm2835_spi_set_speed_hz(5); */ + bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); + bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + std::string send = "Hello 6502!"; + /* char send[] = "Hello 6502!"; */ + std::vector buffer; + buffer.reserve(send.size()); + for (char c : send) { + std::cout << c << std::endl; + buffer.push_back(c); + } + /* for (char i = 0; i < 8; i++) { */ + /* buffer.push_back(i); */ + /* } */ + /* buffer.push_back(0b10110101); */ + bcm2835_spi_transfern(buffer.data(), buffer.size()); + + /* std::jthread eepromT(simulateEEPROM, bytes); */ + /* std::atomic cs; */ + /* std::jthread clockT(printBusWithClock, PHI2, std::ref(cs)); */ + /* /1* auto handler = std::bind(signalHandler, std::placeholders::_1, std::move(eepromT)); *1/ */ + /* signal(SIGINT, signalHandler); */ + + + bcm2835_spi_end(); +}