Skip to content
Snippets Groups Projects
Commit 94718d11 authored by nrs1g15's avatar nrs1g15
Browse files

Working implementation tested working up to 3kbytes per second without errors

parent e329c7f3
Branches
No related tags found
No related merge requests found
......@@ -10,12 +10,20 @@
</component>
<component name="ChangeListManager">
<list default="true" id="81322c4e-6adb-4223-9d00-22249012da89" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/codeStyles/Project.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/codeStyles/codeStyleConfig.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/buffer.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/buffer.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/openLogger.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/openLogger.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/openLogger.h" beforeDir="false" afterPath="$PROJECT_DIR$/openLogger.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/codeStyles/Project.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/codeStyles/Project.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/can.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/can.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/can.h" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/can.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/circularBuffer.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/circularBuffer.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/circularBuffer.h" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/circularBuffer.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/openLoggerController.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/openLoggerController.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/openLoggerController.h" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/openLoggerController.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sufst-openlogger_controller/sufst-openlogger_controller.ino" beforeDir="false" afterPath="$PROJECT_DIR$/../sufst-openlogger_controller/sufst-openlogger_controller.ino" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
......@@ -34,47 +42,68 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/sufst-openlogger.ino">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="221">
<caret line="13" selection-start-line="13" selection-end-line="13" />
<state relative-caret-position="289">
<caret line="17" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/openLogger.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="188" column="17" selection-start-line="188" selection-start-column="17" selection-end-line="188" selection-end-column="17" />
<state relative-caret-position="517">
<caret line="212" column="35" selection-start-line="212" selection-start-column="35" selection-end-line="212" selection-end-column="35" />
<folding>
<element signature="e#2005#2079#0" expanded="true" />
<element signature="e#2252#2329#0" expanded="true" />
<element signature="e#2602#2639#0" expanded="true" />
<element signature="e#2816#2891#0" expanded="true" />
<element signature="e#3157#3230#0" expanded="true" />
<element signature="e#3807#3846#0" expanded="true" />
<element signature="e#4619#4695#0" expanded="true" />
<element signature="e#4979#5020#0" expanded="true" />
<element signature="e#5475#5510#0" expanded="true" />
<element signature="e#6078#6120#0" expanded="true" />
<element signature="e#6175#6206#0" expanded="true" />
<element signature="e#6743#6784#0" expanded="true" />
<element signature="e#6919#6999#0" expanded="true" />
<element signature="e#7100#7140#0" expanded="true" />
<element signature="e#7231#7276#0" expanded="true" />
<element signature="e#7376#7460#0" expanded="true" />
<element signature="e#7577#7612#0" expanded="true" />
<element signature="e#7671#7706#0" expanded="true" />
<element signature="e#7816#7896#0" expanded="true" />
<element signature="e#180#665#0" expanded="true" />
<element signature="e#2546#2627#0" expanded="true" />
<element signature="e#2800#2884#0" expanded="true" />
<element signature="e#3157#3194#0" expanded="true" />
<element signature="e#3371#3453#0" expanded="true" />
<element signature="e#3725#3802#0" expanded="true" />
<element signature="e#5088#5171#0" expanded="true" />
<element signature="e#5408#5449#0" expanded="true" />
<element signature="e#5904#5939#0" expanded="true" />
<element signature="e#5602#5641#0" expanded="true" />
<element signature="e#6603#6674#0" expanded="true" />
<element signature="e#7284#7325#0" expanded="true" />
<element signature="e#7460#7540#0" expanded="true" />
<element signature="e#7641#7681#0" expanded="true" />
<element signature="e#7772#7817#0" expanded="true" />
<element signature="e#7917#8001#0" expanded="true" />
<element signature="e#8118#8153#0" expanded="true" />
<element signature="e#8212#8247#0" expanded="true" />
<element signature="e#8357#8437#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/buffer.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="577">
<caret line="89" column="51" selection-start-line="89" selection-start-column="51" selection-end-line="89" selection-end-column="51" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/buffer.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="561">
<caret line="33" column="22" selection-start-line="33" selection-start-column="22" selection-end-line="33" selection-end-column="22" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/openLogger.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="323">
<caret line="19" selection-start-line="19" selection-end-line="19" />
<state relative-caret-position="340">
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" />
<folding>
<element signature="e#188#230#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
......@@ -83,12 +112,6 @@
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>DEBUG_SD</find>
<find>DEBUG</find>
<find>serk</find>
<find>512</find>
<find>digital</find>
<find>OPENLOGGER_INFO_WRITES_STOPPED</find>
<find>read</find>
<find>Read</find>
<find>Write</find>
......@@ -113,12 +136,19 @@
<find>sdRx</find>
<find>uartTxBufferUsedCapacity</find>
<find>openloggerBeginSd</find>
<find>wdt</find>
<find>[</find>
<find>bein</find>
<find>unitSizeBytes</find>
<find>sdB</find>
<find>sdBufferUsedBytes</find>
</findStrings>
<replaceStrings>
<replace>DEBUG_OPENLOGGER</replace>
<replace>rxUartByte</replace>
<replace>uartStopByte</replace>
<replace>sdRxBufferUsedCapacity</replace>
<replace>unitSize</replace>
</replaceStrings>
</component>
<component name="Git.Settings">
......@@ -130,23 +160,38 @@
<option value="$PROJECT_DIR$/circularBuffer.h" />
<option value="$PROJECT_DIR$/circularBuffer.cpp" />
<option value="$PROJECT_DIR$/sufst-openlogger.ino" />
<option value="$PROJECT_DIR$/openLogger.h" />
<option value="$PROJECT_DIR$/buffer.h" />
<option value="$PROJECT_DIR$/openLogger.cpp" />
<option value="$PROJECT_DIR$/buffer.cpp" />
<option value="$PROJECT_DIR$/openLogger.h" />
</list>
</option>
</component>
<component name="OCFindUsagesOptions" text="true" ivars="false" properties="true" derivedClasses="false" />
<component name="ProjectFrameBounds">
<option name="x" value="501" />
<option name="width" value="1428" />
<option name="height" value="1038" />
<option name="x" value="990" />
<option name="width" value="2861" />
<option name="height" value="2111" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes />
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="sufst-openlogger" type="b2602c69:ProjectViewProjectNode" />
<item name="sufst-openlogger" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
</panes>
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
......@@ -190,19 +235,19 @@
<workItem from="1563216461708" duration="10620000" />
<workItem from="1563385421018" duration="9848000" />
<workItem from="1563401015844" duration="2752000" />
<workItem from="1563476541945" duration="14446000" />
<workItem from="1563476541945" duration="26985000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="61566000" />
<option name="totallyTimeSpent" value="74105000" />
</component>
<component name="ToolWindowManager">
<frame x="401" y="0" width="1142" height="830" extended-state="0" />
<frame x="660" y="0" width="1907" height="1407" extended-state="0" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49856734" weight="0.522686" />
<window_info active="true" id="Structure" order="1" sideWeight="0.50143266" side_tool="true" visible="true" weight="0.25878003" />
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49803922" visible="true" weight="0.19978343" />
<window_info active="true" id="Structure" order="1" sideWeight="0.5019608" side_tool="true" visible="true" weight="0.19978343" />
<window_info id="Favorites" order="2" side_tool="true" weight="0.32916266" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
......@@ -227,44 +272,61 @@
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/circularBuffer.cpp" />
<entry file="file://$PROJECT_DIR$/circularBuffer.h" />
<entry file="file://$PROJECT_DIR$/sufst-openlogger.ino">
<entry file="file://$PROJECT_DIR$/buffer.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="221">
<caret line="13" selection-start-line="13" selection-end-line="13" />
<state relative-caret-position="577">
<caret line="89" column="51" selection-start-line="89" selection-start-column="51" selection-end-line="89" selection-end-column="51" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/openLogger.h">
<entry file="file://$PROJECT_DIR$/buffer.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="323">
<caret line="19" selection-start-line="19" selection-end-line="19" />
<state relative-caret-position="561">
<caret line="33" column="22" selection-start-line="33" selection-start-column="22" selection-end-line="33" selection-end-column="22" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/openLogger.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="188" column="17" selection-start-line="188" selection-start-column="17" selection-end-line="188" selection-end-column="17" />
<state relative-caret-position="517">
<caret line="212" column="35" selection-start-line="212" selection-start-column="35" selection-end-line="212" selection-end-column="35" />
<folding>
<element signature="e#180#665#0" expanded="true" />
<element signature="e#2546#2627#0" expanded="true" />
<element signature="e#2800#2884#0" expanded="true" />
<element signature="e#3157#3194#0" expanded="true" />
<element signature="e#3371#3453#0" expanded="true" />
<element signature="e#3725#3802#0" expanded="true" />
<element signature="e#5088#5171#0" expanded="true" />
<element signature="e#5408#5449#0" expanded="true" />
<element signature="e#5904#5939#0" expanded="true" />
<element signature="e#5602#5641#0" expanded="true" />
<element signature="e#6603#6674#0" expanded="true" />
<element signature="e#7284#7325#0" expanded="true" />
<element signature="e#7460#7540#0" expanded="true" />
<element signature="e#7641#7681#0" expanded="true" />
<element signature="e#7772#7817#0" expanded="true" />
<element signature="e#7917#8001#0" expanded="true" />
<element signature="e#8118#8153#0" expanded="true" />
<element signature="e#8212#8247#0" expanded="true" />
<element signature="e#8357#8437#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/sufst-openlogger.ino">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="289">
<caret line="17" selection-start-line="17" selection-end-line="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/openLogger.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="340">
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" />
<folding>
<element signature="e#2005#2079#0" expanded="true" />
<element signature="e#2252#2329#0" expanded="true" />
<element signature="e#2602#2639#0" expanded="true" />
<element signature="e#2816#2891#0" expanded="true" />
<element signature="e#3157#3230#0" expanded="true" />
<element signature="e#3807#3846#0" expanded="true" />
<element signature="e#4619#4695#0" expanded="true" />
<element signature="e#4979#5020#0" expanded="true" />
<element signature="e#5475#5510#0" expanded="true" />
<element signature="e#6078#6120#0" expanded="true" />
<element signature="e#6175#6206#0" expanded="true" />
<element signature="e#6743#6784#0" expanded="true" />
<element signature="e#6919#6999#0" expanded="true" />
<element signature="e#7100#7140#0" expanded="true" />
<element signature="e#7231#7276#0" expanded="true" />
<element signature="e#7376#7460#0" expanded="true" />
<element signature="e#7577#7612#0" expanded="true" />
<element signature="e#7671#7706#0" expanded="true" />
<element signature="e#7816#7896#0" expanded="true" />
<element signature="e#188#230#0" expanded="true" />
</folding>
</state>
</provider>
......
//
// Created by Sil on 7/19/2019.
//
#include "buffer.h"
uint8_t cirBufferBegin(cirBuffer_handle cirBuff, void *data, uint16_t len, uint8_t unitSize)
{
if (cirBuff == NULL) {
return 0;
}
if (data == NULL) {
return 0;
}
cirBuff->head = 0;
cirBuff->tail = 0;
cirBuff->capacity = len;
cirBuff->full = 0;
cirBuff->data = (uint8_t *) data;
cirBuff->unitSize = unitSize;
return 1;
}
uint16_t cirBufferAvailable(cirBuffer_handle cirBuff)
{
if (!cirBuff->full) {
if (cirBuff->head >= cirBuff->tail) {
return (cirBuff->head - cirBuff->tail);
}
else {
return (cirBuff->capacity + cirBuff->head - cirBuff->tail);
}
}
else {
return cirBuff->capacity;
}
}
uint16_t cirBufferAvailableForWrite(cirBuffer_handle cirBuff)
{
return cirBuff->capacity - cirBufferAvailable(cirBuff);
}
void cirBufferWrite(cirBuffer_handle cirBuff, void *unit)
{
memcpy(&(cirBuff->data[cirBuff->head * cirBuff->unitSize]), unit, cirBuff->unitSize);
++cirBuff->head &= (cirBuff->capacity - 1);
if (cirBuff->head == cirBuff->tail) {
cirBuff->full = 1;
++cirBuff->tail &= (cirBuff->capacity - 1);
}
}
void cirBufferWriteBytes(cirBuffer_handle cirBuff, void *src, uint16_t len)
{
uint16_t head = cirBuff->head;
uint16_t andOperator = cirBuff->capacity - 1;
uint16_t remainingBytes = cirBufferAvailableForWrite(cirBuff);
if ((remainingBytes + len) >= cirBuff->capacity) {
cirBuff->full = 1;
}
for (uint16_t i = 0; i < len; i++) {
cirBuff->data[head] = ((uint8_t *) src)[i];
++head &= andOperator;
}
if (cirBuff->full) {
cirBuff->tail = (cirBuff->tail + (len - remainingBytes)) & cirBuff->capacity;
}
cirBuff->head = head;
cirBuff->full = 0;
}
uint8_t *cirBufferExternalWrite(cirBuffer_handle cirBuff)
{
uint8_t *head = &(cirBuff->data[cirBuff->head]);
++cirBuff->head &= cirBuff->capacity - 1;
if (cirBuff->head == cirBuff->tail) {
cirBuff->full = 1;
++cirBuff->tail &= (cirBuff->capacity - 1);
}
return head;
}
void cirBufferRead(cirBuffer_handle cirBuff, void *loc)
{
memcpy(loc, &(cirBuff->data[cirBuff->tail * cirBuff->unitSize]), cirBuff->unitSize);
++cirBuff->tail &= (cirBuff->capacity - 1);
cirBuff->full = 0;
}
void cirBufferReadBytes(cirBuffer_handle cirBuff, void *loc, uint16_t len)
{
uint16_t tail = cirBuff->tail;
uint16_t andOperator = cirBuff->capacity - 1;
for (uint16_t i = 0; i < len; i++) {
((uint8_t *) loc)[i] = cirBuff->data[tail];
++tail &= andOperator;
}
cirBuff->tail = tail;
cirBuff->full = 0;
}
void cirBufferReset(cirBuffer_handle cirBuff)
{
cirBuff->head = 0;
cirBuff->tail = 0;
cirBuff->full = 0;
}
//
// Created by Sil on 7/19/2019.
//
#ifndef BUFFER_H
#define BUFFER_H
#include "Arduino.h"
typedef struct CirBuffer
{
uint16_t head;
uint16_t tail;
uint8_t full;
uint8_t *data;
uint16_t capacity;
uint8_t unitSize;
};
typedef CirBuffer *cirBuffer_handle;
/*
* THE LEN OF THE BUFFER MUST BE A POWER OF 2
*/
uint8_t cirBufferBegin(cirBuffer_handle cirBuff, void *data, uint16_t len, uint8_t unitSize);
uint16_t cirBufferAvailable(cirBuffer_handle cirBuff);
uint16_t cirBufferAvailableForWrite(cirBuffer_handle cirBuff);
void cirBufferWrite(cirBuffer_handle cirBuff, void *unit);
uint8_t *cirBufferExternalWrite(cirBuffer_handle cirBuff);
void cirBufferRead(cirBuffer_handle cirBuff, void *loc);
void cirBufferReadBytes(cirBuffer_handle cirBuff, void *loc, uint16_t len);
void cirBufferReset(cirBuffer_handle cirBuff);
#endif //BUFFER_H
......@@ -6,6 +6,7 @@
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include "buffer.h"
#include <SdFat.h>
......@@ -13,6 +14,38 @@ SdFat sd;
SdFile file;
#if DEBUG_OPENLOGGER
enum
{
uartStartByte = 's',
uartStopByte = 'f'
};
enum
{
uartTypeData = 'd',
uartTypeCommand = 'c',
uartTypeInfo = 'i'
};
enum
{
uartCommandStartWrites = 'v',
uartCommandStopWrites = 'b'
};
enum
{
uartInfoAlreadyRunning = '1',
uartInfoAlreadyIdle = '2',
uartInfoWritesStarted = '3',
uartInfoWritesStartError = '4',
uartInfoStopped = '5',
uartInfoStopError = '6',
uartInfoWriteError = '7',
uartInfoWritesNotStarted = '8'
};
#else
enum
{
uartStartByte = 's',
......@@ -43,6 +76,7 @@ enum
uartInfoWriteError,
uartInfoWritesNotStarted
};
#endif // DEBUG_OPENLOGGER
typedef enum
{
......@@ -61,14 +95,6 @@ typedef enum
checkingForCommand
} UartRxState;
typedef struct
{
uint8_t buffer[OPENLOGGER_WRITE_BUFFER_LEN];
uint16_t head;
uint16_t tail;
} SdBuffer;
uint8_t openloggerBeginSd();
void uartCheckingForStartByte();
......@@ -97,7 +123,9 @@ OpenloggerState openloggerState = idle;
UartRxState uartRxState = checkingForStart;
SdBuffer sdBuffer;
uint8_t sdBuffer[OPENLOGGER_WRITE_BUFFER_LEN];
CirBuffer sdCirBuffer;
void (*openloggerState_ptrs[4])(void) = {openloggerIdle, openloggerStarting, openloggerRunning, openloggerStopping};
......@@ -105,7 +133,7 @@ void
(*uartRxState_ptrs[5])(void) = {uartCheckingForStartByte, uartCheckingForType, uartCheckingForLength, uartReceivingData,
uartCheckingForCommand};
uint8_t uartExpectedDataLength = 0;
volatile uint8_t uartExpectedDataLength = 0;
uint8_t openloggerLastSeenCommand = 0;
......@@ -128,7 +156,7 @@ void uartCheckingForStartByte()
#if DEBUG_OPENLOGGER
Serial.print("OPLOG: CHECKING START ");
Serial.println(Serial.peek());
Serial.println((char) Serial.peek());
#endif // DEBUG_OPENLOGGER
if (Serial.read() == uartStartByte) {
......@@ -140,7 +168,7 @@ void uartCheckingForType()
{
#if DEBUG_OPENLOGGER
Serial.print("OPLOG: CHECKING FOR TYPE ");
Serial.println(Serial.peek());
Serial.println((char) Serial.peek());
#endif // DEBUG_OPENLOGGER
if (Serial.peek() <= uartTypeInfo) {
......@@ -164,88 +192,39 @@ void uartCheckingForLength()
#if DEBUG_OPENLOGGER
Serial.print("OPLOG: CHECKING LENGTH ");
Serial.println(Serial.peek());
Serial.println((char) Serial.peek());
#endif // DEBUG_OPENLOGGER
if (Serial.peek() <= 56) {
uartExpectedDataLength = Serial.read();
uartRxState = receivingData;
}
else {
uartRxState = checkingForStart;
}
}
void uartReceivingData()
{
#if DEBUG_OPENLOGGER
Serial.print("OPLOG: RECEIVED DATA ");
Serial.println(Serial.peek());
#endif // DEBUG_OPENLOGGER
static uint16_t sdBufferUsedBytes = 0;
for (; uartExpectedDataLength > 0; uartExpectedDataLength--) {
sdBuffer.buffer[sdBuffer.head] = Serial.read();
++sdBuffer.head &= (OPENLOGGER_WRITE_BUFFER_LEN - 1);
}
if (sdBuffer.head >= sdBuffer.tail) {
sdBufferUsedBytes = sdBuffer.head - sdBuffer.tail;
}
else {
sdBufferUsedBytes = (OPENLOGGER_WRITE_BUFFER_LEN + sdBuffer.head) - sdBuffer.tail;
}
if (sdBufferUsedBytes >= OPENLOGGER_BYTES_PER_BLOCK_WRITE) {
#if DEBUG_OPENLOGGER
Serial.println("OPLOG: WRITING BLOCK");
Serial.print("OPLOG: LEFT ");
Serial.println(uartExpectedDataLength);
#endif // DEBUG_OPENLOGGER
static uint8_t sdPayload[64];
static uint8_t sdBlocksWritten = 0;
for (uint8_t i = 0; i < OPENLOGGER_BYTES_PER_BLOCK_WRITE; i++) {
sdPayload[i] = sdBuffer.buffer[sdBuffer.tail];
++sdBuffer.tail &= (OPENLOGGER_WRITE_BUFFER_LEN - 1);
}
if (!file.write(sdPayload, OPENLOGGER_BYTES_PER_BLOCK_WRITE)) {
openloggerInformMaster(uartInfoWriteError);
openloggerState = stopping;
}
sdBlocksWritten++;
if (sdBlocksWritten >= OPENLOGGER_BLOCKS_BEFORE_FLUSH) {
file.flush();
sdBlocksWritten = 0;
Serial.readBytes(cirBufferExternalWrite(&sdCirBuffer), 1);
}
uartRxState = checkingForStart;
}
}
void uartCheckingForCommand()
{
#if DEBUG_OPENLOGGER
Serial.print("OPLOG: CHECKING COMMAND ");
Serial.println(Serial.peek());
Serial.println((char) Serial.peek());
#endif // DEBUG_OPENLOGGER
if (Serial.peek() <= stopping) {
openloggerState = (Serial.read() == uartCommandStartWrites) ? starting : stopping;
}
uartRxState = checkingForStart;
}
......@@ -311,30 +290,59 @@ void openloggerStopping()
void openloggerProcess()
{
static uint8_t sdPayload[OPENLOGGER_BYTES_PER_BLOCK_WRITE];
if (cirBufferAvailable(&sdCirBuffer) >= OPENLOGGER_BYTES_PER_BLOCK_WRITE) {
#if DEBUG_OPENLOGGER
Serial.println("OPLOG: WRITING BLOCK");
#endif // DEBUG_OPENLOGGER
static uint8_t sdBlocksWritten = 0;
cirBufferReadBytes(&sdCirBuffer, sdPayload, OPENLOGGER_BYTES_PER_BLOCK_WRITE);
if (!file.write(sdPayload, OPENLOGGER_BYTES_PER_BLOCK_WRITE)) {
openloggerInformMaster(uartInfoWriteError);
openloggerState = stopping;
}
sdBlocksWritten++;
if (sdBlocksWritten >= OPENLOGGER_BLOCKS_BEFORE_FLUSH) {
file.flush();
sdBlocksWritten = 0;
}
}
openloggerState_ptrs[openloggerState]();
}
uint8_t openloggerBegin()
{
#if !DEBUG_OPENLOGGER
Serial.begin(OPENLOGGER_SERIAL_BUAD_RATE, OPENLOGGER_SERIAL_PARITY_CONFIG);
#else
Serial.begin(OPENLOGGER_SERIAL_BUAD_RATE);
#endif // !DEBUG_OPENLOGGER
#if DEBUG_OPENLOGGER
Serial.println("OPLOG: BEGIN");
Serial.setTimeout((uint32_t)(-1));
#endif // DEBUG_OPENLOGGER
#if !DEBUG_OPENLOGGER
watchdogBegin500ms();
#endif // !DEBUG_OPENLOGGER
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
sdBuffer.head = 0;
sdBuffer.tail = 0;
cirBufferBegin(&sdCirBuffer, sdBuffer, OPENLOGGER_WRITE_BUFFER_LEN, sizeof(uint8_t));
return 1;
}
......
......@@ -9,10 +9,17 @@
#define DEBUG_OPENLOGGER 0
#define OPENLOGGER_SERIAL_BUAD_RATE 250000
#define OPENLOGGER_SERIAL_PARITY_CONFIG SERIAL_8E1
#if DEBUG_OPENLOGGER
#define OPENLOGGER_BYTES_PER_BLOCK_WRITE 64
#define OPENLOGGER_SERIAL_BUAD_RATE 115200
#else
#define OPENLOGGER_SERIAL_BUAD_RATE 115200
#endif // DEBUG_OPENLOGGER
#define OPENLOGGER_BYTES_PER_BLOCK_WRITE 128
#define OPENLOGGER_WRITE_BUFFER_LEN OPENLOGGER_BYTES_PER_BLOCK_WRITE * 4
#define OPENLOGGER_BLOCKS_BEFORE_FLUSH 4
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment