Skip to content
Snippets Groups Projects
step_cl.cpp 11.42 KiB
/*
osap/drivers/step_cl.cpp

stepper in closed loop mode 

Jake Read at the Center for Bits and Atoms
(c) Massachusetts Institute of Technology 2020

This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and ponyo
projects. Copyright is retained and must be preserved. The work is provided as
is; no warranty is provided, and users accept all liability.
*/

#include "step_cl.h"

Step_CL* Step_CL::instance = 0;

Step_CL* Step_CL::getInstance(void){
    if(instance == 0){
        instance = new Step_CL();
    }
    return instance;
}

Step_CL* step_cl = Step_CL::getInstance();

// https://github.com/cmaglie/FlashStorage
// flash LUT
// FlashStorage(flash_lut, step_cl_calib_table_t);
// float __attribute__((__aligned__(256))) lut[16384];

Step_CL::Step_CL(void){}

#define CALIB_CSCALE 0.4F
#define CALIB_STEP_DELAY 10
#define CALIB_SETTLE_DELAY 1
#define CALIB_SAMPLE_PER_TICK 10 

#define ENCODER_COUNTS 16384

void Step_CL::init(void){
    stepper_hw->init(false, 0.4);
    enc_as5047->init();
    // this lut == stored lut 
    //lut = flash_lut.read();
}

// das lut / flash stuff, 
// s/o to mechaduino code for this 
#define PAGE_SIZE 512 // start_flash_write reports this bit value, use datasheet 25.8.3 to reference 
const float __attribute__((__aligned__(PAGE_SIZE))) lut[ENCODER_COUNTS] = {};

static const unsigned page_size = PAGE_SIZE;    // size of each page in flash mem, read out w/ NVMCTRL->PARAM.bit.PSZ 
static const unsigned floats_per_page = page_size / sizeof(float); // count of floats that can be stored in each page 
static float page[floats_per_page]; // a temporary page-width of floats, to buffer before writing to flash mem 
static unsigned page_f; // the page[page_f - 1] == the last float we wrote 
static const void * page_ptr = (const uint8_t *) lut; // ptr to the address in flash memory where we want to write 
static unsigned num_pages_written; // counting  

static void start_flash_write(void){
    num_pages_written = 0;
    page_f = 0;
    sysError("Writing to flash 0x" + String((uintptr_t) page_ptr, HEX));
    sysError("Page size PSZ= " + String(NVMCTRL->PARAM.bit.PSZ)); // datasheet for this 
    // try with wqw, and auto update? 
    NVMCTRL->CTRLA.bit.WMODE = NVMCTRL_CTRLA_WMODE_MAN;
}

static void flash_disable_cache(void){