Skip to content

Commit 2179a87

Browse files
committed
feat: state path to switch into standalone mode
1 parent af77853 commit 2179a87

5 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "registers/system/system.h"
22
#include "states.h"
3+
#include "sdcard/sdcard.h"
34

45
int main(void) {
56

67
SYSTEM_Initialize();
78

8-
state_t current_state = STATE_STANDBY;
9+
state_t current_state = SDCARD_standalone_check() == SUCCESS ? STATE_STANDALONE : STATE_STANDBY;
910

1011
while (1) {
1112
current_state = STATES_RunState(current_state);

src/sdcard/sdcard.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#define FILENAME_SIZE (SFN_MAX + SFN_SUFFIX_LEN + 1)
1616
#define BUF_MAX FF_MIN_SS
1717
#define SDCARD_DRIVE "0:"
18+
#define CONFIG_FILENAME "PSLAB.CFG"
1819

20+
static bool s_mounted = false;
1921
static TCHAR s_sector_buf[BUF_MAX];
2022
_Static_assert(
2123
sizeof(s_sector_buf) == 512,
@@ -141,3 +143,51 @@ response_t SDCARD_get_file_info(void)
141143

142144
return SUCCESS;
143145
}
146+
147+
/**
148+
* @brief Read the configuration file from the SD card.
149+
* @return true if the config file was read successfully, false otherwise or if the config structure is invalid.
150+
*/
151+
static bool read_config_file(void) {
152+
FIL file;
153+
if (f_open(&file, CONFIG_FILENAME, FA_READ) != FR_OK) {
154+
return false;
155+
}
156+
157+
TCHAR buf[6] = {0}; // Config file should have a magic header of 5 bytes
158+
UINT bytes_read = 0;
159+
if (f_read(&file, buf, sizeof buf - 1, &bytes_read) != FR_OK
160+
|| bytes_read != sizeof buf - 1) {
161+
f_close(&file);
162+
return false;
163+
}
164+
f_close(&file);
165+
166+
return true;
167+
}
168+
169+
response_t SDCARD_standalone_check(void) {
170+
if (!SD_SPI_IsMediaPresent()) {
171+
return FAILED;
172+
}
173+
174+
FATFS drive;
175+
if (f_mount(&drive, SDCARD_DRIVE, 1) != FR_OK) {
176+
return FAILED;
177+
}
178+
s_mounted = true;
179+
180+
if (!read_config_file()) {
181+
SDCARD_standalone_unmount();
182+
return FAILED;
183+
}
184+
185+
return SUCCESS;
186+
}
187+
188+
void SDCARD_standalone_unmount(void) {
189+
if (s_mounted) {
190+
f_mount(0, SDCARD_DRIVE, 0);
191+
s_mounted = false;
192+
}
193+
}

src/sdcard/sdcard.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,15 @@ response_t SDCARD_read_file(void);
9797
*/
9898
response_t SDCARD_get_file_info(void);
9999

100+
/**
101+
* @brief Check if SD-card is present and can be mounted.
102+
*
103+
* @details Checks if the SD-card can be mounted and if the configuration file is present and valid.
104+
* If the configuration file is not present or invalid, or if the SD-card is not mounted, the function returns FAILED.
105+
* The configuration file is expected to be named "PSLAB.CFG" and contain a magic header of 5 bytes with a null terminator.
106+
*
107+
* @return SUCCESS
108+
*/
109+
response_t SDCARD_standalone_check(void);
110+
100111
#endif // _SDCARD_H

src/states.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ state_t RunCommand(void) {
3636
return STATE_STANDBY;
3737
}
3838

39+
/**
40+
* @brief Run in standalone mode until serial traffic is detected.
41+
* @return STATE_STANDALONE or STATE_RUNCOMMAND if serial traffic is detected.
42+
*/
43+
state_t Standalone(void) {
44+
if (UART_IsRxReady(U1SELECT)) {
45+
return STATE_RUNCOMMAND;
46+
}
47+
48+
return STATE_STANDALONE;
49+
}
50+
3951
state_func_t* const state_table[NUM_STATES] = {
4052
Standby,
4153
RunCommand,
54+
Standalone,
4255
};
4356

4457
state_t STATES_RunState(state_t current_state) {

src/states.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern "C" {
1616
typedef enum {
1717
STATE_STANDBY,
1818
STATE_RUNCOMMAND,
19+
STATE_STANDALONE,
1920
NUM_STATES
2021
} state_t;
2122

0 commit comments

Comments
 (0)