Merge branch 'merge-stm32' into 'main'

Merge STM32 repo

See merge request sta-git/alpaka/sta-core!1
This commit is contained in:
Henrik Stickann 2023-04-22 11:21:16 +00:00
commit 13af941187
75 changed files with 6603 additions and 2286 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# IDE settings
.settings/
.vscode/
# Contain local paths
.mxproject

View File

@ -96,6 +96,7 @@ Interfaces for the following resources are provided:
* Signal
* SPI
* UART
* I2C
## HAL implementations
@ -108,6 +109,16 @@ Implementations using the HAL are provided for the following interfaces:
To enable these implementations follow the instructions from the individual headers.
## Merge with STM32 Core
The merge with STM32 Core allows seamless usage of the STM32 implementations for the different Interfaces etc.
Notable inclusions:
* CAN
* I2C
* SPI
* UART
## Atomic implementations
Implementations using atomic variables are provided for the following interfaces:

2751
docs/Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
# Doxyfile 1.9.4
# Doxyfile 1.9.6
# This file describes the settings to be used by the documentation system
# doxygen (www.doxygen.org) for a project.
@ -19,7 +19,8 @@
# configuration file:
# doxygen -x [configFile]
# Use doxygen to compare the used configuration file with the template
# configuration file without replacing the environment variables:
# configuration file without replacing the environment variables or CMake type
# replacement variables:
# doxygen -x_noenv [configFile]
#---------------------------------------------------------------------------
@ -41,19 +42,19 @@ DOXYFILE_ENCODING = UTF-8
# title of most generated pages and in a few other places.
# The default value is: My Project.
PROJECT_NAME = "My Project"
PROJECT_NAME = "TODO: Add project name"
# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER =
PROJECT_NUMBER = "TODO: Add project version"
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
# quick idea about the purpose of the project. Keep the description short.
PROJECT_BRIEF =
PROJECT_BRIEF = "TODO: Add brief project description"
# With the PROJECT_LOGO tag one can specify a logo or an icon that is included
# in the documentation. The maximum height of the logo should not exceed 55
@ -85,7 +86,7 @@ CREATE_SUBDIRS = NO
# level increment doubles the number of directories, resulting in 4096
# directories at level 8 which is the default and also the maximum value. The
# sub-directories are organized in 2 levels, the first level always has a fixed
# numer of 16 directories.
# number of 16 directories.
# Minimum value: 0, maximum value: 8, default value: 8.
# This tag requires that the tag CREATE_SUBDIRS is set to YES.
@ -192,15 +193,15 @@ STRIP_FROM_PATH =
# specify the list of include paths that are normally passed to the compiler
# using the -I flag.
STRIP_FROM_INC_PATH = App/include \
Libs/BMP388/include \
Libs/can_bus/include \
Libs/cmsis_os2/include \
Libs/hdlc/include \
Libs/iso-tp/include \
Libs/MCP2518FD/include \
Libs/mpaland-printf \
Libs/sta-core/include
STRIP_FROM_INC_PATH = \
include \
App/include \
Libs/sta-core/include \
Libs/mpaland-printf \
Libs/rtos2/include \
Libs/MCP2518FD/include \
Libs/iso-tp/include \
Libs/BMP388/include
# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but
# less readable) file names. This can be useful is your file systems doesn't
@ -349,7 +350,7 @@ OPTIMIZE_OUTPUT_SLICE = NO
#
# Note see also the list of default file extension mappings.
EXTENSION_MAPPING =
EXTENSION_MAPPING =
# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments
# according to the Markdown format, which allows for more readable
@ -575,7 +576,8 @@ HIDE_UNDOC_MEMBERS = NO
# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all
# undocumented classes that are normally visible in the class hierarchy. If set
# to NO, these classes will be included in the various overviews. This option
# has no effect if EXTRACT_ALL is enabled.
# will also hide undocumented C++ concepts if enabled. This option has no effect
# if EXTRACT_ALL is enabled.
# The default value is: NO.
HIDE_UNDOC_CLASSES = NO
@ -613,7 +615,8 @@ INTERNAL_DOCS = NO
# Windows (including Cygwin) and MacOS, users should typically set this option
# to NO, whereas on Linux or other Unix flavors it should typically be set to
# YES.
# The default value is: system dependent.
# Possible values are: SYSTEM, NO and YES.
# The default value is: SYSTEM.
CASE_SENSE_NAMES = NO
@ -694,7 +697,7 @@ SORT_MEMBERS_CTORS_1ST = NO
# appear in their defined order.
# The default value is: NO.
SORT_GROUP_NAMES = NO
SORT_GROUP_NAMES = YES
# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by
# fully-qualified names, including namespaces. If set to NO, the class list will
@ -865,6 +868,14 @@ WARN_IF_INCOMPLETE_DOC = YES
WARN_NO_PARAMDOC = NO
# If WARN_IF_UNDOC_ENUM_VAL option is set to YES, doxygen will warn about
# undocumented enumeration values. If set to NO, doxygen will accept
# undocumented enumeration values. If EXTRACT_ALL is set to YES then this flag
# will automatically be disabled.
# The default value is: NO.
WARN_IF_UNDOC_ENUM_VAL = NO
# If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when
# a warning is encountered. If the WARN_AS_ERROR tag is set to FAIL_ON_WARNINGS
# then doxygen will continue running as if WARN_AS_ERROR tag is set to NO, but
@ -921,10 +932,21 @@ INPUT =
# libiconv (or the iconv built into libc) for the transcoding. See the libiconv
# documentation (see:
# https://www.gnu.org/software/libiconv/) for the list of possible encodings.
# See also: INPUT_FILE_ENCODING
# The default value is: UTF-8.
INPUT_ENCODING = UTF-8
# This tag can be used to specify the character encoding of the source files
# that doxygen parses The INPUT_FILE_ENCODING tag can be used to specify
# character encoding on a per file pattern basis. Doxygen will compare the file
# name with each pattern and apply the encoding instead of the default
# INPUT_ENCODING) if there is a match. The character encodings are a list of the
# form: pattern=encoding (like *.php=ISO-8859-1). See cfg_input_encoding
# "INPUT_ENCODING" for further information on supported encodings.
INPUT_FILE_ENCODING =
# If the value of the INPUT tag contains directories, you can use the
# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and
# *.h) to filter out the source-files in the directories.
@ -943,16 +965,14 @@ INPUT_ENCODING = UTF-8
# comment), *.py, *.pyw, *.f90, *.f95, *.f03, *.f08, *.f18, *.f, *.for, *.vhd,
# *.vhdl, *.ucf, *.qsf and *.ice.
FILE_PATTERNS = *.c \
*.cpp \
*.tpp \
*.h \
*.hpp \
*.markdown \
*.md \
*.dox \
*.py \
*.pyw \
FILE_PATTERNS = \
*.c \
*.cpp \
*.h \
*.hpp \
*.tpp \
*.py \
*.pyw
# The RECURSIVE tag can be used to specify whether or not subdirectories should
# be searched for input files as well.
@ -967,7 +987,12 @@ RECURSIVE = YES
# Note that relative paths are relative to the directory from which doxygen is
# run.
EXCLUDE = Core Drivers Middlewares Debug Release
EXCLUDE = \
Core \
Drivers \
Middlewares \
Debug \
Release
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
@ -1037,6 +1062,11 @@ IMAGE_PATH =
# code is scanned, but not when the output code is generated. If lines are added
# or removed, the anchors will not be placed correctly.
#
# Note that doxygen will use the data processed and written to standard output
# for further processing, therefore nothing else, like debug statements or used
# commands (so in case of a Windows batch file always use @echo OFF), should be
# written to standard output.
#
# Note that for custom extensions or not directly supported extensions you also
# need to set EXTENSION_MAPPING for the extension otherwise the files are not
# properly processed by doxygen.
@ -1078,6 +1108,15 @@ FILTER_SOURCE_PATTERNS =
USE_MDFILE_AS_MAINPAGE =
# The Fortran standard specifies that for fixed formatted Fortran code all
# characters from position 72 are to be considered as comment. A common
# extension is to allow longer lines before the automatic comment starts. The
# setting FORTRAN_COMMENT_AFTER will also make it possible that longer lines can
# be processed before the automatic comment starts.
# Minimum value: 7, maximum value: 10000, default value: 72.
FORTRAN_COMMENT_AFTER = 72
#---------------------------------------------------------------------------
# Configuration options related to source browsing
#---------------------------------------------------------------------------
@ -1215,10 +1254,11 @@ CLANG_DATABASE_PATH =
ALPHABETICAL_INDEX = YES
# In case all classes in a project start with a common prefix, all classes will
# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag
# can be used to specify a prefix (or a list of prefixes) that should be ignored
# while generating the index headers.
# The IGNORE_PREFIX tag can be used to specify a prefix (or a list of prefixes)
# that should be ignored while generating the index headers. The IGNORE_PREFIX
# tag works for classes, function and member names. The entity will be placed in
# the alphabetical list under the first letter of the entity name that remains
# after removing the prefix.
# This tag requires that the tag ALPHABETICAL_INDEX is set to YES.
IGNORE_PREFIX =
@ -1297,7 +1337,12 @@ HTML_STYLESHEET =
# Doxygen will copy the style sheet files to the output directory.
# Note: The order of the extra style sheet files is of importance (e.g. the last
# style sheet in the list overrules the setting of the previous ones in the
# list). For an example see the documentation.
# list).
# Note: Since the styling of scrollbars can currently not be overruled in
# Webkit/Chromium, the styling will be left out of the default doxygen.css if
# one or more extra stylesheets have been specified. So if scrollbar
# customization is desired it has to be added explicitly. For an example see the
# documentation.
# This tag requires that the tag GENERATE_HTML is set to YES.
HTML_EXTRA_STYLESHEET =
@ -1312,6 +1357,19 @@ HTML_EXTRA_STYLESHEET =
HTML_EXTRA_FILES =
# The HTML_COLORSTYLE tag can be used to specify if the generated HTML output
# should be rendered with a dark or light theme.
# Possible values are: LIGHT always generate light mode output, DARK always
# generate dark mode output, AUTO_LIGHT automatically set the mode according to
# the user preference, use light mode if no preference is set (the default),
# AUTO_DARK automatically set the mode according to the user preference, use
# dark mode if no preference is set and TOGGLE allow to user to switch between
# light and dark mode via a button.
# The default value is: AUTO_LIGHT.
# This tag requires that the tag GENERATE_HTML is set to YES.
HTML_COLORSTYLE = TOGGLE
# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen
# will adjust the colors in the style sheet and background images according to
# this color. Hue is specified as an angle on a color-wheel, see
@ -1675,17 +1733,6 @@ HTML_FORMULA_FORMAT = png
FORMULA_FONTSIZE = 10
# Use the FORMULA_TRANSPARENT tag to determine whether or not the images
# generated for formulas are transparent PNGs. Transparent PNGs are not
# supported properly for IE 6.0, but are supported on all modern browsers.
#
# Note that when changing this option you need to delete any form_*.png files in
# the HTML output directory before the changes have effect.
# The default value is: YES.
# This tag requires that the tag GENERATE_HTML is set to YES.
FORMULA_TRANSPARENT = YES
# The FORMULA_MACROFILE can contain LaTeX \newcommand and \renewcommand commands
# to create new LaTeX commands to be used in formulas as building blocks. See
# the section "Including formulas" for details.
@ -1861,7 +1908,7 @@ EXTRA_SEARCH_MAPPINGS =
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
# The default value is: YES.
GENERATE_LATEX = YES
GENERATE_LATEX = NO
# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
@ -2389,7 +2436,7 @@ HIDE_UNDOC_RELATIONS = YES
# set to NO
# The default value is: NO.
HAVE_DOT = YES
HAVE_DOT = NO
# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is allowed
# to run in parallel. When set to 0 doxygen will base this on the number of
@ -2401,26 +2448,38 @@ HAVE_DOT = YES
DOT_NUM_THREADS = 0
# When you want a differently looking font in the dot files that doxygen
# generates you can specify the font name using DOT_FONTNAME. You need to make
# sure dot is able to find the font, which can be done by putting it in a
# standard location or by setting the DOTFONTPATH environment variable or by
# setting DOT_FONTPATH to the directory containing the font.
# The default value is: Helvetica.
# DOT_COMMON_ATTR is common attributes for nodes, edges and labels of
# subgraphs. When you want a differently looking font in the dot files that
# doxygen generates you can specify fontname, fontcolor and fontsize attributes.
# For details please see <a href=https://graphviz.org/doc/info/attrs.html>Node,
# Edge and Graph Attributes specification</a> You need to make sure dot is able
# to find the font, which can be done by putting it in a standard location or by
# setting the DOTFONTPATH environment variable or by setting DOT_FONTPATH to the
# directory containing the font. Default graphviz fontsize is 14.
# The default value is: fontname=Helvetica,fontsize=10.
# This tag requires that the tag HAVE_DOT is set to YES.
DOT_FONTNAME = Helvetica
DOT_COMMON_ATTR = "fontname=Helvetica,fontsize=10"
# The DOT_FONTSIZE tag can be used to set the size (in points) of the font of
# dot graphs.
# Minimum value: 4, maximum value: 24, default value: 10.
# DOT_EDGE_ATTR is concatenated with DOT_COMMON_ATTR. For elegant style you can
# add 'arrowhead=open, arrowtail=open, arrowsize=0.5'. <a
# href=https://graphviz.org/doc/info/arrows.html>Complete documentation about
# arrows shapes.</a>
# The default value is: labelfontname=Helvetica,labelfontsize=10.
# This tag requires that the tag HAVE_DOT is set to YES.
DOT_FONTSIZE = 10
DOT_EDGE_ATTR = "labelfontname=Helvetica,labelfontsize=10"
# By default doxygen will tell dot to use the default font as specified with
# DOT_FONTNAME. If you specify a different font using DOT_FONTNAME you can set
# the path where dot can find it using this tag.
# DOT_NODE_ATTR is concatenated with DOT_COMMON_ATTR. For view without boxes
# around nodes set 'shape=plain' or 'shape=plaintext' <a
# href=https://www.graphviz.org/doc/info/shapes.html>Shapes specification</a>
# The default value is: shape=box,height=0.2,width=0.4.
# This tag requires that the tag HAVE_DOT is set to YES.
DOT_NODE_ATTR = "shape=box,height=0.2,width=0.4"
# You can set the path where dot can find font specified with fontname in
# DOT_COMMON_ATTR and others dot attributes.
# This tag requires that the tag HAVE_DOT is set to YES.
DOT_FONTPATH =
@ -2663,18 +2722,6 @@ DOT_GRAPH_MAX_NODES = 50
MAX_DOT_GRAPH_DEPTH = 0
# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent
# background. This is disabled by default, because dot on Windows does not seem
# to support this out of the box.
#
# Warning: Depending on the platform used, enabling this option may lead to
# badly anti-aliased labels on the edges of a graph (i.e. they become hard to
# read).
# The default value is: NO.
# This tag requires that the tag HAVE_DOT is set to YES.
DOT_TRANSPARENT = NO
# Set the DOT_MULTI_TARGETS tag to YES to allow dot to generate multiple output
# files in one run (i.e. multiple -o and -T options on the command line). This
# makes dot run faster, but since only newer versions of dot (>1.8.10) support

View File

@ -0,0 +1,12 @@
#ifndef STA_CORE_ARDUINO_NOT_IMPLEMENTED_HPP
#define STA_CORE_ARDUINO_NOT_IMPLEMENTED_HPP
/**
* @defgroup sta_core_arduino Arduino
* @ingroup sta_core_platforms
* @brief Modules implemented for the Arduino platform.
*/
#endif // STA_CORE_ARDUINO_NOT_IMPLEMENTED_HPP

View File

@ -1,79 +1,63 @@
/**
* @file
* @brief Assertion handling.
*
* Configuration:
* * STA_ASSERT_FORCE: Ignore debug defines and always enable assertions
* * DEBUG: Enables assertions when defined
* * NDEBUG: Disables assertions when defined (overrides DEBUG)
*/
#ifndef STA_ASSERT_HPP
#define STA_ASSERT_HPP
#ifndef STA_CORE_ASSERT_HPP
#define STA_CORE_ASSERT_HPP
/**
* @defgroup staCore Core
* @defgroup sta_core Core
* @brief STA Core library
*/
/**
* @defgroup staCoreBuildConfig Build Config
* @ingroup staCore
* @brief Build configuration options
* @defgroup sta_core_platforms Platforms
* @ingroup sta_core
* @brief Platform specific implementations.
*/
/**
* @defgroup staCoreAssert Assert
* @ingroup staCore
* @brief Assertion handling.
*/
#ifdef DOXYGEN
/**
* @def STA_ASSERT_ENABLE
* @brief Enable module.
*
* Automatically defined if DEBUG is defined.
* Automatically disabled if NDEBUG is defined.
*
* @ingroup staCoreBuildConfig
*/
# define STA_ASSERT_ENABLE
/**
* @def STA_ASSERT_DISABLE
* @brief Force disable module.
*
* Overrides STA_ASSERT_ENABLE option.
*
* @ingroup staCoreBuildConfig
*/
# define STA_ASSERT_DISABLE
#endif // DOXYGEN
#include <sta/config.hpp>
#ifdef DEBUG
# ifndef STA_ASSERT_ENABLE
# define STA_ASSERT_ENABLE
# endif // !STA_ASSERT_ENABLE
#endif // DEBUG
#if defined(NDEBUG) || defined(STA_ASSERT_DISABLE)
# ifdef STA_ASSERT_ENABLE
# undef STA_ASSERT_ENABLE
# endif // STA_ASSERT_ENABLE
#endif // NDEBUG || STA_ASSERT_DISABLE
// Determine if module should be enabled
// Condition:
// STA_ASSERT_FORCE is defined
// or
// DEBUG is defined but not NDEBUG
#ifdef STA_ASSERT_FORCE
# define STA_ASSERT_ENABLED
#else // !STA_ASSERT_FORCE
# if defined(DEBUG) && !defined(NDEBUG)
# define STA_ASSERT_ENABLED
# endif // DEBUG && !NDEBUG
#endif // !STA_ASSERT_FORCE
// Show enabled module in doxygen output
#ifdef DOXYGEN
# define STA_ASSERT_ENABLE
#endif // DOXYGEN
#ifdef STA_ASSERT_ENABLE
#if defined(STA_ASSERT_ENABLED) || defined(DOXYGEN)
#include <cstdint>
/**
* @defgroup sta_core_assert Assert
* @ingroup sta_core
* @brief Assertion handling.
*/
namespace sta
{
/**
* @ingroup sta_core_assert
* @{
*/
/**
* @brief Handle failed assertions.
*
@ -85,8 +69,6 @@ namespace sta
* @param expr Asserted expression or message
* @param file File name
* @param line Line number
*
* @ingroup staCoreAssert
*/
void assert_failed(const char * expr, const char * file, uint32_t line);
@ -94,48 +76,38 @@ namespace sta
* @brief Stop execution.
*
* Weak implementation can be overridden.
*
* @ingroup staCoreAssert
*/
void assert_halt();
/** @} */
} // namespace sta
/**
* @def STA_HALT
* @brief Set function called after failed asserts.
*
* @ingroup staCoreBuildConfig
* @ingroup sta_core_assert
* @{
*/
# ifndef STA_HALT
# define STA_HALT() sta::assert_halt()
# endif // !STA_HALT
/**
* @brief Assert expression.
*
* @param expr Expression
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT(expr) ( (void)( !(expr) && ( sta::assert_failed(#expr, __FILE__, __LINE__), 1 ) && ( STA_HALT(), 1 ) ) )
# define STA_ASSERT(expr) ( (void)( !(expr) && ( sta::assert_failed(#expr, __FILE__, __LINE__), 1 ) && ( sta::assert_halt(), 1 ) ) )
/**
* @brief Assert expression.
*
* @param expr Expression
* @param msg Message shown on failure
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT_MSG(expr, msg) ( (void)( !(expr) && ( sta::assert_failed(msg, __FILE__, __LINE__), 1 ) && ( STA_HALT(), 1 ) ) )
# define STA_ASSERT_MSG(expr, msg) ( (void)( !(expr) && ( sta::assert_failed(msg, __FILE__, __LINE__), 1 ) && ( sta::assert_halt(), 1 ) ) )
/**
* @brief Assert expression if condition is true.
*
* @param cond Condition
* @param expr Expression
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT_COND(cond, expr) ( (void)( (cond) ? STA_ASSERT(expr) : 1 ) )
/**
@ -144,15 +116,19 @@ namespace sta
* @param cond Condition
* @param expr Expression
* @param msg Message shown on failure
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT_COND_MSG(cond, expr, msg) ( (void)( (cond) ? STA_ASSERT_MSG(expr, msg) ) )
/**
* @brief Expression only evaluated when assertions are enabled.
*
* @param expr Expression
*/
# define STA_ASSERT_EXTRA(expr) expr
# define STA_ASSERT_EXTRA(expr) expr;
/** @} */
#else // !STA_ASSERT_ENABLE
#else // !STA_ASSERT_ENABLED
# define STA_ASSERT(expr) ((void)0)
# define STA_ASSERT_MSG(expr, msg) ((void)0)
@ -161,7 +137,7 @@ namespace sta
# define STA_ASSERT_EXTRA(expr) ((void)0)
#endif // !STA_ASSERT_ENABLE
#endif // !STA_ASSERT_ENABLED
#endif // STA_ASSERT_HPP
#endif // STA_CORE_ASSERT_HPP

View File

@ -1,39 +1,46 @@
/**
* @file
* @brief Atomic mutex implementation.
*
* Configuration:
* STA_ATOMIC_ENABLE: Enable module
* STA_STDLIB_HAS_ATOMIC: Enable module
*/
#ifndef STA_ATOMIC_MUTEX_HPP
#define STA_ATOMIC_MUTEX_HPP
#ifndef STA_CORE_ATOMIC_MUTEX_HPP
#define STA_CORE_ATOMIC_MUTEX_HPP
#include <sta/config.hpp>
#ifdef STA_ATOMIC_ENABLE
#ifdef STA_STDLIB_HAS_ATOMIC
# define STA_ATOMIC_ENABLED
#endif // STA_STDLIB_HAS_ATOMIC
#include <sta/intf/mutex.hpp>
#if defined(STA_ATOMIC_ENABLED) || defined(DOXYGEN)
#include <sta/mutex.hpp>
#include <atomic>
namespace sta
{
/**
* @brief Implementation of `Mutex` interface using `std::atomic_flag`.
*/
class AtomicMutex : public Mutex
{
public:
AtomicMutex();
/**
* @brief Implementation of `Mutex` interface using `std::atomic_flag`.
*/
class AtomicMutex : public Mutex
{
public:
AtomicMutex();
void acquire() override;
void release() override;
void acquire() override;
void release() override;
private:
std::atomic_flag lock_; /**< Atomic flag used as lock */
};
private:
std::atomic_flag lock_; /**< Atomic flag used as lock */
};
} // namespace sta
#endif // STA_ATOMIC_ENABLE
#endif // STA_ATOMIC_ENABLED
#endif // STA_ATOMIC_MUTEX_HPP
#endif // STA_CORE_ATOMIC_MUTEX_HPP

View File

@ -1,41 +1,48 @@
/**
* @file
* @brief Atomic signal implementation.
*
* Configuration:
* STA_ATOMIC_ENABLE: Enable module
* STA_STDLIB_HAS_ATOMIC: Enable module
*/
#ifndef STA_ATOMIC_SIGNAL_HPP
#define STA_ATOMIC_SIGNAL_HPP
#ifndef STA_CORE_ATOMIC_SIGNAL_HPP
#define STA_CORE_ATOMIC_SIGNAL_HPP
#include <sta/config.hpp>
#ifdef STA_ATOMIC_ENABLE
#ifdef STA_STDLIB_HAS_ATOMIC
# define STA_ATOMIC_ENABLED
#endif // STA_STDLIB_HAS_ATOMIC
#include <sta/intf/signal.hpp>
#if defined(STA_ATOMIC_ENABLED) || defined(DOXYGEN)
#include <sta/signal.hpp>
#include <atomic>
namespace sta
{
/**
* @brief Implementation of `Signal` interface using `std::atomic`.
*/
class AtomicSignal : public Signal
{
public:
AtomicSignal();
/**
* @brief Implementation of `Signal` interface using `std::atomic`.
*/
class AtomicSignal : public Signal
{
public:
AtomicSignal();
void notify() override;
bool peek() override;
bool test() override;
void wait() override;
void notify() override;
bool peek() override;
bool test() override;
void wait() override;
private:
std::atomic<bool> signal_; /**< Atomic bool used as signal */
};
private:
std::atomic<bool> signal_; /**< Atomic bool used as signal */
};
} // namespace sta
#endif // STA_ATOMIC_ENABLE
#endif // STA_ATOMIC_ENABLED
#endif // STA_ATOMIC_SIGNAL_HPP
#endif // STA_CORE_ATOMIC_SIGNAL_HPP

View File

@ -2,20 +2,15 @@
* @file
* @brief CAN controller driver interface.
*/
#ifndef STA_CAN_CONTROLLER_HPP
#define STA_CAN_CONTROLLER_HPP
#ifndef STA_CORE_CAN_CONTROLLER_HPP
#define STA_CORE_CAN_CONTROLLER_HPP
/**
* @defgroup can CAN
* @defgroup sta_core_can CAN
* @ingroup sta_core
* @brief CAN controller driver interface.
*/
/**
* @defgroup canAPI API
* @ingroup can
* @brief Public interface.
*/
#include <sta/can/filter.hpp>
#include <sta/can/headers.hpp>
@ -24,97 +19,97 @@
namespace sta
{
/**
* @brief CAN controller driver interface.
*
* @ingroup canAPI
*/
class CanController
{
public:
// RX/TX
//
/**
* @brief CAN controller driver interface.
*
* @ingroup sta_core_can
*/
class CanController
{
public:
// RX/TX
//
/**
* @brief Send frame to CAN controller for transmission.
*
* @param header CAN frame TX header
* @param payload CAN frame payload
* @return True on success
*/
virtual bool sendFrame(const CanTxHeader & header, const uint8_t * payload) = 0;
/**
* @brief Send frame to CAN controller for transmission.
*
* @param header CAN frame TX header
* @param payload CAN frame payload
* @return True on success
*/
virtual bool sendFrame(const CanTxHeader & header, const uint8_t * payload) = 0;
/**
* @brief Get received frame from the CAN controller.
*
* @param[in] fifo FIFO storing frame
* @param[out] header CAN frame RX header destination
* @param[out] payload CAN frame payload destination
* @return True on success
*/
virtual bool receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload) = 0;
/**
* @brief Get received frame from the CAN controller.
*
* @param[in] fifo FIFO storing frame
* @param[out] header CAN frame RX header destination
* @param[out] payload CAN frame payload destination
* @return True on success
*/
virtual bool receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload) = 0;
/**
* @brief Get RX FIFO flags.
*
* @return FIFO flags
*/
virtual uint32_t getRxFifoFlags() = 0;
/**
* @brief Get RX FIFO flags.
*
* @return FIFO flags
*/
virtual uint32_t getRxFifoFlags() = 0;
/**
* @brief Get list of RX FIFO indices with pending messages.
*/
virtual CanPendingRxFifos getPendingRxFifos() = 0;
/**
* @brief Get list of RX FIFO indices with pending messages.
*/
virtual CanPendingRxFifos getPendingRxFifos() = 0;
// RX filter
//
// RX filter
//
/**
* @brief Change filter settings.
*
* @param idx Filter index
* @param filter Filter configuration
* @param active Enable filter after applying settings
*/
virtual void configureFilter(uint8_t idx, const CanFilter & filter, bool active = false) = 0;
/**
* @brief Enable filter.
*
* @param idx Filter index
*/
virtual void enableFilter(uint8_t idx) = 0;
/**
* @brief Disable filter.
*
* @param idx Filter index
*/
virtual void disableFilter(uint8_t idx) = 0;
/**
* @brief Disable and clear all filters.
*/
virtual void clearFilters() = 0;
/**
* @brief Change filter settings.
*
* @param idx Filter index
* @param filter Filter configuration
* @param active Enable filter after applying settings
*/
virtual void configureFilter(uint8_t idx, const CanFilter & filter, bool active = false) = 0;
/**
* @brief Enable filter.
*
* @param idx Filter index
*/
virtual void enableFilter(uint8_t idx) = 0;
/**
* @brief Disable filter.
*
* @param idx Filter index
*/
virtual void disableFilter(uint8_t idx) = 0;
/**
* @brief Disable and clear all filters.
*/
virtual void clearFilters() = 0;
// Info
//
// Info
//
/**
* @brief Get number of available filters.
*/
virtual uint8_t maxFilterCount() const = 0;
/**
* @brief Get number of available filters.
*/
virtual uint8_t maxFilterCount() const = 0;
/**
* @brief Get number of available FIFOs.
*/
virtual uint8_t maxFifoCount() const = 0;
/**
* @brief Get number of available FIFOs.
*/
virtual uint8_t maxFifoCount() const = 0;
/**
* @brief Get maximum supported payload size.
*/
virtual uint8_t maxPayloadSize() const = 0;
};
/**
* @brief Get maximum supported payload size.
*/
virtual uint8_t maxPayloadSize() const = 0;
};
} // namespace sta
#endif // STA_CAN_CONTROLLER_HPP
#endif // STA_CORE_CAN_CONTROLLER_HPP

View File

@ -2,8 +2,8 @@
* @file
* @brief CAN message filter types.
*/
#ifndef STA_CAN_FILTER_HPP
#define STA_CAN_FILTER_HPP
#ifndef STA_CORE_CAN_FILTER_HPP
#define STA_CORE_CAN_FILTER_HPP
#include <sta/can/id.hpp>
@ -12,38 +12,38 @@
namespace sta
{
/**
* @defgroup canFilter Filters
* @ingroup canAPI
* @brief CAN message filter types.
*/
/**
* @defgroup sta_core_can_filters Filters
* @ingroup sta_core_can
* @brief CAN message filter types.
* @{
*/
/**
* @brief ID format matched by CAN filter.
*
* @ingroup canFilter
*/
enum class CanFilterIdFormat
{
ANY, /**< Match both ID formats */
STD, /**< Match standard format IDs */
EXT /**< Match extended format IDs */
};
/**
* @brief ID format matched by CAN filter.
*/
enum class CanFilterIdFormat
{
ANY, /**< Match both ID formats */
STD, /**< Match standard format IDs */
EXT /**< Match extended format IDs */
};
/**
* @brief CAN filter settings.
*
* @ingroup canFilter
*/
struct CanFilter
{
CanId obj; /**< ID object */
CanId mask; /**< ID mask */
CanFilterIdFormat type; /**< ID format to match */
uint8_t fifo; /**< FIFO to store matches */
};
/**
* @brief CAN filter settings.
*/
struct CanFilter
{
CanId obj; /**< ID object */
CanId mask; /**< ID mask */
CanFilterIdFormat type; /**< ID format to match */
uint8_t fifo; /**< FIFO to store matches */
};
/** @} */
} // namespace sta
#endif // STA_CAN_FILTER_HPP
#endif // STA_CORE_CAN_FILTER_HPP

View File

@ -2,8 +2,8 @@
* @file
* @brief CAN frame headers.
*/
#ifndef STA_CAN_HEADERS_HPP
#define STA_CAN_HEADERS_HPP
#ifndef STA_CORE_CAN_HEADERS_HPP
#define STA_CORE_CAN_HEADERS_HPP
#include <sta/can/id.hpp>
@ -12,37 +12,37 @@
namespace sta
{
/**
* @defgroup canHeader Frame headers
* @ingroup canAPI
* @brief CAN header types for transmitted / received frames.
*/
/**
* @defgroup sta_core_can_headers Frame headers
* @ingroup sta_core_can
* @brief CAN header types for transmitted / received frames.
* @{
*/
/**
* @brief CAN TX frame header.
*
* @ingroup canHeader
*/
struct CanTxHeader
{
CanFrameId id; /**< Frame ID */
uint8_t payloadLength; /**< Size of data to send */
};
/**
* @brief CAN TX frame header.
*/
struct CanTxHeader
{
CanFrameId id; /**< Frame ID */
uint8_t payloadLength; /**< Size of data to send */
};
/**
* @brief CAN RX frame header.
*
* @ingroup canHeader
*/
struct CanRxHeader
{
CanFrameId id; /**< Frame ID */
uint8_t payloadLength; /**< Size of received data */
uint32_t timestamp; /**< RX timestamp */
uint8_t filter; /**< RX filter match */
};
/**
* @brief CAN RX frame header.
*/
struct CanRxHeader
{
CanFrameId id; /**< Frame ID */
uint8_t payloadLength; /**< Size of received data */
uint32_t timestamp; /**< RX timestamp */
uint8_t filter; /**< RX filter match */
};
/** @} */
} // namespace sta
#endif // STA_CAN_HEADERS_HPP
#endif // STA_CORE_CAN_HEADERS_HPP

View File

@ -2,117 +2,109 @@
* @file
* @brief CAN frame ID types.
*/
#ifndef STA_CAN_ID_HPP
#define STA_CAN_ID_HPP
#ifndef STA_CORE_CAN_ID_HPP
#define STA_CORE_CAN_ID_HPP
#include <cstdint>
namespace sta
{
/**
* @defgroup canID Frame IDs
* @ingroup canAPI
* @brief Types for working with CAN ID values and formats.
*/
/**
* @defgroup sta_core_can_ids Frame IDs
* @ingroup sta_core_can
* @brief Types for working with CAN ID values and formats.
*/
/**
* @brief CAN frame ID format.
*
* @ingroup canID
*/
enum class CanIdFormat : uint8_t
{
STD, /**< Standard format */
EXT /**< Extended format */
};
/**
* @brief CAN frame ID format.
*
* @ingroup sta_core_can_ids
*/
enum class CanIdFormat : uint8_t
{
STD, /**< Standard format */
EXT /**< Extended format */
};
/**
* @brief CAN frame ID.
*
* @ingroup canID
*/
struct CanId
{
uint32_t sid; /**< Standard ID field (11 bits) */
uint32_t eid; /**< Extended ID field (18 bits) */
};
/**
* @brief CAN frame ID.
*
* @ingroup sta_core_can_ids
*/
struct CanId
{
uint32_t sid; /**< Standard ID field (11 bits) */
uint32_t eid; /**< Extended ID field (18 bits) */
};
/**
* @brief CAN frame ID and format.
*
* @ingroup canID
*/
struct CanFrameId
{
CanIdFormat format; /**< ID format */
uint32_t sid; /**< Standard ID field (11 bits) */
uint32_t eid; /**< Extended ID field (18 bits) */
};
/**
* @brief CAN frame ID and format.
*
* @ingroup sta_core_can_ids
*/
struct CanFrameId
{
CanIdFormat format; /**< ID format */
uint32_t sid; /**< Standard ID field (11 bits) */
uint32_t eid; /**< Extended ID field (18 bits) */
};
// Comparison operators
//
// Comparison operators
//
/**
* @brief Equal to operator.
*
* @param lhs Left hand side CAN ID
* @param rhs Right hand side CAN ID
* @return True if CAN IDs are equal
*
* @ingroup canID
*/
bool operator ==(const CanId & lhs, const CanId & rhs);
/**
* @brief Not equal to operator.
*
* @param lhs Left hand side CAN ID
* @param rhs Right hand side CAN ID
* @return True if CAN IDs are not equal
*
* @ingroup canID
*/
bool operator !=(const CanId & lhs, const CanId & rhs);
/**
* @brief Equal to operator.
*
* @param lhs Left hand side CAN ID
* @param rhs Right hand side CAN ID
* @return True if CAN IDs are equal
*/
bool operator ==(const CanId & lhs, const CanId & rhs);
/**
* @brief Not equal to operator.
*
* @param lhs Left hand side CAN ID
* @param rhs Right hand side CAN ID
* @return True if CAN IDs are not equal
*/
bool operator !=(const CanId & lhs, const CanId & rhs);
/**
* @brief Equal to operator.
*
* @param lhs Left hand side CAN Frame ID
* @param rhs Right hand side CAN Frame ID
* @return True if CAN Frame IDs are equal
*
* @ingroup canID
*/
bool operator ==(const CanFrameId & lhs, const CanFrameId & rhs);
/**
* @brief Not equal to operator.
*
* @param lhs Left hand side CAN Frame ID
* @param rhs Right hand side CAN Frame ID
* @return True if CAN Frame IDs are not equal
*
* @ingroup canID
*/
bool operator !=(const CanFrameId & lhs, const CanFrameId & rhs);
/**
* @brief Equal to operator.
*
* @param lhs Left hand side CAN Frame ID
* @param rhs Right hand side CAN Frame ID
* @return True if CAN Frame IDs are equal
*/
bool operator ==(const CanFrameId & lhs, const CanFrameId & rhs);
/**
* @brief Not equal to operator.
*
* @param lhs Left hand side CAN Frame ID
* @param rhs Right hand side CAN Frame ID
* @return True if CAN Frame IDs are not equal
*/
bool operator !=(const CanFrameId & lhs, const CanFrameId & rhs);
} // namespace sta
/**
* @brief Maximum CAN standard ID value.
*
* @ingroup canID
* @ingroup sta_core_can_ids
*/
#define CAN_SID_MAX UINT32_C(0x7FF)
/**
* @brief Maximum CAN extended ID value.
*
* @ingroup canID
* @ingroup sta_core_can_ids
*/
#define CAN_EID_MAX UINT32_C(0x3FFFF)
#endif // STA_CAN_ID_HPP
#endif // STA_CORE_CAN_ID_HPP

View File

@ -1,63 +1,82 @@
#ifndef STA_CAN_ITER_HPP
#define STA_CAN_ITER_HPP
/**
* @file
* @brief Custom iterators for CAN controllers.
*/
#ifndef STA_CORE_CAN_ITER_HPP
#define STA_CORE_CAN_ITER_HPP
#include <cstdint>
namespace sta
{
class CanPendingRxFifos
{
public:
using value_type = uint8_t;
using reference = value_type &;
using const_reference = const value_type &;
using size_type = uint8_t;
/**
* @brief Iterable container interface for CAN RX flags.
*
* @ingroup sta_core_can_ids
*/
class CanPendingRxFifos
{
public:
using value_type = uint8_t;
using reference = value_type &;
using const_reference = const value_type &;
using size_type = uint8_t;
class const_iterator
{
public:
using value_type = CanPendingRxFifos::value_type;
using reference = const_reference;
using pointer = const value_type *;
/**
* @brief Custom iterator for active RX queues.
*/
class const_iterator
{
public:
using value_type = CanPendingRxFifos::value_type;
using reference = const_reference;
using pointer = const value_type *;
public:
const_iterator(const const_iterator & iter);
public:
const_iterator(const const_iterator & iter);
const_iterator & operator=(const const_iterator & iter);
const_iterator & operator=(const const_iterator & iter);
bool operator==(const const_iterator & iter) const;
bool operator!=(const const_iterator & iter) const;
bool operator==(const const_iterator & iter) const;
bool operator!=(const const_iterator & iter) const;
const_iterator & operator++();
const_iterator operator++(int);
const_iterator & operator++();
const_iterator operator++(int);
reference operator*() const;
reference operator*() const;
private:
const_iterator(uint32_t rxFlags, uint8_t idx, uint8_t endIdx);
private:
const_iterator(uint32_t rxFlags, uint8_t idx, uint8_t endIdx);
bool isRxPending() const;
/**
* @brief Check if current RX queue has pending data.
*/
bool isRxPending() const;
friend class CanPendingRxFifos;
friend class CanPendingRxFifos;
private:
uint32_t rxFlags_;
uint8_t idx_;
uint8_t endIdx_;
};
private:
uint32_t rxFlags_; /**< RX flag bits */
uint8_t idx_; /**< Current flag index */
uint8_t endIdx_; /**< Iterator end index */
};
public:
CanPendingRxFifos(uint32_t rxFlags, uint8_t numFifos);
public:
/**
* @param rxFlags RX flag bits
* @param numFifos Number of RX FIFOs
*/
CanPendingRxFifos(uint32_t rxFlags, uint8_t numFifos);
const_iterator begin() const;
const_iterator end() const;
const_iterator begin() const;
const_iterator end() const;
private:
uint32_t rxFlags_;
uint8_t numFifos_;
};
private:
uint32_t rxFlags_; /**< RX flag bits */
uint8_t numFifos_; /**< Number of RX FIFOs */
};
} // namespace sta
#endif // STA_CAN_ITER_HPP
#endif // STA_CORE_CAN_ITER_HPP

View File

@ -2,8 +2,8 @@
* @file
* @brief Subscription interface for CAN controller drivers.
*/
#ifndef STA_CAN_SUBSCRIBABLE_HPP
#define STA_CAN_SUBSCRIBABLE_HPP
#ifndef STA_CORE_CAN_SUBSCRIBABLE_HPP
#define STA_CORE_CAN_SUBSCRIBABLE_HPP
#include <sta/can/filter.hpp>
#include <sta/can/headers.hpp>
@ -11,94 +11,92 @@
namespace sta
{
/**
* @defgroup canSub Subscription
* @ingroup canAPI
* @brief Subscription interface for CAN controller drivers.
*/
/**
* @defgroup sta_core_can_sub Subscription
* @ingroup sta_core_can
* @brief Subscription interface for CAN controller drivers.
* @{
*/
/**
* @brief Callback for handling received frames.
*
* @param header Frame header
* @param buffer Frame payload buffer
*
* @ingroup canSub
*/
using CanRxCallback = void (*) (const CanRxHeader & header, const uint8_t * buffer);
/**
* @brief Callback for handling received frames.
*
* @param header Frame header
* @param buffer Frame payload buffer
*/
using CanRxCallback = void (*) (const CanRxHeader & header, const uint8_t * buffer);
/**
* @brief Filter configuration and message handler.
*
* @ingroup canSub
*/
struct CanFilterConfig
{
CanFilter filter; /**< Filter handled by callback */
CanRxCallback callback; /**< Callback for message handling */
};
/**
* @brief Filter configuration and message handler.
*/
struct CanFilterConfig
{
CanFilter filter; /**< Filter handled by callback */
CanRxCallback callback; /**< Callback for message handling */
};
/**
* @brief CAN controller with subscriptions.
*
* @tparam T Implementation of CanController interface
*
* @ingroup canSub
*/
template <typename T>
class SubscribableCanController : public T
{
public:
using T::T;
/**
* @brief CAN controller with subscriptions.
*
* @tparam T Implementation of CanController interface
*/
template <typename T>
class SubscribableCanController : public T
{
public:
using T::T;
// Subscriptions
//
// Subscriptions
//
/**
* @brief Subscribe to specific message types.
*
* @param subscriptions Array of message filters and handlers
* @param num Number of array entries
*/
bool subscribe(const CanFilterConfig * subscriptions, uint8_t num);
/**
* @brief Subscribe to specific message types.
*
* @param subscriptions Array of message filters and handlers
* @param num Number of array entries
*/
bool subscribe(const CanFilterConfig * subscriptions, uint8_t num);
/**
* @brief Subscribe to all messages.
*
* @param callback Called when message is received
* @param fifo FIFO used for received messages
*/
void subscribeAll(CanRxCallback callback, uint8_t fifo);
/**
* @brief Subscribe to all messages.
*
* @param callback Called when message is received
* @param fifo FIFO used for received messages
*/
void subscribeAll(CanRxCallback callback, uint8_t fifo);
/**
* @brief Unsubscribe from all messages.
*
* No more messages will be received.
*/
void unsubscribeAll();
/**
* @brief Unsubscribe from all messages.
*
* No more messages will be received.
*/
void unsubscribeAll();
/**
* @brief Read message from RX FIFO and notify subscriber.
*/
void receiveAndNotify(uint8_t fifo);
/**
* @brief Read message from RX FIFO and notify subscriber.
*/
void receiveAndNotify(uint8_t fifo);
/**
* @brief Process pending frames from RX FIFOs.
*/
void processMessages();
/**
* @brief Process pending frames from RX FIFOs.
*/
void processMessages();
private:
CanRxCallback filterCallbacks_[T::MAX_FILTER_COUNT]; /**< Callbacks for RX filters */
};
private:
CanRxCallback filterCallbacks_[T::MAX_FILTER_COUNT]; /**< Callbacks for RX filters */
};
/** @} */
} // namespace sta
#include <sta/can/subscribable.tpp>
#endif // STA_CAN_SUBSCRIBABLE_HPP
#endif // STA_CORE_CAN_SUBSCRIBABLE_HPP

View File

@ -1,12 +1,12 @@
/**
* @brief Implementation of template class CanController<T>.
*/
#ifndef STA_CAN_SUBSCRIBABLE_TPP
#define STA_CAN_SUBSCRIBABLE_TPP
#ifndef STA_CORE_CAN_SUBSCRIBABLE_TPP
#define STA_CORE_CAN_SUBSCRIBABLE_TPP
#ifndef STA_CAN_SUBSCRIBABLE_HPP
#error "Direct use of internal header. Use <sta/intf/can/subscribable.hpp> instead"
#endif // !STA_CAN_SUBSCRIBABLE_HPP
#ifndef STA_CORE_CAN_SUBSCRIBABLE_HPP
#error "Direct use of internal header. Use <sta/can/subscribable.hpp> instead"
#endif // !STA_CORE_CAN_SUBSCRIBABLE_HPP
#ifndef STA_STDLIB_DISABLE
# include <algorithm> // fill_n
@ -15,106 +15,106 @@
namespace sta
{
template <typename T>
bool SubscribableCanController<T>::subscribe(const CanFilterConfig * subscriptions, uint8_t num)
{
// Check bounds
if (num > T::MAX_FILTER_COUNT)
return false;
template <typename T>
bool SubscribableCanController<T>::subscribe(const CanFilterConfig * subscriptions, uint8_t num)
{
// Check bounds
if (num > T::MAX_FILTER_COUNT)
return false;
// Clear previous subscriptions
unsubscribeAll();
// Clear previous subscriptions
unsubscribeAll();
for (uint8_t i = 0; i < num; ++i)
{
// Save handler callback
filterCallbacks_[i] = subscriptions[i].callback;
for (uint8_t i = 0; i < num; ++i)
{
// Save handler callback
filterCallbacks_[i] = subscriptions[i].callback;
// Configure and enable filter
T::configureFilter(i, subscriptions[i].filter, true);
}
// Configure and enable filter
T::configureFilter(i, subscriptions[i].filter, true);
}
return true;
}
return true;
}
template <typename T>
void SubscribableCanController<T>::subscribeAll(CanRxCallback callback, uint8_t fifo)
{
uint8_t filterIdx = 0;
template <typename T>
void SubscribableCanController<T>::subscribeAll(CanRxCallback callback, uint8_t fifo)
{
uint8_t filterIdx = 0;
// Clear previous subscriptions
unsubscribeAll();
// Clear previous subscriptions
unsubscribeAll();
// Setup default filter
CanFilter filter{};
filter.type = CanFilterIdFormat::ANY;
filter.fifo = fifo;
// Setup default filter
CanFilter filter{};
filter.type = CanFilterIdFormat::ANY;
filter.fifo = fifo;
// Store callback
filterCallbacks_[filterIdx] = callback;
// Store callback
filterCallbacks_[filterIdx] = callback;
// Configure and enable default filter
T::configureFilter(filterIdx, filter, true);
}
// Configure and enable default filter
T::configureFilter(filterIdx, filter, true);
}
template <typename T>
void SubscribableCanController<T>::unsubscribeAll()
{
// Disable all filters
T::clearFilters();
template <typename T>
void SubscribableCanController<T>::unsubscribeAll()
{
// Disable all filters
T::clearFilters();
// Clear filter callbacks
// Clear filter callbacks
#ifndef STA_STDLIB_DISABLE
std::fill_n(filterCallbacks_, T::MAX_FILTER_COUNT, nullptr);
std::fill_n(filterCallbacks_, T::MAX_FILTER_COUNT, nullptr);
#else // STA_STDLIB_DISABLE
for (uint8_t i = 0; i < T::MAX_FILTER_COUNT; ++i)
{
filterCallbacks_[i] = nullptr;
}
for (uint8_t i = 0; i < T::MAX_FILTER_COUNT; ++i)
{
filterCallbacks_[i] = nullptr;
}
#endif // STA_STDLIB_DISABLE
}
}
template <typename T>
void SubscribableCanController<T>::receiveAndNotify(uint8_t fifo)
{
CanRxHeader header;
uint8_t payload[T::MAX_PAYLOAD_SIZE];
template <typename T>
void SubscribableCanController<T>::receiveAndNotify(uint8_t fifo)
{
CanRxHeader header;
uint8_t payload[T::MAX_PAYLOAD_SIZE];
if (T::receiveFrame(fifo, &header, payload))
{
//displayFrameUART(frame);
if (T::receiveFrame(fifo, &header, payload))
{
//displayFrameUART(frame);
// Forward frame to filter callback
if (fifo <= T::MAX_FILTER_COUNT && filterCallbacks_[header.filter])
{
filterCallbacks_[header.filter](header, payload);
}
}
}
// Forward frame to filter callback
if (fifo <= T::MAX_FILTER_COUNT && filterCallbacks_[header.filter])
{
filterCallbacks_[header.filter](header, payload);
}
}
}
template <typename T>
void SubscribableCanController<T>::processMessages()
{
// Read RX interrupt flags
uint32_t RFIF = T::getRxFifoFlags();
template <typename T>
void SubscribableCanController<T>::processMessages()
{
// Read RX interrupt flags
uint32_t RFIF = T::getRxFifoFlags();
if (RFIF != 0)
{
// Check all flags
for (uint8_t fifo = 0; fifo < T::MAX_FIFO_COUNT; ++fifo)
{
// Process messages if flag is set
if (RFIF & 0x1)
{
receiveAndNotify(fifo);
}
if (RFIF != 0)
{
// Check all flags
for (uint8_t fifo = 0; fifo < T::MAX_FIFO_COUNT; ++fifo)
{
// Process messages if flag is set
if (RFIF & 0x1)
{
receiveAndNotify(fifo);
}
// Shift next RX flag to LSB
RFIF >>= 1;
}
}
}
// Shift next RX flag to LSB
RFIF >>= 1;
}
}
}
} // namespace sta
#endif // STA_CAN_SUBSCRIBABLE_TPP
#endif // STA_CORE_CAN_SUBSCRIBABLE_TPP

View File

@ -3,83 +3,54 @@
* @brief Debug output via UART.
*
* Configuration:
* STA_DEBUG_SERIAL_ENABLE: Enable module
* STA_DEBUG_SERIAL_FORCE: Ignore debug defines and always enable output
* DEBUG: Enables output when defined
* NDEBUG: Disables output when defined (overrides DEBUG)
*
* The `sta::DebugSerial` instance must be provided.
* NOTE: Include this header before the definition because
* the default internal linkage of const namespace variables
* will cause undefined reference errors otherwise.
* * STA_DEBUG_SERIAL_UART: UART interface for output
* * STA_DEBUG_SERIAL_FORCE: Ignore debug defines and always enable output
* * DEBUG: Enables output when defined
* * NDEBUG: Disables output when defined (overrides DEBUG)
*/
#ifndef STA_DEBUG_SERIAL_HPP
#define STA_DEBUG_SERIAL_HPP
/**
* @defgroup staCoreDebug Debug Serial
* @ingroup staCore
* @brief Debug serial output.
*/
#ifdef DOXYGEN
/**
* @def STA_DEBUG_SERIAL_ENABLE
* @brief Enable module.
*
* Automatically defined if DEBUG is defined.
* Automatically disabled if NDEBUG is defined.
*
* @ingroup staCoreBuildConfig
*/
# define STA_DEBUG_SERIAL_ENABLE
/**
* @def STA_DEBUG_SERIAL_DISABLE
* @brief Force disable module.
*
* Overrides STA_DEBUG_SERIAL_ENABLE option.
*
* @ingroup staCoreBuildConfig
*/
# define STA_DEBUG_SERIAL_DISABLE
#endif // DOXYGEN
#ifndef STA_CORE_DEBUG_SERIAL_HPP
#define STA_CORE_DEBUG_SERIAL_HPP
#include <sta/config.hpp>
#ifdef DEBUG
# ifndef STA_DEBUG_SERIAL_ENABLE
# define STA_DEBUG_SERIAL_ENABLE
# endif // !STA_DEBUG_SERIAL_ENABLE
#endif // DEBUG
#if defined(NDEBUG) || defined(STA_DEBUG_SERIAL_DISABLE)
# ifdef STA_DEBUG_SERIAL_ENABLE
# undef STA_DEBUG_SERIAL_ENABLE
# endif // STA_DEBUG_SERIAL_ENABLE
#endif // NDEBUG || STA_DEBUG_SERIAL_DISABLE
// Determine if module should be enabled
// Condition 1: STA_DEBUG_SERIAL_UART is defined
// Condition 2:
// STA_DEBUG_SERIAL_FORCE is defined
// or
// DEBUG is defined but not NDEBUG
#ifdef STA_DEBUG_SERIAL_UART
# ifdef STA_DEBUG_SERIAL_FORCE
# define STA_DEBUG_SERIAL_ENABLED
# else // !STA_DEBUG_SERIAL_FORCE
# if defined(DEBUG) && !defined(NDEBUG)
# define STA_DEBUG_SERIAL_ENABLED
# endif // DEBUG && !NDEBUG
# endif // !STA_DEBUG_SERIAL_FORCE
#endif // STA_DEBUG_SERIAL_UART
// Show enabled module in doxygen output
#ifdef DOXYGEN
# define STA_DEBUG_SERIAL_ENABLE
#endif // DOXYGEN
#ifdef STA_DEBUG_SERIAL_ENABLE
#if defined(STA_DEBUG_SERIAL_ENABLED) || defined(DOXYGEN)
#include <sta/printable_uart.hpp>
/**
* @defgroup sta_core_debug Debug Serial
* @ingroup sta_core
* @brief Debug serial output.
*/
namespace sta
{
/**
* @brief %UART print object for debug serial output.
*
* @ingroup staCoreDebug
*/
extern PrintableUART DebugSerial;
/**
* @brief %UART print object for debug serial output.
*
* @ingroup sta_core_debug
*/
extern PrintableUART DebugSerial;
} // namespace sta
@ -88,7 +59,7 @@ namespace sta
*
* @param ... See @ref sta::PrintableUART::print
*
* @ingroup staCoreDebug
* @ingroup sta_core_debug
*/
# define STA_DEBUG_PRINT(...) sta::DebugSerial.print(__VA_ARGS__)
/**
@ -96,13 +67,13 @@ namespace sta
*
* @param ... See @ref sta::PrintableUART::println
*
* @ingroup staCoreDebug
* @ingroup sta_core_debug
*/
# define STA_DEBUG_PRINTLN(...) sta::DebugSerial.println(__VA_ARGS__)
#else // !STA_DEBUG_SERIAL_ENABLE
#else // !STA_DEBUG_SERIAL_ENABLED
# define STA_DEBUG_PRINT(...) ((void)0)
# define STA_DEBUG_PRINTLN(...) ((void)0)
#endif // !STA_DEBUG_SERIAL_ENABLE
#endif // !STA_DEBUG_SERIAL_ENABLED
#endif // STA_DEBUG_SERIAL_HPP
#endif // STA_CORE_DEBUG_SERIAL_HPP

View File

@ -2,14 +2,9 @@
* @file
* @brief Helper macros for managing endian handling.
*/
#ifndef STA_ENDIAN_HPP
#define STA_ENDIAN_HPP
#ifndef STA_CORE_ENDIAN_HPP
#define STA_CORE_ENDIAN_HPP
/**
* @defgroup staCoreEndian Endian
* @ingroup staCore
* @brief Endian handling.
*/
#include <sta/config.hpp>
@ -18,99 +13,95 @@
#endif // !STA_MCU_BIG_ENDIAN && !STA_MCU_LITTLE_ENDIAN
/**
* @defgroup sta_core_endian Endian
* @ingroup sta_core
* @brief Endian handling.
* @{
*/
/**
* @brief Get 16-bit value with swapped byte order.
*
* @param u16 16-bit input value
* @return 16-bit value w/ swapped byte order
*
* @ingroup staCoreEndian
*/
#define STA_UINT16_SWAP_BYTE_ORDER(u16) \
( \
((u16 & 0x00FF) << 8) \
| ((u16 & 0xFF00) >> 8) \
)
( \
((u16 & 0x00FF) << 8) \
| ((u16 & 0xFF00) >> 8) \
)
/**
* @brief Get 32-bit value with swapped byte order.
*
* @param u32 32-bit input value
* @return 32-bit value w/ swapped byte order
*
* @ingroup staCoreEndian
*/
#define STA_UINT32_SWAP_BYTE_ORDER(u32) \
( \
((u32 & 0x000000FF) << 24) \
| ((u32 & 0x0000FF00) << 8) \
| ((u32 & 0x00FF0000) >> 8) \
| ((u32 & 0xFF000000) >> 24) \
)
( \
((u32 & 0x000000FF) << 24) \
| ((u32 & 0x0000FF00) << 8) \
| ((u32 & 0x00FF0000) >> 8) \
| ((u32 & 0xFF000000) >> 24) \
)
/**
* @brief Get initializer list for byte array with big-endian byte order from 16-bit value.
*
* @param u16 16-bit input value
* @return Initializer list for uint8_t[2]
*
* @ingroup staCoreEndian
*/
#define STA_UINT16_TO_BYTES_BE(u16) \
{ \
static_cast<uint8_t>(u16 >> 8), \
static_cast<uint8_t>(u16) \
}
{ \
static_cast<uint8_t>(u16 >> 8), \
static_cast<uint8_t>(u16) \
}
/**
* @brief Get initializer list for byte array with little-endian byte order from 16-bit value.
*
* @param u16 16-bit input value
* @return Initializer list for uint8_t[2]
*
* @ingroup staCoreEndian
*/
#define STA_UINT16_TO_BYTES_LE(u16) \
{ \
static_cast<uint8_t>(u16), \
static_cast<uint8_t>(u16 >> 8) \
}
{ \
static_cast<uint8_t>(u16), \
static_cast<uint8_t>(u16 >> 8) \
}
/**
* @brief Get initializer list for byte array with big-endian byte order from 32-bit value.
*
* @param u32 32-bit input value
* @return Initializer list for uint8_t[4]
*
* @ingroup staCoreEndian
*/
#define STA_UINT32_TO_BYTES_BE(u32) \
{ \
static_cast<uint8_t>(u32 >> 24), \
static_cast<uint8_t>(u32 >> 16), \
static_cast<uint8_t>(u32 >> 8), \
static_cast<uint8_t>(u32) \
}
{ \
static_cast<uint8_t>(u32 >> 24), \
static_cast<uint8_t>(u32 >> 16), \
static_cast<uint8_t>(u32 >> 8), \
static_cast<uint8_t>(u32) \
}
/**
* @brief Get initializer list for byte array with little-endian byte order from 32-bit value.
*
* @param u32 32-bit input value
* @return Initializer list for uint8_t[4]
*
* @ingroup staCoreEndian
*/
#define STA_UINT32_TO_BYTES_LE(u32) \
{ \
static_cast<uint8_t>(u32), \
static_cast<uint8_t>(u32 >> 8), \
static_cast<uint8_t>(u32 >> 16), \
static_cast<uint8_t>(u32 >> 24) \
}
{ \
static_cast<uint8_t>(u32), \
static_cast<uint8_t>(u32 >> 8), \
static_cast<uint8_t>(u32 >> 16), \
static_cast<uint8_t>(u32 >> 24) \
}
/**
* @defe STA_UINT16_TO_BE(u16)
* @def STA_UINT16_TO_BE(u16)
* @brief Convert 16-bit value to big-endian byte order.
*
* @param u16 16-bit input value
@ -174,6 +165,9 @@
*/
/** @} */
#ifdef STA_MCU_LITTLE_ENDIAN
# define STA_UINT16_TO_BE(u16) STA_UINT16_SWAP_BYTE_ORDER(u16)
@ -186,7 +180,7 @@
# define STA_UINT32_TO_BYTES(u32) STA_UINT32_TO_BYTES_LE(u32)
# define STA_UINT32_TO_BYTES_SWAP(u32) STA_UINT32_TO_BYTES_BE(u32)
#elif STA_MCU_BIG_ENDIAN
#else // STA_MCU_BIG_ENDIAN
# define STA_UINT16_TO_BE(u16) (u16)
# define STA_UINT16_TO_LE(u16) STA_UINT16_SWAP_BYTE_ORDER(u16)
@ -201,4 +195,4 @@
#endif // STA_MCU_BIG_ENDIAN
#endif // STA_ENDIAN_HPP
#endif // STA_CORE_ENDIAN_HPP

View File

@ -2,151 +2,151 @@
* @file
* @brief Helper for using enum values as flags.
*/
#ifndef STA_ENUM_FLAGS_HPP
#define STA_ENUM_FLAGS_HPP
#ifndef STA_CORE_ENUM_FLAGS_HPP
#define STA_CORE_ENUM_FLAGS_HPP
#include <cstdint>
namespace sta
{
/**
* @brief 32-bit flag register with enum type representing single flag bits.
*
* @tparam F Enum type used for flag bits
*
* @ingroup staCore
*/
template <typename F>
class EnumFlags
{
public:
using flag_type = F; /**< Enum type used for flag bits */
/**
* @brief 32-bit flag register with enum type representing single flag bits.
*
* @tparam F Enum type used for flag bits
*
* @ingroup sta_core
*/
template <typename F>
class EnumFlags
{
public:
using flag_type = F; /**< Enum type used for flag bits */
public:
/**
* @brief Default constructor.
*/
EnumFlags();
/**
* @brief Copy constructor.
*
* @param other Flags to copy
*/
EnumFlags(const EnumFlags & other);
/**
* @brief Construct from single flag.
*
* @param flag Single flag bit to set
*/
EnumFlags(flag_type flag);
public:
/**
* @brief Default constructor.
*/
EnumFlags();
/**
* @brief Copy constructor.
*
* @param other Flags to copy
*/
EnumFlags(const EnumFlags & other);
/**
* @brief Construct from single flag.
*
* @param flag Single flag bit to set
*/
EnumFlags(flag_type flag);
// Modification
//
// Modification
//
/**
* @brief Set bits from flags register.
*
* @param flags Flag bits to set
*/
void set(const EnumFlags & flags);
/**
* @brief Clear flag bits.
*
* @param flags Flag bits to clear
*/
void clear(const EnumFlags & flags);
/**
* @brief Clear all flag bits.
*/
void clear();
/**
* @brief Set bits from flags register.
*
* @param flags Flag bits to set
*/
void set(const EnumFlags & flags);
/**
* @brief Clear flag bits.
*
* @param flags Flag bits to clear
*/
void clear(const EnumFlags & flags);
/**
* @brief Clear all flag bits.
*/
void clear();
// Inspection
//
// Inspection
//
/**
* @brief Test if all flags are set.
*
* @param flags Flag bits to check
* @return True if all checked flag bits are set
*/
bool isSet(const EnumFlags & flags) const;
/**
* @brief Test if all flags are set.
*
* @param flags Flag bits to check
* @return True if all checked flag bits are set
*/
bool isSet(const EnumFlags & flags) const;
/**
* @brief Test if any flag is set.
*
* @param flags Flag bits to check
* @return True if any checked flag bit is set
*/
bool isAnySet(const EnumFlags & flags) const;
/**
* @brief Test if any flag is set.
*
* @param flags Flag bits to check
* @return True if any checked flag bit is set
*/
bool isAnySet(const EnumFlags & flags) const;
// Operator overloads
//
// Operator overloads
//
/**
* @brief Equal to operator.
*
* @param rhs Flags to compare
* @return True if flags are equal
*/
bool operator ==(const EnumFlags & rhs) const;
/**
* @brief Not equal to operator.
*
* @param rhs Flags to compare
* @return True if flags are not equal
*/
bool operator !=(const EnumFlags & rhs) const;
/**
* @brief Equal to operator.
*
* @param rhs Flags to compare
* @return True if flags are equal
*/
bool operator ==(const EnumFlags & rhs) const;
/**
* @brief Not equal to operator.
*
* @param rhs Flags to compare
* @return True if flags are not equal
*/
bool operator !=(const EnumFlags & rhs) const;
/**
* @brief Bitwise AND operator.
*
* @param rhs Other flags
* @return Result of bitwise AND
*/
EnumFlags operator &(const EnumFlags & rhs) const;
/**
* @brief Bitwise OR operator.
*
* @param rhs Other flags
* @return Result of bitwise OR
*/
EnumFlags operator |(const EnumFlags & rhs) const;
/**
* @brief Bitwise AND operator.
*
* @param rhs Other flags
* @return Result of bitwise AND
*/
EnumFlags operator &(const EnumFlags & rhs) const;
/**
* @brief Bitwise OR operator.
*
* @param rhs Other flags
* @return Result of bitwise OR
*/
EnumFlags operator |(const EnumFlags & rhs) const;
/**
* @brief Bitwise AND assignment operator.
*
* @param rhs Other flags
* @return Reference to this
*/
EnumFlags & operator &=(const EnumFlags & rhs);
/**
* @brief Bitwise OR assignment operator.
*
* @param rhs Other flags
* @return Reference to this
*/
EnumFlags & operator |=(const EnumFlags & rhs);
/**
* @brief Bitwise AND assignment operator.
*
* @param rhs Other flags
* @return Reference to this
*/
EnumFlags & operator &=(const EnumFlags & rhs);
/**
* @brief Bitwise OR assignment operator.
*
* @param rhs Other flags
* @return Reference to this
*/
EnumFlags & operator |=(const EnumFlags & rhs);
/**
* @brief Explicitly convert flags to uint32_t.
*/
explicit operator uint32_t();
/**
* @brief Explicitly convert flags to uint32_t.
*/
explicit operator uint32_t();
private:
/**
* @brief Construct from uint32_t flags.
*
* @param flags Flags
*/
EnumFlags(uint32_t flags);
private:
/**
* @brief Construct from uint32_t flags.
*
* @param flags Flags
*/
EnumFlags(uint32_t flags);
private:
uint32_t flags_; /**< Flags register */
};
private:
uint32_t flags_; /**< Flags register */
};
} // namespace sta
@ -155,23 +155,23 @@ namespace sta
*
* @param enumType Enum type to overload
*
* @ingroup staCore
* @ingroup sta_core
*/
#define STA_ENUM_FLAGS_OVERLOAD(enumType) \
sta::EnumFlags<enumType> operator |(enumType lhs, enumType rhs) \
{ \
return sta::EnumFlags<enumType>{lhs} | rhs; \
}
sta::EnumFlags<enumType> operator |(enumType lhs, enumType rhs) \
{ \
return sta::EnumFlags<enumType>{lhs} | rhs; \
}
/**
* @brief Declare alias for sta::EnumFlags specialization.
*
* @param enumType Enum type for specialization
*
* @ingroup staCore
* @ingroup sta_core
*/
#define STA_ENUM_FLAGS_ALIAS(enumType) \
using enumType ## Flags = sta::EnumFlags<enumType>
using enumType ## Flags = sta::EnumFlags<enumType>
/**
* @brief Declare enum and create sta::EnumFlags alias and overloads.
@ -180,19 +180,19 @@ namespace sta
* @param value1 First enum value
* @param ... Enum values 2 - 32
*
* @ingroup staCore
* @ingroup sta_core
*/
#define STA_ENUM_FLAGS_DECL(enumType, value1, ...) \
enum class enumType \
{ \
value1, ##__VA_ARGS__ \
}; \
STA_ENUM_FLAGS_ALIAS(enumType); \
STA_ENUM_FLAGS_OVERLOAD(enumType)
enum class enumType \
{ \
value1, ##__VA_ARGS__ \
}; \
STA_ENUM_FLAGS_ALIAS(enumType); \
STA_ENUM_FLAGS_OVERLOAD(enumType)
// Include template implementation
#include <sta/enum_flags.tpp>
#endif // STA_ENUM_FLAGS_HPP
#endif // STA_CORE_ENUM_FLAGS_HPP

View File

@ -1,116 +1,116 @@
/**
* @brief Template class implementation for `EnumFlags<T>`.
*/
#ifndef STA_ENUM_FLAGS_TPP
#define STA_ENUM_FLAGS_TPP
#ifndef STA_CORE_ENUM_FLAGS_TPP
#define STA_CORE_ENUM_FLAGS_TPP
#ifndef STA_ENUM_FLAGS_HPP
#ifndef STA_CORE_ENUM_FLAGS_HPP
# error "Direct use of internal header. Use <sta/enum_flags.hpp> instead"
#endif // !STA_ENUM_FLAGS_HPP
#endif // !STA_CORE_ENUM_FLAGS_HPP
namespace sta
{
template <typename F>
EnumFlags<F>::EnumFlags()
: flags_{0}
{}
template <typename F>
EnumFlags<F>::EnumFlags()
: flags_{0}
{}
template <typename F>
EnumFlags<F>::EnumFlags(const EnumFlags & other)
: flags_{other.flags_}
{}
template <typename F>
EnumFlags<F>::EnumFlags(const EnumFlags & other)
: flags_{other.flags_}
{}
template <typename F>
EnumFlags<F>::EnumFlags(flag_type flag)
: flags_{1U << static_cast<uint8_t>(flag)}
{}
template <typename F>
EnumFlags<F>::EnumFlags(flag_type flag)
: flags_{1U << static_cast<uint8_t>(flag)}
{}
template <typename F>
EnumFlags<F>::EnumFlags(uint32_t flags)
: flags_{flags}
{}
template <typename F>
EnumFlags<F>::EnumFlags(uint32_t flags)
: flags_{flags}
{}
template <typename F>
void EnumFlags<F>::set(const EnumFlags & flags)
{
flags_ |= flags.flags_;
}
template <typename F>
void EnumFlags<F>::set(const EnumFlags & flags)
{
flags_ |= flags.flags_;
}
template <typename F>
void EnumFlags<F>::clear(const EnumFlags & flags)
{
flags_ &= ~(flags.flags_);
}
template <typename F>
void EnumFlags<F>::clear(const EnumFlags & flags)
{
flags_ &= ~(flags.flags_);
}
template <typename F>
void EnumFlags<F>::clear()
{
flags_ = 0;
}
template <typename F>
void EnumFlags<F>::clear()
{
flags_ = 0;
}
template <typename F>
bool EnumFlags<F>::isSet(const EnumFlags & flags) const
{
return (flags_ & flags.flags_) == flags.flags_;
}
template <typename F>
bool EnumFlags<F>::isSet(const EnumFlags & flags) const
{
return (flags_ & flags.flags_) == flags.flags_;
}
template <typename F>
bool EnumFlags<F>::isAnySet(const EnumFlags & flags) const
{
return (flags_ & flags.flags_) != 0;
}
template <typename F>
bool EnumFlags<F>::isAnySet(const EnumFlags & flags) const
{
return (flags_ & flags.flags_) != 0;
}
template <typename F>
bool EnumFlags<F>::operator ==(const EnumFlags<F> & rhs) const
{
return (flags_ == rhs.flags_);
}
template <typename F>
bool EnumFlags<F>::operator ==(const EnumFlags<F> & rhs) const
{
return (flags_ == rhs.flags_);
}
template <typename F>
bool EnumFlags<F>::operator !=(const EnumFlags<F> & rhs) const
{
return (flags_ != rhs.flags_);
}
template <typename F>
bool EnumFlags<F>::operator !=(const EnumFlags<F> & rhs) const
{
return (flags_ != rhs.flags_);
}
template <typename F>
EnumFlags<F> EnumFlags<F>::operator &(const EnumFlags<F> & rhs) const
{
return EnumFlags(flags_ & rhs.flags_);
}
template <typename F>
EnumFlags<F> EnumFlags<F>::operator &(const EnumFlags<F> & rhs) const
{
return EnumFlags(flags_ & rhs.flags_);
}
template <typename F>
EnumFlags<F> EnumFlags<F>::operator |(const EnumFlags<F> & rhs) const
{
return EnumFlags(flags_ | rhs.flags_);
}
template <typename F>
EnumFlags<F> EnumFlags<F>::operator |(const EnumFlags<F> & rhs) const
{
return EnumFlags(flags_ | rhs.flags_);
}
template <typename F>
EnumFlags<F> & EnumFlags<F>::operator &=(const EnumFlags<F> & rhs)
{
flags_ &= rhs.flags_;
return *this;
}
template <typename F>
EnumFlags<F> & EnumFlags<F>::operator &=(const EnumFlags<F> & rhs)
{
flags_ &= rhs.flags_;
return *this;
}
template <typename F>
EnumFlags<F> & EnumFlags<F>::operator |=(const EnumFlags<F> & rhs)
{
flags_ |= rhs.flags_;
return *this;
}
template <typename F>
EnumFlags<F> & EnumFlags<F>::operator |=(const EnumFlags<F> & rhs)
{
flags_ |= rhs.flags_;
return *this;
}
template <typename F>
EnumFlags<F>::operator uint32_t()
{
return flags_;
}
template <typename F>
EnumFlags<F>::operator uint32_t()
{
return flags_;
}
} // namespace sta
#endif // STA_ENUM_FLAGS_TPP
#endif // STA_CORE_ENUM_FLAGS_TPP

View File

@ -2,117 +2,117 @@
* @file
* @brief FIFO buffer type.
*/
#ifndef STA_FIFO_BUFFER_HPP
#define STA_FIFO_BUFFER_HPP
#ifndef STA_CORE_FIFO_BUFFER_HPP
#define STA_CORE_FIFO_BUFFER_HPP
namespace sta
{
/**
* @brief FIFO buffer.
*
* @tparam Size Size type
* @tparam N Buffer size
*
* @ingroup staCore
*/
template <typename Size, Size N>
class FifoBuffer
{
public:
/**
* @brief FIFO size type.
*/
using size_type = Size;
/**
* @brief Max number of bytes in FIFO.
*/
static constexpr size_type MAX_SIZE = N;
/**
* @brief FIFO buffer.
*
* @tparam Size Size type
* @tparam N Buffer size
*
* @ingroup sta_core
*/
template <typename Size, Size N>
class FifoBuffer
{
public:
/**
* @brief FIFO size type.
*/
using size_type = Size;
/**
* @brief Max number of bytes in FIFO.
*/
static constexpr size_type MAX_SIZE = N;
public:
/**
* @brief Construct empty FIFO buffer.
*/
FifoBuffer();
/**
* @brief Construct FIFO buffer with initial data.
*
* @param buffer Source buffer
* @param size Buffer size
*/
FifoBuffer(const uint8_t * buffer, Size size);
public:
/**
* @brief Construct empty FIFO buffer.
*/
FifoBuffer();
/**
* @brief Construct FIFO buffer with initial data.
*
* @param buffer Source buffer
* @param size Buffer size
*/
FifoBuffer(const uint8_t * buffer, Size size);
/**
* @brief Clear buffer content.
*/
void clear();
/**
* @brief Clear buffer content.
*/
void clear();
/**
* @brief Set data in buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to write
*/
void set(const uint8_t * buffer, size_type size);
/**
* @brief Set data in buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to write
*/
void set(const uint8_t * buffer, size_type size);
/**
* @brief Append value to end of buffer.
*
* @param value Value
*/
void pushBack(uint8_t value);
/**
* @brief Append data to end of buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to write
*/
void pushBack(const uint8_t * buffer, size_type size);
/**
* @brief Append value repeatedly to end of buffer.
*
* @param value Fill value
* @param count Repeat count
*/
void pushBack(uint8_t value, size_type count);
/**
* @brief Append value to end of buffer.
*
* @param value Value
*/
void pushBack(uint8_t value);
/**
* @brief Append data to end of buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to write
*/
void pushBack(const uint8_t * buffer, size_type size);
/**
* @brief Append value repeatedly to end of buffer.
*
* @param value Fill value
* @param count Repeat count
*/
void pushBack(uint8_t value, size_type count);
/**
* @brief Take data from start of buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
void popFront(uint8_t * buffer, size_type size);
/**
* @brief Take data from start of buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
void popFront(uint8_t * buffer, size_type size);
/**
* @brief Get size of data in buffer.
*
* @return Data size
*/
size_type size() const;
/**
* @brief Get size of data in buffer.
*
* @return Data size
*/
size_type size() const;
/**
* @brief Check if buffer is full.
*
* @return True if buffer is full
*/
bool isFull() const;
/**
* @brief Check if buffer is empty.
*
* @return True if buffer is empty
*/
bool isEmpty() const;
/**
* @brief Check if buffer is full.
*
* @return True if buffer is full
*/
bool isFull() const;
/**
* @brief Check if buffer is empty.
*
* @return True if buffer is empty
*/
bool isEmpty() const;
private:
uint8_t * head_; /**< Read position */
uint8_t * tail_; /**< Write position */
uint8_t buffer_[N]; /**< Buffer data */
};
private:
uint8_t * head_; /**< Read position */
uint8_t * tail_; /**< Write position */
uint8_t buffer_[N]; /**< Buffer data */
};
} // namespace sta
#include <sta/fifo_buffer.tpp>
#endif // STA_FIFO_BUFFER_HPP
#endif // STA_CORE_FIFO_BUFFER_HPP

View File

@ -1,103 +1,103 @@
#ifndef STA_FIFO_BUFFER_TPP
#define STA_FIFO_BUFFER_TPP
#ifndef STA_CORE_FIFO_BUFFER_TPP
#define STA_CORE_FIFO_BUFFER_TPP
#ifndef STA_FIFO_BUFFER_HPP
#ifndef STA_CORE_FIFO_BUFFER_HPP
# error "Internal header. Include <sta/fifo_buffer.hpp> instead"
#endif // !STA_FIFO_BUFFER_HPP
#endif // !STA_CORE_FIFO_BUFFER_HPP
#include <cstring>
namespace sta
{
template <typename Size, Size N>
FifoBuffer<Size, N>::FifoBuffer()
: head_{buffer_}, tail_{buffer_}
{}
template <typename Size, Size N>
FifoBuffer<Size, N>::FifoBuffer()
: head_{buffer_}, tail_{buffer_}
{}
template <typename Size, Size N>
FifoBuffer<Size, N>::FifoBuffer(const uint8_t * buffer, size_type size)
{
set(buffer, size);
}
template <typename Size, Size N>
FifoBuffer<Size, N>::FifoBuffer(const uint8_t * buffer, size_type size)
{
set(buffer, size);
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::set(const uint8_t * buffer, size_type bsize)
{
STA_ASSERT(bsize <= sizeof(buffer_));
STA_ASSERT(buffer != nullptr);
template <typename Size, Size N>
void FifoBuffer<Size, N>::set(const uint8_t * buffer, size_type bsize)
{
STA_ASSERT(bsize <= sizeof(buffer_));
STA_ASSERT(buffer != nullptr);
head_ = buffer_;
tail_ = buffer_ + bsize;
memcpy(buffer_, buffer, bsize);
}
head_ = buffer_;
tail_ = buffer_ + bsize;
memcpy(buffer_, buffer, bsize);
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::clear()
{
head_ = tail_ = buffer_;
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::clear()
{
head_ = tail_ = buffer_;
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::pushBack(uint8_t value)
{
STA_ASSERT_MSG(tail_ < buffer_ + sizeof(buffer_), "Buffer overflow");
template <typename Size, Size N>
void FifoBuffer<Size, N>::pushBack(uint8_t value)
{
STA_ASSERT_MSG(tail_ < buffer_ + sizeof(buffer_), "Buffer overflow");
*tail_++ = value;
}
*tail_++ = value;
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::pushBack(const uint8_t * buffer, size_type bsize)
{
STA_ASSERT_MSG(size() + bsize <= sizeof(buffer_), "Buffer overflow");
STA_ASSERT(buffer != nullptr);
template <typename Size, Size N>
void FifoBuffer<Size, N>::pushBack(const uint8_t * buffer, size_type bsize)
{
STA_ASSERT_MSG(size() + bsize <= sizeof(buffer_), "Buffer overflow");
STA_ASSERT(buffer != nullptr);
memcpy(tail_, buffer, bsize);
tail_ += bsize;
}
memcpy(tail_, buffer, bsize);
tail_ += bsize;
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::pushBack(uint8_t value, size_type count)
{
STA_ASSERT_MSG(size() + count <= sizeof(buffer_), "Buffer overflow");
template <typename Size, Size N>
void FifoBuffer<Size, N>::pushBack(uint8_t value, size_type count)
{
STA_ASSERT_MSG(size() + count <= sizeof(buffer_), "Buffer overflow");
memset(tail_, value, count);
tail_ += count;
}
memset(tail_, value, count);
tail_ += count;
}
template <typename Size, Size N>
void FifoBuffer<Size, N>::popFront(uint8_t * buffer, size_type bsize)
{
STA_ASSERT_MSG(size() >= bsize, "Not enough data");
STA_ASSERT(buffer != nullptr);
template <typename Size, Size N>
void FifoBuffer<Size, N>::popFront(uint8_t * buffer, size_type bsize)
{
STA_ASSERT_MSG(size() >= bsize, "Not enough data");
STA_ASSERT(buffer != nullptr);
memcpy(buffer, head_, bsize);
head_ += bsize;
}
memcpy(buffer, head_, bsize);
head_ += bsize;
}
template <typename Size, Size N>
typename FifoBuffer<Size, N>::size_type FifoBuffer<Size, N>::size() const
{
return (tail_ - head_);
}
template <typename Size, Size N>
typename FifoBuffer<Size, N>::size_type FifoBuffer<Size, N>::size() const
{
return (tail_ - head_);
}
template <typename Size, Size N>
bool FifoBuffer<Size, N>::isFull() const
{
return (tail_ == buffer_ + sizeof(buffer_));
}
template <typename Size, Size N>
bool FifoBuffer<Size, N>::isFull() const
{
return (tail_ == buffer_ + sizeof(buffer_));
}
template <typename Size, Size N>
bool FifoBuffer<Size, N>::isEmpty() const
{
return (head_ == tail_);
}
template <typename Size, Size N>
bool FifoBuffer<Size, N>::isEmpty() const
{
return (head_ == tail_);
}
} // namespace sta
#endif // STA_FIFO_BUFFER_TPP
#endif // STA_CORE_FIFO_BUFFER_TPP

47
include/sta/gpio_pin.hpp Normal file
View File

@ -0,0 +1,47 @@
/**
* @file
* @brief GPIO pin interface definitions.
*/
#ifndef STA_CORE_GPIO_PIN_HPP
#define STA_CORE_GPIO_PIN_HPP
namespace sta
{
/**
* @defgroup sta_core_gpio GPIO
* @ingroup sta_core
* @brief GPIO pins.
* @{
*/
/**
* @brief GPIO pin state.
*/
enum class GpioPinState
{
LOW,
HIGH
};
/**
* @brief Interface for GPIO pins.
*/
class GpioPin
{
public:
/**
* @brief Set pin output state.
*
* @param state Output state
*/
virtual void setState(GpioPinState state) = 0;
};
/** @} */
} // namespace sta
#endif // STA_CORE_GPIO_PIN_HPP

27
include/sta/i2c.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef STA_I2C_HPP
#define STA_I2C_HPP
#include <cstdint>
#include <sta/mutex.hpp>
namespace sta {
class I2cDevice {
protected:
uint16_t address;
Mutex* mutex;
bool master;
bool blocking;
public:
I2cDevice(uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false, bool blocking=true);
virtual bool transmit(uint8_t* data, uint16_t size) = 0;
virtual bool receive(uint8_t* data, uint16_t size) = 0;
virtual void acquire();
virtual void release();
};
}
#endif // STA_I2C_HPP

View File

@ -1,35 +0,0 @@
/**
* @brief GPIO pin interface definitions.
*/
#ifndef STA_GPIO_PIN_HPP
#define STA_GPIO_PIN_HPP
namespace sta
{
/**
* @brief GPIO pin state.
*/
enum class GpioPinState
{
LOW,
HIGH
};
/**
* @brief Interface for GPIO pins.
*/
class GpioPin
{
public:
/**
* @brief Set pin output state.
*
* @param state Output state
*/
virtual void setState(GpioPinState state) = 0;
};
} // namespace sta
#endif // STA_GPIO_PIN_HPP

View File

@ -1,30 +0,0 @@
/**
* @brief Mutex interface definition.
*/
#ifndef STA_MUTEX_HPP
#define STA_MUTEX_HPP
namespace sta
{
/**
* @brief Interface for mutex objects.
*/
class Mutex
{
public:
/**
* @brief Block until mutex has been acquired.
*/
virtual void acquire() = 0;
/**
* @brief Release mutex.
*/
virtual void release() = 0;
static Mutex * ALWAYS_FREE; /**< Fake mutex that can always be acquired */
};
} // namespace sta
#endif // STA_MUTEX_HPP

View File

@ -1,40 +0,0 @@
/**
* @brief Signal interface definition.
*/
#ifndef STA_SIGNAL_HPP
#define STA_SIGNAL_HPP
namespace sta
{
/**
* @brief Interface for signal objects.
*/
class Signal
{
public:
/**
* @brief Enter signaled state.
*/
virtual void notify() = 0;
/**
* @brief Check signal state w/o changing it.
*
* @return True if in signaled state
*/
virtual bool peek() = 0;
/**
* @brief Check signal state.
*
* @return True if in signaled state
*/
virtual bool test() = 0;
/**
* @brief Wait until signaled state is entered.
*/
virtual void wait() = 0;
};
} // namespace sta
#endif // STA_SIGNAL_HPP

View File

@ -1,27 +0,0 @@
/**
* @brief Signatures for time related functions.
*/
#ifndef STA_TIME_HPP
#define STA_TIME_HPP
#include <cstdint>
namespace sta
{
/**
* @brief Signature for millisecond precision time.
*
* @return Time in milliseconds
*/
using TimeMsFn = uint32_t (*)();
/**
* @brief Signature for microseconds precision time.
*
* @return Time in microseconds
*/
using TimeUsFn = uint32_t (*)();
} // namespace sta
#endif // STA_TIME_HPP

View File

@ -1,50 +0,0 @@
/**
* @file
* @brief UART interface definition.
*/
#ifndef STA_INTF_UART_HPP
#define STA_INTF_UART_HPP
#include <cstddef>
#include <cstdint>
namespace sta
{
/**
* @brief Interface for %UART.
*/
class UART
{
public:
/**
* @brief Write buffer to %UART.
*
* @param buffer Source buffer
* @param size Number of bytes in buffer
*/
virtual void write(const uint8_t * buffer, size_t size) = 0;
/**
* @brief Write unsigned integer to %UART.
*
* @param value Unsigned integer value
*/
void write(uint8_t value);
/**
* @brief Write unsigned integer to %UART.
*
* @param value Unsigned integer value
*/
void write(uint16_t value);
/**
* @brief Write unsigned integer to %UART.
*
* @param value Unsigned integer value
*/
void write(uint32_t value);
};
} // namespace sta
#endif // STA_INTF_UART_HPP

View File

@ -2,12 +2,15 @@
* @file
* @brief Helper for useful compiler features.
*/
#ifndef STA_LANG_HPP
#define STA_LANG_HPP
#ifndef STA_CORE_LANG_HPP
#define STA_CORE_LANG_HPP
#include <sta/config.hpp>
/**
* @defgroup staCoreLang Lang
* @ingroup staCore
* @defgroup sta_core_lang Lang
* @ingroup sta_core
* @brief Compiler instructions.
* @{
*/
@ -113,4 +116,4 @@
/** @} */
#endif // STA_LANG_HPP
#endif // STA_CORE_LANG_HPP

33
include/sta/mutex.hpp Normal file
View File

@ -0,0 +1,33 @@
/**
* @file
* @brief Mutex interface definition.
*/
#ifndef STA_CORE_MUTEX_HPP
#define STA_CORE_MUTEX_HPP
namespace sta
{
/**
* @brief Interface for mutex objects.
*
* @ingroup sta_core
*/
class Mutex
{
public:
/**
* @brief Block until mutex has been acquired.
*/
virtual void acquire() = 0;
/**
* @brief Release mutex.
*/
virtual void release() = 0;
static Mutex * ALWAYS_FREE; /**< Fake mutex that can always be acquired */
};
} // namespace sta
#endif // STA_CORE_MUTEX_HPP

View File

@ -2,10 +2,10 @@
* @file
* @brief Printable UART interface definition.
*/
#ifndef STA_PRINTABLE_UART_HPP
#define STA_PRINTABLE_UART_HPP
#ifndef STA_CORE_PRINTABLE_UART_HPP
#define STA_CORE_PRINTABLE_UART_HPP
#include <sta/intf/uart.hpp>
#include <sta/uart.hpp>
#include <cstddef>
#include <cstdint>
@ -13,192 +13,192 @@
namespace sta
{
/**
* @brief Integer representation.
*
* @ingroup staCore
*/
enum class IntegerBase
{
DEC, /**< Decimal */
BIN, /**< Binary */
HEX /**< Hexadecimal */
};
/**
* @brief Integer representation.
*
* @ingroup sta_core
*/
enum class IntegerBase
{
DEC, /**< Decimal */
BIN, /**< Binary */
HEX /**< Hexadecimal */
};
/**
* @brief Printable interface for UART.
*
* @ingroup staCore
*/
class PrintableUART
{
public:
/**
* @param intf UART instance
*/
PrintableUART(UART * intf);
/**
* @brief Printable interface for UART.
*
* @ingroup sta_core
*/
class PrintableUART
{
public:
/**
* @param intf UART instance
*/
PrintableUART(UART * intf);
/**
* @brief Print single character.
*
* @param c Character to print
*/
void print(char c);
/**
* @brief Print boolean value.
*
* @param b Boolean value
*/
void print(bool b);
/**
* @brief Print floating point value.
*
* @param d Floating point value
*/
void print(double d);
/**
* @brief Print integer in selected base.
*
* @param num 8-bit unsigned integer
* @param base Integer base
*/
void print(uint8_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num 16-bit unsigned integer
* @param base Integer base
*/
void print(uint16_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num 32-bit unsigned integer
* @param base Integer base
*/
void print(uint32_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num Integer
* @param base Integer base
*/
void print(size_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print c-string.
*
* @param str Null terminated string
*/
void print(const char * str);
/**
* @brief Print string.
*
* @param str String buffer
* @param length String length
*/
void print(const char * str, size_t length);
/**
* @brief Print single character.
*
* @param c Character to print
*/
void print(char c);
/**
* @brief Print boolean value.
*
* @param b Boolean value
*/
void print(bool b);
/**
* @brief Print floating point value.
*
* @param d Floating point value
*/
void print(double d);
/**
* @brief Print integer in selected base.
*
* @param num 8-bit unsigned integer
* @param base Integer base
*/
void print(uint8_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num 16-bit unsigned integer
* @param base Integer base
*/
void print(uint16_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num 32-bit unsigned integer
* @param base Integer base
*/
void print(uint32_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num Integer
* @param base Integer base
*/
void print(size_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print c-string.
*
* @param str Null terminated string
*/
void print(const char * str);
/**
* @brief Print string.
*
* @param str String buffer
* @param length String length
*/
void print(const char * str, size_t length);
/**
* @brief Print new-line.
*/
void println();
/**
* @brief Print single character followed by a new-line.
*
* @param c Character to print
*/
void println(char c);
/**
* @brief Print boolean value followed by a new-line.
*
* @param b Boolean value
*/
void println(bool b);
/**
* @brief Print floating point value followed by a new-line.
*
* @param d Floating point value
*/
void println(double d);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 8-bit unsigned integer
* @param base Integer base
*/
void println(uint8_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 16-bit unsigned integer
* @param base Integer base
*/
void println(uint16_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 32-bit unsigned integer
* @param base Integer base
*/
void println(uint32_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num Integer
* @param base Integer base
*/
void println(size_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print c-string followed by a new-line.
*
* @param str Null terminated string
*/
void println(const char * str);
/**
* @brief Print string followed by a new-line.
*
* @param str String buffer
* @param length String length
*/
void println(const char * str, size_t length);
/**
* @brief Print new-line.
*/
void println();
/**
* @brief Print single character followed by a new-line.
*
* @param c Character to print
*/
void println(char c);
/**
* @brief Print boolean value followed by a new-line.
*
* @param b Boolean value
*/
void println(bool b);
/**
* @brief Print floating point value followed by a new-line.
*
* @param d Floating point value
*/
void println(double d);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 8-bit unsigned integer
* @param base Integer base
*/
void println(uint8_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 16-bit unsigned integer
* @param base Integer base
*/
void println(uint16_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 32-bit unsigned integer
* @param base Integer base
*/
void println(uint32_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num Integer
* @param base Integer base
*/
void println(size_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print c-string followed by a new-line.
*
* @param str Null terminated string
*/
void println(const char * str);
/**
* @brief Print string followed by a new-line.
*
* @param str String buffer
* @param length String length
*/
void println(const char * str, size_t length);
private:
/**
* @brief Print unsigned integer in selected base.
*
* @param value Unsigned integer value
* @param base Integer base
* @param fmt printf format string for base 10
* @param size Size of value in bytes
*/
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size);
/**
* @brief Print unsigned integer in base 10.
*
* @param value Unsigned integer value
* @param fmt printf format string
*/
void printDec(uintmax_t value, const char * fmt);
/**
* @brief Print unsigned integer in base 2.
*
* @param value Unsigned integer value
* @param digits Number of digits to print
*/
void printBin(uintmax_t value, size_t digits);
/**
* @brief Print unsigned integer in base 16.
*
* @param value Unsigned integer value
* @param digits Number of digits to print
*/
void printHex(uintmax_t value, size_t digits);
private:
/**
* @brief Print unsigned integer in selected base.
*
* @param value Unsigned integer value
* @param base Integer base
* @param fmt printf format string for base 10
* @param size Size of value in bytes
*/
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size);
/**
* @brief Print unsigned integer in base 10.
*
* @param value Unsigned integer value
* @param fmt printf format string
*/
void printDec(uintmax_t value, const char * fmt);
/**
* @brief Print unsigned integer in base 2.
*
* @param value Unsigned integer value
* @param digits Number of digits to print
*/
void printBin(uintmax_t value, size_t digits);
/**
* @brief Print unsigned integer in base 16.
*
* @param value Unsigned integer value
* @param digits Number of digits to print
*/
void printHex(uintmax_t value, size_t digits);
private:
UART * intf_;
};
private:
UART * intf_;
};
} // namespace sta
#endif // STA_PRINTABLE_UART_HPP
#endif // STA_CORE_PRINTABLE_UART_HPP

View File

@ -1,28 +1,13 @@
/**
* @file
* @brief Compatibility layer for different printf implementations.
*/
#ifndef STA_PRINTF_HPP
#define STA_PRINTF_HPP
#ifdef DOXYGEN
/**
* @def STA_PRINTF_USE_STDLIB
* @brief Use printf implementation from STD library.
*
* @ingroup staCoreBuildConfig
* Configuration:
* * STA_PRINTF_USE_STDLIB: Use printf implementation from standard library
* * STA_PRINTF_USE_MPALAND: Use printf implementation from Marco Paland
*/
# define STA_PRINTF_USE_STDLIB
/**
* @def STA_PRINTF_USE_MPALAND
* @brief Use printf implementation from Marco Paland.
*
* @ingroup staCoreBuildConfig
*/
# define STA_PRINTF_USE_MPALAND
#endif // DOXYGEN
#ifndef STA_CORE_PRINTF_HPP
#define STA_CORE_PRINTF_HPP
#include <sta/config.hpp>
@ -39,4 +24,4 @@
#endif // STA_PRINTF_USE_MPALAND
#endif // STA_PRINTF_HPP
#endif // STA_CORE_PRINTF_HPP

43
include/sta/signal.hpp Normal file
View File

@ -0,0 +1,43 @@
/**
* @file
* @brief Signal interface definition.
*/
#ifndef STA_CORE_SIGNAL_HPP
#define STA_CORE_SIGNAL_HPP
namespace sta
{
/**
* @brief Interface for signal objects.
*
* @ingroup sta_core
*/
class Signal
{
public:
/**
* @brief Enter signaled state.
*/
virtual void notify() = 0;
/**
* @brief Check signal state w/o changing it.
*
* @return True if in signaled state
*/
virtual bool peek() = 0;
/**
* @brief Check signal state.
*
* @return True if in signaled state
*/
virtual bool test() = 0;
/**
* @brief Wait until signaled state is entered.
*/
virtual void wait() = 0;
};
} // namespace sta
#endif // STA_CORE_SIGNAL_HPP

View File

@ -1,12 +1,12 @@
/**
* @file
* @brief SPI device interface.
* @brief SPI bus peripheral device.
*/
#ifndef STA_SPI_DEVICE_HPP
#define STA_SPI_DEVICE_HPP
#ifndef STA_CORE_SPI_DEVICE_HPP
#define STA_CORE_SPI_DEVICE_HPP
#include <sta/intf/gpio_pin.hpp>
#include <sta/spi/interface.hpp>
#include <sta/gpio_pin.hpp>
#include <sta/spi/spi.hpp>
#include <cstddef>
#include <cstdint>
@ -14,102 +14,102 @@
namespace sta
{
/**
* @brief Interface for SPI devices.
*
* @ingroup staCoreSPI
*/
class SpiDevice
{
public:
/**
* @param intf SPI hardware interface
* @param csPin Chip select pin
*/
SpiDevice(SpiInterface * intf, GpioPin * csPin);
/**
* @brief Peripheral device connected via SPI.
*
* @ingroup sta_core_spi
*/
class SPIDevice
{
public:
/**
* @param intf %SPI hardware interface
* @param csPin Chip select pin
*/
SPIDevice(SPI * intf, GpioPin * csPin);
/**
* @brief Start transmission with device.
*
* Must be called before any I/O operations.
*/
void beginTransmission();
/**
* @brief End transmission with device.
*
* Must be called after last I/O operation.
*/
void endTransmission();
/**
* @brief Start transmission with device.
*
* Must be called before any I/O operations.
*/
void beginTransmission();
/**
* @brief End transmission with device.
*
* Must be called after last I/O operation.
*/
void endTransmission();
/**
* @brief Send single byte of data.
*
* @param value 8-bit value
*/
void transfer(uint8_t value);
/**
* @brief Send two bytes of data.
*
* @param value 16-bit value
*/
void transfer16(uint16_t value);
/**
* @brief Send data from buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to transfer
*/
void transfer(const uint8_t * buffer, size_t size);
/**
* @brief Send and receive data simultaneously.
*
* @param txBuffer Send buffer
* @param rxBuffer Receive buffer
* @param size Number of bytes to transfer
*/
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size);
/**
* @brief Read incoming data to buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
void receive(uint8_t * buffer, size_t size);
/**
* @brief Send single byte of data.
*
* @param value 8-bit value
*/
void transfer(uint8_t value);
/**
* @brief Send two bytes of data.
*
* @param value 16-bit value
*/
void transfer16(uint16_t value);
/**
* @brief Send data from buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to transfer
*/
void transfer(const uint8_t * buffer, size_t size);
/**
* @brief Send and receive data simultaneously.
*
* @param txBuffer Send buffer
* @param rxBuffer Receive buffer
* @param size Number of bytes to transfer
*/
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size);
/**
* @brief Read incoming data to buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
void receive(uint8_t * buffer, size_t size);
/**
* @brief Send byte value repeatedly.
*
* @param value 8-bit value to repeat
* @param count Number of repetitions
*/
void fill(uint8_t value, size_t count);
/**
* @brief Send byte value repeatedly.
*
* @param value 8-bit value to repeat
* @param count Number of repetitions
*/
void fill(uint8_t value, size_t count);
/**
* @brief Get SPI interface settings.
*
* @return SPI settings
*/
const SpiSettings & settings() const;
/**
* @brief Get %SPI interface settings.
*
* @return SPI settings
*/
const SpiSettings & settings() const;
/**
* @brief Activate device via CS pin.
*/
void select();
/**
* @brief Deactivate device via CS pin.
*/
void deselect();
/**
* @brief Activate device via CS pin.
*/
void select();
/**
* @brief Deactivate device via CS pin.
*/
void deselect();
private:
SpiInterface * intf_; /**< SPI hardware interface */
GpioPin * csPin_; /**< Chip select pin */
};
private:
SPI * intf_; /**< %SPI hardware interface */
GpioPin * csPin_; /**< Chip select pin */
};
} // namespace sta
#endif // STA_SPI_DEVICE_HPP
#endif // STA_CORE_SPI_DEVICE_HPP

View File

@ -1,103 +0,0 @@
/**
* @file
* @brief SPI interface definition.
*/
#ifndef STA_SPI_INTERFACE_HPP
#define STA_SPI_INTERFACE_HPP
#include <sta/intf/mutex.hpp>
#include <sta/spi/settings.hpp>
#include <cstddef>
#include <cstdint>
namespace sta
{
/**
* @brief Interface for SPI hardware.
*
* @ingroup staCoreSPI
*/
class SpiInterface
{
public:
/**
* @param mutex Mutex object for managing shared access. Pass nullptr for no access control
*/
SpiInterface(Mutex * mutex = nullptr);
/**
* @brief Send single byte of data.
*
* @param value 8-bit value
*/
virtual void transfer(uint8_t value) = 0;
/**
* @brief Send two bytes of data.
*
* @param value 16-bit value
*/
virtual void transfer16(uint16_t value) = 0;
/**
* @brief Send data from buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to transfer
*/
virtual void transfer(const uint8_t * buffer, size_t size) = 0;
/**
* @brief Send and receive data simultaneously.
*
* @param txBuffer Send buffer
* @param rxBuffer Receive buffer
* @param size Number of bytes to transfer
*/
virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0;
/**
* @brief Read incoming data to buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
virtual void receive(uint8_t * buffer, size_t size) = 0;
/**
* @brief Send byte value repeatedly.
*
* @param value 8-bit value to repeat
* @param count Number of repetitions
*/
virtual void fill(uint8_t value, size_t count) = 0;
/**
* @brief Get SPI interface settings.
*
* @return SPI settings
*/
virtual const SpiSettings & settings() const = 0;
/**
* @brief Acquire usage rights to use the interface.
*
* Must be called before any I/O operations are executed.
*/
virtual void acquire();
/**
* @brief Release usage rights for interface.
*
* Must be called after last I/O operation.
*/
virtual void release();
private:
Mutex * mutex_; /**< Mutex object */
};
} // namespace sta
#endif // STA_SPI_INTERFACE_HPP

View File

@ -1,13 +1,14 @@
/**
* @file
* @brief SPI settings.
* @brief SPI bus settings.
*/
#ifndef STA_SPI_SETTINGS_HPP
#define STA_SPI_SETTINGS_HPP
#ifndef STA_CORE_SPI_SETTINGS_HPP
#define STA_CORE_SPI_SETTINGS_HPP
/**
* @defgroup staCoreSPI SPI
* @ingroup staCore
* @defgroup sta_core_spi SPI
* @ingroup sta_core
* @brief SPI interface.
*/
@ -17,98 +18,98 @@
namespace sta
{
/**
* @ingroup staCoreSPI
* @{
*/
/**
* @ingroup sta_core_spi
* @{
*/
/**
* @brief SPI clock polarity.
*/
enum class SpiClkPolarity
{
LOW, /**< Low idle clock */
HIGH /**< High idle clock */
};
/**
* @brief %SPI clock polarity.
*/
enum class SPIClkPolarity
{
LOW, /**< Low idle clock */
HIGH /**< High idle clock */
};
/**
* @brief SPI clock phase.
*/
enum class SpiClkPhase
{
EDGE_1, /**< Sample on first edge, shift out on second edge */
EDGE_2 /**< Shift out on first edge, sample on second edge */
};
/**
* @brief %SPI clock phase.
*/
enum class SPIClkPhase
{
EDGE_1, /**< Sample on first edge, shift out on second edge */
EDGE_2 /**< Shift out on first edge, sample on second edge */
};
/**
* @brief SPI clock mode.
*/
enum class SpiMode
{
MODE_0, /**< Low idle clock, sample on rising edge, shift out on falling edge */
MODE_1, /**< Low idle clock, sample on falling edge, shift out on rising edge */
MODE_2, /**< High idle clock, sample on rising edge, shift out on falling edge */
MODE_3 /**< High idle clock, sample on falling edge, shift out on rising edge */
};
/**
* @brief %SPI clock mode.
*/
enum class SPIMode
{
MODE_0, /**< Low idle clock, sample on rising edge, shift out on falling edge */
MODE_1, /**< Low idle clock, sample on falling edge, shift out on rising edge */
MODE_2, /**< High idle clock, sample on rising edge, shift out on falling edge */
MODE_3 /**< High idle clock, sample on falling edge, shift out on rising edge */
};
/**
* @brief SPI data size.
*/
enum class SpiDataSize
{
SIZE_8, /**< 8-bit data size */
SIZE_16 /**< 16-bit data size */
};
/**
* @brief %SPI data size.
*/
enum class SPIDataSize
{
SIZE_8, /**< 8-bit data size */
SIZE_16 /**< 16-bit data size */
};
/**
* @brief SPI bit order.
*/
enum class SpiBitOrder
{
MSB, /**< Send most significant bit first */
LSB /**< Send least significant bit first */
};
/**
* @brief %SPI bit order.
*/
enum class SPIBitOrder
{
MSB, /**< Send most significant bit first */
LSB /**< Send least significant bit first */
};
/**
* @brief SPI settings.
*/
struct SpiSettings
{
SpiMode mode; /**< SPI clock mode */
SpiDataSize dataSize; /**< SPI data size */
SpiBitOrder bitOrder; /**< SPI bit order */
uint32_t clkSpeed; /**< SPI clock speed */
};
/**
* @brief %SPI settings.
*/
struct SPISettings
{
SPIMode mode; /**< %SPI clock mode */
SPIDataSize dataSize; /**< %SPI data size */
SPIBitOrder bitOrder; /**< %SPI bit order */
uint32_t clkSpeed; /**< %SPI clock speed */
};
/**
* @brief Get SPI clock polarity from clock mode.
*
* @param mode SPI clock mode
* @return SPI clock polarity
*/
SpiClkPolarity getSpiClkPolarity(SpiMode mode);
/**
* @brief Get SPI clock phase from clock mode.
*
* @param mode SPI clock mode
* @return SPI clock phase
*/
SpiClkPhase getSpiClkPhase(SpiMode mode);
/**
* @brief Get SPI clock mode from clock phase and polarity.
*
* @param polarity SPI clock polarity
* @param phase SPI clock phase
* @return SPI clock mode
*/
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase);
/**
* @brief Get %SPI clock polarity from clock mode.
*
* @param mode %SPI clock mode
* @return %SPI clock polarity
*/
SPIClkPolarity getSPIClkPolarity(SPIMode mode);
/**
* @brief Get %SPI clock phase from clock mode.
*
* @param mode %SPI clock mode
* @return %SPI clock phase
*/
SPIClkPhase getSPIClkPhase(SPIMode mode);
/**
* @brief Get %SPI clock mode from clock phase and polarity.
*
* @param polarity %SPI clock polarity
* @param phase %SPI clock phase
* @return %SPI clock mode
*/
SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase);
/** @} */
/** @} */
} // namespace sta
#endif // STA_SPI_SETTINGS_HPP
#endif // STA_CORE_SPI_SETTINGS_HPP

107
include/sta/spi/spi.hpp Normal file
View File

@ -0,0 +1,107 @@
/**
* @file
* @brief SPI bus software interface.
*/
#ifndef STA_CORE_SPI_SPI_HPP
#define STA_CORE_SPI_SPI_HPP
#include <sta/mutex.hpp>
#include <sta/spi/settings.hpp>
#include <cstddef>
#include <cstdint>
namespace sta
{
/**
* @brief Interface class for %SPI hardware.
*
* Represents a single %SPI bus that can be shared by multiple devices.
*
* @ingroup sta_core_spi
*/
class SPI
{
public:
/**
* @param settings %SPI bus settings
* @param mutex Mutex object for managing shared access. Pass nullptr for no access control
*/
SPI(const SPISettings & settings, Mutex * mutex = nullptr);
/**
* @brief Send single byte of data.
*
* @param value 8-bit value
*/
virtual void transfer(uint8_t value) = 0;
/**
* @brief Send two bytes of data.
*
* @param value 16-bit value
*/
virtual void transfer16(uint16_t value) = 0;
/**
* @brief Send data from buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to transfer
*/
virtual void transfer(const uint8_t * buffer, size_t size) = 0;
/**
* @brief Send and receive data simultaneously.
*
* @param txBuffer Send buffer
* @param rxBuffer Receive buffer
* @param size Number of bytes to transfer
*/
virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0;
/**
* @brief Read incoming data to buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
virtual void receive(uint8_t * buffer, size_t size) = 0;
/**
* @brief Send byte value repeatedly.
*
* @param value 8-bit value to repeat
* @param count Number of repetitions
*/
virtual void fill(uint8_t value, size_t count) = 0;
/**
* @brief Get %SPI interface settings.
*
* @return %SPI settings
*/
const SPISettings & settings() const;
/**
* @brief Acquire usage rights to use the interface.
*
* Must be called before any I/O operations are executed.
*/
virtual void acquire();
/**
* @brief Release usage rights for interface.
*
* Must be called after last I/O operation.
*/
virtual void release();
private:
SPISettings settings_; /**< %SPI settings */
Mutex * mutex_; /**< Mutex object */
};
} // namespace sta
#endif // STA_CORE_SPI_SPI_HPP

120
include/sta/stm32/can.hpp Normal file
View File

@ -0,0 +1,120 @@
/**
* @file
* @brief Implementation of CanController using STM32 HAL.
*
* Configuration:
* * STA_STM32_CAN_GLOBAL: Create global CanBus object using this CAN instance
*/
#ifndef STA_CORE_STM32_CAN_HPP
#define STA_CORE_STM32_CAN_HPP
/**
* @defgroup sta_core_stm32_can CAN
* @ingroup sta_core_stm32
* @brief STM32 CAN module.
*
* Check @ref stm32BuildConfig for configuration options.
*/
// Only enable module on STM32 platform w/ HAL CAN module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# ifdef HAL_CAN_MODULE_ENABLED
# define STA_STM32_CAN_ENABLED
# endif // HAL_CAN_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#if defined(STA_STM32_CAN_ENABLED) || defined(DOXYGEN)
#include <sta/can/controller.hpp>
namespace sta
{
/**
* @brief Implementation of CanController interface using HAL.
*
* @ingroup sta_core_stm32_can
*/
class STM32CanController : public CanController
{
public:
static constexpr uint8_t MAX_FILTER_COUNT = 14; /**< Max number of filters */
static constexpr uint8_t MAX_FIFO_COUNT = 2; /**< Max number of FIFOs */
static constexpr uint8_t MAX_PAYLOAD_SIZE = 8; /**< Maximum payload size */
public:
/**
* @param handle CAN handle
*/
STM32CanController(CAN_HandleTypeDef * handle);
/**
* @brief Enable RX pending interrupts.
*/
void enableRxInterrupts();
/**
* @brief Start CAN controller.
*/
void start();
/**
* @brief Stop CAN controller.
*/
void stop();
// RX/TX
//
bool sendFrame(const CanTxHeader & header, const uint8_t * payload) override;
bool receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload) override;
uint32_t getRxFifoFlags() override;
// RX Filter
//
void configureFilter(uint8_t idx, const CanFilter & filter, bool active = false) override;
void enableFilter(uint8_t idx) override;
void disableFilter(uint8_t idx) override;
void clearFilters() override;
private:
/**
* @brief Initialize filter settings.
*/
void initFilters();
private:
CAN_HandleTypeDef * handle_; /**< CAN handle */
CAN_FilterTypeDef filters_[MAX_FILTER_COUNT]; /**< Filter settings */
};
#if defined(STA_STM32_CAN_GLOBAL) || DOXYGEN
/**
* @brief Global CAN instance.
*
* @ingroup sta_core_stm32_can
*/
extern STM32CanController CanBus;
/**
* @brief Interrupt handler for pending RX frames.
*
* May be implemented by application.
*
* @ingroup sta_core_stm32_can
*/
void CanBus_RxPendingCallback();
#endif // STA_STM32_CAN_GLOBAL
} // namespace sta
#endif // STA_STM32_CAN_ENABLED
#endif // STA_CORE_STM32_CAN_HPP

View File

@ -0,0 +1,107 @@
/**
* @file
* @brief Helper macros for STM32 clock queries.
*/
#ifndef STA_CORE_STM32_CLOCKS_HPP
#define STA_CORE_STM32_CLOCKS_HPP
// Only enable module on STM32 platform
#include <sta/config.hpp>
#if defined(STA_PLATFORM_STM32) || defined(DOXYGEN)
#include <sta/stm32/hal.hpp>
/**
* @defgroup sta_core_stm32_clocks Clocks
* @ingroup sta_core_stm32
* @brief STM32 Clock queries.
* @{
*/
namespace sta
{
/**
* @brief Get peripheral clock frequency.
*
* @return Clock frequency
*/
using PCLKFreqFn = uint32_t (*)();
} // namespace sta
/**
* @brief Get function returning PCLK frequency.
*
* @param n Index of peripheral clock
*/
#define STA_STM32_GET_PCLK_FREQ_FN(n) HAL_RCC_GetPCLK ## n ## Freq
/**
* @brief Internal helper for macro expansion.
*
* @param n PCLK index
* @return Function returning PCLK frequency
*/
#define _STA_STM32_GET_PCLK_FREQ_FN(n) STA_STM32_GET_PCLK_FREQ_FN(n)
/**
* @brief Map instance name to PCLK index.
*
* @param type Hardware type
* @param idx Instance index
* @return PCLK index
*/
#define _STA_STM32_PCLK_IDX_MAP(type, idx) STA_STM32_ ## type ## _ ## idx ## _PCLK_IDX
// Get HAL handle to PCLK index map macro
/**
* @brief Map instance handle to PCLK index.
*
* @param handle HAL handle
* @return PCLK index
*/
#define _STA_STM32_HANDLE_PCLK_IDX_MAP(handle) STA_STM32_ ## handle ## _PCLK_IDX
/**
* @brief Get function returning frequency of PCLK used by TIM.
*
* @param n TIM index
*/
#define STA_STM32_GET_TIM_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(TIM, n))
/**
* @brief Get function returning frequency of PCLK used by SPI interface.
*
* @param n SPI interface index
*/
#define STA_STM32_GET_SPI_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(SPI, n))
/**
* @brief Get function returning frequency of PCLK used by I2C interface.
*
* @param n I2C interface index
*/
#define STA_STM32_GET_I2C_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(I2C, n))
/**
* @brief Get function returning frequency of PCLK used by USART interface.
*
* @param n USART interface index
*/
#define STA_STM32_GET_USART_PCLK_FREQ_FN(n) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_PCLK_IDX_MAP(USART, n))
/**
* @brief Get function returning frequency of PCLK used by HAL instance.
*
* @param handle Instance handle
*/
#define STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle) _STA_STM32_GET_PCLK_FREQ_FN(_STA_STM32_HANDLE_PCLK_IDX_MAP(handle))
/** @} */
#endif // STA_PLATFORM_STM32
#endif // STA_CORE_STM32_CLOCKS_HPP

View File

@ -0,0 +1,57 @@
/**
* @file
* @brief Delay functions.
*
* Configuration:
* * STA_STM32_DELAY_US_TIM: 1 MHz TIM instance used by sta::delayUs
*
* NOTE: TIM time base must be started before use of sta::delayUs by calling sta::initHAL.
* When using startup system task this is handled automatically.
*/
#ifndef STA_CORE_STM32_DELAY_HPP
#define STA_CORE_STM32_DELAY_HPP
// Only enable module on STM32 platform
#include <sta/config.hpp>
#if defined(STA_PLATFORM_STM32) || defined(DOXYGEN)
#include <cstdint>
namespace sta
{
/**
* @defgroup sta_core_stm32_delay Delay
* @ingroup sta_core_stm32
* @brief STM32 Delay module.
* @{
*/
/**
* @brief Millisecond delay.
*
* @param ms Milliseconds
*/
void delayMs(uint32_t ms);
#if defined(STA_STM32_DELAY_US_TIM) || defined(DOXYGEN)
/**
* @brief Microsecond delay.
*
* @param us Microseconds
*/
void delayUs(uint32_t us);
#endif // STA_STM32_DELAY_US_TIM
/** @} */
} // namespace sta
#endif // STA_PLATFORM_STM32
#endif // STA_CORE_STM32_DELAY_HPP

View File

@ -0,0 +1,113 @@
/**
* @file
* @brief Wrapper for STM32 GPIO pins.
*/
#ifndef STA_CORE_STM32_GPIO_PIN_HPP
#define STA_CORE_STM32_GPIO_PIN_HPP
// Only enable module on STM32 platform w/ HAL GPIO module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# ifdef HAL_GPIO_MODULE_ENABLED
# define STA_STM32_GPIO_ENABLED
# endif // HAL_GPIO_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#if defined(STA_STM32_GPIO_ENABLED) || defined(DOXYGEN)
#include <sta/gpio_pin.hpp>
/**
* @defgroup sta_core_stm32_gpio GPIO
* @ingroup sta_core_stm32
* @brief STM32 GPIO module.
*/
namespace sta
{
/**
* @ingroup sta_core_stm32_gpio
* @{
*/
/**
* @brief Container for STM GPIO Pin data.
*/
class STM32GpioPin : public GpioPin
{
public:
/**
* @param port GPIO port
* @param pin Pin index
*/
STM32GpioPin(GPIO_TypeDef * port, uint16_t pin);
void setState(GpioPinState state) override;
/**
* @brief Get GPIO port for pin.
*
* @return GPIO port
*/
GPIO_TypeDef * getPort() const;
/**
* @brief Get pin index for pin.
*
* @return Pin index
*/
uint16_t getPin() const;
/**
* @brief Get GPIO port index for pin.
*
* @return GPIO port index
*/
uint8_t getPortIndex() const;
private:
GPIO_TypeDef * port_; /**< GPIO port */
uint16_t pin_; /**< GPIO pin */
};
/**
* @brief Interrupt trigger edge.
*/
enum class InterruptEdge
{
RISING, /**< Rising edge */
FALLING, /**< Falling edge */
BOTH /**< Rising and falling edge */
};
/**
* @brief Check pin EXIT pin configuration.
*
* @param pin GPIO pin
* @param edge Interrupt trigger edge
* @return True if EXIT pin and trigger edge matches
*/
bool isInterruptEdge(const STM32GpioPin & pin, InterruptEdge edge);
/** @} */
} // namespace sta
/**
* @brief Create STM32GpioPin object from pin label.
*
* @param label Pin label
*
* @ingroup sta_core_stm32_gpio
*/
#define STA_STM32_GPIO_PIN(label) sta::STM32GpioPin{label##_GPIO_Port, label##_Pin}
#endif // STA_STM32_GPIO_ENABLED
#endif // STA_CORE_STM32_GPIO_PIN_HPP

26
include/sta/stm32/hal.hpp Normal file
View File

@ -0,0 +1,26 @@
/**
* @file
* @brief Generic header for including the STM32 HAL headers.
*/
#ifndef STA_CORE_STM32_HAL_HPP
#define STA_CORE_STM32_HAL_HPP
/**
* @defgroup sta_core_stm32 STM32
* @ingroup sta_core_platforms
* @brief Modules implemented for the STM32 platform.
*/
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
// Include STM32 HAL headers
#include <main.h>
#else // !STA_PLATFORM_STM32
# warning "Included STM32 HAL on non-STM32 platform!"
#endif // !STA_PLATFORM_STM32
#endif // STA_CORE_STM32_HAL_HPP

39
include/sta/stm32/i2c.hpp Normal file
View File

@ -0,0 +1,39 @@
#ifndef STA_STM32_I2C_HPP
#define STA_STM32_I2C_HPP
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# ifdef HAL_I2C_MODULE_ENABLED
# define STA_STM32_I2C_ENABLED
# endif // HAL_SPI_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#ifdef STA_STM32_I2C_ENABLED
#include <sta/i2c.hpp>
namespace sta {
class STM32I2cDevice : public I2cDevice {
private:
I2C_HandleTypeDef* i2cHandle;
const uint32_t timeout = HAL_MAX_DELAY;
public:
STM32I2cDevice(
I2C_HandleTypeDef* i2cHandle,
uint16_t address,
Mutex* mutex=nullptr,
bool master=false,
bool blocking=true
);
bool transmit(uint8_t* data, uint16_t size) override;
bool receive(uint8_t* data, uint16_t size) override;
bool deviceReady();
};
}
#endif // STA_STM32_I2C_ENABLED
#endif // STA_STM32_I2C_HPP

View File

@ -0,0 +1,20 @@
/**
* @file
* @brief Global STM32 HAL initialization.
*/
#ifndef STA_CORE_STM32_INIT_HPP
#define STA_CORE_STM32_INIT_HPP
namespace sta
{
/**
* @brief Initialize global HAL objects.
*
* @ingroup sta_core_stm32
*/
void initHAL();
} // namespace sta
#endif // STA_CORE_STM32_INIT_HPP

View File

@ -0,0 +1,78 @@
/**
* @file
* @brief Configuration for STM32F411xE family.
*/
#ifndef STA_CORE_STM32_MCU_STM32F411xE_HPP
#define STA_CORE_STM32_MCU_STM32F411xE_HPP
#ifndef STM32F411xE
# error "MCU config incompatible"
#endif // !STM32F411xE
#include <sta/stm32/mcu/common.hpp>
// Peripheral clock mappings
//
// TIM to PCLK
#define STA_STM32_TIM_1_PCLK_IDX 2
#define STA_STM32_TIM_2_PCLK_IDX 1
#define STA_STM32_TIM_3_PCLK_IDX 1
#define STA_STM32_TIM_4_PCLK_IDX 1
#define STA_STM32_TIM_5_PCLK_IDX 1
#define STA_STM32_TIM_9_PCLK_IDX 2
#define STA_STM32_TIM_10_PCLK_IDX 2
#define STA_STM32_TIM_11_PCLK_IDX 2
// SPI to PCLK
#define STA_STM32_SPI_1_PCLK_IDX 2
#define STA_STM32_SPI_2_PCLK_IDX 1
#define STA_STM32_SPI_3_PCLK_IDX 1
#define STA_STM32_SPI_4_PCLK_IDX 2
#define STA_STM32_SPI_5_PCLK_IDX 2
// I2C to PCLK
#define STA_STM32_I2C_1_PCLK_IDX 1
#define STA_STM32_I2C_2_PCLK_IDX 1
#define STA_STM32_I2C_3_PCLK_IDX 1
// USART to PCLK
#define STA_STM32_USART_1_PCLK_IDX 2
#define STA_STM32_USART_2_PCLK_IDX 1
#define STA_STM32_USART_6_PCLK_IDX 2
// HAL handle mappings
//
#define STA_STM32_htim1_PCLK_IDX STA_STM32_TIM_1_PCLK_IDX
#define STA_STM32_htim2_PCLK_IDX STA_STM32_TIM_2_PCLK_IDX
#define STA_STM32_htim3_PCLK_IDX STA_STM32_TIM_3_PCLK_IDX
#define STA_STM32_htim4_PCLK_IDX STA_STM32_TIM_4_PCLK_IDX
#define STA_STM32_htim5_PCLK_IDX STA_STM32_TIM_5_PCLK_IDX
#define STA_STM32_htim9_PCLK_IDX STA_STM32_TIM_9_PCLK_IDX
#define STA_STM32_htim10_PCLK_IDX STA_STM32_TIM_10_PCLK_IDX
#define STA_STM32_htim11_PCLK_IDX STA_STM32_TIM_11_PCLK_IDX
// SPI to PCLK
#define STA_STM32_hspi1_PCLK_IDX STA_STM32_SPI_1_PCLK_IDX
#define STA_STM32_hspi2_PCLK_IDX STA_STM32_SPI_2_PCLK_IDX
#define STA_STM32_hspi3_PCLK_IDX STA_STM32_SPI_3_PCLK_IDX
#define STA_STM32_hspi4_PCLK_IDX STA_STM32_SPI_4_PCLK_IDX
#define STA_STM32_hspi5_PCLK_IDX STA_STM32_SPI_5_PCLK_IDX
// I2C to PCLK
#define STA_STM32_hi2c1_PCLK_IDX STA_STM32_I2C_1_PCLK_IDX
#define STA_STM32_hi2c2_PCLK_IDX STA_STM32_I2C_2_PCLK_IDX
#define STA_STM32_h12c3_PCLK_IDX STA_STM32_I2C_3_PCLK_IDX
// USART to PCLK
#define STA_STM32_husart1_PCLK_IDX STA_STM32_USART_1_PCLK_IDX
#define STA_STM32_husart2_PCLK_IDX STA_STM32_USART_2_PCLK_IDX
#define STA_STM32_husart6_PCLK_IDX STA_STM32_USART_6_PCLK_IDX
#endif // STA_CORE_STM32_MCU_STM32F411xE_HPP

View File

@ -0,0 +1,17 @@
/**
* @file
* @brief Configuration for STM32F413xx family.
*/
#ifndef STA_CORE_STM32_MCU_STM32F413xx_HPP
#define STA_CORE_STM32_MCU_STM32F413xx_HPP
#ifndef STM32F413xx
# error "MCU config incompatible"
#endif // !STM32F413xx
#include <sta/stm32/mcu/common.hpp>
#endif // STA_CORE_STM32_MCU_STM32F413xx_HPP

View File

@ -0,0 +1,17 @@
/**
* @file
* @brief Common configuration for STM32 MCUs
*/
#ifndef STA_CORE_STM32_MCU_COMMON_HPP
#define STA_CORE_STM32_MCU_COMMON_HPP
// TODO: Are all STM32 MCUs little endian?
#define STA_MCU_LITTLE_ENDIAN
// Enable STM32 platform
#define STA_PLATFORM_STM32
#endif // STA_CORE_STM32_MCU_COMMON_HPP

139
include/sta/stm32/spi.hpp Normal file
View File

@ -0,0 +1,139 @@
/**
* @file
* @brief SPI bus implementation using STM32 HAL.
*/
#ifndef STA_CORE_STM32_SPI_HPP
#define STA_CORE_STM32_SPI_HPP
// Only enable module on STM32 platform w/ HAL SPI module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# ifndef HAL_GPIO_MODULE_ENABLED
# error "STM32 GPIO module required!"
# endif // !HAL_GPIO_MODULE_ENABLED
# ifdef HAL_SPI_MODULE_ENABLED
# define STA_STM32_SPI_ENABLED
# endif // HAL_SPI_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#if defined(STA_STM32_SPI_ENABLED) || defined(DOXYGEN)
#include <sta/spi/device.hpp>
#include <sta/spi/spi.hpp>
#include <sta/stm32/clocks.hpp>
#include <sta/stm32/gpio_pin.hpp>
/**
* @defgroup sta_core_stm32_spi SPI
* @ingroup sta_core_stm32
* @brief STM32 %SPI module.
*/
namespace sta
{
/**
* @addtogroup sta_core_stm32_spi
* @{
*/
/**
* @brief STM32 HAL implementation of the `SPI` interface class.
*/
class STM32SPI : public SPI
{
public:
struct Info
{
SPI_HandleTypeDef * handle; /**< STM32 HAL handle */
uint32_t pclkFreq; /**< Peripheral clock frequency used by interface */
};
public:
/**
* @param handle STM32 HAL handle
* @param pclkFreq Peripheral clock frequency used by %SPI interface
* @param mutex Mutex object for managing access. Pass nullptr for no access control
*/
STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr);
/**
* @param info Interface info
* @param mutex Mutex object for managing access. Pass nullptr for no access control
*/
STM32SPI(const Info & info, Mutex * mutex = nullptr);
void transfer(uint8_t value) override;
void transfer16(uint16_t value) override;
void transfer(const uint8_t * buffer, size_t size) override;
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override;
void receive(uint8_t * buffer, size_t size) override;
void fill(uint8_t value, size_t count) override;
private:
SPI_HandleTypeDef * handle_; /**< STM32 HAL handle */
};
/**
* @brief STM32 HAL implementation of the `SPIDevice` class.
*/
class STM32SPIDevice : public SPIDevice
{
public:
/**
* @param intf %SPI interface
* @param csPin Device CS pin
*/
STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin);
private:
STM32GpioPin csPin_; /**< Device CS pin */
};
/** @} */
} // namespace sta
/**
* @brief Get bus info for STM32 %SPI interface via HAL handle.
*
* Requires STA_STM32_<handle>_PCLK_IDX to be defined for the MCU.
* MCU mappings are found in the sta/stm32/mcu/.hpp files.
*
* Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used.
*
* @param handle STM32 HAL %SPI handle
*
* @ingroup sta_core_stm32_spi
*/
#define STA_STM32_SPI_INFO(handle) sta::STM32SPI::Info{&handle, STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle)()}
/**
* @brief Get bus info for STM32 %SPI interface via index.
*
* Requires STA_STM32_SPI_<n>_PCLK_IDX to be defined for the MCU.
* MCU mappings are found in the sta/stm32/mcu/.hpp files.
*
* Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used.
*
* @param n STM32 %SPI interface index
*
* @ingroup sta_core_stm32_spi
*/
#define STA_STM32_SPI_INFO_N(n) sta::STM32SPI::Info{&handle, STA_STM32_GET_SPI_PCLK_FREQ_FN(n)()}
#endif // STA_STM32_SPI_ENABLED
#endif // STA_CORE_STM32_SPI_HPP

View File

@ -0,0 +1,56 @@
/**
* @file
* @brief Implementation of UART using STM32 HAL.
*/
#ifndef STA_CORE_STM32_UART_HPP
#define STA_CORE_STM32_UART_HPP
// Only enable module on STM32 platform w/ HAL UART module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# ifdef HAL_UART_MODULE_ENABLED
# define STA_STM32_UART_ENABLED
# endif // HAL_UART_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#if defined(STA_STM32_UART_ENABLED) || defined(DOXYGEN)
#include <sta/uart.hpp>
/**
* @defgroup sta_core_stm32_uart UART
* @ingroup sta_core_stm32
* @brief STM32 UART module.
*/
namespace sta
{
/**
* @brief Implementation of UART interface using HAL.
*
* @ingroup sta_core_stm32_uart
*/
class STM32UART : public UART
{
public:
/**
* @param handle STM32 HAL handle
*/
STM32UART(UART_HandleTypeDef * handle);
void write(const uint8_t * buffer, size_t size) override;
private:
UART_HandleTypeDef * handle_; /**< STM32 HAL handle */
};
} // namespace sta
#endif // STA_STM32_UART_ENABLED
#endif // STA_CORE_STM32_UART_HPP

28
include/sta/time.hpp Normal file
View File

@ -0,0 +1,28 @@
/**
* @file
* @brief Signatures for time related functions.
*/
#ifndef STA_CORE_TIME_HPP
#define STA_CORE_TIME_HPP
#include <cstdint>
namespace sta
{
/**
* @brief Signature for millisecond precision time.
*
* @return Time in milliseconds
*/
using TimeMsFn = uint32_t (*)();
/**
* @brief Signature for microseconds precision time.
*
* @return Time in microseconds
*/
using TimeUsFn = uint32_t (*)();
} // namespace sta
#endif // STA_CORE_TIME_HPP

59
include/sta/uart.hpp Normal file
View File

@ -0,0 +1,59 @@
/**
* @file
* @brief UART interface definition.
*/
#ifndef STA_CORE_UART_HPP
#define STA_CORE_UART_HPP
#include <cstddef>
#include <cstdint>
/**
* @defgroup sta_core_uart UART
* @ingroup sta_core
* @brief UART interface.
*/
namespace sta
{
/**
* @brief Interface for %UART.
*
* @ingroup sta_core_uart
*/
class UART
{
public:
/**
* @brief Write buffer to %UART.
*
* @param buffer Source buffer
* @param size Number of bytes in buffer
*/
virtual void write(const uint8_t * buffer, size_t size) = 0;
/**
* @brief Write unsigned integer to %UART.
*
* @param value Unsigned integer value
*/
void write(uint8_t value);
/**
* @brief Write unsigned integer to %UART.
*
* @param value Unsigned integer value
*/
void write(uint16_t value);
/**
* @brief Write unsigned integer to %UART.
*
* @param value Unsigned integer value
*/
void write(uint32_t value);
};
} // namespace sta
#endif // STA_CORE_UART_HPP

View File

@ -1,6 +1,6 @@
{
"owner" : "sta",
"name": "sta-core",
"version": "0.1.0",
"version": "1.0.0",
"dependencies": []
}

View File

@ -1,5 +1,5 @@
#include <sta/assert.hpp>
#ifdef STA_ASSERT_ENABLE
#ifdef STA_ASSERT_ENABLED
#include <sta/debug_serial.hpp>
#include <sta/lang.hpp>
@ -7,24 +7,24 @@
namespace sta
{
STA_WEAK
void assert_failed(const char * expr, const char * file, uint32_t line)
{
// printf("%s:%d: Assertion failed: %s", file, line, expr)
STA_DEBUG_PRINT(file);
STA_DEBUG_PRINT(':');
STA_DEBUG_PRINT(line);
STA_DEBUG_PRINT(": Assertion failed: ");
STA_DEBUG_PRINTLN(expr);
}
STA_WEAK
void assert_failed(const char * expr, const char * file, uint32_t line)
{
// printf("%s:%d: Assertion failed: %s", file, line, expr)
STA_DEBUG_PRINT(file);
STA_DEBUG_PRINT(':');
STA_DEBUG_PRINT(line);
STA_DEBUG_PRINT(": Assertion failed: ");
STA_DEBUG_PRINTLN(expr);
}
STA_WEAK
void assert_halt()
{
STA_BKPT();
while (true);
}
STA_WEAK
void assert_halt()
{
STA_BKPT();
while (true);
}
} // namespace sta
#endif // STA_ASSERT_ENABLE
#endif // STA_ASSERT_ENABLED

View File

@ -1,23 +1,23 @@
#include <sta/atomic/mutex.hpp>
#ifdef STA_ATOMIC_ENABLE
#ifdef STA_ATOMIC_ENABLED
namespace sta
{
AtomicMutex::AtomicMutex()
: lock_{ATOMIC_FLAG_INIT}
{}
AtomicMutex::AtomicMutex()
: lock_{ATOMIC_FLAG_INIT}
{}
void AtomicMutex::acquire()
{
while (lock_.test_and_set());
}
void AtomicMutex::acquire()
{
while (lock_.test_and_set());
}
void AtomicMutex::release()
{
lock_.clear();
}
void AtomicMutex::release()
{
lock_.clear();
}
} // namespace sta
#endif // STA_ATOMIC_ENABLE
#endif // STA_ATOMIC_ENABLED

View File

@ -1,33 +1,33 @@
#include <sta/atomic/signal.hpp>
#ifdef STA_ATOMIC_ENABLE
#ifdef STA_ATOMIC_ENABLED
namespace sta
{
AtomicSignal::AtomicSignal()
: signal_{false}
{}
AtomicSignal::AtomicSignal()
: signal_{false}
{}
void AtomicSignal::notify()
{
signal_.store(true);
}
void AtomicSignal::notify()
{
signal_.store(true);
}
bool AtomicSignal::peek()
{
return signal_.load();
}
bool AtomicSignal::peek()
{
return signal_.load();
}
bool AtomicSignal::test()
{
return signal_.exchange(false);
}
bool AtomicSignal::test()
{
return signal_.exchange(false);
}
void AtomicSignal::wait()
{
while (!signal_.exchange(false));
}
void AtomicSignal::wait()
{
while (!signal_.exchange(false));
}
} // namespace sta
#endif // STA_ATOMIC_ENABLE
#endif // STA_ATOMIC_ENABLED

View File

@ -3,24 +3,24 @@
namespace sta
{
bool operator ==(const CanId & lhs, const CanId & rhs)
{
return (lhs.sid == rhs.sid && lhs.eid == rhs.eid);
}
bool operator ==(const CanId & lhs, const CanId & rhs)
{
return (lhs.sid == rhs.sid && lhs.eid == rhs.eid);
}
bool operator !=(const CanId & lhs, const CanId & rhs)
{
return !(lhs == rhs);
}
bool operator !=(const CanId & lhs, const CanId & rhs)
{
return !(lhs == rhs);
}
bool operator ==(const CanFrameId & lhs, const CanFrameId & rhs)
{
return (lhs.format == rhs.format && lhs.sid == rhs.sid && lhs.eid == rhs.eid);
}
bool operator ==(const CanFrameId & lhs, const CanFrameId & rhs)
{
return (lhs.format == rhs.format && lhs.sid == rhs.sid && lhs.eid == rhs.eid);
}
bool operator !=(const CanFrameId & lhs, const CanFrameId & rhs)
{
return !(lhs == rhs);
}
bool operator !=(const CanFrameId & lhs, const CanFrameId & rhs)
{
return !(lhs == rhs);
}
} // namespace sta

View File

@ -5,91 +5,91 @@
namespace sta
{
CanPendingRxFifos::const_iterator::const_iterator(uint32_t rxFlags, uint8_t idx, uint8_t endIdx)
: rxFlags_{rxFlags}, idx_{idx}, endIdx_{endIdx}
{}
CanPendingRxFifos::const_iterator::const_iterator(uint32_t rxFlags, uint8_t idx, uint8_t endIdx)
: rxFlags_{rxFlags}, idx_{idx}, endIdx_{endIdx}
{}
CanPendingRxFifos::const_iterator::const_iterator(const const_iterator & iter)
: rxFlags_{iter.rxFlags_}, idx_{iter.idx_}, endIdx_{iter.endIdx_}
{}
CanPendingRxFifos::const_iterator::const_iterator(const const_iterator & iter)
: rxFlags_{iter.rxFlags_}, idx_{iter.idx_}, endIdx_{iter.endIdx_}
{}
CanPendingRxFifos::const_iterator & CanPendingRxFifos::const_iterator::operator=(const const_iterator & iter)
{
rxFlags_ = iter.rxFlags_;
idx_ = iter.idx_;
endIdx_ = iter.endIdx_;
CanPendingRxFifos::const_iterator & CanPendingRxFifos::const_iterator::operator=(const const_iterator & iter)
{
rxFlags_ = iter.rxFlags_;
idx_ = iter.idx_;
endIdx_ = iter.endIdx_;
return *this;
}
return *this;
}
bool CanPendingRxFifos::const_iterator::operator==(const const_iterator & iter) const
{
return (rxFlags_ == iter.rxFlags_) && (idx_ == iter.idx_) && (endIdx_ == iter.endIdx_);
}
bool CanPendingRxFifos::const_iterator::operator==(const const_iterator & iter) const
{
return (rxFlags_ == iter.rxFlags_) && (idx_ == iter.idx_) && (endIdx_ == iter.endIdx_);
}
bool CanPendingRxFifos::const_iterator::operator!=(const const_iterator & iter) const
{
return !(*this == iter);
}
bool CanPendingRxFifos::const_iterator::operator!=(const const_iterator & iter) const
{
return !(*this == iter);
}
CanPendingRxFifos::const_iterator & CanPendingRxFifos::const_iterator::operator++()
{
while (idx_ < endIdx_)
{
++idx_;
if (isRxPending())
{
break;
}
}
CanPendingRxFifos::const_iterator & CanPendingRxFifos::const_iterator::operator++()
{
while (idx_ < endIdx_)
{
++idx_;
if (isRxPending())
{
break;
}
}
return *this;
}
return *this;
}
CanPendingRxFifos::const_iterator CanPendingRxFifos::const_iterator::operator++(int)
{
uint8_t oldIdx = idx_;
CanPendingRxFifos::const_iterator CanPendingRxFifos::const_iterator::operator++(int)
{
uint8_t oldIdx = idx_;
while (idx_ < endIdx_)
{
++idx_;
if (isRxPending())
{
break;
}
}
while (idx_ < endIdx_)
{
++idx_;
if (isRxPending())
{
break;
}
}
return const_iterator(rxFlags_, oldIdx, endIdx_);
}
return const_iterator(rxFlags_, oldIdx, endIdx_);
}
CanPendingRxFifos::const_iterator::reference CanPendingRxFifos::const_iterator::operator*() const
{
STA_ASSERT_MSG(idx_ != endIdx_, "Dereferencing out-of-bounds iterator");
CanPendingRxFifos::const_iterator::reference CanPendingRxFifos::const_iterator::operator*() const
{
STA_ASSERT_MSG(idx_ != endIdx_, "Dereferencing out-of-bounds iterator");
return idx_;
}
return idx_;
}
bool CanPendingRxFifos::const_iterator::isRxPending() const
{
return ( (rxFlags_ >> idx_) & 0x1 );
}
bool CanPendingRxFifos::const_iterator::isRxPending() const
{
return ( (rxFlags_ >> idx_) & 0x1 );
}
CanPendingRxFifos::CanPendingRxFifos(uint32_t rxFlags, uint8_t numFifos)
: rxFlags_{rxFlags}, numFifos_{numFifos}
{}
CanPendingRxFifos::CanPendingRxFifos(uint32_t rxFlags, uint8_t numFifos)
: rxFlags_{rxFlags}, numFifos_{numFifos}
{}
CanPendingRxFifos::const_iterator CanPendingRxFifos::begin() const
{
return const_iterator(rxFlags_, 0, numFifos_);
}
CanPendingRxFifos::const_iterator CanPendingRxFifos::begin() const
{
return const_iterator(rxFlags_, 0, numFifos_);
}
CanPendingRxFifos::const_iterator CanPendingRxFifos::end() const
{
return const_iterator(rxFlags_, numFifos_, numFifos_);
}
CanPendingRxFifos::const_iterator CanPendingRxFifos::end() const
{
return const_iterator(rxFlags_, numFifos_, numFifos_);
}
} // namespace sta

30
src/debug_serial.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <sta/debug_serial.hpp>
#ifdef STA_DEBUG_SERIAL_ENABLED
#ifdef STA_PLATFORM_STM32
#include <sta/stm32/uart.hpp>
#include <usart.h>
// Set platform UART alias
using PlatformUART = sta::STM32UART;
#endif // STA_PLATFORM_STM32
namespace
{
// Create platform specific serial interface
PlatformUART platformDebugSerial(&STA_DEBUG_SERIAL_UART);
}
namespace sta
{
// Create debug serial object using platform specific serial interface
PrintableUART DebugSerial(&platformDebugSerial);
} // namespace sta
#endif // STA_DEBUG_SERIAL_ENABLED

22
src/i2c.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <sta/i2c.hpp>
namespace sta {
I2cDevice::I2cDevice(uint16_t address_7bit, Mutex* mutex, bool master, bool blocking) {
this->address = address_7bit << 1;
this->mutex = mutex;
this->master = master;
this->blocking = blocking;
}
void I2cDevice::acquire() {
if (this->mutex != nullptr) {
mutex->acquire();
}
}
void I2cDevice::release() {
if (this->mutex != nullptr) {
mutex->release();
}
}
}

View File

@ -1,20 +0,0 @@
#include <sta/intf/mutex.hpp>
namespace sta
{
/**
* @brief Dummy mutex implementation with no access control.
*/
class DummyMutex : public Mutex
{
public:
void acquire() override {}
void release() override {}
};
static DummyMutex dummyMutex;
Mutex * Mutex::ALWAYS_FREE = &dummyMutex;
} // namespace sta

View File

@ -1,29 +0,0 @@
#include <sta/intf/uart.hpp>
#include <sta/printf.hpp>
#include <cinttypes>
#include <cstring>
namespace sta
{
void UART::write(uint8_t value)
{
// TODO Handle endian-ness
write(&value, 1);
}
void UART::write(uint16_t value)
{
// TODO Handle endian-ness
write(reinterpret_cast<uint8_t *>(&value), sizeof(value));
}
void UART::write(uint32_t value)
{
// TODO Handle endian-ness
write(reinterpret_cast<uint8_t *>(&value), sizeof(value));
}
} // namespace sta

20
src/mutex.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <sta/mutex.hpp>
namespace sta
{
/**
* @brief Dummy mutex implementation with no access control.
*/
class DummyMutex : public Mutex
{
public:
void acquire() override {}
void release() override {}
};
static DummyMutex dummyMutex;
Mutex * Mutex::ALWAYS_FREE = &dummyMutex;
} // namespace sta

View File

@ -10,202 +10,202 @@
namespace sta
{
PrintableUART::PrintableUART(UART * intf)
: intf_{intf}
{
STA_ASSERT(intf != nullptr);
}
PrintableUART::PrintableUART(UART * intf)
: intf_{intf}
{
STA_ASSERT(intf != nullptr);
}
void PrintableUART::print(char c)
{
print(&c, 1);
}
void PrintableUART::print(char c)
{
print(&c, 1);
}
void PrintableUART::print(bool b)
{
print(b ? "true" : "false");
}
void PrintableUART::print(bool b)
{
print(b ? "true" : "false");
}
void PrintableUART::print(double d)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "%f", d);
print(buffer);
}
void PrintableUART::print(double d)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "%f", d);
print(buffer);
}
void PrintableUART::print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu8, sizeof(num));
}
void PrintableUART::print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu8, sizeof(num));
}
void PrintableUART::print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu16, sizeof(num));
}
void PrintableUART::print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu16, sizeof(num));
}
void PrintableUART::print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu32, sizeof(num));
}
void PrintableUART::print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu32, sizeof(num));
}
void PrintableUART::print(size_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%z", sizeof(num));
}
void PrintableUART::print(size_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%z", sizeof(num));
}
void PrintableUART::print(const char * str)
{
print(str, strlen(str));
}
void PrintableUART::print(const char * str)
{
print(str, strlen(str));
}
void PrintableUART::print(const char * str, size_t length)
{
intf_->write(reinterpret_cast<const uint8_t *>(str), length);
}
void PrintableUART::print(const char * str, size_t length)
{
intf_->write(reinterpret_cast<const uint8_t *>(str), length);
}
void PrintableUART::println()
{
print("\r\n", 2);
}
void PrintableUART::println()
{
print("\r\n", 2);
}
void PrintableUART::println(char c)
{
print(&c, 1);
println();
}
void PrintableUART::println(char c)
{
print(&c, 1);
println();
}
void PrintableUART::println(bool b)
{
print(b);
println();
}
void PrintableUART::println(bool b)
{
print(b);
println();
}
void PrintableUART::println(double d)
{
print(d);
println();
}
void PrintableUART::println(double d)
{
print(d);
println();
}
void PrintableUART::println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(size_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(size_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void PrintableUART::println(const char * str)
{
println(str, strlen(str));
}
void PrintableUART::println(const char * str)
{
println(str, strlen(str));
}
void PrintableUART::println(const char * str, size_t length)
{
print(str, length);
println();
}
void PrintableUART::println(const char * str, size_t length)
{
print(str, length);
println();
}
void PrintableUART::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
{
switch (base)
{
case IntegerBase::DEC:
printDec(num, fmt);
break;
void PrintableUART::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
{
switch (base)
{
case IntegerBase::DEC:
printDec(num, fmt);
break;
case IntegerBase::BIN:
// Digits in base 2 = size in bytes * 8
printBin(num, size * 8);
break;
case IntegerBase::BIN:
// Digits in base 2 = size in bytes * 8
printBin(num, size * 8);
break;
case IntegerBase::HEX:
// Digits in base 16 = size in bytes * 2
printHex(num, size * 2);
break;
case IntegerBase::HEX:
// Digits in base 16 = size in bytes * 2
printHex(num, size * 2);
break;
default:
print("<invalid_base>");
}
}
default:
print("<invalid_base>");
}
}
void PrintableUART::printDec(uintmax_t num, const char * fmt)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
print(buffer);
}
void PrintableUART::printDec(uintmax_t num, const char * fmt)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
print(buffer);
}
void PrintableUART::printBin(uintmax_t value, size_t digits)
{
// Need 8 digits for every byte
char buffer[sizeof(value) * 8];
void PrintableUART::printBin(uintmax_t value, size_t digits)
{
// Need 8 digits for every byte
char buffer[sizeof(value) * 8];
// Check bounds
if (digits > sizeof(buffer))
{
print("<bin_value_too_big>");
return;
}
// Nothing to do
if (digits == 0)
return;
// Check bounds
if (digits > sizeof(buffer))
{
print("<bin_value_too_big>");
return;
}
// Nothing to do
if (digits == 0)
return;
for (size_t i = 0; i < digits; ++i)
{
// Convert bit to '0' or '1'
// First digit in buffer is MSB in value, so shift from high to low
buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1);
}
for (size_t i = 0; i < digits; ++i)
{
// Convert bit to '0' or '1'
// First digit in buffer is MSB in value, so shift from high to low
buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1);
}
print(buffer, digits);
}
print(buffer, digits);
}
void PrintableUART::printHex(uintmax_t value, size_t digits)
{
// Need 2 digits for every byte
char buffer[sizeof(value) * 2];
void PrintableUART::printHex(uintmax_t value, size_t digits)
{
// Need 2 digits for every byte
char buffer[sizeof(value) * 2];
// Check bounds
if (digits > sizeof(buffer))
{
print("<hex_value_too_big>");
return;
}
// Nothing to do
if (digits == 0)
return;
// Check bounds
if (digits > sizeof(buffer))
{
print("<hex_value_too_big>");
return;
}
// Nothing to do
if (digits == 0)
return;
for (size_t i = 0; i < digits; ++i)
{
// Convert 4 bits to hex
// First digit in buffer is 4 MSBs in value, so shift from high to low
uint8_t hex = ((value >> ((digits - 1 - i) * 4)) & 0xF);
if (hex > 9)
buffer[i] = 'A' + (hex - 10);
else
buffer[i] = '0' + hex;
}
for (size_t i = 0; i < digits; ++i)
{
// Convert 4 bits to hex
// First digit in buffer is 4 MSBs in value, so shift from high to low
uint8_t hex = ((value >> ((digits - 1 - i) * 4)) & 0xF);
if (hex > 9)
buffer[i] = 'A' + (hex - 10);
else
buffer[i] = '0' + hex;
}
print(buffer, digits);
}
print(buffer, digits);
}
} // namespace sta

View File

@ -5,84 +5,84 @@
namespace sta
{
SpiDevice::SpiDevice(SpiInterface * intf, GpioPin * csPin)
: intf_{intf}, csPin_{csPin}
{
STA_ASSERT(intf != nullptr);
STA_ASSERT(csPin != nullptr);
}
SPIDevice::SPIDevice(SPI * intf, GpioPin * csPin)
: intf_{intf}, csPin_{csPin}
{
STA_ASSERT(intf != nullptr);
STA_ASSERT(csPin != nullptr);
}
void SpiDevice::beginTransmission()
{
// Acquire SPI access and activate device
intf_->acquire();
select();
}
void SPIDevice::beginTransmission()
{
// Acquire SPI access and activate device
intf_->acquire();
select();
}
void SpiDevice::endTransmission()
{
// Deactivate device and release SPI access
deselect();
intf_->release();
}
void SPIDevice::endTransmission()
{
// Deactivate device and release SPI access
deselect();
intf_->release();
}
// Forward I/O operations to SPI interface
// Forward I/O operations to SPI interface
void SpiDevice::transfer(uint8_t data)
{
intf_->transfer(data);
}
void SPIDevice::transfer(uint8_t data)
{
intf_->transfer(data);
}
void SpiDevice::transfer16(uint16_t data)
{
intf_->transfer16(data);
}
void SPIDevice::transfer16(uint16_t data)
{
intf_->transfer16(data);
}
void SpiDevice::transfer(const uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
void SPIDevice::transfer(const uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
intf_->transfer(buffer, size);
}
intf_->transfer(buffer, size);
}
void SpiDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr);
STA_ASSERT(size != 0);
void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr);
STA_ASSERT(size != 0);
intf_->transfer(txBuffer, rxBuffer, size);
}
intf_->transfer(txBuffer, rxBuffer, size);
}
void SpiDevice::receive(uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
void SPIDevice::receive(uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
intf_->receive(buffer, size);
}
intf_->receive(buffer, size);
}
void SpiDevice::fill(uint8_t value, size_t count)
{
STA_ASSERT(count != 0);
void SPIDevice::fill(uint8_t value, size_t count)
{
STA_ASSERT(count != 0);
intf_->fill(value, count);
}
intf_->fill(value, count);
}
const SpiSettings & SpiDevice::settings() const
{
return intf_->settings();
}
const SpiSettings & SPIDevice::settings() const
{
return intf_->settings();
}
void SpiDevice::select()
{
csPin_->setState(GpioPinState::LOW);
}
void SPIDevice::select()
{
csPin_->setState(GpioPinState::LOW);
}
void SpiDevice::deselect()
{
csPin_->setState(GpioPinState::HIGH);
}
void SPIDevice::deselect()
{
csPin_->setState(GpioPinState::HIGH);
}
} // namespace sta

View File

@ -1,21 +0,0 @@
#include <sta/spi/interface.hpp>
namespace sta
{
SpiInterface::SpiInterface(Mutex * mutex /* = nullptr */)
: mutex_{mutex}
{}
void SpiInterface::acquire()
{
if (mutex_ != nullptr)
mutex_->acquire();
}
void SpiInterface::release()
{
if (mutex_ != nullptr)
mutex_->release();
}
} // namespace sta

View File

@ -6,67 +6,67 @@
namespace sta
{
SpiClkPolarity getSpiClkPolarity(SpiMode mode)
{
switch (mode)
{
case SpiMode::MODE_0:
case SpiMode::MODE_1:
return SpiClkPolarity::LOW;
SPIClkPolarity getSPIClkPolarity(SPIMode mode)
{
switch (mode)
{
case SPIMode::MODE_0:
case SPIMode::MODE_1:
return SPIClkPolarity::LOW;
case SpiMode::MODE_2:
case SpiMode::MODE_3:
return SpiClkPolarity::HIGH;
case SPIMode::MODE_2:
case SPIMode::MODE_3:
return SPIClkPolarity::HIGH;
default:
// Unreachable case
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled");
STA_UNREACHABLE();
}
}
default:
// Unreachable case
STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
STA_UNREACHABLE();
}
}
SpiClkPhase getSpiClkPhase(SpiMode mode)
{
switch (mode)
{
case SpiMode::MODE_0:
case SpiMode::MODE_2:
return SpiClkPhase::EDGE_1;
SPIClkPhase getSPIClkPhase(SPIMode mode)
{
switch (mode)
{
case SPIMode::MODE_0:
case SPIMode::MODE_2:
return SPIClkPhase::EDGE_1;
case SpiMode::MODE_1:
case SpiMode::MODE_3:
return SpiClkPhase::EDGE_2;
case SPIMode::MODE_1:
case SPIMode::MODE_3:
return SPIClkPhase::EDGE_2;
default:
// Unreachable case
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled");
STA_UNREACHABLE();
}
}
default:
// Unreachable case
STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
STA_UNREACHABLE();
}
}
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase)
{
if (polarity == SpiClkPolarity::LOW)
{
if (phase == SpiClkPhase::EDGE_1)
{
return SpiMode::MODE_0;
}
else
{
return SpiMode::MODE_1;
}
}
else
{
if (phase == SpiClkPhase::EDGE_1)
{
return SpiMode::MODE_2;
}
else
{
return SpiMode::MODE_3;
}
}
}
SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase)
{
if (polarity == SPIClkPolarity::LOW)
{
if (phase == SPIClkPhase::EDGE_1)
{
return SPIMode::MODE_0;
}
else
{
return SPIMode::MODE_1;
}
}
else
{
if (phase == SPIClkPhase::EDGE_1)
{
return SPIMode::MODE_2;
}
else
{
return SPIMode::MODE_3;
}
}
}
} // namespace sta

26
src/spi/spi.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <sta/spi/spi.hpp>
namespace sta
{
SPI::SPI(const SPISettings & settings, Mutex * mutex /* = nullptr */)
: settings_{settings}, mutex_{mutex}
{}
const SPISettings & SPI::settings() const
{
return settings_;
}
void SPI::acquire()
{
if (mutex_ != nullptr)
mutex_->acquire();
}
void SPI::release()
{
if (mutex_ != nullptr)
mutex_->release();
}
} // namespace sta

210
src/stm32/can.cpp Normal file
View File

@ -0,0 +1,210 @@
#include <sta/stm32/can.hpp>
#ifdef STA_STM32_CAN_ENABLED
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
STM32CanController::STM32CanController(CAN_HandleTypeDef * handle)
: handle_{handle}
{
initFilters();
}
void STM32CanController::enableRxInterrupts()
{
HAL_CAN_ActivateNotification(handle_,
CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_RX_FIFO1_MSG_PENDING
);
}
void STM32CanController::start()
{
HAL_CAN_Start(handle_);
}
void STM32CanController::stop()
{
HAL_CAN_Stop(handle_);
}
bool STM32CanController::sendFrame(const CanTxHeader & header, const uint8_t * payload)
{
STA_ASSERT_MSG(header.payloadLength <= 8, "CAN 2.0B payload size exceeded");
CAN_TxHeaderTypeDef halHeader;
if (header.id.format == CanIdFormat::STD)
{
halHeader.StdId = header.id.sid & 0x7FF;
halHeader.IDE = CAN_ID_STD;
}
else
{
// Combine SID and EID
halHeader.ExtId = ((header.id.sid & 0x7FF) << 18) | (header.id.eid & 0x3FFFF);
halHeader.IDE = CAN_ID_EXT;
}
halHeader.DLC = header.payloadLength;
uint32_t mailbox; // Don't care
return (HAL_OK == HAL_CAN_AddTxMessage(handle_, &halHeader, const_cast<uint8_t *>(payload), &mailbox));
}
bool STM32CanController::receiveFrame(uint8_t fifo, CanRxHeader * header, uint8_t * payload)
{
// Check if message is available
if (HAL_CAN_GetRxFifoFillLevel(handle_, fifo) == 0)
return false;
// Retrieve message
CAN_RxHeaderTypeDef halHeader;
HAL_CAN_GetRxMessage(handle_, fifo, &halHeader, payload);
if (halHeader.IDE == CAN_ID_STD)
{
header->id.format = CanIdFormat::STD;
header->id.sid = halHeader.StdId;
header->id.eid = 0;
}
else
{
header->id.format = CanIdFormat::EXT;
// Separate SID and EID
header->id.sid = (halHeader.ExtId >> 18);
header->id.eid = halHeader.ExtId & 0x3FFFF;
}
// No conversion required for CAN 2B standard
header->payloadLength = halHeader.DLC;
header->timestamp = halHeader.Timestamp;
header->filter = halHeader.FilterMatchIndex;
return true;
}
uint32_t STM32CanController::getRxFifoFlags()
{
//
return (HAL_CAN_GetRxFifoFillLevel(handle_, CAN_RX_FIFO0) != 0)
| (HAL_CAN_GetRxFifoFillLevel(handle_, CAN_RX_FIFO1) != 0) << 1;
}
void STM32CanController::configureFilter(uint8_t idx, const CanFilter & filter, bool active /* = false */)
{
CAN_FilterTypeDef * config = &filters_[idx];
if (filter.type == CanFilterIdFormat::STD)
{
config->FilterIdHigh = 0;
config->FilterIdLow = filter.obj.sid & 0x7FF;
config->FilterMaskIdHigh = 0;
config->FilterMaskIdLow = filter.mask.sid & 0x7FF;
}
else
{
config->FilterIdHigh = ((filter.obj.sid & 0x7FF) << 2) | ((filter.obj.eid >> 16) & 0x3);
config->FilterIdLow = filter.obj.eid & 0xFFFF;
config->FilterMaskIdHigh = ((filter.mask.sid & 0x7FF) << 2) | ((filter.mask.eid >> 16) & 0x3);
config->FilterMaskIdLow = filter.mask.eid & 0xFFFF;
}
config->FilterFIFOAssignment = filter.fifo;
config->FilterActivation = (active ? CAN_FILTER_ENABLE : CAN_FILTER_DISABLE);
HAL_CAN_ConfigFilter(handle_, config);
}
void STM32CanController::enableFilter(uint8_t idx)
{
CAN_FilterTypeDef * config = &filters_[idx];
config->FilterActivation = CAN_FILTER_ENABLE;
HAL_CAN_ConfigFilter(handle_, config);
}
void STM32CanController::disableFilter(uint8_t idx)
{
CAN_FilterTypeDef * config = &filters_[idx];
config->FilterActivation = CAN_FILTER_DISABLE;
HAL_CAN_ConfigFilter(handle_, config);
}
void STM32CanController::clearFilters()
{
for (uint32_t i = 0; i < MAX_FILTER_COUNT; ++i)
{
CAN_FilterTypeDef * config = &filters_[i];
// Only disable active filters
if (config->FilterActivation == CAN_FILTER_ENABLE)
{
config->FilterActivation = CAN_FILTER_DISABLE;
HAL_CAN_ConfigFilter(handle_, config);
}
}
}
void STM32CanController::initFilters()
{
for (uint32_t i = 0; i < MAX_FILTER_COUNT; ++i)
{
CAN_FilterTypeDef * config = &filters_[i];
config->FilterBank = i;
config->FilterMode = CAN_FILTERMODE_IDMASK;
config->FilterScale = CAN_FILTERSCALE_32BIT;
config->FilterActivation = CAN_FILTER_DISABLE;
config->SlaveStartFilterBank = MAX_FILTER_COUNT;
}
}
} // namespace sta
#ifdef STA_STM32_CAN_GLOBAL
#include <can.h>
namespace sta
{
STM32CanController CanBus(&STA_STM32_CAN_GLOBAL);
STA_WEAK
void CanBus_RxPendingCallback()
{}
} // namespace sta
extern "C"
{
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if (hcan == &STA_STM32_CAN_GLOBAL)
{
sta::CanBus_RxPendingCallback();
}
}
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if (hcan == &STA_STM32_CAN_GLOBAL)
{
sta::CanBus_RxPendingCallback();
}
}
}
#endif // STA_STM32_CAN_GLOBAL
#endif // STA_STM32_CAN_ENABLED

75
src/stm32/delay.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <sta/stm32/delay.hpp>
#ifdef STA_PLATFORM_STM32
#include <sta/stm32/hal.hpp>
#include <sta/stm32/clocks.hpp>
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
void delayMs(uint32_t ms)
{
HAL_Delay(ms);
}
} // namespace sta
#ifdef STA_STM32_DELAY_US_TIM
#ifndef HAL_TIM_MODULE_ENABLED
# error "STM32 HAL TIM module not enabled!"
#endif // HAL_TIM_MODULE_ENABLED
#include <tim.h>
namespace sta
{
uint32_t gDelayUsMul = 1;
void delayUs(uint32_t us)
{
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
}
bool isValidDelayUsTIM()
{
// Get PCLK multiplier for TIM clock
uint32_t pclkMul = 1;
switch (STA_STM32_DELAY_US_TIM.Init.ClockDivision)
{
case TIM_CLOCKDIVISION_DIV1:
pclkMul = 1;
break;
case TIM_CLOCKDIVISION_DIV2:
pclkMul = 2;
break;
case TIM_CLOCKDIVISION_DIV4:
pclkMul = 4;
break;
default:
STA_ASSERT(false);
STA_UNREACHABLE();
}
// Calculate TIM clock frequency
uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)();
// Calculate update frequency based on prescaler value
uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1;
uint32_t updateFreq = clkFreq / prescaler;
gDelayUsMul = updateFreq / 1000000;
// TIM must have at least microsecond precision (>= 1 MHz frequency)
return (updateFreq >= 1000000);
}
} // namespace sta
#endif // STA_STM32_DELAY_US_TIM
#endif // STA_PLATFORM_STM32

83
src/stm32/gpio_pin.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <sta/stm32/gpio_pin.hpp>
#ifdef STA_STM32_GPIO_ENABLED
#include <sta/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
STM32GpioPin::STM32GpioPin(GPIO_TypeDef * port, uint16_t pin)
: port_{port}, pin_{pin}
{
STA_ASSERT(port != nullptr);
}
void STM32GpioPin::setState(GpioPinState state)
{
HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
GPIO_TypeDef * STM32GpioPin::getPort() const
{
return port_;
}
uint16_t STM32GpioPin::getPin() const
{
return pin_;
}
uint8_t STM32GpioPin::getPortIndex() const
{
return GPIO_GET_INDEX(port_);
}
bool isInterruptEdge(const STM32GpioPin & gpioPin, InterruptEdge edge)
{
uint32_t pin = gpioPin.getPin();
for (uint32_t i = 0; i < 8 * sizeof(pin); ++i)
{
uint32_t ioPos = 1U << i;
if (pin & ioPos)
{
// Check input mode
uint32_t mode = (gpioPin.getPort()->MODER >> (2U * i)) & GPIO_MODE;
if (mode != MODE_INPUT)
{
return false;
}
// Is EXTI configured?
if (EXTI->IMR & ioPos)
{
bool rising = (EXTI->RTSR & ioPos);
bool falling = (EXTI->FTSR & ioPos);
switch (edge)
{
case InterruptEdge::RISING:
return rising;
case InterruptEdge::FALLING:
return falling;
case InterruptEdge::BOTH:
return rising && falling;
default:
STA_ASSERT(false);
STA_UNREACHABLE();
}
}
}
}
return false;
}
} // namespace sta
#endif // STA_STM32_GPIO_ENABLED

53
src/stm32/i2c.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <sta/stm32/i2c.hpp>
namespace sta {
STM32I2cDevice::STM32I2cDevice(I2C_HandleTypeDef* i2cHandle, uint16_t address, Mutex* mutex, bool master, bool blocking)
: I2cDevice(address, mutex, master, blocking) {
this->master = master;
}
bool STM32I2cDevice::transmit(uint8_t* data, uint16_t size) {
HAL_StatusTypeDef res;
if (this->blocking) {
if (!this->master) {
res = HAL_I2C_Master_Transmit(i2cHandle, address, data, size, this->timeout);
} else {
res = HAL_I2C_Slave_Transmit(i2cHandle , data, size, this->timeout);
}
} else {
if (!this->master) {
res = HAL_I2C_Master_Transmit_IT(i2cHandle, address, data, size);
} else {
res = HAL_I2C_Slave_Transmit_IT(i2cHandle , data, size);
}
}
return res == HAL_OK;
}
bool STM32I2cDevice::receive(uint8_t* data, uint16_t size) {
HAL_StatusTypeDef res;
if (this->blocking) {
if (!this->master) {
res = HAL_I2C_Master_Receive(i2cHandle, address, data, size, this->timeout);
} else {
res = HAL_I2C_Slave_Receive(i2cHandle , data, size, this->timeout);
}
} else {
if (!this->master) {
res = HAL_I2C_Master_Receive_IT(i2cHandle, address, data, size);
} else {
res = HAL_I2C_Slave_Receive_IT(i2cHandle , data, size);
}
}
return res == HAL_OK;
}
bool STM32I2cDevice::deviceReady() {
HAL_StatusTypeDef res = HAL_I2C_IsDeviceReady(this->i2cHandle, this->address, 8, this->timeout);
return res == HAL_OK;
}
}

28
src/stm32/init.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <sta/stm32/init.hpp>
#include <sta/assert.hpp>
#ifdef STA_STM32_DELAY_US_TIM
#ifndef HAL_TIM_MODULE_ENABLED
# error "STM32 HAL TIM module not enabled!"
#endif // HAL_TIM_MODULE_ENABLED
#include <tim.h>
#endif // STA_STM32_DELAY_US_TIM
namespace sta
{
void initHAL()
{
#ifdef STA_STM32_DELAY_US_TIM
// Validate TIM used for delayUs
extern bool isValidDelayUsTIM();
STA_ASSERT(isValidDelayUsTIM());
// Start timer base
HAL_TIM_Base_Start(&STA_STM32_DELAY_US_TIM);
#endif // STA_STM32_DELAY_US_TIM
}
} // namespace sta

168
src/stm32/spi.cpp Normal file
View File

@ -0,0 +1,168 @@
#include <sta/stm32/spi.hpp>
#ifdef STA_STM32_SPI_ENABLED
#include <sta/assert.hpp>
#include <sta/endian.hpp>
#include <sta/lang.hpp>
#ifdef STA_MCU_LITTLE_ENDIAN
# define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::MSB
#elif STA_MCU_BIG_ENDIAN
# define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::LSB
#endif
namespace sta
{
static SPISettings getSPISettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq)
{
SPISettings settings;
settings.mode = getSPIMode(
(handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SPIClkPolarity::LOW : SPIClkPolarity::HIGH,
(handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SPIClkPhase::EDGE_1 : SPIClkPhase::EDGE_2
);
settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SPIDataSize::SIZE_8 : SPIDataSize::SIZE_16;
settings.bitOrder = (handle->Init.FirstBit == SPI_FIRSTBIT_MSB) ? SPIBitOrder::MSB : SPIBitOrder::LSB;
uint32_t prescaler = 1;
switch (handle->Init.BaudRatePrescaler)
{
case SPI_BAUDRATEPRESCALER_2:
prescaler = 2;
break;
case SPI_BAUDRATEPRESCALER_4:
prescaler = 4;
break;
case SPI_BAUDRATEPRESCALER_8:
prescaler = 8;
break;
case SPI_BAUDRATEPRESCALER_16:
prescaler = 16;
break;
case SPI_BAUDRATEPRESCALER_32:
prescaler = 32;
break;
case SPI_BAUDRATEPRESCALER_64:
prescaler = 64;
break;
case SPI_BAUDRATEPRESCALER_128:
prescaler = 128;
break;
case SPI_BAUDRATEPRESCALER_256:
prescaler = 256;
break;
default:
// Unreachable case
STA_ASSERT_MSG(false, "Case for SPI_BAUDRATEPRESCALER not handled");
STA_UNREACHABLE();
}
// SPI clock speed is based of PCLK
settings.clkSpeed = pclkFreq / prescaler;
return settings;
}
STM32SPI::STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr)
: SPI(getSPISettings(handle, pclkFreq), mutex), handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
STM32SPI::STM32SPI(const Info & info, Mutex * mutex /* = nullptr */)
: STM32SPI(info.handle, info.pclkFreq, mutex)
{}
void STM32SPI::transfer(uint8_t value)
{
if (settings().dataSize == SPIDataSize::SIZE_8)
{
HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
}
else
{
// Required since tx buffer is cast to uint16_t * internally
uint16_t dummy = value;
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
}
}
void STM32SPI::transfer16(uint16_t value)
{
uint16_t size = 1;
// Send as two bytes if data size is 8-bit
if (settings().dataSize == SPIDataSize::SIZE_8)
{
size = 2;
if (settings().bitOrder == STA_STM32_SPI_REVERSE_BIT_ORDER)
{
// Reverse byte order from internal representation
value = STA_UINT16_SWAP_BYTE_ORDER(value);
}
}
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), size, HAL_MAX_DELAY);
}
void STM32SPI::transfer(const uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
STA_ASSERT(size != 0);
HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
}
void STM32SPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr);
STA_ASSERT(size != 0);
HAL_SPI_TransmitReceive(handle_, const_cast<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY);
}
void STM32SPI::receive(uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
HAL_SPI_Receive(handle_, buffer, size, HAL_MAX_DELAY);
}
void STM32SPI::fill(uint8_t value, size_t count)
{
STA_ASSERT(count != 0);
if (settings().dataSize == SPIDataSize::SIZE_8)
{
for (size_t i = 0; i < count; ++i)
{
HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
}
}
else
{
// Required since tx buffer is cast to uint16_t * internally
uint16_t dummy = value;
for (size_t i = 0; i < count; ++i)
{
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
}
}
}
STM32SPIDevice::STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin)
: SPIDevice(intf, &csPin_), csPin_{csPin}
{}
} // namespace sta
#endif // STA_STM32_SPI_ENABLED

25
src/stm32/uart.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <sta/stm32/uart.hpp>
#ifdef STA_STM32_UART_ENABLED
#include <sta/assert.hpp>
namespace sta
{
STM32UART::STM32UART(UART_HandleTypeDef * handle)
: handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
void STM32UART::write(const uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
}
} // namespace sta
#endif // STA_STM32_UART_ENABLED

29
src/uart.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <sta/uart.hpp>
#include <sta/printf.hpp>
#include <cinttypes>
#include <cstring>
namespace sta
{
void UART::write(uint8_t value)
{
// TODO Handle endian-ness
write(&value, 1);
}
void UART::write(uint16_t value)
{
// TODO Handle endian-ness
write(reinterpret_cast<uint8_t *>(&value), sizeof(value));
}
void UART::write(uint32_t value)
{
// TODO Handle endian-ness
write(reinterpret_cast<uint8_t *>(&value), sizeof(value));
}
} // namespace sta