From 987ea3555d48e9135f4a7e4789a1b74ba2ee664b Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 10 Nov 2015 15:24:42 -0800 Subject: [PATCH] SD card detection and filesystem mounting. --- firmware/application/event.hpp | 1 - firmware/application/main.cpp | 32 ++++++++++++++++------ firmware/application/sd_card.hpp | 47 ++++++++++++++++++++++++++++++++ firmware/common/message.hpp | 13 +++++++++ 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 firmware/application/sd_card.hpp diff --git a/firmware/application/event.hpp b/firmware/application/event.hpp index e9b77c937..fbf40d0a4 100644 --- a/firmware/application/event.hpp +++ b/firmware/application/event.hpp @@ -26,7 +26,6 @@ constexpr auto EVT_MASK_RTC_TICK = EVENT_MASK(0); constexpr auto EVT_MASK_LCD_FRAME_SYNC = EVENT_MASK(1); -constexpr auto EVT_MASK_SD_CARD_PRESENT = EVENT_MASK(2); constexpr auto EVT_MASK_SWITCHES = EVENT_MASK(3); constexpr auto EVT_MASK_ENCODER = EVENT_MASK(4); constexpr auto EVT_MASK_TOUCH = EVENT_MASK(5); diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index 3b10085e2..66060469f 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -52,6 +52,8 @@ #include "lpc43xx_cpp.hpp" using namespace lpc43xx; +#include "sd_card.hpp" + #include class EventDispatcher { @@ -87,6 +89,7 @@ private: ui::Context& context; uint32_t encoder_last = 0; bool is_running = true; + bool sd_card_present = false; eventmask_t wait() { return chEvtWaitAny(ALL_EVENTS); @@ -105,10 +108,6 @@ private: handle_lcd_frame_sync(); } - if( events & EVT_MASK_SD_CARD_PRESENT ) { - handle_sd_card_detect(); - } - if( events & EVT_MASK_SWITCHES ) { handle_switches(); } @@ -130,7 +129,28 @@ private: } void handle_rtc_tick() { + const auto sd_card_present_now = sdc_lld_is_card_inserted(&SDCD1); + if( sd_card_present_now != sd_card_present ) { + sd_card_present = sd_card_present_now; + if( sd_card_present ) { + if( sdcConnect(&SDCD1) == CH_SUCCESS ) { + if( sd_card::filesystem::mount() == FR_OK ) { + SDCardStatusMessage message { true }; + context.message_map().send(&message); + } else { + // TODO: Error, modal warning? + } + } else { + // TODO: Error, modal warning? + } + } else { + sdcDisconnect(&SDCD1); + + SDCardStatusMessage message { false }; + context.message_map().send(&message); + } + } } static ui::Widget* touch_widget(ui::Widget* const w, ui::TouchEvent event) { @@ -181,10 +201,6 @@ private: painter.paint_widget_tree(top_widget); } - void handle_sd_card_detect() { - - } - void handle_switches() { const auto switches_state = get_switches_state(); for(size_t i=0; i;