Discussion:
[Owfs-developers] Custom 1-wire slave - need to send two bytes
Gabrijel Erman
2017-01-18 17:12:43 UTC
Permalink
Hi,

I developed a custom 1-wire slave device that requires 2 bytes to be sent
on write. Complete .c file is as follows:
#include <config.h>
#include "owfs_config.h"
#include "ow_e5_device.h"

#define _1W_E5_READ 0x51
#define _1W_E5_WRITE 0x53

READ_FUNCTION(FS_read_status);
WRITE_FUNCTION(FS_write_valve);

static struct aggregate E5_val_bits = { 8, ag_numbers, ag_aggregate, };
static struct aggregate E5_val_write = { 2, ag_numbers, ag_separate, };

static struct filetype E5_DEVICE[] = {
F_STANDARD,
{"io_status", PROPERTY_LENGTH_BITFIELD, &E5_val_bits, ft_bitfield,
fc_stable, FS_read_status, NO_WRITE_FUNCTION, VISIBLE, NO_FILETYPE_DATA, },
{"io_valve", PROPERTY_LENGTH_UNSIGNED, &E5_val_write, ft_unsigned,
fc_volatile, NO_READ_FUNCTION, FS_write_valve, VISIBLE, NO_FILETYPE_DATA,
},
};

DeviceEntryExtended(E5, E5_DEVICE, DEV_resume, NO_GENERIC_READ,
NO_GENERIC_WRITE);

static GOOD_OR_BAD OW_read(BYTE * data, const struct parsedname *pn);
static GOOD_OR_BAD OW_write(BYTE * data, const struct parsedname *pn);

static ZERO_OR_ERROR FS_read_status(struct one_wire_query *owq)
{
BYTE state;
if ( OW_read( &state, PN(owq) ) ) {
return -EINVAL ;
}
OWQ_U(owq)=state;
return 0;
}

static ZERO_OR_ERROR FS_write_valve(struct one_wire_query *owq)
{
if (OW_write(OWQ_buffer(owq), PN(owq))){
return -EINVAL ;
}
return 0;
}

static GOOD_OR_BAD OW_read(BYTE * data, const struct parsedname *pn)
{
BYTE cmd[] = { _1W_E5_READ, };
BYTE resp[1] ;
struct transaction_log t[] = {
TRXN_START,
TRXN_WRITE1(cmd),
TRXN_READ1(resp),
TRXN_END,
};

RETURN_BAD_IF_BAD(BUS_transaction(t, pn)) ;

data[0]=resp[0];
return gbGOOD;
}

static GOOD_OR_BAD OW_write(BYTE *data, const struct parsedname *pn)
{
//data 0 => mask
//data 1 => value
data[0] &= ~(1 << 0); //ensure 0-th bit is removed from mask
BYTE cmd[] = { _1W_E5_WRITE, data[0], data[1], };
struct transaction_log t[] = {
TRXN_START,
TRXN_WRITE3(cmd),
TRXN_END,
};

RETURN_BAD_IF_BAD(BUS_transaction(t, pn));

return gbGOOD;
}

I can read the device without any problems but if I try to write
echo "22" > io_valve.ALL
I get error:
bash: echo: write error: Invalid argument
And running owfs reports:
DEBUG: ow_write.c:(136) Error interpreting input value.
DEBUG: ow_write.c:(112) Error writing to /E5.54594D454B01/io_valve.ALL
DEBUG: ow_parsename.c:(63) /E5.54594D454B01/io_valve.ALL
unique: 50, error: -22 (Invalid argument), outsize: 16
unique: 51, opcode: RELEASE (18), nodeid: 4, insize: 64, pid: 0
release[0] flags: 0x20001
CALL: owfs_callback.c:(135) RELEASE path=/E5.54594D454B01/io_valve.ALL
unique: 51, success, outsize: 16

What is that I am doing wrong. Thanks!

Gabrijel
Jan Kandziora
2017-01-18 21:14:11 UTC
Permalink
Post by Gabrijel Erman
I can read the device without any problems but if I try to write
echo "22" > io_valve.ALL
To sort out problems with CRLF, please set up owserver and use the
owwrite tool instead of echo.

Kind regards

Jan
morfeus
2017-01-20 19:15:47 UTC
Permalink
Hi Jan,

well, using echo wasn't a problem at all because owwrite give the same
results.
I ended up looking other .c of owfs and activating debug until I got what
was needed. At the end:

1. in the structure, that particular write needs to be:
{"io_valve", 2, NON_AGGREGATE, ft_ascii, fc_stable, NO_READ_FUNCTION,
FS_write_valve, VISIBLE, NO_FILETYPE_DATA, },

2. OW_write function needs to be re-written to accept two "normal" BYTEs and
not array (ok, can be done also using array, but ok). Looks like this:
static GOOD_OR_BAD OW_write(BYTE data0,BYTE data1, const struct parsedname
*pn)
{
BYTE cmd[] = { _1W_E5_WRITE, data0, data1, };
struct transaction_log t[] = {
TRXN_START,
TRXN_WRITE3(cmd),
TRXN_END,
};

RETURN_BAD_IF_BAD(BUS_transaction(t, pn));

return gbGOOD;
}

3. function that gets called on write needs to look like this:
static ZERO_OR_ERROR FS_write_valve(struct one_wire_query *owq)
{
if (OW_write(OWQ_buffer(owq)[0],OWQ_buffer(owq)[1], PN(owq))){
return -EINVAL ;
}

return 0;
}

Hope this helps someone if runs into the same problem;)

Gabrijel



--
View this message in context: http://owfs-developers.1086194.n5.nabble.com/Custom-1-wire-slave-need-to-send-two-bytes-tp12906p12910.html
Sent from the OWFS Developers mailing list archive at Nabble.com.
Loading...