From 7c005b3678f871842242e54709df558c8f2bbf45 Mon Sep 17 00:00:00 2001 From: nrs1g15 <nrs1g15@soton.ac.uk> Date: Fri, 26 Jul 2019 16:17:36 +0100 Subject: [PATCH] Added built in sd writing for new can-sd board and added crc8 to dash board comms --- sufst-controller/.idea/workspace.xml | 436 ++++++++++++++++++++++ sufst-controller/checksum.h | 62 +++ sufst-controller/crc8.c | 106 ++++++ sufst-controller/daq.cpp | 50 +++ sufst-controller/daq.h | 12 + sufst-controller/dashController.cpp | 9 +- sufst-controller/ecuCan.cpp | 71 +--- sufst-controller/ecuOpenloggerTags.h | 19 - sufst-controller/openLoggerController.cpp | 296 --------------- sufst-controller/openLoggerController.h | 49 --- sufst-controller/sd.cpp | 65 ++++ sufst-controller/sd.h | 25 ++ sufst-controller/sufst-controller.ino | 35 +- 13 files changed, 778 insertions(+), 457 deletions(-) create mode 100644 sufst-controller/.idea/workspace.xml create mode 100644 sufst-controller/checksum.h create mode 100644 sufst-controller/crc8.c create mode 100644 sufst-controller/daq.cpp create mode 100644 sufst-controller/daq.h delete mode 100644 sufst-controller/ecuOpenloggerTags.h delete mode 100644 sufst-controller/openLoggerController.cpp delete mode 100644 sufst-controller/openLoggerController.h create mode 100644 sufst-controller/sd.cpp create mode 100644 sufst-controller/sd.h diff --git a/sufst-controller/.idea/workspace.xml b/sufst-controller/.idea/workspace.xml new file mode 100644 index 0000000..83597d5 --- /dev/null +++ b/sufst-controller/.idea/workspace.xml @@ -0,0 +1,436 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="CMakeRunConfigurationManager" shouldGenerate="true" shouldDeleteObsolete="true"> + <generated /> + </component> + <component name="CMakeSettings"> + <configurations> + <configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" /> + </configurations> + </component> + <component name="ChangeListManager"> + <list default="true" id="2070e8be-2952-4d02-bba6-112a351189c8" name="Default Changelist" comment=""> + <change afterPath="$PROJECT_DIR$/daq.cpp" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/daq.h" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/sd.cpp" afterDir="false" /> + <change afterPath="$PROJECT_DIR$/sd.h" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-can-sd-xbee/sufst-can-sd-xbee.ino" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/dashController.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/dashController.cpp" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/ecuCan.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/ecuCan.cpp" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/ecuOpenloggerTags.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/openLoggerController.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/openLoggerController.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/sufst-controller.ino" beforeDir="false" afterPath="$PROJECT_DIR$/sufst-controller.ino" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-dash/sufst-dash.ino" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-dash/sufst-dash.ino" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/codeStyles/Project.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/codeStyles/codeStyleConfig.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/misc.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/modules.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/sufst-openlogger.iml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/vcs.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/.idea/workspace.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/buffer.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/buffer.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/openLogger.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/openLogger.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger/sufst-openlogger.ino" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/codeStyles/Project.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/codeStyles/codeStyleConfig.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/misc.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/modules.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/sufst-openlogger_controller.iml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/vcs.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/workspace.xml" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/buffer.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/buffer.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/can.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/can.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/ecuCan.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/ecuCan.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/ecuOpenloggerTags.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/openLoggerController.cpp" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/openLoggerController.h" beforeDir="false" /> + <change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/sufst-openlogger_controller.ino" beforeDir="false" /> + </list> + <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> + <option name="SHOW_DIALOG" value="false" /> + <option name="HIGHLIGHT_CONFLICTS" value="true" /> + <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> + <option name="LAST_RESOLUTION" value="IGNORE" /> + </component> + <component name="ClangdSettings"> + <option name="formatViaClangd" value="false" /> + </component> + <component name="FileEditorManager"> + <leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/ecuCan.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="272"> + <caret line="21" column="30" selection-start-line="21" selection-start-column="30" selection-end-line="21" selection-end-column="30" /> + <folding> + <element signature="e#39#58#0" expanded="true" /> + <element signature="e#2102#2297#0" expanded="true" /> + <element signature="e#2352#2383#0" expanded="true" /> + <element signature="e#1513#1708#0" expanded="true" /> + <element signature="e#1763#1794#0" expanded="true" /> + <element signature="e#2169#2364#0" expanded="true" /> + <element signature="e#2419#2450#0" expanded="true" /> + <element signature="e#2787#2982#0" expanded="true" /> + <element signature="e#3037#3068#0" expanded="true" /> + <element signature="e#3376#3571#0" expanded="true" /> + <element signature="e#3626#3657#0" expanded="true" /> + <element signature="e#3981#4176#0" expanded="true" /> + <element signature="e#4231#4262#0" expanded="true" /> + <element signature="e#4560#4755#0" expanded="true" /> + <element signature="e#4810#4841#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/dashController.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="308"> + <caret line="37" column="40" lean-forward="true" selection-start-line="37" selection-start-column="40" selection-end-line="37" selection-end-column="40" /> + <folding> + <element signature="e#40#67#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/daq.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="204"> + <caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" /> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/can.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="227"> + <caret line="28" column="15" selection-start-line="28" selection-start-column="15" selection-end-line="28" selection-end-column="15" /> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/daq.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="291"> + <caret line="34" selection-start-line="34" selection-end-line="34" /> + <folding> + <element signature="e#40#56#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/sd.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="410"> + <caret line="36" selection-start-line="36" selection-end-line="36" /> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="true"> + <entry file="file://$PROJECT_DIR$/crc8.c"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="478"> + <caret line="91" selection-start-line="91" selection-end-line="91" /> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/checksum.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="51"> + <caret line="3" column="24" lean-forward="true" selection-start-line="3" selection-start-column="24" selection-end-line="3" selection-end-column="24" /> + <folding> + <element signature="e#1532#1544#0" expanded="true" /> + <element signature="e#1926#1927#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/sd.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="374"> + <caret line="22" column="5" selection-start-line="22" selection-start-column="5" selection-end-line="22" selection-end-column="5" /> + </state> + </provider> + </entry> + </file> + <file pinned="false" current-in-tab="false"> + <entry file="file://$PROJECT_DIR$/sufst-controller.ino"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="478"> + <caret line="39" column="10" selection-start-line="39" selection-start-column="10" selection-end-line="39" selection-end-column="10" /> + <folding> + <element signature="e#57#72#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + </file> + </leaf> + </component> + <component name="FindInProjectRecents"> + <findStrings> + <find>Serial.print</find> + <find>Se</find> + <find>EcuDataOpenlogger</find> + <find>EcuDataStruct</find> + <find>ecuOpenloggerSave</find> + </findStrings> + <replaceStrings> + <replace>EcuDataStruct</replace> + <replace>EcuDataSaveFormat</replace> + <replace>daqEcuWrite</replace> + </replaceStrings> + </component> + <component name="Git.Settings"> + <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." /> + </component> + <component name="IdeDocumentHistory"> + <option name="CHANGED_PATHS"> + <list> + <option value="$PROJECT_DIR$/../sufst-dash/sufst-dash.ino" /> + <option value="$PROJECT_DIR$/sdTags.h" /> + <option value="$PROJECT_DIR$/sdParserTags.h" /> + <option value="$PROJECT_DIR$/dataTags.h" /> + <option value="$PROJECT_DIR$/ecuCan.cpp" /> + <option value="$PROJECT_DIR$/sd.cpp" /> + <option value="$PROJECT_DIR$/sufst-controller.ino" /> + <option value="$PROJECT_DIR$/sd.h" /> + <option value="$PROJECT_DIR$/daq.h" /> + <option value="$PROJECT_DIR$/daq.cpp" /> + <option value="$PROJECT_DIR$/dashController.cpp" /> + <option value="$PROJECT_DIR$/crc8.c" /> + <option value="$PROJECT_DIR$/checksum.h" /> + </list> + </option> + </component> + <component name="ProjectFrameBounds" extendedState="7"> + <option name="width" value="960" /> + <option name="height" value="1030" /> + </component> + <component name="ProjectLevelVcsManager" settingsEditedManually="true" /> + <component name="ProjectView"> + <navigator proportions="" version="1"> + <foldersAlwaysOnTop value="true" /> + </navigator> + <panes> + <pane id="ProjectPane"> + <subPane> + <expand> + <path> + <item name="sufst-controller" type="b2602c69:ProjectViewProjectNode" /> + <item name="sufst-controller" type="462c0819:PsiDirectoryNode" /> + </path> + </expand> + <select /> + </subPane> + </pane> + <pane id="Scope" /> + </panes> + </component> + <component name="PropertiesComponent"> + <property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> + <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> + <property name="WebServerToolWindowFactoryState" value="false" /> + <property name="node.js.detected.package.eslint" value="true" /> + <property name="node.js.detected.package.tslint" value="true" /> + <property name="node.js.path.for.package.eslint" value="project" /> + <property name="node.js.path.for.package.tslint" value="project" /> + <property name="node.js.selected.package.eslint" value="(autodetect)" /> + <property name="node.js.selected.package.tslint" value="(autodetect)" /> + <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" /> + <property name="nodejs_npm_path_reset_for_default_project" value="true" /> + <property name="settings.editor.selected.configurable" value="preferences.sourceCode.C/C++" /> + </component> + <component name="RunDashboard"> + <option name="ruleStates"> + <list> + <RuleState> + <option name="name" value="ConfigurationTypeDashboardGroupingRule" /> + </RuleState> + <RuleState> + <option name="name" value="StatusDashboardGroupingRule" /> + </RuleState> + </list> + </option> + </component> + <component name="SvnConfiguration"> + <configuration /> + </component> + <component name="TaskManager"> + <task active="true" id="Default" summary="Default task"> + <changelist id="2070e8be-2952-4d02-bba6-112a351189c8" name="Default Changelist" comment="" /> + <created>1563993769139</created> + <option name="number" value="Default" /> + <option name="presentableId" value="Default" /> + <updated>1563993769139</updated> + <workItem from="1563993770477" duration="2408000" /> + <workItem from="1564136548177" duration="6195000" /> + </task> + <servers /> + </component> + <component name="TimeTrackingManager"> + <option name="totallyTimeSpent" value="8603000" /> + </component> + <component name="ToolWindowManager"> + <frame x="-7" y="-7" width="1550" height="838" extended-state="7" /> + <editor active="true" /> + <layout> + <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.12080537" /> + <window_info id="Structure" order="1" side_tool="true" weight="0.25" /> + <window_info id="Favorites" order="2" side_tool="true" /> + <window_info anchor="bottom" id="Message" order="0" /> + <window_info anchor="bottom" id="Find" order="1" /> + <window_info anchor="bottom" id="Run" order="2" /> + <window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> + <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> + <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> + <window_info anchor="bottom" id="TODO" order="6" /> + <window_info anchor="bottom" id="Database Changes" order="7" /> + <window_info anchor="bottom" id="Version Control" order="8" /> + <window_info anchor="bottom" id="Terminal" order="9" /> + <window_info anchor="bottom" id="Event Log" order="10" side_tool="true" /> + <window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" /> + <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> + <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> + <window_info anchor="right" id="Database" order="3" /> + </layout> + </component> + <component name="TypeScriptGeneratedFilesManager"> + <option name="version" value="1" /> + </component> + <component name="editorHistoryManager"> + <entry file="file://$PROJECT_DIR$/../sufst-dash/sufst-dash.ino"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="238"> + <caret line="20" column="35" selection-start-line="20" selection-start-column="35" selection-end-line="20" selection-end-column="35" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/openLoggerController.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="-100" /> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/dashController.h"> + <provider selected="true" editor-type-id="text-editor" /> + </entry> + <entry file="file://$PROJECT_DIR$/ecuCan.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="272"> + <caret line="21" column="30" selection-start-line="21" selection-start-column="30" selection-end-line="21" selection-end-column="30" /> + <folding> + <element signature="e#39#58#0" expanded="true" /> + <element signature="e#2102#2297#0" expanded="true" /> + <element signature="e#2352#2383#0" expanded="true" /> + <element signature="e#1513#1708#0" expanded="true" /> + <element signature="e#1763#1794#0" expanded="true" /> + <element signature="e#2169#2364#0" expanded="true" /> + <element signature="e#2419#2450#0" expanded="true" /> + <element signature="e#2787#2982#0" expanded="true" /> + <element signature="e#3037#3068#0" expanded="true" /> + <element signature="e#3376#3571#0" expanded="true" /> + <element signature="e#3626#3657#0" expanded="true" /> + <element signature="e#3981#4176#0" expanded="true" /> + <element signature="e#4231#4262#0" expanded="true" /> + <element signature="e#4560#4755#0" expanded="true" /> + <element signature="e#4810#4841#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/can.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="227"> + <caret line="28" column="15" selection-start-line="28" selection-start-column="15" selection-end-line="28" selection-end-column="15" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/daq.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="204"> + <caret line="12" lean-forward="true" selection-start-line="12" selection-end-line="12" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/daq.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="291"> + <caret line="34" selection-start-line="34" selection-end-line="34" /> + <folding> + <element signature="e#40#56#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/sd.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="410"> + <caret line="36" selection-start-line="36" selection-end-line="36" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/sd.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="374"> + <caret line="22" column="5" selection-start-line="22" selection-start-column="5" selection-end-line="22" selection-end-column="5" /> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/dashController.cpp"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="308"> + <caret line="37" column="40" lean-forward="true" selection-start-line="37" selection-start-column="40" selection-end-line="37" selection-end-column="40" /> + <folding> + <element signature="e#40#67#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/sufst-controller.ino"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="478"> + <caret line="39" column="10" selection-start-line="39" selection-start-column="10" selection-end-line="39" selection-end-column="10" /> + <folding> + <element signature="e#57#72#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/checksum.h"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="51"> + <caret line="3" column="24" lean-forward="true" selection-start-line="3" selection-start-column="24" selection-end-line="3" selection-end-column="24" /> + <folding> + <element signature="e#1532#1544#0" expanded="true" /> + <element signature="e#1926#1927#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/crc8.c"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="478"> + <caret line="91" selection-start-line="91" selection-end-line="91" /> + </state> + </provider> + </entry> + </component> +</project> \ No newline at end of file diff --git a/sufst-controller/checksum.h b/sufst-controller/checksum.h new file mode 100644 index 0000000..ec2b71a --- /dev/null +++ b/sufst-controller/checksum.h @@ -0,0 +1,62 @@ +/* + * Library: libcrc + * File: include/checksum.h + * Author: Lammert Bies + * + * This file is licensed under the MIT License as stated below + * + * Copyright (c) 1999-2016 Lammert Bies + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Description + * ----------- + * The headerfile include/checksum.h contains the definitions and prototypes + * for routines that can be used to calculate several kinds of checksums. + */ + +#ifndef DEF_LIBCRC_CHECKSUM_H +#define DEF_LIBCRC_CHECKSUM_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "Arduino.h" + +/* + * #define CRC_START_xxxx + * + * The constants of the form CRC_START_xxxx define the values that are used for + * initialization of a CRC value for common used calculation methods. + */ + +#define CRC_START_8 0x00 + +/* + * Prototype list of global functions + */ + +uint8_t crc_8(const unsigned char *input_str, unsigned char num_bytes); + +#ifdef __cplusplus +} +#endif + +#endif // DEF_LIBCRC_CHECKSUM_H diff --git a/sufst-controller/crc8.c b/sufst-controller/crc8.c new file mode 100644 index 0000000..6174e70 --- /dev/null +++ b/sufst-controller/crc8.c @@ -0,0 +1,106 @@ +/* + * Library: libcrc + * File: src/crc8.c + * Author: Lammert Bies + * + * This file is licensed under the MIT License as stated below + * + * Copyright (c) 1999-2016 Lammert Bies + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Description + * ----------- + * The source file src/crc8.c contains routines for the calculation of 8 bit + * CRC values according to the calculation rules used in the SHT1x and SHT7x + * series of temperature and humidity sensors. + */ + +#include "checksum.h" + +/* + * static uint8_t sht75_crc_table[]; + * + * The SHT75 humidity sensor is capable of calculating an 8 bit CRC checksum to + * ensure data integrity. The lookup table crc_table[] is used to recalculate + * the CRC. + */ + +static uint8_t sht75_crc_table[] = { + + 0, 49, 98, 83, 196, 245, 166, 151, 185, 136, 219, 234, 125, 76, 31, 46, + 67, 114, 33, 16, 135, 182, 229, 212, 250, 203, 152, 169, 62, 15, 92, 109, + 134, 183, 228, 213, 66, 115, 32, 17, 63, 14, 93, 108, 251, 202, 153, 168, + 197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 30, 47, 184, 137, 218, 235, + 61, 12, 95, 110, 249, 200, 155, 170, 132, 181, 230, 215, 64, 113, 34, 19, + 126, 79, 28, 45, 186, 139, 216, 233, 199, 246, 165, 148, 3, 50, 97, 80, + 187, 138, 217, 232, 127, 78, 29, 44, 2, 51, 96, 81, 198, 247, 164, 149, + 248, 201, 154, 171, 60, 13, 94, 111, 65, 112, 35, 18, 133, 180, 231, 214, + 122, 75, 24, 41, 190, 143, 220, 237, 195, 242, 161, 144, 7, 54, 101, 84, + 57, 8, 91, 106, 253, 204, 159, 174, 128, 177, 226, 211, 68, 117, 38, 23, + 252, 205, 158, 175, 56, 9, 90, 107, 69, 116, 39, 22, 129, 176, 227, 210, + 191, 142, 221, 236, 123, 74, 25, 40, 6, 55, 100, 85, 194, 243, 160, 145, + 71, 118, 37, 20, 131, 178, 225, 208, 254, 207, 156, 173, 58, 11, 88, 105, + 4, 53, 102, 87, 192, 241, 162, 147, 189, 140, 223, 238, 121, 72, 27, 42, + 193, 240, 163, 146, 5, 52, 103, 86, 120, 73, 26, 43, 188, 141, 222, 239, + 130, 179, 224, 209, 70, 119, 36, 21, 59, 10, 89, 104, 255, 206, 157, 172 +}; + +/* + * uint8_t crc_8( const unsigned char *input_str, size_t num_bytes ); + * + * The function crc_8() calculates the 8 bit wide CRC of an input string of a + * given length. + */ + +uint8_t crc_8(const unsigned char *input_str, unsigned char num_bytes) +{ + + size_t a; + uint8_t crc; + const unsigned char *ptr; + + crc = CRC_START_8; + ptr = input_str; + + if (ptr != NULL) + for (a = 0; a < num_bytes; a++) { + + crc = sht75_crc_table[(*ptr) ^ crc]; + ptr++; + } + + return crc; + +} /* crc_8 */ + +/* + * uint8_t update_crc_8( unsigned char crc, unsigned char val ); + * + * Given a databyte and the previous value of the CRC value, the function + * update_crc_8() calculates and returns the new actual CRC value of the data + * comming in. + */ + +uint8_t update_crc_8(unsigned char crc, unsigned char val) +{ + + return sht75_crc_table[val ^ crc]; + +} /* update_crc_8 */ diff --git a/sufst-controller/daq.cpp b/sufst-controller/daq.cpp new file mode 100644 index 0000000..90da9c6 --- /dev/null +++ b/sufst-controller/daq.cpp @@ -0,0 +1,50 @@ +// +// Created by Sil on 26/07/2019. +// + +#include "daq.h" +#include "sd.h" + +struct DaqEcuFormat +{ + uint8_t startTag; + uint32_t timestamp; + uint16_t data; + uint8_t endTag; +}; + +/* + * e.g 1 is rpm and 2 is tps... etc + */ + +const uint8_t daqEcuDataToTag[8][4] = { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16}, + {17, 18, 19, 20}, + {21, 22, 23, 24}, + {25, 26, 27, 28}, + {29, 30, 31, 32} +}; + +void daqEcuWrite(CanMsgTemplate *canMsg, uint8_t index) +{ + static DaqEcuFormat daqEcuFormat; + + uint8_t ecuParamterTag = daqEcuDataToTag[canMsg->idByte[0]][index]; + + daqEcuFormat.startTag = ecuParamterTag; + + daqEcuFormat.timestamp = canMsg->timestamp; + + daqEcuFormat.data = canMsg->data16[index]; + + daqEcuFormat.endTag = ecuParamterTag; + + sdWrite(&daqEcuFormat, sizeof(DaqEcuFormat)); + + /* + * TODO: This is where wireless will happen too + */ +} \ No newline at end of file diff --git a/sufst-controller/daq.h b/sufst-controller/daq.h new file mode 100644 index 0000000..f577556 --- /dev/null +++ b/sufst-controller/daq.h @@ -0,0 +1,12 @@ +// +// Created by Sil on 7/21/2019. +// + +#ifndef DAQ_H +#define DAQ_H + +#include "can.h" + +void daqEcuWrite(CanMsgTemplate *canMsg, uint8_t index); + +#endif //DAQ_H diff --git a/sufst-controller/dashController.cpp b/sufst-controller/dashController.cpp index f4a4794..b2da1bf 100644 --- a/sufst-controller/dashController.cpp +++ b/sufst-controller/dashController.cpp @@ -3,6 +3,7 @@ // #include "dashController.h" +#include "checksum.h" #define DASH_SERIAL_START_BYTE 0x55 @@ -18,8 +19,6 @@ void dashBegin() void dashEcuSetRpm(uint16_t rpm) { - Serial.println(rpm); - gRpm = rpm; } @@ -36,7 +35,7 @@ void dashControllerProcess() if ((currentMs - lastSerialMs) >= DASH_REFRESH_MS) { - static uint8_t serialPayload[5]; + static uint8_t serialPayload[6]; serialPayload[0] = DASH_SERIAL_START_BYTE; @@ -44,6 +43,8 @@ void dashControllerProcess() memcpy(&(serialPayload[3]), &gRpm, 2); - Serial2.write(serialPayload, 5); + serialPayload[6] = crc_8(&(serialPayload[1]), 4); + + Serial2.write(serialPayload, 6); } } diff --git a/sufst-controller/ecuCan.cpp b/sufst-controller/ecuCan.cpp index 3d2acff..0027a47 100644 --- a/sufst-controller/ecuCan.cpp +++ b/sufst-controller/ecuCan.cpp @@ -3,56 +3,9 @@ // #include "ecuCan.h" -#include "openLoggerController.h" -#include "ecuOpenloggerTags.h" +#include "daq.h" #include "dashController.h" -struct EcuDataOpenlogger -{ - uint8_t startTag; - uint32_t timestamp; - uint16_t data; - uint8_t endTag; -}; - -void ecuOpenloggerWrite(EcuDataOpenlogger *ecuDataOpenlogger); - -void ecuOpenloggerSave(CanMsgTemplate *canMsgTemplate, uint8_t index); - -void ecuOpenloggerWrite(EcuDataOpenlogger &ecuDataOpenlogger) -{ - static uint8_t openloggerPayload[8]; - - memcpy(openloggerPayload, &(ecuDataOpenlogger.startTag), 1); - - memcpy(&(openloggerPayload[1]), &(ecuDataOpenlogger.timestamp), 4); - - memcpy(&(openloggerPayload[5]), &(ecuDataOpenlogger.data), 2); - - memcpy(&(openloggerPayload[7]), &(ecuDataOpenlogger.endTag), 1); - - openloggerWrite(openloggerPayload, 8); -} - -void ecuOpenloggerSave(CanMsgTemplate *canMsg, uint8_t index) -{ - static EcuDataOpenlogger ecuDataOpenlogger; - - uint8_t ecuId = canMsg->idByte[0]; - - uint8_t ecuParamterTag = ecuMsgParameterToOpenloggerTag[ecuId][index]; - - ecuDataOpenlogger.startTag = ecuParamterTag; - - ecuDataOpenlogger.timestamp = canMsg->timestamp; - - ecuDataOpenlogger.data = canMsg->data16[index]; - - ecuDataOpenlogger.endTag = ecuParamterTag; - - ecuOpenloggerWrite(ecuDataOpenlogger); -} - void ecuCanMsg0x2000(CanMsgTemplate *canMsg) { #if DEBUG_ECU_CAN @@ -73,9 +26,9 @@ void ecuCanMsg0x2000(CanMsgTemplate *canMsg) uint16_t waterTempC = canMsg->data16[2]; uint16_t airTempC = canMsg->data16[3]; - ecuOpenloggerSave(canMsg, 0); - ecuOpenloggerSave(canMsg, 1); - ecuOpenloggerSave(canMsg, 2); + daqEcuWrite(canMsg, 0); + daqEcuWrite(canMsg, 1); + daqEcuWrite(canMsg, 2); dashEcuSetRpm(rpm); dashEcuSetWaterTemp(waterTempC); @@ -101,7 +54,7 @@ void ecuCanMsg0x2001(CanMsgTemplate *canMsg) uint16_t speedKphx10 = canMsg->data16[2]; uint16_t oilPressKpa = canMsg->data16[3]; - ecuOpenloggerSave(canMsg, 1); + daqEcuWrite(canMsg, 1); } void ecuCanMsg0x2002(CanMsgTemplate *canMsg) @@ -124,7 +77,7 @@ void ecuCanMsg0x2002(CanMsgTemplate *canMsg) uint16_t batteryVx10 = canMsg->data16[2]; uint16_t fuelComsumLpHrx10 = canMsg->data16[3]; - ecuOpenloggerSave(canMsg, 2); + daqEcuWrite(canMsg, 2); // Serial.println((float)(batteryVx10 / 10.0)); } @@ -149,7 +102,7 @@ void ecuCanMsg0x2003(CanMsgTemplate *canMsg) uint16_t injectionTimeMsx100 = canMsg->data16[2]; uint16_t fuelComsumLp100kMx10 = canMsg->data16[3]; - ecuOpenloggerSave(canMsg, 1); + daqEcuWrite(canMsg, 1); } void ecuCanMsg0x2004(CanMsgTemplate *canMsg) @@ -172,7 +125,7 @@ void ecuCanMsg0x2004(CanMsgTemplate *canMsg) uint16_t ana3mV = canMsg->data16[2]; uint16_t camAdvanceDegx10 = canMsg->data16[3]; - ecuOpenloggerSave(canMsg, 3); + daqEcuWrite(canMsg, 3); } void ecuCanMsg0x2005(CanMsgTemplate *canMsg) @@ -195,7 +148,7 @@ void ecuCanMsg0x2005(CanMsgTemplate *canMsg) uint16_t crankErrorsNr = canMsg->data16[2]; uint16_t camErrorsNr = canMsg->data16[3]; - ecuOpenloggerSave(canMsg, 0); + daqEcuWrite(canMsg, 0); } void ecuCanMsg0x2006(CanMsgTemplate *canMsg) @@ -239,9 +192,9 @@ void ecuCanMsg0x2007(CanMsgTemplate *canMsg) uint16_t lambdaPidAdjPercx10 = canMsg->data16[2]; uint16_t ecuSwitchesBitField = canMsg->data16[3]; // - ecuOpenloggerSave(canMsg, 0); - ecuOpenloggerSave(canMsg, 1); - ecuOpenloggerSave(canMsg, 2); + daqEcuWrite(canMsg, 0); + daqEcuWrite(canMsg, 1); + daqEcuWrite(canMsg, 2); } void ecuCanMsg0x2008(CanMsgTemplate *canMsg) diff --git a/sufst-controller/ecuOpenloggerTags.h b/sufst-controller/ecuOpenloggerTags.h deleted file mode 100644 index c0bc6d3..0000000 --- a/sufst-controller/ecuOpenloggerTags.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Sil on 7/21/2019. -// - -#ifndef ECUOPENLOGGERTAGS_H -#define ECUOPENLOGGERTAGS_H - -uint8_t ecuMsgParameterToOpenloggerTag[8][4] = { - {1, 2, 3, 4}, - {5, 6, 7, 8}, - {9, 10, 11, 12}, - {13, 14, 15, 16}, - {17, 18, 19, 20}, - {21, 22, 23, 24}, - {25, 26, 27, 28}, - {29, 30, 31, 32} -}; - -#endif //ECUOPENLOGGERTAGS_H diff --git a/sufst-controller/openLoggerController.cpp b/sufst-controller/openLoggerController.cpp deleted file mode 100644 index d530acb..0000000 --- a/sufst-controller/openLoggerController.cpp +++ /dev/null @@ -1,296 +0,0 @@ -// -// Created by Sil on 14/07/2019. -// - -#include "openLoggerController.h" -#include "buffer.h" - -enum -{ - uartStartByte = 's', - uartStopByte = 'f' -}; - -enum -{ - uartTypeData = 0, - uartTypeCommand, - uartTypeInfo -}; - -enum -{ - uartCommandStartWrites = 0, - uartCommandStopWrites -}; - -enum -{ - uartInfoAlreadyRunning = 0, - uartInfoAlreadyIdle, - uartInfoWritesStarted, - uartInfoWritesStartError, - uartInfoStopped, - uartInfoStopError, - uartInfoWriteError, - uartInfoWritesNotStarted -}; - -typedef enum -{ - idle = 0, - running, - failedToStart, - failedToStop, - failToWrite -} OpenloggerState; - -typedef enum -{ - checkingForStart = 0, - checkingForType, - checkingForInfo -} OpenloggerUartRxState; - -static uint8_t openloggerTxDataBuffer[OPENLOGGER_TX_BUFFER_LEN]; - -static CirBuffer openloggerTxDataCirBuffer; - -static uint8_t openloggerTxCommandBuffer[OPENLOGGER_TX_COMMAND_BUFFER_LEN]; - -static CirBuffer openloggerTxCommandCirBuffer; - -OpenloggerState openloggerState = idle; - -OpenloggerUartRxState openloggerUartRxState = checkingForStart; - -static bool uartCurrentlyTransmitting = false; - -void openloggerIdle(); - -void openloggerRunning(); - -void openloggerFailedToStart(); - -void openloggerFailedToStop(); - -void openloggerFailedToWrite(); - -void openloggerRxCheckingForStart(); - -void openloggerRxCheckingForType(); - -void openloggerRxCheckingForInfo(); - -/* - * openloggerState_ptrs correspond to INFO packets, so e.g if OPENLOGGER_INFO_WRITES_STARTED (0x02) is seen then - * [2] is called which is openloggerRunning. - */ - -OpenloggerState rxInfoTypeToStateLookup[8] = {running, idle, running, failedToStart, idle, failedToStop, - failToWrite, idle}; - -void (*openloggerState_ptrs[8])(void) = {openloggerIdle, openloggerRunning, - openloggerFailedToStart, openloggerFailedToStop, - openloggerFailedToWrite}; - -void (*openloggerUartRxState_ptrs[3])(void) = {openloggerRxCheckingForStart, openloggerRxCheckingForType, - openloggerRxCheckingForInfo}; - -void serialEvent1() -{ - openloggerUartRxState_ptrs[openloggerUartRxState](); -} - -void openloggerRxCheckingForStart() -{ - -#if DEBUG_OPENLOGGER - Serial.print("OPLOG: CHECKING START "); - Serial.println(Serial1.peek()); -#endif // DEBUG_OPENLOGGER - - if (Serial1.read() == uartStartByte) { - openloggerUartRxState = checkingForType; - } -} - -void openloggerRxCheckingForType() -{ -#if DEBUG_OPENLOGGER - Serial.print("OPLOG: CHECKING FOR TYPE "); - Serial.println(Serial1.peek()); -#endif // DEBUG_OPENLOGGER - - if (Serial1.read() == uartTypeInfo) { - openloggerUartRxState = checkingForInfo; - } - else { - openloggerUartRxState = checkingForStart; - } -} - -void openloggerRxCheckingForInfo() -{ -#if DEBUG_OPENLOGGER - Serial.print("OPLOG: CHECKING FOR INFO "); - Serial.println(Serial1.peek()); -#endif // DEBUG_OPENLOGGER - /* - * The value received from the openlogger dictates what state we are in. - */ - openloggerState = rxInfoTypeToStateLookup[Serial1.read()]; - - openloggerUartRxState = checkingForStart; - -} - -void openloggerIdle() -{ - -} - -void openloggerRunning() -{ - /* - * The uart Tx buffer has a capacity of 64 bytes, if we try to write more than this then Serial.write becomes - * blocking, so we want to avoid this at all costs. - * - */ - - static uint8_t uartTxPayload[64]; - - static uint16_t uartAmountToTransmit = 0; - - static uint32_t uartLastTransmissionMs = millis(); - - uint32_t uartCurrentMs = millis(); - - uint16_t uartTxDataAmount = cirBufferAvailable(&openloggerTxDataCirBuffer); - - if (uartTxDataAmount > 0) { - - if ((uartCurrentMs - uartLastTransmissionMs) >= 5) { - if (Serial1.availableForWrite() >= 35) { - - uint8_t uartTxBytes = (uartTxDataAmount >= 32) ? 32 : uartTxDataAmount; - - Serial1.write(uartStartByte); - Serial1.write(uartTypeData); - Serial1.write(uartTxBytes); - - cirBufferReadBytes(&openloggerTxDataCirBuffer, uartTxPayload, uartTxBytes); - - Serial1.write(uartTxPayload, uartTxBytes); - - uartLastTransmissionMs = millis(); - } - } - } -} - -void openloggerFailedToStart() -{ -#if DEBUG_OPENLOGGER - Serial.println("OPLOG: FAILED TO START"); -#endif // DEBUG_OPENLOGGER - - openloggerState = idle; -} - -void openloggerFailedToStop() -{ -#if DEBUG_OPENLOGGER - Serial.println("OPLOG: FAILED TO STOP"); -#endif // DEBUG_OPENLOGGER - - openloggerState = idle; -} - -void openloggerFailedToWrite() -{ -#if DEBUG_OPENLOGGER - Serial.println("OPLOG: FAILED TO WRITE"); -#endif // DEBUG_OPENLOGGER - - openloggerState = idle; -} - -void openloggerBegin(uint32_t serialBaud) -{ - - Serial1.begin(serialBaud); - - cirBufferBegin(&openloggerTxDataCirBuffer, - openloggerTxDataBuffer, - OPENLOGGER_TX_BUFFER_LEN, - sizeof(uint8_t)); - - cirBufferBegin(&openloggerTxCommandCirBuffer, - openloggerTxCommandBuffer, - OPENLOGGER_TX_COMMAND_BUFFER_LEN, - sizeof(uint8_t)); - -#if DEBUG_OPENLOGGER - Serial.println("OPLOG: BEGIN FINISHED"); -#endif // DEBUG_OPENLOGGER -} - -void openloggerBeginWrites() -{ - openloggerWriteCommand(uartCommandStartWrites); - -#if DEBUG_OPENLOGGER - Serial.println("OPLOG: OPLOG STARTING WRITES"); -#endif // DEBUG_OPENLOGGER -} - -void openloggerStopWrites() -{ - openloggerWriteCommand(uartCommandStopWrites); - -#if DEBUG_OPENLOGGER - Serial.println("OPLOG: OPLOG STOPPING WRITES"); -#endif // DEBUG_OPENLOGGER -} - -void openloggerProcess() -{ - if (!uartCurrentlyTransmitting) { - if (cirBufferAvailable(&openloggerTxCommandCirBuffer) > 0) { - -#if DEBUG_OPENLOGGER - Serial.print("OPLOG: COMMAND "); - Serial.println(*((uint8_t *)cirBufferPeek(&openloggerTxCommandCirBuffer)), HEX); -#endif // DEBUG_OPENLOGGER - - Serial1.write(uartStartByte); - Serial1.write(uartTypeCommand); - Serial1.write(*((uint8_t *) cirBufferExternalRead(&openloggerTxCommandCirBuffer))); - Serial1.flush(); - } - } - - openloggerState_ptrs[openloggerState](); -} - -void openloggerWrite(void *pPayload, uint8_t payloadLen) -{ - - if (openloggerState == running) { - cirBufferWriteBytes(&openloggerTxDataCirBuffer, pPayload, payloadLen); - } -} - -void openloggerWriteCommand(uint8_t command) -{ - -#if DEBUG_OPENLOGGER - Serial.print("OPLOG: APPENDING COMMAND "); - Serial.println(command, HEX); - Serial.flush(); -#endif // DEBUG_OPENLOGGER - - cirBufferWrite(&openloggerTxCommandCirBuffer, &command); - -} diff --git a/sufst-controller/openLoggerController.h b/sufst-controller/openLoggerController.h deleted file mode 100644 index fd48ca6..0000000 --- a/sufst-controller/openLoggerController.h +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by Sil on 14/07/2019. -// - -#ifndef SUFST_OPENLOGGER_INO_SDCARDMASTER_H -#define SUFST_OPENLOGGER_INO_SDCARDMASTER_H - -#include "Arduino.h" - -#define DEBUG_OPENLOGGER 0 - -#define OPENLOGGER_TX_BUFFER_LEN 1024 -#define OPENLOGGER_TX_COMMAND_BUFFER_LEN 8 - -/* - * The serial port is hard coded to serial1 so ensure openlogger is connected to serial1. - * - * There are three types of transactions: save this data, do this command or heres some info. - * - * STRUCTURE OF A VALID SD CARD UART SAVE THIS DATA TRANSACTION - * - * START BYTE | DATA TYPE | LENGTH BYTE | DATA BYTES | STOP BYTE - * - * STRUCTURE OF A VALID COMMAND TRANSACTION - * - * START BYTE | COMMAND TYPE | COMMAND BYTE | STOP BYTE - * - * STUCTURE OF VALID INFO TRANSACTION - * - * START BYTE | INFO TYPE | INFO BYTE | STOP BYTE - * - * If no rx has been received for more than 500ms the rx state is reset to checking for start byte - * This ensures the open logger does not get stuck in the middle of a transaction if the master stops sending data - * - */ -void openloggerBegin(uint32_t serialBaud); - -void openloggerBeginWrites(); - -void openloggerStopWrites(); - -void openloggerProcess(); - -void openloggerWrite(void *pPayload, uint8_t payloadLen); - -void openloggerWriteCommand(uint8_t command); - - -#endif //SUFST_OPENLOGGER_INO_SDCARDMASTER_H diff --git a/sufst-controller/sd.cpp b/sufst-controller/sd.cpp new file mode 100644 index 0000000..6928449 --- /dev/null +++ b/sufst-controller/sd.cpp @@ -0,0 +1,65 @@ +// +// Created by Sil on 26/07/2019. +// + +#include "sd.h" +#include "buffer.h" +#include "SdFat.h" + +SdFat sd; + +SdFile file; + +uint8_t sdBuffer[SD_BUFFER_LEN]; + +CirBuffer sdCirBuffer; + +uint8_t sdBegin() +{ + if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(10))) { + return 0; + } + + if (sd.exists(SD_FILE_NAME)) { + if (!file.open(SD_FILE_NAME, O_WRITE | O_APPEND)) { + return 0; + } + } + else { + if (!file.open(SD_FILE_NAME, O_WRITE | O_CREAT)) { + return 0; + } + } + + if (!cirBufferBegin(&sdCirBuffer, sdBuffer, SD_BUFFER_LEN, sizeof(uint8_t))) { + return 0; + } + + return 1; +} + +void sdWrite(void *data, uint8_t len) +{ + cirBufferWriteBytes(&sdCirBuffer, data, len); +} + +void sdProcess() +{ + static uint8_t sdPayload[SD_WRITE_LEN]; + static uint8_t sdWritesCntBeforeFlush = 0; + + if (cirBufferAvailable(&sdCirBuffer) >= SD_WRITE_LEN) { + + cirBufferReadBytes(&sdCirBuffer, sdPayload, SD_WRITE_LEN); + + file.write(sdPayload, SD_WRITE_LEN); + + sdWritesCntBeforeFlush++; + + if (sdWritesCntBeforeFlush <= SD_WRITES_BEFORE_FLUSH) { + file.flush(); + + sdWritesCntBeforeFlush = 0; + } + } +} \ No newline at end of file diff --git a/sufst-controller/sd.h b/sufst-controller/sd.h new file mode 100644 index 0000000..bbe13ef --- /dev/null +++ b/sufst-controller/sd.h @@ -0,0 +1,25 @@ +// +// Created by Sil on 26/07/2019. +// + +#ifndef SD_H +#define SD_H + +#include "Arduino.h" +/* + * For multiple SPIs connected to the sd SPI #define USE_STANDARD_SPI_LIBRARY 1 must be in in SdFatConfig.h. + */ + +#define SD_CS_PIN 0 +#define SD_BUFFER_LEN 256 +#define SD_WRITE_LEN 64 +#define SD_FILE_NAME "DATA.BIN" +#define SD_WRITES_BEFORE_FLUSH 8 + +uint8_t sdBegin(); + +void sdWrite(void *data, uint8_t len); + +void sdProcess(); + +#endif //SD_H diff --git a/sufst-controller/sufst-controller.ino b/sufst-controller/sufst-controller.ino index 999c3bd..8d39956 100644 --- a/sufst-controller/sufst-controller.ino +++ b/sufst-controller/sufst-controller.ino @@ -1,7 +1,7 @@ #define DEBUG_ECU_CAN 0 #define DEBUG_ECU_CAN_MINIMAL 0 -#include "openLoggerController.h" +#include "sd.h" #include "ecuCan.h" #include "can.h" #include "dashController.h" @@ -11,8 +11,6 @@ #define OPENLOGGER_SERIAL_BUAD_RATE 115200 -uint8_t arduinoProcessSerial(); - void setup() { // put your setup code here, to run once: @@ -25,8 +23,7 @@ void setup() dashBegin(); - // Openlogger runs on uart port TX1 RX1 (Serial1) - //openloggerBegin(OPENLOGGER_SERIAL_BUAD_RATE); + sdBegin(); canBegin(); } @@ -38,31 +35,9 @@ void loop() /* * ALL PROCESSES *MUST* BE NON BLOCKING */ - //wdt_reset(); - - openloggerProcess(); - arduinoProcessSerial(); canProcessRx(); dashControllerProcess(); - //canProcessTx(); -} - -uint8_t arduinoProcessSerial() -{ - if (Serial.available()) { - - uint8_t serialByte = Serial.read(); + sdProcess(); - switch (serialByte) { - case 'g':openloggerBeginWrites(); - - break; - - case 's':openloggerStopWrites(); - - break; - } - } - - return 1; -} + //canProcessTx(); +} \ No newline at end of file -- GitLab