From 37a41e5f8389cacc0039237e97d394ff7052a800 Mon Sep 17 00:00:00 2001 From: sys64738 Date: Mon, 2 May 2022 03:21:00 +0200 Subject: [PATCH] picoemp stuff for g10 glitching (doesn't work...) --- src/cli/rl78-glitch.c | 94 ++++++++++++++++++++++++++----------------- src/zap/zap_adc.c | 4 +- src/zap/zap_picoemp.c | 14 +++++-- src/zap/zap_picoemp.h | 4 ++ src/zap/zap_pinout.h | 4 +- 5 files changed, 74 insertions(+), 46 deletions(-) diff --git a/src/cli/rl78-glitch.c b/src/cli/rl78-glitch.c index c6db886..142dc12 100644 --- a/src/cli/rl78-glitch.c +++ b/src/cli/rl78-glitch.c @@ -47,12 +47,14 @@ sec: flg=fe bot=03 fsws=0000 fswe=003f static uint8_t databuf[DUMP_SIZE]; static struct glitch_param_randrange offset; static struct glitch_param_randrange/*adc*/ length; +//static struct glitch_param_const length; static bool glitch_init_core1_stuff(bool exttrig, uint glitchpin) { offset.min = 10*1000; offset.max = 35433*1000; length.min = 100; - length.max = 50/*26*/*1000; + length.max = 10*50/*26*/*1000; + //length.value = 5*1000; // 5 us #if PINOUT_USE_DRAGONZAP //length.adc_index = 0;//999; #else @@ -61,6 +63,7 @@ static bool glitch_init_core1_stuff(bool exttrig, uint glitchpin) { #endif glitch_param_randrange_init(&offset); + glitch_param_const_init(&length); glitch_param_randrange_init(&length); //glitch_param_adc_init(&length); @@ -580,9 +583,9 @@ void cli_tool78_glitch_param_g10(void) { zap_dac_set_shutdown(false); #endif - //zap_max_init(false, true); - zap_max_use_vhi_jp300(true); - zap_max_set_inhibit(false); + /*zap_max_use_vhi_jp300(true); + zap_max_set_inhibit(false);*/ + zap_picoemp_init(); if (!glitch_init_core1_stuff(true, ZAP_GLITCH_OUT)) { printf("bad glitcher params!\n"); @@ -598,9 +601,13 @@ void cli_tool78_glitch_param_g10(void) { bool first = true; restart: + zap_picoemp_set_armed(false); glitch_disarm(); /*busy_wait_ms(40);*/ + zap_picoemp_set_armed(true); + while (!zap_picoemp_has_hv()) ; + st = tool78_init_ocd(hw, NULL, passwd); if (st == 0xc3) { printf("aa\n"); @@ -615,9 +622,9 @@ restart: //printf("result: 0x%02x ver=%04x\n", st, ver); if (st != 0xf2 && st != 0xf0) { if (st == 0xf3) { // idk - gpio_put(12, true); + //gpio_put(12, true); printf("init: 0x%02x off=%lu len=%lu vv=%03x\n", st, *off, *len, vvv); - gpio_put(12, false); + //gpio_put(12, false); goto do_reset_stuff; } else { printf("init: 0x%02x\n", st); @@ -716,62 +723,73 @@ restart: gpio_set_dir(7, GPIO_IN);*/ while (true) { - //vvv = zap_adc_read(1); + /*//vvv = zap_adc_read(1); vvv = (random() & 0x3ff) + 0x400; zap_dac_set_b(vvv); - zap_dac_latch(); + zap_dac_latch();*/ glitch_arm(); rr = hw->recv(2, checkbuf, 150*1000); //printf("rr=%d cb=%02x %02x\n", rr, checkbuf[0], checkbuf[1]); glitch_disarm(); - if (rr == 2 && (checkbuf[0] != 'H' || checkbuf[1] != 'i')) rr = 0; - if (rr == 2) { - rr = hw->recv(lenlen, checkbuf, 150*1000); - //printf("recv data rr=%d\n", rr); - } - if (rr <= 0) { - // timeout or something - printf("X"); - off = (volatile uint32_t*)&glitch_param_cur.offset_ns.cur; - len = (volatile uint32_t*)&glitch_param_cur.length_ns.cur; - printf("X off=%lu len=%lu vv=%03x\n", *off, *len, vvv); - hw->deinit(); - goto do_reset_stuff; - } else { - int firstbad = -1, lastbad = -1; - for (int i = 0; i < rr; ++i) { - if (checkbuf[i] != (0xaa^0xff)) { - if (firstbad == -1) firstbad = i; - lastbad = i; - } + bool first_ = true; + do { + if (rr == 2 && (checkbuf[0] != 'H' || checkbuf[1] != 'i')) rr = 0; + if (rr == 2) { + rr = hw->recv(lenlen, checkbuf, 150*1000); + //printf("recv data rr=%d\n", rr); } - if (firstbad >= 0) { - //gpio_put(12, true); + if (rr <= 0) { + if (!first_) break; + // timeout or something + printf("X"); off = (volatile uint32_t*)&glitch_param_cur.offset_ns.cur; len = (volatile uint32_t*)&glitch_param_cur.length_ns.cur; - printf("glitch first=%d last=%d off=%lu len=%lu vvv=%03x rr=%d v=%02x\n", - firstbad, lastbad, *off, *len, vvv, rr, checkbuf[firstbad]^0xaa); - //gpio_put(12, false); + //printf("X off=%lu len=%lu vv=%03x\n", *off, *len, vvv); + hw->deinit(); + goto do_reset_stuff; } else { - printf("."); - printf("NN off=%lu len=%lu vv=%03x\n", *off, *len, vvv); + int firstbad = -1, lastbad = -1; + for (int i = 0; i < rr; ++i) { + if (checkbuf[i] != (0xaa^0xff)) { + if (firstbad == -1) firstbad = i; + lastbad = i; + } + } + if (firstbad >= 0) { + gpio_put(12, true); + off = (volatile uint32_t*)&glitch_param_cur.offset_ns.cur; + len = (volatile uint32_t*)&glitch_param_cur.length_ns.cur; + printf("glitch first=%d last=%d off=%lu len=%lu vvv=%03x rr=%d v=%02x\n", + firstbad, lastbad, *off, *len, vvv, rr, checkbuf[firstbad]^0xaa); + gpio_put(12, false); + } else { + printf("."); + //printf("NN off=%lu len=%lu vv=%03x\n", *off, *len, vvv); + } } - } + + first_=false; + if (!zap_picoemp_has_hv()) { + rr = hw->recv(2, checkbuf, 150*1000); + } else break; + } while (true); } deinit_bad: + zap_picoemp_set_armed(false); busy_wait_ms(500); hw->deinit(); return; do_reset_stuff: + zap_picoemp_set_armed(false); busy_wait_ms(300); hw->deinit(); // glitch for way too long to power cycle - sio_hw->gpio_set = 1u << ZAP_GLITCH_OUT; + //sio_hw->gpio_set = 1u << ZAP_GLITCH_OUT; busy_wait_ms(16); - sio_hw->gpio_clr = 1u << ZAP_GLITCH_OUT; + //sio_hw->gpio_clr = 1u << ZAP_GLITCH_OUT; goto restart; } diff --git a/src/zap/zap_adc.c b/src/zap/zap_adc.c index a9327c2..3343bba 100644 --- a/src/zap/zap_adc.c +++ b/src/zap/zap_adc.c @@ -12,11 +12,11 @@ void zap_adc_init(void) { adc_gpio_init(ZAP_ADC_A); adc_gpio_init(ZAP_ADC_B); - //adc_gpio_init(ZAP_ADC_REF); + adc_gpio_init(ZAP_ADC_CHG); } uint16_t zap_adc_read(int idx) { - if (idx < 0 || idx >= 2/*3*/) return 0xf000; + if (idx < 0 || idx >= 3/*3*/) return 0xf000; adc_select_input(idx); return adc_read(); diff --git a/src/zap/zap_picoemp.c b/src/zap/zap_picoemp.c index 9b464ad..73e4433 100644 --- a/src/zap/zap_picoemp.c +++ b/src/zap/zap_picoemp.c @@ -10,6 +10,7 @@ #include #include +#include "zap_adc.h" #include "zap_pinout.h" #include "zap_picoemp.h" @@ -24,10 +25,11 @@ void zap_picoemp_init(void) { gpio_set_slew_rate(ZAP_PICOEMP_HVP, GPIO_SLEW_RATE_FAST); gpio_set_function(ZAP_PICOEMP_HVP, GPIO_FUNC_SIO); - gpio_set_dir(ZAP_PICOEMP_CHG, GPIO_IN); + zap_adc_init(); + /*gpio_set_dir(ZAP_PICOEMP_CHG, GPIO_IN); gpio_set_pulls(ZAP_PICOEMP_CHG, false, false); gpio_set_inover(ZAP_PICOEMP_CHG, GPIO_OVERRIDE_INVERT); - gpio_set_function(ZAP_PICOEMP_CHG, GPIO_FUNC_SIO); + gpio_set_function(ZAP_PICOEMP_CHG, GPIO_FUNC_SIO);*/ zap_picoemp_set_armed(false); } @@ -81,13 +83,17 @@ void zap_picoemp_set_armed(bool arm) { } void zap_picoemp_pulse(void) { - gpio_put(ZAP_PICOEMP_HVP, true); + /*gpio_put(ZAP_PICOEMP_HVP, true); busy_wait_us_32(5); gpio_put(ZAP_PICOEMP_HVP, false); - busy_wait_us_32(5); + busy_wait_us_32(5);*/ gpio_put(ZAP_PICOEMP_HVP, true); busy_wait_us_32(5); gpio_put(ZAP_PICOEMP_HVP, false); busy_wait_ms(250); } +uint16_t zap_picoemp_chg_get(void) { + return zap_adc_read(2); +} + diff --git a/src/zap/zap_picoemp.h b/src/zap/zap_picoemp.h index b4581b4..ab91636 100644 --- a/src/zap/zap_picoemp.h +++ b/src/zap/zap_picoemp.h @@ -10,8 +10,12 @@ void zap_picoemp_init(void); bool zap_picoemp_get_armed(void); void zap_picoemp_set_armed(bool arm); uint16_t zap_picoemp_chg_get(void); +static inline bool zap_picoemp_has_hv(void) { + return zap_picoemp_chg_get() < ((uint32_t)((0.85f/3.3f)*0xfff)); +} void zap_picoemp_pulse(void); + #endif diff --git a/src/zap/zap_pinout.h b/src/zap/zap_pinout.h index fce7272..0d67341 100644 --- a/src/zap/zap_pinout.h +++ b/src/zap/zap_pinout.h @@ -24,10 +24,10 @@ #define ZAP_ADC_A 26 #define ZAP_ADC_B 27 -/*#define ZAP_ADC_REF 28*/ +#define ZAP_ADC_CHG 28 #define ZAP_PICOEMP_PWM 14 -#define ZAP_PICOEMP_CHG 15 +/*#define ZAP_PICOEMP_CHG 15*/ #define ZAP_PICOEMP_HVP ZAP_GLITCH_OUT #endif