Skip to content

Commit

Permalink
Initial 1-wire support (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baldanos authored and bvernoux committed Nov 7, 2016
1 parent a2aab9d commit e79c160
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 2 deletions.
1 change: 1 addition & 0 deletions common/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ static struct cmd_map {
{ T_SUMP, cmd_sump },
{ T_JTAG, cmd_mode_init },
{ T_RNG, cmd_rng },
{ T_ONEWIRE, cmd_mode_init },
{ T_TWOWIRE, cmd_mode_init },
{ T_THREEWIRE, cmd_mode_init },
{ 0, NULL }
Expand Down
99 changes: 99 additions & 0 deletions hydrabus/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ t_token_dict tl_dict[] = {
{ T_THREEWIRE, "3-wire" },
{ T_SCRIPT, "script" },
{ T_FILE, "filename" },
{ T_ONEWIRE, "1-wire" },

{ T_LEFT_SQ, "[" },
{ T_RIGHT_SQ, "]" },
Expand Down Expand Up @@ -879,6 +880,99 @@ t_token tokens_jtag[] = {
{ }
};

#define ONEWIRE_PARAMETERS \
{ T_DEVICE, \
.arg_type = T_ARG_UINT, \
.help = "1-wire device (1)" }, \
{ T_PULL, \
.arg_type = T_ARG_TOKEN, \
.subtokens = tokens_gpio_pull, \
.help = "GPIO pull (up/down/floating)" }, \
{ T_MSB_FIRST, \
.help = "Send/receive MSB first" }, \
{ T_LSB_FIRST, \
.help = "Send/receive LSB first" },

t_token tokens_mode_onewire[] = {
{
T_SHOW,
.subtokens = tokens_mode_show,
.help = "Show 1-wire parameters"
},
ONEWIRE_PARAMETERS
/* 1-wire-specific commands */
{
T_SCAN,
.help = "Scan for connected devices"
},
{
T_READ,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Read byte (repeat with :<num>)"
},
{
T_HD,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Read byte (repeat with :<num>) and print hexdump"
},
{
T_WRITE,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Write byte (repeat with :<num>)"
},
{
T_ARG_UINT,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Write byte (repeat with :<num>)"
},
{
T_ARG_STRING,
.help = "Write string"
},
/* BP commands */
{
T_LEFT_SQ,
.help = "Reset bus"
},
{
T_MINUS,
.help = "Toggle pin high"
},
{
T_UNDERSCORE,
.help = "Toggle pin low"
},
{
T_DOT,
.help = "Read bit"
},
{
T_AMPERSAND,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Delay 1 usec (repeat with :<num>)"
},
{
T_PERCENT,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Delay 1 msec (repeat with :<num>)"
},
{
T_TILDE,
.flags = T_FLAG_SUFFIX_TOKEN_DELIM_INT,
.help = "Write a random byte (repeat with :<num>)"
},
{
T_EXIT,
.help = "Exit 1-wire mode"
},
{ }
};

t_token tokens_onewire[] = {
ONEWIRE_PARAMETERS
{ }
};

#define TWOWIRE_PARAMETERS \
{ T_DEVICE, \
.arg_type = T_ARG_UINT, \
Expand Down Expand Up @@ -1443,6 +1537,11 @@ t_token tl_tokens[] = {
.help = "I2C mode",
.help_full = "Configuration: i2c [pull (up/down/floating)] [frequency (value hz/khz/mhz)]\r\nInteraction: [<start>] [<stop>] <read/write (value:repeat)>"
},
{
T_ONEWIRE,
.subtokens = tokens_onewire,
.help = "1-wire mode"
},
{
T_TWOWIRE,
.subtokens = tokens_twowire,
Expand Down
1 change: 1 addition & 0 deletions hydrabus/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ enum {
T_THREEWIRE,
T_SCRIPT,
T_FILE,
T_ONEWIRE,

/* BP-compatible commands */
T_LEFT_SQ,
Expand Down
4 changes: 3 additions & 1 deletion hydrabus/hydrabus.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ HYDRABUSSRC = hydrabus/hydrabus.c \
hydrabus/hydrabus_sump.c \
hydrabus/hydrabus_mode_jtag.c \
hydrabus/hydrabus_rng.c \
hydrabus/hydrabus_mode_onewire.c \
hydrabus/hydrabus_mode_twowire.c \
hydrabus/hydrabus_mode_threewire.c \
hydrabus/hydrabus_mode_can.c \
Expand All @@ -22,7 +23,8 @@ HYDRABUSSRC = hydrabus/hydrabus.c \
hydrabus/hydrabus_bbio_uart.c \
hydrabus/hydrabus_bbio_i2c.c \
hydrabus/hydrabus_bbio_rawwire.c \
hydrabus/hydrabus_freq.c
hydrabus/hydrabus_freq.c \
hydrabus/hydrabus_bbio_onewire.c

# Required include directories
HYDRABUSINC = ./hydrabus
3 changes: 2 additions & 1 deletion hydrabus/hydrabus_bbio.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "hydrabus_bbio_uart.h"
#include "hydrabus_bbio_i2c.h"
#include "hydrabus_bbio_rawwire.h"
#include "hydrabus_bbio_onewire.h"

int cmd_bbio(t_hydra_console *con)
{
Expand All @@ -55,7 +56,7 @@ int cmd_bbio(t_hydra_console *con)
break;
case BBIO_1WIRE:
cprint(con, "1W01", 4);
//TODO
bbio_mode_onewire(con);
break;
case BBIO_RAWWIRE:
cprint(con, "RAW1", 4);
Expand Down
7 changes: 7 additions & 0 deletions hydrabus/hydrabus_bbio.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,11 @@
#define BBIO_RAWWIRE_SET_SPEED 0b01100000
#define BBIO_RAWWIRE_CONFIG 0b10000000

/*
* 1-Wire-specific commands
*/
#define BBIO_ONEWIRE_RESET 0b00000010
#define BBIO_ONEWIRE_READ 0b00000100
#define BBIO_ONEWIRE_BULK_TRANSFER 0b00010000

int cmd_bbio(t_hydra_console *con);
68 changes: 68 additions & 0 deletions hydrabus/hydrabus_bbio_onewire.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2014-2016 Benjamin VERNOUX
* Copyright (C) 2016 Nicolas OBERLI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "common.h"
#include "tokenline.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "hydrabus_bbio.h"
#include "hydrabus_bbio_onewire.h"
#include "hydrabus_mode_onewire.h"


void bbio_mode_onewire(t_hydra_console *con)
{
uint8_t bbio_subcommand, i;
uint8_t rx_data[16], tx_data[16];
uint8_t data;

onewire_init_proto_default(con);
onewire_pin_init(con);

while (!USER_BUTTON) {
if(chnRead(con->sdu, &bbio_subcommand, 1) == 1) {
switch(bbio_subcommand) {
case BBIO_RESET:
onewire_cleanup(con);
return;
case BBIO_ONEWIRE_RESET:
onewire_start(con);
case BBIO_ONEWIRE_READ:
rx_data[0] = onewire_read_u8(con);
cprint(con, (char *)&rx_data[0], 1);
break;
default:
if ((bbio_subcommand & BBIO_ONEWIRE_BULK_TRANSFER) == BBIO_ONEWIRE_BULK_TRANSFER) {
// data contains the number of bytes to
// write
data = (bbio_subcommand & 0b1111) + 1;

chnRead(con->sdu, tx_data, data);
for(i=0; i<data; i++) {
onewire_write_u8(con, tx_data[i]);
}
cprint(con, "\x01", 1);
}
}
}
}
onewire_cleanup(con);
}
19 changes: 19 additions & 0 deletions hydrabus/hydrabus_bbio_onewire.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2016 Nicolas OBERLI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

void bbio_mode_onewire(t_hydra_console *con);
3 changes: 3 additions & 0 deletions hydrabus/hydrabus_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern const mode_exec_t mode_i2c_exec;
extern const mode_exec_t mode_uart_exec;
extern const mode_exec_t mode_nfc_exec;
extern const mode_exec_t mode_jtag_exec;
extern const mode_exec_t mode_onewire_exec;
extern const mode_exec_t mode_twowire_exec;
extern const mode_exec_t mode_threewire_exec;
extern const mode_exec_t mode_can_exec;
Expand All @@ -48,6 +49,7 @@ extern t_token tokens_mode_i2c[];
extern t_token tokens_mode_uart[];
extern t_token tokens_mode_nfc[];
extern t_token tokens_mode_jtag[];
extern t_token tokens_mode_onewire[];
extern t_token tokens_mode_twowire[];
extern t_token tokens_mode_threewire[];
extern t_token tokens_mode_can[];
Expand All @@ -62,6 +64,7 @@ static struct {
{ T_UART, tokens_mode_uart, &mode_uart_exec },
{ T_NFC, tokens_mode_nfc, &mode_nfc_exec },
{ T_JTAG, tokens_mode_jtag, &mode_jtag_exec },
{ T_ONEWIRE, tokens_mode_onewire, &mode_onewire_exec },
{ T_TWOWIRE, tokens_mode_twowire, &mode_twowire_exec },
{ T_THREEWIRE, tokens_mode_threewire, &mode_threewire_exec },
{ T_CAN, tokens_mode_can, &mode_can_exec },
Expand Down
Loading

0 comments on commit e79c160

Please sign in to comment.