From 5e00441d99b06519836562eb1358b3b4b34af275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 31 Dec 2022 20:18:34 +0100 Subject: [PATCH] Add separate source file handling the mounting of the SD card. This will give proper access to the GUI to check whether an SD is mounted --- stm-firmware/include/reflow-controller/sd.h | 42 ++++++++++++++ stm-firmware/main.c | 38 +------------ stm-firmware/sd.c | 62 +++++++++++++++++++++ 3 files changed, 106 insertions(+), 36 deletions(-) create mode 100644 stm-firmware/include/reflow-controller/sd.h create mode 100644 stm-firmware/sd.c diff --git a/stm-firmware/include/reflow-controller/sd.h b/stm-firmware/include/reflow-controller/sd.h new file mode 100644 index 0000000..5233a71 --- /dev/null +++ b/stm-firmware/include/reflow-controller/sd.h @@ -0,0 +1,42 @@ +/* Reflow Oven Controller + * + * Copyright (C) 2022 Mario Hüttel + * + * This file is part of the Reflow Oven Controller Project. + * + * The reflow oven controller is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * GDSII-Converter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the reflow oven controller project. + * If not, see . + */ + + #ifndef _SD_H_ + #define _SD_H_ + +#include +#include + +/** + * @brief Mount the SD card if available and not already mounted + * @param fs Filesystem to mount to. + * @return true if mounted, false if an error occured or the SD is not inserted and cannot be mounted + */ +bool mount_sd_card_if_avail(FATFS *fs); + +/** + * @brief Return whether an SD card is inserted and mounted + * + * @return true SD inserted and mounted + * @return false No SD inserted or not mounted + */ +bool sd_card_is_mounted(void); + +#endif /* _SD_H_ */ \ No newline at end of file diff --git a/stm-firmware/main.c b/stm-firmware/main.c index d485043..5573003 100644 --- a/stm-firmware/main.c +++ b/stm-firmware/main.c @@ -32,13 +32,13 @@ #include #include #include -#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h" #include #include #include #include #include #include +#include #include #include #include @@ -88,40 +88,6 @@ static inline void uart_gpio_config(void) #endif } -/** - * @brief Mount the SD card if available and not already mounted - * @param mounted The current mounting state of the SD card - * @return true if mounted, false if an error occured or the SD is not inserted and cannot be mounted - */ -static bool mount_sd_card_if_avail(bool mounted) -{ - FRESULT res; - static uint8_t IN_SECTION(.ccm.bss) inserted_counter = 0; - - if (sdio_check_inserted() && mounted) { - memset(fs_ptr, 0, sizeof(FATFS)); - sdio_stop_clk(); - inserted_counter = 0; - return false; - } - - if (!sdio_check_inserted() && inserted_counter < 255) - inserted_counter++; - - if (!sdio_check_inserted() && !mounted && inserted_counter > 4) { - inserted_counter = 0; - res = f_mount(fs_ptr, "0:/", 1); - if (res == FR_OK) { - led_set(1, 1); - return true; - } else { - return false; - } - } - - return mounted; -} - /** * @brief Process the boot status structure in the safety (backup) RAM * Depending on the flags set there, this function will: @@ -315,7 +281,7 @@ int main(void) if (systick_ticks_have_passed(quarter_sec_timestamp, 250)) { led_set(1u, 0); sd_old = sd_card_mounted; - sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted); + sd_card_mounted = mount_sd_card_if_avail(fs_ptr); if (sd_card_mounted && !sd_old) { adc_pt1000_get_resistance_calibration(NULL, NULL, &cal_active); diff --git a/stm-firmware/sd.c b/stm-firmware/sd.c new file mode 100644 index 0000000..c061231 --- /dev/null +++ b/stm-firmware/sd.c @@ -0,0 +1,62 @@ +/* Reflow Oven Controller + * + * Copyright (C) 2022 Mario Hüttel + * + * This file is part of the Reflow Oven Controller Project. + * + * The reflow oven controller is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * GDSII-Converter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the reflow oven controller project. + * If not, see . + */ + +#include +#include "fatfs/ff.h" +#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h" +#include +#include +#include + +static bool sd_card_mounted_state = false; + +bool mount_sd_card_if_avail(FATFS *fs) +{ + FRESULT res; + static uint8_t IN_SECTION(.ccm.bss) inserted_counter = 0; + + if (sdio_check_inserted() && sd_card_mounted_state) { + memset(fs, 0, sizeof(FATFS)); + sdio_stop_clk(); + inserted_counter = 0; + return false; + } + + if (!sdio_check_inserted() && inserted_counter < 255) + inserted_counter++; + + if (!sdio_check_inserted() && !sd_card_mounted_state && inserted_counter > 4) { + inserted_counter = 0; + res = f_mount(fs, "0:/", 1); + if (res == FR_OK) { + led_set(1, 1); + return true; + } else { + return false; + } + } + + return sd_card_mounted_state; +} + +bool sd_card_is_mounted(void) +{ + return sd_card_mounted_state; +} \ No newline at end of file