Skip to content

Commit 544896c

Browse files
committed
get SD card to work on the car lol
1 parent f6e7845 commit 544896c

File tree

3 files changed

+106
-49
lines changed

3 files changed

+106
-49
lines changed

Telemetry-SD-Card/main.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "./fsdaq_encoder_generated_from_dbc.hpp"
21
#include "FATFileSystem.h"
32
#include "SDBlockDevice.h"
43
#include "mbed.h"
@@ -16,24 +15,6 @@ SDBlockDevice sd{
1615
// "sd" is the name of the filesystem; i.e. filepaths are /sd/...
1716
FATFileSystem fs{"sd"};
1817

19-
// Our data
20-
Values vals{};
21-
ValuesRow current_row{};
22-
23-
// Debug
24-
mbed_stats_heap_t heap;
25-
mbed_stats_stack_t stack;
26-
27-
void print_mem_usage() {
28-
mbed_stats_heap_get(&heap);
29-
printf("\nHeap: %u/%u (used/reserved), max usage: %u, allocs: %u\n",
30-
heap.current_size, heap.reserved_size, heap.max_size,
31-
heap.alloc_cnt);
32-
mbed_stats_stack_get(&stack);
33-
printf("Stack: max usage: %u, bytes reserved: %u\n\n", stack.max_size,
34-
stack.reserved_size);
35-
}
36-
3718
void error_quit(std::string msg) {
3819
printf("%s\n", msg.c_str());
3920
while (1) {
@@ -49,8 +30,6 @@ int main(int argc, char *argv[]) {
4930
// TODO: experiment with async SPI and/or DMA
5031
// sd.set_async_spi_mode(true, DMAUsage::DMA_USAGE_ALWAYS);
5132

52-
print_mem_usage();
53-
5433
// Mount the FATFileSystem (so we can use regular C file IO like fopen/read)
5534
int error = fs.mount(&sd);
5635
if (error) {
@@ -61,35 +40,14 @@ int main(int argc, char *argv[]) {
6140
if (error) error_quit("Error: could not reformat SD card! Is the SD card plugged in?\n");
6241
}
6342

64-
mkdir("/sd/fsdaqqqq", 0777);
65-
66-
print_mem_usage();
43+
mkdir("/sd/testing", 0777);
6744

68-
FILE *file = fopen("/sd/fsdaqqqq/test.fsdaq", "w+");
45+
FILE *file = fopen("/sd/testing/test.txt", "w+");
6946
if (file == NULL) {
7047
error_quit("Error opening file!");
7148
}
7249

73-
fwrite("FSDAQ001", 8, 1, file);
74-
75-
write_fsdaq_schema(file);
76-
77-
print_mem_usage();
78-
79-
80-
for (int i=0; i<5; i++) {
81-
memset(&current_row, INT_MAX, sizeof(current_row));
82-
vals.setRow(current_row, i);
83-
}
84-
write_fsdaq_batch(&vals, file);
85-
write_fsdaq_batch(&vals, file);
86-
write_fsdaq_batch(&vals, file);
87-
write_fsdaq_batch(&vals, file);
88-
write_fsdaq_batch(&vals, file);
89-
90-
print_mem_usage();
91-
92-
fwrite("FSDAQ001", 8, 1, file);
50+
fwrite("alksjdlkasjdklasjflka Hellow World!!!", 100, 1, file);
9351

9452
fclose(file);
9553

Telemetry-SD-Card/main_fsdaq.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "./fsdaq_encoder_generated_from_dbc.hpp"
2+
#include "FATFileSystem.h"
3+
#include "SDBlockDevice.h"
4+
#include "mbed.h"
5+
6+
// SDBlockDevice - lowest-level interfaces with the SD card.
7+
// Pin configurations and transfer speeds are set in mbed_app.json.
8+
SDBlockDevice sd{
9+
MBED_CONF_SD_SPI_MOSI,
10+
MBED_CONF_SD_SPI_MISO,
11+
MBED_CONF_SD_SPI_CLK,
12+
MBED_CONF_SD_SPI_CS,
13+
MBED_CONF_SD_TRX_FREQUENCY,
14+
};
15+
// FATFileSystem - Creates a FAT filesystem on the SDBlockDevice.
16+
// "sd" is the name of the filesystem; i.e. filepaths are /sd/...
17+
FATFileSystem fs{"sd"};
18+
19+
// Our data
20+
Values vals{};
21+
ValuesRow current_row{};
22+
23+
// Debug
24+
mbed_stats_heap_t heap;
25+
mbed_stats_stack_t stack;
26+
27+
void print_mem_usage() {
28+
mbed_stats_heap_get(&heap);
29+
printf("\nHeap: %u/%u (used/reserved), max usage: %u, allocs: %u\n",
30+
heap.current_size, heap.reserved_size, heap.max_size,
31+
heap.alloc_cnt);
32+
mbed_stats_stack_get(&stack);
33+
printf("Stack: max usage: %u, bytes reserved: %u\n\n", stack.max_size,
34+
stack.reserved_size);
35+
}
36+
37+
void error_quit(std::string msg) {
38+
printf("%s\n", msg.c_str());
39+
while (1) {
40+
};
41+
}
42+
43+
int main(int argc, char *argv[]) {
44+
printf("Hello World!\n");
45+
46+
// Enable debug logging (to be turned off)
47+
sd.debug(true);
48+
49+
// TODO: experiment with async SPI and/or DMA
50+
// sd.set_async_spi_mode(true, DMAUsage::DMA_USAGE_ALWAYS);
51+
52+
print_mem_usage();
53+
54+
// Mount the FATFileSystem (so we can use regular C file IO like fopen/read)
55+
int error = fs.mount(&sd);
56+
if (error) {
57+
// Reformat if we can't mount the filesystem.
58+
// This should only happen on the first boot
59+
printf("No filesystem found, formatting...\n");
60+
error = fs.reformat(&sd);
61+
if (error) error_quit("Error: could not reformat SD card! Is the SD card plugged in?\n");
62+
}
63+
64+
mkdir("/sd/fsdaqqqq", 0777);
65+
66+
print_mem_usage();
67+
68+
FILE *file = fopen("/sd/fsdaqqqq/test.fsdaq", "w+");
69+
if (file == NULL) {
70+
error_quit("Error opening file!");
71+
}
72+
73+
fwrite("FSDAQ001", 8, 1, file);
74+
75+
write_fsdaq_schema(file);
76+
77+
print_mem_usage();
78+
79+
80+
for (int i=0; i<5; i++) {
81+
memset(&current_row, INT_MAX, sizeof(current_row));
82+
vals.setRow(current_row, i);
83+
}
84+
write_fsdaq_batch(&vals, file);
85+
write_fsdaq_batch(&vals, file);
86+
write_fsdaq_batch(&vals, file);
87+
write_fsdaq_batch(&vals, file);
88+
write_fsdaq_batch(&vals, file);
89+
90+
print_mem_usage();
91+
92+
fwrite("FSDAQ001", 8, 1, file);
93+
94+
fclose(file);
95+
96+
printf("Goodbye World!\n\n");
97+
while (1) {
98+
};
99+
}

Telemetry-SD-Card/mbed_app.json5

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
"sd.SPI_CS": "D3",
3030
},
3131
"NUCLEO_F446RE": {
32-
"sd.SPI_MOSI": "D11",
33-
"sd.SPI_MISO": "D12",
34-
"sd.SPI_CLK": "D13",
35-
"sd.SPI_CS": "D10",
32+
"sd.SPI_MOSI": "PB_15",
33+
"sd.SPI_MISO": "PB_14",
34+
"sd.SPI_CLK": "PB_13",
35+
"sd.SPI_CS": "PB_12",
3636
},
3737
}
3838
}

0 commit comments

Comments
 (0)