RATP i2c and GPIO support
This patch series implements support for i2c and GPIO operations via RATP.
[PATCH 1/4] ratp: implement i2c read/write support [PATCH 2/4] bbremote: implement i2c read/write support [PATCH 3/4] ratp: implement support for GPIO commands [PATCH 4/4] bbremote: implement support for GPIO operations
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
PATCH 1/4 ratp: implement i2c read/write support
Introduce two new RATP commands that allow running i2c read/write operations, very similar in format to the already existing md/mw RATP commands.
The messages are defined with a fixed 16-bit long register field, but it will only be treated as a 16-bit address if I2C_FLAG_WIDE_ADDRESS is set in the message flags field. If this flag is unset, the start register address is assumed 8-bit long.
If the message includes the I2C_FLAG_MASTER_MODE flag, the start register field is ignored and a i2c master send/receive operation is performed.
--- common/ratp/Makefile | 1 + common/ratp/i2c.c | 281 +++++++++++++++++++++++++++++++++++++++++++ include/ratp_bb.h | 4 + 3 files changed, 286 insertions(+) create mode 100644 common/ratp/i2c.c
common/ratp/Makefile +1 -0
@@ -4,3 +4,4 @@ obj-y += getenv.o obj-y += md.o obj-y += mw.o obj-y += reset.o +obj-y += i2c.o
common/ratp/i2c.c new file +281 -0
281 lines not on this page · raw message
include/ratp_bb.h +4 -0
@@ -17,6 +17,10 @@#define BB_RATP_TYPE_MW 12#define BB_RATP_TYPE_MW_RETURN 13#define BB_RATP_TYPE_RESET 14 +#define BB_RATP_TYPE_I2C_READ 15 +#define BB_RATP_TYPE_I2C_READ_RETURN 16 +#define BB_RATP_TYPE_I2C_WRITE 17 +#define BB_RATP_TYPE_I2C_WRITE_RETURN 18struct ratp_bb {uint16_t type;
Signature · 7 lines
2.18.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 1/4] ratp: implement i2c read/write support
Quoted 5 lines · Aleksander Morgado
Introduce two new RATP commands that allow running i2c read/write operations, very similar in format to the already existing md/mw RATP commands.
Quoted 9 lines
The messages are defined with a fixed 16-bit long register field, but it will only be treated as a 16-bit address if
I2C_FLAG_WIDE_ADDRESSis set in the message flags field. If this flag is unset, the start register address is assumed 8-bit long.If the message includes the
I2C_FLAG_MASTER_MODEflag, the start register field is ignored and a i2c master send/receive operation is performed.
LGTM, minor nitpicks below.
Quoted 23 lines
Signed-off-by Aleksander Morgado <[email protected]>--- common/ratp/Makefile | 1 + common/ratp/i2c.c | 281 +++++++++++++++++++++++++++++++++++++++++++ include/ratp_bb.h | 4 + 3 files changed, 286 insertions(+) create mode 100644 common/ratp/i2c.ccommon/ratp/Makefile +1
-0@@ -4,3 +4,4 @@ obj-y += getenv.o obj-y += md.o obj-y += mw.o obj-y += reset.o +obj-y += i2c.ocommon/ratp/i2c.c new file +0
-0
common/ratp/i2c.c line 3
Quoted 3 lines
common/ratp/i2c.c new file +3
-0@@ -0,0 +1,281 @@ +/* +* Copyright (c) 2011-2018 Sascha Hauer <[email protected]>, Pengutronix +*
Is the above really true?
Quoted 25 lines
common/ratp/i2c.c new file +25
-0+* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <common.h> +#include <ratp_bb.h> +#include <init.h> +#include <driver.h> +#include <malloc.h> +#include <errno.h> +#include <i2c/i2c.h> + +/* NOTE: +* - Fixed-size fields (e.g. integers) are given just after the header. +* - Variable-length fields are stored inside the buffer[] and their position
common/ratp/i2c.c line 38
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+* within the buffer[] and their size are given as fixed-sized fields after +* the header. +* The message may be extended at any time keeping backwards compatibility, +* as the position of the buffer[] is given by the buffer_offset field. i.e. +* increasing the buffer_offset field we can extend the fixed-sized section +* to add more fields. +*/ + +#define I2C_FLAG_WIDE_ADDRESS (1 << 0) +#define I2C_FLAG_MASTER_MODE (1 << 1)
BIT() might be a bit more concise
common/ratp/i2c.c line 49
Quoted 11 lines
common/ratp/i2c.c new file +11
-0+ +struct ratp_bb_i2c_read_request { +struct ratp_bb header; +uint16_t buffer_offset; +uint8_t bus; +uint8_t addr; +uint16_t reg; +uint8_t flags; +uint16_t size; +uint8_t buffer[]; +} __attribute__((packed));
There should already be __packed shortcut, so you can use it instead of the "full form". Same for all other headers below.
Quoted 37 lines
common/ratp/i2c.c new file +37
-0+ +struct ratp_bb_i2c_read_response { +struct ratp_bb header; +uint16_t buffer_offset; +uint32_t errno; +uint16_t data_size; +uint16_t data_offset; +uint8_t buffer[]; +} __attribute__((packed)); + +struct ratp_bb_i2c_write_request { +struct ratp_bb header; +uint16_t buffer_offset; +uint8_t bus; +uint8_t addr; +uint16_t reg; +uint8_t flags; +uint16_t data_size; +uint16_t data_offset; +uint8_t buffer[]; +} __attribute__((packed)); + +struct ratp_bb_i2c_write_response { +struct ratp_bb header; +uint16_t buffer_offset; +uint32_t errno; +uint16_t written; +uint8_t buffer[]; +} __attribute__((packed)); + +static int ratp_cmd_i2c_read (const struct ratp_bb *req,int req_len, +struct ratp_bb **rsp,int *rsp_len) +{ +struct ratp_bb_i2c_read_request *i2c_read_req = (struct ratp_bb_i2c_read_request *)req; +struct ratp_bb_i2c_read_response *i2c_read_rsp; +struct i2c_adapter *adapter; +struct i2c_client client;
common/ratp/i2c.c line 96
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+uint16_t buffer_offset; +int i2c_read_rsp_len; +uint16_t reg; +uint16_t size; +uint32_t wide =0 ; +int ret =0 ; + +/* At least message header should be valid */ +if (req_len <sizeof (*i2c_read_req)) { + printf("ratp i2c read ignored: size mismatch (%d < %zu)\n",
You can use pr_err() here instead. This way this error message would have log level embedded in it and on builds/terminals that support it it would be colored accordingly (red). You can also look into utilizing pr_fmt() for providing common prefix for all of your pr_* printed messages. Like, for example, is done here:
https://git.pengutronix.de/cgit/barebox/tree/drivers/pci/pci.c#n1
common/ratp/i2c.c line 97
Quoted 1 line
common/ratp/i2c.c new file +1
-0+ req_len, sizeof (*i2c_read_req));
Looks like there's a whitespace before "(" (GNU coding style?).
Quoted 5 lines
common/ratp/i2c.c new file +5
-0+ ret = -EINVAL ; + goto out_rsp; + } + +/* We don't require any buffer here, but just validate buffer position and size */
common/ratp/i2c.c line 112
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+ buffer_offset =be16_to_cpu (i2c_read_req->buffer_offset); +if (buffer_offset != req_len) { +printf ("ratp i2c read ignored: invalid buffer offset (%hu != %d)\n" , + buffer_offset, req_len); + ret = -EINVAL ; + goto out_rsp; + } + + reg =be16_to_cpu (i2c_read_req->reg); + size =be16_to_cpu (i2c_read_req->size);
Looks like more of whitespace before opening parenthesis.
Quoted 39 lines
common/ratp/i2c.c new file +39
-0+if (i2c_read_req->flags &I2C_FLAG_WIDE_ADDRESS ) + wide =I2C_ADDR_16_BIT ; + +out_rsp: +/* Avoid reading anything on error */ +if (ret !=0 ) + size =0 ; + + i2c_read_rsp_len =sizeof (*i2c_read_rsp) + size; + i2c_read_rsp =xzalloc (i2c_read_rsp_len); + i2c_read_rsp->header.type =cpu_to_be16 (BB_RATP_TYPE_I2C_READ_RETURN ); + i2c_read_rsp->buffer_offset =cpu_to_be16 (sizeof (*i2c_read_rsp)); + i2c_read_rsp->data_offset =0 ; + +/* Don't read anything on error or if 0 bytes were requested */ +if (size >0 ) { + adapter =i2c_get_adapter (i2c_read_req->bus); +if (!adapter) { +printf ("ratp i2c read ignored: i2c bus %u not found\n" , i2c_read_req->bus); + ret = -ENODEV ; + goto out; + } + + client.adapter = adapter; + client.addr = i2c_read_req->addr; + +if (i2c_read_req->flags &I2C_FLAG_MASTER_MODE ) { + ret =i2c_master_recv (&client, i2c_read_rsp->buffer, size); + }else { + ret =i2c_read_reg (&client, reg | wide, i2c_read_rsp->buffer, size); + } +if (ret != size) { +printf ("ratp i2c read ignored: not all bytes read (%u < %u)\n" , ret, size); + ret = -EIO ; + goto out; + } + ret =0 ; + } +
common/ratp/i2c.c line 161
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+out: +if (ret !=0 ) { + i2c_read_rsp->data_size =0 ; + i2c_read_rsp->errno =cpu_to_be32 (ret); + i2c_read_rsp_len =sizeof (*i2c_read_rsp); + }else { + i2c_read_rsp->data_size =cpu_to_be16 (size); + i2c_read_rsp->errno =0 ; + } +
It looks like you can move:
i2c_read_rsp->data_size = cpu_to_be16(size);i2c_read_rsp->errno = cpu_to_be32(ret);
outside of if since it should work as intended for both cases (size is 0 if ret != 0).
Quoted 73 lines
common/ratp/i2c.c new file +73
-0+ *rsp = (struct ratp_bb *)i2c_read_rsp; + *rsp_len = i2c_read_rsp_len; + +return ret; +} + +BAREBOX_RATP_CMD_START (I2C_READ ) + .request_id = BB_RATP_TYPE_I2C_READ, + .response_id = BB_RATP_TYPE_I2C_READ_RETURN, + .cmd = ratp_cmd_i2c_read +BAREBOX_RATP_CMD_END + + +static int ratp_cmd_i2c_write(const struct ratp_bb *req, int req_len, +struct ratp_bb **rsp,int *rsp_len) +{ +struct ratp_bb_i2c_write_request *i2c_write_req = (struct ratp_bb_i2c_write_request *)req; +struct ratp_bb_i2c_write_response *i2c_write_rsp; +struct i2c_adapter *adapter; +struct i2c_client client; +uint8_t *buffer; +uint16_t buffer_offset; +uint16_t buffer_size; +uint16_t reg; +uint16_t data_offset; +uint16_t data_size; +uint32_t wide =0 ; +int ret =0 ; +int written =0 ; + +/* At least message header should be valid */ +if (req_len <sizeof (*i2c_write_req)) { +printf ("ratp i2c write ignored: size mismatch (%d < %zu)\n" , + req_len,sizeof (*i2c_write_req)); + ret = -EINVAL ; + goto out; + } + +/* Validate buffer position and size */ + buffer_offset =be16_to_cpu (i2c_write_req->buffer_offset); +if (req_len < buffer_offset) { +printf ("ratp i2c write ignored: invalid buffer offset (%d < %hu)\n" , + req_len, buffer_offset); + ret = -EINVAL ; + goto out; + } + buffer_size = req_len - buffer_offset; + buffer = ((uint8_t *)i2c_write_req) + buffer_offset; + +/* Validate data position and size */ + data_offset =be16_to_cpu (i2c_write_req->data_offset); +if (data_offset !=0 ) { +printf ("ratp i2c write ignored: invalid data offset\n" ); + ret = -EINVAL ; + goto out; + } + data_size =be16_to_cpu (i2c_write_req->data_size); +if (!data_size) { +/* Success */ + goto out; + } + +/* Validate buffer size */ +if (buffer_size < data_size) { +printf ("ratp i2c write ignored: size mismatch (%d < %hu): data not fully given\n" , + buffer_size, data_size); + ret = -EINVAL ; + goto out; + } + + reg =be16_to_cpu (i2c_write_req->reg); +if (i2c_write_req->flags &I2C_FLAG_WIDE_ADDRESS ) + wide =I2C_ADDR_16_BIT ;
common/ratp/i2c.c line 244
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+ + adapter =i2c_get_adapter (i2c_write_req->bus); +if (!adapter) { +printf ("ratp i2c write ignored: i2c bus %u not found\n" , i2c_write_req->bus); + ret = -ENODEV ; + goto out; + } + + client.adapter = adapter; + client.addr = i2c_write_req->addr;
This pattern seems to have quite a bit of users in the codebase already, so if you feel like going through the motions needed, this can probably added as a simple helper function in include/i2c/i2c.h and drivers/i2c/i2c.c.
Thanks,
Andrey Smirnov
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 1/4] ratp: implement i2c read/write support
Hey Andrey,
Thanks for the review :) see some comments below.
Quoted 38 lines
Quoted 25 lines
common/ratp/i2c.c new file +25
-0+/* Don't read anything on error or if 0 bytes were requested */ +if (size >0 ) { + adapter =i2c_get_adapter (i2c_read_req->bus); +if (!adapter) { +printf ("ratp i2c read ignored: i2c bus %u not found\n" , i2c_read_req->bus); + ret = -ENODEV ; + goto out; + } + + client.adapter = adapter; + client.addr = i2c_read_req->addr; + +if (i2c_read_req->flags &I2C_FLAG_MASTER_MODE ) { + ret =i2c_master_recv (&client, i2c_read_rsp->buffer, size); + }else { + ret =i2c_read_reg (&client, reg | wide, i2c_read_rsp->buffer, size); + } +if (ret != size) { +printf ("ratp i2c read ignored: not all bytes read (%u < %u)\n" , ret, size); + ret = -EIO ; + goto out; + } + ret =0 ; + } +common/ratp/i2c.c line 161
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+out: +if (ret !=0 ) { + i2c_read_rsp->data_size =0 ; + i2c_read_rsp->errno =cpu_to_be32 (ret); + i2c_read_rsp_len =sizeof (*i2c_read_rsp); + }else { + i2c_read_rsp->data_size =cpu_to_be16 (size); + i2c_read_rsp->errno =0 ; + } +It looks like you can move:
Quoted 6 lines
i2c_read_rsp->data_size=cpu_to_be16(size);i2c_read_rsp->errno=cpu_to_be32(ret);outside of if since it should work as intended for both cases (size is 0 if ret != 0).
Don't think I can do that. In the if (size > 0) {} just a bit above, size is not modified but ret may become an error. We do want to make sure 0 is returned as size when there is an error, so cannot move it outside the if() as you suggest here.
Signature · 7 lines
Aleksander https://aleksander.es _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 1/4] ratp: implement i2c read/write support
In reply to Aleksander MorgadoQuoted 70 lines · Aleksander Morgado
Introduce two new RATP commands that allow running i2c read/write operations, very similar in format to the already existing md/mw RATP commands.
The messages are defined with a fixed 16-bit long register field, but it will only be treated as a 16-bit address if
I2C_FLAG_WIDE_ADDRESSis set in the message flags field. If this flag is unset, the start register address is assumed 8-bit long.If the message includes the
I2C_FLAG_MASTER_MODEflag, the start register field is ignored and a i2c master send/receive operation is performed.Signed-off-by Aleksander Morgado <[email protected]>--- common/ratp/Makefile | 1 + common/ratp/i2c.c | 281 +++++++++++++++++++++++++++++++++++++++++++ include/ratp_bb.h | 4 + 3 files changed, 286 insertions(+) create mode 100644 common/ratp/i2c.ccommon/ratp/Makefile +1
-0@@ -4,3 +4,4 @@ obj-y += getenv.o obj-y += md.o obj-y += mw.o obj-y += reset.o +obj-y += i2c.ocommon/ratp/i2c.c new file +34
-0@@ -0,0 +1,281 @@ +/* +* Copyright (c) 2011-2018 Sascha Hauer <[email protected]>, Pengutronix +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <common.h> +#include <ratp_bb.h> +#include <init.h> +#include <driver.h> +#include <malloc.h> +#include <errno.h> +#include <i2c/i2c.h> + +/* NOTE: +* - Fixed-size fields (e.g. integers) are given just after the header. +* - Variable-length fields are stored inside the buffer[] and their position +* within the buffer[] and their size are given as fixed-sized fields after +* the header. +* The message may be extended at any time keeping backwards compatibility, +* as the position of the buffer[] is given by the buffer_offset field. i.e. +* increasing the buffer_offset field we can extend the fixed-sized section +* to add more fields.
common/ratp/i2c.c line 44
Quoted 10 lines
common/ratp/i2c.c new file +10
-0+*/ + +#define I2C_FLAG_WIDE_ADDRESS (1 << 0) +#define I2C_FLAG_MASTER_MODE (1 << 1) + +struct ratp_bb_i2c_read_request { +struct ratp_bb header; +uint16_t buffer_offset; +uint8_t bus; +uint8_t addr;
I wonder how we see the RATP support. If it's for adhoc debugging then bus/addr is fine. The caller should have no expectations that the bus number is constant though. Likewise for the address which might change across different board revisions.
Should we have support for resolving names, which could be provided by aliases in dt?
We could still add name resolving support later as a separate call, I just thought that now is the time to think how we proceed.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 1/4] ratp: implement i2c read/write support
Quoted 11 lines
common/ratp/i2c.c line 44
Quoted 5 lines
common/ratp/i2c.c new file +5
-0+struct ratp_bb_i2c_read_request { +struct ratp_bb header; +uint16_t buffer_offset; +uint8_t bus; +uint8_t addr;I wonder how we see the RATP support. If it's for adhoc debugging then bus/addr is fine. The caller should have no expectations that the bus number is constant though. Likewise for the address which might change across different board revisions.
Quoted 6 lines
Should we have support for resolving names, which could be provided by aliases in dt?
We could still add name resolving support later as a separate call, I just thought that now is the time to think how we proceed.
I truly have no opinion here, but if name resolving is added at some point I can either update this operation or even add a new one.
Signature · 7 lines
Aleksander https://aleksander.es _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 1/4] ratp: implement i2c read/write support
Quoted 14 lines · Aleksander Morgado
Quoted 14 lines
common/ratp/i2c.c line 44
Quoted 5 lines
common/ratp/i2c.c new file +5
-0+struct ratp_bb_i2c_read_request { +struct ratp_bb header; +uint16_t buffer_offset; +uint8_t bus; +uint8_t addr;I wonder how we see the RATP support. If it's for adhoc debugging then bus/addr is fine. The caller should have no expectations that the bus number is constant though. Likewise for the address which might change across different board revisions.
Should we have support for resolving names, which could be provided by aliases in dt?
Quoted 6 lines
Quoted 3 lines
We could still add name resolving support later as a separate call, I just thought that now is the time to think how we proceed.
I truly have no opinion here, but if name resolving is added at some point I can either update this operation or even add a new one.
Ok, as said, we can add the name resolving functionality later. So, as long as you don't see such numbering issues with the things you want to do with RATP then I am fine with adding this when it's needed.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 2/4 bbremote: implement i2c read/write support
In reply to Aleksander MorgadoExtend the bbremote script with operations to perform binary i2c read/write operations.
E.g.:
barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x8f 0x30 0x00 0x00
barebox:/ i2c_write -b 0 -a 0x68 -r 0x06A0 -w 0x87 0x30 0x00 0x00
barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x87 0x30 0x00 0x00
barebox:/ i2c_write -b 0 -a 0x68 -r 0x06A0 -w 0x8f 0x30 0x00 0x00
barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x8f 0x30 0x00 0x00
===================================================================
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8f300000
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-write 0x00 0x68 0x06A0 0x01 "87300000"
4 bytes written
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
87300000
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-write 0x00 0x68 0x06A0 0x01 "8f300000"
4 bytes written
$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8f300000--- scripts/remote/controller.py | 24 ++++++++++ scripts/remote/main.py | 37 ++++++++++++++++ scripts/remote/messages.py | 86 ++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+)
scripts/remote/controller.py +24 -0
@@ -58,6 +58,18 @@ def unpack(data): elif p_type == BBType.reset: logging.debug("received: reset") return BBPacketReset(raw=data) + elif p_type == BBType.i2c_read: + logging.debug("received: i2c_read") + return BBPacketI2cRead(raw=data) + elif p_type == BBType.i2c_read_return: + logging.debug("received: i2c_read_return") + return BBPacketI2cReadReturn(raw=data) + elif p_type == BBType.i2c_write: + logging.debug("received: i2c_write") + return BBPacketI2cWrite(raw=data) + elif p_type == BBType.i2c_write_return: + logging.debug("received: i2c_write_return") + return BBPacketI2cWriteReturn(raw=data) else: logging.debug("received: UNKNOWN") return BBPacket(raw=data)
@@ -136,6 +148,18 @@ class Controller(Thread): logging.info("Mw return: %r", r) return (r.exit_code,r.written) + def i2c_read(self, bus, addr, reg, flags, size): + self._send(BBPacketI2cRead(bus=bus, addr=addr, reg=reg, flags=flags, size=size)) + r = self._expect(BBPacketI2cReadReturn) + logging.info("i2c read return: %r", r) + return (r.exit_code,r.data) + + def i2c_write(self, bus, addr, reg, flags, data): + self._send(BBPacketI2cWrite(bus=bus, addr=addr, reg=reg, flags=flags, data=data)) + r = self._expect(BBPacketI2cWriteReturn) + logging.info("i2c write return: %r", r) + return (r.exit_code,r.written) + def reset(self, force): self._send(BBPacketReset(force=force))
scripts/remote/main.py +37 -0
@@ -98,6 +98,27 @@ def handle_mw(args): return res +def handle_i2c_read(args): + ctrl = get_controller(args) + (res,data) = ctrl.i2c_read(args.bus, args.address, args.reg, args.flags, args.size) + if res == 0: + print(binascii.hexlify(data)) + ctrl.close() + return res + + +def handle_i2c_write(args): + ctrl = get_controller(args) + data=args.data + if ((len(data) % 2) != 0): + data="0"+data + (res,written) = ctrl.i2c_write(args.bus, args.address, args.reg, args.flags, binascii.unhexlify(data)) + if res == 0: + print("%i bytes written" % written) + ctrl.close() + return res + + def handle_reset(args): ctrl = get_controller(args) ctrl.reset(args.force)
@@ -188,6 +209,22 @@ parser_mw.add_argument('address', type=auto_int, help="address") parser_mw.add_argument('data', help="data") parser_mw.set_defaults(func=handle_mw) +parser_i2c_read = subparsers.add_parser('i2c-read', help="run i2c read command") +parser_i2c_read.add_argument('bus', type=auto_int, help="bus") +parser_i2c_read.add_argument('address', type=auto_int, help="address") +parser_i2c_read.add_argument('reg', type=auto_int, help="reg") +parser_i2c_read.add_argument('flags', type=auto_int, help="flags") +parser_i2c_read.add_argument('size', type=auto_int, help="size") +parser_i2c_read.set_defaults(func=handle_i2c_read) + +parser_i2c_write = subparsers.add_parser('i2c-write', help="run i2c write command") +parser_i2c_write.add_argument('bus', type=auto_int, help="bus") +parser_i2c_write.add_argument('address', type=auto_int, help="address") +parser_i2c_write.add_argument('reg', type=auto_int, help="reg") +parser_i2c_write.add_argument('flags', type=auto_int, help="flags") +parser_i2c_write.add_argument('data', help="data") +parser_i2c_write.set_defaults(func=handle_i2c_write) + parser_reset = subparsers.add_parser('reset', help="run reset command") parser_reset_force = parser_reset.add_mutually_exclusive_group(required=False) parser_reset_force.add_argument('--force', dest='force', action='store_true')
scripts/remote/messages.py +86 -0
95 lines not on this page · raw message
Signature · 7 lines
2.18.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 3/4 ratp: implement support for GPIO commands
In reply to Aleksander MorgadoIntroduce three new RATP commands that allow getting and setting GPIO values as well as configuring the direction of the GPIO pins.
--- common/ratp/Makefile | 1 + common/ratp/gpio.c | 148 +++++++++++++++++++++++++++++++++++++++++++ include/ratp_bb.h | 6 ++ 3 files changed, 155 insertions(+) create mode 100644 common/ratp/gpio.c
common/ratp/Makefile +1 -0
@@ -5,3 +5,4 @@ obj-y += md.o obj-y += mw.o obj-y += reset.o obj-y += i2c.o +obj-y += gpio.o
common/ratp/gpio.c new file +148 -0
148 lines not on this page · raw message
include/ratp_bb.h +6 -0
@@ -21,6 +21,12 @@#define BB_RATP_TYPE_I2C_READ_RETURN 16#define BB_RATP_TYPE_I2C_WRITE 17#define BB_RATP_TYPE_I2C_WRITE_RETURN 18 +#define BB_RATP_TYPE_GPIO_GET_VALUE 19 +#define BB_RATP_TYPE_GPIO_GET_VALUE_RETURN 20 +#define BB_RATP_TYPE_GPIO_SET_VALUE 21 +#define BB_RATP_TYPE_GPIO_SET_VALUE_RETURN 22 +#define BB_RATP_TYPE_GPIO_SET_DIRECTION 23 +#define BB_RATP_TYPE_GPIO_SET_DIRECTION_RETURN 24struct ratp_bb {uint16_t type;
Signature · 7 lines
2.18.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 3/4] ratp: implement support for GPIO commands
Quoted 4 lines · Aleksander Morgado
Introduce three new RATP commands that allow getting and setting GPIO values as well as configuring the direction of the GPIO pins.
I avoided repeating nits I already mentioned in i2c patch, some additional nits are below
Quoted 59 lines
Signed-off-by Aleksander Morgado <[email protected]>--- common/ratp/Makefile | 1 + common/ratp/gpio.c | 148 +++++++++++++++++++++++++++++++++++++++++++ include/ratp_bb.h | 6 ++ 3 files changed, 155 insertions(+) create mode 100644 common/ratp/gpio.ccommon/ratp/Makefile +1
-0@@ -5,3 +5,4 @@ obj-y += md.o obj-y += mw.o obj-y += reset.o obj-y += i2c.o +obj-y += gpio.ocommon/ratp/gpio.c new file +36
-0@@ -0,0 +1,148 @@ +/* +* Copyright (c) 2018 Sascha Hauer <[email protected]>, Pengutronix +* +* See file CREDITS for list of people who contributed to this +* project. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#include <common.h> +#include <ratp_bb.h> +#include <malloc.h> +#include <environment.h> +#include <gpio.h> +#include <errno.h> + +struct ratp_bb_gpio_get_value_request { +struct ratp_bb header; +uint32_t gpio; +} __attribute__((packed)); + +struct ratp_bb_gpio_get_value_response { +struct ratp_bb header; +uint8_t value; +} __attribute__((packed)); + +static int ratp_cmd_gpio_get_value (const struct ratp_bb *req,int req_len, +struct ratp_bb **rsp,int *rsp_len)
common/ratp/gpio.c line 46
Quoted 10 lines
common/ratp/gpio.c new file +10
-0+{ +struct ratp_bb_gpio_get_value_request *gpio_req = (struct ratp_bb_gpio_get_value_request *)req; +struct ratp_bb_gpio_get_value_response *gpio_rsp; +int gpio_rsp_len; +uint32_t gpio; +uint8_t value; + +if (req_len <sizeof (*gpio_req)) { +printf ("ratp gpio get value request ignored: size mismatch (%d < %zu)\n" , req_len,sizeof (*gpio_req)); +return 2 ;
Hmm, i2c code was using negative error numbers as returns, but this just returns 2. Is this correct? If so, the it might be worth putting a comment explaining it here, maybe?
common/ratp/gpio.c line 50
Quoted 4 lines
common/ratp/gpio.c new file +4
-0+ } + + gpio =be32_to_cpu (gpio_req->gpio); + value = !!gpio_get_value (gpio);
Is this value variable really needed? It doesn't seem to be use anywhere else but in the assignment below.
Quoted 28 lines
common/ratp/gpio.c new file +28
-0+ + gpio_rsp_len =sizeof (struct ratp_bb_gpio_get_value_response ); + gpio_rsp =xzalloc (gpio_rsp_len); + gpio_rsp->header.type =cpu_to_be16 (BB_RATP_TYPE_GPIO_GET_VALUE_RETURN ); + gpio_rsp->value = value; + + *rsp_len = gpio_rsp_len; + *rsp = (struct ratp_bb *)gpio_rsp; +return 0 ; +} + +BAREBOX_RATP_CMD_START (GPIO_GET_VALUE ) + .request_id = BB_RATP_TYPE_GPIO_GET_VALUE, + .response_id = BB_RATP_TYPE_GPIO_GET_VALUE_RETURN, + .cmd = ratp_cmd_gpio_get_value +BAREBOX_RATP_CMD_END + + +struct ratp_bb_gpio_set_value_request { +struct ratp_bb header; +uint32_t gpio; +uint8_t value; +} __attribute__((packed)); + +static int ratp_cmd_gpio_set_value (const struct ratp_bb *req,int req_len, +struct ratp_bb **rsp,int *rsp_len) +{ +struct ratp_bb_gpio_set_value_request *gpio_req = (struct ratp_bb_gpio_set_value_request *)req;
common/ratp/gpio.c line 88
Quoted 10 lines
common/ratp/gpio.c new file +10
-0+uint32_t gpio; + +if (req_len <sizeof (*gpio_req)) { +printf ("ratp gpio set value request ignored: size mismatch (%d < %zu)\n" , req_len,sizeof (*gpio_req)); +return 2 ; + } + + gpio =be32_to_cpu (gpio_req->gpio); +gpio_set_value (gpio, gpio_req->value); +
Not saying that you should do anything about it, but FYI this will end up a no-op if specified GPIO is cannot be requested.
Thanks,
Andrey Smirnov
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 3/4] ratp: implement support for GPIO commands
In reply to Aleksander MorgadoQuoted 3 lines · Aleksander Morgado
Introduce three new RATP commands that allow getting and setting GPIO values as well as configuring the direction of the GPIO pins.
Same here as with the i2c bus/address thing. Being to able to use names for the GPIOs would be a good thing.
common/ratp/gpio.c line 28
Quoted 11 lines
common/ratp/gpio.c new file +11
-0+#include <common.h> +#include <ratp_bb.h> +#include <malloc.h> +#include <environment.h> +#include <gpio.h> +#include <errno.h> + +struct ratp_bb_gpio_get_value_request { +struct ratp_bb header; +uint32_t gpio; +} __attribute__((packed));
Nitpick: I prefer not to align the variable names in struct definitions. If the next element with a longer type is added then you can only give up the alignment or patch unrelated lines (which is bad because 'git blame' gives no useful output for the changed lines)
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 4/4 bbremote: implement support for GPIO operations
In reply to Aleksander MorgadoExtend the bbremote script with operations to manage GPIOs.
E.g.:
barebox@ZII RDU2+ Board:/ gpio_direction_input 205 barebox@ZII RDU2+ Board:/ gpio_get_value 205 barebox@ZII RDU2+ Board:/ echo $?
1
barebox@ZII RDU2+ Board:/ gpio_direction_input 206 barebox@ZII RDU2+ Board:/ gpio_get_value 206 barebox@ZII RDU2+ Board:/ echo $?
0
barebox@ZII RDU2+ Board:/ gpio_direction_output 204 0 barebox@ZII RDU2+ Board:/ gpio_set_value 204 1
===================================================================
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-direction 205 0 0
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-get-value 205
1 $ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-direction 206 0 0
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-get-value 206
0 $ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-direction 200 1 0
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-value 200 1--- scripts/remote/controller.py | 36 ++++++++++++++ scripts/remote/main.py | 37 ++++++++++++++ scripts/remote/messages.py | 93 ++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+)
scripts/remote/controller.py +36 -0
@@ -70,6 +70,24 @@ def unpack(data): elif p_type == BBType.i2c_write_return: logging.debug("received: i2c_write_return") return BBPacketI2cWriteReturn(raw=data) + elif p_type == BBType.gpio_get_value: + logging.debug("received: gpio_get_value") + return BBPacketGpioGetValue(raw=data) + elif p_type == BBType.gpio_get_value_return: + logging.debug("received: gpio_get_value_return") + return BBPacketGpioGetValueReturn(raw=data) + elif p_type == BBType.gpio_set_value: + logging.debug("received: gpio_set_value") + return BBPacketGpioSetValue(raw=data) + elif p_type == BBType.gpio_set_value_return: + logging.debug("received: gpio_set_value_return") + return BBPacketGpioSetValueReturn(raw=data) + elif p_type == BBType.gpio_set_direction: + logging.debug("received: gpio_set_direction") + return BBPacketGpioSetDirection(raw=data) + elif p_type == BBType.gpio_set_direction_return: + logging.debug("received: gpio_set_direction_return") + return BBPacketGpioSetDirectionReturn(raw=data) else: logging.debug("received: UNKNOWN") return BBPacket(raw=data)
@@ -160,6 +178,24 @@ class Controller(Thread): logging.info("i2c write return: %r", r) return (r.exit_code,r.written) + def gpio_get_value(self, gpio): + self._send(BBPacketGpioGetValue(gpio=gpio)) + r = self._expect(BBPacketGpioGetValueReturn) + logging.info("gpio get value return: %r", r) + return r.value + + def gpio_set_value(self, gpio, value): + self._send(BBPacketGpioSetValue(gpio=gpio, value=value)) + r = self._expect(BBPacketGpioSetValueReturn) + logging.info("gpio set value return: %r", r) + return 0 + + def gpio_set_direction(self, gpio, direction, value): + self._send(BBPacketGpioSetDirection(gpio=gpio, direction=direction, value=value)) + r = self._expect(BBPacketGpioSetDirectionReturn) + logging.info("gpio set direction return: %r", r) + return r.exit_code + def reset(self, force): self._send(BBPacketReset(force=force))
scripts/remote/main.py +37 -0
@@ -119,6 +119,28 @@ def handle_i2c_write(args): return res +def handle_gpio_get_value(args): + ctrl = get_controller(args) + value = ctrl.gpio_get_value(args.gpio) + print ("%u" % value); + ctrl.close() + return 0 + + +def handle_gpio_set_value(args): + ctrl = get_controller(args) + ctrl.gpio_set_value(args.gpio, args.value) + ctrl.close() + return 0 + + +def handle_gpio_set_direction(args): + ctrl = get_controller(args) + res = ctrl.gpio_set_direction(args.gpio, args.direction, args.value) + ctrl.close() + return res + + def handle_reset(args): ctrl = get_controller(args) ctrl.reset(args.force)
@@ -225,6 +247,21 @@ parser_i2c_write.add_argument('flags', type=auto_int, help="flags") parser_i2c_write.add_argument('data', help="data") parser_i2c_write.set_defaults(func=handle_i2c_write) +parser_gpio_get_value = subparsers.add_parser('gpio-get-value', help="run gpio get value command") +parser_gpio_get_value.add_argument('gpio', type=auto_int, help="gpio") +parser_gpio_get_value.set_defaults(func=handle_gpio_get_value) + +parser_gpio_set_value = subparsers.add_parser('gpio-set-value', help="run gpio set value command") +parser_gpio_set_value.add_argument('gpio', type=auto_int, help="gpio") +parser_gpio_set_value.add_argument('value', type=auto_int, help="value") +parser_gpio_set_value.set_defaults(func=handle_gpio_set_value) + +parser_gpio_set_direction = subparsers.add_parser('gpio-set-direction', help="run gpio set direction command") +parser_gpio_set_direction.add_argument('gpio', type=auto_int, help="gpio") +parser_gpio_set_direction.add_argument('direction', type=auto_int, help="direction (0: input, 1: output)") +parser_gpio_set_direction.add_argument('value', type=auto_int, help="value (if output)") +parser_gpio_set_direction.set_defaults(func=handle_gpio_set_direction) + parser_reset = subparsers.add_parser('reset', help="run reset command") parser_reset_force = parser_reset.add_mutually_exclusive_group(required=False) parser_reset_force.add_argument('--force', dest='force', action='store_true')
scripts/remote/messages.py +93 -0
102 lines not on this page · raw message
Signature · 7 lines
2.18.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox