rl78: basic ID stuff works with rl78/g23

This commit is contained in:
Triss 2022-07-01 22:36:25 +02:00
parent 989785e06b
commit 09f91e51cb
4 changed files with 86 additions and 19 deletions

View File

@ -1,4 +1,5 @@
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <pico/time.h> #include <pico/time.h>
@ -99,16 +100,35 @@ result: 0x06. sig:
secget: 0x06 secget: 0x06
sec: flg=fe bot=03 fsws=0000 fswe=003f 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; uint8_t* datatoflash = false ? DATA_main_nodbg : DATA_main_dbgpad;
// NOTE: RL78 ONLY // NOTE: RL78 ONLY
struct tool78_security sec; if (sig.rl78.dev[0] == 'R' && sig.rl78.dev[2] == 'F') {
memset(&sec, 0, sizeof sec); struct tool78_security sec;
st = tool78_do_security_get(hw, &sec); memset(&sec, 0, sizeof sec);
printf("secget: 0x%02x\n", st); st = tool78_do_security_get(hw, &sec);
printf("sec: flg=%02x bot=%02x fsws=%04x fswe=%04x\n", sec.flg, sec.bot, sec.fsws, sec.fswe); 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); //st = tool78_do_security_release(&tool78_hw_rl78_uart1);
//printf("sec rel: 0x%02x\n", st); //printf("sec rel: 0x%02x\n", st);

View File

@ -187,8 +187,8 @@ static enum tool78_stat tool78_cmd_send(struct tool78_hw* hw, uint8_t cmd,
return tool78_stat_ack; return tool78_stat_ack;
} }
static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf, static enum tool78_stat tool78_data_recv_ex(struct tool78_hw* hw, uint8_t* buf,
int expected_len, int timeout_us_first) { int expected_len, int timeout_us_first, int* len_recv) {
uint8_t pre[10]; // stx, len uint8_t pre[10]; // stx, len
uint8_t post[2]; // cksum, etx/etb uint8_t post[2]; // cksum, etx/etb
//printf("recv to=%d\n", timeout_us_first); //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]); //printf("pre[0]=0x%02x\n", pre[0]);
if (hw->target == tool78k0_spi && pre[0] == tool78_stat_busy) if (hw->target == tool78k0_spi && pre[0] == tool78_stat_busy)
return 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) return (hw->flags & tool78_hw_flag_done_reset)
? tool78_stat_busy : tool78_stat_protocol_error; ? tool78_stat_busy : tool78_stat_protocol_error;
}
rr = hw->recv(1, &pre[1], 100*100); rr = hw->recv(1, &pre[1], 100*100);
//printf("rr=%d (0x%08lx) pre[1]=0x%02x\n", rr, (uint32_t)rr, pre[1]); //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]; int len = pre[1];
if (!len) len = 256; if (!len) len = 256;
if (len_recv) *len_recv = len;
if (hw->target == tool78k0_spi && pre[1] == tool78_stat_busy) if (hw->target == tool78k0_spi && pre[1] == tool78_stat_busy)
return tool78_stat_busy; return tool78_stat_busy;
if (expected_len >= 0 && len != expected_len) { if (expected_len >= 0 && len != expected_len) {
//printf("bad length, wanted %d but got %d\n", expected_len, len);
return tool78_stat_protocol_error; 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); rr = hw->recv(2, post, 200*100);
if (rr != 2) return tool78_stat_internal_error; if (rr != 2) return tool78_stat_internal_error;
if (post[1] != tool78_frame_etx && post[1] != tool78_frame_etb) { 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; return tool78_stat_protocol_error;
} }
uint8_t ck = tool78_calc_checksum8(len, buf); uint8_t ck = tool78_calc_checksum8(len, buf);
ck = tool78_digest_checksum8(ck, 1, &pre[1]); ck = tool78_digest_checksum8(ck, 1, &pre[1]);
if (ck != post[0]) { if (ck != post[0]) {
//printf("bad checksum: %02hhx <-> %02hxx\n", ck, post[0]);
return tool78_stat_protocol_error; return tool78_stat_protocol_error;
} }
return tool78_stat_ack; 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, ...) \ /*#define tool78_wait_status(hw, l, timeout_us, ...) \
tool78_wait_status__impl(hw, l, timeout_us, __VA_OPT__(1 ? __VA_ARGS__ : ) -1) \ 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_function(18, GPIO_FUNC_SIO);
gpio_set_dir(18, true); gpio_set_dir(18, GPIO_OUT);
gpio_put(18, true);*/ gpio_put(18, true);*/
memset(sig_dest, 0, 27); memset(sig_dest, 0, 27);
enum tool78_stat st = tool78_cmd_send(hw, tool78_cmd_silicon_signature, 0, NULL); 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; if (st != tool78_stat_ack) return st;
//gpio_put(18, false); //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); //gpio_put(18, true);
//printf("silisig: status 0x%02x\n", st); //printf("silisig: status 0x%02x\n", st);
if (st != tool78_stat_ack) return 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) if ((hw->target & tool78_mcu_mask) != tool78_mcu_rl78)
return tool78_stat_bad_mcu_for_cmd; return tool78_stat_bad_mcu_for_cmd;
memset(sec, 0, sizeof *sec);
uint8_t data[8]; uint8_t data[8];
memset(data, 0, sizeof data);
enum tool78_stat st = tool78_cmd_send(hw, tool78_cmd_security_get, 0, NULL); enum tool78_stat st = tool78_cmd_send(hw, tool78_cmd_security_get, 0, NULL);
if (st != tool78_stat_ack) return st; 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); st = tool78_wait_status(hw, 1, tCS8);
if (st != tool78_stat_ack) return st; 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; if (st != tool78_stat_ack) return st;
sec->flg = data[0]; if (len_real == 8) {
sec->bot = data[1]; // everything up to RL78/G23
sec->fsws = data[2] | ((uint16_t)data[3]<<8); sec->flg = data[0];
sec->fswe = data[4] | ((uint16_t)data[5]<<8); 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; 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; hw->flags &= ~tool78_hw_flag_do_ocd;
enum tool78_stat st = tool78_init_common(hw); enum tool78_stat st = tool78_init_common(hw);
//printf("init common %02x\n", st);
if (st != tool78_stat_ack) return st; if (st != tool78_stat_ack) return st;
if ((hw->target & tool78_mcu_mask) != tool78_mcu_rl78g10) { if ((hw->target & tool78_mcu_mask) != tool78_mcu_rl78g10) {

View File

@ -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, enum tool78_stat tool78_do_g23_security_set(struct tool78_hw* hw,
const struct tool78_security* sec); const struct tool78_security* sec);
// 0xA1 // 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); struct tool78_security* sec);
// 0x9C: payload = passwd; ack/nak // 0x9C: payload = passwd; ack/nak
enum tool78_stat tool78_do_security_idauth(struct tool78_hw* hw, enum tool78_stat tool78_do_security_idauth(struct tool78_hw* hw,

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <hardware/gpio.h> #include <hardware/gpio.h>
#include <hardware/pio.h> #include <hardware/pio.h>
@ -155,7 +156,7 @@ int tool78_hw_help_send(const struct tool78_pio_vars* vars, uint32_t sleep_us_be
if (vars->exclusive) { if (vars->exclusive) {
//gpio_put(3, false); //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; 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; check_timeout_fn ct = NULL;
if (!blockinf) ct = init_single_timeout_until(&ts, at); 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; int i = 0;
for (; i < len; ++i) { for (; i < len; ++i) {
while (pio_sm_is_tx_fifo_full(PINOUT_TOOL78_PIO, vars->smtx)) { 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))) { || !(tool78_hw_rl78_uart1.flags & tool78_hw_flag_done_reset))) {
busy_wait_us_32(sleep_us_between_bytes); 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: end:
if (vars->exclusive) { 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! // FIXME: remove the dependency on tool78_hw_rl78_uart1!
if ((tool78_hw_rl78_uart1.flags & tool78_hw_flag_done_reset) if ((tool78_hw_rl78_uart1.flags & tool78_hw_flag_done_reset)
/*&& !(tool78_hw_rl78_uart1.flags & tool78_hw_flag_do_ocd)*/) { /*&& !(tool78_hw_rl78_uart1.flags & tool78_hw_flag_do_ocd)*/) {
@ -197,7 +222,7 @@ end:
} }
//gpio_put(3, true); //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; return i;