Browse Source

codestyle

Juraj Ďuďák 1 year ago
parent
commit
fa934558b9
17 changed files with 1269 additions and 944 deletions
  1. 192 0
      .clang-format
  2. 37 35
      Inc/app_bridge.h
  3. 16 16
      Inc/app_dummy.h
  4. 14 20
      Inc/mcu_platform.h
  5. 8 7
      Inc/nbus_app.h
  6. 40 39
      Inc/nbus_memory.h
  7. 1 2
      Inc/nbus_slave.h
  8. 178 167
      Inc/nbus_types.h
  9. 4 4
      Makefile
  10. 20 10
      Src/app_bridge.c
  11. 74 45
      Src/app_dummy.c
  12. 197 171
      Src/nbus_app.c
  13. 29 25
      Src/nbus_master_module.c
  14. 130 117
      Src/nbus_memory.c
  15. 32 25
      Src/nbus_slave_broadcast.c
  16. 169 151
      Src/nbus_slave_module_unicast.c
  17. 128 110
      Src/nbus_slave_sensor_unicast.c

+ 192 - 0
.clang-format

@@ -0,0 +1,192 @@
+---
+Language:        Cpp
+# BasedOnStyle:  Microsoft
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignArrayOfStructures: None
+AlignConsecutiveMacros: None
+AlignConsecutiveAssignments: None
+AlignConsecutiveBitFields: None
+AlignConsecutiveDeclarations: None
+AlignEscapedNewlines: Right
+AlignOperands:   Align
+AlignTrailingComments: true
+AllowAllArgumentsOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortEnumsOnASingleLine: false
+AllowShortBlocksOnASingleLine: Never
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortLambdasOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: Never
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: MultiLine
+AttributeMacros:
+  - __capability
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterCaseLabel:  false
+  AfterClass:      true
+  AfterControlStatement: Always
+  AfterEnum:       true
+  AfterFunction:   true
+  AfterNamespace:  true
+  AfterObjCDeclaration: true
+  AfterStruct:     true
+  AfterUnion:      false
+  AfterExternBlock: true
+  BeforeCatch:     true
+  BeforeElse:      true
+  BeforeLambdaBody: false
+  BeforeWhile:     false
+  IndentBraces:    false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeConceptDeclarations: true
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakInheritanceList: BeforeColon
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeColon
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit:     120
+CommentPragmas:  '^ IWYU pragma:'
+QualifierAlignment: Leave
+CompactNamespaces: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DeriveLineEnding: true
+DerivePointerAlignment: false
+DisableFormat:   false
+EmptyLineAfterAccessModifier: Never
+EmptyLineBeforeAccessModifier: LogicalBlock
+ExperimentalAutoDetectBinPacking: false
+PackConstructorInitializers: BinPack
+BasedOnStyle:    ''
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+AllowAllConstructorInitializersOnNextLine: true
+FixNamespaceComments: true
+ForEachMacros:
+  - foreach
+  - Q_FOREACH
+  - BOOST_FOREACH
+IfMacros:
+  - KJ_IF_MAYBE
+IncludeBlocks:   Preserve
+IncludeCategories:
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+    SortPriority:    0
+    CaseSensitive:   false
+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
+    Priority:        3
+    SortPriority:    0
+    CaseSensitive:   false
+  - Regex:           '.*'
+    Priority:        1
+    SortPriority:    0
+    CaseSensitive:   false
+IncludeIsMainRegex: '(Test)?$'
+IncludeIsMainSourceRegex: ''
+IndentAccessModifiers: false
+IndentCaseLabels: false
+IndentCaseBlocks: false
+IndentGotoLabels: true
+IndentPPDirectives: None
+IndentExternBlock: AfterExternBlock
+IndentRequires:  false
+IndentWidth:     4
+IndentWrappedFunctionNames: false
+InsertTrailingCommas: None
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+LambdaBodyIndentation: Signature
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 2
+ObjCBreakBeforeNestedBlockParam: true
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakOpenParenthesis: 0
+PenaltyBreakString: 1000
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 1000
+PenaltyIndentedWhitespace: 0
+PointerAlignment: Right
+PPIndentWidth:   -1
+ReferenceAlignment: Pointer
+ReflowComments:  true
+RemoveBracesLLVM: false
+SeparateDefinitionBlocks: Leave
+ShortNamespaceLines: 1
+SortIncludes:    CaseSensitive
+SortJavaStaticImport: Before
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeParensOptions:
+  AfterControlStatements: true
+  AfterForeachMacros: true
+  AfterFunctionDefinitionName: false
+  AfterFunctionDeclarationName: false
+  AfterIfMacros:   true
+  AfterOverloadedOperator: false
+  BeforeNonEmptyParentheses: false
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  Never
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInLineCommentPrefix:
+  Minimum:         1
+  Maximum:         -1
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+SpaceBeforeSquareBrackets: false
+BitFieldColonSpacing: Both
+Standard:        Latest
+StatementAttributeLikeMacros:
+  - Q_EMIT
+StatementMacros:
+  - Q_UNUSED
+  - QT_REQUIRE_VERSION
+TabWidth:        4
+UseCRLF:         false
+UseTab:          Never
+WhitespaceSensitiveMacros:
+  - STRINGIZE
+  - PP_STRINGIZE
+  - BOOST_PP_STRINGIZE
+  - NS_SWIFT_NAME
+  - CF_SWIFT_NAME
+...
+

+ 37 - 35
Inc/app_bridge.h

@@ -10,28 +10,30 @@
 
 #include "mcu_platform.h"
 
-typedef enum {
-  TYPE_UNKNOWN = 0xFF,
-  TYPE_ACCELEROMETER = 0,
-  TYPE_GYROSCOPE,
-  TYPE_MAGNETOMETER,
-  TYPE_TEMPERATURE,
-  TYPE_HUMIDITY,
-  TYPE_PRESSURE,
-  TYPE_HEART_RATE,
-  TYPE_GNOSTIC_DISTANCE,
+typedef enum
+{
+    TYPE_UNKNOWN = 0xFF,
+    TYPE_ACCELEROMETER = 0,
+    TYPE_GYROSCOPE,
+    TYPE_MAGNETOMETER,
+    TYPE_TEMPERATURE,
+    TYPE_HUMIDITY,
+    TYPE_PRESSURE,
+    TYPE_HEART_RATE,
+    TYPE_GNOSTIC_DISTANCE,
 } nBus_sensorType_t;
 
-typedef enum {
-  PARAM_NONE = 0xFF,
-  PARAM_TIMEBASE = 0,
-  PARAM_RESOLUTION,
-  PARAM_GAIN,
-  PARAM_OFFSET,
-  PARAM_SAMPLERATE,
-  PARAM_RANGE,
-  PARAM_RANGE0,
-  PARAM_FILTER
+typedef enum
+{
+    PARAM_NONE = 0xFF,
+    PARAM_TIMEBASE = 0,
+    PARAM_RESOLUTION,
+    PARAM_GAIN,
+    PARAM_OFFSET,
+    PARAM_SAMPLERATE,
+    PARAM_RANGE,
+    PARAM_RANGE0,
+    PARAM_FILTER
 } nBus_param_t;
 
 #define PARAM_VALUE_NONE 0x7FFFFFFF
@@ -42,21 +44,21 @@ uint8_t nbus_interface_allParamsCount();
 nBus_sensorType_t *nbus_interface_allTypes();
 uint8_t nbus_interface_allTypesCount();
 
-typedef struct {
-  void (*init)(void *hw_interface, void *hw_config);
-  void (*reset)();
-  nBus_sensorType_t (*getType)(uint8_t sensor_index);
-  uint8_t (*getSensorCount)();
-  uint8_t (*getData)(uint8_t sensor_index, uint8_t *data);
-  uint8_t (*setData)(uint8_t *data);
-  uint8_t (*hasParam)(uint8_t sensor_index, nBus_param_t param_name);
-  int32_t (*getParam)(uint8_t sensor_index, nBus_param_t param_name);
-  nBus_param_t (*setParam)(uint8_t sensor_index, nBus_param_t param_name,
-                           int32_t param_value);
-  void (*start)(void);
-  void (*stop)(void);
-  void (*read)(void);
-  uint8_t (*store)(void);
+typedef struct
+{
+    void (*init)(void *hw_interface, void *hw_config);
+    void (*reset)();
+    nBus_sensorType_t (*getType)(uint8_t sensor_index);
+    uint8_t (*getSensorCount)();
+    uint8_t (*getData)(uint8_t sensor_index, uint8_t *data);
+    uint8_t (*setData)(uint8_t *data);
+    uint8_t (*hasParam)(uint8_t sensor_index, nBus_param_t param_name);
+    int32_t (*getParam)(uint8_t sensor_index, nBus_param_t param_name);
+    nBus_param_t (*setParam)(uint8_t sensor_index, nBus_param_t param_name, int32_t param_value);
+    void (*start)(void);
+    void (*stop)(void);
+    void (*read)(void);
+    uint8_t (*store)(void);
 } nBusAppInterface_t;
 
 #endif /* MODULES_NBUS_INC_APP_BRIDGE_H_ */

+ 16 - 16
Inc/app_dummy.h

@@ -9,27 +9,27 @@
 #define INC_APP_DUMMY_H_
 
 #ifdef __cplusplus
-extern "C" {
+extern "C"
+{
 #endif
 
 #include "app_bridge.h"
 
-nBusAppInterface_t *getDummyDriver();
+    nBusAppInterface_t *getDummyDriver();
 
-void dummy_init(void *hw_interface, void *hw_config);
-void dummy_reset();
-nBus_sensorType_t dummy_getType(uint8_t sensor_index);
-uint8_t dummy_getSensorCount();
-uint8_t dummy_getData(uint8_t sensor_index, uint8_t *data);
-uint8_t dummy_setData(uint8_t *data);
-int32_t dummy_getParam(uint8_t sensor_index, nBus_param_t param);
-uint8_t dummy_hasParam(uint8_t sensor_index, nBus_param_t param);
-nBus_param_t dummy_setParam(uint8_t sensor_index, nBus_param_t param,
-                            int32_t value);
-void dummy_start(void);
-void dummy_stop(void);
-void dummy_read(void);
-uint8_t dummy_store(void);
+    void dummy_init(void *hw_interface, void *hw_config);
+    void dummy_reset();
+    nBus_sensorType_t dummy_getType(uint8_t sensor_index);
+    uint8_t dummy_getSensorCount();
+    uint8_t dummy_getData(uint8_t sensor_index, uint8_t *data);
+    uint8_t dummy_setData(uint8_t *data);
+    int32_t dummy_getParam(uint8_t sensor_index, nBus_param_t param);
+    uint8_t dummy_hasParam(uint8_t sensor_index, nBus_param_t param);
+    nBus_param_t dummy_setParam(uint8_t sensor_index, nBus_param_t param, int32_t value);
+    void dummy_start(void);
+    void dummy_stop(void);
+    void dummy_read(void);
+    uint8_t dummy_store(void);
 
 #ifdef __cplusplus
 }

+ 14 - 20
Inc/mcu_platform.h

@@ -8,38 +8,32 @@
 #ifndef __MCU_PLATFORM_H__
 #define __MCU_PLATFORM_H__
 
-#if defined(STM32F100xB) || defined(STM32F100xE) || defined(STM32F101x6) ||    \
-    defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) ||    \
-    defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) ||    \
-    defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
+#if defined(STM32F100xB) || defined(STM32F100xE) || defined(STM32F101x6) || defined(STM32F101xB) ||                    \
+    defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F102x6) || defined(STM32F102xB) ||                    \
+    defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
 #include "stm32f1xx.h"
 #define STM32F1
 #endif
 
-#if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) ||    \
-    defined(STM32F038xx) || defined(STM32F042x6) || defined(STM32F048xx) ||    \
-    defined(STM32F051x8) || defined(STM32F058xx) || defined(STM32F070x6) ||    \
-    defined(STM32F070xB) || defined(STM32F071xB) || defined(STM32F072xB) ||    \
-    defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx) ||    \
-    defined(STM32F030xC)
+#if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) ||                    \
+    defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F051x8) || defined(STM32F058xx) ||                    \
+    defined(STM32F070x6) || defined(STM32F070xB) || defined(STM32F071xB) || defined(STM32F072xB) ||                    \
+    defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
 #include "stm32f0xx.h"
 #define STM32F0
 #endif // defined
 
-#if defined(STM32L011xx) || defined(STM32L021xx) || defined(STM32L031xx) ||    \
-    defined(STM32L041xx) || defined(STM32L051xx) || defined(STM32L052xx) ||    \
-    defined(STM32L053xx) || defined(STM32L061xx) || defined(STM32L062xx) ||    \
-    defined(STM32L063xx) || defined(STM32L071xx) || defined(STM32L072xx) ||    \
-    defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) ||    \
-    defined(STM32L083xx)
+#if defined(STM32L011xx) || defined(STM32L021xx) || defined(STM32L031xx) || defined(STM32L041xx) ||                    \
+    defined(STM32L051xx) || defined(STM32L052xx) || defined(STM32L053xx) || defined(STM32L061xx) ||                    \
+    defined(STM32L062xx) || defined(STM32L063xx) || defined(STM32L071xx) || defined(STM32L072xx) ||                    \
+    defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
 #include "stm32l0xx.h"
 #define STM32L0
 #endif // defined
 
-#if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) ||    \
-    defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) ||    \
-    defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) ||    \
-    defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) ||    \
+#if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || defined(STM32F302xC) ||                    \
+    defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F303x8) || defined(STM32F334x8) ||                    \
+    defined(STM32F328xx) || defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) ||                    \
     defined(STM32F373xC) || defined(STM32F378xx)
 #include "stm32f3xx.h"
 #define STM32F3

+ 8 - 7
Inc/nbus_app.h

@@ -19,16 +19,17 @@
 #include "nbus_cmd.h"
 
 #ifdef __cplusplus
-extern "C" {
+extern "C"
+{
 #endif
 
-void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw);
-void nbus_init_app(void *hw_interface, void *hw_config);
-void nbus_stack(void);
-void nbus_init_memory_driver(nBus_MemoryDriver *memDriver);
+    void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw);
+    void nbus_init_app(void *hw_interface, void *hw_config);
+    void nbus_stack(void);
+    void nbus_init_memory_driver(nBus_MemoryDriver *memDriver);
 
-void nbus_cb_UART_RX(int);
-void nbus_cb_TIM_periodElapsed(void);
+    void nbus_cb_UART_RX(int);
+    void nbus_cb_TIM_periodElapsed(void);
 
 #ifdef __cplusplus
 }

+ 40 - 39
Inc/nbus_memory.h

@@ -15,55 +15,57 @@
 #define MODULE_PARAMS_COUNT 16
 #define MODULE_PARAM_SIZE 4
 
-#define GET_PARAM_ADDRESS(sensor, param)                                       \
-  (MODULE_SENSORS_ADDRESS_OFFSET +                                             \
-   (sensor)*MODULE_PARAMS_COUNT * MODULE_PARAM_SIZE +                          \
-   (param)*MODULE_PARAM_SIZE)
+#define GET_PARAM_ADDRESS(sensor, param)                                                                               \
+    (MODULE_SENSORS_ADDRESS_OFFSET + (sensor)*MODULE_PARAMS_COUNT * MODULE_PARAM_SIZE + (param)*MODULE_PARAM_SIZE)
 
 #define PARAMETER_VALID 0b10
 #define PARAMETER_UNSET 0b01
 
-typedef struct {
-  uint8_t (*init)(void *, uint16_t);
-  uint32_t (*read_word)(uint16_t);
-  uint16_t (*read_half_word)(uint16_t);
-  uint8_t (*read_byte)(uint16_t);
-
-  /**
-   * @brief Zapíše 4B/2B/1B hodnotu na adresu
-   * @param uint32_t data dáta
-   * @param uint16_t address adresa v pamati. Povolený rozsah: 0-0x09FF
-   * @param uint8_t length dĺžka zapisovaných dát
-   */
-  uint8_t (*write_data)(uint8_t *data, uint16_t address, uint8_t length);
-  uint8_t (*read_id)(uint8_t *);
-  uint32_t (*get_capacity)(void);
+typedef struct
+{
+    uint8_t (*init)(void *, uint16_t);
+    uint32_t (*read_word)(uint16_t);
+    uint16_t (*read_half_word)(uint16_t);
+    uint8_t (*read_byte)(uint16_t);
+
+    /**
+     * @brief Zapíše 4B/2B/1B hodnotu na adresu
+     * @param uint32_t data dáta
+     * @param uint16_t address adresa v pamati. Povolený rozsah: 0-0x09FF
+     * @param uint8_t length dĺžka zapisovaných dát
+     */
+    uint8_t (*write_data)(uint8_t *data, uint16_t address, uint8_t length);
+    uint8_t (*read_id)(uint8_t *);
+    uint32_t (*get_capacity)(void);
 } nBus_MemoryDriver;
 
-typedef enum {
-  MEMORY_STORE_FAIL,
-  MEMORY_STORE_OK,
+typedef enum
+{
+    MEMORY_STORE_FAIL,
+    MEMORY_STORE_OK,
 } nBus_memoryState_t;
 
-typedef struct {
-  uint8_t (*storeParam)(uint8_t sensor_index, uint8_t param_name,
-                        uint32_t param_value);
-  uint32_t (*readParam)(uint8_t sensor_index, uint8_t param_name);
-  uint8_t (*isParamActive)(uint8_t sensor_index, uint8_t param_name);
-  void (*readHeaderData)(void);
-  uint8_t (*getId)(uint8_t *);
-  uint32_t (*getCapacity)(void);
+typedef struct
+{
+    uint8_t (*storeParam)(uint8_t sensor_index, uint8_t param_name, uint32_t param_value);
+    uint32_t (*readParam)(uint8_t sensor_index, uint8_t param_name);
+    uint8_t (*isParamActive)(uint8_t sensor_index, uint8_t param_name);
+    void (*readHeaderData)(void);
+    uint8_t (*getId)(uint8_t *);
+    uint32_t (*getCapacity)(void);
 } nBus_memoryInterface_t;
 
-typedef struct __attribute__((packed)) {
-  uint8_t param0 : 2;
-  uint8_t param1 : 2;
-  uint8_t param2 : 2;
-  uint8_t param3 : 2;
+typedef struct __attribute__((packed))
+{
+    uint8_t param0 : 2;
+    uint8_t param1 : 2;
+    uint8_t param2 : 2;
+    uint8_t param3 : 2;
 } nBus_MemorySensorParam_t;
 
-typedef struct {
-  nBus_MemorySensorParam_t params_map[64];
+typedef struct
+{
+    nBus_MemorySensorParam_t params_map[64];
 } nBus_memoryHeader_t;
 
 nBus_memoryInterface_t *getnbusMemoryInterface(void);
@@ -72,8 +74,7 @@ void nbus_memory_init(nBus_MemoryDriver *driver);
 /**
  * @brief Store sensor parameter.
  */
-nBus_memoryState_t nbus_memory_store_param(uint8_t sensor_index, uint8_t param_name,
-                                    uint32_t param_value);
+nBus_memoryState_t nbus_memory_store_param(uint8_t sensor_index, uint8_t param_name, uint32_t param_value);
 uint32_t nbus_memory_read_param(uint8_t sensor_index, uint8_t param_name);
 uint8_t nbus_memory_id(uint8_t *);
 uint32_t nbus_get_capacity(void);

+ 1 - 2
Inc/nbus_slave.h

@@ -22,8 +22,7 @@ void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus);
 void nbus_slave_unicastToModuleSet(nBus_TypeDef *nBus);
 
 void nbus_slave_broadcast(nBus_TypeDef *nBus, nBusCommandType_t request_type);
-void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index,
-                        uint8_t param_name);
+void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index, uint8_t param_name);
 #endif
 
 #endif

+ 178 - 167
Inc/nbus_types.h

@@ -31,13 +31,14 @@
 /**
  * @brief Symbolické konštanty pre príznak pripravenosti dát
  */
-typedef enum {
-  /** Dáta nie sú pripravené. */
-  DATA_EMPTY,
-  /** Dáta sú pripravené. */
-  DATA_READY,
-  /** Dáta sú chybné. */
-  DATA_ERROR,
+typedef enum
+{
+    /** Dáta nie sú pripravené. */
+    DATA_EMPTY,
+    /** Dáta sú pripravené. */
+    DATA_READY,
+    /** Dáta sú chybné. */
+    DATA_ERROR,
 } DataReadyTypedef;
 
 /**
@@ -47,15 +48,16 @@ typedef enum {
  * bit 5: ~r/w príznak pr zápis údajov
  * bt 4-0: kód funkcie.
  */
-typedef struct __attribute__((packed)) {
-  /** Kód funkcie. Bit 0..4 */
-  uint8_t function : 5;
-  /** Príznak pre zápis údajov. Bit 5 */
-  uint8_t notReadWrite : 1;
-  /** Rezervoané. Bit 6 */
-  uint8_t reserve : 1;
-  /** Príznak chyby. Bit 7 */
-  uint8_t error : 1;
+typedef struct __attribute__((packed))
+{
+    /** Kód funkcie. Bit 0..4 */
+    uint8_t function : 5;
+    /** Príznak pre zápis údajov. Bit 5 */
+    uint8_t notReadWrite : 1;
+    /** Rezervoané. Bit 6 */
+    uint8_t reserve : 1;
+    /** Príznak chyby. Bit 7 */
+    uint8_t error : 1;
 } nBus_functionCode_t;
 
 #define REQUEST_GET 0
@@ -66,65 +68,70 @@ typedef struct __attribute__((packed)) {
  * bufferi. Baj obsahuje adresu koncového senzora (bit 0..4) a typ senzora (bit
  * 5-7)
  */
-typedef struct __attribute__((packed)) {
-  /** Adresa pripojeného senzora. Povolené adresy 1..31. Pozícia v ADR byte:
-   * 0..4*/
-  uint8_t address : 5;
-  /** Definuje typ senora: vstup/výstup.
-   * bit 7: 0-výstupné zariadenia, 1-vstupné zariadenia
-   */
-  uint8_t type : 3;
+typedef struct __attribute__((packed))
+{
+    /** Adresa pripojeného senzora. Povolené adresy 1..31. Pozícia v ADR byte:
+     * 0..4*/
+    uint8_t address : 5;
+    /** Definuje typ senora: vstup/výstup.
+     * bit 7: 0-výstupné zariadenia, 1-vstupné zariadenia
+     */
+    uint8_t type : 3;
 } nBus_sensorByte_t;
 
 /**
  * @brief Stavy komunikačného rozhrania počas behu programu.
  */
-typedef enum {
-  /** Čaká sa na príjem dát. Nasledujúci stav: UART_RX_RECEIVING */
-  UART_RX_WAIT,
-  /** Dáta sa prijímajú. Nasledujúci stav: UART_RX_RECEIVED */
-  UART_RX_RECEIVING,
-  /** Dáta sú prijané, v ďašom cykle sa budú spracovať. Nasledujúci stav:
-     UART_RX_WAIT*/
-  UART_RX_RECEIVED
+typedef enum
+{
+    /** Čaká sa na príjem dát. Nasledujúci stav: UART_RX_RECEIVING */
+    UART_RX_WAIT,
+    /** Dáta sa prijímajú. Nasledujúci stav: UART_RX_RECEIVED */
+    UART_RX_RECEIVING,
+    /** Dáta sú prijané, v ďašom cykle sa budú spracovať. Nasledujúci stav:
+       UART_RX_WAIT*/
+    UART_RX_RECEIVED
 } nBus_Uart_RX_state;
 
 /**
  * @brief Určuje typ nBUS príkazu.
  */
-typedef enum {
-  /**
-   * Funckia, resp. príkaz určený konkrétny koncový senzor.
-   */
-  UNICAST_TO_SENSOR,
-  /**
-   * Funckia, resp. príkaz určený konkrétny koncový senzor.
-   */
-  UNICAST_TO_MODULE,
-  /**
-   * Funckia, resp. príkaz určený špecifické senzory.
-   */
-  BROADCAST_SPECIFIC_SENSORS,
-  /**
-   * Funckia, resp. príkaz určený pre všetky moduly.
-   */
-  BROADCAST_GLOBAL
+typedef enum
+{
+    /**
+     * Funckia, resp. príkaz určený konkrétny koncový senzor.
+     */
+    UNICAST_TO_SENSOR,
+    /**
+     * Funckia, resp. príkaz určený konkrétny koncový senzor.
+     */
+    UNICAST_TO_MODULE,
+    /**
+     * Funckia, resp. príkaz určený špecifické senzory.
+     */
+    BROADCAST_SPECIFIC_SENSORS,
+    /**
+     * Funckia, resp. príkaz určený pre všetky moduly.
+     */
+    BROADCAST_GLOBAL
 } nBusCommandType_t;
 
 /**
  * @brief Určuje typ odpovede na požiadavku.
  */
-typedef enum {
-  /** Na požiadavku sa nebude odpovedať. Zvyčajte pri BROADCAST požiadavke. */
-  NO_RESPONSE,
-  /** Na požiadavku sa bude odpovedať. Zvyčajte pri UNICAST požiadavke. */
-  SEND_RESPONSE
+typedef enum
+{
+    /** Na požiadavku sa nebude odpovedať. Zvyčajte pri BROADCAST požiadavke. */
+    NO_RESPONSE,
+    /** Na požiadavku sa bude odpovedať. Zvyčajte pri UNICAST požiadavke. */
+    SEND_RESPONSE
 
 } nBus_response_t;
 
-typedef enum {
-  MEASURE_STOPPED,
-  MEASURE_RUNNING,
+typedef enum
+{
+    MEASURE_STOPPED,
+    MEASURE_RUNNING,
 } nBusMeasurementActive_t;
 
 /**
@@ -139,48 +146,49 @@ typedef enum {
  * - loop_callback: interný callback pre signalizáciu externých udalostí z
  * aplikácie.
  */
-typedef struct {
-  /**
-   * @brief Spustenie príjmu dát na vstunom komunikačnom rozhraní (UART, ..).
-   * 1 parameter: pointer na pole, kde sa uložia načítané dáta.
-   * 2 parameter: maximálna dĺžka načítaných dát.
-   * @note Načíranie dát musí byť asynchrónne. Celé načítanie musí manažovať
-   * aplikácia. Po úspešnom načítaní musí aplikácia zavolať funkciu
-   * nbus_cb_UART_RX(dataI); @see nbus_cb_UART_RX
-   */
-  void (*uart_receive)(uint8_t *, int);
-  /**
-   * @brief Odoslanie odpovede cez komunikačné rozhranie.
-   * 1 parameter: dáta na odoslanie
-   * 2 parameter: počet abjtov na odoslanie
-   */
-  void (*uart_transmit)(uint8_t *, int);
-  /**
-   * @brief Zapnutie signalizácie
-   */
-  void (*led_on)(void);
-  /**
-   * @brief Vypnutie signalizácie
-   */
-  void (*led_off)(void);
-  /**
-   * @brief Zmena stavu signalizácie
-   */
-  void (*led_toggle)(void);
-  /**
-   * @brief Blokujúca pauza [ms].
-   */
-  void (*delay_ms)(uint8_t);
-  /**
-   * Callback protokolu nBus pre signalizáciu udalostí. Tento callback sa v
-   * protokole nBus spúšťa každú iteráciu v hlavnej slučke. Stav aplikácie uruje
-   * návratová hodnota tohto callbacku: 0 - žiadna udalosť nenastala 1 - signál
-   * pre pripravenosť dát z pripojených senzorov. Hodnota 1 spôsobí prečítanie
-   * dát z pripojeného sensora (nBus.interface->read()) 2 - signál oznamujúci
-   * ukončenie načítania vstupných dát cez komunikačné rozhranie. Je to signál
-   * pre ukočenie načítania, ktoré bolo sputené cez uart_receive
-   */
-  uint8_t (*loop_callback)(void);
+typedef struct
+{
+    /**
+     * @brief Spustenie príjmu dát na vstunom komunikačnom rozhraní (UART, ..).
+     * 1 parameter: pointer na pole, kde sa uložia načítané dáta.
+     * 2 parameter: maximálna dĺžka načítaných dát.
+     * @note Načíranie dát musí byť asynchrónne. Celé načítanie musí manažovať
+     * aplikácia. Po úspešnom načítaní musí aplikácia zavolať funkciu
+     * nbus_cb_UART_RX(dataI); @see nbus_cb_UART_RX
+     */
+    void (*uart_receive)(uint8_t *, int);
+    /**
+     * @brief Odoslanie odpovede cez komunikačné rozhranie.
+     * 1 parameter: dáta na odoslanie
+     * 2 parameter: počet abjtov na odoslanie
+     */
+    void (*uart_transmit)(uint8_t *, int);
+    /**
+     * @brief Zapnutie signalizácie
+     */
+    void (*led_on)(void);
+    /**
+     * @brief Vypnutie signalizácie
+     */
+    void (*led_off)(void);
+    /**
+     * @brief Zmena stavu signalizácie
+     */
+    void (*led_toggle)(void);
+    /**
+     * @brief Blokujúca pauza [ms].
+     */
+    void (*delay_ms)(uint8_t);
+    /**
+     * Callback protokolu nBus pre signalizáciu udalostí. Tento callback sa v
+     * protokole nBus spúšťa každú iteráciu v hlavnej slučke. Stav aplikácie uruje
+     * návratová hodnota tohto callbacku: 0 - žiadna udalosť nenastala 1 - signál
+     * pre pripravenosť dát z pripojených senzorov. Hodnota 1 spôsobí prečítanie
+     * dát z pripojeného sensora (nBus.interface->read()) 2 - signál oznamujúci
+     * ukončenie načítania vstupných dát cez komunikačné rozhranie. Je to signál
+     * pre ukočenie načítania, ktoré bolo sputené cez uart_receive
+     */
+    uint8_t (*loop_callback)(void);
 
 } nBusPlatformInterface_t;
 
@@ -190,96 +198,99 @@ typedef struct {
  * Obsahuje stavové a funkčné premenné zabezpečujúce správnu implementáciu
  * komunikácie.
  */
-typedef struct {
-  /**
-   * @brief Prijímací buffer nBUS.
-   */
-  uint8_t rx_buffer[BUFF_SIZE]; // buffer prijatych znakov
+typedef struct
+{
+    /**
+     * @brief Prijímací buffer nBUS.
+     */
+    uint8_t rx_buffer[BUFF_SIZE]; // buffer prijatych znakov
 
-  /**
-   * @brief Odosielací buffer uBUS.
-   */
-  uint8_t tx_buffer[BUFF_SIZE]; // buffer odosielanych znakov
+    /**
+     * @brief Odosielací buffer uBUS.
+     */
+    uint8_t tx_buffer[BUFF_SIZE]; // buffer odosielanych znakov
 
-  /**
-   * @brief Dĺžka prijatej správy
-   */
-  uint8_t volatile rx_length; // usart flag - obsahuje pocet prijatych byte
+    /**
+     * @brief Dĺžka prijatej správy
+     */
+    uint8_t volatile rx_length; // usart flag - obsahuje pocet prijatych byte
 
-  /**
-   * @brief Dĺžka odosielanej správy
-   */
-  uint16_t volatile tx_length; // usart flag - obsahuje pocet vysielanych byte
+    /**
+     * @brief Dĺžka odosielanej správy
+     */
+    uint16_t volatile tx_length; // usart flag - obsahuje pocet vysielanych byte
 
-  /**
-   * @brief Prvy bajt adresy zariadenia slave
-   */
-  volatile uint8_t addressModule;
+    /**
+     * @brief Prvy bajt adresy zariadenia slave
+     */
+    volatile uint8_t addressModule;
 
-  /**
-   * @brief Druhy bajt adresy zariadenia slave
-   */
-  volatile nBus_sensorByte_t sensorInfo;
+    /**
+     * @brief Druhy bajt adresy zariadenia slave
+     */
+    volatile nBus_sensorByte_t sensorInfo;
 
-  nBus_functionCode_t function_code;
-  volatile nBusCommandType_t request_type;
+    nBus_functionCode_t function_code;
+    volatile nBusCommandType_t request_type;
 
-  /** @brief Flag pre signalizaciu ukoncenia primu dat cez UART. */
-  volatile nBus_Uart_RX_state uart_state;
+    /** @brief Flag pre signalizaciu ukoncenia primu dat cez UART. */
+    volatile nBus_Uart_RX_state uart_state;
 
-  nBus_response_t send_response;
+    nBus_response_t send_response;
 
-  /** @brief CRC8 sucet pre datovy ramec */
-  volatile uint8_t CRC8;
+    /** @brief CRC8 sucet pre datovy ramec */
+    volatile uint8_t CRC8;
 
-  volatile nBusMeasurementActive_t measure_active;
+    volatile nBusMeasurementActive_t measure_active;
 
-  volatile uint32_t data_timebase;
+    volatile uint32_t data_timebase;
 
-  nBusAppInterface_t *interface;
+    nBusAppInterface_t *interface;
 
-  nBus_memoryInterface_t *memoryInterface;
+    nBus_memoryInterface_t *memoryInterface;
 
 #if USE_USART_DMA_TX == 1
-  /**
-   * @brief Príznak ukončenia odoslania dát cez DMA
-   *
-   * Vlastnosť existuje, len ak má makro USE_USART_DMA_TX hodnotu 1
-   */
-  volatile uint8_t flag_UartTxCplt_DMA;
+    /**
+     * @brief Príznak ukončenia odoslania dát cez DMA
+     *
+     * Vlastnosť existuje, len ak má makro USE_USART_DMA_TX hodnotu 1
+     */
+    volatile uint8_t flag_UartTxCplt_DMA;
 #endif
 
-  nBusPlatformInterface_t *hw_platform;
+    nBusPlatformInterface_t *hw_platform;
 
 } nBus_TypeDef;
 
 /**
  * @brief Určuje typ uBUS príkazu pre koncove slave zariadenie.
  */
-typedef enum {
-  /**
-   * všeobecný príkaz, dáta sú uložené v konfigurácii (FLASH)
-   */
-  COMMAND_SLAVE_GENERAL,
-  /**
-   * špecifický príkaz (state, reset, set/get value/param). Pracuje sa s
-   * koncovým senzorom. Obsluja poziadavky je v module pre daný senzor.
-   */
-  COMMAND_SLAVE_SPECIFIC,
+typedef enum
+{
+    /**
+     * všeobecný príkaz, dáta sú uložené v konfigurácii (FLASH)
+     */
+    COMMAND_SLAVE_GENERAL,
+    /**
+     * špecifický príkaz (state, reset, set/get value/param). Pracuje sa s
+     * koncovým senzorom. Obsluja poziadavky je v module pre daný senzor.
+     */
+    COMMAND_SLAVE_SPECIFIC,
 } UnicastSlaveCommandType;
 
 /**
  * @brief Určuje stav komunikačnej vrstvy
  */
-typedef enum {
-  /**
-   * Dáta sú prijaté a pripravené na spracovanie.
-   */
-  UART_DATA_READY,
-  /**
-   * Nie je prijatý žiadny príkaz na spracovanie.
-   */
-  UART_DATA_EMPTY,
+typedef enum
+{
+    /**
+     * Dáta sú prijaté a pripravené na spracovanie.
+     */
+    UART_DATA_READY,
+    /**
+     * Nie je prijatý žiadny príkaz na spracovanie.
+     */
+    UART_DATA_EMPTY,
 } UART_DataState;
 
 /**
@@ -289,12 +300,12 @@ typedef enum {
  * Vo funkcii sa nastaví aj správna dĺžka odpovede.
  * @param error_code chybový kód odpovede
  */
-#define error_response(error_code)                                             \
-  {                                                                            \
-    uBus.sb_length = META_SIZE + 1;                                            \
-    uBus.send_buff[2] = uBus.send_buff[2] | FLAG_ERROR;                        \
-    uBus.send_buff[3] = error_code;                                            \
-  }
+#define error_response(error_code)                                                                                     \
+    {                                                                                                                  \
+        uBus.sb_length = META_SIZE + 1;                                                                                \
+        uBus.send_buff[2] = uBus.send_buff[2] | FLAG_ERROR;                                                            \
+        uBus.send_buff[3] = error_code;                                                                                \
+    }
 
 #endif
 

+ 4 - 4
Makefile

@@ -5,13 +5,13 @@ check:
 	cppcheck .
 
 cs:
-	clang-format -n ./Src/* ./Inc/*
+	clang-format -style=file:./.clang-format -n ./Src/* ./Inc/*
 
 csf:
-	clang-format -i ./Src/* ./Inc/*
+	clang-format -style=file:./.clang-format -i ./Src/* ./Inc/*
 
 cs-v14:
-	docker run --rm -v ${PWD}:/src xianpengshen/clang-tools:14 clang-format -n ./Src/* ./Inc/*
+	docker run --rm -v ${PWD}:/src xianpengshen/clang-tools:14 clang-format -style=file:./.clang-format -n ./Src/* ./Inc/*
 
 csf-v14:
-	docker run --rm -v ${PWD}:/src xianpengshen/clang-tools:14 clang-format -i ./Src/* ./Inc/*
+	docker run --rm -v ${PWD}:/src xianpengshen/clang-tools:14 clang-format -style=file:./.clang-format -i ./Src/* ./Inc/*

+ 20 - 10
Src/app_bridge.c

@@ -6,17 +6,27 @@
  */
 #include "app_bridge.h"
 
-nBus_param_t allParams[] = {PARAM_TIMEBASE, PARAM_RESOLUTION, PARAM_GAIN,
-                            PARAM_OFFSET,   PARAM_SAMPLERATE, PARAM_RANGE,
-							PARAM_RANGE0,   PARAM_FILTER};
-nBus_sensorType_t allTypes[] = {
-    TYPE_ACCELEROMETER, TYPE_GYROSCOPE, TYPE_MAGNETOMETER, TYPE_TEMPERATURE,
-    TYPE_HUMIDITY,      TYPE_PRESSURE,  TYPE_HEART_RATE};
+nBus_param_t allParams[] = {PARAM_TIMEBASE,   PARAM_RESOLUTION, PARAM_GAIN,   PARAM_OFFSET,
+                            PARAM_SAMPLERATE, PARAM_RANGE,      PARAM_RANGE0, PARAM_FILTER};
+nBus_sensorType_t allTypes[] = {TYPE_ACCELEROMETER, TYPE_GYROSCOPE, TYPE_MAGNETOMETER, TYPE_TEMPERATURE,
+                                TYPE_HUMIDITY,      TYPE_PRESSURE,  TYPE_HEART_RATE};
 
-nBus_param_t *nbus_interface_allParams() { return allParams; }
+nBus_param_t *nbus_interface_allParams()
+{
+    return allParams;
+}
 
-uint8_t nbus_interface_allParamsCount() { return sizeof(allParams)/sizeof(nBus_param_t); }
+uint8_t nbus_interface_allParamsCount()
+{
+    return sizeof(allParams) / sizeof(nBus_param_t);
+}
 
-nBus_sensorType_t *nbus_interface_allTypes() { return allTypes; }
+nBus_sensorType_t *nbus_interface_allTypes()
+{
+    return allTypes;
+}
 
-uint8_t nbus_interface_allTypesCount() { return sizeof(nBus_param_t)/sizeof(nBus_sensorType_t); }
+uint8_t nbus_interface_allTypesCount()
+{
+    return sizeof(nBus_param_t) / sizeof(nBus_sensorType_t);
+}

+ 74 - 45
Src/app_dummy.c

@@ -2,72 +2,101 @@
 #include <stdint.h>
 
 nBusAppInterface_t dummy_driver = {
-    dummy_init,     dummy_reset,   dummy_getType,  dummy_getSensorCount,
-    dummy_getData,  dummy_setData, dummy_hasParam, dummy_getParam,
-    dummy_setParam, dummy_start,   dummy_stop,     dummy_read,
-    dummy_store,
+    dummy_init,     dummy_reset,    dummy_getType, dummy_getSensorCount, dummy_getData, dummy_setData, dummy_hasParam,
+    dummy_getParam, dummy_setParam, dummy_start,   dummy_stop,           dummy_read,    dummy_store,
 };
 
-nBusAppInterface_t *getDummyDriver() { return &dummy_driver; }
-
-void dummy_init(void *hw_interface, void *hw_config) {}
+nBusAppInterface_t *getDummyDriver()
+{
+    return &dummy_driver;
+}
 
-void dummy_reset() {}
+void dummy_init(void *hw_interface, void *hw_config)
+{
+}
 
-nBus_sensorType_t dummy_getType(uint8_t sensor_index) {
+void dummy_reset()
+{
+}
 
-  if (sensor_index > dummy_getSensorCount())
+nBus_sensorType_t dummy_getType(uint8_t sensor_index)
+{
+
+    if (sensor_index > dummy_getSensorCount())
+        return TYPE_UNKNOWN;
+
+    switch (sensor_index)
+    {
+    case 1:
+        return TYPE_ACCELEROMETER;
+    case 2:
+        return TYPE_GYROSCOPE;
+    case 3:
+        return TYPE_MAGNETOMETER;
+    case 4:
+        return TYPE_HEART_RATE;
+    case 5:
+        return TYPE_PRESSURE;
+    }
     return TYPE_UNKNOWN;
-
-  switch (sensor_index) {
-  case 1:
-    return TYPE_ACCELEROMETER;
-  case 2:
-    return TYPE_GYROSCOPE;
-  case 3:
-    return TYPE_MAGNETOMETER;
-  case 4:
-    return TYPE_HEART_RATE;
-  case 5:
-    return TYPE_PRESSURE;
-  }
-  return TYPE_UNKNOWN;
 }
 
-uint8_t dummy_getSensorCount() { return 4; }
+uint8_t dummy_getSensorCount()
+{
+    return 4;
+}
 
 uint32_t dummy_data[4] = {0x12345678, 0x9ABCDEF0, 0x87654321, 0x0FEDCBA9};
 
-uint8_t dummy_getData(uint8_t sensor_index, uint8_t *data) {
-  for (uint8_t i = 0; i < 4; ++i) {
-    data[i] = ((uint8_t *)&dummy_data)[i + (sensor_index - 1) * 8];
-    data[4 + i] = ((uint8_t *)&dummy_data)[4 + i + (sensor_index - 1) * 8];
-  }
+uint8_t dummy_getData(uint8_t sensor_index, uint8_t *data)
+{
+    for (uint8_t i = 0; i < 4; ++i)
+    {
+        data[i] = ((uint8_t *)&dummy_data)[i + (sensor_index - 1) * 8];
+        data[4 + i] = ((uint8_t *)&dummy_data)[4 + i + (sensor_index - 1) * 8];
+    }
 
-  return 8;
+    return 8;
 }
 
-uint8_t dummy_setData(uint8_t *data) { return 1; }
+uint8_t dummy_setData(uint8_t *data)
+{
+    return 1;
+}
 
-int32_t dummy_getParam(uint8_t sensor_index, nBus_param_t param) {
-  return 0xABCD4567;
+int32_t dummy_getParam(uint8_t sensor_index, nBus_param_t param)
+{
+    return 0xABCD4567;
 }
 
-uint8_t dummy_hasParam(uint8_t sensor_index, nBus_param_t param) {
-  if (sensor_index < dummy_getSensorCount())
-    return 1;
-  return 0;
+uint8_t dummy_hasParam(uint8_t sensor_index, nBus_param_t param)
+{
+    if (sensor_index < dummy_getSensorCount())
+        return 1;
+    return 0;
 }
 
-nBus_param_t dummy_setParam(uint8_t sensor_index, nBus_param_t param,
-                            int32_t value) {
-  return param;
+nBus_param_t dummy_setParam(uint8_t sensor_index, nBus_param_t param, int32_t value)
+{
+    return param;
 }
 
-void dummy_start(void) { return; }
+void dummy_start(void)
+{
+    return;
+}
 
-void dummy_stop(void) { return; }
+void dummy_stop(void)
+{
+    return;
+}
 
-void dummy_read(void) { return; }
+void dummy_read(void)
+{
+    return;
+}
 
-uint8_t dummy_store(void) { return 0; }
+uint8_t dummy_store(void)
+{
+    return 0;
+}

+ 197 - 171
Src/nbus_app.c

@@ -4,161 +4,175 @@
 nBus_TypeDef nBus;
 
 static uint8_t const crc8x_table[] = {
-    0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31,
-    0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
-    0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9,
-    0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
-    0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1,
-    0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
-    0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE,
-    0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
-    0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16,
-    0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
-    0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80,
-    0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
-    0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8,
-    0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
-    0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10,
-    0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
-    0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F,
-    0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
-    0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7,
-    0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
-    0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF,
-    0xFA, 0xFD, 0xF4, 0xF3};
+    0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77, 0x7E,
+    0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D, 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB,
+    0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD, 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8,
+    0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD, 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6,
+    0xE3, 0xE4, 0xED, 0xEA, 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D,
+    0x9A, 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A, 0x57, 0x50,
+    0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A, 0x89, 0x8E, 0x87, 0x80, 0x95,
+    0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4, 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
+    0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4, 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F,
+    0x58, 0x4D, 0x4A, 0x43, 0x44, 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A,
+    0x33, 0x34, 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E,
+    0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xA9, 0xA0, 0xA7,
+    0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC,
+    0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3};
 
 /* -------------------------------------------------------- */
 /* ------------------ PRIVATE FUNCTIONS-------------------- */
 /* -------------------------------------------------------- */
 
-static uint8_t crc8x_fast(void const *mem, uint16_t len) {
-  uint8_t crc = CRC8_INIT_VALUE;
-  uint8_t const *data = (uint8_t *)mem;
-  if (data == NULL)
-    return 0xff;
-  crc &= 0xff;
-  while (len--)
-    crc = crc8x_table[crc ^ *data++];
-  return crc;
+static uint8_t crc8x_fast(void const *mem, uint16_t len)
+{
+    uint8_t crc = CRC8_INIT_VALUE;
+    uint8_t const *data = (uint8_t *)mem;
+    if (data == NULL)
+        return 0xff;
+    crc &= 0xff;
+    while (len--)
+        crc = crc8x_table[crc ^ *data++];
+    return crc;
 }
 
-inline static void receivePacket(void) {
-  nBus.hw_platform->uart_receive(nBus.rx_buffer, 64);
+inline static void receivePacket(void)
+{
+    nBus.hw_platform->uart_receive(nBus.rx_buffer, 64);
 }
 
-inline static void send_response() {
-  if (nBus.send_response == SEND_RESPONSE) {
-    nBus.tx_buffer[0] -= 1; // prvý bajt sa nepočíta
-    nBus.hw_platform->uart_transmit(nBus.tx_buffer, nBus.tx_length);
-  }
-  nBus.hw_platform->led_off();
+inline static void send_response()
+{
+    if (nBus.send_response == SEND_RESPONSE)
+    {
+        nBus.tx_buffer[0] -= 1; // prvý bajt sa nepočíta
+        nBus.hw_platform->uart_transmit(nBus.tx_buffer, nBus.tx_length);
+    }
+    nBus.hw_platform->led_off();
 }
 
 #if MODULE_MASTER == 1
-inline static void receive_slave_response() {}
+inline static void receive_slave_response()
+{
+}
 #endif
 
-static nBusCommandType_t get_request_type() {
-  nBus.addressModule = nBus.rx_buffer[0];
-  nBus.sensorInfo = *(const nBus_sensorByte_t *)&nBus.rx_buffer[1];
-  nBus.function_code = *(const nBus_functionCode_t *)&nBus.rx_buffer[2];
+static nBusCommandType_t get_request_type()
+{
+    nBus.addressModule = nBus.rx_buffer[0];
+    nBus.sensorInfo = *(const nBus_sensorByte_t *)&nBus.rx_buffer[1];
+    nBus.function_code = *(const nBus_functionCode_t *)&nBus.rx_buffer[2];
 
-  if (nBus.sensorInfo.address != 0 && nBus.addressModule != 0) {
-    nBus.request_type = UNICAST_TO_SENSOR;
-    return UNICAST_TO_SENSOR;
-  }
+    if (nBus.sensorInfo.address != 0 && nBus.addressModule != 0)
+    {
+        nBus.request_type = UNICAST_TO_SENSOR;
+        return UNICAST_TO_SENSOR;
+    }
 
-  if (nBus.sensorInfo.address == 0 && nBus.addressModule != 0) {
-    nBus.request_type = UNICAST_TO_MODULE;
-    return UNICAST_TO_MODULE;
-  }
+    if (nBus.sensorInfo.address == 0 && nBus.addressModule != 0)
+    {
+        nBus.request_type = UNICAST_TO_MODULE;
+        return UNICAST_TO_MODULE;
+    }
 
-  if (nBus.sensorInfo.address != 0 && nBus.addressModule == 0) {
-    nBus.request_type = BROADCAST_SPECIFIC_SENSORS;
-    return BROADCAST_SPECIFIC_SENSORS;
-  }
+    if (nBus.sensorInfo.address != 0 && nBus.addressModule == 0)
+    {
+        nBus.request_type = BROADCAST_SPECIFIC_SENSORS;
+        return BROADCAST_SPECIFIC_SENSORS;
+    }
 
-  nBus.request_type = BROADCAST_GLOBAL;
-  return BROADCAST_GLOBAL;
+    nBus.request_type = BROADCAST_GLOBAL;
+    return BROADCAST_GLOBAL;
 }
 
-static void process_request() {
-  nBusCommandType_t request_type = get_request_type();
-  nBus.send_response = SEND_RESPONSE;
+static void process_request()
+{
+    nBusCommandType_t request_type = get_request_type();
+    nBus.send_response = SEND_RESPONSE;
 
-  nBus.tx_buffer[0] = 0;
-  nBus.tx_buffer[1] = nBus.rx_buffer[0]; // Module address
-  nBus.tx_buffer[2] = nBus.rx_buffer[1]; // Sensor address
+    nBus.tx_buffer[0] = 0;
+    nBus.tx_buffer[1] = nBus.rx_buffer[0]; // Module address
+    nBus.tx_buffer[2] = nBus.rx_buffer[1]; // Sensor address
 
-  uint8_t crcC = crc8x_fast(nBus.rx_buffer, nBus.rx_length - 1);
-  if (crcC != nBus.rx_buffer[nBus.rx_length - 1]) {
-    nBus.send_response = NO_RESPONSE;
-    return;
-  }
+    uint8_t crcC = crc8x_fast(nBus.rx_buffer, nBus.rx_length - 1);
+    if (crcC != nBus.rx_buffer[nBus.rx_length - 1])
+    {
+        nBus.send_response = NO_RESPONSE;
+        return;
+    }
 
 #if MODULE_SLAVE == 1
-  // spracovanie broadcast komunikacie
-  if ((request_type == BROADCAST_SPECIFIC_SENSORS ||
-       request_type == BROADCAST_GLOBAL)) {
-    nbus_slave_broadcast(&nBus, request_type);
-    return;
-  }
+    // spracovanie broadcast komunikacie
+    if ((request_type == BROADCAST_SPECIFIC_SENSORS || request_type == BROADCAST_GLOBAL))
+    {
+        nbus_slave_broadcast(&nBus, request_type);
+        return;
+    }
 #endif
 
-  // paket nie je adresovany tomuto modulu
-  if (nBus.addressModule != MODULE_ADDRESS) {
-    nBus.send_response = NO_RESPONSE;
-    return;
-  }
-
-  nBus.function_code.error = 0;
-  nBus.tx_length = META_SIZE;
-
-#if MODULE_SLAVE == 1
-  if (nBus.function_code.notReadWrite == REQUEST_GET) {
-    if (request_type == UNICAST_TO_SENSOR) {
-      nbus_slave_unicastToSensorGet(&nBus);
+    // paket nie je adresovany tomuto modulu
+    if (nBus.addressModule != MODULE_ADDRESS)
+    {
+        nBus.send_response = NO_RESPONSE;
+        return;
     }
 
-    if (request_type == UNICAST_TO_MODULE) {
-      nbus_slave_unicastToModuleGet(&nBus);
-    }
+    nBus.function_code.error = 0;
+    nBus.tx_length = META_SIZE;
 
-  } else {
-    // else request is REQUEST_SET
-    if (request_type == UNICAST_TO_SENSOR) {
-      nbus_slave_unicastToSensorSet(&nBus);
+#if MODULE_SLAVE == 1
+    if (nBus.function_code.notReadWrite == REQUEST_GET)
+    {
+        if (request_type == UNICAST_TO_SENSOR)
+        {
+            nbus_slave_unicastToSensorGet(&nBus);
+        }
+
+        if (request_type == UNICAST_TO_MODULE)
+        {
+            nbus_slave_unicastToModuleGet(&nBus);
+        }
     }
-
-    if (request_type == UNICAST_TO_MODULE) {
-      nbus_slave_unicastToModuleSet(&nBus);
+    else
+    {
+        // else request is REQUEST_SET
+        if (request_type == UNICAST_TO_SENSOR)
+        {
+            nbus_slave_unicastToSensorSet(&nBus);
+        }
+
+        if (request_type == UNICAST_TO_MODULE)
+        {
+            nbus_slave_unicastToModuleSet(&nBus);
+        }
     }
-  }
 
-  nBus.tx_buffer[3] = *(uint8_t *)&nBus.function_code;
-  nBus.tx_buffer[nBus.tx_length - 1] =
-      crc8x_fast(&nBus.tx_buffer[1], nBus.tx_length - 2);
-  nBus.tx_buffer[0] = nBus.tx_length;
+    nBus.tx_buffer[3] = *(uint8_t *)&nBus.function_code;
+    nBus.tx_buffer[nBus.tx_length - 1] = crc8x_fast(&nBus.tx_buffer[1], nBus.tx_length - 2);
+    nBus.tx_buffer[0] = nBus.tx_length;
 #endif
 
 #if MODULE_MASTER == 1
-  if (nBus.function_code.notReadWrite == REQUEST_GET) {
-    nbus_master_unicastToModuleGet(&nBus);
-  } else {
-    nbus_master_unicastToModuleSet(&nBus);
-  }
+    if (nBus.function_code.notReadWrite == REQUEST_GET)
+    {
+        nbus_master_unicastToModuleGet(&nBus);
+    }
+    else
+    {
+        nbus_master_unicastToModuleSet(&nBus);
+    }
 #endif
 }
 
-static void nbus_blink_LED(uint8_t delay) {
-  nBus.hw_platform->led_on();
-  nBus.hw_platform->delay_ms(delay);
-  nBus.hw_platform->led_off();
-  nBus.hw_platform->delay_ms(delay);
-  nBus.hw_platform->led_on();
-  nBus.hw_platform->delay_ms(delay);
-  nBus.hw_platform->led_off();
-  nBus.hw_platform->delay_ms(delay);
+static void nbus_blink_LED(uint8_t delay)
+{
+    nBus.hw_platform->led_on();
+    nBus.hw_platform->delay_ms(delay);
+    nBus.hw_platform->led_off();
+    nBus.hw_platform->delay_ms(delay);
+    nBus.hw_platform->led_on();
+    nBus.hw_platform->delay_ms(delay);
+    nBus.hw_platform->led_off();
+    nBus.hw_platform->delay_ms(delay);
 }
 
 /* -------------------------------------------------------- */
@@ -171,9 +185,10 @@ static void nbus_blink_LED(uint8_t delay) {
  * @param int size Size of received packet
  * Received packet is located in uBus.rx_buffer
  */
-void nbus_cb_UART_RX(int size) {
-  nBus.rx_length = size;
-  nBus.uart_state = UART_RX_RECEIVED;
+void nbus_cb_UART_RX(int size)
+{
+    nBus.rx_length = size;
+    nBus.uart_state = UART_RX_RECEIVED;
 }
 
 /* -------------------------------------------------------- */
@@ -189,18 +204,19 @@ void nbus_cb_UART_RX(int size) {
  * @param hw Application level implementation of base functions (uart_receive,
  * uart_transmit, led_on/off/toggle, ...). These are MCU dependent functions.
  */
-void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw) {
-  nBus.hw_platform = hw;
+void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw)
+{
+    nBus.hw_platform = hw;
 
-  nBus.rx_length = 0;
-  nBus.data_timebase = 0;
-  nBus.measure_active = MEASURE_STOPPED;
+    nBus.rx_length = 0;
+    nBus.data_timebase = 0;
+    nBus.measure_active = MEASURE_STOPPED;
 
-  nBus.uart_state = UART_RX_WAIT;
+    nBus.uart_state = UART_RX_WAIT;
 
-  nBus.interface = interface;
+    nBus.interface = interface;
 
-  receivePacket();
+    receivePacket();
 }
 
 /**
@@ -211,8 +227,9 @@ void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw) {
  * be hSPI, hUART, hADC, ...
  * @param hw_config configuration for hardware structure object.
  */
-void nbus_init_app(void *hw_interface, void *hw_config) {
-  nBus.interface->init(hw_interface, hw_config);
+void nbus_init_app(void *hw_interface, void *hw_config)
+{
+    nBus.interface->init(hw_interface, hw_config);
 }
 
 /**
@@ -223,27 +240,31 @@ void nbus_init_app(void *hw_interface, void *hw_config) {
  * from/to memory
  * @todo implement capacity parameter
  */
-void nbus_init_memory_driver(nBus_MemoryDriver *memDriver) {
-  nbus_memory_init(memDriver);
-  nBus.memoryInterface = getnbusMemoryInterface();
-  nbus_blink_LED(100);
-
-  nBus.memoryInterface->readHeaderData();
-
-  uint8_t *params = nbus_interface_allParams();
-  uint8_t paramCnt = nbus_interface_allParamsCount();
-  uint32_t paramValue;
-  for (uint32_t index = 1; index <= nBus.interface->getSensorCount(); index++) {
-	  for (int paramIndex = 0; paramIndex < paramCnt; paramIndex++) {
-		  if (nbus_is_param_active(index, params[paramIndex])) {
-			  if (nBus.interface->hasParam(index, params[paramIndex])) {
-				  paramValue = nbus_memory_read_param(index, params[paramIndex]);
-				  nBus.interface->setParam(index, params[paramIndex], paramValue);
-			  }
-		  }
-	  }
-  }
-
+void nbus_init_memory_driver(nBus_MemoryDriver *memDriver)
+{
+    nbus_memory_init(memDriver);
+    nBus.memoryInterface = getnbusMemoryInterface();
+    nbus_blink_LED(100);
+
+    nBus.memoryInterface->readHeaderData();
+
+    uint8_t *params = nbus_interface_allParams();
+    uint8_t paramCnt = nbus_interface_allParamsCount();
+    uint32_t paramValue;
+    for (uint32_t index = 1; index <= nBus.interface->getSensorCount(); index++)
+    {
+        for (int paramIndex = 0; paramIndex < paramCnt; paramIndex++)
+        {
+            if (nbus_is_param_active(index, params[paramIndex]))
+            {
+                if (nBus.interface->hasParam(index, params[paramIndex]))
+                {
+                    paramValue = nbus_memory_read_param(index, params[paramIndex]);
+                    nBus.interface->setParam(index, params[paramIndex], paramValue);
+                }
+            }
+        }
+    }
 }
 
 /**
@@ -266,31 +287,36 @@ void nbus_init_memory_driver(nBus_MemoryDriver *memDriver) {
  * - 2 - communication error/timeout. The reception of packed process will be
  * reset.
  */
-void nbus_stack(void) {
+void nbus_stack(void)
+{
 
-  nbus_blink_LED(50);
+    nbus_blink_LED(50);
 
-  while (1) {
-    if (nBus.uart_state == UART_RX_RECEIVED) {
+    while (1)
+    {
+        if (nBus.uart_state == UART_RX_RECEIVED)
+        {
 
-      process_request();
-      nBus.uart_state = UART_RX_WAIT;
+            process_request();
+            nBus.uart_state = UART_RX_WAIT;
 
-      send_response();
+            send_response();
 #if MODULE_MASTER == 1
-      receive_slave_response();
+            receive_slave_response();
 #endif
+        }
+
+        if (nBus.hw_platform->loop_callback != NULL)
+        {
+            switch (nBus.hw_platform->loop_callback())
+            {
+            case 1:
+                nBus.interface->read();
+                break;
+            case 2:
+                nBus.uart_state = UART_RX_WAIT;
+                break;
+            }
+        }
     }
-
-    if (nBus.hw_platform->loop_callback != NULL) {
-      switch (nBus.hw_platform->loop_callback()) {
-      case 1:
-        nBus.interface->read();
-        break;
-      case 2:
-        nBus.uart_state = UART_RX_WAIT;
-        break;
-      }
-    }
-  }
 }

+ 29 - 25
Src/nbus_master_module.c

@@ -10,32 +10,36 @@
 
 #if MODULE_MASTER == 1
 
-void nbus_master_unicastToModuleSet(nBus_TypeDef *nbus) {}
+void nbus_master_unicastToModuleSet(nBus_TypeDef *nbus)
+{
+}
 
-void nbus_master_unicastToModuleGet(nBus_TypeDef *nbus) {
-  //		case CMD_SYNC:
-  {
-    if (nbus->rx_length < 11) {
-      return;
+void nbus_master_unicastToModuleGet(nBus_TypeDef *nbus)
+{
+    //		case CMD_SYNC:
+    {
+        if (nbus->rx_length < 11)
+        {
+            return;
+        }
+
+        RTC_TimeTypeDef sTime = {0};
+        RTC_DateTypeDef sDate = {0};
+
+        sTime.Hours = nbus->rx_buffer[7];
+        sTime.Minutes = nbus->rx_buffer[8];
+        sTime.Seconds = nbus->rx_buffer[9];
+        sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
+        sTime.StoreOperation = RTC_STOREOPERATION_RESET;
+        HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
+
+        sDate.WeekDay = nbus->rx_buffer[6];
+        sDate.Date = nbus->rx_buffer[5];
+        sDate.Month = nbus->rx_buffer[4];
+        sDate.Year = nbus->rx_buffer[3];
+
+        HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
     }
-
-    RTC_TimeTypeDef sTime = {0};
-    RTC_DateTypeDef sDate = {0};
-
-    sTime.Hours = nbus->rx_buffer[7];
-    sTime.Minutes = nbus->rx_buffer[8];
-    sTime.Seconds = nbus->rx_buffer[9];
-    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
-    sTime.StoreOperation = RTC_STOREOPERATION_RESET;
-    HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
-
-    sDate.WeekDay = nbus->rx_buffer[6];
-    sDate.Date = nbus->rx_buffer[5];
-    sDate.Month = nbus->rx_buffer[4];
-    sDate.Year = nbus->rx_buffer[3];
-
-    HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
-  }
-  //		break;
+    //		break;
 }
 #endif

+ 130 - 117
Src/nbus_memory.c

@@ -9,152 +9,165 @@
 
 nBus_MemoryDriver *memoryImplementation;
 
-nBus_memoryInterface_t memory_itnerface = {
-	nbus_memory_store_param,  nbus_memory_read_param, nbus_is_param_active,
-    nbus_read_header_data, nbus_memory_id,  nbus_get_capacity};
+nBus_memoryInterface_t memory_itnerface = {nbus_memory_store_param, nbus_memory_read_param, nbus_is_param_active,
+                                           nbus_read_header_data,   nbus_memory_id,         nbus_get_capacity};
 nBus_memoryHeader_t memoryHeader;
 
-nBus_memoryInterface_t *getnbusMemoryInterface(void) {
-  return &memory_itnerface;
+nBus_memoryInterface_t *getnbusMemoryInterface(void)
+{
+    return &memory_itnerface;
 }
 
 uint8_t memory_buffer[32];
 
-void nbus_memory_init(nBus_MemoryDriver *driver) {
-  memoryImplementation = driver;
-  for (uint32_t i = 0; i < 64; i++) {
-    memoryHeader.params_map[i].param0 = PARAMETER_UNSET;
-    memoryHeader.params_map[i].param1 = PARAMETER_UNSET;
-    memoryHeader.params_map[i].param2 = PARAMETER_UNSET;
-    memoryHeader.params_map[i].param3 = PARAMETER_UNSET;
-  }
+void nbus_memory_init(nBus_MemoryDriver *driver)
+{
+    memoryImplementation = driver;
+    for (uint32_t i = 0; i < 64; i++)
+    {
+        memoryHeader.params_map[i].param0 = PARAMETER_UNSET;
+        memoryHeader.params_map[i].param1 = PARAMETER_UNSET;
+        memoryHeader.params_map[i].param2 = PARAMETER_UNSET;
+        memoryHeader.params_map[i].param3 = PARAMETER_UNSET;
+    }
 }
 
-uint8_t ubus_memory_write4B(uint32_t data, uint16_t adr) {
-	memory_buffer[0] = data>>24;
-	memory_buffer[1] = (data>>16) & 0xFF;
-	memory_buffer[2] = (data>>8) & 0xFF;
-	memory_buffer[3] = (data) & 0xFF;
+uint8_t ubus_memory_write4B(uint32_t data, uint16_t adr)
+{
+    memory_buffer[0] = data >> 24;
+    memory_buffer[1] = (data >> 16) & 0xFF;
+    memory_buffer[2] = (data >> 8) & 0xFF;
+    memory_buffer[3] = (data)&0xFF;
     return memoryImplementation->write_data(memory_buffer, adr, 4);
 }
 
-uint8_t ubus_memory_write2B(uint16_t data, uint16_t adr) {
-	memory_buffer[0] = (data>>8) & 0xFF;
-	memory_buffer[1] = (data) & 0xFF;
+uint8_t ubus_memory_write2B(uint16_t data, uint16_t adr)
+{
+    memory_buffer[0] = (data >> 8) & 0xFF;
+    memory_buffer[1] = (data)&0xFF;
     return memoryImplementation->write_data(memory_buffer, adr, 2);
 }
 
-uint8_t ubus_memory_write1B(uint8_t data, uint16_t adr) {
-	memory_buffer[0] = (data) & 0xFF;
-	return memoryImplementation->write_data(memory_buffer, adr, 1);
+uint8_t ubus_memory_write1B(uint8_t data, uint16_t adr)
+{
+    memory_buffer[0] = (data)&0xFF;
+    return memoryImplementation->write_data(memory_buffer, adr, 1);
 }
 
-
-nBus_memoryState_t nbus_memory_store_param(uint8_t sensor_index, uint8_t param_name,
-                                    uint32_t param_value) {
-  if (param_name >= 16) {
-    return MEMORY_STORE_FAIL;
-  }
-  uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
-  if (addr > memoryImplementation->get_capacity()) {
-    return MEMORY_STORE_FAIL;
-  }
-
-  /*
-   * Write information about validity of parameter for sensor 'sensor_index'. 0 -
-   * parameter not set, 0xAA - parameter is setted
-   */
-  uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
-  switch (param_name % 4) {
-  case 0:
-    memoryHeader.params_map[address_offset].param0 = PARAMETER_VALID;
-    break;
-  case 1:
-    memoryHeader.params_map[address_offset].param1 = PARAMETER_VALID;
-    break;
-  case 2:
-    memoryHeader.params_map[address_offset].param2 = PARAMETER_VALID;
-    break;
-  case 3:
-    memoryHeader.params_map[address_offset].param3 = PARAMETER_VALID;
-    break;
-  }
-
-  ubus_memory_write1B(
-      *(uint8_t *)(&memoryHeader.params_map[address_offset]), sensor_index);
-  return ubus_memory_write4B(param_value, addr) == 0
-             ? MEMORY_STORE_OK
-             : MEMORY_STORE_FAIL;
+nBus_memoryState_t nbus_memory_store_param(uint8_t sensor_index, uint8_t param_name, uint32_t param_value)
+{
+    if (param_name >= 16)
+    {
+        return MEMORY_STORE_FAIL;
+    }
+    uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
+    if (addr > memoryImplementation->get_capacity())
+    {
+        return MEMORY_STORE_FAIL;
+    }
+
+    /*
+     * Write information about validity of parameter for sensor 'sensor_index'. 0 -
+     * parameter not set, 0xAA - parameter is setted
+     */
+    uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
+    switch (param_name % 4)
+    {
+    case 0:
+        memoryHeader.params_map[address_offset].param0 = PARAMETER_VALID;
+        break;
+    case 1:
+        memoryHeader.params_map[address_offset].param1 = PARAMETER_VALID;
+        break;
+    case 2:
+        memoryHeader.params_map[address_offset].param2 = PARAMETER_VALID;
+        break;
+    case 3:
+        memoryHeader.params_map[address_offset].param3 = PARAMETER_VALID;
+        break;
+    }
+
+    ubus_memory_write1B(*(uint8_t *)(&memoryHeader.params_map[address_offset]), sensor_index);
+    return ubus_memory_write4B(param_value, addr) == 0 ? MEMORY_STORE_OK : MEMORY_STORE_FAIL;
 }
 
-uint32_t nbus_memory_read_param(uint8_t sensor_index, uint8_t param_name) {
-  if (param_name >= 16) {
-    return ERROR_VALUE_I32;
-  }
-  uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
-  if (addr > memoryImplementation->get_capacity()) {
-    return ERROR_VALUE_I32;
-  }
-  return memoryImplementation->read_word(addr);
+uint32_t nbus_memory_read_param(uint8_t sensor_index, uint8_t param_name)
+{
+    if (param_name >= 16)
+    {
+        return ERROR_VALUE_I32;
+    }
+    uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
+    if (addr > memoryImplementation->get_capacity())
+    {
+        return ERROR_VALUE_I32;
+    }
+    return memoryImplementation->read_word(addr);
 }
 
-void nbus_read_header_data(void) {
-  uint8_t data;
-  for (uint32_t adr = 0; adr < 63; adr += 4) {
-    data = memoryImplementation->read_byte(adr);
-    memoryHeader.params_map[adr + 0] = *(nBus_MemorySensorParam_t *)(&data);
-    data = memoryImplementation->read_byte(adr + 1);
-    memoryHeader.params_map[adr + 1] = *(nBus_MemorySensorParam_t *)(&data);
-    data = memoryImplementation->read_byte(adr + 2);
-    memoryHeader.params_map[adr + 2] = *(nBus_MemorySensorParam_t *)(&data);
-    data = memoryImplementation->read_byte(adr + 3);
-    memoryHeader.params_map[adr + 3] = *(nBus_MemorySensorParam_t *)(&data);
-  }
+void nbus_read_header_data(void)
+{
+    uint8_t data;
+    for (uint32_t adr = 0; adr < 63; adr += 4)
+    {
+        data = memoryImplementation->read_byte(adr);
+        memoryHeader.params_map[adr + 0] = *(nBus_MemorySensorParam_t *)(&data);
+        data = memoryImplementation->read_byte(adr + 1);
+        memoryHeader.params_map[adr + 1] = *(nBus_MemorySensorParam_t *)(&data);
+        data = memoryImplementation->read_byte(adr + 2);
+        memoryHeader.params_map[adr + 2] = *(nBus_MemorySensorParam_t *)(&data);
+        data = memoryImplementation->read_byte(adr + 3);
+        memoryHeader.params_map[adr + 3] = *(nBus_MemorySensorParam_t *)(&data);
+    }
 }
 
-uint8_t nbus_memory_id(uint8_t *data) {
-  return memoryImplementation->read_id(data);
+uint8_t nbus_memory_id(uint8_t *data)
+{
+    return memoryImplementation->read_id(data);
 }
 
-uint32_t nbus_get_capacity(void) {
-  return memoryImplementation->get_capacity();
+uint32_t nbus_get_capacity(void)
+{
+    return memoryImplementation->get_capacity();
 }
 
-uint8_t nbus_is_param_active(uint8_t sensor_index, uint8_t param_name) {
-  uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
-  uint8_t is_active;
-  switch (param_name % 4) {
-  case 0:
-    is_active = memoryHeader.params_map[address_offset].param0;
-    break;
-  case 1:
-    is_active = memoryHeader.params_map[address_offset].param1;
-    break;
-  case 2:
-    is_active = memoryHeader.params_map[address_offset].param2;
-    break;
-  case 3:
-    is_active = memoryHeader.params_map[address_offset].param3;
-    break;
-  default:
-    is_active = 0;
-  }
-
-  return is_active;
+uint8_t nbus_is_param_active(uint8_t sensor_index, uint8_t param_name)
+{
+    uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
+    uint8_t is_active;
+    switch (param_name % 4)
+    {
+    case 0:
+        is_active = memoryHeader.params_map[address_offset].param0;
+        break;
+    case 1:
+        is_active = memoryHeader.params_map[address_offset].param1;
+        break;
+    case 2:
+        is_active = memoryHeader.params_map[address_offset].param2;
+        break;
+    case 3:
+        is_active = memoryHeader.params_map[address_offset].param3;
+        break;
+    default:
+        is_active = 0;
+    }
+
+    return is_active;
 }
 
 // TODO pouzit upravenu funkciu z drivera: DS28EC20_writeMem
-void memory_params_format(void) {
-	uint8_t empty_data[32] = {
-			0,0,0,0,0,0,0,0,
-			0,0,0,0,0,0,0,0,
-			0,0,0,0,0,0,0,0,
-			0,0,0,0,0,0,0,0};
-  for (uint32_t adr = 0; adr < 64; adr += 32) {
-    memoryImplementation->write_data(empty_data, adr, 32);
-  }
+void memory_params_format(void)
+{
+    uint8_t empty_data[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+    for (uint32_t adr = 0; adr < 64; adr += 32)
+    {
+        memoryImplementation->write_data(empty_data, adr, 32);
+    }
 }
 
-uint8_t getParameterHeaderOffset(uint8_t sensor_index, uint8_t param_name) {
-  return (sensor_index - 1) * 4 + param_name / 4;
+uint8_t getParameterHeaderOffset(uint8_t sensor_index, uint8_t param_name)
+{
+    return (sensor_index - 1) * 4 + param_name / 4;
 }

+ 32 - 25
Src/nbus_slave_broadcast.c

@@ -2,36 +2,43 @@
 
 #if MODULE_SLAVE == 1
 
-inline void setErrorResponse(nBus_TypeDef *nbus, uint8_t code) {
-  nbus->function_code.error = 1;
-  nbus->tx_buffer[4] = code;
-  nbus->tx_length += 1;
+inline void setErrorResponse(nBus_TypeDef *nbus, uint8_t code)
+{
+    nbus->function_code.error = 1;
+    nbus->tx_buffer[4] = code;
+    nbus->tx_length += 1;
 }
 
-void nbus_slave_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type) {
-  if (request_type == BROADCAST_SPECIFIC_SENSORS) {
-  }
-
-  if (request_type == BROADCAST_GLOBAL) {
-    switch (nbus->function_code.function) {
-
-    case CMD_START: {
-      nbus->measure_active = MEASURE_RUNNING;
-      nbus->interface->start();
-      nbus->hw_platform->led_on();
-    } break;
-
-    case CMD_STOP: {
-      nbus->measure_active = MEASURE_STOPPED;
-      nbus->interface->stop();
-      nbus->hw_platform->led_off();
-    } break;
+void nbus_slave_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type)
+{
+    if (request_type == BROADCAST_SPECIFIC_SENSORS)
+    {
+    }
 
-    default:; // nothing
+    if (request_type == BROADCAST_GLOBAL)
+    {
+        switch (nbus->function_code.function)
+        {
+
+        case CMD_START: {
+            nbus->measure_active = MEASURE_RUNNING;
+            nbus->interface->start();
+            nbus->hw_platform->led_on();
+        }
+        break;
+
+        case CMD_STOP: {
+            nbus->measure_active = MEASURE_STOPPED;
+            nbus->interface->stop();
+            nbus->hw_platform->led_off();
+        }
+        break;
+
+        default:; // nothing
+        }
     }
-  }
 
-  nbus->send_response = NO_RESPONSE;
+    nbus->send_response = NO_RESPONSE;
 }
 
 #endif

+ 169 - 151
Src/nbus_slave_module_unicast.c

@@ -2,172 +2,190 @@
 
 #if MODULE_SLAVE == 1
 
-void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus) {
-  switch (nbus->function_code.function) {
-
-  case CMD_ECHO: {
-    for (uint8_t i = 3; i < nbus->rx_length - 1; i++) {
-      nbus->tx_buffer[i + 1] = nbus->rx_buffer[i];
-    }
-    nbus->tx_length += (nbus->rx_length - 4);
-  }
-
-  break;
-
-  case CMD_PARAM: {
-    // same as nbus_unicastToSensorSet
-    int32_t param_value =
-        nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]);
-    if (param_value == PARAM_VALUE_NONE) {
-      setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
-      break;
+void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
+{
+    switch (nbus->function_code.function)
+    {
+
+    case CMD_ECHO: {
+        for (uint8_t i = 3; i < nbus->rx_length - 1; i++)
+        {
+            nbus->tx_buffer[i + 1] = nbus->rx_buffer[i];
+        }
+        nbus->tx_length += (nbus->rx_length - 4);
     }
 
-    nbus->tx_buffer[4] = (uint8_t)(param_value & 0xFF);
-    nbus->tx_buffer[5] = (uint8_t)((param_value >> 8) & 0xFF);
-    nbus->tx_buffer[6] = (uint8_t)((param_value >> 16) & 0xFF);
-    nbus->tx_buffer[7] = (uint8_t)((param_value >> 24) & 0xFF);
-
-    nbus->tx_length += 4;
-  } break;
-
-  case CMD_SENSOR_CNT: {
-    nbus->tx_buffer[4] = nbus->interface->getSensorCount();
-    nbus->tx_length += 1;
-  } break;
-
-  case CMD_DATA: {
-    if (nbus->measure_active == MEASURE_RUNNING) {
-      // response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
-      uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
-      if (cnt == 0) {
-        setErrorResponse(nbus, DEVICE_BUSY);
-      }
-      nbus->tx_length += cnt;
-
-    } else {
-      setErrorResponse(nbus, DEVICE_NOT_READY);
-    }
+    break;
 
-  } break;
+    case CMD_PARAM: {
+        // same as nbus_unicastToSensorSet
+        int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]);
+        if (param_value == PARAM_VALUE_NONE)
+        {
+            setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+            break;
+        }
+
+        nbus->tx_buffer[4] = (uint8_t)(param_value & 0xFF);
+        nbus->tx_buffer[5] = (uint8_t)((param_value >> 8) & 0xFF);
+        nbus->tx_buffer[6] = (uint8_t)((param_value >> 16) & 0xFF);
+        nbus->tx_buffer[7] = (uint8_t)((param_value >> 24) & 0xFF);
+
+        nbus->tx_length += 4;
+    }
+    break;
 
-  case CMD_SENSOR_TYPE: {
-    // response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
-    for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++) {
-      nbus->tx_buffer[4 + 2 * i] = i;
-      nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
-      nbus->tx_length += 2;
+    case CMD_SENSOR_CNT: {
+        nbus->tx_buffer[4] = nbus->interface->getSensorCount();
+        nbus->tx_length += 1;
+    }
+    break;
+
+    case CMD_DATA: {
+        if (nbus->measure_active == MEASURE_RUNNING)
+        {
+            // response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
+            uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
+            if (cnt == 0)
+            {
+                setErrorResponse(nbus, DEVICE_BUSY);
+            }
+            nbus->tx_length += cnt;
+        }
+        else
+        {
+            setErrorResponse(nbus, DEVICE_NOT_READY);
+        }
     }
-  } break;
-
-  case CMD_INFO: {
-    switch (nbus->rx_buffer[3]) {
-    case INFO_MODULE_NAME:
-      nbus->tx_buffer[4] = MODULE_NAME[0];
-      nbus->tx_buffer[5] = MODULE_NAME[1];
-      nbus->tx_buffer[6] = MODULE_NAME[2];
-      nbus->tx_buffer[7] = MODULE_NAME[3];
-      nbus->tx_buffer[8] = MODULE_NAME[4];
-      nbus->tx_buffer[9] = MODULE_NAME[5];
-      nbus->tx_buffer[10] = MODULE_NAME[6];
-      nbus->tx_buffer[11] = MODULE_NAME[7];
-      nbus->tx_length += 8;
-      break;
-    case INFO_MODULE_TYPE:
-      nbus->tx_buffer[4] = MODULE_TYPE[0];
-      nbus->tx_buffer[5] = MODULE_TYPE[1];
-      nbus->tx_buffer[6] = MODULE_TYPE[2];
-      nbus->tx_length += 3;
-      break;
-    case INFO_MODULE_UUID:
-      // Reference manual: Unique device ID registers
-      uint32_t(*unique_id_3) =
-          (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
-      *(nbus->tx_buffer) = (uint32_t)unique_id_3;
-      nbus->tx_length += 4;
-      break;
-    case INFO_MODULE_FW:
-      nbus->tx_buffer[4] = VERSION_FW[0];
-      nbus->tx_buffer[5] = '.';
-      nbus->tx_buffer[6] = VERSION_FW[1];
-      nbus->tx_length += 3;
-      break;
-    case INFO_MODULE_HW:
-      nbus->tx_buffer[4] = VERSION_HW[0];
-      nbus->tx_buffer[5] = '.';
-      nbus->tx_buffer[6] = VERSION_HW[1];
-      nbus->tx_length += 3;
-      break;
-    case INFO_MODULE_MEMORY_ID: {
-      uint8_t n = nbus->memoryInterface->getId(&nbus->tx_buffer[4]);
-      nbus->tx_length += n;
+    break;
+
+    case CMD_SENSOR_TYPE: {
+        // response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
+        for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++)
+        {
+            nbus->tx_buffer[4 + 2 * i] = i;
+            nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
+            nbus->tx_length += 2;
+        }
     }
+    break;
+
+    case CMD_INFO: {
+        switch (nbus->rx_buffer[3])
+        {
+        case INFO_MODULE_NAME:
+            nbus->tx_buffer[4] = MODULE_NAME[0];
+            nbus->tx_buffer[5] = MODULE_NAME[1];
+            nbus->tx_buffer[6] = MODULE_NAME[2];
+            nbus->tx_buffer[7] = MODULE_NAME[3];
+            nbus->tx_buffer[8] = MODULE_NAME[4];
+            nbus->tx_buffer[9] = MODULE_NAME[5];
+            nbus->tx_buffer[10] = MODULE_NAME[6];
+            nbus->tx_buffer[11] = MODULE_NAME[7];
+            nbus->tx_length += 8;
+            break;
+        case INFO_MODULE_TYPE:
+            nbus->tx_buffer[4] = MODULE_TYPE[0];
+            nbus->tx_buffer[5] = MODULE_TYPE[1];
+            nbus->tx_buffer[6] = MODULE_TYPE[2];
+            nbus->tx_length += 3;
+            break;
+        case INFO_MODULE_UUID:
+            // Reference manual: Unique device ID registers
+            uint32_t(*unique_id_3) = (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
+            *(nbus->tx_buffer) = (uint32_t)unique_id_3;
+            nbus->tx_length += 4;
+            break;
+        case INFO_MODULE_FW:
+            nbus->tx_buffer[4] = VERSION_FW[0];
+            nbus->tx_buffer[5] = '.';
+            nbus->tx_buffer[6] = VERSION_FW[1];
+            nbus->tx_length += 3;
+            break;
+        case INFO_MODULE_HW:
+            nbus->tx_buffer[4] = VERSION_HW[0];
+            nbus->tx_buffer[5] = '.';
+            nbus->tx_buffer[6] = VERSION_HW[1];
+            nbus->tx_length += 3;
+            break;
+        case INFO_MODULE_MEMORY_ID: {
+            uint8_t n = nbus->memoryInterface->getId(&nbus->tx_buffer[4]);
+            nbus->tx_length += n;
+        }
+        }
     }
-  } break;
+    break;
 
-  default: {
-    setErrorResponse(nbus, ILLEGAL_FUNCTION);
-  }
-  }
+    default: {
+        setErrorResponse(nbus, ILLEGAL_FUNCTION);
+    }
+    }
 }
 
-void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus) {
-  switch (nbus->function_code.function) {
-  case CMD_PARAM: {
-    // same as nbus_unicastToSensorSet
-    int32_t param_value = nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 |
-                          nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
-    nBus_param_t p = nbus->interface->setParam(
-        0, (nBus_param_t)nbus->rx_buffer[3], param_value);
-    if (p == PARAM_NONE) {
-      setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
-      break;
-    }
-    nbus->tx_buffer[4] = OK_CODE;
-    nbus->tx_length += 1;
-  } break;
-
-  case CMD_DATA: {
-    nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
-    if (nbus->tx_buffer[4] != OK_CODE) {
-      nbus->function_code.error = 1;
+void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
+{
+    switch (nbus->function_code.function)
+    {
+    case CMD_PARAM: {
+        // same as nbus_unicastToSensorSet
+        int32_t param_value =
+            nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
+        nBus_param_t p = nbus->interface->setParam(0, (nBus_param_t)nbus->rx_buffer[3], param_value);
+        if (p == PARAM_NONE)
+        {
+            setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+            break;
+        }
+        nbus->tx_buffer[4] = OK_CODE;
+        nbus->tx_length += 1;
     }
+    break;
 
-    nbus->tx_length += 1;
-  } break;
-
-  case CMD_SLEEP: {
-    nbus->tx_buffer[4] = OK_CODE;
-    nbus->tx_length += 1;
-  } break;
-
-  case CMD_WAKEUP: {
-    nbus->tx_buffer[4] = OK_CODE;
-    nbus->tx_length += 1;
-  }
-  case CMD_RESET: {
-    // POZOR!  cas fomatovania: 0.3s (pri 1W pamati)
-    memory_params_format();
-    nbus->tx_buffer[4] = OK_CODE;
-    nbus->tx_length += 1;
-  } break;
-
-    /*
-      case CMD_STORE: {
-        if (nbus->interface->store != NULL) {
-          nbus->interface->store();
-          nbus->tx_buffer[4] = OK_CODE;
-        } else {
-          nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
+    case CMD_DATA: {
+        nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
+        if (nbus->tx_buffer[4] != OK_CODE)
+        {
+            nbus->function_code.error = 1;
         }
 
         nbus->tx_length += 1;
-      } break;
-    */
-  default: {
-    setErrorResponse(nbus, ILLEGAL_FUNCTION);
-  }
-  }
+    }
+    break;
+
+    case CMD_SLEEP: {
+        nbus->tx_buffer[4] = OK_CODE;
+        nbus->tx_length += 1;
+    }
+    break;
+
+    case CMD_WAKEUP: {
+        nbus->tx_buffer[4] = OK_CODE;
+        nbus->tx_length += 1;
+    }
+    case CMD_RESET: {
+        // POZOR!  cas fomatovania: 0.3s (pri 1W pamati)
+        memory_params_format();
+        nbus->tx_buffer[4] = OK_CODE;
+        nbus->tx_length += 1;
+    }
+    break;
+
+        /*
+          case CMD_STORE: {
+            if (nbus->interface->store != NULL) {
+              nbus->interface->store();
+              nbus->tx_buffer[4] = OK_CODE;
+            } else {
+              nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
+            }
+
+            nbus->tx_length += 1;
+          } break;
+        */
+    default: {
+        setErrorResponse(nbus, ILLEGAL_FUNCTION);
+    }
+    }
 }
 
 #endif

+ 128 - 110
Src/nbus_slave_sensor_unicast.c

@@ -2,132 +2,150 @@
 
 #if MODULE_SLAVE == 1
 
-void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus) {
-  switch (nbus->function_code.function) {
-  case CMD_SENSOR_TYPE: {
-    nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
-    if (t == TYPE_UNKNOWN) {
-      setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
-      break;
+void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus)
+{
+    switch (nbus->function_code.function)
+    {
+    case CMD_SENSOR_TYPE: {
+        nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
+        if (t == TYPE_UNKNOWN)
+        {
+            setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
+            break;
+        }
+        nbus->tx_buffer[4] = t;
+        nbus->tx_length += 1;
     }
-    nbus->tx_buffer[4] = t;
-    nbus->tx_length += 1;
-  } break;
-
-  case CMD_PARAM: {
-    if (nbus->rx_length >= 5) {
-      if (nbus->interface->hasParam(nbus->sensorInfo.address,
-                                    (nBus_param_t)nbus->rx_buffer[3]) == 0) {
-        setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
-        break;
-      }
-      nbus->tx_buffer[4] = nbus->rx_buffer[3];
-      int32_t param_value = nbus->interface->getParam(
-          nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]);
-      nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
-      nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
-      nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
-      nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
-      nbus->tx_length += 5;
-    } else {
-      nBus_param_t *params = nbus_interface_allParams();
-      for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++) {
-        if (nbus->interface->hasParam(nbus->sensorInfo.address, params[i])) {
-          int32_t param_value =
-              nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
-          nbus->tx_buffer[4 + 5 * i] = params[i];
-          // nbus->tx_buffer[5+2*i] =
-          // nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
-          nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
-          nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
-          nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
-          nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
-
-          nbus->tx_length += 5;
+    break;
+
+    case CMD_PARAM: {
+        if (nbus->rx_length >= 5)
+        {
+            if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]) == 0)
+            {
+                setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+                break;
+            }
+            nbus->tx_buffer[4] = nbus->rx_buffer[3];
+            int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]);
+            nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
+            nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
+            nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
+            nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
+            nbus->tx_length += 5;
+        }
+        else
+        {
+            nBus_param_t *params = nbus_interface_allParams();
+            for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++)
+            {
+                if (nbus->interface->hasParam(nbus->sensorInfo.address, params[i]))
+                {
+                    int32_t param_value = nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
+                    nbus->tx_buffer[4 + 5 * i] = params[i];
+                    // nbus->tx_buffer[5+2*i] =
+                    // nbus->interface->getParam(nbus->sensorInfo.address, params[i]);
+                    nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
+                    nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
+                    nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
+                    nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
+
+                    nbus->tx_length += 5;
+                }
+            }
         }
-      }
     }
-  } break;
-
-  case CMD_DATA: {
-    if (nbus->measure_active == MEASURE_RUNNING) {
-      uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address,
-                                             &nbus->tx_buffer[4]);
-      if (cnt == 0) {
-        setErrorResponse(nbus, DEVICE_BUSY);
-        // return;
-      }
-      nbus->tx_length += cnt;
-    } else {
-      setErrorResponse(nbus, DEVICE_NOT_READY);
+    break;
+
+    case CMD_DATA: {
+        if (nbus->measure_active == MEASURE_RUNNING)
+        {
+            uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
+            if (cnt == 0)
+            {
+                setErrorResponse(nbus, DEVICE_BUSY);
+                // return;
+            }
+            nbus->tx_length += cnt;
+        }
+        else
+        {
+            setErrorResponse(nbus, DEVICE_NOT_READY);
+        }
     }
+    break;
 
-  } break;
-
-  default: {
-    setErrorResponse(nbus, ILLEGAL_FUNCTION);
-  }
-  }
+    default: {
+        setErrorResponse(nbus, ILLEGAL_FUNCTION);
+    }
+    }
 }
 
-void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus) {
-  int32_t param_value;
-  switch (nbus->function_code.function) {
-  case CMD_PARAM: {
-    if (!nbus->interface->hasParam(nbus->sensorInfo.address,
-                                   (nBus_param_t)nbus->rx_buffer[3])) {
-      setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
-      break;
-    }
+void nbus_slave_unicastToSensorSet(nBus_TypeDef *nbus)
+{
+    int32_t param_value;
+    switch (nbus->function_code.function)
+    {
+    case CMD_PARAM: {
+        if (!nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3]))
+        {
+            setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+            break;
+        }
 
-    param_value = nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 |
-                  nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
-    nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address,
-                                               (nBus_param_t)nbus->rx_buffer[3],
-                                               param_value);
-    if (p == PARAM_NONE) {
-      setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
-      break;
+        param_value =
+            nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
+        nBus_param_t p =
+            nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->rx_buffer[3], param_value);
+        if (p == PARAM_NONE)
+        {
+            setErrorResponse(nbus, ILLEGAL_DATA_VALUE);
+            break;
+        }
+        nbus->tx_buffer[4] = OK_CODE;
+        nbus->tx_length += 1;
     }
-    nbus->tx_buffer[4] = OK_CODE;
-    nbus->tx_length += 1;
-  } break;
+    break;
 
-  case CMD_DATA: {
-    nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
-    if (nbus->tx_buffer[4] != OK_CODE) {
-      nbus->function_code.error = 1;
+    case CMD_DATA: {
+        nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
+        if (nbus->tx_buffer[4] != OK_CODE)
+        {
+            nbus->function_code.error = 1;
+        }
+        nbus->tx_length += 1;
     }
-    nbus->tx_length += 1;
-  } break;
+    break;
+
+    case CMD_STORE: {
+        nBus_param_t *all_params = nbus_interface_allParams();
+        for (uint32_t i = 0; i < nbus_interface_allParamsCount(); i++)
+        {
+            if (nbus->interface->hasParam(nbus->sensorInfo.address, all_params[i]))
+            {
+                sensor_store_param(nbus, nbus->sensorInfo.address, all_params[i]);
+                // param_value = nbus->interface->getParam(nbus->sensorInfo.address,
+                // all_params[i]);
+                // nbus->memoryInterface->storeParam(nbus->sensorInfo.address,
+                // all_params[i], param_value);
+            }
+        }
 
-  case CMD_STORE: {
-    nBus_param_t *all_params = nbus_interface_allParams();
-    for (uint32_t i = 0; i < nbus_interface_allParamsCount(); i++) {
-      if (nbus->interface->hasParam(nbus->sensorInfo.address, all_params[i])) {
-        sensor_store_param(nbus, nbus->sensorInfo.address, all_params[i]);
-        // param_value = nbus->interface->getParam(nbus->sensorInfo.address,
-        // all_params[i]);
-        // nbus->memoryInterface->storeParam(nbus->sensorInfo.address,
-        // all_params[i], param_value);
-      }
+        nbus->tx_buffer[4] = 1;
+        nbus->tx_length += 1;
     }
+    break;
 
-    nbus->tx_buffer[4] = 1;
-    nbus->tx_length += 1;
-
-  } break;
-
-  default: {
-    setErrorResponse(nbus, ILLEGAL_FUNCTION);
-  }
-  }
+    default: {
+        setErrorResponse(nbus, ILLEGAL_FUNCTION);
+    }
+    }
 }
 
-void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index,
-                        uint8_t param_name) {
-  uint32_t param_value = nbus->interface->getParam(sensor_index, param_name);
-  nbus->memoryInterface->storeParam(sensor_index, param_name, param_value);
+void sensor_store_param(nBus_TypeDef *nbus, uint8_t sensor_index, uint8_t param_name)
+{
+    uint32_t param_value = nbus->interface->getParam(sensor_index, param_name);
+    nbus->memoryInterface->storeParam(sensor_index, param_name, param_value);
 }
 
 #endif