GEX core repository.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gex-core/units/digital_in/unit_din.c

233 lines
5.8 KiB

7 years ago
//
// Created by MightyPork on 2017/11/25.
//
#include <stm32f072xb.h>
#include "comm/messages.h"
#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
GPIO_TypeDef *port;
};
// ------------------------------------------------------------------------
/** 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);
dbg("load bin, pulldown = %X", priv->pulldown);
}
/** 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, 0); // version
pb_char(pb, priv->port_name);
pb_u16(pb, priv->pins);
pb_u16(pb, priv->pulldown);
pb_u16(pb, priv->pullup);
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
static error_t DI_loadIni(Unit *unit, const char *key, const char *value)
7 years ago
{
bool suc = true;
struct priv *priv = unit->data;
if (streq(key, "port")) {
suc = parse_port(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 {
return E_BAD_KEY;
7 years ago
}
if (!suc) return E_BAD_VALUE;
return E_SUCCESS;
7 years ago
}
/** 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)");
7 years ago
iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp512));
7 years ago
iw_comment(iw, "Pins with pull-up");
7 years ago
iw_entry(iw, "pull-up", "%s", str_pinmask(priv->pullup, unit_tmp512));
7 years ago
iw_comment(iw, "Pins with pull-down");
7 years ago
iw_entry(iw, "pull-down", "%s", str_pinmask(priv->pulldown, unit_tmp512));
7 years ago
#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)
7 years ago
{
bool suc = true;
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv), &suc);
if (!suc) return E_OUT_OF_MEM;
7 years ago
// some defaults
priv->port_name = 'A';
priv->pins = 0x0001;
priv->pulldown = 0x0000;
priv->pullup = 0x0000;
return E_SUCCESS;
7 years ago
}
/** Finalize unit set-up */
static error_t DI_init(Unit *unit)
7 years ago
{
bool suc = true;
struct priv *priv = unit->data;
priv->pulldown &= priv->pins;
priv->pullup &= priv->pins;
// --- Parse config ---
priv->port = port2periph(priv->port_name, &suc);
if (!suc) return E_BAD_CONFIG;
7 years ago
// Claim all needed pins
TRY(rsc_claim_gpios(unit, priv->port_name, priv->pins));
7 years ago
uint16_t mask = 1;
for (int i = 0; i < 16; i++, mask <<= 1) {
if (priv->pins & mask) {
uint32_t ll_pin = 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);
}
}
return E_SUCCESS;
7 years ago
}
/** Tear down the unit */
static void DI_deInit(Unit *unit)
{
struct priv *priv = unit->data;
if (unit->status == E_SUCCESS) {
bool suc = true;
uint16_t mask = 1;
for (int i = 0; i < 16; i++, mask <<= 1) {
if (priv->pins & mask) {
uint32_t ll_pin = pin2ll((uint8_t) i, &suc);
assert_param(suc); // this should never fail if we got this far
// configure the pin as analog
LL_GPIO_SetPinMode(priv->port, ll_pin, LL_GPIO_MODE_ANALOG);
}
7 years ago
}
}
// Release all resources
rsc_teardown(unit);
// Free memory
free(unit->data);
unit->data = NULL;
}
// ------------------------------------------------------------------------
enum PinCmd_ {
CMD_READ = 0,
};
/** Handle a request message */
static error_t DI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
7 years ago
{
(void)pp;
struct priv *priv = unit->data;
uint16_t packed = port_pack((uint16_t) priv->port->IDR, priv->pins);
switch (command) {
case CMD_READ:;
7 years ago
PayloadBuilder pb = pb_start((uint8_t*)unit_tmp512, 64, NULL);
7 years ago
pb_u16(&pb, packed);
7 years ago
com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, pb_length(&pb));
return E_SUCCESS;
7 years ago
default:
return E_UNKNOWN_COMMAND;
7 years ago
}
}
// ------------------------------------------------------------------------
/** Unit template */
const UnitDriver UNIT_DIN = {
.name = "DI",
.description = "Digital input",
// Settings
.preInit = DI_preInit,
.cfgLoadBinary = DI_loadBinary,
.cfgWriteBinary = DI_writeBinary,
.cfgLoadIni = DI_loadIni,
.cfgWriteIni = DI_writeIni,
// Init
.init = DI_init,
.deInit = DI_deInit,
// Function
.handleRequest = DI_handleRequest,
};