diff --git a/src/cli/rl78.c b/src/cli/rl78.c index d6f9e10..3679f63 100644 --- a/src/cli/rl78.c +++ b/src/cli/rl78.c @@ -1,4 +1,5 @@ +#include #include #include #include @@ -99,16 +100,35 @@ result: 0x06. sig: secget: 0x06 sec: flg=fe bot=03 fsws=0000 fswe=003f +result: 0x06. sig: +0x10 0x00 0x0a 0x52 0x37 0x46 0x31 0x30 0x30 0x47 0x4c 0x47 0x20 0xff 0xff 0x01 0xff 0x2f 0x0f 0x01 0x00 0x02 +secget: 0x06 +sec: flg=17:1d bot=07 fsws=0000 fswe=0000 +fswget: 0x06 +fsw: block=0000..003f, rw=y invert=y + */ uint8_t* datatoflash = false ? DATA_main_nodbg : DATA_main_dbgpad; // NOTE: RL78 ONLY - struct tool78_security sec; - memset(&sec, 0, sizeof sec); - st = tool78_do_security_get(hw, &sec); - printf("secget: 0x%02x\n", st); - printf("sec: flg=%02x bot=%02x fsws=%04x fswe=%04x\n", sec.flg, sec.bot, sec.fsws, sec.fswe); + if (sig.rl78.dev[0] == 'R' && sig.rl78.dev[2] == 'F') { + struct tool78_security sec; + memset(&sec, 0, sizeof sec); + st = tool78_do_security_get(hw, &sec); + printf("secget: 0x%02x\n", st); + printf("sec: flg=%02x:%02x bot=%02x fsws=%04x fswe=%04x\n", sec.flg, sec.flg2, sec.bot, sec.fsws, sec.fswe); + + if (sig.rl78.dev[1] == '7') { + struct tool78_fsw_settings fsw; + st = tool78_do_fsw_get(hw, &fsw); + printf("fswget: 0x%02x\n", st); + printf("fsw: block=%04hx..%04hx, rw=%c invert=%c\n", + fsw.block_start, fsw.block_end, + (fsw.fswopt_rw_allow ? 'y' : 'n'), + (fsw.fsw_area_invert ? 'y' : 'n')); + } + } //st = tool78_do_security_release(&tool78_hw_rl78_uart1); //printf("sec rel: 0x%02x\n", st); diff --git a/src/tool78/tool78_cmds.c b/src/tool78/tool78_cmds.c index ac1cd4f..22ad4ad 100644 --- a/src/tool78/tool78_cmds.c +++ b/src/tool78/tool78_cmds.c @@ -187,8 +187,8 @@ static enum tool78_stat tool78_cmd_send(struct tool78_hw* hw, uint8_t cmd, return tool78_stat_ack; } -static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf, - int expected_len, int timeout_us_first) { +static enum tool78_stat tool78_data_recv_ex(struct tool78_hw* hw, uint8_t* buf, + int expected_len, int timeout_us_first, int* len_recv) { uint8_t pre[10]; // stx, len uint8_t post[2]; // cksum, etx/etb //printf("recv to=%d\n", timeout_us_first); @@ -200,9 +200,11 @@ static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf, //printf("pre[0]=0x%02x\n", pre[0]); if (hw->target == tool78k0_spi && pre[0] == tool78_stat_busy) return tool78_stat_busy; - if (pre[0] != tool78_frame_stx) + if (pre[0] != tool78_frame_stx) { + //printf("didn't get stx, but %02hhx\n", pre[0]); return (hw->flags & tool78_hw_flag_done_reset) ? tool78_stat_busy : tool78_stat_protocol_error; + } rr = hw->recv(1, &pre[1], 100*100); //printf("rr=%d (0x%08lx) pre[1]=0x%02x\n", rr, (uint32_t)rr, pre[1]); @@ -215,10 +217,12 @@ static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf, int len = pre[1]; if (!len) len = 256; + if (len_recv) *len_recv = len; if (hw->target == tool78k0_spi && pre[1] == tool78_stat_busy) return tool78_stat_busy; if (expected_len >= 0 && len != expected_len) { + //printf("bad length, wanted %d but got %d\n", expected_len, len); return tool78_stat_protocol_error; } @@ -228,18 +232,23 @@ static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf, rr = hw->recv(2, post, 200*100); if (rr != 2) return tool78_stat_internal_error; if (post[1] != tool78_frame_etx && post[1] != tool78_frame_etb) { + //printf("didn't get etx/etb, but got %02hhx\n", post[1]); return tool78_stat_protocol_error; } uint8_t ck = tool78_calc_checksum8(len, buf); ck = tool78_digest_checksum8(ck, 1, &pre[1]); if (ck != post[0]) { + //printf("bad checksum: %02hhx <-> %02hxx\n", ck, post[0]); return tool78_stat_protocol_error; } return tool78_stat_ack; } - +inline static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, + uint8_t* buf, int expected_len, int timeout_us_first) { + return tool78_data_recv_ex(hw, buf, expected_len, timeout_us_first, NULL); +} /*#define tool78_wait_status(hw, l, timeout_us, ...) \ tool78_wait_status__impl(hw, l, timeout_us, __VA_OPT__(1 ? __VA_ARGS__ : ) -1) \ */ @@ -594,15 +603,16 @@ enum tool78_stat tool78_do_silicon_signature(struct tool78_hw* hw, } /*gpio_set_function(18, GPIO_FUNC_SIO); - gpio_set_dir(18, true); + gpio_set_dir(18, GPIO_OUT); gpio_put(18, true);*/ memset(sig_dest, 0, 27); enum tool78_stat st = tool78_cmd_send(hw, tool78_cmd_silicon_signature, 0, NULL); + //printf("sent silisig: %02x\n", st); if (st != tool78_stat_ack) return st; //gpio_put(18, false); - st = tool78_wait_status(hw, 1, tWT11); // aka tCS11 + st = tool78_wait_status(hw, 1, tWT11*2); // aka tCS11 //gpio_put(18, true); //printf("silisig: status 0x%02x\n", st); if (st != tool78_stat_ack) return st; @@ -739,7 +749,9 @@ enum tool78_stat tool78_do_security_get(struct tool78_hw* hw, if ((hw->target & tool78_mcu_mask) != tool78_mcu_rl78) return tool78_stat_bad_mcu_for_cmd; + memset(sec, 0, sizeof *sec); uint8_t data[8]; + memset(data, 0, sizeof data); enum tool78_stat st = tool78_cmd_send(hw, tool78_cmd_security_get, 0, NULL); if (st != tool78_stat_ack) return st; @@ -747,13 +759,22 @@ enum tool78_stat tool78_do_security_get(struct tool78_hw* hw, st = tool78_wait_status(hw, 1, tCS8); if (st != tool78_stat_ack) return st; - st = tool78_data_recv(hw, data, 8, tSD8); + int len_real; + st = tool78_data_recv_ex(hw, data, -1, tSD8, &len_real); if (st != tool78_stat_ack) return st; - sec->flg = data[0]; - sec->bot = data[1]; - sec->fsws = data[2] | ((uint16_t)data[3]<<8); - sec->fswe = data[4] | ((uint16_t)data[5]<<8); + if (len_real == 8) { + // everything up to RL78/G23 + sec->flg = data[0]; + sec->bot = data[1]; + sec->fsws = data[2] | ((uint16_t)data[3]<<8); + sec->fswe = data[4] | ((uint16_t)data[5]<<8); + } else { + // RL78/G23 + sec->flg = data[0]; + sec->flg2 = data[1]; + sec->bot = data[2]; + } return st; } @@ -1505,6 +1526,7 @@ enum tool78_stat tool78_init_sfp(struct tool78_hw* hw, tool78_silicon_sig_t* sig hw->flags &= ~tool78_hw_flag_do_ocd; enum tool78_stat st = tool78_init_common(hw); + //printf("init common %02x\n", st); if (st != tool78_stat_ack) return st; if ((hw->target & tool78_mcu_mask) != tool78_mcu_rl78g10) { diff --git a/src/tool78/tool78_cmds.h b/src/tool78/tool78_cmds.h index cb0a673..cb643bf 100644 --- a/src/tool78/tool78_cmds.h +++ b/src/tool78/tool78_cmds.h @@ -54,7 +54,7 @@ enum tool78_stat tool78_do_g10_erase_then_write(struct tool78_hw*, enum tool78_stat tool78_do_g23_security_set(struct tool78_hw* hw, const struct tool78_security* sec); // 0xA1 -enum tool78_stat tool78_do_g23_security_get(struct tool78_hw* hw, // RL78 only +enum tool78_stat tool78_do_g23_security_get(struct tool78_hw* hw, struct tool78_security* sec); // 0x9C: payload = passwd; ack/nak enum tool78_stat tool78_do_security_idauth(struct tool78_hw* hw, diff --git a/src/tool78/tool78_hw_helpers.c b/src/tool78/tool78_hw_helpers.c index d9dbef8..b84101b 100644 --- a/src/tool78/tool78_hw_helpers.c +++ b/src/tool78/tool78_hw_helpers.c @@ -1,5 +1,6 @@ #include +#include #include #include @@ -155,7 +156,7 @@ int tool78_hw_help_send(const struct tool78_pio_vars* vars, uint32_t sleep_us_be if (vars->exclusive) { //gpio_put(3, false); - pio_sm_set_enabled(PINOUT_TOOL78_PIO, vars->smrx, false); + //pio_sm_set_enabled(PINOUT_TOOL78_PIO, vars->smrx, false); } bool blockinf = timeout_us < 0; @@ -164,6 +165,11 @@ int tool78_hw_help_send(const struct tool78_pio_vars* vars, uint32_t sleep_us_be check_timeout_fn ct = NULL; if (!blockinf) ct = init_single_timeout_until(&ts, at); + /*size_t nyeeted=0; + uint8_t yeets[100]; + memset(yeets, 0, sizeof yeets);*/ + + int i = 0; for (; i < len; ++i) { while (pio_sm_is_tx_fifo_full(PINOUT_TOOL78_PIO, vars->smtx)) { @@ -182,10 +188,29 @@ int tool78_hw_help_send(const struct tool78_pio_vars* vars, uint32_t sleep_us_be || !(tool78_hw_rl78_uart1.flags & tool78_hw_flag_done_reset))) { busy_wait_us_32(sleep_us_between_bytes); } + + if (vars->exclusive) { + //if (nyeeted < sizeof yeets) { + while (pio_sm_is_rx_fifo_empty(PINOUT_TOOL78_PIO, vars->smrx)) + ; + char x = *(volatile uint8_t*)&PINOUT_TOOL78_PIO->rxf[vars->smrx]; + (void)x; + //yeets[nyeeted] = x; + //if (bitswap) yeets[nyeeted] = bitswap(yeets[nyeeted]); + //++nyeeted; + //} + } } end: + if (vars->exclusive) { + /*if (yeets[2] != 0x9a && yeets[0] == 0x01) { + for (size_t i = 0; i < nyeeted; ++i) { + printf("yeet %02hhx\n", yeets[i]); + } + }*/ + // FIXME: remove the dependency on tool78_hw_rl78_uart1! if ((tool78_hw_rl78_uart1.flags & tool78_hw_flag_done_reset) /*&& !(tool78_hw_rl78_uart1.flags & tool78_hw_flag_do_ocd)*/) { @@ -197,7 +222,7 @@ end: } //gpio_put(3, true); - pio_sm_set_enabled(PINOUT_TOOL78_PIO, vars->smrx, true); + //pio_sm_set_enabled(PINOUT_TOOL78_PIO, vars->smrx, true); } return i;