From 8a640043f1f2b99700a69b868a2fa12717ea76f6 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 26 May 2012 13:55:10 -0700 Subject: [PATCH] stm32: i2c: Simplify host_send_response() Simplify this code, so that it does only one pass of the response data, calculating the checksum as it copies. BUG=chrome-os-partner:10535 TEST=manual: build for all boards build and boot on daisy see that keyboard still works Change-Id: I8b0a2ce1632dd4c1ff54736fb636d9a5cf8c9214 Signed-off-by: Simon Glass Reviewed-on: https://gerrit.chromium.org/gerrit/25981 --- chip/stm32/i2c.c | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/chip/stm32/i2c.c b/chip/stm32/i2c.c index 7717dc280c..e4840e786e 100644 --- a/chip/stm32/i2c.c +++ b/chip/stm32/i2c.c @@ -121,36 +121,23 @@ static int i2c_write_raw(int port, void *buf, int len) return len; } -static void _send_result(int slot, enum ec_status result, int size) -{ - int i; - int len = 1; - uint8_t sum = 0; - - ASSERT(slot == 0); - /* record the error code */ - host_buffer[0] = result; - if (size) { - /* compute checksum */ - for (i = 1; i <= size; i++) - sum += host_buffer[i]; - host_buffer[size + 1] = sum; - len = size + 2; - } - - /* send the answer to the AP */ - i2c_write_raw(I2C2, host_buffer, len); -} - void host_send_response(int slot, enum ec_status result, const uint8_t *data, int size) { - uint8_t *out = host_get_buffer(slot); + uint8_t *out = host_buffer; + int sum, i; - if (size > 0 && data != out) - memcpy(out, data, size); + ASSERT(slot == 0); + *out++ = result; + for (i = 0, sum = 0; i < size; i++, data++, out++) { + if (data != out) + *out = *data; + sum += *data; + } + *out++ = sum & 0xff; - _send_result(slot, result, size); + /* send the answer to the AP */ + i2c_write_raw(I2C2, host_buffer, out - host_buffer); } uint8_t *host_get_buffer(int slot)