Deleted Pin unit, replaced by DI and DO

sipo
Ondřej Hruška 7 years ago
parent 9873d18298
commit 0cdb811d40
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 1
      gex.mk
  2. 2
      platform/platform.c
  3. 234
      units/pin/unit_pin.c
  4. 12
      units/pin/unit_pin.h
  5. 1
      units/test/unit_test.c

@ -8,7 +8,6 @@ GEX_SRC_DIR = \
User/units/system \ User/units/system \
User/units/neopixel \ User/units/neopixel \
User/units/test \ User/units/test \
User/units/pin \
User/units/digital_out \ User/units/digital_out \
User/units/digital_in \ User/units/digital_in \
User/TinyFrame \ User/TinyFrame \

@ -8,7 +8,6 @@
#include "framework/resources.h" #include "framework/resources.h"
#include "framework/unit_registry.h" #include "framework/unit_registry.h"
#include "units/pin/unit_pin.h"
#include "units/digital_out/unit_dout.h" #include "units/digital_out/unit_dout.h"
#include "units/digital_in/unit_din.h" #include "units/digital_in/unit_din.h"
#include "units/neopixel/unit_neopixel.h" #include "units/neopixel/unit_neopixel.h"
@ -23,7 +22,6 @@ void plat_init_resources(void)
__HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE();
// --- Common unit drivers --- // --- Common unit drivers ---
ureg_add_type(&UNIT_PIN);
ureg_add_type(&UNIT_DOUT); ureg_add_type(&UNIT_DOUT);
ureg_add_type(&UNIT_DIN); ureg_add_type(&UNIT_DIN);

@ -1,234 +0,0 @@
//
// Created by MightyPork on 2017/11/25.
//
#include "comm/messages.h"
#include "unit_base.h"
#include "unit_pin.h"
/** Private data structure */
struct priv {
char port_name;
uint8_t pin_number;
bool output;
bool pull_up;
bool open_drain;
uint32_t ll_pin;
GPIO_TypeDef *port;
};
// ------------------------------------------------------------------------
/** Load from a binary buffer stored in Flash */
static void Pin_loadBinary(Unit *unit, PayloadParser *pp)
{
struct priv *priv = unit->data;
priv->port_name = pp_char(pp);
priv->pin_number = pp_u8(pp);
priv->output = pp_bool(pp);
priv->pull_up = pp_bool(pp);
priv->open_drain = pp_bool(pp);
}
/** Write to a binary buffer for storing in Flash */
static void Pin_writeBinary(Unit *unit, PayloadBuilder *pb)
{
struct priv *priv = unit->data;
pb_char(pb, priv->port_name);
pb_u8(pb, priv->pin_number);
pb_bool(pb, priv->output);
pb_bool(pb, priv->pull_up);
pb_bool(pb, priv->open_drain);
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
static bool Pin_loadIni(Unit *unit, const char *key, const char *value)
{
bool suc = true;
struct priv *priv = unit->data;
if (streq(key, "pin")) {
suc = parse_pin(value, &priv->port_name, &priv->pin_number);
}
else if (streq(key, "dir")) {
priv->output = str_parse_01(value, "IN", "OUT", &suc);
}
else if (streq(key, "pull")) {
priv->pull_up = str_parse_01(value, "DOWN", "UP", &suc);
}
else if (streq(key, "opendrain")) {
priv->open_drain = str_parse_yn(value, &suc);
} else {
return false;
}
return suc;
}
/** Generate INI file section for the unit */
static void Pin_writeIni(Unit *unit, IniWriter *iw)
{
struct priv *priv = unit->data;
iw_comment(iw, "Physical pin");
iw_entry(iw, "pin", "%c%d", priv->port_name, priv->pin_number);
iw_comment(iw, "Direction (IN, OUT)");
iw_entry(iw, "dir", "%s", str_01(priv->output, "IN", "OUT"));
iw_comment(iw, "Pull resistor, only for input (UP, DOWN)");
iw_entry(iw, "pull", "%s", str_01(priv->pull_up, "DOWN", "UP"));
iw_comment(iw, "Open drain, only for output (Y, N)");
iw_entry(iw, "opendrain", "%s", str_yn(priv->open_drain));
}
// ------------------------------------------------------------------------
/** Allocate data structure and set defaults */
static bool Pin_preInit(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv), &suc);
CHECK_SUC();
// some defaults
priv->pin_number = 0;
priv->port_name = 'A';
priv->output = true;
priv->open_drain = false;
priv->pull_up = false;
return true;
}
/** Finalize unit set-up */
static bool Pin_init(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data;
// --- Parse config ---
priv->ll_pin = pin2ll(priv->pin_number, &suc);
priv->port = port2periph(priv->port_name, &suc);
Resource rsc = pin2resource(priv->port_name, priv->pin_number, &suc);
if (!suc) {
unit->status = E_BAD_CONFIG;
return false;
}
// --- Claim resources ---
if (!rsc_claim(unit, rsc)) return false;
// --- Init hardware ---
LL_GPIO_SetPinMode(priv->port, priv->ll_pin,
priv->output ? LL_GPIO_MODE_OUTPUT : LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinOutputType(priv->port, priv->ll_pin,
priv->open_drain ? LL_GPIO_OUTPUT_OPENDRAIN : LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(priv->port, priv->ll_pin,
priv->pull_up ? LL_GPIO_PULL_UP : LL_GPIO_PULL_DOWN);
LL_GPIO_SetPinSpeed(priv->port, priv->ll_pin,
LL_GPIO_SPEED_FREQ_HIGH);
return true;
}
/** Tear down the unit */
static void Pin_deInit(Unit *unit)
{
struct priv *priv = unit->data;
// configure the pin as analog
LL_GPIO_SetPinMode(priv->port, priv->ll_pin, LL_GPIO_MODE_ANALOG);
// Release all resources
rsc_teardown(unit);
// Free memory
free(unit->data);
unit->data = NULL;
}
// ------------------------------------------------------------------------
enum PinCmd_ {
CMD_CLEAR = 0,
CMD_SET = 1,
CMD_TOGGLE = 2,
CMD_READ = 3,
};
/** Handle a request message */
static bool Pin_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{
(void)pp;
struct priv *priv = unit->data;
switch (command) {
case CMD_CLEAR:
if (priv->output) {
LL_GPIO_ResetOutputPin(priv->port, priv->ll_pin);
} else goto must_be_output;
break;
case CMD_SET:
if (priv->output) {
LL_GPIO_SetOutputPin(priv->port, priv->ll_pin);
} else goto must_be_output;
break;
case CMD_TOGGLE:
if (priv->output) {
LL_GPIO_TogglePin(priv->port, priv->ll_pin);
} else goto must_be_output;
break;
case CMD_READ:
if (!priv->output) {
com_respond_u8(frame_id, (bool) LL_GPIO_IsInputPinSet(priv->port, priv->ll_pin));
} else goto must_be_input;
break;
default:
com_respond_bad_cmd(frame_id);
return false;
}
return true;
must_be_output:
com_respond_err(frame_id, "NOT OUTPUT PIN");
return false;
must_be_input:
com_respond_err(frame_id, "NOT INPUT PIN");
return false;
}
// ------------------------------------------------------------------------
/** Unit template */
const UnitDriver UNIT_PIN = {
.name = "PIN",
.description = "Single digital I/O pin",
// Settings
.preInit = Pin_preInit,
.cfgLoadBinary = Pin_loadBinary,
.cfgWriteBinary = Pin_writeBinary,
.cfgLoadIni = Pin_loadIni,
.cfgWriteIni = Pin_writeIni,
// Init
.init = Pin_init,
.deInit = Pin_deInit,
// Function
.handleRequest = Pin_handleRequest,
};

@ -1,12 +0,0 @@
//
// Created by MightyPork on 2017/11/25.
//
#ifndef U_PIN_H
#define U_PIN_H
#include "unit.h"
extern const UnitDriver UNIT_PIN;
#endif //U_PIN_H

@ -96,6 +96,7 @@ enum PinCmd_ {
CMD_BULKWRITE = 3, CMD_BULKWRITE = 3,
}; };
// this is a very long text for testing bulk read
static const char *longtext = "The history of all hitherto existing societies is the history of class struggles.\n\nFreeman and slave, patrician and plebeian, lord and serf, guild-master and journeyman, in a word, oppressor and oppressed, stood in constant opposition to one another, carried on an uninterrupted, now hidden, now open fight, a fight that each time ended, either in a revolutionary re-constitution of society at large, or in the common ruin of the contending classes.\n\nIn the earlier epochs of history, we find almost everywhere a complicated arrangement of society into various orders, a manifold gradation of social rank. In ancient Rome we have patricians, knights, plebeians, slaves; in the Middle Ages, feudal lords, vassals, guild-masters, journeymen, apprentices, serfs; in almost all of these classes, again, subordinate gradations.\n\nThe modern bourgeois society that has sprouted from the ruins of feudal society has not done away with class antagonisms. It has but established new classes, new conditions of oppression, new forms of struggle in place of the old ones. Our epoch, the epoch of the bourgeoisie, possesses, however, this distinctive feature: it has simplified the class antagonisms. Society as a whole is more and more splitting up into two great hostile camps, into two great classes, directly facing each other: Bourgeoisie and Proletariat.\n\nFrom the serfs of the Middle Ages sprang the chartered burghers of the earliest towns. From these burgesses the first elements of the bourgeoisie were developed.\n\nThe discovery of America, the rounding of the Cape, opened up fresh ground for the rising bourgeoisie. The East-Indian and Chinese markets, the colonisation of America, trade with the colonies, the increase in the means of exchange and in commodities generally, gave to commerce, to navigation, to industry, an impulse never before known, and thereby, to the revolutionary element in the tottering feudal society, a rapid development.\n\nThe feudal system of industry, under which industrial production was monopolised by closed guilds, now no longer sufficed for the growing wants of the new markets. The manufacturing system took its place. The guild-masters were pushed on one side by the manufacturing middle class; division of labour between the different corporate guilds vanished in the face of division of labour in each single workshop.\n\nMeantime the markets kept ever growing, the demand ever rising. Even manufacture no longer sufficed. Thereupon, steam and machinery revolutionised industrial production. The place of manufacture was taken by the giant, Modern Industry, the place of the industrial middle class, by industrial millionaires, the leaders of whole industrial armies, the modern bourgeois.\n\nModern industry has established the world-market, for which the discovery of America paved the way. This market has given an immense development to commerce, to navigation, to communication by land. This development has, in its time, reacted on the extension of industry; and in proportion as industry, commerce, navigation, railways extended, in the same proportion the bourgeoisie developed, increased its capital, and pushed into the background every class handed down from the Middle Ages.\n\nWe see, therefore, how the modern bourgeoisie is itself the product of a long course of development, of a series of revolutions in the modes of production and of exchange.\n\nEach step in the development of the bourgeoisie was accompanied by a corresponding political advance of that class. An oppressed class under the sway of the feudal nobility, an armed and self-governing association in the mediaeval commune; here independent urban republic (as in Italy and Germany), there taxable \"third estate\" of the monarchy (as in France), afterwards, in the period of manufacture proper, serving either the semi-feudal or the absolute monarchy as a counterpoise against the nobility, and, in fact, corner-stone of the great monarchies in general, the bourgeoisie has at last, since the establishment of Modern Industry and of the world-market, conquered for itself, in the modern representative State, exclusive political sway. The executive of the modern State is but a committee for managing the common affairs of the whole bourgeoisie."; static const char *longtext = "The history of all hitherto existing societies is the history of class struggles.\n\nFreeman and slave, patrician and plebeian, lord and serf, guild-master and journeyman, in a word, oppressor and oppressed, stood in constant opposition to one another, carried on an uninterrupted, now hidden, now open fight, a fight that each time ended, either in a revolutionary re-constitution of society at large, or in the common ruin of the contending classes.\n\nIn the earlier epochs of history, we find almost everywhere a complicated arrangement of society into various orders, a manifold gradation of social rank. In ancient Rome we have patricians, knights, plebeians, slaves; in the Middle Ages, feudal lords, vassals, guild-masters, journeymen, apprentices, serfs; in almost all of these classes, again, subordinate gradations.\n\nThe modern bourgeois society that has sprouted from the ruins of feudal society has not done away with class antagonisms. It has but established new classes, new conditions of oppression, new forms of struggle in place of the old ones. Our epoch, the epoch of the bourgeoisie, possesses, however, this distinctive feature: it has simplified the class antagonisms. Society as a whole is more and more splitting up into two great hostile camps, into two great classes, directly facing each other: Bourgeoisie and Proletariat.\n\nFrom the serfs of the Middle Ages sprang the chartered burghers of the earliest towns. From these burgesses the first elements of the bourgeoisie were developed.\n\nThe discovery of America, the rounding of the Cape, opened up fresh ground for the rising bourgeoisie. The East-Indian and Chinese markets, the colonisation of America, trade with the colonies, the increase in the means of exchange and in commodities generally, gave to commerce, to navigation, to industry, an impulse never before known, and thereby, to the revolutionary element in the tottering feudal society, a rapid development.\n\nThe feudal system of industry, under which industrial production was monopolised by closed guilds, now no longer sufficed for the growing wants of the new markets. The manufacturing system took its place. The guild-masters were pushed on one side by the manufacturing middle class; division of labour between the different corporate guilds vanished in the face of division of labour in each single workshop.\n\nMeantime the markets kept ever growing, the demand ever rising. Even manufacture no longer sufficed. Thereupon, steam and machinery revolutionised industrial production. The place of manufacture was taken by the giant, Modern Industry, the place of the industrial middle class, by industrial millionaires, the leaders of whole industrial armies, the modern bourgeois.\n\nModern industry has established the world-market, for which the discovery of America paved the way. This market has given an immense development to commerce, to navigation, to communication by land. This development has, in its time, reacted on the extension of industry; and in proportion as industry, commerce, navigation, railways extended, in the same proportion the bourgeoisie developed, increased its capital, and pushed into the background every class handed down from the Middle Ages.\n\nWe see, therefore, how the modern bourgeoisie is itself the product of a long course of development, of a series of revolutions in the modes of production and of exchange.\n\nEach step in the development of the bourgeoisie was accompanied by a corresponding political advance of that class. An oppressed class under the sway of the feudal nobility, an armed and self-governing association in the mediaeval commune; here independent urban republic (as in Italy and Germany), there taxable \"third estate\" of the monarchy (as in France), afterwards, in the period of manufacture proper, serving either the semi-feudal or the absolute monarchy as a counterpoise against the nobility, and, in fact, corner-stone of the great monarchies in general, the bourgeoisie has at last, since the establishment of Modern Industry and of the world-market, conquered for itself, in the modern representative State, exclusive political sway. The executive of the modern State is but a committee for managing the common affairs of the whole bourgeoisie.";
static void br_longtext(struct bulk_read *bulk, uint32_t chunk, uint8_t *buffer) static void br_longtext(struct bulk_read *bulk, uint32_t chunk, uint8_t *buffer)

Loading…
Cancel
Save