added unit and basic config

sipo
Ondřej Hruška 7 years ago
parent a3df4d79e1
commit 9e7ae008b6
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 5
      framework/resources.c
  2. 5
      gex.mk
  3. 8
      platform/platform.c
  4. 219
      units/i2c/unit_i2c.c
  5. 12
      units/i2c/unit_i2c.h

@ -92,14 +92,13 @@ bool rsc_claim_gpios(Unit *unit, char port_name, uint16_t pins)
Resource rsc = pin2resource(port_name, (uint8_t) i, &suc); Resource rsc = pin2resource(port_name, (uint8_t) i, &suc);
if (!suc) { if (!suc) {
unit->status = E_BAD_CONFIG; unit->status = E_BAD_CONFIG;
// rsc_teardown(unit);
rsc_teardown(unit);
return false; return false;
} }
suc = rsc_claim(unit, rsc); suc = rsc_claim(unit, rsc);
if (!suc) { if (!suc) {
rsc_teardown(unit); // rsc_teardown(unit);
return false; return false;
} }
} }

@ -10,6 +10,7 @@ GEX_SRC_DIR = \
User/units/test \ User/units/test \
User/units/digital_out \ User/units/digital_out \
User/units/digital_in \ User/units/digital_in \
User/units/i2c \
User/TinyFrame \ User/TinyFrame \
User/CWPack \ User/CWPack \
User/tasks User/tasks
@ -80,8 +81,8 @@ else
GEX_CDEFS = $(GEX_CDEFS_BASE) \ GEX_CDEFS = $(GEX_CDEFS_BASE) \
-DUSE_FULL_ASSERT=1 \ -DUSE_FULL_ASSERT=1 \
-DVERBOSE_ASSERT=1 \ -DVERBOSE_ASSERT=1 \
-DDEBUG_VFS=0 \ -DDEBUG_VFS=1 \
-DDEBUG_FLASH_WRITE=0 \ -DDEBUG_FLASH_WRITE=1 \
-DVERBOSE_HARDFAULT=1 \ -DVERBOSE_HARDFAULT=1 \
-DUSE_STACK_MONITOR=1 \ -DUSE_STACK_MONITOR=1 \
-DUSE_DEBUG_UART=1 -DUSE_DEBUG_UART=1

@ -11,6 +11,7 @@
#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"
#include "units/i2c/unit_i2c.h"
#include "units/test/unit_test.h" #include "units/test/unit_test.h"
void plat_init_resources(void) void plat_init_resources(void)
@ -22,8 +23,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_DOUT);
ureg_add_type(&UNIT_DIN);
#if HAVE_TEST_UNIT #if HAVE_TEST_UNIT
ureg_add_type(&UNIT_TEST); ureg_add_type(&UNIT_TEST);
@ -35,7 +34,6 @@ void plat_init_resources(void)
// Platform STM32F103C8T6 Bluepill ($4 board from eBay) // Platform STM32F103C8T6 Bluepill ($4 board from eBay)
// Units supported by the platform (known to work correctly) // Units supported by the platform (known to work correctly)
ureg_add_type(&UNIT_NEOPIXEL);
// free all present resources // free all present resources
{ {
@ -77,8 +75,10 @@ void plat_init_resources(void)
__HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE();
// Units supported by the platform (known to work correctly) // Units supported by the platform (known to work correctly)
// ureg_add_type(&UNIT_XYZ); ureg_add_type(&UNIT_DOUT);
ureg_add_type(&UNIT_DIN);
ureg_add_type(&UNIT_NEOPIXEL); ureg_add_type(&UNIT_NEOPIXEL);
ureg_add_type(&UNIT_I2C);
// Free all present resources // Free all present resources
{ {

@ -0,0 +1,219 @@
//
// Created by MightyPork on 2018/01/02.
//
#include "comm/messages.h"
#include "unit_base.h"
#include "utils/avrlibc.h"
#include "unit_i2c.h"
// I2C master
/** Private data structure */
struct priv {
uint8_t periph_num; //!< 1 or 2
bool anf; //!< Enable analog noise filter
uint8_t dnf; //!< Enable digital noise filter (1-15 ... max spike width)
uint8_t speed; //!< 0 - Standard, 1 - Fast, 2 - Fast+
I2C_TypeDef *periph;
};
// ------------------------------------------------------------------------
/** Load from a binary buffer stored in Flash */
static void UI2C_loadBinary(Unit *unit, PayloadParser *pp)
{
struct priv *priv = unit->data;
uint8_t version = pp_u8(pp);
(void)version;
priv->periph_num = pp_u8(pp);
priv->anf = pp_bool(pp);
priv->dnf = pp_u8(pp);
priv->speed = pp_u8(pp);
}
/** Write to a binary buffer for storing in Flash */
static void UI2C_writeBinary(Unit *unit, PayloadBuilder *pb)
{
struct priv *priv = unit->data;
pb_u8(pb, 0); // version
pb_u8(pb, priv->periph_num);
pb_bool(pb, priv->anf);
pb_u8(pb, priv->dnf);
pb_u8(pb, priv->speed);
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
static bool UI2C_loadIni(Unit *unit, const char *key, const char *value)
{
bool suc = true;
struct priv *priv = unit->data;
if (streq(key, "device")) {
priv->periph_num = (uint8_t) avr_atoi(value);
}
else if (streq(key, "analog-filter")) {
priv->anf = str_parse_yn(value, &suc);
}
else if (streq(key, "digital-filter")) {
priv->dnf = (uint8_t) avr_atoi(value);
}
else if (streq(key, "speed")) {
priv->speed = (uint8_t) avr_atoi(value);
}
else {
return false;
}
return suc;
}
/** Generate INI file section for the unit */
static void UI2C_writeIni(Unit *unit, IniWriter *iw)
{
struct priv *priv = unit->data;
iw_comment(iw, "Peripheral number (I2Cx)");
iw_entry(iw, "device", "%d", (int)priv->periph_num);
iw_comment(iw, "Speed: 1-Standard, 2-Fast, 3-Fast+");
iw_entry(iw, "speed", "%d", (int)priv->speed);
iw_comment(iw, "Analog noise filter enable (Y,N)");
iw_entry(iw, "analog-filter", "%s", str_yn(priv->anf));
iw_comment(iw, "Digital noise filter bandwidth (0-15)");
iw_entry(iw, "digital-filter", "%d", (int)priv->dnf);
}
// ------------------------------------------------------------------------
/** Allocate data structure and set defaults */
static bool UI2C_preInit(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv), &suc);
CHECK_SUC();
// some defaults
priv->periph_num = 1;
priv->speed = 1;
priv->anf = true;
priv->dnf = 0;
return true;
}
/** Finalize unit set-up */
static bool UI2C_init(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data;
if (!(priv->periph_num >= 1 && priv->periph_num <= 2)) {
unit->status = E_BAD_CONFIG;
dbg("!! Bad I2C periph");
return false;
}
if (!(priv->speed >= 1 && priv->speed <= 3)) {
unit->status = E_BAD_CONFIG;
dbg("!! Bad I2C speed");
return false;
}
if (priv->dnf > 15) {
unit->status = E_BAD_CONFIG;
dbg("!! Bad I2C DNF bw");
return false;
}
// assign and claim the peripheral
if (priv->periph_num == 1) {
suc = rsc_claim(unit, R_I2C1);
CHECK_SUC();
priv->periph = I2C1;
} else {
suc = rsc_claim(unit, R_I2C2);
CHECK_SUC();
priv->periph = I2C2;
}
// TODO claim pins (config option to remap?)
return true;
}
/** Tear down the unit */
static void UI2C_deInit(Unit *unit)
{
struct priv *priv = unit->data;
// de-init the pins & peripheral only if inited correctly
if (unit->status == E_SUCCESS) {
// TODO
}
// Release all resources
rsc_teardown(unit);
// Free memory
free(unit->data);
unit->data = NULL;
}
// ------------------------------------------------------------------------
enum PinCmd_ {
CMD_WRITE = 0,
CMD_READ = 1,
};
/** Handle a request message */
static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{
struct priv *priv = unit->data;
switch (command) {
case CMD_WRITE:
//
break;
case CMD_READ:
//
break;
default:
com_respond_bad_cmd(frame_id);
return false;
}
return true;
}
// ------------------------------------------------------------------------
/** Unit template */
const UnitDriver UNIT_I2C = {
.name = "I2C",
.description = "I2C master",
// Settings
.preInit = UI2C_preInit,
.cfgLoadBinary = UI2C_loadBinary,
.cfgWriteBinary = UI2C_writeBinary,
.cfgLoadIni = UI2C_loadIni,
.cfgWriteIni = UI2C_writeIni,
// Init
.init = UI2C_init,
.deInit = UI2C_deInit,
// Function
.handleRequest = UI2C_handleRequest,
};

@ -0,0 +1,12 @@
//
// Created by MightyPork on 2018/01/02.
//
#ifndef GEX_F072_UNIT_I2C_H
#define GEX_F072_UNIT_I2C_H
#include "unit.h"
extern const UnitDriver UNIT_I2C;
#endif //GEX_F072_UNIT_I2C_H
Loading…
Cancel
Save