Skip to content

Commit

Permalink
Add read with timeout feature (#165)
Browse files Browse the repository at this point in the history
Add support for timeout read in the following modes
 * UART
 * LIN
 * SMARTCARD

* Add read with timeout in UART mode
* Add timedout read on smartcard mode
* Add timeout command
* Add timeout read feature for hexdump mode
  • Loading branch information
Baldanos authored Mar 30, 2024
1 parent 34e3d63 commit 80e8bb7
Show file tree
Hide file tree
Showing 21 changed files with 188 additions and 115 deletions.
1 change: 1 addition & 0 deletions src/common/mode_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ typedef struct {
#define MODE_CONFIG_PROTO_BUFFER_SIZE (256)
typedef struct {
uint8_t dev_num;
uint32_t timeout;
union {
uart_config_t uart;
smartcard_config_t smartcard;
Expand Down
42 changes: 13 additions & 29 deletions src/drv/stm32cube/bsp_smartcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,49 +243,33 @@ bsp_status_t bsp_smartcard_write_u8(bsp_dev_smartcard_t dev_num, uint8_t* tx_dat
* @param dev_num: SMARTCARD dev num.
* @param rx_data: Data to receive.
* @param nb_data: Number of data to receive.
* @retval status of the transfer.
* @param timeout: Number of miliseconds to wait
* @retval status of the transfer. nb_data will contain the number of read
* bytes
*/
bsp_status_t bsp_smartcard_read_u8(bsp_dev_smartcard_t dev_num, uint8_t* rx_data, uint8_t nb_data)
bsp_status_t bsp_smartcard_read_u8(bsp_dev_smartcard_t dev_num, uint8_t* rx_data, uint8_t *nb_data, uint32_t timeout)
{
SMARTCARD_HandleTypeDef* hsmartcard;
hsmartcard = &smartcard_handle[dev_num];

bsp_status_t status;
__HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
status = (bsp_status_t) HAL_SMARTCARD_Receive(hsmartcard, rx_data, nb_data, SMARTCARDx_TIMEOUT_MAX);

if(status != BSP_OK) {
smartcard_error(dev_num);
}
return status;
}

/**
* @brief Read bytes in blocking mode, with timeout
* @param dev_num: SMARTCARD dev num.
* @param rx_data: Data to receive.
* @param nb_data: Number of data to receive.
* @param timeout: Number of ticks to wait
* @retval Number of bytes read
*/
bsp_status_t bsp_smartcard_read_u8_timeout(bsp_dev_smartcard_t dev_num, uint8_t* rx_data,
uint8_t nb_data, uint32_t timeout)
{
SMARTCARD_HandleTypeDef* hsmartcard;
hsmartcard = &smartcard_handle[dev_num];
status = (bsp_status_t) HAL_SMARTCARD_Receive(hsmartcard, rx_data, *nb_data, timeout);

bsp_status_t status;
__HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
status = (bsp_status_t) HAL_SMARTCARD_Receive(hsmartcard, rx_data, nb_data, timeout);
switch(status) {
switch(status){
case BSP_OK:
*nb_data = hsmartcard->RxXferSize;
break;
case BSP_TIMEOUT:
return (nb_data-(hsmartcard->RxXferCount)-1);
*nb_data = hsmartcard->RxXferSize - hsmartcard->RxXferCount - 1;
break;
case BSP_ERROR:
default:
*nb_data = 0;
smartcard_error(dev_num);
return 0;
}

return status;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/drv/stm32cube/bsp_smartcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ bsp_status_t bsp_smartcard_init(bsp_dev_smartcard_t dev_num, mode_config_proto_t
bsp_status_t bsp_smartcard_deinit(bsp_dev_smartcard_t dev_num);

bsp_status_t bsp_smartcard_write_u8(bsp_dev_smartcard_t dev_num, uint8_t* tx_data, uint8_t nb_data);
bsp_status_t bsp_smartcard_read_u8(bsp_dev_smartcard_t dev_num, uint8_t* rx_data, uint8_t nb_data);
bsp_status_t bsp_smartcard_read_u8(bsp_dev_smartcard_t dev_num, uint8_t* rx_data, uint8_t *nb_data, uint32_t timeout);

bsp_status_t bsp_smartcard_read_u8_timeout(bsp_dev_smartcard_t dev_num, uint8_t* rx_data, uint8_t nb_data, uint32_t timeout);
bsp_status_t bsp_smartcard_write_read_u8(bsp_dev_smartcard_t dev_num, uint8_t* tx_data, uint8_t* rx_data, uint8_t nb_data);
bsp_status_t bsp_smartcard_rxne(bsp_dev_smartcard_t dev_num);

Expand Down
38 changes: 11 additions & 27 deletions src/drv/stm32cube/bsp_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,46 +258,30 @@ bsp_status_t bsp_uart_write_u8(bsp_dev_uart_t dev_num, uint8_t* tx_data, uint8_t
* @param dev_num: UART dev num.
* @param rx_data: Data to receive.
* @param nb_data: Number of data to receive.
* @retval status of the transfer.
*/
bsp_status_t bsp_uart_read_u8(bsp_dev_uart_t dev_num, uint8_t* rx_data, uint8_t nb_data)
{
UART_HandleTypeDef* huart;
huart = &uart_handle[dev_num];

bsp_status_t status;
status = (bsp_status_t) HAL_UART_Receive(huart, rx_data, nb_data, UARTx_TIMEOUT_MAX);
if(status != BSP_OK) {
uart_error(dev_num);
}
return status;
}

/**
* @brief Read bytes in blocking mode, with timeout
* @param dev_num: UART dev num.
* @param rx_data: Data to receive.
* @param nb_data: Number of data to receive.
* @param timeout: Number of ticks to wait
* @retval Number of bytes read
* @param timeout: Number of miliseconds to wait
* @retval status of the transfer. nb_data will contain the number of read
* bytes
*/
bsp_status_t bsp_uart_read_u8_timeout(bsp_dev_uart_t dev_num, uint8_t* rx_data,
uint8_t nb_data, uint32_t timeout)
bsp_status_t bsp_uart_read_u8(bsp_dev_uart_t dev_num, uint8_t* rx_data, uint8_t *nb_data, uint32_t timeout)
{
UART_HandleTypeDef* huart;
huart = &uart_handle[dev_num];

bsp_status_t status;
status = (bsp_status_t) HAL_UART_Receive(huart, rx_data, nb_data, timeout);
status = (bsp_status_t) HAL_UART_Receive(huart, rx_data, *nb_data, timeout);
switch(status){
case BSP_OK:
*nb_data = huart->RxXferSize;
break;
case BSP_TIMEOUT:
return (nb_data-(huart->RxXferCount)-1);
*nb_data = huart->RxXferSize - huart->RxXferCount - 1;
break;
case BSP_ERROR:
default:
*nb_data = 0;
uart_error(dev_num);
return 0;
}
return status;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/drv/stm32cube/bsp_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ bsp_status_t bsp_uart_init(bsp_dev_uart_t dev_num, mode_config_proto_t* mode_con
bsp_status_t bsp_uart_deinit(bsp_dev_uart_t dev_num);

bsp_status_t bsp_uart_write_u8(bsp_dev_uart_t dev_num, uint8_t* tx_data, uint8_t nb_data);
bsp_status_t bsp_uart_read_u8(bsp_dev_uart_t dev_num, uint8_t* rx_data, uint8_t nb_data);
bsp_status_t bsp_uart_read_u8_timeout(bsp_dev_uart_t dev_num, uint8_t* rx_data, uint8_t nb_data, uint32_t timeout);
bsp_status_t bsp_uart_read_u8(bsp_dev_uart_t dev_num, uint8_t* rx_data, uint8_t *nb_data, uint32_t timeout);
bsp_status_t bsp_uart_write_read_u8(bsp_dev_uart_t dev_num, uint8_t* tx_data, uint8_t* rx_data, uint8_t nb_data);
bsp_status_t bsp_uart_rxne(bsp_dev_uart_t dev_num);

Expand Down
16 changes: 16 additions & 0 deletions src/hydrabus/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ const t_token_dict tl_dict[] = {
{ T_CONVENTION, "convention" },
{ T_DELAY, "delay" },
{ T_CLOCK_STRETCH, "clock-stretch" },
{ T_TIMEOUT, "timeout" },
/* Developer warning add new command(s) here */

/* BP-compatible commands */
Expand Down Expand Up @@ -475,6 +476,11 @@ t_token tokens_parity[] = {
T_STOP_BITS,\
.arg_type = T_ARG_UINT,\
.help = "Stop bits (1/2)"\
},\
{\
T_TIMEOUT,\
.arg_type = T_ARG_UINT,\
.help = "Read timeout in msec"\
},

t_token tokens_mode_uart[] = {
Expand Down Expand Up @@ -604,6 +610,11 @@ t_token tokens_uart[] = {
.arg_type = T_ARG_UINT,\
.help = "Communication convention",\
.help_full = "Set communication convention (0=normal, 1=inverse)"\
},\
{\
T_TIMEOUT,\
.arg_type = T_ARG_UINT,\
.help = "Read timeout in msec"\
},

t_token tokens_mode_smartcard[] = {
Expand Down Expand Up @@ -717,6 +728,11 @@ t_token tokens_smartcard[] = {
.arg_type = T_ARG_UINT,\
.help = "LIN device (1/2)"\
},\
{\
T_TIMEOUT,\
.arg_type = T_ARG_UINT,\
.help = "Read timeout in msec"\
},

t_token tokens_mode_lin[] = {
{
Expand Down
1 change: 1 addition & 0 deletions src/hydrabus/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ enum {
T_CONVENTION,
T_DELAY,
T_CLOCK_STRETCH,
T_TIMEOUT,
/* Developer warning add new command(s) here */

/* BP-compatible commands */
Expand Down
15 changes: 11 additions & 4 deletions src/hydrabus/hydrabus_bbio_smartcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void bbio_smartcard_init_proto_default(t_hydra_console *con)

/* Defaults */
proto->dev_num = 0;
proto->timeout = 10000;
proto->config.smartcard.dev_speed = 9600;
proto->config.smartcard.dev_parity = 0;
proto->config.smartcard.dev_stop_bit = 1;
Expand All @@ -57,7 +58,8 @@ void bbio_mode_smartcard(t_hydra_console *con)
uint8_t bbio_subcommand;
uint8_t *tx_data = pool_alloc_bytes(0x1000); // 4096 bytes
uint8_t *rx_data = pool_alloc_bytes(0x1000); // 4096 bytes
uint8_t data;
uint8_t data, tmp;
uint8_t to_read;
uint32_t dev_speed=0;
uint32_t final_baudrate;
bsp_status_t status;
Expand Down Expand Up @@ -142,13 +144,17 @@ void bbio_mode_smartcard(t_hydra_console *con)
i=0;
while(i<to_rx) {
if((to_rx-i) >= 255) {
to_read = 255;
bsp_smartcard_read_u8(proto->dev_num,
rx_data+i,
255);
&to_read,
TIME_MS2I(proto->timeout));
} else {
tmp = to_rx-i;
bsp_smartcard_read_u8(proto->dev_num,
rx_data+i,
to_rx-i);
&tmp,
TIME_MS2I(proto->timeout));
}
i+=255;
}
Expand Down Expand Up @@ -185,7 +191,8 @@ void bbio_mode_smartcard(t_hydra_console *con)
bsp_smartcard_set_rst(proto->dev_num, 0);
DelayMs(1);
bsp_smartcard_set_rst(proto->dev_num, 1);
i = bsp_smartcard_read_u8_timeout(proto->dev_num, rx_data, 33, TIME_MS2I(500));
to_read = 33;
bsp_smartcard_read_u8(proto->dev_num, rx_data, &to_read, TIME_MS2I(500));
cprint(con, (char *)&i, 1);
cprint(con, (char *)rx_data, i);
break;
Expand Down
10 changes: 6 additions & 4 deletions src/hydrabus/hydrabus_bbio_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void bbio_uart_init_proto_default(t_hydra_console *con)

/* Defaults */
proto->dev_num = 0;
proto->timeout = 10000;
proto->config.uart.dev_speed = 9600;
proto->config.uart.dev_parity = 0;
proto->config.uart.dev_stop_bit = 1;
Expand All @@ -53,10 +54,11 @@ static THD_FUNCTION(uart_reader_thread, arg)
if(!chThdShouldTerminateX())
{
if(bsp_uart_rxne(proto->dev_num)) {
bytes_read = bsp_uart_read_u8_timeout(proto->dev_num,
proto->buffer_rx,
UART_BRIDGE_BUFF_SIZE,
TIME_US2I(100));
bytes_read = UART_BRIDGE_BUFF_SIZE;
bsp_uart_read_u8(proto->dev_num,
proto->buffer_rx,
&bytes_read,
TIME_US2I(100));
if(bytes_read > 0) {
cprint(con, (char *)proto->buffer_rx, bytes_read);
}
Expand Down
18 changes: 13 additions & 5 deletions src/hydrabus/hydrabus_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const char hydrabus_mode_str_mul_read[] = "READ: ";
const char hydrabus_mode_str_mul_value_u8[] = "0x%02X ";
const char hydrabus_mode_str_mul_br[] = "\r\n";
const char hydrabus_mode_str_mdelay[] = "DELAY: %lu ms\r\n";
const char hydrabus_mode_str_read_timeout[] = "! TIMEOUT: read %lu out of %lu\r\n";

static const char mode_str_write_error[] = "WRITE error:%d\r\n";
static const char mode_str_read_error[] = "READ error:%d\r\n";
Expand Down Expand Up @@ -233,7 +234,7 @@ int cmd_mode_exec(t_hydra_console *con, t_tokenline_parsed *p)
usec = 1;
}
DelayUs(usec);
break;
break;
case T_PERCENT:
factor = 1000;
if (p->tokens[t + 1] == T_ARG_TOKEN_SUFFIX_INT) {
Expand Down Expand Up @@ -454,7 +455,7 @@ static int hydrabus_mode_read(t_hydra_console *con, t_tokenline_parsed *p,
if(con->mode->exec->read != NULL) {
mode_status = con->mode->exec->read(con, p_proto->buffer_rx, count);
}
if (mode_status != HYDRABUS_MODE_STATUS_OK)
if (mode_status == BSP_ERROR)
hydrabus_mode_read_error(con, mode_status);

return t - token_pos;
Expand Down Expand Up @@ -494,12 +495,19 @@ static int hydrabus_mode_hexdump(t_hydra_console *con, t_tokenline_parsed *p,
}

if(con->mode->exec->dump != NULL) {
mode_status = con->mode->exec->dump(con, p_proto->buffer_rx, to_rx);
mode_status = con->mode->exec->dump(con, p_proto->buffer_rx, &to_rx);
}
if (mode_status == HYDRABUS_MODE_STATUS_OK) {
switch(mode_status) {
case BSP_OK:
print_hex(con, p_proto->buffer_rx, to_rx);
} else {
break;
case BSP_TIMEOUT:
print_hex(con, p_proto->buffer_rx, to_rx);
cprintf(con, hydrabus_mode_str_read_timeout, bytes_read+to_rx, count);
return t - token_pos;
default:
hydrabus_mode_read_error(con, mode_status);
return t - token_pos;
}

bytes_read += to_rx;
Expand Down
5 changes: 4 additions & 1 deletion src/hydrabus/hydrabus_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ extern const char hydrabus_mode_str_mul_value_u8[];
/* "\r\n" */
extern const char hydrabus_mode_str_mul_br[];

/* "TIMEOUT: read %lu out of %lu\r\n" */
extern const char hydrabus_mode_str_read_timeout[];

typedef struct mode_exec_t {
/* Initialize mode hardware. */
int (*init)(t_hydra_console *con, t_tokenline_parsed *p);
Expand All @@ -66,7 +69,7 @@ typedef struct mode_exec_t {
/* Read data command 'read' or 'read:n' (return status 0=OK) */
uint32_t (*read)(t_hydra_console *con, uint8_t *rx_data, uint8_t nb_data);
/* Dump data */
uint32_t (*dump)(t_hydra_console *con, uint8_t *rx_data, uint8_t nb_data);
uint32_t (*dump)(t_hydra_console *con, uint8_t *rx_data, uint8_t *nb_data);
/* Write & Read data (return status 0=OK) */
uint32_t (*write_read)(t_hydra_console *con, uint8_t *tx_data, uint8_t *rx_data, uint8_t nb_data);
/* Set CLK High (x-WIRE or other raw mode) command '/' */
Expand Down
4 changes: 2 additions & 2 deletions src/hydrabus/hydrabus_mode_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ static uint32_t read(t_hydra_console *con, uint8_t *rx_data, uint8_t nb_data)
return status;
}

static uint32_t dump(t_hydra_console *con, uint8_t *rx_data, uint8_t nb_data)
static uint32_t dump(t_hydra_console *con, uint8_t *rx_data, uint8_t *nb_data)
{
uint32_t status;
uint8_t i, tmp;
mode_config_proto_t* proto = &con->mode->proto;
status = BSP_ERROR;
for(i = 0; i < nb_data; i++) {
for(i = 0; i < *nb_data; i++) {
if(proto->config.i2c.ack_pending) {
/* Send I2C ACK */
status = bsp_i2c_read_ack(I2C_DEV_NUM, TRUE);
Expand Down
4 changes: 2 additions & 2 deletions src/hydrabus/hydrabus_mode_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,12 +959,12 @@ static uint32_t read(t_hydra_console *con, uint8_t *rx_data, uint8_t nb_data)
return BSP_OK;
}

static uint32_t dump(t_hydra_console *con, uint8_t *rx_data, uint8_t nb_data)
static uint32_t dump(t_hydra_console *con, uint8_t *rx_data, uint8_t *nb_data)
{
uint8_t i;

i = 0;
while(i < nb_data) {
while(i < *nb_data) {
rx_data[i] = jtag_read_u8(con);
i++;
}
Expand Down
Loading

0 comments on commit 80e8bb7

Please sign in to comment.