Fixed config flash routines, added probe calibration routine

Have only tested one config write so far. Will test others and fix if needed
This commit is contained in:
true 2023-11-01 09:43:14 -07:00
parent 6394c3aaff
commit b3c799d7e7
3 changed files with 114 additions and 74 deletions

View File

@ -16,9 +16,6 @@ void probe_mode_switch(uint8_t mode);
void probe_measure(); void probe_measure();
void probe_set_thresh(uint8_t set);
uint8_t probe_get_thresh();
#endif /* _INC_PROBE_H */ #endif /* _INC_PROBE_H */

View File

@ -174,6 +174,7 @@ __attribute__ ((long_call, section(".ramfunc"))) void flash_erase_page(uint32_t
// enable EOPIE (per DS, EOP isn't set unless EOPIE is set) // enable EOPIE (per DS, EOP isn't set unless EOPIE is set)
// and signal that we want to erase a page // and signal that we want to erase a page
FLASH->SR = FLASH_SR_EOP;
FLASH->CR |= FLASH_CR_EOPIE | FLASH_CR_PER; FLASH->CR |= FLASH_CR_EOPIE | FLASH_CR_PER;
// disable interrupts // disable interrupts
@ -186,15 +187,13 @@ __attribute__ ((long_call, section(".ramfunc"))) void flash_erase_page(uint32_t
// ensure flash is not busy, then wait for end of programming // ensure flash is not busy, then wait for end of programming
while (FLASH->SR & FLASH_SR_BSY); while (FLASH->SR & FLASH_SR_BSY);
while (!(FLASH->SR & FLASH_SR_EOP)); while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR |= FLASH_SR_EOP;
// re-lock flash
FLASH->CR = FLASH_CR_LOCK;
// re-enable interrupts // re-enable interrupts
__set_PRIMASK(primask); __set_PRIMASK(primask);
// clear EOP
FLASH->SR |= FLASH_SR_EOP;
// and re-lock flash
FLASH->CR = FLASH_CR_LOCK;
} }
/* /*
@ -209,7 +208,7 @@ __attribute__ ((long_call, section(".ramfunc"))) void flash_write_page(uint32_t
uint8_t i; uint8_t i;
uint32_t *src; uint32_t *src;
volatile uint32_t *dst; uint32_t *dst;
// ensure flash is not busy // ensure flash is not busy
while (FLASH->SR & FLASH_SR_BSY); while (FLASH->SR & FLASH_SR_BSY);
@ -221,6 +220,7 @@ __attribute__ ((long_call, section(".ramfunc"))) void flash_write_page(uint32_t
// enable EOPIE (per DS, EOP isn't set unless EOPIE is set) // enable EOPIE (per DS, EOP isn't set unless EOPIE is set)
// and signal that we want to write a page // and signal that we want to write a page
FLASH->SR = FLASH_SR_EOP;
FLASH->CR |= FLASH_CR_EOPIE | FLASH_CR_PG; FLASH->CR |= FLASH_CR_EOPIE | FLASH_CR_PG;
// disable interrupts // disable interrupts
@ -229,28 +229,25 @@ __attribute__ ((long_call, section(".ramfunc"))) void flash_write_page(uint32_t
// write data to flash // write data to flash
src = data; src = data;
dst = (volatile uint32_t *)addr; dst = (uint32_t *)addr;
for (i = 0; i < (FLASH_PAGE_SIZE / 4); i++) { for (i = 0; i < (FLASH_PAGE_SIZE / 4) - 1; i++) {
// on the last page, enable the page write start bit to commit to flash
if (i == (FLASH_PAGE_SIZE / 4) - 1) {
FLASH->CR |= FLASH_CR_PGSTRT;
}
*dst++ = *src++; *dst++ = *src++;
} }
// for the last word, enable the PGSTRT bit then write the word to commit to flash
FLASH->CR |= FLASH_CR_PGSTRT;
*dst = *src;
// ensure flash is not busy, then wait for end of programming // ensure flash is not busy, then wait for end of programming
while (FLASH->SR & FLASH_SR_BSY); while (FLASH->SR & FLASH_SR_BSY);
while (!(FLASH->SR & FLASH_SR_EOP)); while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR |= FLASH_SR_EOP;
// re-lock flash
FLASH->CR = FLASH_CR_LOCK;
// re-enable interrupts // re-enable interrupts
__set_PRIMASK(primask); __set_PRIMASK(primask);
// clear EOP
FLASH->SR |= FLASH_SR_EOP;
// and re-lock flash
FLASH->CR = FLASH_CR_LOCK;
} }
/* /*
@ -262,7 +259,7 @@ void flash_commit_conf()
uint32_t i; uint32_t i;
uint8_t flash_idx = 0; uint8_t flash_idx = 0;
uint8_t flash_full; uint8_t flash_full = 0;
struct UConf *fconf; struct UConf *fconf;
uint32_t *f32, *u32; uint32_t *f32, *u32;
@ -280,7 +277,6 @@ void flash_commit_conf()
// if all pages are occupied, erase the first page // if all pages are occupied, erase the first page
if (i == UCONF_COUNT) { if (i == UCONF_COUNT) {
flash_full = 1; flash_full = 1;
flash_erase_page(UCONF_FLASH_START);
} }
// copy flash page from ROM to the write buffer, // copy flash page from ROM to the write buffer,
@ -310,6 +306,12 @@ void flash_commit_conf()
*u32++ = *f32++; *u32++ = *f32++;
} }
// erase this page
flash_erase_page(UCONF_FLASH_START + (UCONF_SIZE * flash_idx));
// commit to flash
flash_write_page(UCONF_FLASH_START + (UCONF_SIZE * flash_idx), write);
// if flash was full, erase other pages // if flash was full, erase other pages
if (flash_full) { if (flash_full) {
for (i = (UCONF_FLASH_START + FLASH_PAGE_SIZE); i < FLASH_END; i += FLASH_PAGE_SIZE) { for (i = (UCONF_FLASH_START + FLASH_PAGE_SIZE); i < FLASH_END; i += FLASH_PAGE_SIZE) {

View File

@ -16,11 +16,10 @@
#include "adc.h" #include "adc.h"
#include "led.h" #include "led.h"
#include "rgbprog.h"
#include "userio.h" #include "userio.h"
#include "flash.h"
#define CONT_OPEN 1240
@ -34,38 +33,32 @@
#define DIODE_SHORT 1900 // test unit measures ~1820 #define DIODE_SHORT 1900 // test unit measures ~1820
#define CONT_SHORTED 858 // worst case measured value of short // continuity offsets from calibrated short value
#define CONT_47OHM 869 // measured value of 47ohm 1% resistor at room temp #define CONT_47OHM 16
#define CONT_100OHM 886 // measured value of 100ohm 1% resistor at room temp #define CONT_100OHM 34
#define CONT_EXTRA_COUNTS 8 // extra raw counts as a fudge factor, post-scaling
#define CONT_LATCH_THRESH 15 // extra raw counts for latching indicator, post-scaling
#define CONT_EXTRA_COUNTS 3 // extra raw counts as a fudge factor
#define CONT_CAL_THRESHOLD 890 // typical short is ~845-860 region
#define CONT_LATCH_THRESHOLD 5 // extra raw counts for latching indicator
#define CONT_LATCH_TIMEOUT 4 // probe measurement passes to hold latched indicator #define CONT_LATCH_TIMEOUT 4 // probe measurement passes to hold latched indicator
static uint16_t vref; static uint16_t vref;
static uint16_t cont_thresh_set = 0; static uint8_t cont_cal_timer;
static const uint16_t cont_thresh[] = { static uint16_t cont_cal_low;
static uint16_t cont_cal_avg;
static const uint16_t cont_thresh_tbl[] = {
CONT_100OHM, CONT_47OHM CONT_100OHM, CONT_47OHM
}; };
static uint8_t latch = 0; static uint8_t latch = 0;
static uint8_t buzzer = 0;
void probe_set_thresh(uint8_t set)
{
cont_thresh_set = set;
}
uint8_t probe_get_thresh()
{
return cont_thresh_set;
}
static inline void probe_measure_cont() static inline void probe_measure_cont()
{ {
uint16_t probe, v_ext; uint16_t probe, v_ext;
@ -75,57 +68,105 @@ static inline void probe_measure_cont()
probe = adc_avg[ADC_PROBE]; probe = adc_avg[ADC_PROBE];
v_ext = adc_avg[ADC_VREF_EXT]; v_ext = adc_avg[ADC_VREF_EXT];
// if the button has been pushed, toggle the buzzer // update vref
if (userio_get_btn() > 0) { if (!vref) vref = v_ext;
buzzer ^= 1; vref += v_ext;
} vref >>= 1;
// if the button has been held, change the threshold // if the button has been held, change the threshold
if (userio_get_btn_held() == 60) { if (userio_get_btn_held() == 60) {
userio_set_btn_override(); userio_set_btn_override();
cont_thresh_set ^= 1; conf.cont_sensitivity ^= 1;
} }
// this LED will not set anything, // if the button has _really_ been held, clear the calibration
if (userio_get_btn_held() == 600) {
conf.cont_shorted = 0;
}
// if there is no calibration, try to perform it
if (!conf.cont_shorted) {
// until this is performed, show error LEDs
rgbprog_error_flasher(5, 0, 0, 100);
// initialize
if (!cont_cal_timer) {
cont_cal_low = 0xfff;
cont_cal_avg = 0;
}
// if probe appears to be shorted, calibrate to lowest value read
if (adc_avg[ADC_PROBE] < CONT_CAL_THRESHOLD) {
// low value
if (adc_avg[ADC_PROBE] < cont_cal_low) {
cont_cal_low = adc_avg[ADC_PROBE];
}
// average value
if (!cont_cal_avg) {
cont_cal_avg = adc_avg[ADC_PROBE];
} else {
cont_cal_avg += adc_avg[ADC_PROBE];
cont_cal_avg >>= 1;
}
cont_cal_timer++;
} else {
cont_cal_timer = 0;
}
// after held long enough, commit
if (cont_cal_timer == 180) {
cont_cal_timer = 0;
conf.cont_shorted = (cont_cal_avg + cont_cal_low) >> 1;
flash_commit_conf();
}
}
// right LED remains unused while buzzer is being used,
// but zero it out anyway // but zero it out anyway
led_setrgb(1, 0, 0, 0); led_setrgb(1, 0, 0, 0);
// if no calibration yet, bail here
if (!conf.cont_shorted) return;
// if the button has been pushed, toggle buzzer on/off
if (userio_get_btn() > 0) {
conf.cont_buzzer ^= 1;
}
if (vref) { if (vref) {
// note: we don't use VREF anymore...
// it's too noisy :/
// probe level // probe level
x = probe << 12; // maximum possible level x = probe - conf.cont_shorted;
x /= vref; // normalize to 4096max if (x > 0xfff) x = 0;
if (x > 4095) x = 4095;
// threshold level // threshold level
c = cont_thresh[cont_thresh_set] << 12; c = cont_thresh_tbl[conf.cont_sensitivity];
c /= vref;
// are we measuring a lower value? // are we measuring a lower ohm value on the probes than our threshold?
if (x < (c + CONT_EXTRA_COUNTS)) { // roughly 100ohm or lower if (x < (c + CONT_EXTRA_COUNTS)) { // roughly 100ohm or lower
latch = CONT_LATCH_TIMEOUT; // latch any continuity for a while latch = CONT_LATCH_TIMEOUT; // latch any continuity for a while
} }
b = buzzer ? 100 : 0; // blue led to indicate buzzer is enabled
b = conf.cont_buzzer ? 20 : 0;
if (latch) { if (latch) {
// hysteresis // hysteresis
if (x >= c + CONT_LATCH_THRESH) latch--; if (x >= (c + CONT_LATCH_THRESHOLD)) latch--;
// indicate continuity // indicate continuity
led_setrgb(0, 0, 250, b); led_setrgb(0, 0, 250, b);
led_buzz(0); led_buzz(0);
if (buzzer) led_buzz(1); if (conf.cont_buzzer) led_buzz(1);
} else { } else {
// idle // idle
g = cont_thresh_set ? 40 : 0; g = conf.cont_sensitivity ? 40 : 0;
led_setrgb(0, 120, g, b); led_setrgb(0, 120, g, b);
led_buzz(0); led_buzz(0);
} }
} }
if (!vref) vref = v_ext;
vref += v_ext;
vref >>= 1;
} }
static inline void probe_measure_diode() static inline void probe_measure_diode()