Skip to content

Commit

Permalink
Add BBIO_I2C_CLK_STRETCH command
Browse files Browse the repository at this point in the history
  • Loading branch information
bvernoux committed Jan 2, 2023
1 parent c3d97ee commit ebf7329
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 84 deletions.
149 changes: 75 additions & 74 deletions src/hydrabus/hydrabus_bbio.h
Expand Up @@ -3,7 +3,7 @@
*
* Copyright (C) 2020 Guillaume VINET
* Copyright (C) 2015-2016 Nicolas OBERLI
* Copyright (C) 2016 Benjamin VERNOUX
* Copyright (C) 2016-2022 Benjamin VERNOUX
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,78 +23,79 @@
/*
* Basic BBIO modes
*/
#define BBIO_RESET 0b00000000
#define BBIO_SPI 0b00000001
#define BBIO_I2C 0b00000010
#define BBIO_UART 0b00000011
#define BBIO_1WIRE 0b00000100
#define BBIO_RESET 0b00000000
#define BBIO_SPI 0b00000001
#define BBIO_I2C 0b00000010
#define BBIO_UART 0b00000011
#define BBIO_1WIRE 0b00000100
#define BBIO_RAWWIRE 0b00000101
#define BBIO_JTAG 0b00000110
#define BBIO_JTAG 0b00000110

//Common modes command
#define BBIO_MODE_ID 0b00000001

//Hydrabus specific
#define BBIO_CAN 0b00001000
#define BBIO_PIN 0b00001001
#define BBIO_FLASH 0b00001010
#define BBIO_CAN 0b00001000
#define BBIO_PIN 0b00001001
#define BBIO_FLASH 0b00001010
#define BBIO_SMARTCARD 0b00001011
#define BBIO_NFC_READER 0b00001100
#define BBIO_MMC 0b00001101
#define BBIO_MMC 0b00001101

#define BBIO_RESET_HW 0b00001111
#define BBIO_PWM 0b00010010
#define BBIO_PWM 0b00010010
#define BBIO_PWM_CLEAR 0b00010010
#define BBIO_VOLT 0b00010100
#define BBIO_VOLT 0b00010100
#define BBIO_VOLT_CONT 0b00010101
#define BBIO_FREQ 0b00010110
#define BBIO_FREQ 0b00010110

/*
* SPI-specific commands
*/
#define BBIO_SPI_CS_LOW 0b00000010
#define BBIO_SPI_CS_HIGH 0b00000011
#define BBIO_SPI_WRITE_READ 0b00000100
#define BBIO_SPI_CS_LOW 0b00000010
#define BBIO_SPI_CS_HIGH 0b00000011
#define BBIO_SPI_WRITE_READ 0b00000100
#define BBIO_SPI_WRITE_READ_NCS 0b00000101
#define BBIO_SPI_SNIFF_ALL 0b00001101
#define BBIO_SPI_SNIFF_ALL 0b00001101
#define BBIO_SPI_SNIFF_CS_LOW 0b00001110
#define BBIO_SPI_SNIFF_CS_HIGH 0b00001111
#define BBIO_SPI_SNIFF_CS_HIGH 0b00001111
#define BBIO_SPI_BULK_TRANSFER 0b00010000
#define BBIO_SPI_CONFIG_PERIPH 0b01000000
#define BBIO_SPI_SET_SPEED 0b01100000
#define BBIO_SPI_CONFIG 0b10000000
#define BBIO_SPI_AVR 0b00000110
#define BBIO_SPI_AVR_NULL 0b00000000
#define BBIO_SPI_SET_SPEED 0b01100000
#define BBIO_SPI_CONFIG 0b10000000
#define BBIO_SPI_AVR 0b00000110
#define BBIO_SPI_AVR_NULL 0b00000000
#define BBIO_SPI_AVR_VERSION 0b00000001
#define BBIO_SPI_AVR_READ 0b00000010
#define BBIO_SPI_AVR_READ 0b00000010

/*
* I2C-specific commands
*/
#define BBIO_I2C_START_BIT 0b00000010
#define BBIO_I2C_STOP_BIT 0b00000011
#define BBIO_I2C_READ_BYTE 0b00000100
#define BBIO_I2C_ACK_BIT 0b00000110
#define BBIO_I2C_NACK_BIT 0b00000111
#define BBIO_I2C_WRITE_READ 0b00001000
#define BBIO_I2C_START_BIT 0b00000010
#define BBIO_I2C_STOP_BIT 0b00000011
#define BBIO_I2C_READ_BYTE 0b00000100
#define BBIO_I2C_ACK_BIT 0b00000110
#define BBIO_I2C_NACK_BIT 0b00000111
#define BBIO_I2C_WRITE_READ 0b00001000
#define BBIO_I2C_START_SNIFF 0b00001111
#define BBIO_I2C_BULK_WRITE 0b00010000
#define BBIO_I2C_BULK_WRITE 0b00010000
#define BBIO_I2C_CLK_STRETCH 0b00100000
#define BBIO_I2C_CONFIG_PERIPH 0b01000000
#define BBIO_I2C_SET_SPEED 0b01100000
#define BBIO_I2C_SET_SPEED 0b01100000

/*
* CAN-specific commands
*/
#define BBIO_CAN_READ 0b00000010
#define BBIO_CAN_ID 0b00000011
#define BBIO_CAN_FILTER_OFF 0b00000100
#define BBIO_CAN_FILTER_ON 0b00000101
#define BBIO_CAN_FILTER 0b00000110
#define BBIO_CAN_WRITE 0b00001000
#define BBIO_CAN_READ 0b00000010
#define BBIO_CAN_ID 0b00000011
#define BBIO_CAN_FILTER_OFF 0b00000100
#define BBIO_CAN_FILTER_ON 0b00000101
#define BBIO_CAN_FILTER 0b00000110
#define BBIO_CAN_WRITE 0b00001000
#define BBIO_CAN_SET_TIMINGS 0b00010000
#define BBIO_CAN_SET_SPEED 0b01100000
#define BBIO_CAN_SLCAN 0b10100000
#define BBIO_CAN_SET_SPEED 0b01100000
#define BBIO_CAN_SLCAN 0b10100000

/*
* PIN control-specific commands
Expand All @@ -110,49 +111,49 @@
* UART-specific commands
*/
#define BBIO_UART_START_ECHO 0b00000010
#define BBIO_UART_STOP_ECHO 0b00000011
#define BBIO_UART_BAUD_RATE 0b00000111
#define BBIO_UART_BRIDGE 0b00001111
#define BBIO_UART_STOP_ECHO 0b00000011
#define BBIO_UART_BAUD_RATE 0b00000111
#define BBIO_UART_BRIDGE 0b00001111
#define BBIO_UART_BULK_TRANSFER 0b00010000
#define BBIO_UART_CONFIG_PERIPH 0b01000000
#define BBIO_UART_SET_SPEED 0b01100000
#define BBIO_UART_CONFIG 0b10000000
#define BBIO_UART_SET_SPEED 0b01100000
#define BBIO_UART_CONFIG 0b10000000

/*
* 2-Wire-specific commands
*/
#define BBIO_RAWWIRE_I2C_START 0b00000010
#define BBIO_RAWWIRE_I2C_STOP 0b00000011
#define BBIO_RAWWIRE_CS_LOW 0b00000100
#define BBIO_RAWWIRE_CS_HIGH 0b00000101
#define BBIO_RAWWIRE_READ_BYTE 0b00000110
#define BBIO_RAWWIRE_READ_BIT 0b00000111
#define BBIO_RAWWIRE_PEEK_INPUT 0b00001000
#define BBIO_RAWWIRE_CLK_TICK 0b00001001
#define BBIO_RAWWIRE_CLK_LOW 0b00001010
#define BBIO_RAWWIRE_CLK_HIGH 0b00001011
#define BBIO_RAWWIRE_DATA_LOW 0b00001100
#define BBIO_RAWWIRE_DATA_HIGH 0b00001101
#define BBIO_RAWWIRE_BULK_TRANSFER 0b00010000
#define BBIO_RAWWIRE_BULK_CLK 0b00100000
#define BBIO_RAWWIRE_BULK_BIT 0b00110000
#define BBIO_RAWWIRE_CONFIG_PERIPH 0b01000000
#define BBIO_RAWWIRE_SET_SPEED 0b01100000
#define BBIO_RAWWIRE_CONFIG 0b10000000
#define BBIO_RAWWIRE_I2C_START 0b00000010
#define BBIO_RAWWIRE_I2C_STOP 0b00000011
#define BBIO_RAWWIRE_CS_LOW 0b00000100
#define BBIO_RAWWIRE_CS_HIGH 0b00000101
#define BBIO_RAWWIRE_READ_BYTE 0b00000110
#define BBIO_RAWWIRE_READ_BIT 0b00000111
#define BBIO_RAWWIRE_PEEK_INPUT 0b00001000
#define BBIO_RAWWIRE_CLK_TICK 0b00001001
#define BBIO_RAWWIRE_CLK_LOW 0b00001010
#define BBIO_RAWWIRE_CLK_HIGH 0b00001011
#define BBIO_RAWWIRE_DATA_LOW 0b00001100
#define BBIO_RAWWIRE_DATA_HIGH 0b00001101
#define BBIO_RAWWIRE_BULK_TRANSFER 0b00010000
#define BBIO_RAWWIRE_BULK_CLK 0b00100000
#define BBIO_RAWWIRE_BULK_BIT 0b00110000
#define BBIO_RAWWIRE_CONFIG_PERIPH 0b01000000
#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
#define BBIO_ONEWIRE_CONFIG_PERIPH 0b01000000
#define BBIO_ONEWIRE_RESET 0b00000010
#define BBIO_ONEWIRE_READ 0b00000100
#define BBIO_ONEWIRE_BULK_TRANSFER 0b00010000
#define BBIO_ONEWIRE_CONFIG_PERIPH 0b01000000

/*
* Flash-specific commands
*/
#define BBIO_FLASH_EN_LOW 0b00000010
#define BBIO_FLASH_EN_HIGH 0b00000011
#define BBIO_FLASH_EN_LOW 0b00000010
#define BBIO_FLASH_EN_HIGH 0b00000011
#define BBIO_FLASH_WRITE_READ 0b00000100
#define BBIO_FLASH_WRITE_BYTE 0b00000101
#define BBIO_FLASH_WRITE_CMD 0b00000110
Expand All @@ -170,7 +171,7 @@
#define BBIO_SMARTCARD_WRITE_READ 0b00000100
#define BBIO_SMARTCARD_PRESCALER 0b00000110
#define BBIO_SMARTCARD_GUARDTIME 0b00000111
#define BBIO_SMARTCARD_ATR 0b00001000
#define BBIO_SMARTCARD_ATR 0b00001000
#define BBIO_SMARTCARD_SET_SPEED 0b01100000
#define BBIO_SMARTCARD_CONFIG 0b10000000

Expand All @@ -186,12 +187,12 @@
/*
* Hydra NFC specific comamnds
*/
#define BBIO_NFC_RF_OFF 0b00000010
#define BBIO_NFC_RF_ON 0b00000011
#define BBIO_NFC_CMD_SEND_BITS 0b00000100
#define BBIO_NFC_CMD_SEND_BYTES 0b00000101
#define BBIO_NFC_RF_OFF 0b00000010
#define BBIO_NFC_RF_ON 0b00000011
#define BBIO_NFC_CMD_SEND_BITS 0b00000100
#define BBIO_NFC_CMD_SEND_BYTES 0b00000101
#define BBIO_NFC_SET_MODE_ISO_14443A 0b00000110
#define BBIO_NFC_SET_MODE_ISO_15693 0b00000111
#define BBIO_NFC_SET_MODE_ISO_15693 0b00000111

/*
* MMC-specific commands
Expand Down
58 changes: 48 additions & 10 deletions src/hydrabus/hydrabus_bbio_i2c.c
Expand Up @@ -2,7 +2,7 @@
* HydraBus/HydraNFC
*
* Copyright (C) 2015-2016 Nicolas OBERLI
* Copyright (C) 2016 Benjamin VERNOUX
* Copyright (C) 2016-2023 Benjamin VERNOUX
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,7 @@ void bbio_i2c_init_proto_default(t_hydra_console *con)
proto->config.i2c.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_PULLUP;
proto->config.i2c.dev_speed = 1;
proto->config.i2c.ack_pending = 0;
proto->config.i2c.dev_clock_stretch_timeout = 0;
}

void bbio_i2c_sniff(t_hydra_console *con)
Expand Down Expand Up @@ -91,6 +92,7 @@ void bbio_mode_i2c(t_hydra_console *con)
uint8_t *rx_data = pool_alloc_bytes(0x1000); // 4096 bytes
uint8_t data;
uint8_t tx_ack_flag;
uint32_t clock_stretch_timeout;
bsp_status_t status;
mode_config_proto_t* proto = &con->mode->proto;

Expand Down Expand Up @@ -125,6 +127,7 @@ void bbio_mode_i2c(t_hydra_console *con)
cprint(con, "\x01", 1);
break;
case BBIO_I2C_READ_BYTE:
data = 0;
status = bsp_i2c_master_read_u8(proto->dev_num, &data);
cprint(con, (char *)&data, 1);
break;
Expand Down Expand Up @@ -155,13 +158,13 @@ void bbio_mode_i2c(t_hydra_console *con)
/* Send all I2C Data */
for(i = 0; i < to_tx; i++)
{
bsp_i2c_master_write_u8(proto->dev_num, tx_data[i], &tx_ack_flag);
if(tx_ack_flag != TRUE)
status = bsp_i2c_master_write_u8(proto->dev_num, tx_data[i], &tx_ack_flag);
if((status != BSP_OK) || (tx_ack_flag != TRUE))
{
break; /* Error */
}
}
if(tx_ack_flag != TRUE)
if((status != BSP_OK) || (tx_ack_flag != TRUE))
{
/* Error */
cprint(con, "\x00", 1);
Expand All @@ -174,13 +177,38 @@ void bbio_mode_i2c(t_hydra_console *con)
/* Read I2C bytes with ACK */
for(i = 0; i < to_rx - 1; i++)
{
bsp_i2c_master_read_u8(proto->dev_num, &rx_data[i]);
bsp_i2c_read_ack(proto->dev_num, TRUE);
status = bsp_i2c_master_read_u8(proto->dev_num, &rx_data[i]);
if(status != BSP_OK)
{
break; /* Return now */
}
status = bsp_i2c_read_ack(proto->dev_num, TRUE);
if(status != BSP_OK)
{
break; /* Return now */
}
}
if(status != BSP_OK)
{
/* Error */
cprint(con, "\x00", 1);
break; /* Return now */
}

/* Send NACK for last I2C byte read */
bsp_i2c_master_read_u8(proto->dev_num, &rx_data[i]);
bsp_i2c_read_ack(proto->dev_num, FALSE);
status = bsp_i2c_master_read_u8(proto->dev_num, &rx_data[i]);
if(status != BSP_OK)
{
/* Error */
cprint(con, "\x00", 1);
break; /* Return now */
}
status = bsp_i2c_read_ack(proto->dev_num, FALSE);
if(status != BSP_OK)
{
/* Error */
cprint(con, "\x00", 1);
break; /* Return now */
}
}

/* Send I2C Stop */
Expand All @@ -189,6 +217,17 @@ void bbio_mode_i2c(t_hydra_console *con)
cprint(con, "\x01", 1);
cprint(con, (char *)rx_data, to_rx);
break;
case BBIO_I2C_CLK_STRETCH:
chnRead(con->sdu, rx_data, 4);
clock_stretch_timeout = (rx_data[0] << 24) + (rx_data[1] << 16) + (rx_data[2] << 8) + rx_data[3];
proto->config.i2c.dev_clock_stretch_timeout = clock_stretch_timeout;
status = bsp_i2c_master_init(proto->dev_num, proto);
if(status == BSP_OK) {
cprint(con, "\x01", 1);
} else {
cprint(con, "\x00", 1);
}
break;
default:
if ((bbio_subcommand & BBIO_AUX_MASK) == BBIO_AUX_MASK) {
cprintf(con, "%c", bbio_aux(con, bbio_subcommand));
Expand Down Expand Up @@ -224,7 +263,6 @@ void bbio_mode_i2c(t_hydra_console *con)
status = bsp_i2c_master_init(proto->dev_num, proto);
//Set AUX[0] (PC4) value
bbio_aux_write((bbio_subcommand & 0b10)>>1);

if(status == BSP_OK) {
cprint(con, "\x01", 1);
} else {
Expand Down

0 comments on commit ebf7329

Please sign in to comment.