Exemplo n.º 1
0
static uint8_t Test(CLS1_ConstStdIOTypePtr io) {
  static FIL fp;
  UINT bw;
  uint8_t read_buf[16];
  uint8_t write_buf[10];
  uint8_t i;

  if (FAT1_isWriteProtected()) {
    CLS1_SendStr((unsigned char*)"disk is write protected!\r\n", io->stdErr);
    return ERR_FAILED;
  }
  /* write file */
  CLS1_SendStr((const unsigned char*)"Creating test.txt...\r\n", io->stdOut);
  if (FAT1_open(&fp, "./test.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed creating file!\r\n", io->stdErr);
    return ERR_FAILED;
  }
  /* write text */
  if (FAT1_write(&fp, "Hello world ", sizeof("Hello world ")-1, &bw)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr);
    (void)FAT1_close(&fp);
    return ERR_FAILED;
  }
  write_buf[0] = '\0';
  for(i=0;i<4;i++) {
    UTIL1_strcatNum8u(write_buf, sizeof(write_buf), i);
    UTIL1_chcat(write_buf, sizeof(write_buf), ' ');
  }
  if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr);
    (void)FAT1_close(&fp);
    return ERR_FAILED;
  }
  /* closing file */
  (void)FAT1_close(&fp);

  /* read from file */
  CLS1_SendStr((const unsigned char*)"Read from file...\r\n", io->stdOut);
  if (FAT1_open(&fp, "./test.txt", FA_READ)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed opening file!\r\n", io->stdErr);
    return ERR_FAILED;
  }
  if (FAT1_read(&fp, &read_buf[0], sizeof(read_buf)-2, &bw)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed reading file!\r\n", io->stdErr);
    (void)FAT1_close(&fp);
    return ERR_FAILED;
  }
  read_buf[sizeof(read_buf)-2] = '\0'; /* terminate string */
  UTIL1_strcat(read_buf, sizeof(read_buf), (unsigned char*)"\r\n");
  CLS1_SendStr(read_buf, io->stdOut);
  CLS1_SendStr((const unsigned char*)"\r\n", io->stdOut);
  /* close file */
  (void)FAT1_close(&fp);
  return ERR_OK;
}
Exemplo n.º 2
0
static void Test(void) {
  UINT bw; /* number of bytes written */

  if (FAT1_isDiskPresent()) { /* if no hardware CardDetect pin is assigned, it will always return TRUE */
    LEDR_On(); /* turn red RGB LED on */
    FAT1_mount(0, &fs); /* mount file system */
    if (!FAT1_isWriteProtected()) { /* if no hardware WritePtotect pin is assigned, it will always return FALSE */
      LEDG_On(); /* turn green RGB LED on */
      if (FAT1_open(&fp, "./test.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) { /* open file, will always create it if not already on disk */
        for(;;){} /* error! */
      }
      if (FAT1_write(&fp, "Hello World!", sizeof("Hello World!")-1, &bw)!=FR_OK) { /* write string to file */
        for(;;){} /* error! */
      }
    }
    (void)FAT1_close(&fp); /* close file */
    FAT1_mount(0, NULL); /* unmount file system */
  }
}
Exemplo n.º 3
0
/*!
 * \brief Copy the source file to a destination file
 * \param[in] srcFileName Source file name
 * \param[in] dstFileName Destination file name
 * \param[in] io IO handler for output
 * \return Error code, ERR_OK for success.
 */
byte UFFS_FAT_CopyFile(const byte *srcFileName, const byte *dstFileName, const CLS1_StdIOType *io)
{
	bool sourceUffs = pdTRUE;	///< source is uffs (otherwise FAT)
	bool destUffs = pdTRUE;		///< destination is uffs (otherwise FAT)
	int fd1=-1, fd2=-1;			// uffs file pointers
	FAT1_FIL fsrc, fdst;  	// FAT file objects
	FAT1_FRESULT fres;		// FAT result
	uint8_t buffer[32];   /* copy buffer */
	UINT br, bw, bt=0;          /* file read/write counters */
	byte res =  ERR_OK;
	
	if( srcFileName[1] == ':')
		sourceUffs = pdFALSE;
	if( dstFileName[1] == ':')
	{
		destUffs = pdFALSE;
		if (FAT1_isWriteProtected() || FAT1_FS_READONLY) 
		{
			MSGLN("destination FAT disk is write protected!");
			return ERR_FAILED;
		}
	}

	/* open source file */
	
	if(sourceUffs)
	{
		fd1 = uffs_open((char*)srcFileName, UO_RDONLY);
		if (fd1  < 0) {
			MSGLN("open source file failed");
			return ERR_FAILED;
		}		
	}
	else
	{
		fres = FAT1_open(&fsrc, (char*)srcFileName, FA_OPEN_EXISTING | FA_READ);
		if (fres != FR_OK) 
			{
				MSGLN("open source file failed. Result=%d", fres);
				return ERR_FAILED;
			}
	}
	
	/* create destination file */
	
	if(destUffs)
	{
		fd2 = uffs_open((char*)dstFileName, UO_CREATE|UO_WRONLY);
		if (fd2  < 0) {
			MSGLN("open destination file failed");
			return ERR_FAILED;
		}		
	}
	else
	{
		fres = FAT1_open(&fdst, (char*)dstFileName, FA_CREATE_ALWAYS | FA_WRITE);
		if (fres != FR_OK) {
			MSGLN("open destination file failed. Result=%d", fres);
			return ERR_FAILED;
		}
	}
	
	/* now copy source to destination */
	
	for (;;) 
	{
		if(sourceUffs)
		{
			br = uffs_read(fd1, buffer, sizeof(buffer));
		}
		else
		{
			fres = FAT1_read(&fsrc, buffer, sizeof(buffer), &br);
			if (fres != FR_OK) 
			{
				MSGLN("reading source file failed. Result=%d", fres);
				res = ERR_FAILED;
				break;
			}
		}
		bt += br;
		if (br == 0) 
		{ /* EOF */
			break; /* get out of loop */
		}
		if(destUffs)
		{
			bw = uffs_write(fd2, buffer, br);
		}
		else
		{
			fres = FAT1_write(&fdst, buffer, br, &bw);
			if (fres != ERR_OK) {
				MSGLN("writing destination file failed. Result=%d", fres);
				res = ERR_FAILED;
				break;
			}			
		}
		if (bw < br) {
			MSGLN("failed writing destination file, or disk full");
			res = ERR_FAILED;
			break;
		}
	} /* for */
	
	/* close all files */
	
	if(sourceUffs)
		uffs_close(fd1);
	else
	{
		fres = FAT1_close(&fsrc);
		if (fres != FR_OK) 
		{
			MSGLN("closing source file failed. Result=%d", fres);
			res = ERR_FAILED;
		}
	}
	if(destUffs)
		uffs_close(fd2);
	else
	{
		fres = FAT1_close(&fdst);
		if (fres != FR_OK) {
			MSGLN("closing destination file failed. Result=%d", fres);
			res = ERR_FAILED;
		}
	}
	MSGLN("%u bytes copied.", bt);
	return res;
}