stm32f103c data logger

Post Reply
ezzd27
Posts: 1
Joined: Sat Apr 15, 2023 8:48 pm

stm32f103c data logger

Post by ezzd27 »

i use stm32 bluebill with sd module to convert serial data from serial port to text file in the sd card and the data coming continuously from fpga with high buad rate and am stuck with this project my last experiment was using FreeRTOS with this code: #include <FreeRTOS.h>
#include <task.h>
#include <SD.h>
#include <SPI.h>
#include <stm32_def.h>

#define SERIAL_RX_PIN PA10
#define FILENAME "data.txt"
#define BUFFER_SIZE 20000
#define MAX_SERIAL_CHARS 100
#define SD_CS_PIN PA4

static void task1(void *pvParameters) {
char buffer[BUFFER_SIZE];
int index = 0;
for (;;) {
if (Serial.available()) {
char c = Serial.read();
if (index < BUFFER_SIZE && index < MAX_SERIAL_CHARS) {
buffer[index++] = c;
} else {
Serial.print("Serial buffer overflow, contents: ");
Serial.write(buffer, index);
index = 0;
}
}
vTaskDelay(pdMS_TO_TICKS(1));
}
}

static void task2(void *pvParameters) {
File file;
char buffer[BUFFER_SIZE];
int index = 0;
for (;;) {
if (SD.exists(FILENAME)) {
file = SD.open(FILENAME, FILE_WRITE);
if (file) {
if (index > 0) {
Serial.print("Writing to file, buffer contents: ");
Serial.write(buffer, index);
int bytes_written = file.write(buffer, index);
if (bytes_written != index) {
Serial.println("File write error!");
}
index = 0;
}
file.close();
} else {
Serial.println("Unable to open file for writing!");
}
} else {
file = SD.open(FILENAME, FILE_WRITE);
if (file) {
file.close();
} else {
Serial.println("Unable to create file!");
}
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

void setup()
{
Serial.begin(9600);
pinMode(SERIAL_RX_PIN, INPUT);

SPI.begin();
if (!SD.begin(20, SD_CS_PIN)) { // Use PA4 as CS pin and SPI1 bus
Serial.println("SD card initialization failed!");
while (1);
}

xTaskCreate(task1, "SerialTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(task2, "FileTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
vTaskStartScheduler();
}

void loop()
{
}
Post Reply

Return to “Let us know a bit about you and your projects”