diff options
author | BIGTREETECH <38851044+bigtreetech@users.noreply.github.com> | 2023-06-08 08:55:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-07 20:55:46 -0400 |
commit | 645a1b8364c3110f706db0f976ac5fa20b968c36 (patch) | |
tree | 6a7d22aeb89c616bbea8c78cc857ab789b5d66cb /src/i2ccmds.c | |
parent | b389c70d5a76f6c8f63283126daecd2e98899f8e (diff) | |
download | kutter-645a1b8364c3110f706db0f976ac5fa20b968c36.tar.gz kutter-645a1b8364c3110f706db0f976ac5fa20b968c36.tar.xz kutter-645a1b8364c3110f706db0f976ac5fa20b968c36.zip |
i2c_software: Implementation of software i2c (#6141)
Signed-off-by: Alan.Ma from BigTreeTech <tech@biqu3d.com>
Diffstat (limited to 'src/i2ccmds.c')
-rw-r--r-- | src/i2ccmds.c | 55 |
1 files changed, 47 insertions, 8 deletions
diff --git a/src/i2ccmds.c b/src/i2ccmds.c index 69af011b..099fe775 100644 --- a/src/i2ccmds.c +++ b/src/i2ccmds.c @@ -9,18 +9,20 @@ #include "command.h" //sendf #include "sched.h" //DECL_COMMAND #include "board/gpio.h" //i2c_write/read/setup +#include "i2c_software.h" // i2c_software_setup #include "i2ccmds.h" +enum { + IF_SOFTWARE = 1, IF_HARDWARE = 2 +}; + void command_config_i2c(uint32_t *args) { - uint8_t addr = args[3] & 0x7f; struct i2cdev_s *i2c = oid_alloc(args[0], command_config_i2c , sizeof(*i2c)); - i2c->i2c_config = i2c_setup(args[1], args[2], addr); } -DECL_COMMAND(command_config_i2c, - "config_i2c oid=%c i2c_bus=%u rate=%u address=%u"); +DECL_COMMAND(command_config_i2c, "config_i2c oid=%c"); struct i2cdev_s * i2cdev_oid_lookup(uint8_t oid) @@ -29,13 +31,35 @@ i2cdev_oid_lookup(uint8_t oid) } void +command_i2c_set_bus(uint32_t *args) +{ + uint8_t addr = args[3] & 0x7f; + struct i2cdev_s *i2c = i2cdev_oid_lookup(args[0]); + i2c->i2c_config = i2c_setup(args[1], args[2], addr); + i2c->flags |= IF_HARDWARE; +} +DECL_COMMAND(command_i2c_set_bus, + "i2c_set_bus oid=%c i2c_bus=%u rate=%u address=%u"); + +void +i2cdev_set_software_bus(struct i2cdev_s *i2c, struct i2c_software *is) +{ + i2c->i2c_software = is; + i2c->flags |= IF_SOFTWARE; +} + +void command_i2c_write(uint32_t *args) { uint8_t oid = args[0]; struct i2cdev_s *i2c = oid_lookup(oid, command_config_i2c); uint8_t data_len = args[1]; uint8_t *data = command_decode_ptr(args[2]); - i2c_write(i2c->i2c_config, data_len, data); + uint_fast8_t flags = i2c->flags; + if (flags & IF_SOFTWARE) + i2c_software_write(i2c->i2c_software, data_len, data); + else + i2c_write(i2c->i2c_config, data_len, data); } DECL_COMMAND(command_i2c_write, "i2c_write oid=%c data=%*s"); @@ -48,7 +72,13 @@ command_i2c_read(uint32_t * args) uint8_t *reg = command_decode_ptr(args[2]); uint8_t data_len = args[3]; uint8_t data[data_len]; - i2c_read(i2c->i2c_config, reg_len, reg, data_len, data); + uint_fast8_t flags = i2c->flags; + if (flags & IF_SOFTWARE) + i2c_software_read( + i2c->i2c_software, reg_len, reg, data_len, data); + else + i2c_read( + i2c->i2c_config, reg_len, reg, data_len, data); sendf("i2c_read_response oid=%c response=%*s", oid, data_len, data); } DECL_COMMAND(command_i2c_read, "i2c_read oid=%c reg=%*s read_len=%u"); @@ -66,13 +96,22 @@ command_i2c_modify_bits(uint32_t *args) uint8_t data_len = clear_set_len/2; uint8_t *clear_set = command_decode_ptr(args[4]); uint8_t receive_data[reg_len + data_len]; + uint_fast8_t flags = i2c->flags; memcpy(receive_data, reg, reg_len); - i2c_read(i2c->i2c_config, reg_len, reg, data_len, receive_data + reg_len); + if (flags & IF_SOFTWARE) + i2c_software_read( + i2c->i2c_software, reg_len, reg, data_len, receive_data + reg_len); + else + i2c_read( + i2c->i2c_config, reg_len, reg, data_len, receive_data + reg_len); for (int i = 0; i < data_len; i++) { receive_data[reg_len + i] &= ~clear_set[i]; receive_data[reg_len + i] |= clear_set[data_len + i]; } - i2c_write(i2c->i2c_config, reg_len + data_len, receive_data); + if (flags & IF_SOFTWARE) + i2c_software_write(i2c->i2c_software, reg_len + data_len, receive_data); + else + i2c_write(i2c->i2c_config, reg_len + data_len, receive_data); } DECL_COMMAND(command_i2c_modify_bits, "i2c_modify_bits oid=%c reg=%*s clear_set_bits=%*s"); |