static msg_t SdThread(void *arg){ chRegSetThreadName("MicroSD"); (void)arg; FRESULT err; uint32_t clusters; FATFS *fsp; FIL Log; msg_t id; /* wait until card not ready */ NOT_READY: while (!sdcIsCardInserted(&SDCD1)) chThdSleepMilliseconds(SDC_POLLING_INTERVAL); chThdSleepMilliseconds(SDC_POLLING_INTERVAL); if (!sdcIsCardInserted(&SDCD1)) goto NOT_READY; else insert_handler(); /* fs mounted? */ if (!fs_ready) return RDY_RESET; /* are we have at least 16MB of free space? */ err = f_getfree("/", &clusters, &fsp); err_check(); if ((clusters * (uint32_t)SDC_FS.csize * (uint32_t)MMCSD_BLOCK_SIZE) < (16*1024*1024)) return RDY_RESET; /* open file for writing log */ char namebuf[MAX_FILENAME_SIZE]; name_from_time(namebuf); err = f_open(&Log, namebuf, FA_WRITE | FA_CREATE_ALWAYS); err_check(); /* main write cycle * This writer waits msg_t with mavlink message ID. Based on that ID it * will pack extern mavlink struct with proper packing function. */ setGlobalFlag(GlobalFlags.logger_ready); while (TRUE){ /* wait ID */ if (logwriter_mb.fetch(&id, TIME_INFINITE) == RDY_OK){ if (!sdcIsCardInserted(&SDCD1)){ clearGlobalFlag(GlobalFlags.logger_ready); remove_handler(); goto NOT_READY; } err = WriteLog(&Log, id, &fresh_data); err_check(); } err = fs_sync(&Log); err_check(); } return 0; }
void poll_inserted() { const auto card_present_now = sdcIsCardInserted(&SDCD1); if( card_present_now != card_present ) { card_present = card_present_now; Status new_status { card_present ? Status::Present : Status::NotPresent }; if( card_present ) { if( sdcConnect(&SDCD1) == CH_SUCCESS ) { if( mount() == FR_OK ) { new_status = Status::Mounted; } else { new_status = Status::MountError; } } else { new_status = Status::ConnectError; } } else { sdcDisconnect(&SDCD1); } status_ = new_status; status_signal.emit(status_); } }
/** * @brief Inserion monitor timer callback function. * * @param[in] p pointer to the @p SDCDriver object * * @notapi */ static void tmrfunc(void *p) { SDCDriver *sdcp = p; if (cnt > 0) { if (sdcIsCardInserted(sdcp)) { if (--cnt == 0) { chEvtBroadcastI(&inserted_event); } } else cnt = SDC_POLLING_INTERVAL; } else { if (!sdcIsCardInserted(sdcp)) { cnt = SDC_POLLING_INTERVAL; chEvtBroadcastI(&removed_event); } } chVTSetI(&tmr, MS2ST(SDC_POLLING_DELAY), tmrfunc, sdcp); }