din renames, dout split

sipo
Ondřej Hruška 7 years ago
parent 41364db93a
commit 5a853244d5
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 6
      units/digital_in/_din_api.c
  2. 6
      units/digital_in/_din_exti.c
  3. 2
      units/digital_in/_din_exti.h
  4. 10
      units/digital_in/_din_init.c
  5. 4
      units/digital_in/_din_init.h
  6. 8
      units/digital_in/_din_settings.c
  7. 10
      units/digital_in/_din_settings.h
  8. 30
      units/digital_in/unit_din.c
  9. 6
      units/digital_in/unit_din.h
  10. 73
      units/digital_out/_dout_api.c
  11. 72
      units/digital_out/_dout_init.c
  12. 20
      units/digital_out/_dout_init.h
  13. 24
      units/digital_out/_dout_internal.h
  14. 83
      units/digital_out/_dout_settings.c
  15. 31
      units/digital_out/_dout_settings.h
  16. 244
      units/digital_out/unit_dout.c
  17. 10
      units/digital_out/unit_dout.h
  18. 10
      units/neopixel/_npx_api.c
  19. 12
      units/neopixel/unit_neopixel.c
  20. 10
      units/neopixel/unit_neopixel.h

@ -11,7 +11,7 @@
#include "_din_internal.h" #include "_din_internal.h"
/** Read request */ /** Read request */
error_t UU_DI_Read(Unit *unit, uint16_t *packed) error_t UU_DIn_Read(Unit *unit, uint16_t *packed)
{ {
CHECK_TYPE(unit, &UNIT_DIN); CHECK_TYPE(unit, &UNIT_DIN);
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -20,7 +20,7 @@ error_t UU_DI_Read(Unit *unit, uint16_t *packed)
} }
/** Arm pins */ /** Arm pins */
error_t UU_DI_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_packed) error_t UU_DIn_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_packed)
{ {
CHECK_TYPE(unit, &UNIT_DIN); CHECK_TYPE(unit, &UNIT_DIN);
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -52,7 +52,7 @@ error_t UU_DI_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_pack
} }
/** DisArm pins */ /** DisArm pins */
error_t UU_DI_DisArm(Unit *unit, uint16_t disarm_packed) error_t UU_DIn_DisArm(Unit *unit, uint16_t disarm_packed)
{ {
CHECK_TYPE(unit, &UNIT_DIN); CHECK_TYPE(unit, &UNIT_DIN);
struct priv *priv = unit->data; struct priv *priv = unit->data;

@ -18,7 +18,7 @@
* data1 - packed, triggering pin * data1 - packed, triggering pin
* data2 - snapshot * data2 - snapshot
*/ */
static void DI_SendTriggerReportToMaster(Job *job) static void DIn_SendTriggerReportToMaster(Job *job)
{ {
PayloadBuilder pb = pb_start(unit_tmp512, UNIT_TMP_LEN, NULL); 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->data1); // packed, 1 on the triggering pin
@ -40,7 +40,7 @@ static void DI_SendTriggerReportToMaster(Job *job)
* *
* @param arg - the unit is passed here * @param arg - the unit is passed here
*/ */
void DI_handleExti(void *arg) void DIn_handleExti(void *arg)
{ {
const uint64_t ts = PTIM_GetMicrotime(); const uint64_t ts = PTIM_GetMicrotime();
@ -74,7 +74,7 @@ void DI_handleExti(void *arg)
.timestamp = ts, .timestamp = ts,
.data1 = pinmask_pack(trigger_map, priv->pins), .data1 = pinmask_pack(trigger_map, priv->pins),
.data2 = pinmask_pack(snapshot, priv->pins), .data2 = pinmask_pack(snapshot, priv->pins),
.cb = DI_SendTriggerReportToMaster .cb = DIn_SendTriggerReportToMaster
}; };
scheduleJob(&j); scheduleJob(&j);
} }

@ -16,6 +16,6 @@
* *
* @param arg - the unit is passed here * @param arg - the unit is passed here
*/ */
void DI_handleExti(void *arg); void DIn_handleExti(void *arg);
#endif //GEX_F072_DIN_EXTI_H #endif //GEX_F072_DIN_EXTI_H

@ -11,7 +11,7 @@
#include "_din_exti.h" #include "_din_exti.h"
/** Allocate data structure and set defaults */ /** Allocate data structure and set defaults */
error_t DI_preInit(Unit *unit) error_t DIn_preInit(Unit *unit)
{ {
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv)); struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv));
if (priv == NULL) return E_OUT_OF_MEM; if (priv == NULL) return E_OUT_OF_MEM;
@ -31,7 +31,7 @@ error_t DI_preInit(Unit *unit)
} }
/** Finalize unit set-up */ /** Finalize unit set-up */
error_t DI_init(Unit *unit) error_t DIn_init(Unit *unit)
{ {
bool suc = true; bool suc = true;
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -88,7 +88,7 @@ error_t DI_init(Unit *unit)
LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTS[priv->port_name-'A'], LL_SYSCFG_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); irqd_attach(EXTIS[i], DIn_handleExti, unit);
} }
} }
} }
@ -103,7 +103,7 @@ error_t DI_init(Unit *unit)
/** Tear down the unit */ /** Tear down the unit */
void DI_deInit(Unit *unit) void DIn_deInit(Unit *unit)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -116,7 +116,7 @@ void DI_deInit(Unit *unit)
for (int i = 0; i < 16; i++, mask <<= 1) { for (int i = 0; i < 16; i++, mask <<= 1) {
if (triggs & mask) { if (triggs & mask) {
LL_EXTI_DisableIT_0_31(LL_EXTI_LINES[i]); LL_EXTI_DisableIT_0_31(LL_EXTI_LINES[i]);
irqd_detach(EXTIS[i], DI_handleExti); irqd_detach(EXTIS[i], DIn_handleExti);
} }
} }
} }

@ -12,9 +12,9 @@
#include "unit_base.h" #include "unit_base.h"
/** Finalize unit set-up */ /** Finalize unit set-up */
error_t DI_init(Unit *unit); error_t DIn_init(Unit *unit);
/** Tear down the unit */ /** Tear down the unit */
void DI_deInit(Unit *unit); void DIn_deInit(Unit *unit);
#endif //GEX_F072_DIN_INIT_H #endif //GEX_F072_DIN_INIT_H

@ -10,7 +10,7 @@
#include "_din_settings.h" #include "_din_settings.h"
/** Load from a binary buffer stored in Flash */ /** Load from a binary buffer stored in Flash */
void DI_loadBinary(Unit *unit, PayloadParser *pp) void DIn_loadBinary(Unit *unit, PayloadParser *pp)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -33,7 +33,7 @@ void DI_loadBinary(Unit *unit, PayloadParser *pp)
} }
/** Write to a binary buffer for storing in Flash */ /** Write to a binary buffer for storing in Flash */
void DI_writeBinary(Unit *unit, PayloadBuilder *pb) void DIn_writeBinary(Unit *unit, PayloadBuilder *pb)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -52,7 +52,7 @@ void DI_writeBinary(Unit *unit, PayloadBuilder *pb)
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */ /** Parse a key-value pair from the INI file */
error_t DI_loadIni(Unit *unit, const char *key, const char *value) error_t DIn_loadIni(Unit *unit, const char *key, const char *value)
{ {
bool suc = true; bool suc = true;
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -90,7 +90,7 @@ error_t DI_loadIni(Unit *unit, const char *key, const char *value)
} }
/** Generate INI file section for the unit */ /** Generate INI file section for the unit */
void DI_writeIni(Unit *unit, IniWriter *iw) void DIn_writeIni(Unit *unit, IniWriter *iw)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;

@ -12,20 +12,20 @@
#include "unit_base.h" #include "unit_base.h"
/** Allocate data structure and set defaults */ /** Allocate data structure and set defaults */
error_t DI_preInit(Unit *unit); error_t DIn_preInit(Unit *unit);
/** Load from a binary buffer stored in Flash */ /** Load from a binary buffer stored in Flash */
void DI_loadBinary(Unit *unit, PayloadParser *pp); void DIn_loadBinary(Unit *unit, PayloadParser *pp);
/** Write to a binary buffer for storing in Flash */ /** Write to a binary buffer for storing in Flash */
void DI_writeBinary(Unit *unit, PayloadBuilder *pb); void DIn_writeBinary(Unit *unit, PayloadBuilder *pb);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */ /** Parse a key-value pair from the INI file */
error_t DI_loadIni(Unit *unit, const char *key, const char *value); error_t DIn_loadIni(Unit *unit, const char *key, const char *value);
/** Generate INI file section for the unit */ /** Generate INI file section for the unit */
void DI_writeIni(Unit *unit, IniWriter *iw); void DIn_writeIni(Unit *unit, IniWriter *iw);
#endif //GEX_F072_DIN_SETTINGS_H #endif //GEX_F072_DIN_SETTINGS_H

@ -20,13 +20,13 @@ enum PinCmd_ {
}; };
/** Handle a request message */ /** Handle a request message */
static error_t DI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) static error_t DIn_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{ {
uint16_t pins = 0; uint16_t pins = 0;
switch (command) { switch (command) {
case CMD_READ:; case CMD_READ:;
TRY(UU_DI_Read(unit, &pins)); TRY(UU_DIn_Read(unit, &pins));
PayloadBuilder pb = pb_start((uint8_t*)unit_tmp512, UNIT_TMP_LEN, NULL); PayloadBuilder pb = pb_start((uint8_t*)unit_tmp512, UNIT_TMP_LEN, NULL);
pb_u16(&pb, pins); // packed input pins pb_u16(&pb, pins); // packed input pins
@ -37,21 +37,21 @@ static error_t DI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Pay
pins = pp_u16(pp); pins = pp_u16(pp);
if (!pp->ok) return E_MALFORMED_COMMAND; if (!pp->ok) return E_MALFORMED_COMMAND;
TRY(UU_DI_Arm(unit, pins, 0)); TRY(UU_DIn_Arm(unit, pins, 0));
return E_SUCCESS; return E_SUCCESS;
case CMD_ARM_AUTO:; case CMD_ARM_AUTO:;
pins = pp_u16(pp); pins = pp_u16(pp);
if (!pp->ok) return E_MALFORMED_COMMAND; if (!pp->ok) return E_MALFORMED_COMMAND;
TRY(UU_DI_Arm(unit, 0, pins)); TRY(UU_DIn_Arm(unit, 0, pins));
return E_SUCCESS; return E_SUCCESS;
case CMD_DISARM:; case CMD_DISARM:;
pins = pp_u16(pp); pins = pp_u16(pp);
if (!pp->ok) return E_MALFORMED_COMMAND; if (!pp->ok) return E_MALFORMED_COMMAND;
TRY(UU_DI_DisArm(unit, pins)); TRY(UU_DIn_DisArm(unit, pins));
return E_SUCCESS; return E_SUCCESS;
default: default:
@ -64,7 +64,7 @@ static error_t DI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Pay
* *
* @param unit * @param unit
*/ */
static void DI_updateTick(Unit *unit) static void DIn_updateTick(Unit *unit)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -82,15 +82,15 @@ const UnitDriver UNIT_DIN = {
.name = "DI", .name = "DI",
.description = "Digital input with triggers", .description = "Digital input with triggers",
// Settings // Settings
.preInit = DI_preInit, .preInit = DIn_preInit,
.cfgLoadBinary = DI_loadBinary, .cfgLoadBinary = DIn_loadBinary,
.cfgWriteBinary = DI_writeBinary, .cfgWriteBinary = DIn_writeBinary,
.cfgLoadIni = DI_loadIni, .cfgLoadIni = DIn_loadIni,
.cfgWriteIni = DI_writeIni, .cfgWriteIni = DIn_writeIni,
// Init // Init
.init = DI_init, .init = DIn_init,
.deInit = DI_deInit, .deInit = DIn_deInit,
// Function // Function
.handleRequest = DI_handleRequest, .handleRequest = DIn_handleRequest,
.updateTick = DI_updateTick, .updateTick = DIn_updateTick,
}; };

@ -18,7 +18,7 @@ extern const UnitDriver UNIT_DIN;
* @param packed - output; the packed (right aligned) bits representing the pins, highest to lowest, are written here. * @param packed - output; the packed (right aligned) bits representing the pins, highest to lowest, are written here.
* @return success * @return success
*/ */
error_t UU_DI_Read(Unit *unit, uint16_t *packed); error_t UU_DIn_Read(Unit *unit, uint16_t *packed);
/** /**
* Arm pins for trigger generation * Arm pins for trigger generation
@ -28,7 +28,7 @@ error_t UU_DI_Read(Unit *unit, uint16_t *packed);
* @param arm_auto_packed - packed bit map of pins to arm for auto trigger (repeated) * @param arm_auto_packed - packed bit map of pins to arm for auto trigger (repeated)
* @return success * @return success
*/ */
error_t UU_DI_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_packed); error_t UU_DIn_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_packed);
/** /**
* Dis-arm pins to not generate events * Dis-arm pins to not generate events
@ -37,6 +37,6 @@ error_t UU_DI_Arm(Unit *unit, uint16_t arm_single_packed, uint16_t arm_auto_pack
* @param disarm_packed - packed bit map of pins to dis-arm * @param disarm_packed - packed bit map of pins to dis-arm
* @return success * @return success
*/ */
error_t UU_DI_DisArm(Unit *unit, uint16_t disarm_packed); error_t UU_DIn_DisArm(Unit *unit, uint16_t disarm_packed);
#endif //U_DIN_H #endif //U_DIN_H

@ -0,0 +1,73 @@
//
// Created by MightyPork on 2018/02/03.
//
#include "platform.h"
#include "unit_base.h"
#include "unit_dout.h"
#define DOUT_INTERNAL
#include "_dout_internal.h"
error_t UU_DOut_Write(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
uint16_t set = spread;
uint16_t reset = ((~spread) & mask);
priv->port->BSRR = set | (reset << 16);
return E_SUCCESS;
}
error_t UU_DOut_Set(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = spread;
return E_SUCCESS;
}
error_t UU_DOut_Clear(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = (spread<<16);
return E_SUCCESS;
}
error_t UU_DOut_Toggle(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
uint16_t flipped = (uint16_t) (~priv->port->ODR) & mask;
uint16_t set = flipped & spread;
uint16_t reset = ((~flipped) & mask) & spread;
priv->port->BSRR = set | (reset<<16);
return E_SUCCESS;
}
error_t UU_DOut_GetPinCount(Unit *unit, uint8_t *count)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint32_t packed = pinmask_pack(0xFFFF, priv->pins);
*count = (uint8_t)(32 - __CLZ(packed));
return E_SUCCESS;
}

@ -0,0 +1,72 @@
//
// Created by MightyPork on 2018/02/03.
//
#include "platform.h"
#include "unit_base.h"
#define DOUT_INTERNAL
#include "_dout_internal.h"
#include "_dout_init.h"
/** Allocate data structure and set defaults */
error_t DOut_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->open_drain = 0x0000;
priv->initial = 0x0000;
return E_SUCCESS;
}
/** Finalize unit set-up */
error_t DOut_init(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data;
priv->initial &= priv->pins;
priv->open_drain &= priv->pins;
// --- 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));
for (int i = 0; i < 16; i++) {
if (priv->pins & (1 << i)) {
uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc);
// --- Init hardware ---
LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(priv->port, ll_pin,
(priv->open_drain & (1 << i)) ? LL_GPIO_OUTPUT_OPENDRAIN : LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(priv->port, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
}
}
// Set the initial state
priv->port->ODR &= ~priv->pins;
priv->port->ODR |= priv->initial;
return E_SUCCESS;
}
/** Tear down the unit */
void DOut_deInit(Unit *unit)
{
// pins are de-inited during teardown
// Release all resources
rsc_teardown(unit);
// Free memory
free_ck(unit->data);
}

@ -0,0 +1,20 @@
//
// Created by MightyPork on 2018/02/03.
//
#ifndef GEX_F072_DOUT_INIT_H
#define GEX_F072_DOUT_INIT_H
#ifndef DOUT_INTERNAL
#error bad include!
#endif
#include "unit_base.h"
/** Finalize unit set-up */
error_t DOut_init(Unit *unit);
/** Tear down the unit */
void DOut_deInit(Unit *unit);
#endif //GEX_F072_DOUT_INIT_H

@ -0,0 +1,24 @@
//
// Created by MightyPork on 2018/02/03.
//
#ifndef GEX_F072_DOUT_INTERNAL_H
#define GEX_F072_DOUT_INTERNAL_H
#ifndef DOUT_INTERNAL
#error bad include!
#endif
#include "unit_base.h"
/** Private data structure */
struct priv {
char port_name;
uint16_t pins; // pin mask
uint16_t initial; // initial pin states
uint16_t open_drain; // open drain pins
GPIO_TypeDef *port;
};
#endif //GEX_F072_DOUT_INTERNAL_H

@ -0,0 +1,83 @@
//
// Created by MightyPork on 2018/02/03.
//
#include "platform.h"
#include "unit_base.h"
#define DOUT_INTERNAL
#include "_dout_internal.h"
#include "_dout_settings.h"
/** Load from a binary buffer stored in Flash */
void DOut_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->initial = pp_u16(pp);
priv->open_drain = pp_u16(pp);
}
/** Write to a binary buffer for storing in Flash */
void DOut_writeBinary(Unit *unit, PayloadBuilder *pb)
{
struct priv *priv = unit->data;
pb_u8(pb, 0); // version
pb_char(pb, priv->port_name);
pb_u16(pb, priv->pins);
pb_u16(pb, priv->initial);
pb_u16(pb, priv->open_drain);
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
error_t DOut_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, "initial")) {
priv->initial = parse_pinmask(value, &suc);
}
else if (streq(key, "open-drain")) {
priv->open_drain = 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 DOut_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, "Initially high pins");
iw_entry(iw, "initial", "%s", pinmask2str(priv->initial, unit_tmp512));
iw_comment(iw, "Open-drain pins");
iw_entry(iw, "open-drain", "%s", pinmask2str(priv->open_drain, unit_tmp512));
}

@ -0,0 +1,31 @@
//
// Created by MightyPork on 2018/02/03.
//
#ifndef GEX_F072_DOUT_SETTINGS_H
#define GEX_F072_DOUT_SETTINGS_H
#ifndef DOUT_INTERNAL
#error bad include!
#endif
#include "unit_base.h"
/** Allocate data structure and set defaults */
error_t DOut_preInit(Unit *unit);
/** Load from a binary buffer stored in Flash */
void DOut_loadBinary(Unit *unit, PayloadParser *pp);
/** Write to a binary buffer for storing in Flash */
void DOut_writeBinary(Unit *unit, PayloadBuilder *pb);
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
error_t DOut_loadIni(Unit *unit, const char *key, const char *value);
/** Generate INI file section for the unit */
void DOut_writeIni(Unit *unit, IniWriter *iw);
#endif //GEX_F072_DOUT_SETTINGS_H

@ -2,223 +2,12 @@
// Created by MightyPork on 2017/11/25. // Created by MightyPork on 2017/11/25.
// //
#include "comm/messages.h"
#include "unit_base.h" #include "unit_base.h"
#include "unit_dout.h" #include "unit_dout.h"
/** Private data structure */ #define DOUT_INTERNAL
struct priv { #include "_dout_settings.h"
char port_name; #include "_dout_init.h"
uint16_t pins; // pin mask
uint16_t initial; // initial pin states
uint16_t open_drain; // open drain pins
GPIO_TypeDef *port;
};
// ------------------------------------------------------------------------
/** Load from a binary buffer stored in Flash */
static void DO_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->initial = pp_u16(pp);
priv->open_drain = pp_u16(pp);
}
/** Write to a binary buffer for storing in Flash */
static void DO_writeBinary(Unit *unit, PayloadBuilder *pb)
{
struct priv *priv = unit->data;
pb_u8(pb, 0); // version
pb_char(pb, priv->port_name);
pb_u16(pb, priv->pins);
pb_u16(pb, priv->initial);
pb_u16(pb, priv->open_drain);
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
static error_t DO_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, "initial")) {
priv->initial = parse_pinmask(value, &suc);
}
else if (streq(key, "open-drain")) {
priv->open_drain = 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 */
static void DO_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, "Initially high pins");
iw_entry(iw, "initial", "%s", pinmask2str(priv->initial, unit_tmp512));
iw_comment(iw, "Open-drain pins");
iw_entry(iw, "open-drain", "%s", pinmask2str(priv->open_drain, unit_tmp512));
}
// ------------------------------------------------------------------------
/** Allocate data structure and set defaults */
static error_t DO_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->open_drain = 0x0000;
priv->initial = 0x0000;
return E_SUCCESS;
}
/** Finalize unit set-up */
static error_t DO_init(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data;
priv->initial &= priv->pins;
priv->open_drain &= priv->pins;
// --- 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));
for (int i = 0; i < 16; i++) {
if (priv->pins & (1 << i)) {
uint32_t ll_pin = hw_pin2ll((uint8_t) i, &suc);
// --- Init hardware ---
LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(priv->port, ll_pin,
(priv->open_drain & (1 << i)) ? LL_GPIO_OUTPUT_OPENDRAIN : LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(priv->port, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
}
}
// Set the initial state
priv->port->ODR &= ~priv->pins;
priv->port->ODR |= priv->initial;
return E_SUCCESS;
}
/** Tear down the unit */
static void DO_deInit(Unit *unit)
{
// pins are de-inited during teardown
// Release all resources
rsc_teardown(unit);
// Free memory
free_ck(unit->data);
}
// ------------------------------------------------------------------------
error_t UU_DO_Write(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
uint16_t set = spread;
uint16_t reset = ((~spread) & mask);
priv->port->BSRR = set | (reset << 16);
return E_SUCCESS;
}
error_t UU_DO_Set(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = spread;
return E_SUCCESS;
}
error_t UU_DO_Clear(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
priv->port->BSRR = (spread<<16);
return E_SUCCESS;
}
error_t UU_DO_Toggle(Unit *unit, uint16_t packed)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint16_t mask = priv->pins;
uint16_t spread = pinmask_spread(packed, mask);
uint16_t flipped = (uint16_t) (~priv->port->ODR) & mask;
uint16_t set = flipped & spread;
uint16_t reset = ((~flipped) & mask) & spread;
priv->port->BSRR = set | (reset<<16);
return E_SUCCESS;
}
error_t UU_DO_GetPinCount(Unit *unit, uint8_t *count)
{
CHECK_TYPE(unit, &UNIT_DOUT);
struct priv *priv = unit->data;
uint32_t packed = pinmask_pack(0xFFFF, priv->pins);
*count = (uint8_t)(32 - __CLZ(packed));
return E_SUCCESS;
}
enum PinCmd_ { enum PinCmd_ {
CMD_TEST = 0, CMD_TEST = 0,
@ -228,22 +17,23 @@ enum PinCmd_ {
}; };
/** Handle a request message */ /** Handle a request message */
static error_t DO_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) static error_t DOut_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command,
PayloadParser *pp)
{ {
uint16_t packed = pp_u16(pp); uint16_t packed = pp_u16(pp);
switch (command) { switch (command) {
case CMD_TEST: case CMD_TEST:
return UU_DO_Write(unit, packed); return UU_DOut_Write(unit, packed);
case CMD_SET: case CMD_SET:
return UU_DO_Set(unit, packed); return UU_DOut_Set(unit, packed);
case CMD_CLEAR: case CMD_CLEAR:
return UU_DO_Clear(unit, packed); return UU_DOut_Clear(unit, packed);
case CMD_TOGGLE: case CMD_TOGGLE:
return UU_DO_Toggle(unit, packed); return UU_DOut_Toggle(unit, packed);
default: default:
return E_UNKNOWN_COMMAND; return E_UNKNOWN_COMMAND;
@ -257,14 +47,14 @@ const UnitDriver UNIT_DOUT = {
.name = "DO", .name = "DO",
.description = "Digital output", .description = "Digital output",
// Settings // Settings
.preInit = DO_preInit, .preInit = DOut_preInit,
.cfgLoadBinary = DO_loadBinary, .cfgLoadBinary = DOut_loadBinary,
.cfgWriteBinary = DO_writeBinary, .cfgWriteBinary = DOut_writeBinary,
.cfgLoadIni = DO_loadIni, .cfgLoadIni = DOut_loadIni,
.cfgWriteIni = DO_writeIni, .cfgWriteIni = DOut_writeIni,
// Init // Init
.init = DO_init, .init = DOut_init,
.deInit = DO_deInit, .deInit = DOut_deInit,
// Function // Function
.handleRequest = DO_handleRequest, .handleRequest = DOut_handleRequest,
}; };

@ -18,7 +18,7 @@ extern const UnitDriver UNIT_DOUT;
* @param packed - packed pin states (aligned to right) * @param packed - packed pin states (aligned to right)
* @return success * @return success
*/ */
error_t UU_DO_Write(Unit *unit, uint16_t packed); error_t UU_DOut_Write(Unit *unit, uint16_t packed);
/** /**
* Set pins (clear none) * Set pins (clear none)
@ -27,7 +27,7 @@ error_t UU_DO_Write(Unit *unit, uint16_t packed);
* @param packed - packed pins, 1 if pin should be set * @param packed - packed pins, 1 if pin should be set
* @return success * @return success
*/ */
error_t UU_DO_Set(Unit *unit, uint16_t packed); error_t UU_DOut_Set(Unit *unit, uint16_t packed);
/** /**
* Clear multiple pins * Clear multiple pins
@ -36,7 +36,7 @@ error_t UU_DO_Set(Unit *unit, uint16_t packed);
* @param packed - packed pins, 1 if pin should be cleared * @param packed - packed pins, 1 if pin should be cleared
* @return * @return
*/ */
error_t UU_DO_Clear(Unit *unit, uint16_t packed); error_t UU_DOut_Clear(Unit *unit, uint16_t packed);
/** /**
* Toggle pins * Toggle pins
@ -45,7 +45,7 @@ error_t UU_DO_Clear(Unit *unit, uint16_t packed);
* @param packed - packed pins, 1 if pin should be toggled * @param packed - packed pins, 1 if pin should be toggled
* @return * @return
*/ */
error_t UU_DO_Toggle(Unit *unit, uint16_t packed); error_t UU_DOut_Toggle(Unit *unit, uint16_t packed);
/** /**
* Get number of configured pins * Get number of configured pins
@ -54,6 +54,6 @@ error_t UU_DO_Toggle(Unit *unit, uint16_t packed);
* @param count output, written with 0-16 * @param count output, written with 0-16
* @return success * @return success
*/ */
error_t UU_DO_GetPinCount(Unit *unit, uint8_t *count); error_t UU_DOut_GetPinCount(Unit *unit, uint8_t *count);
#endif //U_DOUT_H #endif //U_DOUT_H

@ -11,7 +11,7 @@
#include "ws2812.h" #include "ws2812.h"
/* Clear the strip */ /* Clear the strip */
error_t UU_NEOPIXEL_Clear(Unit *unit) error_t UU_Npx_Clear(Unit *unit)
{ {
CHECK_TYPE(unit, &UNIT_NEOPIXEL); CHECK_TYPE(unit, &UNIT_NEOPIXEL);
@ -21,7 +21,7 @@ error_t UU_NEOPIXEL_Clear(Unit *unit)
} }
/* Load packed */ /* Load packed */
error_t UU_NEOPIXEL_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes) error_t UU_Npx_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes)
{ {
CHECK_TYPE(unit, &UNIT_NEOPIXEL); CHECK_TYPE(unit, &UNIT_NEOPIXEL);
@ -43,19 +43,19 @@ static error_t load_u32(Unit *unit, const uint8_t *bytes, uint32_t nbytes, bool
} }
/* Load U32, LE */ /* Load U32, LE */
inline error_t UU_NEOPIXEL_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) inline error_t UU_Npx_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes)
{ {
return load_u32(unit, bytes, nbytes, false); return load_u32(unit, bytes, nbytes, false);
} }
/* Load U32, BE */ /* Load U32, BE */
inline error_t UU_NEOPIXEL_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes) inline error_t UU_Npx_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes)
{ {
return load_u32(unit, bytes, nbytes, true); return load_u32(unit, bytes, nbytes, true);
} }
/* Get the pixel count */ /* Get the pixel count */
error_t UU_NEOPIXEL_GetCount(Unit *unit, uint16_t *count) error_t UU_Npx_GetCount(Unit *unit, uint16_t *count)
{ {
CHECK_TYPE(unit, &UNIT_NEOPIXEL); CHECK_TYPE(unit, &UNIT_NEOPIXEL);

@ -26,27 +26,27 @@ static error_t Npx_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Pa
switch (command) { switch (command) {
/** Clear the entire strip */ /** Clear the entire strip */
case CMD_CLEAR: case CMD_CLEAR:
return UU_NEOPIXEL_Clear(unit); return UU_Npx_Clear(unit);
/** Load packed RGB colors (length must match the strip size) */ /** Load packed RGB colors (length must match the strip size) */
case CMD_LOAD:; case CMD_LOAD:;
bytes = pp_tail(pp, &len); bytes = pp_tail(pp, &len);
return UU_NEOPIXEL_Load(unit, bytes, len); return UU_Npx_Load(unit, bytes, len);
/** Load sparse (uint32_t) colors, 0x00RRGGBB, little endian. */ /** Load sparse (uint32_t) colors, 0x00RRGGBB, little endian. */
case CMD_LOAD_U32_LE: case CMD_LOAD_U32_LE:
bytes = pp_tail(pp, &len); bytes = pp_tail(pp, &len);
return UU_NEOPIXEL_LoadU32LE(unit, bytes, len); return UU_Npx_LoadU32LE(unit, bytes, len);
/** Load sparse (uint32_t) colors, 0x00RRGGBB, big endian. */ /** Load sparse (uint32_t) colors, 0x00RRGGBB, big endian. */
case CMD_LOAD_U32_BE: case CMD_LOAD_U32_BE:
bytes = pp_tail(pp, &len); bytes = pp_tail(pp, &len);
return UU_NEOPIXEL_LoadU32BE(unit, bytes, len); return UU_Npx_LoadU32BE(unit, bytes, len);
/** Get the Neopixel strip length */ /** Get the Neopixel strip length */
case CMD_GET_LEN:; case CMD_GET_LEN:;
uint16_t count; uint16_t count;
TRY(UU_NEOPIXEL_GetCount(unit, &count)); TRY(UU_Npx_GetCount(unit, &count));
com_respond_u16(frame_id, count); com_respond_u16(frame_id, count);
return E_SUCCESS; return E_SUCCESS;
@ -59,7 +59,7 @@ static error_t Npx_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Pa
/** Unit template */ /** Unit template */
const UnitDriver UNIT_NEOPIXEL = { const UnitDriver UNIT_NEOPIXEL = {
.name = "NEOPIXEL", .name = "NPX",
.description = "Neopixel RGB LED strip", .description = "Neopixel RGB LED strip",
// Settings // Settings
.preInit = Npx_preInit, .preInit = Npx_preInit,

@ -18,7 +18,7 @@ extern const UnitDriver UNIT_NEOPIXEL;
* @param unit * @param unit
* @return success * @return success
*/ */
error_t UU_NEOPIXEL_Clear(Unit *unit); error_t UU_Npx_Clear(Unit *unit);
/** /**
* Load the strip with packed bytes R,G,B. * Load the strip with packed bytes R,G,B.
@ -28,7 +28,7 @@ error_t UU_NEOPIXEL_Clear(Unit *unit);
* @param nbytes - number of bytes, must be count*3 * @param nbytes - number of bytes, must be count*3
* @return success * @return success
*/ */
error_t UU_NEOPIXEL_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes); error_t UU_Npx_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes);
/** /**
* Load the strip with sparse (uint32_t) colors 0x00RRGGBB as little endian bytes * Load the strip with sparse (uint32_t) colors 0x00RRGGBB as little endian bytes
@ -39,7 +39,7 @@ error_t UU_NEOPIXEL_Load(Unit *unit, const uint8_t *packed_rgb, uint32_t nbytes)
* @param nbytes - number of bytes, must be count*4 * @param nbytes - number of bytes, must be count*4
* @return success * @return success
*/ */
error_t UU_NEOPIXEL_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes); error_t UU_Npx_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes);
/** /**
* Load the strip with sparse (uint32_t) colors 0x00RRGGBB as big endian bytes * Load the strip with sparse (uint32_t) colors 0x00RRGGBB as big endian bytes
@ -50,7 +50,7 @@ error_t UU_NEOPIXEL_LoadU32LE(Unit *unit, const uint8_t *bytes, uint32_t nbytes)
* @param nbytes - number of bytes, must be count*4 * @param nbytes - number of bytes, must be count*4
* @return success * @return success
*/ */
error_t UU_NEOPIXEL_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes); error_t UU_Npx_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes);
/** /**
* Get number of pixels on the strip * Get number of pixels on the strip
@ -59,6 +59,6 @@ error_t UU_NEOPIXEL_LoadU32BE(Unit *unit, const uint8_t *bytes, uint32_t nbytes)
* @param count - destination for the count value * @param count - destination for the count value
* @return success * @return success
*/ */
error_t UU_NEOPIXEL_GetCount(Unit *unit, uint16_t *count); error_t UU_Npx_GetCount(Unit *unit, uint16_t *count);
#endif //U_NEOPIXEL_H #endif //U_NEOPIXEL_H

Loading…
Cancel
Save