From f1b8db78d400f3408ead2eac6a3f3c4635465ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 3 Feb 2018 10:27:37 +0100 Subject: [PATCH] split DIN --- units/digital_in/_din_api.c | 71 ++++++ units/digital_in/_din_exti.c | 81 +++++++ units/digital_in/_din_exti.h | 21 ++ units/digital_in/_din_init.c | 129 +++++++++++ units/digital_in/_din_init.h | 20 ++ units/digital_in/_din_internal.h | 31 +++ units/digital_in/_din_settings.c | 124 ++++++++++ units/digital_in/_din_settings.h | 31 +++ units/digital_in/unit_din.c | 380 +------------------------------ units/usart/_usart_internal.h | 1 - 10 files changed, 512 insertions(+), 377 deletions(-) create mode 100644 units/digital_in/_din_api.c create mode 100644 units/digital_in/_din_exti.c create mode 100644 units/digital_in/_din_exti.h create mode 100644 units/digital_in/_din_init.c create mode 100644 units/digital_in/_din_init.h create mode 100644 units/digital_in/_din_internal.h create mode 100644 units/digital_in/_din_settings.c create mode 100644 units/digital_in/_din_settings.h diff --git a/units/digital_in/_din_api.c b/units/digital_in/_din_api.c new file mode 100644 index 0000000..43ebcfe --- /dev/null +++ b/units/digital_in/_din_api.c @@ -0,0 +1,71 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" +#include "unit_din.h" + +#define DIN_INTERNAL + +#include "_din_internal.h" + +/** Read request */ +error_t UU_DI_Read(Unit *unit, uint16_t *packed) +{ + CHECK_TYPE(unit, &UNIT_DIN); + struct priv *priv = unit->data; + *packed = pinmask_pack((uint16_t) priv->port->IDR, priv->pins); + return E_SUCCESS; +} + +/** Arm pins */ +error_t UU_DI_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_packed) +{ + CHECK_TYPE(unit, &UNIT_DIN); + struct priv *priv = unit->data; + + uint16_t arm_single = pinmask_spread(arm_single_packed, priv->pins); + uint16_t arm_auto = pinmask_spread(arm_auto_packed, priv->pins); + + // abort if user tries to arm pin that doesn't have a trigger configured + if (0 != ((arm_single | arm_auto) & ~(priv->trig_fall | priv->trig_rise))) { + return E_BAD_VALUE; + } + + // arm and reset hold-offs + // we use critical section to avoid irq between the two steps + vPortEnterCritical(); + { + priv->arm_auto |= arm_single; + priv->arm_single |= arm_auto; + const uint16_t combined = arm_single | arm_auto; + for (int i = 0; i < 16; i++) { + if (combined & (1 << i)) { + priv->holdoff_countdowns[i] = 0; + } + } + } + vPortExitCritical(); + + return E_SUCCESS; +} + +/** DisArm pins */ +error_t UU_DI_DisArm(Unit *unit, uint16_t disarm_packed) +{ + CHECK_TYPE(unit, &UNIT_DIN); + struct priv *priv = unit->data; + + uint16_t disarm = pinmask_spread(disarm_packed, priv->pins); + + // abort if user tries to disarm pin that doesn't have a trigger configured + if (0 != ((disarm) & ~(priv->trig_fall | priv->trig_rise))) { + return E_BAD_VALUE; + } + + priv->arm_auto &= ~disarm; + priv->arm_single &= ~disarm; + + return E_SUCCESS; +} diff --git a/units/digital_in/_din_exti.c b/units/digital_in/_din_exti.c new file mode 100644 index 0000000..f5e3328 --- /dev/null +++ b/units/digital_in/_din_exti.c @@ -0,0 +1,81 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define DIN_INTERNAL + +#include "_din_internal.h" +#include "_din_exti.h" + +/** + * Send a trigger event to master (called on the message queue thread). + * + * unit - unit + * timestamp - timestamp + * data1 - packed, triggering pin + * data2 - snapshot + */ +static void DI_SendTriggerReportToMaster(Job *job) +{ + PayloadBuilder pb = pb_start(unit_tmp512, UNIT_TMP_LEN, NULL); + pb_u16(&pb, (uint16_t) job->data1); // packed, 1 on the triggering pin + pb_u16(&pb, (uint16_t) job->data2); // packed, snapshot + assert_param(pb.ok); + + EventReport event = { + .unit = job->unit, + .timestamp = job->timestamp, + .data = pb.start, + .length = (uint16_t) pb_length(&pb), + }; + + EventReport_Send(&event); +} + +/** + * EXTI callback for pin change interrupts + * + * @param arg - the unit is passed here + */ +void DI_handleExti(void *arg) +{ + const uint64_t ts = PTIM_GetMicrotime(); + + Unit *unit = arg; + struct priv *priv = unit->data; + const uint16_t snapshot = (uint16_t) priv->port->IDR; + + uint16_t trigger_map = 0; + + uint16_t mask = 1; + const uint16_t armed_pins = priv->arm_single | priv->arm_auto; + for (int i = 0; i < 16; i++, mask <<= 1) { + if (!LL_EXTI_ReadFlag_0_31(LL_EXTI_LINES[i])) continue; + LL_EXTI_ClearFlag_0_31(LL_EXTI_LINES[i]); + + // Armed and ready + if ((armed_pins & mask) && (priv->holdoff_countdowns[i] == 0)) { + // Mark as captured + trigger_map |= (1 << i); + // Start hold-off (no-op if zero hold-off) + priv->holdoff_countdowns[i] = priv->trig_holdoff; + } + } + + // Disarm all possibly used single triggers + priv->arm_single &= ~trigger_map; + + if (trigger_map != 0) { + Job j = { + .unit = unit, + .timestamp = ts, + .data1 = pinmask_pack(trigger_map, priv->pins), + .data2 = pinmask_pack(snapshot, priv->pins), + .cb = DI_SendTriggerReportToMaster + }; + scheduleJob(&j); + } +} diff --git a/units/digital_in/_din_exti.h b/units/digital_in/_din_exti.h new file mode 100644 index 0000000..0ce6ff7 --- /dev/null +++ b/units/digital_in/_din_exti.h @@ -0,0 +1,21 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_DIN_EXTI_H +#define GEX_F072_DIN_EXTI_H + +#ifndef DIN_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** + * EXTI callback for pin change interrupts + * + * @param arg - the unit is passed here + */ +void DI_handleExti(void *arg); + +#endif //GEX_F072_DIN_EXTI_H diff --git a/units/digital_in/_din_init.c b/units/digital_in/_din_init.c new file mode 100644 index 0000000..8ef7cc6 --- /dev/null +++ b/units/digital_in/_din_init.c @@ -0,0 +1,129 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define DIN_INTERNAL +#include "_din_internal.h" +#include "_din_init.h" +#include "_din_exti.h" + +/** Allocate data structure and set defaults */ +error_t DI_preInit(Unit *unit) +{ + struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv)); + if (priv == NULL) return E_OUT_OF_MEM; + + // some defaults + priv->port_name = 'A'; + priv->pins = 0x0001; + priv->pulldown = 0x0000; + priv->pullup = 0x0000; + + priv->trig_rise = 0x0000; + priv->trig_fall = 0x0000; + priv->trig_holdoff = 100; + priv->def_auto = 0x0000; + + return E_SUCCESS; +} + +/** Finalize unit set-up */ +error_t DI_init(Unit *unit) +{ + bool suc = true; + struct priv *priv = unit->data; + + priv->pulldown &= priv->pins; + priv->pullup &= priv->pins; + priv->trig_rise &= priv->pins; + priv->trig_fall &= priv->pins; + priv->def_auto &= (priv->trig_rise|priv->trig_fall); + + // copy auto-arm defaults to the auto-arm register (the register may be manipulated by commands) + priv->arm_auto = priv->def_auto; + priv->arm_single = 0; + + // clear countdowns + memset(priv->holdoff_countdowns, 0, sizeof(priv->holdoff_countdowns)); + + // --- Parse config --- + priv->port = hw_port2periph(priv->port_name, &suc); + if (!suc) return E_BAD_CONFIG; + + // Claim all needed pins + TRY(rsc_claim_gpios(unit, priv->port_name, priv->pins)); + + uint16_t mask = 1; + for (int i = 0; i < 16; i++, mask <<= 1) { + if (priv->pins & mask) { + uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc); + + // --- Init hardware --- + LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_INPUT); + + uint32_t pull = 0; + +#if PLAT_NO_FLOATING_INPUTS + pull = LL_GPIO_PULL_UP; +#else + pull = LL_GPIO_PULL_NO; +#endif + + if (priv->pulldown & mask) pull = LL_GPIO_PULL_DOWN; + if (priv->pullup & mask) pull = LL_GPIO_PULL_UP; + LL_GPIO_SetPinPull(priv->port, ll_pin, pull); + + if ((priv->trig_rise|priv->trig_fall) & mask) { + LL_EXTI_EnableIT_0_31(LL_EXTI_LINES[i]); + + if (priv->trig_rise & mask) { + LL_EXTI_EnableRisingTrig_0_31(LL_EXTI_LINES[i]); + } + if (priv->trig_fall & mask) { + LL_EXTI_EnableFallingTrig_0_31(LL_EXTI_LINES[i]); + } + + LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTS[priv->port_name-'A'], LL_SYSCFG_EXTI_LINES[i]); + + irqd_attach(EXTIS[i], DI_handleExti, unit); + } + } + } + + // request ticks if we have triggers and any hold-offs configured + if ((priv->trig_rise|priv->trig_fall) && priv->trig_holdoff > 0) { + unit->tick_interval = 1; + } + + return E_SUCCESS; +} + + +/** Tear down the unit */ +void DI_deInit(Unit *unit) +{ + struct priv *priv = unit->data; + + // pins are de-inited during teardown + + // Detach EXTI handlers and disable interrupts + const uint16_t triggs = priv->trig_rise | priv->trig_fall; + if (unit->status == E_SUCCESS && triggs) { + uint16_t mask = 1; + for (int i = 0; i < 16; i++, mask <<= 1) { + if (triggs & mask) { + LL_EXTI_DisableIT_0_31(LL_EXTI_LINES[i]); + irqd_detach(EXTIS[i], DI_handleExti); + } + } + } + + // Release all resources + rsc_teardown(unit); + + // Free memory + free_ck(unit->data); +} diff --git a/units/digital_in/_din_init.h b/units/digital_in/_din_init.h new file mode 100644 index 0000000..7490e75 --- /dev/null +++ b/units/digital_in/_din_init.h @@ -0,0 +1,20 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_DIN_INIT_H +#define GEX_F072_DIN_INIT_H + +#ifndef DIN_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Finalize unit set-up */ +error_t DI_init(Unit *unit); + +/** Tear down the unit */ +void DI_deInit(Unit *unit); + +#endif //GEX_F072_DIN_INIT_H diff --git a/units/digital_in/_din_internal.h b/units/digital_in/_din_internal.h new file mode 100644 index 0000000..1fcfa6a --- /dev/null +++ b/units/digital_in/_din_internal.h @@ -0,0 +1,31 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_DIN_INTERNAL_H +#define GEX_F072_DIN_INTERNAL_H + +#ifndef DIN_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Private data structure */ +struct priv { + char port_name; + uint16_t pins; // pin mask + uint16_t pulldown; // pull-downs (default is pull-up) + uint16_t pullup; // pull-ups + uint16_t trig_rise; // pins generating events on rising edge + uint16_t trig_fall; // pins generating events on falling edge + uint16_t trig_holdoff; // ms + uint16_t def_auto; // initial auto triggers + + uint16_t arm_auto; // pins armed for auto reporting + uint16_t arm_single; // pins armed for single event + uint16_t holdoff_countdowns[16]; // countdowns to arm for each pin in the bit map + GPIO_TypeDef *port; +}; + +#endif //GEX_F072_DIN_INTERNAL_H diff --git a/units/digital_in/_din_settings.c b/units/digital_in/_din_settings.c new file mode 100644 index 0000000..3324e14 --- /dev/null +++ b/units/digital_in/_din_settings.c @@ -0,0 +1,124 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#include "platform.h" +#include "unit_base.h" + +#define DIN_INTERNAL +#include "_din_internal.h" +#include "_din_settings.h" + +/** Load from a binary buffer stored in Flash */ +void DI_loadBinary(Unit *unit, PayloadParser *pp) +{ + struct priv *priv = unit->data; + + uint8_t version = pp_u8(pp); + (void)version; + + priv->port_name = pp_char(pp); + priv->pins = pp_u16(pp); + priv->pulldown = pp_u16(pp); + priv->pullup = pp_u16(pp); + + if (version >= 1) { + priv->trig_rise = pp_u16(pp); + priv->trig_fall = pp_u16(pp); + priv->trig_holdoff = pp_u16(pp); + } + if (version >= 2) { + priv->def_auto = pp_u16(pp); + } +} + +/** Write to a binary buffer for storing in Flash */ +void DI_writeBinary(Unit *unit, PayloadBuilder *pb) +{ + struct priv *priv = unit->data; + + pb_u8(pb, 2); // version + + pb_char(pb, priv->port_name); + pb_u16(pb, priv->pins); + pb_u16(pb, priv->pulldown); + pb_u16(pb, priv->pullup); + pb_u16(pb, priv->trig_rise); + pb_u16(pb, priv->trig_fall); + pb_u16(pb, priv->trig_holdoff); + pb_u16(pb, priv->def_auto); +} + +// ------------------------------------------------------------------------ + +/** Parse a key-value pair from the INI file */ +error_t DI_loadIni(Unit *unit, const char *key, const char *value) +{ + bool suc = true; + struct priv *priv = unit->data; + + if (streq(key, "port")) { + suc = parse_port_name(value, &priv->port_name); + } + else if (streq(key, "pins")) { + priv->pins = parse_pinmask(value, &suc); + } + else if (streq(key, "pull-up")) { + priv->pullup = parse_pinmask(value, &suc); + } + else if (streq(key, "pull-down")) { + priv->pulldown = parse_pinmask(value, &suc); + } + else if (streq(key, "trig-rise")) { + priv->trig_rise = parse_pinmask(value, &suc); + } + else if (streq(key, "trig-fall")) { + priv->trig_fall = parse_pinmask(value, &suc); + } + else if (streq(key, "auto-trigger")) { + priv->def_auto = parse_pinmask(value, &suc); + } + else if (streq(key, "hold-off")) { + priv->trig_holdoff = (uint16_t) avr_atoi(value); + } + else { + return E_BAD_KEY; + } + + if (!suc) return E_BAD_VALUE; + return E_SUCCESS; +} + +/** Generate INI file section for the unit */ +void DI_writeIni(Unit *unit, IniWriter *iw) +{ + struct priv *priv = unit->data; + + iw_comment(iw, "Port name"); + iw_entry(iw, "port", "%c", priv->port_name); + + iw_comment(iw, "Pins (comma separated, supports ranges)"); + iw_entry(iw, "pins", "%s", pinmask2str(priv->pins, unit_tmp512)); + + iw_comment(iw, "Pins with pull-up"); + iw_entry(iw, "pull-up", "%s", pinmask2str(priv->pullup, unit_tmp512)); + + iw_comment(iw, "Pins with pull-down"); + iw_entry(iw, "pull-down", "%s", pinmask2str(priv->pulldown, unit_tmp512)); + + iw_cmt_newline(iw); + iw_comment(iw, "Trigger pins activated by rising/falling edge"); + iw_entry(iw, "trig-rise", "%s", pinmask2str(priv->trig_rise, unit_tmp512)); + iw_entry(iw, "trig-fall", "%s", pinmask2str(priv->trig_fall, unit_tmp512)); + + iw_comment(iw, "Trigger pins auto-armed by default"); + iw_entry(iw, "auto-trigger", "%s", pinmask2str(priv->def_auto, unit_tmp512)); + + iw_comment(iw, "Triggers hold-off time (ms)"); + iw_entry(iw, "hold-off", "%d", (int)priv->trig_holdoff); + +#if PLAT_NO_FLOATING_INPUTS + iw_comment(iw, "NOTE: Pins use pull-up by default.\r\n"); +#endif +} + diff --git a/units/digital_in/_din_settings.h b/units/digital_in/_din_settings.h new file mode 100644 index 0000000..ab4554e --- /dev/null +++ b/units/digital_in/_din_settings.h @@ -0,0 +1,31 @@ +// +// Created by MightyPork on 2018/02/03. +// + +#ifndef GEX_F072_DIN_SETTINGS_H +#define GEX_F072_DIN_SETTINGS_H + +#ifndef DIN_INTERNAL +#error bad include! +#endif + +#include "unit_base.h" + +/** Allocate data structure and set defaults */ +error_t DI_preInit(Unit *unit); + +/** Load from a binary buffer stored in Flash */ +void DI_loadBinary(Unit *unit, PayloadParser *pp); + +/** Write to a binary buffer for storing in Flash */ +void DI_writeBinary(Unit *unit, PayloadBuilder *pb); + +// ------------------------------------------------------------------------ + +/** Parse a key-value pair from the INI file */ +error_t DI_loadIni(Unit *unit, const char *key, const char *value); + +/** Generate INI file section for the unit */ +void DI_writeIni(Unit *unit, IniWriter *iw); + +#endif //GEX_F072_DIN_SETTINGS_H diff --git a/units/digital_in/unit_din.c b/units/digital_in/unit_din.c index fe18232..773e87b 100644 --- a/units/digital_in/unit_din.c +++ b/units/digital_in/unit_din.c @@ -5,385 +5,13 @@ #include "unit_base.h" #include "unit_din.h" -/** Private data structure */ -struct priv { - char port_name; - uint16_t pins; // pin mask - uint16_t pulldown; // pull-downs (default is pull-up) - uint16_t pullup; // pull-ups - uint16_t trig_rise; // pins generating events on rising edge - uint16_t trig_fall; // pins generating events on falling edge - uint16_t trig_holdoff; // ms - uint16_t def_auto; // initial auto triggers - - uint16_t arm_auto; // pins armed for auto reporting - uint16_t arm_single; // pins armed for single event - uint16_t holdoff_countdowns[16]; // countdowns to arm for each pin in the bit map - GPIO_TypeDef *port; -}; +#define DIN_INTERNAL +#include "_din_internal.h" +#include "_din_settings.h" +#include "_din_init.h" // ------------------------------------------------------------------------ -/** Load from a binary buffer stored in Flash */ -static void DI_loadBinary(Unit *unit, PayloadParser *pp) -{ - struct priv *priv = unit->data; - - uint8_t version = pp_u8(pp); - (void)version; - - priv->port_name = pp_char(pp); - priv->pins = pp_u16(pp); - priv->pulldown = pp_u16(pp); - priv->pullup = pp_u16(pp); - - if (version >= 1) { - priv->trig_rise = pp_u16(pp); - priv->trig_fall = pp_u16(pp); - priv->trig_holdoff = pp_u16(pp); - } - if (version >= 2) { - priv->def_auto = pp_u16(pp); - } -} - -/** Write to a binary buffer for storing in Flash */ -static void DI_writeBinary(Unit *unit, PayloadBuilder *pb) -{ - struct priv *priv = unit->data; - - pb_u8(pb, 2); // version - - pb_char(pb, priv->port_name); - pb_u16(pb, priv->pins); - pb_u16(pb, priv->pulldown); - pb_u16(pb, priv->pullup); - pb_u16(pb, priv->trig_rise); - pb_u16(pb, priv->trig_fall); - pb_u16(pb, priv->trig_holdoff); - pb_u16(pb, priv->def_auto); -} - -// ------------------------------------------------------------------------ - -/** Parse a key-value pair from the INI file */ -static error_t DI_loadIni(Unit *unit, const char *key, const char *value) -{ - bool suc = true; - struct priv *priv = unit->data; - - if (streq(key, "port")) { - suc = parse_port_name(value, &priv->port_name); - } - else if (streq(key, "pins")) { - priv->pins = parse_pinmask(value, &suc); - } - else if (streq(key, "pull-up")) { - priv->pullup = parse_pinmask(value, &suc); - } - else if (streq(key, "pull-down")) { - priv->pulldown = parse_pinmask(value, &suc); - } - else if (streq(key, "trig-rise")) { - priv->trig_rise = parse_pinmask(value, &suc); - } - else if (streq(key, "trig-fall")) { - priv->trig_fall = parse_pinmask(value, &suc); - } - else if (streq(key, "auto-trigger")) { - priv->def_auto = parse_pinmask(value, &suc); - } - else if (streq(key, "hold-off")) { - priv->trig_holdoff = (uint16_t) avr_atoi(value); - } - else { - return E_BAD_KEY; - } - - if (!suc) return E_BAD_VALUE; - return E_SUCCESS; -} - -/** Generate INI file section for the unit */ -static void DI_writeIni(Unit *unit, IniWriter *iw) -{ - struct priv *priv = unit->data; - - iw_comment(iw, "Port name"); - iw_entry(iw, "port", "%c", priv->port_name); - - iw_comment(iw, "Pins (comma separated, supports ranges)"); - iw_entry(iw, "pins", "%s", pinmask2str(priv->pins, unit_tmp512)); - - iw_comment(iw, "Pins with pull-up"); - iw_entry(iw, "pull-up", "%s", pinmask2str(priv->pullup, unit_tmp512)); - - iw_comment(iw, "Pins with pull-down"); - iw_entry(iw, "pull-down", "%s", pinmask2str(priv->pulldown, unit_tmp512)); - - iw_cmt_newline(iw); - iw_comment(iw, "Trigger pins activated by rising/falling edge"); - iw_entry(iw, "trig-rise", "%s", pinmask2str(priv->trig_rise, unit_tmp512)); - iw_entry(iw, "trig-fall", "%s", pinmask2str(priv->trig_fall, unit_tmp512)); - - iw_comment(iw, "Trigger pins auto-armed by default"); - iw_entry(iw, "auto-trigger", "%s", pinmask2str(priv->def_auto, unit_tmp512)); - - iw_comment(iw, "Triggers hold-off time (ms)"); - iw_entry(iw, "hold-off", "%d", (int)priv->trig_holdoff); - -#if PLAT_NO_FLOATING_INPUTS - iw_comment(iw, "NOTE: Pins use pull-up by default.\r\n"); -#endif -} - -// ------------------------------------------------------------------------ - -/** Allocate data structure and set defaults */ -static error_t DI_preInit(Unit *unit) -{ - struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv)); - if (priv == NULL) return E_OUT_OF_MEM; - - // some defaults - priv->port_name = 'A'; - priv->pins = 0x0001; - priv->pulldown = 0x0000; - priv->pullup = 0x0000; - - priv->trig_rise = 0x0000; - priv->trig_fall = 0x0000; - priv->trig_holdoff = 100; - priv->def_auto = 0x0000; - - return E_SUCCESS; -} - -/** - * Send a trigger event to master (called on the message queue thread) - */ -static void ID_SendTriggerReportToMaster(Job *job) -{ - PayloadBuilder pb = pb_start(unit_tmp512, UNIT_TMP_LEN, NULL); - pb_u16(&pb, (uint16_t) job->data1); // packed, 1 on the triggering pin - pb_u16(&pb, (uint16_t) job->data2); // packed, snapshot - assert_param(pb.ok); - - EventReport event = { - .unit = job->unit, - .timestamp = job->timestamp, - .data = pb.start, - .length = (uint16_t) pb_length(&pb), - }; - - EventReport_Send(&event); -} - -/** - * EXTI callback for pin change interrupts - * - * @param arg - the unit is passed here - */ -static void DI_handleExti(void *arg) -{ - const uint64_t ts = PTIM_GetMicrotime(); - - Unit *unit = arg; - struct priv *priv = unit->data; - const uint16_t snapshot = (uint16_t) priv->port->IDR; - - uint16_t trigger_map = 0; - - uint16_t mask = 1; - const uint16_t armed_pins = priv->arm_single|priv->arm_auto; - for (int i = 0; i < 16; i++, mask <<= 1) { - if (!LL_EXTI_ReadFlag_0_31(LL_EXTI_LINES[i])) continue; - LL_EXTI_ClearFlag_0_31(LL_EXTI_LINES[i]); - - // Armed and ready - if ((armed_pins & mask) && (priv->holdoff_countdowns[i] == 0)) { - // Mark as captured - trigger_map |= (1 << i); - // Start hold-off (no-op if zero hold-off) - priv->holdoff_countdowns[i] = priv->trig_holdoff; - } - } - - // Disarm all possibly used single triggers - priv->arm_single &= ~trigger_map; - - if (trigger_map != 0) { - Job j = { - .unit = unit, - .timestamp = ts, - .data1 = pinmask_pack(trigger_map, priv->pins), - .data2 = pinmask_pack(snapshot, priv->pins), - .cb = ID_SendTriggerReportToMaster - }; - scheduleJob(&j); - } -} - -/** Finalize unit set-up */ -static error_t DI_init(Unit *unit) -{ - bool suc = true; - struct priv *priv = unit->data; - - priv->pulldown &= priv->pins; - priv->pullup &= priv->pins; - priv->trig_rise &= priv->pins; - priv->trig_fall &= priv->pins; - priv->def_auto &= (priv->trig_rise|priv->trig_fall); - - // copy auto-arm defaults to the auto-arm register (the register may be manipulated by commands) - priv->arm_auto = priv->def_auto; - priv->arm_single = 0; - - // clear countdowns - memset(priv->holdoff_countdowns, 0, sizeof(priv->holdoff_countdowns)); - - // --- Parse config --- - priv->port = hw_port2periph(priv->port_name, &suc); - if (!suc) return E_BAD_CONFIG; - - // Claim all needed pins - TRY(rsc_claim_gpios(unit, priv->port_name, priv->pins)); - - uint16_t mask = 1; - for (int i = 0; i < 16; i++, mask <<= 1) { - if (priv->pins & mask) { - uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc); - - // --- Init hardware --- - LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_INPUT); - - uint32_t pull = 0; - - #if PLAT_NO_FLOATING_INPUTS - pull = LL_GPIO_PULL_UP; - #else - pull = LL_GPIO_PULL_NO; - #endif - - if (priv->pulldown & mask) pull = LL_GPIO_PULL_DOWN; - if (priv->pullup & mask) pull = LL_GPIO_PULL_UP; - LL_GPIO_SetPinPull(priv->port, ll_pin, pull); - - if ((priv->trig_rise|priv->trig_fall) & mask) { - LL_EXTI_EnableIT_0_31(LL_EXTI_LINES[i]); - - if (priv->trig_rise & mask) { - LL_EXTI_EnableRisingTrig_0_31(LL_EXTI_LINES[i]); - } - if (priv->trig_fall & mask) { - LL_EXTI_EnableFallingTrig_0_31(LL_EXTI_LINES[i]); - } - - LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTS[priv->port_name-'A'], LL_SYSCFG_EXTI_LINES[i]); - - irqd_attach(EXTIS[i], DI_handleExti, unit); - } - } - } - - // request ticks if we have triggers and any hold-offs configured - if ((priv->trig_rise|priv->trig_fall) && priv->trig_holdoff > 0) { - unit->tick_interval = 1; - } - - return E_SUCCESS; -} - - -/** Tear down the unit */ -static void DI_deInit(Unit *unit) -{ - struct priv *priv = unit->data; - - // pins are de-inited during teardown - - // Detach EXTI handlers and disable interrupts - const uint16_t triggs = priv->trig_rise | priv->trig_fall; - if (unit->status == E_SUCCESS && triggs) { - uint16_t mask = 1; - for (int i = 0; i < 16; i++, mask <<= 1) { - if (triggs & mask) { - LL_EXTI_DisableIT_0_31(LL_EXTI_LINES[i]); - irqd_detach(EXTIS[i], DI_handleExti); - } - } - } - - // Release all resources - rsc_teardown(unit); - - // Free memory - free_ck(unit->data); -} - -// ------------------------------------------------------------------------ - -/** Read request */ -error_t UU_DI_Read(Unit *unit, uint16_t *packed) -{ - CHECK_TYPE(unit, &UNIT_DIN); - struct priv *priv = unit->data; - *packed = pinmask_pack((uint16_t) priv->port->IDR, priv->pins); - return E_SUCCESS; -} - -/** Arm pins */ -error_t UU_DI_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_packed) -{ - CHECK_TYPE(unit, &UNIT_DIN); - struct priv *priv = unit->data; - - uint16_t arm_single = pinmask_spread(arm_single_packed, priv->pins); - uint16_t arm_auto = pinmask_spread(arm_auto_packed, priv->pins); - - // abort if user tries to arm pin that doesn't have a trigger configured - if (0 != ((arm_single|arm_auto) & ~(priv->trig_fall|priv->trig_rise))) { - return E_BAD_VALUE; - } - - // arm and reset hold-offs - // we use critical section to avoid irq between the two steps - vPortEnterCritical(); - { - priv->arm_auto |= arm_single; - priv->arm_single |= arm_auto; - const uint16_t combined = arm_single | arm_auto; - for (int i = 0; i < 16; i++) { - if (combined & (1 << i)) { - priv->holdoff_countdowns[i] = 0; - } - } - } - vPortExitCritical(); - - return E_SUCCESS; -} - -/** DisArm pins */ -error_t UU_DI_DisArm(Unit *unit, uint16_t disarm_packed) -{ - CHECK_TYPE(unit, &UNIT_DIN); - struct priv *priv = unit->data; - - uint16_t disarm = pinmask_spread(disarm_packed, priv->pins); - - // abort if user tries to disarm pin that doesn't have a trigger configured - if (0 != ((disarm) & ~(priv->trig_fall|priv->trig_rise))) { - return E_BAD_VALUE; - } - - priv->arm_auto &= ~disarm; - priv->arm_single &= ~disarm; - - return E_SUCCESS; -} - enum PinCmd_ { CMD_READ = 0, CMD_ARM_SINGLE = 1, diff --git a/units/usart/_usart_internal.h b/units/usart/_usart_internal.h index d5e8eff..8db5983 100644 --- a/units/usart/_usart_internal.h +++ b/units/usart/_usart_internal.h @@ -61,7 +61,6 @@ struct priv { volatile uint16_t tx_buf_nw; //!< Next Write index volatile uint16_t tx_buf_chunk; //!< Size of the currently being transmitted chunk (for advancing the pointers) volatile bool tx_dma_busy; //!< Flag that the Tx DMA request is ongoing - }; /** Allocate data structure and set defaults */