From 913f431a54cbeb56bf7027b8e84974c89b084e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 5 Jan 2018 10:11:57 +0100 Subject: [PATCH] stub of SPI write cmd, multicast cmd --- gex.mk | 1 + platform/platform.c | 2 ++ units/spi/unit_spi.c | 63 +++++++++++++++++++++++++++++++++++++------- units/spi/unit_spi.h | 17 +++++++++++- utils/error.h | 1 + 5 files changed, 74 insertions(+), 10 deletions(-) diff --git a/gex.mk b/gex.mk index 7de5234..8eb9e9a 100644 --- a/gex.mk +++ b/gex.mk @@ -11,6 +11,7 @@ GEX_SRC_DIR = \ User/units/digital_out \ User/units/digital_in \ User/units/i2c \ + User/units/spi \ User/TinyFrame \ User/CWPack \ User/tasks diff --git a/platform/platform.c b/platform/platform.c index 95ca54f..bfd439f 100644 --- a/platform/platform.c +++ b/platform/platform.c @@ -13,6 +13,7 @@ #include "units/neopixel/unit_neopixel.h" #include "units/i2c/unit_i2c.h" #include "units/test/unit_test.h" +#include "units/spi/unit_spi.h" void plat_init_resources(void) { @@ -77,6 +78,7 @@ void plat_init_resources(void) ureg_add_type(&UNIT_DIN); ureg_add_type(&UNIT_NEOPIXEL); ureg_add_type(&UNIT_I2C); + ureg_add_type(&UNIT_SPI); // Free all present resources { diff --git a/units/spi/unit_spi.c b/units/spi/unit_spi.c index 043a6f0..df893b8 100644 --- a/units/spi/unit_spi.c +++ b/units/spi/unit_spi.c @@ -417,27 +417,72 @@ static void USPI_deInit(Unit *unit) // ------------------------------------------------------------------------ + +error_t UU_SPI_Multicast(Unit *unit, uint16_t slaves, + const uint8_t *request, + uint32_t req_len) +{ + // + + return E_NOT_IMPLEMENTED; +} + +error_t UU_SPI_Write(Unit *unit, uint8_t slave_num, + const uint8_t *request, + uint8_t *response, + uint32_t req_len, + uint32_t resp_skip, + uint32_t resp_len) +{ + // + + return E_NOT_IMPLEMENTED; +} + + enum PinCmd_ { CMD_WRITE = 0, + CMD_MULTICAST = 1, }; /** Handle a request message */ static error_t USPI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) { - uint16_t addr; - uint32_t len; - uint8_t regnum; - uint32_t size; + uint8_t slave; + uint16_t slaves; + uint16_t req_len; + uint16_t resp_skip; + uint16_t resp_len; + const uint8_t *bb; - // NOTE: 10-bit addresses must have the highest bit set to 1 for indication (0x8000 | addr) + uint32_t len; switch (command) { - /** Write byte(s) - addr:u16, byte(s) */ + /** Write and read byte(s) - slave_num:u8, req_len:u16, resp_skip:u16, resp_len:u16, byte(s) */ case CMD_WRITE: - addr = pp_u16(pp); - const uint8_t *bb = pp_tail(pp, &len); + slave = pp_u8(pp); + req_len = pp_u16(pp); + resp_skip = pp_u16(pp); + resp_len = pp_u16(pp); + + bb = pp_tail(pp, &len); + + TRY(UU_SPI_Write(unit, slave, + bb, (uint8_t *) unit_tmp512, + req_len, resp_skip, resp_len)); + + com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, resp_len); + return E_SUCCESS; + + /** Write byte(s) to multiple slaves - slaves:u16, req_len:u16, byte(s) */ + case CMD_MULTICAST: + slaves = pp_u16(pp); + req_len = pp_u16(pp); + + bb = pp_tail(pp, &len); - return UU_SPI_Write(unit, addr, bb, len); // TODo implement function + SPI pins mapping + TRY(UU_SPI_Multicast(unit, slaves, bb, req_len)); + return E_SUCCESS; default: return E_UNKNOWN_COMMAND; diff --git a/units/spi/unit_spi.h b/units/spi/unit_spi.h index 0374459..bfa3742 100644 --- a/units/spi/unit_spi.h +++ b/units/spi/unit_spi.h @@ -30,7 +30,7 @@ extern const UnitDriver UNIT_SPI; * |<-- resp_len -->| * * @param unit - SPI unit - * @param slave_num - slave number (SS pin index) + * @param slave_num - slave number (SS pin index, counted from least significant bit) * @param request - request bytes buffer * @param response - response bytes buffer * @param req_len - number of bytes in the request. Will be right-padded with zeros. @@ -45,4 +45,19 @@ error_t UU_SPI_Write(Unit *unit, uint8_t slave_num, uint32_t resp_skip, uint32_t resp_len); +/** + * Write to multiple slaves at once. + * This is similar to UU_SPI_Write, but performs no read and works only if the device + * is configured as tx-only. + * + * @param unit - SPI unit + * @param slaves - bitmap of slaves to write (packed bits representing the SSN pins) + * @param request - request bytes buffer + * @param req_len - length of the request buffer + * @return success + */ +error_t UU_SPI_Multicast(Unit *unit, uint16_t slaves, + const uint8_t *request, + uint32_t req_len); + #endif //GEX_F072_UNIT_SPI_H diff --git a/utils/error.h b/utils/error.h index 2e9cc99..9b52727 100644 --- a/utils/error.h +++ b/utils/error.h @@ -42,6 +42,7 @@ X(OVERRUN, NULL) /* used in bulk transfer */ \ X(PROTOCOL_BREACH, NULL) /* eating with the wrong spoon */ \ X(BAD_UNIT_TYPE, NULL) \ + X(NOT_IMPLEMENTED, NULL) \ \ /* VFS user errors (those are meant to be shown to user) */ \ X(ERROR_DURING_TRANSFER, "Error during transfer") \