Skip to content

Commit

Permalink
Initial SWIO commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Baldanos committed Jan 23, 2025
1 parent 8a7a6ff commit a2d1f57
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/hydrabus/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const t_token_dict tl_dict[] = {
{ T_TIMEOUT, "timeout" },
{ T_PEEK, "peek" },
{ T_POKE, "poke" },
{ T_SWIO, "swio" },
/* Developer warning add new command(s) here */

/* BP-compatible commands */
Expand Down Expand Up @@ -1374,6 +1375,10 @@ t_token tokens_mode_onewire[] = {
T_ARG_STRING,
.help = "Write string"
},
{
T_DEBUG,
.help = "minichlink compatible SWIO debugger"
},
/* BP commands */
{
T_LEFT_SQ,
Expand Down
1 change: 1 addition & 0 deletions src/hydrabus/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ enum {
T_TIMEOUT,
T_PEEK,
T_POKE,
T_SWIO,
/* Developer warning add new command(s) here */

/* BP-compatible commands */
Expand Down
2 changes: 2 additions & 0 deletions src/hydrabus/hydrabus_bbio.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
#define BBIO_ONEWIRE_RESET 0b00000010
#define BBIO_ONEWIRE_READ 0b00000100
#define BBIO_ONEWIRE_BULK_TRANSFER 0b00010000
#define BBIO_ONEWIRE_SWIO_READ 0b00100000
#define BBIO_ONEWIRE_SWIO_WRITE 0b00110000
#define BBIO_ONEWIRE_CONFIG_PERIPH 0b01000000

/*
Expand Down
24 changes: 22 additions & 2 deletions src/hydrabus/hydrabus_bbio_onewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static void bbio_mode_id(t_hydra_console *con)
void bbio_mode_onewire(t_hydra_console *con)
{
mode_config_proto_t* proto = &con->mode->proto;
uint32_t swio_data;
uint8_t bbio_subcommand, i;
uint8_t rx_data[16], tx_data[16];
uint8_t data;
Expand All @@ -64,6 +65,17 @@ void bbio_mode_onewire(t_hydra_console *con)
rx_data[0] = onewire_read_u8(con);
cprint(con, (char *)&rx_data[0], 1);
break;
case BBIO_ONEWIRE_SWIO_READ:
chnRead(con->sdu, &data, 1);
swio_data = onewire_swio_read_reg(con, data);
cprint(con, (void *)&swio_data, 4);
break;
case BBIO_ONEWIRE_SWIO_WRITE:
chnRead(con->sdu, &data, 1);
chnRead(con->sdu, (uint8_t *)&swio_data, 4);
onewire_swio_write_reg(con, data, swio_data);
cprint(con, "\x01", 1);
break;
default:
if ((bbio_subcommand & BBIO_AUX_MASK) == BBIO_AUX_MASK) {
cprintf(con, "%c", bbio_aux(con, bbio_subcommand));
Expand All @@ -78,8 +90,16 @@ void bbio_mode_onewire(t_hydra_console *con)
}
cprint(con, "\x01", 1);
} else if ((bbio_subcommand & BBIO_ONEWIRE_CONFIG_PERIPH) == BBIO_ONEWIRE_CONFIG_PERIPH) {
proto->config.onewire.dev_gpio_pull = (bbio_subcommand & 0b100)?1:0;
status = onewire_pin_init(con);
if(bbio_subcommand & 0b1000) {
onewire_init_proto_swio(con);
proto->config.onewire.dev_gpio_pull = (bbio_subcommand & 0b100)?1:0;
status = onewire_pin_init(con);
onewire_swio_reset(con);
} else {
onewire_init_proto_default(con);
proto->config.onewire.dev_gpio_pull = (bbio_subcommand & 0b100)?1:0;
status = onewire_pin_init(con);
}
//Set AUX[0] (PC4) value
bbio_aux_write((bbio_subcommand & 0b10)>>1);

Expand Down
194 changes: 194 additions & 0 deletions src/hydrabus/hydrabus_mode_onewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ void onewire_init_proto_default(t_hydra_console *con)
proto->config.onewire.dev_bit_lsb_msb = DEV_FIRSTBIT_LSB;
}

void onewire_init_proto_swio(t_hydra_console *con)
{
mode_config_proto_t* proto = &con->mode->proto;

/* Defaults */
proto->dev_num = 0;
proto->config.onewire.dev_gpio_mode = MODE_CONFIG_DEV_GPIO_OUT_PUSHPULL;
proto->config.onewire.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_NOPULL;
proto->config.onewire.dev_bit_lsb_msb = DEV_FIRSTBIT_MSB;
}

static void show_params(t_hydra_console *con)
{
mode_config_proto_t* proto = &con->mode->proto;
Expand Down Expand Up @@ -388,6 +399,182 @@ static int init(t_hydra_console *con, t_tokenline_parsed *p)
return tokens_used;
}

/* Around 250us */
static void onewire_swio_delay_250(void)
{
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
}

void onewire_swio_reset(t_hydra_console *con)
{
(void) con;
onewire_low();
DelayMs(20);
onewire_high();
}

void onewire_swio_write_bit(t_hydra_console *con, uint8_t bit)
{
onewire_mode_output(con);

if(bit) {
onewire_low();
onewire_swio_delay_250();
onewire_high();
onewire_swio_delay_250();
onewire_swio_delay_250();
onewire_swio_delay_250();
} else {
onewire_low();
onewire_swio_delay_250();
onewire_swio_delay_250();
onewire_swio_delay_250();
onewire_swio_delay_250();
onewire_high();
}
}

uint8_t onewire_swio_read_bit(t_hydra_console *con)
{
uint8_t bit=0;

onewire_mode_output(con);
onewire_low();
onewire_swio_delay_250();
onewire_mode_input(con);
onewire_swio_delay_250();
onewire_swio_delay_250();
bit = bsp_gpio_pin_read(BSP_GPIO_PORTB, ONEWIRE_PIN);
while (0 == bsp_gpio_pin_read(BSP_GPIO_PORTB, ONEWIRE_PIN)){};
onewire_high();
onewire_mode_output(con);
return bit;
}

uint32_t onewire_swio_read_reg(t_hydra_console *con, uint8_t address)
{
uint32_t result = 0;
uint8_t i;

// start bit
onewire_swio_write_bit(con, 1);

for (i=0; i<7; i++) {
onewire_swio_write_bit(con, (address<<i) & 0x40);
}

// Read command
onewire_swio_write_bit(con, 0);

for (i=0; i<32; i++) {
result = result << 1;
result |= onewire_swio_read_bit(con);
}

DelayUs(10);

return result;

}

void onewire_swio_write_reg(t_hydra_console *con, uint8_t address, uint32_t value)
{
uint8_t i;

// start bit
onewire_swio_write_bit(con, 1);

for (i=0; i<7; i++) {
onewire_swio_write_bit(con, (address<<i) & 0x40);
}

// Write command
onewire_swio_write_bit(con, 1);

for (i=0; i<32; i++) {
onewire_swio_write_bit(con, ((value<<i) & 0x80000000) != 0);
}

DelayUs(10);
}

void onewire_swio_debug(t_hydra_console *con)
{
uint32_t value;
uint8_t command, reg;

cprintf(con, "Interrupt by pressing user button.\r\n");
cprint(con, "\r\n", 2);

while (!hydrabus_ubtn()) {
if(chnReadTimeout(con->sdu, &command, 1, 1)) {
switch(command) {
case '?':
// TODO
cprint(con, "+", 1);
break;
case 'p':
case 'P':
// TODO
cprint(con, "+", 1);
break;
case 'w':
if(chnRead(con->sdu, &reg, 1) == 1) {
if(chnRead(con->sdu, (uint8_t *)&value, 4) == 4) {
onewire_swio_write_reg(con, reg, value);
}
}
cprint(con, "+", 1);
break;
case 'r':
if(chnRead(con->sdu, &reg, 1) == 1) {
value = onewire_swio_read_reg(con, reg);
cprint(con, (void *)&value, 4);
}
break;
default:
cprint(con, "+", 1);
break;
}
}
}
}

static int exec(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
{
mode_config_proto_t* proto = &con->mode->proto;
Expand Down Expand Up @@ -421,6 +608,13 @@ static int exec(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
case T_SCAN:
onewire_scan(con);
break;
case T_DEBUG:
onewire_init_proto_swio(con);
onewire_pin_init(con);
onewire_swio_debug(con);
onewire_init_proto_default(con);
onewire_pin_init(con);
break;
default:
return t - token_pos;
}
Expand Down
7 changes: 7 additions & 0 deletions src/hydrabus/hydrabus_mode_onewire.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


void onewire_init_proto_default(t_hydra_console *con);
void onewire_init_proto_swio(t_hydra_console *con);
bool onewire_pin_init(t_hydra_console *con);
uint8_t onewire_read_u8(t_hydra_console *con);
void onewire_write_u8(t_hydra_console *con, uint8_t tx_data);
Expand All @@ -38,6 +39,12 @@ void onewire_send_bit(t_hydra_console *con, uint8_t bit);
uint8_t onewire_read_bit(t_hydra_console *con);
void onewire_cleanup(t_hydra_console *con);
void onewire_start(t_hydra_console *con);
void onewire_swio_write_bit(t_hydra_console *con, uint8_t bit);
void onewire_swio_reset(t_hydra_console *con);
uint8_t onewire_swio_read_bit(t_hydra_console *con);
uint32_t onewire_swio_read_reg(t_hydra_console *con, uint8_t address);
void onewire_swio_write_reg(t_hydra_console *con, uint8_t address, uint32_t value);
void onewire_swio_debug(t_hydra_console *con);

struct onewire_scan_state {
uint8_t ROM_ADDR[8];
Expand Down

0 comments on commit a2d1f57

Please sign in to comment.