Created
May 30, 2019 14:41
-
-
Save kkmonster/7146a9b3ba515d4509287ce109d1a970 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "arduino.h" | |
#include <SPI.h> | |
#include <SD.h> | |
#include <stdio.h> | |
#include <string.h> | |
const int chipSelect = PA4; | |
typedef struct data_log_typedef | |
{ | |
char replay[10] = {0}; | |
uint8_t replay_len = 0; | |
char at[100] = {0}; | |
uint8_t at_len = 0; | |
}; | |
data_log_typedef data_log[10]; | |
uint8_t data_log_index_head = 0; | |
uint8_t data_log_index_tail = 0; | |
const uint8_t led_pin = PC13; | |
uint8_t receiver_buffer[100]; | |
uint8_t transmit_buffer[20]; | |
uint8_t receiver_index; | |
uint8_t transmit_index; | |
uint8_t get_new_data; | |
void setup() | |
{ | |
// Open serial communications and wait for port to open: | |
Serial.begin(115200); | |
Serial1.begin(38610); // pin PB7 // replay | |
Serial2.begin(38610); // pin PA2 // command | |
pinMode(led_pin, OUTPUT_OPEN_DRAIN); | |
uint8_t ct = 21; | |
while (ct) | |
{ | |
ct--; | |
delay(50); | |
digitalWrite(led_pin, !digitalRead(led_pin)); | |
} | |
Serial.print("\nInitializing SD card..."); | |
if (!SD.begin(chipSelect)) | |
{ | |
Serial.println("SD card FAIL"); | |
while (1) | |
{ | |
delay(50); | |
digitalWrite(led_pin, !digitalRead(led_pin)); | |
} | |
} | |
Serial.println("SD card OK"); | |
get_new_data = 0; | |
} | |
uint32_t t0, t1, t2; | |
void loop(void) | |
{ | |
t0 = micros(); | |
if (Serial1.available()) | |
{ | |
get_new_data = 1; | |
t1 = micros(); | |
transmit_index++; | |
if (transmit_index >= 20) | |
transmit_index = 0; | |
transmit_buffer[transmit_index] = Serial1.read(); | |
} | |
if (Serial2.available()) | |
{ | |
receiver_index++; | |
if (receiver_index >= 100) | |
receiver_index = 0; | |
receiver_buffer[receiver_index] = Serial2.read(); | |
} | |
if ((t0 - t1 > 500) && (get_new_data == 1)) | |
{ | |
get_new_data = 0; | |
data_log_index_head++; | |
if (data_log_index_head >= 10) | |
data_log_index_head = 0; | |
memcpy(data_log[data_log_index_head].at, receiver_buffer, receiver_index); | |
memcpy(data_log[data_log_index_head].replay, transmit_buffer, transmit_index); | |
data_log[data_log_index_head].at_len = receiver_index; | |
data_log[data_log_index_head].replay_len = transmit_index; | |
receiver_index = 0; | |
transmit_index = 0; | |
} | |
if ((data_log_index_tail != data_log_index_head) && (t0 - t1 > 2000)) | |
{ | |
File dataFile = SD.open(data_log[data_log_index_tail].replay, FILE_WRITE); | |
// if the file is available, write to it: | |
if (dataFile) | |
{ | |
// dataFile.print(millis()); | |
// dataFile.print("\t\t"); | |
// dataFile.println(dataString, HEX); | |
// dataFile.close(); | |
data_log_index_tail++; | |
if (data_log_index_tail >= 10) | |
data_log_index_tail = 0; | |
} | |
} | |
delayMicroseconds(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment