diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2020-10-29 23:54:17 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2020-10-30 14:04:08 -0400 |
commit | 473828ca6aef18c574b8665ae484513e5592af03 (patch) | |
tree | 74d6b8f205ce92282475ad0f6575ea0c5af01fc2 /src/i2ccmds.c | |
parent | aaf3dc6ac3e3a38b0dd3508d72594214ede27c5c (diff) | |
download | kutter-473828ca6aef18c574b8665ae484513e5592af03.tar.gz kutter-473828ca6aef18c574b8665ae484513e5592af03.tar.xz kutter-473828ca6aef18c574b8665ae484513e5592af03.zip |
command: Add command_decode_ptr() helper
Add a helper function to convert from a string buffer passed in the
args[] parameter to an actual pointer. This avoids all the callers
needing to perfrom pointer manipulation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/i2ccmds.c')
-rw-r--r-- | src/i2ccmds.c | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/i2ccmds.c b/src/i2ccmds.c index 388af6ed..cde0f6fa 100644 --- a/src/i2ccmds.c +++ b/src/i2ccmds.c @@ -4,6 +4,7 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. +#include <string.h> // memcpy #include "basecmd.h" //oid_alloc #include "command.h" //sendf #include "sched.h" //DECL_COMMAND @@ -30,7 +31,7 @@ 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 = (void*)(size_t)args[2]; + uint8_t *data = command_decode_ptr(args[2]); i2c_write(i2c->i2c_config, data_len, data); } DECL_COMMAND(command_i2c_write, "i2c_write oid=%c data=%*s"); @@ -41,10 +42,9 @@ command_i2c_read(uint32_t * args) uint8_t oid = args[0]; struct i2cdev_s *i2c = oid_lookup(oid, command_config_i2c); uint8_t reg_len = args[1]; - uint8_t *reg = (void*)(size_t)args[2]; + uint8_t *reg = command_decode_ptr(args[2]); uint8_t data_len = args[3]; - uint8_t receive_array[data_len]; - uint8_t *data = (void*)(size_t)receive_array; + uint8_t data[data_len]; i2c_read(i2c->i2c_config, reg_len, reg, data_len, data); sendf("i2c_read_response oid=%c response=%*s", oid, data_len, data); } @@ -56,24 +56,20 @@ command_i2c_modify_bits(uint32_t *args) uint8_t oid = args[0]; struct i2cdev_s *i2c = oid_lookup(oid, command_config_i2c); uint8_t reg_len = args[1]; - uint8_t *reg = (void*)(size_t)args[2]; + uint8_t *reg = command_decode_ptr(args[2]); uint32_t clear_set_len = args[3]; - if (clear_set_len % 2 != 0) { + if (clear_set_len % 2 != 0) shutdown("i2c_modify_bits: Odd number of bits!"); - } uint8_t data_len = clear_set_len/2; - uint8_t *clear_set = (void*)(size_t)args[4]; - uint8_t receive_array[reg_len + data_len]; - uint8_t *receive_data = (void*)(size_t)receive_array; - for (int i = 0; i < reg_len; i++) { - *(receive_data + i) = *(reg + i); - } + uint8_t *clear_set = command_decode_ptr(args[4]); + uint8_t receive_data[reg_len + data_len]; + memcpy(receive_data, reg, reg_len); 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 + clear_set_len/2 + 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_array); + 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"); |