From abae48ddaf42d526801d630352ed4bd5fbb8faa0 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 14 Oct 2025 14:20:20 -0700 Subject: [PATCH 01/23] Initial Commit --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 1 + blaze-lite/core/lib/spi_flash/spiFlash.h | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 blaze-lite/core/lib/spi_flash/spiFlash.cpp create mode 100644 blaze-lite/core/lib/spi_flash/spiFlash.h diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp new file mode 100644 index 0000000..2cbbd39 --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -0,0 +1 @@ +#include "spiFlash.h" diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h new file mode 100644 index 0000000..0abbd6b --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -0,0 +1,25 @@ +/* + spiFlash.h - a library that handles the Adafruit SPI flash breakout board for Blackpill Arduino + To use: + include this header file in your main program, DO NOT directly call the cpp file +*/ + +#ifndef SPI_FLASH_H +#define SPI_FLASH_H + +#include +#include +#include + +#include + +class spiFlash { + public: + + private: + +} + + + +#endif \ No newline at end of file From 7fca79cab0de3057966aa7e1c9e248a08057ef4b Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 14 Oct 2025 14:29:41 -0700 Subject: [PATCH 02/23] added Libary dependency and some basic methods --- blaze-lite/core/lib/spi_flash/spiFlash.h | 9 +++++++++ blaze-lite/core/platformio.ini | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 0abbd6b..225e9ce 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -15,8 +15,17 @@ class spiFlash { public: + //Constructor: + spiFlash(); + + //Get Methods: + uint8_t getCS_PIN(uint8_t pin); + + //Set Methods: + bool setCS_PIN(uint8_t pin); private: + CS_PIN; } diff --git a/blaze-lite/core/platformio.ini b/blaze-lite/core/platformio.ini index 13d25c3..3dd57ca 100644 --- a/blaze-lite/core/platformio.ini +++ b/blaze-lite/core/platformio.ini @@ -11,8 +11,9 @@ [env:blackpill_f411ce] platform = ststm32 board = blackpill_f411ce -framework=arduino +framework = arduino upload_protocol = dfu -build_flags = - -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC - -D USBCON +build_flags = + -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC + -D USBCON +lib_deps = adafruit/Adafruit SPIFlash@^5.1.1 From 6ffd79c62b4b39eb06aa8b7f3e0c4997689187e2 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 14 Oct 2025 14:30:36 -0700 Subject: [PATCH 03/23] update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2ac704 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/blaze-lite/core/.vscode From edf819e7739de29353cccfb1ab0a8e783348a20c Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 21 Oct 2025 13:22:48 -0700 Subject: [PATCH 04/23] added basic functions and headers --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 25 ++++++++++++++++++++++ blaze-lite/core/lib/spi_flash/spiFlash.h | 25 +++++++++++++--------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index 2cbbd39..b6fae22 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -1 +1,26 @@ +/* + * spiFlash.cpp - SPI Flash library for Blaze + * To use: + include the corresponding header file in your main program, DO NOT directly call this cpp file +*/ #include "spiFlash.h" + +//Constructor: +spiFlash::spiFlash() { + //Default Constructor + CS_PIN = 0; //sets default Chip Select pin to 0 + + Adafruit_FlashTransport_SPI flashTransport(CS_PIN, SPI); + Adafruit_SPIFlash flash(&flashTransport); +} + +//Get Methods: +uint8_t spiFlash::getCS_PIN(uint8_t pin) { + return CS_PIN; +} + +//Set Methods: +void spiFlash::setCS_PIN(uint8_t pin) { + CS_PIN = pin; +} + diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 225e9ce..f566633 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -1,9 +1,3 @@ -/* - spiFlash.h - a library that handles the Adafruit SPI flash breakout board for Blackpill Arduino - To use: - include this header file in your main program, DO NOT directly call the cpp file -*/ - #ifndef SPI_FLASH_H #define SPI_FLASH_H @@ -22,13 +16,24 @@ class spiFlash { uint8_t getCS_PIN(uint8_t pin); //Set Methods: - bool setCS_PIN(uint8_t pin); + void setCS_PIN(uint8_t pin); - private: - CS_PIN; + //functionality methods: + ssize_t read(size_t offset, size_t bytes, uint8_t* buffer); -} + uint8_t queue(size_t bytes, uint8_t* data, int priority = P_UNIMPORTANT); + + uint8_t buffer (size_t bytes, uint8_t* data); + + ssize_t write (size_t bytes, uint8_t* data); + void flush (void); + ssize_t kLog (size_t bytes, uint8_t* data); + + private: + uint8_t CS_PIN; + +} #endif \ No newline at end of file From 46ad0b5bd523c127d914441d91ca62468452bf7a Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Mon, 27 Oct 2025 22:03:37 +0000 Subject: [PATCH 05/23] combining prateeks old code --- .../core/lib/spi_flash/old_spiflash.cpp | 90 +++++++++++++++++++ .../core/lib/spi_flash/old_spiflash.hpp | 64 +++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 blaze-lite/core/lib/spi_flash/old_spiflash.cpp create mode 100644 blaze-lite/core/lib/spi_flash/old_spiflash.hpp diff --git a/blaze-lite/core/lib/spi_flash/old_spiflash.cpp b/blaze-lite/core/lib/spi_flash/old_spiflash.cpp new file mode 100644 index 0000000..61d529f --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/old_spiflash.cpp @@ -0,0 +1,90 @@ +#include "spiflash.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* constexpr */ const size_t spiflash::buffer_size = 4098; +size_t spiflash::buffer_offset = 0; + +char spiflash::obuff[/* spiflash::buffer_size */ 4098]; + +bool spiflash::CmpOPriority::operator()(const std::tuple& l, const std::tuple& r) const { + return std::get<0>(l) > std::get<0>(r); +} + + + std::priority_queue< + std::tuple, + std::vector>, + spiflash::CmpOPriority + > spiflash::queuedos = std::priority_queue< + std::tuple, + std::vector>, + spiflash::CmpOPriority + >(); //todo: do I need to make this atomic? + +// priorities from most to least important +/* constexpr */ const uint8_t + spiflash::P_MANDATORY = 0, //just force writes this at next tick + spiflash::P_URGENT = 1, + spiflash::P_IMPORTANT = 2, + spiflash::P_STD = 3, + spiflash::P_UNIMPORTANT = 4, + spiflash::P_OPTIONAL = 5 +; + +ssize_t spiflash:: read (const size_t offset, const size_t bytes, char* buffer) ; //hard read. used for extracting data post-flight +ssize_t spiflash::kread (const size_t offset, const size_t bytes, char* buffer) ; //hard read. used for extracting data post-flight + +void spiflash::queue (/* const */ size_t bytes, /* const */ char* data, /* const */ uint8_t priority) { + spiflash::queuedos.push(std::tie(priority, bytes, data)); +} + +ssize_t spiflash::buffer (const size_t bytes, const char* data) { //TODO: add err handeling + size_t offset = 0, temp = 0; + + memcpy(spiflash::obuff + spiflash::buffer_offset, data, temp = std::min(bytes, spiflash::buffer_size - spiflash::buffer_offset)); + spiflash::buffer_offset += (offset += temp); + + while (bytes - offset > 0) { + spiflash::flush(); + memcpy(spiflash::obuff, data + offset, temp = std::min(bytes - offset, spiflash::buffer_size - spiflash::buffer_offset)); + spiflash::buffer_offset += temp; + offset += temp; + } + + return offset; +} + +ssize_t spiflash::write (const size_t bytes, const char* data) { + //TODO MAKE REAL, rn is just a testr function + + // static auto fd = 0; + // if(!fd) fd = open("./spiflashTestDump.txt", O_CREAT | O_RDWR); + + // write(bytes, data); //FIXME: this is supposed to be normal write function + + std::cout.write(data, bytes); + return 0; +} + +void spiflash::flush (void) { + spiflash::write (spiflash::buffer_offset, spiflash::obuff); + memset(spiflash::obuff, 0, spiflash::buffer_size); + spiflash::buffer_offset = 0; +} + +size_t spiflash::kLog (const size_t bytes, const char* data) ; //just stright up a write +size_t spiflash::kWrite (const size_t bytes, const char* data) ; //just stright up a write +size_t spiflash::kFlush (const size_t bytes, const char* data) ; //just stright up a write + +int spiflash::tick (void) ; \ No newline at end of file diff --git a/blaze-lite/core/lib/spi_flash/old_spiflash.hpp b/blaze-lite/core/lib/spi_flash/old_spiflash.hpp new file mode 100644 index 0000000..42bba62 --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/old_spiflash.hpp @@ -0,0 +1,64 @@ +/* +TODO: +- make amny paramerters const T& +*/ + +#pragma once +#include +#include +#include +#include +#include + +//common storage interface +#ifndef strg + #define strg spiflash +#endif + +#ifndef KLOG_SIZE + #define KLOG_SIZE 4098 +#endif + +namespace spiflash { + extern /* constexpr */ const size_t buffer_size; + extern size_t buffer_offset; + + extern char obuff[/* buffer_size */ 4098]; + + struct CmpOPriority { + bool operator()(const std::tuple& l, const std::tuple& r) const ; + }; + + extern std::priority_queue< + std::tuple, + std::vector>, + CmpOPriority + > queuedos; //todo: do I need to make this atomic? + + //priorities from most to least important + extern /* constexpr */ const uint8_t + P_MANDATORY , //just force writes this at next tick + P_URGENT , + P_IMPORTANT , + P_STD , + P_UNIMPORTANT , + P_OPTIONAL + ; + + ssize_t read (const size_t offset, const size_t bytes, char* buffer) ; + ssize_t kread (const size_t offset, const size_t bytes, char* buffer) ; + + void queue (/* const */ size_t bytes, /* const */ char* data, /* const */ uint8_t priority = P_STD /*std priority*/) ; + + ssize_t buffer (const size_t bytes, const char* data) ; + + ssize_t write (const size_t bytes, const char* data) ; + + void flush (void) ; + + size_t kLog (const size_t bytes, const char* data) ; //just stright up a write + size_t kWrite (const size_t bytes, const char* data) ; //just stright up a write + size_t kFlush (const size_t bytes, const char* data) ; //just stright up a write + + int tick (void) ; +} \ No newline at end of file From b0fdd995839b76db9d5aec3008028d6b0213372e Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Mon, 3 Nov 2025 01:11:17 +0000 Subject: [PATCH 06/23] Mostly complete with spiflash additional layers (no tick, read, write) --- .../core/lib/spi_flash/old_spiflash.cpp | 90 ----------- .../core/lib/spi_flash/old_spiflash.hpp | 64 -------- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 146 ++++++++++++++++-- blaze-lite/core/lib/spi_flash/spiFlash.h | 93 ++++++++--- blaze-lite/core/lib/spi_flash/test.cpp | 134 ++++++++++++++++ 5 files changed, 342 insertions(+), 185 deletions(-) delete mode 100644 blaze-lite/core/lib/spi_flash/old_spiflash.cpp delete mode 100644 blaze-lite/core/lib/spi_flash/old_spiflash.hpp create mode 100644 blaze-lite/core/lib/spi_flash/test.cpp diff --git a/blaze-lite/core/lib/spi_flash/old_spiflash.cpp b/blaze-lite/core/lib/spi_flash/old_spiflash.cpp deleted file mode 100644 index 61d529f..0000000 --- a/blaze-lite/core/lib/spi_flash/old_spiflash.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include "spiflash.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -/* constexpr */ const size_t spiflash::buffer_size = 4098; -size_t spiflash::buffer_offset = 0; - -char spiflash::obuff[/* spiflash::buffer_size */ 4098]; - -bool spiflash::CmpOPriority::operator()(const std::tuple& l, const std::tuple& r) const { - return std::get<0>(l) > std::get<0>(r); -} - - - std::priority_queue< - std::tuple, - std::vector>, - spiflash::CmpOPriority - > spiflash::queuedos = std::priority_queue< - std::tuple, - std::vector>, - spiflash::CmpOPriority - >(); //todo: do I need to make this atomic? - -// priorities from most to least important -/* constexpr */ const uint8_t - spiflash::P_MANDATORY = 0, //just force writes this at next tick - spiflash::P_URGENT = 1, - spiflash::P_IMPORTANT = 2, - spiflash::P_STD = 3, - spiflash::P_UNIMPORTANT = 4, - spiflash::P_OPTIONAL = 5 -; - -ssize_t spiflash:: read (const size_t offset, const size_t bytes, char* buffer) ; //hard read. used for extracting data post-flight -ssize_t spiflash::kread (const size_t offset, const size_t bytes, char* buffer) ; //hard read. used for extracting data post-flight - -void spiflash::queue (/* const */ size_t bytes, /* const */ char* data, /* const */ uint8_t priority) { - spiflash::queuedos.push(std::tie(priority, bytes, data)); -} - -ssize_t spiflash::buffer (const size_t bytes, const char* data) { //TODO: add err handeling - size_t offset = 0, temp = 0; - - memcpy(spiflash::obuff + spiflash::buffer_offset, data, temp = std::min(bytes, spiflash::buffer_size - spiflash::buffer_offset)); - spiflash::buffer_offset += (offset += temp); - - while (bytes - offset > 0) { - spiflash::flush(); - memcpy(spiflash::obuff, data + offset, temp = std::min(bytes - offset, spiflash::buffer_size - spiflash::buffer_offset)); - spiflash::buffer_offset += temp; - offset += temp; - } - - return offset; -} - -ssize_t spiflash::write (const size_t bytes, const char* data) { - //TODO MAKE REAL, rn is just a testr function - - // static auto fd = 0; - // if(!fd) fd = open("./spiflashTestDump.txt", O_CREAT | O_RDWR); - - // write(bytes, data); //FIXME: this is supposed to be normal write function - - std::cout.write(data, bytes); - return 0; -} - -void spiflash::flush (void) { - spiflash::write (spiflash::buffer_offset, spiflash::obuff); - memset(spiflash::obuff, 0, spiflash::buffer_size); - spiflash::buffer_offset = 0; -} - -size_t spiflash::kLog (const size_t bytes, const char* data) ; //just stright up a write -size_t spiflash::kWrite (const size_t bytes, const char* data) ; //just stright up a write -size_t spiflash::kFlush (const size_t bytes, const char* data) ; //just stright up a write - -int spiflash::tick (void) ; \ No newline at end of file diff --git a/blaze-lite/core/lib/spi_flash/old_spiflash.hpp b/blaze-lite/core/lib/spi_flash/old_spiflash.hpp deleted file mode 100644 index 42bba62..0000000 --- a/blaze-lite/core/lib/spi_flash/old_spiflash.hpp +++ /dev/null @@ -1,64 +0,0 @@ -/* -TODO: -- make amny paramerters const T& -*/ - -#pragma once -#include -#include -#include -#include -#include - -//common storage interface -#ifndef strg - #define strg spiflash -#endif - -#ifndef KLOG_SIZE - #define KLOG_SIZE 4098 -#endif - -namespace spiflash { - extern /* constexpr */ const size_t buffer_size; - extern size_t buffer_offset; - - extern char obuff[/* buffer_size */ 4098]; - - struct CmpOPriority { - bool operator()(const std::tuple& l, const std::tuple& r) const ; - }; - - extern std::priority_queue< - std::tuple, - std::vector>, - CmpOPriority - > queuedos; //todo: do I need to make this atomic? - - //priorities from most to least important - extern /* constexpr */ const uint8_t - P_MANDATORY , //just force writes this at next tick - P_URGENT , - P_IMPORTANT , - P_STD , - P_UNIMPORTANT , - P_OPTIONAL - ; - - ssize_t read (const size_t offset, const size_t bytes, char* buffer) ; - ssize_t kread (const size_t offset, const size_t bytes, char* buffer) ; - - void queue (/* const */ size_t bytes, /* const */ char* data, /* const */ uint8_t priority = P_STD /*std priority*/) ; - - ssize_t buffer (const size_t bytes, const char* data) ; - - ssize_t write (const size_t bytes, const char* data) ; - - void flush (void) ; - - size_t kLog (const size_t bytes, const char* data) ; //just stright up a write - size_t kWrite (const size_t bytes, const char* data) ; //just stright up a write - size_t kFlush (const size_t bytes, const char* data) ; //just stright up a write - - int tick (void) ; -} \ No newline at end of file diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index b6fae22..f2c5a62 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -5,22 +5,148 @@ */ #include "spiFlash.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//TODO: Make private methods to simplify code +/* + //char getCS_PIN(char pin); + + //void setCS_PIN(char pin); + + ssize_t read(const size_t offset, const size_t bytes, const char* buffer); + + //char queue(size_t bytes, char* data, int priority = P_UNIMPORTANT); + + //char buffer (const size_t bytes, char* data); + + ssize_t write (const size_t bytes, const char* data); + + //ssize_t kwrite (const size_t bytes, const char* data); + + //void flush (void); + + //ssize_t kLog (const size_t bytes, const char* data); //buffer but with k stuff + + //void kflush (void); //flush but for k stuff + + ssize_t tick(void) ; +*/ //Constructor: -spiFlash::spiFlash() { - //Default Constructor - CS_PIN = 0; //sets default Chip Select pin to 0 +spiFlash::spiFlash (const char cs_pin, const size_t buffer_size, const size_t k_buffer_size) : CS_PIN(cs_pin), buffer_size(buffer_size), k_buffer_size(k_buffer_size), buffer_offset(0), k_buffer_offset(0) { + obuff = new char[ buffer_size]; + kbuff = new char[k_buffer_size]; + queuedos = std::priority_queue, std::vector>, cmp_io_priority>(); + + fd = open("./spifTestNormal.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); //TODO: remove once write() works + kfd = open("./spifTestKernel.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); +} + +spiFlash::~spiFlash () { + kflush(); + flush(); - Adafruit_FlashTransport_SPI flashTransport(CS_PIN, SPI); - Adafruit_SPIFlash flash(&flashTransport); + close(fd); + close(kfd); + + delete[] obuff; + delete[] kbuff; } //Get Methods: -uint8_t spiFlash::getCS_PIN(uint8_t pin) { - return CS_PIN; -} +char spiFlash::getCS_PIN() { return CS_PIN; } //Set Methods: -void spiFlash::setCS_PIN(uint8_t pin) { - CS_PIN = pin; +void spiFlash::setCS_PIN(const char pin) { CS_PIN = pin; } + +//functionality methods: +// ssize_t spiFlash::read(const size_t offset, const size_t bytes, const char* buffer) ; + +char spiFlash::queue(size_t bytes, char* data, char priority) { + queuedos.push(std::tie(priority, bytes, data)); + return 0; +} + +char spiFlash::buffer (const size_t bytes, const char* data) { //TODO: add err handeling + ssize_t err = 0; + size_t offset = 0, temp = 0; + + memcpy(obuff + buffer_offset, data, temp = std::min(bytes, buffer_size - buffer_offset)); + buffer_offset += (offset += temp); + + while (bytes - offset > 0) { + if (err = flush() < 0) return err; + flush(); + + memcpy(obuff, data + offset, temp = std::min(bytes - offset, buffer_size)); + buffer_offset += temp; + offset += temp; + } + + return err; +} + +//return value < 0 means error +ssize_t spiFlash::write (const size_t bytes, const char* data) { + return ::write(fd, data, bytes); +} + +ssize_t spiFlash::kwrite (const size_t bytes, const char* data) { + return ::write(kfd, data, bytes); +} + +char spiFlash::flush (void) { + ssize_t err = write (buffer_offset, obuff); + if (err < 0) return err; + + memset(obuff, 0, buffer_size); + buffer_offset = 0; + + return 0; +} + +ssize_t spiFlash::kLog (const size_t bytes, const char* data) { //TODO: add err handeling + ssize_t err = 0; + size_t offset = 0, temp = 0; + + memcpy(kbuff + k_buffer_offset, data, temp = std::min(bytes, k_buffer_size - k_buffer_offset)); + k_buffer_offset += (offset += temp); + + while (bytes - offset > 0) { + if (err = kflush() < 0) return err; + memcpy(kbuff, data + offset, temp = std::min(bytes - offset, k_buffer_size - k_buffer_offset)); + k_buffer_offset += temp; + offset += temp; + } + + return err; +} + +char spiFlash::kflush (void) { + ssize_t err = kwrite (k_buffer_offset, kbuff); + if (err < 0) return err; + memset(kbuff, 0, k_buffer_size); + k_buffer_offset = 0; + return err; +} + +bool spiFlash::cmp_io_priority:: operator()(const std::tuple& l, const std::tuple& r) const { + return std::get<0>(l) > std::get<0>(r); } +// constexpr const char +// spiFlash::P_MANDATORY = 0, //just force writes this at next tick +// spiFlash::P_URGENT = 1, +// spiFlash::P_IMPORTANT = 2, +// spiFlash::P_STD = 3, +// spiFlash::P_UNIMPORTANT = 4, +// spiFlash::P_OPTIONAL = 5 +// ; diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index f566633..7913b23 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -1,39 +1,90 @@ #ifndef SPI_FLASH_H #define SPI_FLASH_H -#include -#include -#include +// #include +// #include +// #include -#include +// #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//TODO: Documentation, Order methods in aphabetical class spiFlash { - public: - //Constructor: - spiFlash(); + public: + //Constructor: + spiFlash (const char cs_pin, const size_t buffer_size, const size_t k_buffer_size) ; + + //destructor: + ~spiFlash() ; + + //Get Methods: + char getCS_PIN(); + + //Set Methods: + void setCS_PIN(char pin); + + //functionality methods: + ssize_t read(const size_t offset, const size_t bytes, const char* buffer); + + char queue(size_t bytes, char* data, char priority = P_UNIMPORTANT); + + char buffer (const size_t bytes, const char* data); + + ssize_t write (const size_t bytes, const char* data); + + ssize_t kwrite (const size_t bytes, const char* data); + + char flush (void); - //Get Methods: - uint8_t getCS_PIN(uint8_t pin); + ssize_t kLog (const size_t bytes, const char* data); - //Set Methods: - void setCS_PIN(uint8_t pin); + char kflush (void); - //functionality methods: - ssize_t read(size_t offset, size_t bytes, uint8_t* buffer); + ssize_t tick(void) ; - uint8_t queue(size_t bytes, uint8_t* data, int priority = P_UNIMPORTANT); + //types + struct cmp_io_priority { + bool operator()(const std::tuple& l, const std::tuple& r) const ; + }; - uint8_t buffer (size_t bytes, uint8_t* data); + static constexpr const char + P_MANDATORY = 0, //just force writes this at next tick + P_URGENT = 1, + P_IMPORTANT = 2, + P_STD = 3, + P_UNIMPORTANT = 4, + P_OPTIONAL = 5 + ; - ssize_t write (size_t bytes, uint8_t* data); + const size_t buffer_size; + const size_t k_buffer_size; - void flush (void); + private: + char CS_PIN; + + std::priority_queue< + // priority size data + std::tuple, + std::vector>, + cmp_io_priority + > queuedos; - ssize_t kLog (size_t bytes, uint8_t* data); + char* obuff; + size_t buffer_offset; - private: - uint8_t CS_PIN; + char* kbuff; + size_t k_buffer_offset; -} + int fd, kfd; +}; #endif \ No newline at end of file diff --git a/blaze-lite/core/lib/spi_flash/test.cpp b/blaze-lite/core/lib/spi_flash/test.cpp new file mode 100644 index 0000000..bb0eb1c --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/test.cpp @@ -0,0 +1,134 @@ +#include "spiFlash.h" + +using std::cout, std::endl, std::cin; +/* +tests: +1. write +2. kwrite + +3. buffer - 1 small msg +4. flush +5. kLog - 1 small msg +6. kflush + +7. buffer - 2 small msgs +8. kLog - 2 small msgs + +9. buffer - 1 large msg +10. kLog - 1 large msg + +11. buffer - 1 xl msg +12. kLog - 1 xl msg + +13. buffer - 2 large msgs +14. kLog - 2 large msgs + +15. buffer - 1 xl msg -> 1 l msg +16. kLog - 1 xl msg -> 1 l msg + +17. queue +18. tick +*/ + +#define GREETING "Hello World!\n" +#define MSG1 "This is the first msg.\n" +#define MSG2 "This is the second msg.\n" +#define MSGL msgl +#define MSGL2 msgl2 +#define MSGXL msgxl + +#define BUFF_SIZE 256 + +int main () { + char wait; + char msgl[BUFF_SIZE]; + char msgl2[BUFF_SIZE]; + char msgxl[BUFF_SIZE + 8]; + + memset(msgl, '1', sizeof(msgl)); + memset(msgl2, '2', sizeof(msgl2)); + memset(msgxl, 'X', sizeof(msgxl)); + + spiFlash f(0, BUFF_SIZE, BUFF_SIZE); + + + f.write(sizeof(GREETING), GREETING); + cout << "test 1 done" << endl; + cin >> wait; + + f.kwrite(sizeof(GREETING), GREETING); + cout << "test 2 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSG1), MSG1); + cout << "test 3 done" << endl; + cin >> wait; + + f.flush(); + cout << "test 4 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSG1), MSG1); + cout << "test 5 done" << endl; + cin >> wait; + + f.kflush(); + cout << "test 6 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSG1), MSG1); + f.buffer(sizeof(MSG2), MSG2); + f.flush(); + cout << "test 7 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSG1), MSG1); + f.kLog(sizeof(MSG2), MSG2); + f.kflush(); + cout << "test 8 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGL), MSGL); + f.flush(); + cout << "test 9 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGL), MSGL); + f.kflush(); + cout << "test 10 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGXL), MSGXL); + f.flush(); + cout << "test 11 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGXL), MSGXL); + f.kflush(); + cout << "test 12 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGL), MSGL); + f.buffer(sizeof(MSGL2), MSGL2); + f.flush(); + cout << "test 13 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGL), MSGL); + f.kLog(sizeof(MSGL2), MSGL2); + f.kflush(); + cout << "test 14 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGXL), MSGXL); + f.buffer(sizeof(MSGL2), MSGL2); + f.flush(); + cout << "test 15 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGXL), MSGXL); + f.kLog(sizeof(MSGL2), MSGL2); + f.kflush(); + cout << "test 16 done" << endl; + cin >> wait; +} \ No newline at end of file From 4830c7331a341693d393f26a03b005baf166df31 Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 13 Nov 2025 12:11:34 -0800 Subject: [PATCH 07/23] added function call checks for buffer and kLog --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index f2c5a62..70ef98c 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -75,7 +75,12 @@ char spiFlash::queue(size_t bytes, char* data, char priority) { return 0; } -char spiFlash::buffer (const size_t bytes, const char* data) { //TODO: add err handeling +char spiFlash::buffer (const size_t bytes, const char* data) { + //function call error handling + if (bytes == 0) return 0; //no bytes to write + if (data == nullptr) return -1; //null data pointer + if (obuff == nullptr) return -2; //null buffer pointer + ssize_t err = 0; size_t offset = 0, temp = 0; @@ -113,7 +118,12 @@ char spiFlash::flush (void) { return 0; } -ssize_t spiFlash::kLog (const size_t bytes, const char* data) { //TODO: add err handeling +ssize_t spiFlash::kLog (const size_t bytes, const char* data) { + //function call error handling + if (bytes == 0) return 0; //no bytes to write + if (data == nullptr) return -1; //null data pointer + if (kbuff == nullptr) return -2; //null buffer pointer + ssize_t err = 0; size_t offset = 0, temp = 0; From 7d3682a5e6295558eed22ef98f3f96c2916cc533 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 21 Nov 2025 15:28:33 -0800 Subject: [PATCH 08/23] updated read function with error handling --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 11 ++++++++++- blaze-lite/core/lib/spi_flash/spiFlash.h | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index 70ef98c..bc682d6 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -68,7 +68,16 @@ char spiFlash::getCS_PIN() { return CS_PIN; } void spiFlash::setCS_PIN(const char pin) { CS_PIN = pin; } //functionality methods: -// ssize_t spiFlash::read(const size_t offset, const size_t bytes, const char* buffer) ; +ssize_t spiFlash::read(const size_t offset, const size_t bytes, char* buffer){ + //error handling + if (bytes == 0) return 0; //no bytes to read + if (buffer == nullptr) return -1; //null data pointer + if (lseek(fd, offset, SEEK_SET) == -1 ) return -2; //seek error + + ssize_t bytes_read = ::read(fd, buffer, bytes); + + return bytes_read; +} char spiFlash::queue(size_t bytes, char* data, char priority) { queuedos.push(std::tie(priority, bytes, data)); diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 7913b23..989df13 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -33,7 +33,7 @@ class spiFlash { void setCS_PIN(char pin); //functionality methods: - ssize_t read(const size_t offset, const size_t bytes, const char* buffer); + ssize_t read(const size_t offset, const size_t bytes, char* buffer); char queue(size_t bytes, char* data, char priority = P_UNIMPORTANT); From 99c71e057f0f914b88e036a69385f4e33d41c1ba Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 1 Dec 2025 14:56:15 -0800 Subject: [PATCH 09/23] added file for writing unittests --- blaze-lite/core/test/spiUnittests.cpp | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 blaze-lite/core/test/spiUnittests.cpp diff --git a/blaze-lite/core/test/spiUnittests.cpp b/blaze-lite/core/test/spiUnittests.cpp new file mode 100644 index 0000000..6c7e9cc --- /dev/null +++ b/blaze-lite/core/test/spiUnittests.cpp @@ -0,0 +1,4 @@ +/* + This file contains the unit tests for the spiFlash class. +*/ +#include "spiFlash.h" From 36886219f99e995ba46aa96093b31768ab033fb0 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 1 Dec 2025 15:26:35 -0800 Subject: [PATCH 10/23] made changes to lib file and moved untitest files --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 50 +++----- blaze-lite/core/lib/spi_flash/test.cpp | 134 --------------------- blaze-lite/core/test/spiUnittests.cpp | 133 ++++++++++++++++++++ 3 files changed, 152 insertions(+), 165 deletions(-) delete mode 100644 blaze-lite/core/lib/spi_flash/test.cpp diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index bc682d6..30ef6fe 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -16,38 +16,29 @@ #include #include -//TODO: Make private methods to simplify code -/* - //char getCS_PIN(char pin); - - //void setCS_PIN(char pin); - - ssize_t read(const size_t offset, const size_t bytes, const char* buffer); +Adafruit_SPIFlash flash(&flashTransport); - //char queue(size_t bytes, char* data, int priority = P_UNIMPORTANT); +FatVolume fatfs; - //char buffer (const size_t bytes, char* data); +File32 root; +File32 file; - ssize_t write (const size_t bytes, const char* data); - - //ssize_t kwrite (const size_t bytes, const char* data); - - //void flush (void); - - //ssize_t kLog (const size_t bytes, const char* data); //buffer but with k stuff +//TODO: Make private methods to simplify code - //void kflush (void); //flush but for k stuff +constexpr const char + spiFlash::P_MANDATORY = 0, //just force writes this at next tick + spiFlash::P_URGENT = 1, + spiFlash::P_IMPORTANT = 2, + spiFlash::P_STD = 3, + spiFlash::P_UNIMPORTANT = 4, + spiFlash::P_OPTIONAL = 5 +; - ssize_t tick(void) ; -*/ //Constructor: spiFlash::spiFlash (const char cs_pin, const size_t buffer_size, const size_t k_buffer_size) : CS_PIN(cs_pin), buffer_size(buffer_size), k_buffer_size(k_buffer_size), buffer_offset(0), k_buffer_offset(0) { obuff = new char[ buffer_size]; kbuff = new char[k_buffer_size]; queuedos = std::priority_queue, std::vector>, cmp_io_priority>(); - - fd = open("./spifTestNormal.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); //TODO: remove once write() works - kfd = open("./spifTestKernel.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); } spiFlash::~spiFlash () { @@ -59,6 +50,12 @@ spiFlash::~spiFlash () { delete[] obuff; delete[] kbuff; + +} + +//hardware setup function (this should be called in the main file setup loop): +void spiFlash::startUp() { + } //Get Methods: @@ -160,12 +157,3 @@ char spiFlash::kflush (void) { bool spiFlash::cmp_io_priority:: operator()(const std::tuple& l, const std::tuple& r) const { return std::get<0>(l) > std::get<0>(r); } - -// constexpr const char -// spiFlash::P_MANDATORY = 0, //just force writes this at next tick -// spiFlash::P_URGENT = 1, -// spiFlash::P_IMPORTANT = 2, -// spiFlash::P_STD = 3, -// spiFlash::P_UNIMPORTANT = 4, -// spiFlash::P_OPTIONAL = 5 -// ; diff --git a/blaze-lite/core/lib/spi_flash/test.cpp b/blaze-lite/core/lib/spi_flash/test.cpp deleted file mode 100644 index bb0eb1c..0000000 --- a/blaze-lite/core/lib/spi_flash/test.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "spiFlash.h" - -using std::cout, std::endl, std::cin; -/* -tests: -1. write -2. kwrite - -3. buffer - 1 small msg -4. flush -5. kLog - 1 small msg -6. kflush - -7. buffer - 2 small msgs -8. kLog - 2 small msgs - -9. buffer - 1 large msg -10. kLog - 1 large msg - -11. buffer - 1 xl msg -12. kLog - 1 xl msg - -13. buffer - 2 large msgs -14. kLog - 2 large msgs - -15. buffer - 1 xl msg -> 1 l msg -16. kLog - 1 xl msg -> 1 l msg - -17. queue -18. tick -*/ - -#define GREETING "Hello World!\n" -#define MSG1 "This is the first msg.\n" -#define MSG2 "This is the second msg.\n" -#define MSGL msgl -#define MSGL2 msgl2 -#define MSGXL msgxl - -#define BUFF_SIZE 256 - -int main () { - char wait; - char msgl[BUFF_SIZE]; - char msgl2[BUFF_SIZE]; - char msgxl[BUFF_SIZE + 8]; - - memset(msgl, '1', sizeof(msgl)); - memset(msgl2, '2', sizeof(msgl2)); - memset(msgxl, 'X', sizeof(msgxl)); - - spiFlash f(0, BUFF_SIZE, BUFF_SIZE); - - - f.write(sizeof(GREETING), GREETING); - cout << "test 1 done" << endl; - cin >> wait; - - f.kwrite(sizeof(GREETING), GREETING); - cout << "test 2 done" << endl; - cin >> wait; - - f.buffer(sizeof(MSG1), MSG1); - cout << "test 3 done" << endl; - cin >> wait; - - f.flush(); - cout << "test 4 done" << endl; - cin >> wait; - - f.kLog(sizeof(MSG1), MSG1); - cout << "test 5 done" << endl; - cin >> wait; - - f.kflush(); - cout << "test 6 done" << endl; - cin >> wait; - - f.buffer(sizeof(MSG1), MSG1); - f.buffer(sizeof(MSG2), MSG2); - f.flush(); - cout << "test 7 done" << endl; - cin >> wait; - - f.kLog(sizeof(MSG1), MSG1); - f.kLog(sizeof(MSG2), MSG2); - f.kflush(); - cout << "test 8 done" << endl; - cin >> wait; - - f.buffer(sizeof(MSGL), MSGL); - f.flush(); - cout << "test 9 done" << endl; - cin >> wait; - - f.kLog(sizeof(MSGL), MSGL); - f.kflush(); - cout << "test 10 done" << endl; - cin >> wait; - - f.buffer(sizeof(MSGXL), MSGXL); - f.flush(); - cout << "test 11 done" << endl; - cin >> wait; - - f.kLog(sizeof(MSGXL), MSGXL); - f.kflush(); - cout << "test 12 done" << endl; - cin >> wait; - - f.buffer(sizeof(MSGL), MSGL); - f.buffer(sizeof(MSGL2), MSGL2); - f.flush(); - cout << "test 13 done" << endl; - cin >> wait; - - f.kLog(sizeof(MSGL), MSGL); - f.kLog(sizeof(MSGL2), MSGL2); - f.kflush(); - cout << "test 14 done" << endl; - cin >> wait; - - f.buffer(sizeof(MSGXL), MSGXL); - f.buffer(sizeof(MSGL2), MSGL2); - f.flush(); - cout << "test 15 done" << endl; - cin >> wait; - - f.kLog(sizeof(MSGXL), MSGXL); - f.kLog(sizeof(MSGL2), MSGL2); - f.kflush(); - cout << "test 16 done" << endl; - cin >> wait; -} \ No newline at end of file diff --git a/blaze-lite/core/test/spiUnittests.cpp b/blaze-lite/core/test/spiUnittests.cpp index 6c7e9cc..db6d9ba 100644 --- a/blaze-lite/core/test/spiUnittests.cpp +++ b/blaze-lite/core/test/spiUnittests.cpp @@ -2,3 +2,136 @@ This file contains the unit tests for the spiFlash class. */ #include "spiFlash.h" + +using std::cout, std::endl, std::cin; +/* +tests: +1. write +2. kwrite + +3. buffer - 1 small msg +4. flush +5. kLog - 1 small msg +6. kflush + +7. buffer - 2 small msgs +8. kLog - 2 small msgs + +9. buffer - 1 large msg +10. kLog - 1 large msg + +11. buffer - 1 xl msg +12. kLog - 1 xl msg + +13. buffer - 2 large msgs +14. kLog - 2 large msgs + +15. buffer - 1 xl msg -> 1 l msg +16. kLog - 1 xl msg -> 1 l msg + +17. queue +18. tick +*/ + +#define GREETING "Hello World!\n" +#define MSG1 "This is the first msg.\n" +#define MSG2 "This is the second msg.\n" +#define MSGL msgl +#define MSGL2 msgl2 +#define MSGXL msgxl + +#define BUFF_SIZE 256 + +int main () { + char wait; + char msgl[BUFF_SIZE]; + char msgl2[BUFF_SIZE]; + char msgxl[BUFF_SIZE + 8]; + + memset(msgl, '1', sizeof(msgl)); + memset(msgl2, '2', sizeof(msgl2)); + memset(msgxl, 'X', sizeof(msgxl)); + + spiFlash f(0, BUFF_SIZE, BUFF_SIZE); + + + f.write(sizeof(GREETING), GREETING); + cout << "test 1 done" << endl; + cin >> wait; + + f.kwrite(sizeof(GREETING), GREETING); + cout << "test 2 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSG1), MSG1); + cout << "test 3 done" << endl; + cin >> wait; + + f.flush(); + cout << "test 4 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSG1), MSG1); + cout << "test 5 done" << endl; + cin >> wait; + + f.kflush(); + cout << "test 6 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSG1), MSG1); + f.buffer(sizeof(MSG2), MSG2); + f.flush(); + cout << "test 7 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSG1), MSG1); + f.kLog(sizeof(MSG2), MSG2); + f.kflush(); + cout << "test 8 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGL), MSGL); + f.flush(); + cout << "test 9 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGL), MSGL); + f.kflush(); + cout << "test 10 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGXL), MSGXL); + f.flush(); + cout << "test 11 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGXL), MSGXL); + f.kflush(); + cout << "test 12 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGL), MSGL); + f.buffer(sizeof(MSGL2), MSGL2); + f.flush(); + cout << "test 13 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGL), MSGL); + f.kLog(sizeof(MSGL2), MSGL2); + f.kflush(); + cout << "test 14 done" << endl; + cin >> wait; + + f.buffer(sizeof(MSGXL), MSGXL); + f.buffer(sizeof(MSGL2), MSGL2); + f.flush(); + cout << "test 15 done" << endl; + cin >> wait; + + f.kLog(sizeof(MSGXL), MSGXL); + f.kLog(sizeof(MSGL2), MSGL2); + f.kflush(); + cout << "test 16 done" << endl; + cin >> wait; +} \ No newline at end of file From d4ee06e996019dcecf420ed02630c84f2a9e2e28 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 5 Jan 2026 15:54:54 -0800 Subject: [PATCH 11/23] Update spiFlash.h --- blaze-lite/core/lib/spi_flash/spiFlash.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 989df13..fbc0a95 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -1,11 +1,11 @@ #ifndef SPI_FLASH_H #define SPI_FLASH_H -// #include -// #include -// #include +#include +#include +#include -// #include +#include #include #include @@ -26,6 +26,9 @@ class spiFlash { //destructor: ~spiFlash() ; + //setup function: + void startUp(); + //Get Methods: char getCS_PIN(); @@ -49,8 +52,6 @@ class spiFlash { char kflush (void); - ssize_t tick(void) ; - //types struct cmp_io_priority { bool operator()(const std::tuple& l, const std::tuple& r) const ; From cd98ae99cd6f67e6583a66417bbbb97c6e971cea Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Mon, 12 Jan 2026 21:57:18 +0000 Subject: [PATCH 12/23] added actual hardare interaction (v1) -Prateek --- blaze-lite/core/lib/spi_flash/flash_config.h | 82 ++++++++++++++++++++ blaze-lite/core/lib/spi_flash/spiFlash.cpp | 49 ++++++++++-- blaze-lite/core/lib/spi_flash/spiFlash.h | 2 + 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 blaze-lite/core/lib/spi_flash/flash_config.h diff --git a/blaze-lite/core/lib/spi_flash/flash_config.h b/blaze-lite/core/lib/spi_flash/flash_config.h new file mode 100644 index 0000000..672d8b5 --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/flash_config.h @@ -0,0 +1,82 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022 Ha Thach (tinyusb.org) for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef FLASH_CONFIG_H_ +#define FLASH_CONFIG_H_ + +// Un-comment to run example with custom SPI and SS e.g with FRAM breakout +// #define CUSTOM_CS A5 +// #define CUSTOM_SPI SPI + +#if defined(CUSTOM_CS) && defined(CUSTOM_SPI) +Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI); + +#elif defined(ARDUINO_ARCH_ESP32) +// ESP32 use same flash device that store code for file system. +// SPIFlash will parse partition.cvs to detect FATFS partition to use +Adafruit_FlashTransport_ESP32 flashTransport; + +#elif defined(ARDUINO_ARCH_RP2040) +// RP2040 use same flash device that store code for file system. Therefore we +// only need to specify start address and size (no need SPI or SS) +// By default (start=0, size=0), values that match file system setting in +// 'Tools->Flash Size' menu selection will be used. +Adafruit_FlashTransport_RP2040 flashTransport; + +// To be compatible with CircuitPython partition scheme (start_address = 1 MB, +// size = total flash - 1 MB) use const value (CPY_START_ADDR, CPY_SIZE) or +// subclass Adafruit_FlashTransport_RP2040_CPY. Un-comment either of the +// following line: +// Adafruit_FlashTransport_RP2040 +// flashTransport(Adafruit_FlashTransport_RP2040::CPY_START_ADDR, +// Adafruit_FlashTransport_RP2040::CPY_SIZE); +// Adafruit_FlashTransport_RP2040_CPY flashTransport; +#else + +// On-board external flash (QSPI or SPI) macros should already +// defined in your board variant if supported +// - EXTERNAL_FLASH_USE_QSPI +// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI + +#if defined(EXTERNAL_FLASH_USE_QSPI) +Adafruit_FlashTransport_QSPI flashTransport; + +#elif defined(EXTERNAL_FLASH_USE_SPI) +Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, + EXTERNAL_FLASH_USE_SPI); + +#elif defined(__AVR__) || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) + +// Circuit Playground Express built with Arduino SAMD instead of Adafruit SAMD +// core or AVR core Use stand SPI/SS for avr port. Note: For AVR, cache will be +// disable due to lack of memory. +Adafruit_FlashTransport_SPI flashTransport(SS, SPI); + +#else +#error No (Q)SPI flash are defined for your board ! +#endif + +#endif + +#endif \ No newline at end of file diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index 30ef6fe..328b952 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -20,8 +20,9 @@ Adafruit_SPIFlash flash(&flashTransport); FatVolume fatfs; -File32 root; -File32 file; +// File32 root; +// File32 file; +File32 fd, kfd; //TODO: Make private methods to simplify code @@ -54,8 +55,25 @@ spiFlash::~spiFlash () { } //hardware setup function (this should be called in the main file setup loop): +//https://github.com/adafruit/Adafruit_SPIFlash/blob/master/examples/SdFat_datalogging/SdFat_datalogging.ino void spiFlash::startUp() { - + if (!flash.begin()) { + Serial.println("Error, failed to initialize flash chip!"); + while (1) { + delay(1); + } + } + Serial.print("Flash chip JEDEC ID: 0x"); + Serial.println(flash.getJEDECID(), HEX); + + // First call begin to mount the filesystem. Check that it returns true + // to make sure the filesystem was mounted. + if (!fatfs.begin(&flash)) { + Serial.println("Error, failed to mount newly formatted filesystem!"); + Serial.println("Was the flash chip formatted with the fatfs_format example?"); + while (1) delay(1); + } + Serial.println("Mounted filesystem!"); } //Get Methods: @@ -107,11 +125,19 @@ char spiFlash::buffer (const size_t bytes, const char* data) { //return value < 0 means error ssize_t spiFlash::write (const size_t bytes, const char* data) { - return ::write(fd, data, bytes); + #ifdef NOBOARD_TEST + return ::write(fd, data, bytes); + #else + return fd.write(data, bytes); + #endif } ssize_t spiFlash::kwrite (const size_t bytes, const char* data) { - return ::write(kfd, data, bytes); + #ifdef NOBOARD_TEST + return ::write(kfd, data, bytes); + #else + return kfd.write(data, bytes); + #endif } char spiFlash::flush (void) { @@ -154,6 +180,19 @@ char spiFlash::kflush (void) { return err; } +//TODO: implement error tracking +ssize_t spiFlash::tick (void) { + if(queuedos.empty()) return; + + bool isMandatory = std::get<0>(queuedos.top()) == spiFlash::P_MANDATORY; + ssize_t numBytes = this->buffer(std::get<1>(queuedos.top()), std::get<2>(queuedos.top())); + queuedos.pop() + + for(; std::get<0>(queuedos.top()) == spiFlash::P_MANDATORY; queuedos.pop()) numBytes += this->buffer(std::get<1>(queuedos.top()), std::get<2>(queuedos.top())); + + if(isMandatory) this->flush(); +} + bool spiFlash::cmp_io_priority:: operator()(const std::tuple& l, const std::tuple& r) const { return std::get<0>(l) > std::get<0>(r); } diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index fbc0a95..97385c0 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -52,6 +52,8 @@ class spiFlash { char kflush (void); + ssize_t tick (void); + //types struct cmp_io_priority { bool operator()(const std::tuple& l, const std::tuple& r) const ; From 08111a5de0e29b453e22428f23d15410a803187d Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 12 Jan 2026 17:19:31 -0800 Subject: [PATCH 13/23] link fail --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 36 ++--- blaze-lite/core/lib/spi_flash/spiFlash.h | 32 ++-- blaze-lite/core/platformio.ini | 4 +- blaze-lite/core/src/main.cpp | 180 +++++++++++++++++++-- 4 files changed, 203 insertions(+), 49 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index 328b952..aa5d0b0 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -16,7 +16,10 @@ #include #include -Adafruit_SPIFlash flash(&flashTransport); + +const uint8_t CS_PIN = PB8; +Adafruit_FlashTransport_SPI flash_Transport(CS_PIN, SPI); +Adafruit_SPIFlash flash_(&flash_Transport); FatVolume fatfs; @@ -26,17 +29,9 @@ File32 fd, kfd; //TODO: Make private methods to simplify code -constexpr const char - spiFlash::P_MANDATORY = 0, //just force writes this at next tick - spiFlash::P_URGENT = 1, - spiFlash::P_IMPORTANT = 2, - spiFlash::P_STD = 3, - spiFlash::P_UNIMPORTANT = 4, - spiFlash::P_OPTIONAL = 5 -; - //Constructor: -spiFlash::spiFlash (const char cs_pin, const size_t buffer_size, const size_t k_buffer_size) : CS_PIN(cs_pin), buffer_size(buffer_size), k_buffer_size(k_buffer_size), buffer_offset(0), k_buffer_offset(0) { +spiFlash::spiFlash (const uint8_t cs_pin, const size_t buffer_size, const size_t k_buffer_size) + : CS_PIN(cs_pin), buffer_size(buffer_size), k_buffer_size(k_buffer_size), buffer_offset(0), k_buffer_offset(0) { obuff = new char[ buffer_size]; kbuff = new char[k_buffer_size]; queuedos = std::priority_queue, std::vector>, cmp_io_priority>(); @@ -57,30 +52,30 @@ spiFlash::~spiFlash () { //hardware setup function (this should be called in the main file setup loop): //https://github.com/adafruit/Adafruit_SPIFlash/blob/master/examples/SdFat_datalogging/SdFat_datalogging.ino void spiFlash::startUp() { - if (!flash.begin()) { - Serial.println("Error, failed to initialize flash chip!"); + if (!flash_.begin()) { + Serial.println("Error, failed to initialize flash_ chip!"); while (1) { delay(1); } } Serial.print("Flash chip JEDEC ID: 0x"); - Serial.println(flash.getJEDECID(), HEX); + Serial.println(flash_.getJEDECID(), HEX); // First call begin to mount the filesystem. Check that it returns true // to make sure the filesystem was mounted. - if (!fatfs.begin(&flash)) { + if (!fatfs.begin(&flash_)) { Serial.println("Error, failed to mount newly formatted filesystem!"); - Serial.println("Was the flash chip formatted with the fatfs_format example?"); + Serial.println("Was the flash_ chip formatted with the fatfs_format example?"); while (1) delay(1); } Serial.println("Mounted filesystem!"); } //Get Methods: -char spiFlash::getCS_PIN() { return CS_PIN; } +uint8_t spiFlash::getCS_PIN() { return CS_PIN; } //Set Methods: -void spiFlash::setCS_PIN(const char pin) { CS_PIN = pin; } +void spiFlash::setCS_PIN(const uint8_t pin) { CS_PIN = pin; } //functionality methods: ssize_t spiFlash::read(const size_t offset, const size_t bytes, char* buffer){ @@ -182,15 +177,16 @@ char spiFlash::kflush (void) { //TODO: implement error tracking ssize_t spiFlash::tick (void) { - if(queuedos.empty()) return; + if(queuedos.empty()) return 0; bool isMandatory = std::get<0>(queuedos.top()) == spiFlash::P_MANDATORY; ssize_t numBytes = this->buffer(std::get<1>(queuedos.top()), std::get<2>(queuedos.top())); - queuedos.pop() + queuedos.pop(); for(; std::get<0>(queuedos.top()) == spiFlash::P_MANDATORY; queuedos.pop()) numBytes += this->buffer(std::get<1>(queuedos.top()), std::get<2>(queuedos.top())); if(isMandatory) this->flush(); + return numBytes; } bool spiFlash::cmp_io_priority:: operator()(const std::tuple& l, const std::tuple& r) const { diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 97385c0..c4966b9 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -20,8 +20,17 @@ //TODO: Documentation, Order methods in aphabetical class spiFlash { public: + + static constexpr const char + P_MANDATORY = 0, //just force writes this at next tick + P_URGENT = 1, + P_IMPORTANT = 2, + P_STD = 3, + P_UNIMPORTANT = 4, + P_OPTIONAL = 5 + ; //Constructor: - spiFlash (const char cs_pin, const size_t buffer_size, const size_t k_buffer_size) ; + spiFlash (const uint8_t cs_pin, const size_t buffer_size, const size_t k_buffer_size) ; //destructor: ~spiFlash() ; @@ -30,10 +39,10 @@ class spiFlash { void startUp(); //Get Methods: - char getCS_PIN(); + uint8_t getCS_PIN(); //Set Methods: - void setCS_PIN(char pin); + void setCS_PIN(uint8_t pin); //functionality methods: ssize_t read(const size_t offset, const size_t bytes, char* buffer); @@ -59,20 +68,11 @@ class spiFlash { bool operator()(const std::tuple& l, const std::tuple& r) const ; }; - static constexpr const char - P_MANDATORY = 0, //just force writes this at next tick - P_URGENT = 1, - P_IMPORTANT = 2, - P_STD = 3, - P_UNIMPORTANT = 4, - P_OPTIONAL = 5 - ; - const size_t buffer_size; const size_t k_buffer_size; private: - char CS_PIN; + uint8_t CS_PIN; std::priority_queue< // priority size data @@ -86,8 +86,10 @@ class spiFlash { char* kbuff; size_t k_buffer_offset; - - int fd, kfd; + + #ifdef NOBOARD_TEST + int fd, kfd; + #endif }; #endif \ No newline at end of file diff --git a/blaze-lite/core/platformio.ini b/blaze-lite/core/platformio.ini index 3dd57ca..bff5e11 100644 --- a/blaze-lite/core/platformio.ini +++ b/blaze-lite/core/platformio.ini @@ -16,4 +16,6 @@ upload_protocol = dfu build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC -D USBCON -lib_deps = adafruit/Adafruit SPIFlash@^5.1.1 +lib_deps = + adafruit/SdFat - Adafruit Fork@^2.3.102 + adafruit/Adafruit SPIFlash@^5.1.1 diff --git a/blaze-lite/core/src/main.cpp b/blaze-lite/core/src/main.cpp index 026073e..33c0ccc 100644 --- a/blaze-lite/core/src/main.cpp +++ b/blaze-lite/core/src/main.cpp @@ -1,21 +1,175 @@ +/* + * SPI Flash Test for Blackpill STM32F411CE + * Tests the spiFlash library features with Adafruit SPI Flash chip + * Chip Select Pin: PB8 + */ + #include -#include -#include +#include "spiFlash.h" + +// Configuration +#define CS_PIN PB8 +#define BUFFER_SIZE 512 +#define K_BUFFER_SIZE 256 +#define BAUD_RATE 115200 + +// Create spiFlash object +spiFlash flashStorage(CS_PIN, BUFFER_SIZE, K_BUFFER_SIZE); -const long baudrate = 115200; +// Test data +const char testMessage[] = "Hello from Blaze SPI Flash Test!"; +const char testData[] = "This is a longer test message to verify buffering and write operations. Testing 1, 2, 3..."; +char readBuffer[256]; -void setup() -{ - Serial.begin(baudrate); +void setup() { + // Initialize Serial for debugging + SPI.begin(); + Serial.begin(BAUD_RATE); while (!Serial) { - delay(10); + delay(10); // Wait for serial connection } - Serial.println("This is on the BlackPill board"); -} + + delay(2000); // Give time to open serial monitor + + Serial.println("\n=== Blaze SPI Flash Test ==="); + Serial.println("Board: Blackpill STM32F411CE"); + Serial.print("Chip Select Pin: "); + Serial.println(CS_PIN); + Serial.println("============================\n"); + + // Initialize SPI Flash + Serial.println("1. Initializing SPI Flash..."); + flashStorage.startUp(); + Serial.println(" [OK] Flash initialized successfully\n"); + // Test 1: Verify CS pin configuration + Serial.println("2. Testing CS Pin Configuration..."); + Serial.print(" Current CS Pin: "); + Serial.println(flashStorage.getCS_PIN()); + Serial.println(" [OK] CS Pin verified\n"); + + // Test 2: Write test with buffer + Serial.println("3. Testing Buffered Write..."); + Serial.print(" Writing: "); + Serial.println(testMessage); + + ssize_t result = flashStorage.buffer(strlen(testMessage), testMessage); + if (result >= 0) { + Serial.println(" [OK] Data buffered successfully"); + } else { + Serial.print(" [ERROR] Buffer failed with code: "); + Serial.println(result); + } + + // Flush buffer to flash + Serial.println(" Flushing buffer to flash..."); + char flushResult = flashStorage.flush(); + if (flushResult == 0) { + Serial.println(" [OK] Buffer flushed successfully\n"); + } else { + Serial.print(" [ERROR] Flush failed with code: "); + Serial.println(flushResult); + } + + // Test 3: Read back data + Serial.println("4. Testing Read Operation..."); + memset(readBuffer, 0, sizeof(readBuffer)); + ssize_t bytesRead = flashStorage.read(0, strlen(testMessage), readBuffer); + + if (bytesRead > 0) { + Serial.print(" Bytes read: "); + Serial.println(bytesRead); + Serial.print(" Data read: "); + Serial.println(readBuffer); + + if (strcmp(readBuffer, testMessage) == 0) { + Serial.println(" [OK] Data verification successful!\n"); + } else { + Serial.println(" [WARNING] Data mismatch!\n"); + } + } else { + Serial.print(" [ERROR] Read failed with code: "); + Serial.println(bytesRead); + } + // Test 4: Queue system test + Serial.println("5. Testing Priority Queue System..."); + + const char urgentMsg[] = "URGENT"; + const char stdMsg[] = "Standard"; + const char optionalMsg[] = "Optional"; + + // Queue messages with different priorities + flashStorage.queue(strlen(optionalMsg), (char*)optionalMsg, flashStorage.P_OPTIONAL); + Serial.println(" Queued OPTIONAL message"); + + flashStorage.queue(strlen(urgentMsg), (char*)urgentMsg, flashStorage.P_URGENT); + Serial.println(" Queued URGENT message"); + + flashStorage.queue(strlen(stdMsg), (char*)stdMsg, flashStorage.P_STD); + Serial.println(" Queued STANDARD message"); + + Serial.println(" [OK] Messages queued (will be processed in priority order)\n"); -void loop() -{ - Serial.println("This is on the BlackPill board"); -} \ No newline at end of file + // Test 5: Tick operation (process queue) + Serial.println("6. Testing Tick Operation..."); + flashStorage.tick(); + Serial.println(" [OK] Tick executed - highest priority item processed\n"); + + // Test 6: K-log test (kernel log buffer) + Serial.println("7. Testing K-Log Buffer..."); + const char klogMsg[] = "Kernel log entry"; + ssize_t klogResult = flashStorage.kLog(strlen(klogMsg), klogMsg); + + if (klogResult >= 0) { + Serial.println(" [OK] K-log buffer written"); + Serial.println(" Flushing k-log buffer..."); + flashStorage.kflush(); + Serial.println(" [OK] K-log flushed\n"); + } else { + Serial.print(" [ERROR] K-log failed with code: "); + Serial.println(klogResult); + } + + // Test 7: Large data write test + Serial.println("8. Testing Large Data Write..."); + result = flashStorage.buffer(strlen(testData), testData); + if (result >= 0) { + Serial.print(" [OK] Buffered "); + Serial.print(strlen(testData)); + Serial.println(" bytes"); + flashStorage.flush(); + Serial.println(" [OK] Large data flushed\n"); + } else { + Serial.print(" [ERROR] Large buffer failed: "); + Serial.println(result); + } + + Serial.println("\n=== All Tests Complete ==="); + Serial.println("System ready for operation.\n"); +} + +void loop() { + // Continuous operation test + static unsigned long lastWrite = 0; + static int counter = 0; + + // Write a counter value every 5 seconds + if (millis() - lastWrite > 5000) { + lastWrite = millis(); + + char logEntry[64]; + snprintf(logEntry, sizeof(logEntry), "Loop counter: %d, Uptime: %lu ms", counter++, millis()); + + Serial.print("Writing: "); + Serial.println(logEntry); + + flashStorage.buffer(strlen(logEntry), logEntry); + flashStorage.flush(); + + Serial.println("[OK] Log entry written\n"); + } + + // Small delay + delay(100); +} From d91ffe696944ba51445cc3036ed176f2d676e6ad Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Sun, 8 Feb 2026 18:35:19 -0800 Subject: [PATCH 14/23] in spiFlash.cpp: Modify spiFlash::startUp to return boolean status -- Prateek Change spiFlash.cpp startUp function to return a boolean indicating success or failure. --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index aa5d0b0..82f15b1 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -51,12 +51,13 @@ spiFlash::~spiFlash () { //hardware setup function (this should be called in the main file setup loop): //https://github.com/adafruit/Adafruit_SPIFlash/blob/master/examples/SdFat_datalogging/SdFat_datalogging.ino -void spiFlash::startUp() { +bool spiFlash::startUp() { if (!flash_.begin()) { Serial.println("Error, failed to initialize flash_ chip!"); - while (1) { - delay(1); - } + // while (1) { + // delay(1); + // } + return false; } Serial.print("Flash chip JEDEC ID: 0x"); Serial.println(flash_.getJEDECID(), HEX); @@ -66,9 +67,11 @@ void spiFlash::startUp() { if (!fatfs.begin(&flash_)) { Serial.println("Error, failed to mount newly formatted filesystem!"); Serial.println("Was the flash_ chip formatted with the fatfs_format example?"); - while (1) delay(1); + // while (1) delay(1); + return false; } Serial.println("Mounted filesystem!"); + return true; } //Get Methods: From 76c630b508a0b6937cfdd7e9b6ccc68123b9d544 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Sun, 8 Feb 2026 18:35:50 -0800 Subject: [PATCH 15/23] Change startUp method return type to bool -- Prateek --- blaze-lite/core/lib/spi_flash/spiFlash.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index c4966b9..678985b 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -36,7 +36,7 @@ class spiFlash { ~spiFlash() ; //setup function: - void startUp(); + bool startUp(); //Get Methods: uint8_t getCS_PIN(); @@ -92,4 +92,4 @@ class spiFlash { #endif }; -#endif \ No newline at end of file +#endif From 77db70db637cc92e390f8e6602a17088946a3bef Mon Sep 17 00:00:00 2001 From: fkhabur Date: Sun, 8 Feb 2026 18:52:05 -0800 Subject: [PATCH 16/23] made splitflash.h be alphbetical, and spliflashtester also made --- .vscode/c_cpp_properties.json | 20 ++++ blaze-lite/core/lib/spi_flash/spiFlash.h | 19 ++-- .../core/lib/spi_flash/spliFlashTester.cpp | 91 +++++++++++++++++++ 3 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 blaze-lite/core/lib/spi_flash/spliFlashTester.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..748ab32 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "macFrameworkPath": [ + "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks", + "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/SubFrameworks" + ], + "compilerPath": "/usr/bin/g++", + "cStandard": "c17", + "cppStandard": "c++14", + "intelliSenseMode": "macos-clang-arm64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 678985b..19bdbc3 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -17,7 +17,7 @@ #include #include -//TODO: Documentation, Order methods in aphabetical +//TODO: Documentation class spiFlash { public: @@ -33,7 +33,7 @@ class spiFlash { spiFlash (const uint8_t cs_pin, const size_t buffer_size, const size_t k_buffer_size) ; //destructor: - ~spiFlash() ; + ~spiFlash(); //setup function: bool startUp(); @@ -45,24 +45,25 @@ class spiFlash { void setCS_PIN(uint8_t pin); //functionality methods: - ssize_t read(const size_t offset, const size_t bytes, char* buffer); - - char queue(size_t bytes, char* data, char priority = P_UNIMPORTANT); char buffer (const size_t bytes, const char* data); - ssize_t write (const size_t bytes, const char* data); - - ssize_t kwrite (const size_t bytes, const char* data); - char flush (void); ssize_t kLog (const size_t bytes, const char* data); char kflush (void); + ssize_t kwrite (const size_t bytes, const char* data); + + char queue (size_t bytes, char* data, char priority = P_UNIMPORTANT); + + ssize_t read (const size_t offset, const size_t bytes, char* buffer); + ssize_t tick (void); + ssize_t write (const size_t bytes, const char* data); + //types struct cmp_io_priority { bool operator()(const std::tuple& l, const std::tuple& r) const ; diff --git a/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp b/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp new file mode 100644 index 0000000..f99174d --- /dev/null +++ b/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp @@ -0,0 +1,91 @@ +#include "spiFlash.h" +#include "flash_config.h" +#include + +class spliFlashTester{ +private: + const uint8_t CS_PIN = PB8; // got this from the spiFlash.cpp file lol + void printResult(const char* testName, bool passed) { + Serial.print(testName); + Serial.print(": "); + Serial.println(passed ? "PASS" : "FAIL"); + } + + void testStartUp(){ + spiFlash spiFlash(CS_PIN, 256, 128); + bool ok = spiFlash.startUp(); + printResult("StartUp test: ", ok); + } + + void testReadWrite(){ + spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash.startUp(); + + const char msg = "HELLO ROCKET TEAM!!"; + char readback[sizeof(msg)] = {0}; // open memory for our data + + ssize_t written = spiFlash.write(sizeof(msg), msg); // save what the flash interprets from our message + ssize_t read = spiFlash.read(0, sizeof(msg), readback); // read the data into space we opened for it + + bool test = ((written == sizeof(msg)) && (read == sizeof(msg)) && !std::memcmp(written, read, sizeof(msg))); + printResult("Basic Read Write test: ", ok); + } + + void testBufferFlush() { + spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash.startUp(); + + const char msg[] = "BUFFERED"; + char out[sizeof(msg)] = {0}; + + spiFlash.buffer(sizeof(msg), msg); + spiFlash.flush(); + spiFlash.read(0, sizeof(msg), out); + + bool ok = !std::memcmp(msg, out, sizeof(msg)); + printResult("Buffer flush: ", ok); + } + + void testQueuePriority() { + spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash.startUp(); + + char low[] = "LOW"; + char high[] = "HIGH"; + + spiFlash.queue(sizeof(low), low, spiFlash::P_UNIMPORTANT); + spiFlash.queue(sizeof(high), high, spiFlash::P_URGENT); + + while (spiFlash.tick() > 0) {} + + char out1[5] = {0}; + char out2[4] = {0}; + + spiFlash.read(0, sizeof(high), out1); + spiFlash.read(sizeof(high), sizeof(low), out2); + + bool ok = (std::strcmp(out1, "HIGH") == 0 && std::strcmp(out2, "LOW") == 0); + printResult("Queue priority: ", ok); + } + + +public: + void run() { + Serial.println("SPI Flash Tester: start"); + testStartup(); + testReadWrite(); + testBufferFlush(); + testQueuePriority(); + Serial.println("SPI Flash Tester: done"); + } + ~spliFlashTester(); + +}; + +spliFlashTester::spliFlashTester(/* args */) +{ +} + +spliFlashTester::~spliFlashTester() +{ +} From de359814fde73e84b25e043c946d8f9e80018602 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:43:48 -0800 Subject: [PATCH 17/23] Modify spiFlash constructor, remove spiflash::setCS_PIN Updated spiFlash constructor to use default parameters for buffer sizes. --- blaze-lite/core/lib/spi_flash/spiFlash.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.h b/blaze-lite/core/lib/spi_flash/spiFlash.h index 19bdbc3..f775146 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.h +++ b/blaze-lite/core/lib/spi_flash/spiFlash.h @@ -30,7 +30,7 @@ class spiFlash { P_OPTIONAL = 5 ; //Constructor: - spiFlash (const uint8_t cs_pin, const size_t buffer_size, const size_t k_buffer_size) ; + spiFlash (const size_t buffer_size = 512, const size_t k_buffer_size = 512) ; //destructor: ~spiFlash(); @@ -40,10 +40,7 @@ class spiFlash { //Get Methods: uint8_t getCS_PIN(); - - //Set Methods: - void setCS_PIN(uint8_t pin); - + //functionality methods: char buffer (const size_t bytes, const char* data); From 8f62f9651f357504aa026061e478534d7dc44bd9 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:45:28 -0800 Subject: [PATCH 18/23] Refactor spiFlash class constructor and methods Refactor spiFlash constructor and methods for clarity and not using cin/cout when not applicable. Remove unused CS_PIN parameter and commented-out error handling. --Prateek --- blaze-lite/core/lib/spi_flash/spiFlash.cpp | 30 ++++++++++++---------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spiFlash.cpp b/blaze-lite/core/lib/spi_flash/spiFlash.cpp index 82f15b1..82c4491 100644 --- a/blaze-lite/core/lib/spi_flash/spiFlash.cpp +++ b/blaze-lite/core/lib/spi_flash/spiFlash.cpp @@ -30,8 +30,8 @@ File32 fd, kfd; //TODO: Make private methods to simplify code //Constructor: -spiFlash::spiFlash (const uint8_t cs_pin, const size_t buffer_size, const size_t k_buffer_size) - : CS_PIN(cs_pin), buffer_size(buffer_size), k_buffer_size(k_buffer_size), buffer_offset(0), k_buffer_offset(0) { +spiFlash::spiFlash (const size_t buffer_size, const size_t k_buffer_size) + : buffer_size(buffer_size), k_buffer_size(k_buffer_size), buffer_offset(0), k_buffer_offset(0) { obuff = new char[ buffer_size]; kbuff = new char[k_buffer_size]; queuedos = std::priority_queue, std::vector>, cmp_io_priority>(); @@ -41,8 +41,10 @@ spiFlash::~spiFlash () { kflush(); flush(); - close(fd); - close(kfd); + #ifdef NOBOARD_TEST + ::close(fd); + ::close(kfd); + #endif delete[] obuff; delete[] kbuff; @@ -77,19 +79,19 @@ bool spiFlash::startUp() { //Get Methods: uint8_t spiFlash::getCS_PIN() { return CS_PIN; } -//Set Methods: -void spiFlash::setCS_PIN(const uint8_t pin) { CS_PIN = pin; } +// //Set Methods: +// void spiFlash::setCS_PIN(const uint8_t pin) { CS_PIN = pin; } //functionality methods: ssize_t spiFlash::read(const size_t offset, const size_t bytes, char* buffer){ - //error handling - if (bytes == 0) return 0; //no bytes to read - if (buffer == nullptr) return -1; //null data pointer - if (lseek(fd, offset, SEEK_SET) == -1 ) return -2; //seek error + // //error handling + // if (bytes == 0) return 0; //no bytes to read + // if (buffer == nullptr) return -1; //null data pointer + // if (lseek(fd, offset, SEEK_SET) == -1 ) return -2; //seek error - ssize_t bytes_read = ::read(fd, buffer, bytes); + // ssize_t bytes_read = ::read(fd, buffer, bytes); - return bytes_read; + // return bytes_read; } char spiFlash::queue(size_t bytes, char* data, char priority) { @@ -109,7 +111,7 @@ char spiFlash::buffer (const size_t bytes, const char* data) { memcpy(obuff + buffer_offset, data, temp = std::min(bytes, buffer_size - buffer_offset)); buffer_offset += (offset += temp); - while (bytes - offset > 0) { + while ((bytes - offset > 0) && (err == 0)) { if (err = flush() < 0) return err; flush(); @@ -139,7 +141,7 @@ ssize_t spiFlash::kwrite (const size_t bytes, const char* data) { } char spiFlash::flush (void) { - ssize_t err = write (buffer_offset, obuff); + ssize_t err = write(buffer_offset, obuff); if (err < 0) return err; memset(obuff, 0, buffer_size); From 38d44212ca76e04b4025ce9c3056650d731b5cf9 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:46:12 -0800 Subject: [PATCH 19/23] Add NOBOARD_TEST definition for stdio unit tests --- blaze-lite/core/test/spiUnittests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blaze-lite/core/test/spiUnittests.cpp b/blaze-lite/core/test/spiUnittests.cpp index db6d9ba..d0431a8 100644 --- a/blaze-lite/core/test/spiUnittests.cpp +++ b/blaze-lite/core/test/spiUnittests.cpp @@ -1,6 +1,7 @@ /* This file contains the unit tests for the spiFlash class. */ +#define NOBOARD_TEST #include "spiFlash.h" using std::cout, std::endl, std::cin; @@ -134,4 +135,4 @@ int main () { f.kflush(); cout << "test 16 done" << endl; cin >> wait; -} \ No newline at end of file +} From bb2b5ca988181cbc0a14695dd0258dffd827e305 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:46:29 -0800 Subject: [PATCH 20/23] Delete blaze-lite/core/lib/spi_flash/flash_config.h --- blaze-lite/core/lib/spi_flash/flash_config.h | 82 -------------------- 1 file changed, 82 deletions(-) delete mode 100644 blaze-lite/core/lib/spi_flash/flash_config.h diff --git a/blaze-lite/core/lib/spi_flash/flash_config.h b/blaze-lite/core/lib/spi_flash/flash_config.h deleted file mode 100644 index 672d8b5..0000000 --- a/blaze-lite/core/lib/spi_flash/flash_config.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2022 Ha Thach (tinyusb.org) for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef FLASH_CONFIG_H_ -#define FLASH_CONFIG_H_ - -// Un-comment to run example with custom SPI and SS e.g with FRAM breakout -// #define CUSTOM_CS A5 -// #define CUSTOM_SPI SPI - -#if defined(CUSTOM_CS) && defined(CUSTOM_SPI) -Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI); - -#elif defined(ARDUINO_ARCH_ESP32) -// ESP32 use same flash device that store code for file system. -// SPIFlash will parse partition.cvs to detect FATFS partition to use -Adafruit_FlashTransport_ESP32 flashTransport; - -#elif defined(ARDUINO_ARCH_RP2040) -// RP2040 use same flash device that store code for file system. Therefore we -// only need to specify start address and size (no need SPI or SS) -// By default (start=0, size=0), values that match file system setting in -// 'Tools->Flash Size' menu selection will be used. -Adafruit_FlashTransport_RP2040 flashTransport; - -// To be compatible with CircuitPython partition scheme (start_address = 1 MB, -// size = total flash - 1 MB) use const value (CPY_START_ADDR, CPY_SIZE) or -// subclass Adafruit_FlashTransport_RP2040_CPY. Un-comment either of the -// following line: -// Adafruit_FlashTransport_RP2040 -// flashTransport(Adafruit_FlashTransport_RP2040::CPY_START_ADDR, -// Adafruit_FlashTransport_RP2040::CPY_SIZE); -// Adafruit_FlashTransport_RP2040_CPY flashTransport; -#else - -// On-board external flash (QSPI or SPI) macros should already -// defined in your board variant if supported -// - EXTERNAL_FLASH_USE_QSPI -// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI - -#if defined(EXTERNAL_FLASH_USE_QSPI) -Adafruit_FlashTransport_QSPI flashTransport; - -#elif defined(EXTERNAL_FLASH_USE_SPI) -Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, - EXTERNAL_FLASH_USE_SPI); - -#elif defined(__AVR__) || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) - -// Circuit Playground Express built with Arduino SAMD instead of Adafruit SAMD -// core or AVR core Use stand SPI/SS for avr port. Note: For AVR, cache will be -// disable due to lack of memory. -Adafruit_FlashTransport_SPI flashTransport(SS, SPI); - -#else -#error No (Q)SPI flash are defined for your board ! -#endif - -#endif - -#endif \ No newline at end of file From 0d17d350b9c402c89bbabd8e4c314bede4635279 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:47:09 -0800 Subject: [PATCH 21/23] Delete .vscode/c_cpp_properties.json --- .vscode/c_cpp_properties.json | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 748ab32..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "configurations": [ - { - "name": "Mac", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [], - "macFrameworkPath": [ - "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks", - "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/SubFrameworks" - ], - "compilerPath": "/usr/bin/g++", - "cStandard": "c17", - "cppStandard": "c++14", - "intelliSenseMode": "macos-clang-arm64" - } - ], - "version": 4 -} \ No newline at end of file From 20c1bcedff59bcc564b1c6dff238af0140e782eb Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:51:38 -0800 Subject: [PATCH 22/23] Refactor spliFlashTester to make constructor/destructor more concise, remove CS_PIN usage and flash_config.h --- .../core/lib/spi_flash/spliFlashTester.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp b/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp index f99174d..72436c6 100644 --- a/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp +++ b/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp @@ -1,10 +1,8 @@ #include "spiFlash.h" -#include "flash_config.h" #include class spliFlashTester{ private: - const uint8_t CS_PIN = PB8; // got this from the spiFlash.cpp file lol void printResult(const char* testName, bool passed) { Serial.print(testName); Serial.print(": "); @@ -12,13 +10,13 @@ class spliFlashTester{ } void testStartUp(){ - spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash spiFlash(256, 128); bool ok = spiFlash.startUp(); printResult("StartUp test: ", ok); } void testReadWrite(){ - spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash spiFlash(256, 128); spiFlash.startUp(); const char msg = "HELLO ROCKET TEAM!!"; @@ -32,7 +30,7 @@ class spliFlashTester{ } void testBufferFlush() { - spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash spiFlash(256, 128); spiFlash.startUp(); const char msg[] = "BUFFERED"; @@ -47,7 +45,7 @@ class spliFlashTester{ } void testQueuePriority() { - spiFlash spiFlash(CS_PIN, 256, 128); + spiFlash spiFlash(256, 128); spiFlash.startUp(); char low[] = "LOW"; @@ -82,10 +80,5 @@ class spliFlashTester{ }; -spliFlashTester::spliFlashTester(/* args */) -{ -} - -spliFlashTester::~spliFlashTester() -{ -} +spliFlashTester:: spliFlashTester() = default; +spliFlashTester::~spliFlashTester() = default; From ed36f30dc686a2e3f7375dac899343615d537e70 Mon Sep 17 00:00:00 2001 From: prateekgupta5 <93631788+prateekgupta5@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:59:09 -0800 Subject: [PATCH 23/23] Rename spliFlashTester to SpiFlashTester, made the file include a test --- .../core/lib/spi_flash/spliFlashTester.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp b/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp index 72436c6..e562b8e 100644 --- a/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp +++ b/blaze-lite/core/lib/spi_flash/spliFlashTester.cpp @@ -1,7 +1,7 @@ #include "spiFlash.h" #include -class spliFlashTester{ +class SpiFlashTester{ private: void printResult(const char* testName, bool passed) { Serial.print(testName); @@ -76,9 +76,18 @@ class spliFlashTester{ testQueuePriority(); Serial.println("SPI Flash Tester: done"); } - ~spliFlashTester(); + ~SpiFlashTester(); }; -spliFlashTester:: spliFlashTester() = default; -spliFlashTester::~spliFlashTester() = default; +SpiFlashTester:: SpiFlashTester() = default; +SpiFlashTester::~SpiFlashTester() = default; + +void setup () { + auto tester = SpiFlashTester(); + tester.run(); +} + +void loop () { + delay(100); //so we dont fry the arduino +}