int _write_r (struct _reent *r, int fd, char * ptr, int len) { (void)r; if(fd==1 || fd==2) { int index; if (0 == (USART_CR1(USART1) & USART_CR1_UE)) return len; //Don't send if USART is disabled for(index=0; index<len; index++) { if (ptr[index] == '\n') { usart_send_blocking(USART1,'\r'); } usart_send_blocking(USART1, ptr[index]); } return len; } else if(fd>2) { if(file_open[fd-3]) { #ifdef MEDIA_DRIVE _drive_num = fd == 3 ? 0 : 1; #endif WORD bytes_written; pf_switchfile(&fat[fd-3]); int res=pf_write(ptr, len, &bytes_written); dbgprintf("_write_r: len %d, bytes_written %d, result %d\r\n",len, bytes_written, res); if(res==FR_OK) return bytes_written; } } errno=EINVAL; return -1; }
FRESULT PFFS::write ( const void *buff, /* Pointer to the data to be written */ WORD btw, /* Number of bytes to write */ WORD *bw /* Pointer to number of bytes written */ ) { return pf_write(buff, btw, bw); //ok }
/***************************************************************** 函数原型:zbClerkSaveFile(void) 功能描述:操作员文件初始化 参数描述: 返回值: OK ? NOTOK 作 者: 刘及华 日 期: 2012-11-24 修改历史: 日期 修改人 修改描述 ------ --------- ------------- *****************************************************************/ char zbClerkSaveFile(void) { // uchar ucret; uchar ucOpenID; pf_open((void *)clerkFileName, &ucOpenID); pf_write(ucOpenID, 0, (uchar *)clerk_list); pf_close(ucOpenID); return TRUE; }
int write_sdcard(void) { FATFS fs; /* Work area (file system object) for the volume */ BYTE buff[16]; /* File read buffer */ UINT br; /* File read count */ FRESULT res; /* Petit FatFs function common result code */ res = pf_mount(&fs); if (res) return res; res = pf_open("srcfile.dat"); if (res) return res; res = pf_write(buff, 16, &br); return res; }
void SD_::init() { #ifdef SKIPBLANKSD this->WorkingSDCard = true; while ( ! check( pf_mount(&this->SDCard) ) ); while ( ! check( pf_open("data.txt") )); this->SDOffset = 0; while ( ! check( pf_lseek(this->SDOffset) )); WORD written; while ( ! check (pf_write(0,0,&written)); if( WorkingSDCard ) { char InputBuffer[512]; WORD size; while(true) { pf_read(&InputBuffer, 512, &size); if((InputBuffer[0] == '%' && InputBuffer[1] == '$') || (InputBuffer[1] == '%' && InputBuffer[0] == '$')) { this->SDOffset += 512; pf_lseek(this->SDOffset); return; } this->SDOffset += 512; pf_lseek(this->SDOffset); } } #else this->TimeStamp = 0; WorkingSDCard = true; while ( ! check( pf_mount(&this->SDCard) )); while ( ! check( pf_open("data.txt") )); this->SDOffset = 0; while ( ! check( pf_lseek(this->SDOffset) )); #endif }
uint16_t testSDCard(void) { FATFS fatfs; /* File system object */ DIR dir; /* Directory object */ FILINFO fno; /* File information object */ WORD bw, br, i; BYTE buff[64]; print("Mount a volume."); RET(); FRESULT rc = pf_mount(&fatfs); if (rc) die(rc); print("Open a test file (message.txt)."); RET(); rc = pf_open("MESSAGE.TXT"); if (rc) die(rc); print("Type the file content."); RET(); for (;;) { rc = pf_read(buff, sizeof(buff), &br); /* Read a chunk of file */ if (rc || !br) break; /* Error or end of file */ for (i = 0; i < br; i++) /* Type the data */ putchar(buff[i]); } if (rc) die(rc); #if _USE_WRITE sprintf(string,"\nOpen a file to write (write.txt).\n"); print(string); rc = pf_open("WRITE.TXT"); if (rc) die(rc); sprintf(string,"\nWrite a text data. (Hello world!)\n"); print(string); for (;;) { rc = pf_write("Hello world!\r\n", 14, &bw); if (rc || !bw) break; } if (rc) die(rc); sprintf(string,"\nTerminate the file write process.\n"); print(string); rc = pf_write(0, 0, &bw); if (rc) die(rc); #endif #if _USE_DIR print("Open root directory."); RET(); rc = pf_opendir(&dir, ""); if (rc) die(rc); print("Directory listing..."); RET(); for (;;) { rc = pf_readdir(&dir, &fno); /* Read a directory item */ if (rc || !fno.fname[0]) break; /* Error or end of dir */ if (fno.fattrib & AM_DIR) { print(" <dir> "); print(fno.fname); RET(); } else { printInt((uint8_t*)&fno.fsize, sizeof(fno.fsize)); print(fno.fname); RET(); } } if (rc) die(rc); #endif print("\nTest completed.\n"); while(1) { delay_ten_us(50000); delay_ten_us(50000); P1OUT ^= (1 << RED_LED); } return 0xaa55; }
size_t PFFS::write(uint8_t character){ WORD bw; pf_write(&character, 1, &bw); //ok return (bw == 1) ? 1 : -1; }
int main(void) { lcd_init(); ds1307_init(); adc_init(); timer_init(); asm("sei"); /* //test lcd_str("Hello, World"); lcd_gotoxy(1,0); lcd_str("by shtomik"); _delay_ms(1000); lcd_clr(); lcd_str("after clr");*/ start_t(); //test ds1307_setdate(12, 12, 31, 23, 01, 35); //смонтировать microSD res = pf_mount(&fs); if(res == FR_OK) { //диск смонтирован и мы продолжаем работу if(pf_open("test.txt") == FR_OK) { //открыли файл в корне test.txt //... pf_lseek(0); //заполняем буффер, надо набрать 512 байт инфы и писать //иначе будет много лишних пробелов //sprintf(buff, "%s\n%s", "Hello,World!", "by_sht"); //записываем его на карту pf_write(buff, 512, &br); //финализируем запись pf_write(NULL, 0, &br); pf_lseek(512); pf_write(buff, 512, &br); //финализируем запись pf_write(NULL, 0, &br); lcd_clr(); lcd_str("ok"); } else { //file not found or ... lcd_str("file not found"); } //демонтируем диск передав функции нулевой указатель pf_mount(NULL); lcd_clr(); lcd_str("file write"); } else { //не удалось смонтировать диск lcd_clr(); lcd_str("not flash"); } while( 1 ) { ; } return 0; }
FRESULT PFFS::close ( void /* Working copy file object will be closed */ ) { WORD bw; return pf_write(0, 0, &bw); }