static void vFileTask (void *parameter) { uint32_t pos; UINT written; uint8_t data; static FIL fil; portTickType time, time_old; /* delay SD card init by 5 seconds */ vTaskDelay (5000 / portTICK_RATE_MS); /* never fails - data init */ memset (&fatfs, 0, sizeof (fatfs)); f_mount (0, &fatfs); /* opening new file for write access */ debug_printf ("\nCreating logfile (%s).\n", logfile); if (f_open (&fil, logfile, FA_WRITE | FA_CREATE_ALWAYS)) debug_printf ("\nfailed to create file\n"); else { /* Enable Debug output as we were able to open the log file */ debug_printf ("OpenBeacon firmware version %s\nreader_id=%i.\n", VERSION, env.e.reader_id); PtSetDebugLevel (1); /* Storing clock ticks for flushing cache action */ time_old = xTaskGetTickCount (); pos = 0; for (;;) if (xQueueReceive (xLogfile, &data, 100)) { sector_buffer[pos++] = data; if (pos == SECTOR_BUFFER_SIZE) { pos = 0; if (f_write (&fil, §or_buffer, sizeof (sector_buffer), &written) || written != sizeof (sector_buffer)) debug_printf ("\nfailed to write to logfile\n"); } /* flush file every 5 seconds */ time = xTaskGetTickCount (); if ((time - time_old) > (5000 / portTICK_RATE_MS)) { time_old = time; if (f_sync (&fil)) debug_printf ("\nfailed to flush to logfile\n"); } } } }
/* * SDカードのサンプル */ void sd_test() { FRESULT rc; // DIR dir; /* Directory object */ // FILINFO fno; /* File information object */ UINT bw, br, i; char tmp[64]; f_mount(0, &Fatfs); /* Register volume work area (never fails) */ /* * SDカードのMESSAGE.TXTを開いてI2C液晶に表示します。英数カナのみ * 2行分のみ */ rc = f_open(&Fil, "MESSAGE.TXT", FA_READ); if (!rc){ i2c_cmd(0x80); // xprintf("\nType the file content.\n"); for (;;) { rc = f_read(&Fil, buff, sizeof(buff), &br); /* Read a chunk of file */ if (rc || !br) break; /* Error or end of file */ for (i = 0; i < br; i++){ if(i==0x10) i2c_cmd(0xC0); i2c_data(buff[i]); } xprintf("%s\n", buff); } if (rc) die(rc); rc = f_close(&Fil); } /* * ファイル書き込みテスト * SD0001.TXTファイルを作成し、Strawberry Linuxの文字を永遠に書き込む */ rc = f_open(&Fil, "SD0001.TXT", FA_WRITE | FA_OPEN_ALWAYS); f_lseek(&Fil, f_size(&Fil)); if (rc) die(rc); i = 0; // 無限ループでこの関数からは抜けない while(i < 100){ sprintf(tmp, "Strawberry Linux %d\r\n", i); rc = f_write(&Fil, tmp, strlen(tmp), &bw); if (rc) die(rc); xprintf("%s\n", tmp); // SDカードに書き出します。 f_sync(&Fil); i++; } // return; f_mount(0, NULL); }
int dfs_elm_flush(struct dfs_fd *file) { FIL *fd; FRESULT result; fd = (FIL *)(file->data); RT_ASSERT(fd != RT_NULL); result = f_sync(fd); return elm_result_to_dfs(result); }
int FATFileHandle::fsync() { if (f_sync(&FileObject)) { return -1; } else { return 0; } }
static int fatfsWrite(GFILE* f, const void* buf, int size) { int wr; f_write( (FIL*)f->obj, buf, size, (UINT*)&wr); #if !GFILE_NEED_NOAUTOSYNC f_sync( (FIL*)f->obj ); #endif return wr; }
/** * @brief Save the data to specified file. * @param path: pointer to the saving path * @retval File saved */ uint8_t CAMERA_SaveToFile(uint8_t *path) { RTC_TimeTypeDef Time; RTC_DateTypeDef Date; FIL file; uint32_t NumWrittenData; uint8_t ret = 1; char filename[FILEMGR_FILE_NAME_SIZE]; char fullpath[FILEMGR_FILE_NAME_SIZE]; /* Create filename */ k_GetTime(&Time); k_GetDate(&Date); sprintf((char *)filename, "/Camera_%02d%02d%04d_%02d%02d%02d.bmp", Date.Date, Date.Month, Date.Year + 2015, Time.Hours, Time.Minutes, Time.Seconds); strcpy((char *)fullpath, (char *)path); strcat ((char *)fullpath, (char *)filename); BSP_CAMERA_Suspend(); BSP_SD_Init(); /* Can not create file */ if (f_open(&file, (char *)fullpath, FA_CREATE_NEW | FA_WRITE) == FR_OK) { /* Write the received data into the file */ if (f_write(&file, (char *)BMPHeader_QQVGA24Bit, RGB_HEADER_SIZE, (UINT *)&NumWrittenData) == FR_OK) { f_sync(&file); /* Convert RGB16 image to RGB24 */ RGB16toRGB24((uint8_t *)CAMERA_CVRT_BUFFER, (uint8_t *)&buffer_camera); if (f_write(&file, (char *)CAMERA_CVRT_BUFFER, MAX_IMAGE_SIZE, (UINT*)&NumWrittenData)== FR_OK) { /*File Written correctly */ ret = 0; } } f_close(&file); } BSP_CAMERA_Init(RESOLUTION_R160x120); CAMERA_Configured = 1; BSP_CAMERA_Resume(); return ret; }
static bool_t fatfsSync(GFILE *f) { FRESULT ferr; ferr = f_sync( (FIL*)f->obj ); if (ferr != FR_OK) { return FALSE; } return TRUE; }
void pwmrecord(){ speed[0] = SpeedValue_left; speed[1] = SpeedValue_right; encoder[0] = ENCODER_L.count; encoder[1] = ENCODER_R.count; if(&file){ res = f_write(&file, speed, sizeof(speed), &bw); res = f_write(&file, encoder, sizeof(encoder), &bw); } res = f_sync(&file); }
int FATFileSystem::file_sync(fs_file_t file) { FIL *fh = static_cast<FIL*>(file); lock(); FRESULT res = f_sync(fh); unlock(); if (res != FR_OK) { debug_if(FFS_DBG, "f_sync() failed: %d\n", res); } return fat_error_remap(res); }
u32 MountImage(const char* path) { if (mount_state) { if (mount_state != IMG_RAMDRV) f_close(&mount_file); mount_state = 0; } if (!path || !IdentifyImage(path)) return 0; if (f_open(&mount_file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) return 0; f_lseek(&mount_file, 0); f_sync(&mount_file); return (mount_state = IdentifyImage(path)); }
void MessageStorage::closeFile() { module_debug_strg("closeFile"); if(!m_fileOpen) { module_debug_strg("file not open!"); return; } m_fr = f_close(&m_file); f_sync(&m_file); m_fileOpen = false; }
// Couldn't manage to build UTF version without warnings. Trying // --fwide-exec-charset=UTF-16 build flag doesn't help. int main(void) { FATFS fs; FIL f; FRESULT returned; UINT scratch; int i; /// Set clock to 66,67 MHz (200 MHz / 3) MAP_SysCtlClockSet(SYSCTL_SYSDIV_3 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /// LED initialization RgbLedInit(); /// Initialize UART0 (used for stdout) UartInit(0, 115200); printf("Test start!\r\n"); /// Color cycle the LED. Ends with red color. for (i = 7; i > 0; i--) { MAP_SysCtlDelay(2500000); RgbLedSet(i<<1); } // TODO: check mount options if ((returned = f_mount(&fs, _T("/"), 0))) { printf("ERROR: couldn't mount volume.\r\n"); return 1; } if ((returned = f_open(&f, _T("test.txt"), FA_CREATE_ALWAYS | FA_WRITE))) { printf("ERROR: couldn't open file.\r\n"); return 2; } if ((returned = f_write(&f, HELLO_STR, HELLO_STR_LEN, &scratch))) { printf("ERROR: couldn't write to file.\r\n"); return 3; } printf("%d bytes written.\r\n", scratch); // TODO: unmount volume or at least sync filesystem f_sync(&f); return 0; }
void MessageStorage::writeToFile(char * buffer, unsigned int count) { if(!m_fileOpen) { module_debug_strg("file not open!"); return; } UINT bw; f_sync(&m_file); m_fr = f_write(&m_file, (void *) buffer, count, &bw); if(m_fr != FR_OK || bw != count) module_debug_strg("error while writing: %x wrote %d of %d", m_fr, bw, count); }
void log_init(void) { disk_initialize(0); delay(1); int result = disk_initialize(0); if(result == 0) { f_mount(0, &FATFS_Obj); char filename[200]; { int filecounter = 0; FILINFO filetest; sprintf(filename,"0:log%05u.txt",filecounter); while(f_stat(filename,&filetest) == FR_OK) { filecounter++; sprintf(filename,"0:log%05u.txt",filecounter); } } int result = f_open(&file, filename, FA_CREATE_NEW | FA_WRITE); if(result != 0) { led_fastBlink(LED_SDCARD); sd_card_available = 0; return; } result = f_sync(&file); if(result != 0) { led_fastBlink(LED_SDCARD); sd_card_available = 0; return; } result = f_lseek(&file, file.fsize); if(result != 0) { led_fastBlink(LED_SDCARD); sd_card_available = 0; return; } //usb_printfi_buffered("SD filename: %s\n",filename); led_on(LED_SDCARD); sd_card_available = 1; } { //usb_printf_buffered("NO SD"); } }
void kml_writeline(char * line) { uint8_t l = strlen(line); uint16_t wl; // DEBUG("KML:%s\n", line); strcpy_P(line + l, PSTR("\r\n")); l += 2; assert(f_write(log_fil, line, l, &wl) == FR_OK); assert(wl == l); assert(f_sync(log_fil) == FR_OK); }
void Log_Init() { uint32_t dwWriteCnt; if(f_open(&logFile, LOG_PATH, FA_OPEN_ALWAYS | FA_WRITE)==FR_OK) { sprintf(logInitBuffer,"welcome to use FDC2315, the version is %s\r\n",PRODUCT_VER); Log_addDate(logInitBuffer, 0); if(logFile.fsize<2*1024*1024) f_lseek(&logFile, logFile.fsize); f_write(&logFile, logBuffer, strlen(logBuffer), &dwWriteCnt); f_sync(&logFile); } }
/*---------------------------------------------------------------------------*/ int cfs_open(const char *n, int f) { UINT accessmode_app = (FA_OPEN_ALWAYS|FA_WRITE|FA_READ); UINT accessmode = (FA_CREATE_NEW|FA_WRITE|FA_READ); UINT accessmode_ow = (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_WRITE|FA_READ); UINT file_exist = 0; FRESULT rc; DWORD ptr; PRINTF("DEBUG: OrisenSpectrum: cfs_open - flags = %d\r\n", f); rc = f_open(&fil, n, accessmode_app); PRINTF("DEBUG: -OrisenSpectrum: cfs_open: rc = %d\r\n", rc); if (rc == FR_OK) { PRINTF("file exists\r\n"); file_exist = 1; } f_close(&fil); if(f & CFS_READ) { rc = f_open(&fil, n, FA_READ); PRINTF("DEBUG: --OrisenSpectrum: cfs_open: rc = %d\r\n", rc); } if((f & CFS_WRITE) || (f & CFS_APPEND)) { if((f & CFS_WRITE) || ((f & CFS_APPEND) && (file_exist == 0)) ){ PRINTF("File over write=%d\r\n", fil.fptr); rc = f_open(&fil, n, accessmode_ow); } else if((f & CFS_APPEND) && (file_exist == 1)) { //append file rc = f_open(&fil, n, accessmode_app); PRINTF("File append pointer=%d, size=%d\r\n", fil.fptr, fil.fsize); rc |= f_lseek(&fil,fil.fsize); PRINTF("File pointer=%d\r\n", fil.fptr); rc |=f_sync(&fil); } } PRINTF("DEBUG: ---OrisenSpectrum: cfs_open: rc = %d\r\n", rc); if (rc) { //die(rc); return -1; } put_rc(rc); return rc; }
void datalog(void) { float nick, roll, yaw; uint32_t system_time; //update_IMU(); //Ersetzen durch Interrupt Handler!!!!!! nick = getEuler_nick(); roll = getEuler_roll(); yaw = getEuler_yaw(); if(Datalogger_ready() && !datalog_main_opened) { //rc_main = f_mkfs(0,0,0); rc_main = f_open(&Fil_Main, "QuadMain.TXT", FA_WRITE | FA_CREATE_ALWAYS); if(rc_main != FR_OK) { chprintf((BaseChannel *) &SD2, "SD QuadMain.TXT: f_open() failed %d\r\n", rc_main); return; } //rc_main = f_printf(&Fil, "moin\r\n"); rc_main = f_sync(&Fil_Main); if(rc_main != FR_OK) { chprintf((BaseChannel *) &SD2, "SD QuadMain.TXT: f_sync() failed %d\r\n", rc_main); return; } chprintf((BaseChannel *) &SD2, "SD QuadMain.TXT: opened successfull\r\n"); f_printf(&Fil_Main, "Time_Main; Nick_Main; Roll_Main; Yaw_Main\r\n"); f_sync(&Fil_Main); datalog_main_opened = TRUE; } if(Datalogger_ready() && datalog_main_opened) { system_time = chTimeNow(); f_printf(&Fil_Main, "%d;%d;%d;%d\r\n",system_time,(int)(nick*100),(int)(roll*100),(int)(yaw*100)); rc_main = f_sync(&Fil_Main); } }
//Запись свежих данных в память с форматированием int save (FileType FT, char const* fmt, ... ) { FIL file; char* filename = "log.txt"; switch (FT) { case DATA: filename = "actual.dat"; break; case LOG: filename = "log.txt"; break; case SETTINGS: filename = "settings.ini"; break; case DOWNLOAD: filename = "download.tmp"; break; } //В какой файл писать unsigned int bw; //Кол-во записанных байтов char buff[1024+1]; va_list uk_arg; va_start (uk_arg, fmt); int slen = vsnprintf(buff, 1024, fmt, uk_arg); //Форматирование строки if (slen > 1024) slen = 1024; int a, b; // LED_on(LED5); //LED_on(LED_GREEN); f_open(&file, filename, FA_WRITE | FA_OPEN_ALWAYS); //Открываем или создаем f_lseek(&file, f_size(&file)); //Идем в конец файла a = (int)cnt_timer; f_write(&file, buff, slen, &bw); // Write data to the file f_sync(&file); b = (int)cnt_timer; f_close(&file); //Закрываем //LED_off(LED_GREEN); return (b-a); }
static int32_t filerProcessSync(filerFileStruct_t *f) { uint32_t res; if (!f->open) { return -1; } res = f_sync(&f->fp); if (res != FR_OK) { f->open = 0; return -1; } return 0; }
int main(void) { //En vez de usar 16MHz, uso el PLL para conseguir 40MHz SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); unsigned long g=SysCtlClockGet(); FPUEnable(); FPUStackingEnable(); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); GPIOPinConfigure(GPIO_PC4_U4RX); GPIOPinConfigure(GPIO_PC5_U4TX); GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5); UARTConfigSetExpClk(UART4_BASE, SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0); rc = f_mount(0, &Fatfs); //registra un area de trabajo rc = f_open(&Fil, "BuffGPS.TXT", FA_WRITE | FA_OPEN_ALWAYS); //abre o crea un archivo rc = f_lseek(&Fil, Fil.fsize); rc = f_write(&Fil, &nuevo_registro, 15, &bw); rc = f_sync(&Fil); rc = f_close(&Fil); //Configuracion de timer y su interrupcion SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER); valor_timer = (SysCtlClockGet()/20); TimerLoadSet(TIMER0_BASE, TIMER_A, valor_timer -1); IntEnable(INT_TIMER0A); TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); TimerEnable(TIMER0_BASE, TIMER_A); //IntPrioritySet(FAULT_SYSTICK, 0x00); IntPrioritySet(INT_TIMER0A, 0x00); IntMasterEnable(); while(1); }
/*---------------------------------------------------------------------------*/ void initialize_userland(void) { //J ユーザログファイルを開く char logFileName[16] = "ulog.txt"; int ret = f_open(&sLogFile, logFileName, FA_WRITE | FA_OPEN_ALWAYS); //J 基本追記でやります if (ret == FR_OK) { UINT len = 0; f_lseek(&sLogFile, f_size(&sLogFile)); f_write(&sLogFile, "-- Start Userland\n", 18, &len); f_sync(&sLogFile); } else { } return; }
FRESULT writeResult(char test[], int result){ char timeString[20]; getSQL(timeString); // Load time at which the test occurred f_mount(0, &FileSystemObject); if (f_open(&logFile, "/tictoc.csv", FA_WRITE | FA_OPEN_EXISTING)){ // File does not exist yet, try to create it and write the header FRESULT res = f_open(&logFile, "/tictoc.csv", FA_WRITE | FA_CREATE_NEW); if(res) return res; // File opening failed! f_printf(&logFile, "time,device,test,result\n"); // Write header } f_lseek(&logFile, f_size(&logFile)); // Make sure we are at the end of the file f_printf(&logFile, "%s,watch,%s,%d\n",timeString,test,result); // Write line of data f_sync(&logFile); // Make sure file is synched f_close(&logFile); return f_mount(0,0); }
TESTABLE_STATIC int flush_logfile(struct logging_status *ls) { if (ls->writing_status != WRITING_ACTIVE) return -1; if (!isTimeoutMs(ls->flush_tick, FLUSH_INTERVAL_MS)) return -2; pr_debug("Logging: flush\r\n"); const int res = f_sync(g_logfile); if (0 != res) pr_debug_int_msg("Logging: flush err ", res); ls->flush_tick = xTaskGetTickCount(); return res; }
static u16 log_to_file(){ u16 accepted_bytes; f_write(&file, locked_page, log_block_size, &accepted_bytes); { static __xdata u8 loop = 0; if((++loop) == 64){ loop = 0; f_sync(&file); } } return accepted_bytes; }
void FileWriteString(char *Mystring,DWORD len) { unsigned int bytesWritten; filename=MyFileName; if(f_open(&MyFile,filename , FA_READ | FA_WRITE |FA_OPEN_EXISTING)!=FR_OK) { UartDebug("failed to open file \n"); return; } //UartDebug("file has been opened \n"); f_lseek(&MyFile, (&MyFile)->fsize); //f_write(&MyFile, Mystring,strlen(Mystring)-1 , &bytesWritten); f_write(&MyFile, Mystring,len, &bytesWritten); f_sync(&MyFile); f_close(&MyFile); }
void log_sync(void) { if(sd_card_available == 0) { return; } unsigned int result = f_sync(&file); if(result != 0) { led_fastBlink(LED_SDCARD); sd_card_available = 0; return; } }
void log_(loglevel_t ll, const char* file, int line, const char* fmt, ...) { int r; unsigned int bw; va_list argp; if(ll > loglevel) return; if(logfile == NULL) log_open_(); if((uintptr_t)logfile == 1U) return; r = snprintf(prefix, 128, "[%s|%s:%d] ", loglevel2str[ll], file, line); if(r > 127) r = 127; f_write(logfile, prefix, r, &bw); va_start(argp, fmt); r = vsnprintf(logstr, LOGSTR_LENMAX, fmt, argp); if(r > LOGSTR_LENMAX-1) r = LOGSTR_LENMAX-1; f_write(logfile, logstr, r, &bw); f_write(logfile, "\r\n", 2, &bw); f_sync(logfile); }
static int fatfs_fdatasync(mount_point_t *point, file_t *file) { privinfo_t *priv = file->ctx; FRESULT res; if (priv == NULL) { seterrno(EINVAL); return -1; } res = f_sync(&priv->file); if (res == FR_OK) { return 0; } else { fatfs_result_to_errno(res); return -1; } }
static void modify_dev_list_file(uint8_t ep_id, uint8_t dev_type) { FIL fil; FRESULT f_res; uint32_t dev_list[ha_host_ns::max_end_point]; memset(dev_list, 0, sizeof(dev_list)); f_res = f_open(&fil, ha_host_ns::ha_dev_list_file_name, FA_READ | FA_OPEN_ALWAYS); if (f_res != FR_OK) { print_ferr(f_res); return; } char line[64]; for (uint8_t i = 0; i < ha_host_ns::max_end_point; i++) { if (f_gets(line, sizeof(line), &fil)) { sscanf(line, ha_host_ns::dev_list_pattern, &dev_list[i]); } else { break; } } f_close(&fil); dev_list[ep_id] = ((uint32_t) ha_ns::sixlowpan_node_id << 16) | ((uint32_t) ep_id << 8) | (uint32_t) dev_type; f_res = f_open(&fil, ha_host_ns::ha_dev_list_file_name, FA_WRITE | FA_CREATE_ALWAYS); if (f_res != FR_OK) { print_ferr(f_res); return; } f_sync(&fil); for (uint8_t i = 0; i < ha_host_ns::max_end_point; i++) { snprintf(line, sizeof(line), ha_host_ns::dev_list_pattern, dev_list[i]); f_puts(line, &fil); } f_close(&fil); return; }