Custom 1-wire slave - need to send two bytes

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom 1-wire slave - need to send two bytes

morfeus
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

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Owfs-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/owfs-developers
Reply | Threaded
Open this post in threaded view
|

Re: Custom 1-wire slave - need to send two bytes

Jan Kandziora
Am 18.01.2017 um 18:12 schrieb 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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Owfs-developers mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/owfs-developers
Reply | Threaded
Open this post in threaded view
|

Re: Custom 1-wire slave - need to send two bytes

morfeus
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