added DI unit
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "platform.h"
|
||||
#include "unit.h"
|
||||
#include "resources.h"
|
||||
#include "pin_utils.h"
|
||||
|
||||
static bool rsc_initialized = false;
|
||||
|
||||
@@ -82,6 +83,30 @@ bool rsc_claim_range(Unit *unit, Resource rsc0, Resource rsc1)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rsc_claim_gpios(Unit *unit, char port_name, uint16_t pins)
|
||||
{
|
||||
bool suc = true;
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (pins & (1 << i)) {
|
||||
Resource rsc = pin2resource(port_name, (uint8_t) i, &suc);
|
||||
if (!suc) {
|
||||
unit->status = E_BAD_CONFIG;
|
||||
|
||||
rsc_teardown(unit);
|
||||
return false;
|
||||
}
|
||||
|
||||
suc = rsc_claim(unit, rsc);
|
||||
if (!suc) {
|
||||
rsc_teardown(unit);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free a resource for other use
|
||||
*
|
||||
|
||||
@@ -88,4 +88,15 @@ void rsc_teardown(Unit *unit);
|
||||
void rsc_free(Unit *unit, Resource rsc);
|
||||
void rsc_free_range(Unit *unit, Resource rsc0, Resource rsc1);
|
||||
|
||||
/**
|
||||
* Claim GPIOs by bitmask and port name, atomically.
|
||||
* Tear down the unit on failure.
|
||||
*
|
||||
* @param unit - claiming unit
|
||||
* @param port_name - port (eg. 'A')
|
||||
* @param pins - pins, bitmask
|
||||
* @return success
|
||||
*/
|
||||
bool rsc_claim_gpios(Unit *unit, char port_name, uint16_t pins);
|
||||
|
||||
#endif //GEX_RESOURCES_H
|
||||
|
||||
@@ -16,7 +16,7 @@ static bool savebuf_ovhandler(PayloadBuilder *pb, uint32_t more);
|
||||
|
||||
// This is the first entry in a valid config.
|
||||
// Change with each breaking change to force config reset.
|
||||
#define CONFIG_MARKER 0xA55D
|
||||
#define CONFIG_MARKER 0xA55E
|
||||
|
||||
void settings_load(void)
|
||||
{
|
||||
@@ -270,7 +270,7 @@ void settings_load_ini_begin(void)
|
||||
|
||||
void settings_load_ini_key(const char *restrict section, const char *restrict key, const char *restrict value)
|
||||
{
|
||||
dbg("[%s] %s = %s", section, key, value);
|
||||
// dbg("[%s] %s = %s", section, key, value);
|
||||
static char namebuf[INI_KEY_MAX];
|
||||
|
||||
if (streq(section, "SYSTEM")) {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "unit.h"
|
||||
#include "resources.h"
|
||||
|
||||
char unit_tmp64[64];
|
||||
|
||||
// Abort partly inited unit
|
||||
void clean_failed_unit(Unit *unit)
|
||||
{
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
#include "utils/payload_builder.h"
|
||||
#include "utils/payload_parser.h"
|
||||
|
||||
extern char unit_tmp64[64]; // temporary static buffer
|
||||
|
||||
typedef struct unit Unit;
|
||||
typedef struct unit_driver UnitDriver;
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ GEX_SRC_DIR = \
|
||||
User/units/test \
|
||||
User/units/pin \
|
||||
User/units/digital_out \
|
||||
User/units/digital_in \
|
||||
User/TinyFrame \
|
||||
User/CWPack \
|
||||
User/tasks
|
||||
|
||||
@@ -45,13 +45,20 @@
|
||||
|
||||
// -------- Platform specific includes and defines ---------
|
||||
|
||||
/// Feature flags:
|
||||
// PLAT_FLASHBANKS - has the Banks field on the Flash config struct
|
||||
// PLAT_NO_FLOATING_INPUTS - can't have digital inputs with no pull resistor
|
||||
// PLAT_USB_PHYCLOCK - requires special config of phy clock for USB
|
||||
// PLAT_USB_OTGFS - uses the USB OTG IP, needs different config code
|
||||
|
||||
#if defined(GEX_PLAT_F103_BLUEPILL)
|
||||
|
||||
// platform name for the version string
|
||||
#define GEX_PLATFORM "STM32F103-Bluepill"
|
||||
|
||||
// This platform has the Banks field on the Flash config struct for settings save
|
||||
// feature flags
|
||||
#define PLAT_FLASHBANKS 1
|
||||
#define PLAT_NO_FLOATING_INPUTS 1
|
||||
|
||||
#include <stm32f1xx.h>
|
||||
#include <stm32f1xx_hal.h>
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "units/pin/unit_pin.h"
|
||||
#include "units/digital_out/unit_dout.h"
|
||||
#include "units/digital_in/unit_din.h"
|
||||
#include "units/neopixel/unit_neopixel.h"
|
||||
#include "units/test/unit_test.h"
|
||||
|
||||
@@ -24,6 +25,7 @@ void plat_init_resources(void)
|
||||
// --- Common unit drivers ---
|
||||
ureg_add_type(&UNIT_PIN);
|
||||
ureg_add_type(&UNIT_DOUT);
|
||||
ureg_add_type(&UNIT_DIN);
|
||||
|
||||
#if HAVE_TEST_UNIT
|
||||
ureg_add_type(&UNIT_TEST);
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
//
|
||||
// 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 bool 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(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);
|
||||
dbg("parsinf ini, pulldown = %X", priv->pulldown);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return suc;
|
||||
}
|
||||
|
||||
/** 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", str_pinmask(priv->pins, unit_tmp64));
|
||||
|
||||
iw_comment(iw, "Pins with pull-up");
|
||||
iw_entry(iw, "pull-up", "%s", str_pinmask(priv->pullup, unit_tmp64));
|
||||
|
||||
iw_comment(iw, "Pins with pull-down");
|
||||
iw_entry(iw, "pull-down", "%s", str_pinmask(priv->pulldown, unit_tmp64));
|
||||
|
||||
#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 bool DI_preInit(Unit *unit)
|
||||
{
|
||||
bool suc = true;
|
||||
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv), &suc);
|
||||
CHECK_SUC();
|
||||
|
||||
// some defaults
|
||||
priv->port_name = 'A';
|
||||
priv->pins = 0x0001;
|
||||
priv->pulldown = 0x0000;
|
||||
priv->pullup = 0x0000;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Finalize unit set-up */
|
||||
static bool DI_init(Unit *unit)
|
||||
{
|
||||
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) {
|
||||
unit->status = E_BAD_CONFIG;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Claim all needed pins
|
||||
suc = rsc_claim_gpios(unit, priv->port_name, priv->pins);
|
||||
CHECK_SUC();
|
||||
|
||||
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 true;
|
||||
}
|
||||
|
||||
|
||||
/** Tear down the unit */
|
||||
static void DI_deInit(Unit *unit)
|
||||
{
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Release all resources
|
||||
rsc_teardown(unit);
|
||||
|
||||
// Free memory
|
||||
free(unit->data);
|
||||
unit->data = NULL;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
enum PinCmd_ {
|
||||
CMD_READ = 0,
|
||||
};
|
||||
|
||||
/** Handle a request message */
|
||||
static bool DI_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
|
||||
{
|
||||
(void)pp;
|
||||
|
||||
struct priv *priv = unit->data;
|
||||
|
||||
uint16_t packed = port_pack((uint16_t) priv->port->IDR, priv->pins);
|
||||
|
||||
switch (command) {
|
||||
case CMD_READ:;
|
||||
PayloadBuilder pb = pb_start((uint8_t*)unit_tmp64, 64, NULL);
|
||||
pb_u16(&pb, packed);
|
||||
com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp64, pb_length(&pb));
|
||||
break;
|
||||
|
||||
default:
|
||||
com_respond_bad_cmd(frame_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/** 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,
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/11/25.
|
||||
//
|
||||
|
||||
#ifndef U_DIN_H
|
||||
#define U_DIN_H
|
||||
|
||||
#include "unit.h"
|
||||
|
||||
extern const UnitDriver UNIT_DIN;
|
||||
|
||||
#endif //U_DIN_H
|
||||
@@ -23,6 +23,9 @@ 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);
|
||||
@@ -34,6 +37,8 @@ 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);
|
||||
@@ -71,19 +76,18 @@ static bool DO_loadIni(Unit *unit, const char *key, const char *value)
|
||||
static void DO_writeIni(Unit *unit, IniWriter *iw)
|
||||
{
|
||||
struct priv *priv = unit->data;
|
||||
char buf[64];
|
||||
|
||||
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", str_pinmask(priv->pins, buf));
|
||||
iw_entry(iw, "pins", "%s", str_pinmask(priv->pins, unit_tmp64));
|
||||
|
||||
iw_comment(iw, "Initially high pins");
|
||||
iw_entry(iw, "initial", "%s", str_pinmask(priv->initial, buf));
|
||||
iw_entry(iw, "initial", "%s", str_pinmask(priv->initial, unit_tmp64));
|
||||
|
||||
iw_comment(iw, "Open-drain pins");
|
||||
iw_entry(iw, "opendrain", "%s", str_pinmask(priv->open_drain, buf));
|
||||
iw_entry(iw, "opendrain", "%s", str_pinmask(priv->open_drain, unit_tmp64));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
@@ -120,28 +124,9 @@ static bool DO_init(Unit *unit)
|
||||
return false;
|
||||
}
|
||||
|
||||
dbg("Claim rsc");
|
||||
|
||||
// Claim all needed pins
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (priv->pins & (1 << i)) {
|
||||
Resource rsc = pin2resource(priv->port_name, (uint8_t) i, &suc);
|
||||
if (!suc) {
|
||||
unit->status = E_BAD_CONFIG;
|
||||
|
||||
rsc_teardown(unit);
|
||||
return false;
|
||||
}
|
||||
|
||||
suc = rsc_claim(unit, rsc);
|
||||
if (!suc) {
|
||||
rsc_teardown(unit);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbg("Init pins");
|
||||
suc = rsc_claim_gpios(unit, priv->port_name, priv->pins);
|
||||
CHECK_SUC();
|
||||
|
||||
uint16_t mask = 1;
|
||||
for (int i = 0; i < 16; i++, mask <<= 1) {
|
||||
|
||||
@@ -62,7 +62,7 @@ struct PayloadBuilder_ {
|
||||
// --- utilities ---
|
||||
|
||||
/** Get already used bytes count */
|
||||
#define pb_length(pb) ((pb)->current - (pb)->start)
|
||||
#define pb_length(pb) ((uint32_t)((pb)->current - (pb)->start))
|
||||
|
||||
/** Reset the current pointer to start */
|
||||
#define pb_rewind(pb) do { pb->current = pb->start; } while (0)
|
||||
|
||||
Reference in New Issue
Block a user