diff --git a/platform/hw_utils.c b/platform/hw_utils.c index 876b280..7619775 100644 --- a/platform/hw_utils.c +++ b/platform/hw_utils.c @@ -240,34 +240,45 @@ char * pinmask2str_up(uint32_t pins, char *buffer) return buffer; } -/** Spread packed port pins using a mask */ -uint32_t pinmask_spread(uint32_t packed, uint32_t mask) +/** spread a packed pinfield using a mask */ +uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask) { uint32_t result = 0; uint32_t poke = 1; + if(packed == 0) return 0; + for (int i = 0; i<32; i++) { - if (mask & (1<>= 1; + // if (mask == 0) break; } + return result; } /** Pack spread port pins using a mask */ -uint32_t pinmask_pack(uint32_t spread, uint32_t mask) +uint32_t pinmask_pack_32(uint32_t spread, uint32_t mask) { uint32_t result = 0; uint32_t poke = 1; for (int i = 0; i<32; i++) { - if (mask & (1<>= 1; + // if (mask == 0) break; } return result; } diff --git a/platform/hw_utils.h b/platform/hw_utils.h index 8577cff..8fec5d6 100644 --- a/platform/hw_utils.h +++ b/platform/hw_utils.h @@ -98,7 +98,13 @@ char * pinmask2str_up(uint32_t pins, char *buffer); * @param mask - positions of the bits (eg. 0x8803) * @return - bits spread to their positions (always counting from right) */ -uint32_t pinmask_spread(uint32_t packed, uint32_t mask); +uint32_t pinmask_spread_32(uint32_t packed, uint32_t mask); + +/** Spread packed port pins using a mask - 16-bit version */ +static inline uint16_t pinmask_spread(uint16_t packed, uint16_t mask) +{ + return (uint16_t) pinmask_spread_32(packed, mask); +} /** * Pack spread port pins using a mask @@ -107,7 +113,13 @@ uint32_t pinmask_spread(uint32_t packed, uint32_t mask); * @param mask - mask of the bits we want to pack (eg. 0x8803) * @return - packed bits, right aligned (eg. 0b1110) */ -uint32_t pinmask_pack(uint32_t spread, uint32_t mask); +uint32_t pinmask_pack_32(uint32_t spread, uint32_t mask); + +/** Pack spread port pins using a mask - 16-bit version */ +static inline uint16_t pinmask_pack(uint32_t spread, uint32_t mask) +{ + return (uint16_t) pinmask_pack_32(spread, mask); +} /** * Convert spread port pin number to a packed index using a mask diff --git a/units/digital_out/_dout_settings.c b/units/digital_out/_dout_settings.c index f275504..99cc1b3 100644 --- a/units/digital_out/_dout_settings.c +++ b/units/digital_out/_dout_settings.c @@ -47,13 +47,13 @@ error_t DOut_loadIni(Unit *unit, const char *key, const char *value) suc = parse_port_name(value, &priv->port_name); } else if (streq(key, "pins")) { - priv->pins = parse_pinmask(value, &suc); + priv->pins = (uint16_t) parse_pinmask(value, &suc); } else if (streq(key, "initial")) { - priv->initial = parse_pinmask(value, &suc); + priv->initial = (uint16_t) parse_pinmask(value, &suc); } else if (streq(key, "open-drain")) { - priv->open_drain = parse_pinmask(value, &suc); + priv->open_drain = (uint16_t) parse_pinmask(value, &suc); } else { return E_BAD_KEY; diff --git a/units/sipo/_sipo_api.c b/units/sipo/_sipo_api.c new file mode 100644 index 0000000..e5c0a3c --- /dev/null +++ b/units/sipo/_sipo_api.c @@ -0,0 +1,11 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" +#include "unit_sipo.h" + +#define SIPO_INTERNAL +#include "_sipo_internal.h" + diff --git a/units/sipo/_sipo_init.c b/units/sipo/_sipo_init.c new file mode 100644 index 0000000..337f6b9 --- /dev/null +++ b/units/sipo/_sipo_init.c @@ -0,0 +1,123 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define SIPO_INTERNAL +#include "_sipo_internal.h" + +/** Allocate data structure and set defaults */ +error_t USIPO_preInit(Unit *unit) +{ + struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv)); + if (priv == NULL) return E_OUT_OF_MEM; + + priv->store_pname = 'A'; + priv->store_pnum = 0; + priv->store_pol = true; + + priv->shift_pname = 'A'; + priv->shift_pnum = 1; + priv->shift_pol = true; + + priv->clear_pname = 'A'; + priv->clear_pnum = 2; + priv->clear_pol = false; + + priv->data_pname = 'A'; + priv->data_pins = (1<<3); + + return E_SUCCESS; +} + +/** Finalize unit set-up */ +error_t USIPO_init(Unit *unit) +{ + bool suc = true; + struct priv *priv = unit->data; + + // --- Parse config --- + priv->store_ll = hw_pin2ll(priv->store_pnum, &suc); + priv->store_port = hw_port2periph(priv->store_pname, &suc); + Resource store_rsc = hw_pin2resource(priv->store_pname, priv->store_pnum, &suc); + if (!suc) return E_BAD_CONFIG; + TRY(rsc_claim(unit, store_rsc)); + + priv->shift_ll = hw_pin2ll(priv->shift_pnum, &suc); + priv->shift_port = hw_port2periph(priv->shift_pname, &suc); + Resource shift_rsc = hw_pin2resource(priv->shift_pname, priv->shift_pnum, &suc); + if (!suc) return E_BAD_CONFIG; + TRY(rsc_claim(unit, shift_rsc)); + + priv->clear_ll = hw_pin2ll(priv->clear_pnum, &suc); + priv->clear_port = hw_port2periph(priv->clear_pname, &suc); + Resource clear_rsc = hw_pin2resource(priv->clear_pname, priv->clear_pnum, &suc); + if (!suc) return E_BAD_CONFIG; + TRY(rsc_claim(unit, clear_rsc)); + + // Claim all needed pins + TRY(rsc_claim_gpios(unit, priv->data_pname, priv->data_pins)); + priv->data_port = hw_port2periph(priv->data_pname, &suc); + + // --- Init hardware --- + + priv->data_width = 0; + for (int i = 0; i < 16; i++) { + if (priv->data_pins & (1 << i)) { + uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc); + LL_GPIO_SetPinMode(priv->data_port, ll_pin, LL_GPIO_MODE_OUTPUT); + LL_GPIO_SetPinOutputType(priv->data_port, ll_pin, LL_GPIO_OUTPUT_PUSHPULL); + LL_GPIO_SetPinSpeed(priv->data_port, ll_pin, LL_GPIO_SPEED_FREQ_HIGH); + priv->data_width++; + } + } + + // Set the initial state - zeros + priv->data_port->ODR &= ~priv->data_pins; + + + // STORE + LL_GPIO_SetPinMode(priv->store_port, priv->store_ll, LL_GPIO_MODE_OUTPUT); + LL_GPIO_SetPinOutputType(priv->store_port, priv->store_ll, LL_GPIO_OUTPUT_PUSHPULL); + LL_GPIO_SetPinSpeed(priv->store_port, priv->store_ll, LL_GPIO_SPEED_FREQ_HIGH); + + if (priv->store_pol) + LL_GPIO_ResetOutputPin(priv->store_port, priv->store_ll); + else + LL_GPIO_SetOutputPin(priv->store_port, priv->store_ll); + + // SHIFT + LL_GPIO_SetPinMode(priv->shift_port, priv->shift_ll, LL_GPIO_MODE_OUTPUT); + LL_GPIO_SetPinOutputType(priv->shift_port, priv->shift_ll, LL_GPIO_OUTPUT_PUSHPULL); + LL_GPIO_SetPinSpeed(priv->shift_port, priv->shift_ll, LL_GPIO_SPEED_FREQ_HIGH); + + if (priv->shift_pol) + LL_GPIO_ResetOutputPin(priv->shift_port, priv->shift_ll); + else + LL_GPIO_SetOutputPin(priv->shift_port, priv->shift_ll); + + // CLEAR + LL_GPIO_SetPinMode(priv->clear_port, priv->clear_ll, LL_GPIO_MODE_OUTPUT); + LL_GPIO_SetPinOutputType(priv->clear_port, priv->clear_ll, LL_GPIO_OUTPUT_PUSHPULL); + LL_GPIO_SetPinSpeed(priv->clear_port, priv->clear_ll, LL_GPIO_SPEED_FREQ_HIGH); + + if (priv->clear_pol) + LL_GPIO_ResetOutputPin(priv->clear_port, priv->clear_ll); + else + LL_GPIO_SetOutputPin(priv->clear_port, priv->clear_ll); + + return E_SUCCESS; +} + + +/** Tear down the unit */ +void USIPO_deInit(Unit *unit) +{ + // Release all resources, deinit pins + rsc_teardown(unit); + + // Free memory + free_ck(unit->data); +} diff --git a/units/sipo/_sipo_internal.h b/units/sipo/_sipo_internal.h new file mode 100644 index 0000000..3db7808 --- /dev/null +++ b/units/sipo/_sipo_internal.h @@ -0,0 +1,70 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_SIPO_INTERNAL_H +#define GEX_F072_SIPO_INTERNAL_H + +#ifndef SIPO_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Private data structure */ +struct priv { + // settings + char store_pname; + uint8_t store_pnum; + bool store_pol; //!< Store pulse active edge + + char shift_pname; + uint8_t shift_pnum; + bool shift_pol; //!< Shift clock active edge + + char clear_pname; + uint8_t clear_pnum; + bool clear_pol; //!< Clear signal active level + + char data_pname; + uint16_t data_pins; + + // live fields + uint32_t store_ll; + uint32_t shift_ll; + uint32_t clear_ll; + + GPIO_TypeDef *store_port; + GPIO_TypeDef *shift_port; + GPIO_TypeDef *clear_port; + GPIO_TypeDef *data_port; + + uint8_t data_width; +}; + +/** Allocate data structure and set defaults */ +error_t USIPO_preInit(Unit *unit); + +/** Load from a binary buffer stored in Flash */ +void USIPO_loadBinary(Unit *unit, PayloadParser *pp); + +/** Write to a binary buffer for storing in Flash */ +void USIPO_writeBinary(Unit *unit, PayloadBuilder *pb); + +// ------------------------------------------------------------------------ + +/** Parse a key-value pair from the INI file */ +error_t USIPO_loadIni(Unit *unit, const char *key, const char *value); + +/** Generate INI file section for the unit */ +void USIPO_writeIni(Unit *unit, IniWriter *iw); + +// ------------------------------------------------------------------------ + +/** Finalize unit set-up */ +error_t USIPO_init(Unit *unit); + +/** Tear down the unit */ +void USIPO_deInit(Unit *unit); + +#endif //GEX_F072_SIPO_INTERNAL_H diff --git a/units/sipo/_sipo_settings.c b/units/sipo/_sipo_settings.c new file mode 100644 index 0000000..897928f --- /dev/null +++ b/units/sipo/_sipo_settings.c @@ -0,0 +1,122 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define SIPO_INTERNAL +#include "_sipo_internal.h" + +/** Load from a binary buffer stored in Flash */ +void USIPO_loadBinary(Unit *unit, PayloadParser *pp) +{ + struct priv *priv = unit->data; + + uint8_t version = pp_u8(pp); + (void)version; + + priv->store_pname = pp_char(pp); + priv->store_pnum = pp_u8(pp); + priv->store_pol = pp_bool(pp); + + priv->shift_pname = pp_char(pp); + priv->shift_pnum = pp_u8(pp); + priv->shift_pol = pp_bool(pp); + + priv->clear_pname = pp_char(pp); + priv->clear_pnum = pp_u8(pp); + priv->clear_pol = pp_bool(pp); + + priv->data_pname = pp_char(pp); + priv->data_pins = pp_u16(pp); +} + +/** Write to a binary buffer for storing in Flash */ +void USIPO_writeBinary(Unit *unit, PayloadBuilder *pb) +{ + struct priv *priv = unit->data; + + pb_u8(pb, 0); // version + + pb_char(pb, priv->store_pname); + pb_u8(pb, priv->store_pnum); + pb_bool(pb, priv->store_pol); + + pb_char(pb, priv->shift_pname); + pb_u8(pb, priv->shift_pnum); + pb_bool(pb, priv->shift_pol); + + pb_char(pb, priv->clear_pname); + pb_u8(pb, priv->clear_pnum); + pb_bool(pb, priv->clear_pol); + + pb_char(pb, priv->data_pname); + pb_u16(pb, priv->data_pins); +} + +// ------------------------------------------------------------------------ + +/** Parse a key-value pair from the INI file */ +error_t USIPO_loadIni(Unit *unit, const char *key, const char *value) +{ + bool suc = true; + struct priv *priv = unit->data; + + if (streq(key, "store-pin")) { + suc = parse_pin(value, &priv->store_pname, &priv->store_pnum); + } + else if (streq(key, "shift-pin")) { + suc = parse_pin(value, &priv->shift_pname, &priv->shift_pnum); + } + else if (streq(key, "clear-pin")) { + suc = parse_pin(value, &priv->clear_pname, &priv->clear_pnum); + } + + else if (streq(key, "store-pol")) { + priv->store_pol = (bool) avr_atoi(value); + } + else if (streq(key, "shift-pol")) { + priv->shift_pol = (bool) avr_atoi(value); + } + else if (streq(key, "clear-pol")) { + priv->clear_pol = (bool) avr_atoi(value); + } + + else if (streq(key, "data-port")) { + suc = parse_port_name(value, &priv->data_pname); + } + else if (streq(key, "data-pins")) { + priv->data_pins = (uint16_t) parse_pinmask(value, &suc); + } + + else { + return E_BAD_KEY; + } + + if (!suc) return E_BAD_VALUE; + return E_SUCCESS; +} + +/** Generate INI file section for the unit */ +void USIPO_writeIni(Unit *unit, IniWriter *iw) +{ + struct priv *priv = unit->data; + + iw_comment(iw, "Store pin & its active edge (1-rising,0-falling)"); + iw_entry(iw, "store-pin", "%c%d", priv->store_pname, priv->store_pnum); + iw_entry(iw, "store-pol", "%d", priv->store_pol); + + iw_comment(iw, "Shift pin & its active edge"); + iw_entry(iw, "shift-pin", "%c%d", priv->shift_pname, priv->shift_pnum); + iw_entry(iw, "shift-pol", "%d", priv->shift_pol); + + iw_comment(iw, "Clear pin & its active level"); + iw_entry(iw, "clear-pin", "%c%d", priv->clear_pname, priv->clear_pnum); + iw_entry(iw, "clear-pol", "%d", priv->clear_pol); + + iw_comment(iw, "Data port and pins"); + iw_entry(iw, "data-port", "%c", priv->data_pname); + iw_entry(iw, "data-pins", "%s", pinmask2str(priv->data_pins, unit_tmp512)); +} + diff --git a/units/sipo/unit_sipo.c b/units/sipo/unit_sipo.c new file mode 100644 index 0000000..74aead0 --- /dev/null +++ b/units/sipo/unit_sipo.c @@ -0,0 +1,98 @@ +// +// Created by MightyPork on 2017/11/25. +// + +#include "unit_base.h" +#include "unit_sipo.h" + +#define SIPO_INTERNAL + +#include "_sipo_internal.h" + +error_t UU_SIPO_Write(Unit *unit, const uint8_t *buffer, uint16_t buflen) +{ + struct priv *priv = unit->data; + + if (buflen % priv->data_width != 0) { + return E_BAD_COUNT; // must be a multiple of the channel count + } + + // buffer contains data for the individual data pins, back to back as AAA BBB CCC (whole bytes) + + const uint16_t bytelen = buflen / priv->data_width; + + for (int bn = 0; bn < bytelen; bn--) { + // send the byte + for (int i = 0; i < 8; i++) { + uint16_t packed = 0; + + for (int j = 0; j < priv->data_width; j++) { + packed |= (bool) (buffer[bn + j * bytelen] & (1 << i)); + packed <<= 1; + } + + uint16_t spread = pinmask_spread(packed, priv->data_pins); + uint16_t set = spread; + uint16_t reset = ((~spread) & priv->data_pins); + priv->data_port->BSRR = set | (reset << 16); + + if (priv->shift_pol) { + LL_GPIO_SetOutputPin(priv->shift_port, priv->shift_ll); + LL_GPIO_ResetOutputPin(priv->shift_port, priv->shift_ll); + } + else { + LL_GPIO_ResetOutputPin(priv->shift_port, priv->shift_ll); + LL_GPIO_SetOutputPin(priv->shift_port, priv->shift_ll); + } + } + } + + if (priv->store_pol) { + LL_GPIO_SetOutputPin(priv->store_port, priv->store_ll); + LL_GPIO_ResetOutputPin(priv->store_port, priv->store_ll); + } + else { + LL_GPIO_ResetOutputPin(priv->store_port, priv->store_ll); + LL_GPIO_SetOutputPin(priv->store_port, priv->store_ll); + } +} + +// ------------------------------------------------------------------------ + +enum SipoCmd_ { + CMD_WRITE, +}; + +/** Handle a request message */ +static error_t USIPO_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) +{ + switch (command) { + case CMD_WRITE:; + uint32_t len; + const uint8_t *tail = pp_tail(pp, &len); + UU_SIPO_Write(unit, (uint8_t *) tail, (uint16_t) len); + return E_SUCCESS; + + default: + return E_UNKNOWN_COMMAND; + } +} + +// ------------------------------------------------------------------------ + +/** Unit template */ +const UnitDriver UNIT_SIPO = { + .name = "SIPO", + .description = "Shift register driver (595, 4094)", + // Settings + .preInit = USIPO_preInit, + .cfgLoadBinary = USIPO_loadBinary, + .cfgWriteBinary = USIPO_writeBinary, + .cfgLoadIni = USIPO_loadIni, + .cfgWriteIni = USIPO_writeIni, + // Init + .init = USIPO_init, + .deInit = USIPO_deInit, + // Function + .handleRequest = USIPO_handleRequest, +}; diff --git a/units/sipo/unit_sipo.h b/units/sipo/unit_sipo.h new file mode 100644 index 0000000..498a040 --- /dev/null +++ b/units/sipo/unit_sipo.h @@ -0,0 +1,16 @@ +// +// Created by MightyPork on 2017/11/25. +// +// Digital input unit; single or multiple pin read access on one port (A-F) +// + +#ifndef U_SIPO_H +#define U_SIPO_H + +#include "unit.h" + +extern const UnitDriver UNIT_SIPO; + +// UU_ prototypes + +#endif //U_SIPO_H