From e61c79df34707115e7504f7babd4ac7a7d40683b Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Wed, 14 Oct 2020 16:19:01 -0400
Subject: [PATCH] ll op write

---
 .../src/drivers/step_cl.cpp                   | 113 ++++--------------
 .../src/utils/FlashStorage.cpp                |   8 --
 log/cl-step-control-log.md                    |   2 +
 3 files changed, 27 insertions(+), 96 deletions(-)

diff --git a/firmware/cl-step-controller/src/drivers/step_cl.cpp b/firmware/cl-step-controller/src/drivers/step_cl.cpp
index 873dcaa..1eaccb3 100644
--- a/firmware/cl-step-controller/src/drivers/step_cl.cpp
+++ b/firmware/cl-step-controller/src/drivers/step_cl.cpp
@@ -50,117 +50,54 @@ void Step_CL::init(void){
 // #define BYTES_PER_PAGE 512 
 // #define FLOATS_PER_PAGE 128
 // #define PAGES_PER_BLOCK 16 // for 8192 bytes / block 
-#define LUT_SIZE 2048
 
 //const float __attribute__((__aligned__(BYTES_PER_PAGE))) lut[LUT_SIZE] = {}; 
 //const void* page_ptr; 
-//static float buffer[FLOATS_PER_PAGE];
-//static uint32_t bi;
 
-//FlashClass flashClass((const uint8_t*)lut, BYTES_PER_PAGE); // try w/ constructor values ? 
+#define BYTES_PER_BLOCK 8192
+#define FLOATS_PER_BLOCK 2048
 
-typedef struct {
-    float f[LUT_SIZE];
-} flts;
+const float __attribute__((__aligned__(8192))) lut[16834] = {};
+FlashClass flashClass((const uint8_t*)lut);
 
-typedef struct {
-    float f[ENCODER_COUNTS];
-} flts_14b;
+// write mechanics
+const void* block_ptr;
 
-flts readbuf;
+// write buffer 
+static float buffer[FLOATS_PER_BLOCK];
 
-flts inbuf;
-uint32_t bi = 0;
-uint32_t pi = 0;
+uint32_t bfi = 0; // buffer indice 
+uint32_t bli = 0; // block indice 
 
-FlashStorage(flash_storage_0, flts);
-FlashStorage(flash_storage_1, flts);
-FlashStorage(flash_storage_2, flts);
-FlashStorage(flash_storage_3, flts);
-FlashStorage(flash_storage_4, flts);
-FlashStorage(flash_storage_5, flts);
-FlashStorage(flash_storage_6, flts);
-FlashStorage(flash_storage_7, flts);
+//FlashStorage(flash_storage, flts);
 
 void flash_write_init(void){
-    bi = 0;
-    pi = 0;
+    block_ptr = (const uint8_t*) lut;
+    bfi = 0;
+    bli = 0;
 }
 
 void flash_write_page(void){
+    sysError("erasing");
+    flashClass.erase(block_ptr, BYTES_PER_BLOCK);
     sysError("writing");
-    switch(pi){
-        case 0:
-            flash_storage_0.write(inbuf);
-            sysError("wrote brick 0");
-            break;
-        case 1: 
-            flash_storage_1.write(inbuf);
-            sysError("wrote brick 1");
-            break;
-        case 2:
-            flash_storage_2.write(inbuf);
-            sysError("wrote brick 2");
-            break;
-        case 3:
-            flash_storage_3.write(inbuf);
-            sysError("wrote brick 3");
-            break;
-        case 4:
-            flash_storage_4.write(inbuf);
-            sysError("wrote brick 4");
-            break;
-        case 5:
-            flash_storage_5.write(inbuf);
-            sysError("wrote brick 5");
-            break;
-        case 6:
-            flash_storage_6.write(inbuf);
-            sysError("wrote brick 6");
-            break;
-        case 7:
-            flash_storage_7.write(inbuf);
-            sysError("wrote brick 7");
-            break;
-        default:
-            sysError("oob page write");
-            break;
-    } // end switch 
-    pi ++;
+    flashClass.write(block_ptr, (const uint8_t*)buffer, BYTES_PER_BLOCK);
     delay(100);
 }
 
 void flash_write_value(float val){
-    inbuf.f[bi ++] = val;
-    if(bi >= LUT_SIZE){
+    buffer[bfi ++] = val;
+    if(bfi >= FLOATS_PER_BLOCK){
         flash_write_page();
-        bi = 0;
+        bfi = 0;
     }
 }
 
 void Step_CL::print_table(void){
-    sysError("reading 0");
-    inbuf = flash_storage_0.read();
-    for(uint16_t e = 0; e < LUT_SIZE; e ++){
-        sysError(String(inbuf.f[e]));
-        delay(2);
-    }
-    sysError("reading 1");
-    inbuf = flash_storage_1.read();
-    for(uint16_t e = 0; e < LUT_SIZE; e ++){
-        sysError(String(inbuf.f[e]));
-        delay(2);
-    }
-    sysError("reading 2");
-    inbuf = flash_storage_2.read();
-    for(uint16_t e = 0; e < LUT_SIZE; e ++){
-        sysError(String(inbuf.f[e]));
-        delay(5);
-    }
-    sysError("reading 3");
-    inbuf = flash_storage_3.read();
-    for(uint16_t e = 0; e < LUT_SIZE; e ++){
-        sysError(String(inbuf.f[e]));
+    sysError("reading from lut");
+    for(uint32_t i = 0; i < ENCODER_COUNTS; i ++){
+        float ra = lut[i];
+        sysError(String(ra));
         delay(5);
     }
 }
@@ -168,7 +105,7 @@ void Step_CL::print_table(void){
 // the calib routine 
 boolean Step_CL::calibrate(void){
     flash_write_init();
-    for(uint32_t i = 0; i < ENCODER_COUNTS; i ++){
+    for(uint32_t i = 0; i < FLOATS_PER_BLOCK; i ++){
         flash_write_value(i * 1.1F);
     }
     return true;
diff --git a/firmware/cl-step-controller/src/utils/FlashStorage.cpp b/firmware/cl-step-controller/src/utils/FlashStorage.cpp
index 2e21948..7108db4 100644
--- a/firmware/cl-step-controller/src/utils/FlashStorage.cpp
+++ b/firmware/cl-step-controller/src/utils/FlashStorage.cpp
@@ -47,18 +47,15 @@ static inline uint32_t read_unaligned_uint32(const void *data){
 
 // Invalidate all CMCC cache entries if CMCC cache is enabled.
 static void invalidate_CMCC_cache(){
-    /*
     if (CMCC->SR.bit.CSTS) { // CR -> SR 
         CMCC->CTRL.bit.CEN = 0;
         while (CMCC->SR.bit.CSTS) {}
         CMCC->MAINT0.bit.INVALL = 1;
         CMCC->CTRL.bit.CEN = 1;
     }
-    */
 }
 
 void FlashClass::write(const volatile void *flash_ptr, const void *data, uint32_t size){
-    /*
   // Calculate data boundaries
   size = (size + 3) / 4;
   volatile uint32_t *dst_addr = (volatile uint32_t *)flash_ptr;
@@ -97,11 +94,9 @@ void FlashClass::write(const volatile void *flash_ptr, const void *data, uint32_
     NVMCTRL->CTRLA.bit.CACHEDIS0 = original_CACHEDIS0;
     NVMCTRL->CTRLA.bit.CACHEDIS1 = original_CACHEDIS1;
   }
-  */
 }
 
 void FlashClass::erase(const volatile void *flash_ptr, uint32_t size){
-    /*
     const uint8_t *ptr = (const uint8_t *)flash_ptr;
     while (size > ROW_SIZE) {
         erase(ptr);
@@ -109,17 +104,14 @@ void FlashClass::erase(const volatile void *flash_ptr, uint32_t size){
         size -= ROW_SIZE;
     }
     erase(ptr);
-    */
 }
 
 void FlashClass::erase(const volatile void *flash_ptr){
-    /*
     sysError("erase 0x" + String((uint32_t)flash_ptr));
     NVMCTRL->ADDR.reg = ((uint32_t)flash_ptr);
     NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_CMDEX_KEY | NVMCTRL_CTRLB_CMD_EB;
     while (!NVMCTRL->INTFLAG.bit.DONE) { }
     invalidate_CMCC_cache();
-    */
 }
 
 void FlashClass::read(const volatile void *flash_ptr, void *data, uint32_t size){
diff --git a/log/cl-step-control-log.md b/log/cl-step-control-log.md
index 1c38f59..2985501 100644
--- a/log/cl-step-control-log.md
+++ b/log/cl-step-control-log.md
@@ -897,5 +897,7 @@ Suspect ROW_SIZE something is up,
 
 OK this is crazy, I have every fn in the class commented out, and the thing still locks up. 
 
+So, tried my way around that, I have honestly no idea what is going on here. I'm going to try writing from the base class again. 
+
 - neil suggests watching the power rails, flash req 
 - nuclear option is asking neil to chat w/ microchip contacts 
\ No newline at end of file
-- 
GitLab