Przeglądaj źródła

Update FatFs module
Fix problems found with prototypes
Replace xitoa with xprintf

k4be 2 lat temu
rodzic
commit
11f6d5e867
13 zmienionych plików z 2155 dodań i 1542 usunięć
  1. 4 4
      soft/Makefile
  2. 45 32
      soft/diskio.h
  3. 812 537
      soft/ff.c
  4. 245 165
      soft/ff.h
  5. 229 150
      soft/ffconf.h
  6. 62 42
      soft/main.c
  7. 14 0
      soft/main.h
  8. 3 4
      soft/mmc.c
  9. 15 6
      soft/uart1.c
  10. 0 500
      soft/xitoa.S
  11. 0 102
      soft/xitoa.h
  12. 673 0
      soft/xprintf.c
  13. 53 0
      soft/xprintf.h

+ 4 - 4
soft/Makefile

@@ -2,8 +2,8 @@
 PROJECT	= glg
 
 ### Source files and search directory
-CSRC    = main.c uart0.c uart1.c ff.c mmc.c 1wire.c ds18b20.c expander.c HD44780-I2C.c I2C.c
-ASRC    = xitoa.S stime.S
+CSRC    = main.c uart0.c uart1.c ff.c mmc.c 1wire.c ds18b20.c expander.c HD44780-I2C.c I2C.c xprintf.c
+ASRC    = stime.S
 VPATH   =
 
 ### Target device
@@ -13,7 +13,7 @@ DEVICE  = atmega644pa
 OPTIMIZE = s
 
 ### C Standard level (c89, gnu89, c99 or gnu99)
-CSTD = c99
+#CSTD = c99
 
 ### Include dirs, library dirs and definitions
 LIBS	=
@@ -52,7 +52,7 @@ PROJECT   := $(OBJDIR)/$(PROJECT)
 
 
 # Flags for C files
-CFLAGS += -std=$(CSTD)
+#CFLAGS += -std=$(CSTD)
 CFLAGS += -g$(DEBUG)
 CFLAGS += -mmcu=$(DEVICE)
 CFLAGS += -O$(OPTIMIZE) -mcall-prologues

+ 45 - 32
soft/diskio.h

@@ -1,13 +1,13 @@
-/*-----------------------------------------------------------------------
-/  Low level disk interface modlue include file  R0.04a   (C)ChaN, 2007
+/*-----------------------------------------------------------------------/
+/  Low level disk interface modlue include file   (C)ChaN, 2019          /
 /-----------------------------------------------------------------------*/
 
-#ifndef _DISKIO
-
-#define _READONLY	0	/* 1: Read-only mode */
-
-#include "integer.h"
+#ifndef _DISKIO_DEFINED
+#define _DISKIO_DEFINED
 
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 /* Status of Disk Functions */
 typedef BYTE	DSTATUS;
@@ -25,16 +25,12 @@ typedef enum {
 /*---------------------------------------*/
 /* Prototypes for disk control functions */
 
-DSTATUS disk_initialize (BYTE);
-DSTATUS disk_status (BYTE);
-DRESULT disk_read (BYTE, BYTE*, DWORD, UINT);
-#if	_READONLY == 0
-DRESULT disk_write (BYTE, const BYTE*, DWORD, UINT);
-#endif
-DRESULT disk_ioctl (BYTE, BYTE, void*);
-void	disk_timerproc (void);
-
 
+DSTATUS disk_initialize (BYTE pdrv);
+DSTATUS disk_status (BYTE pdrv);
+DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
+DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
+DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
 
 
 /* Disk Status Bits (DSTATUS) */
@@ -44,21 +40,38 @@ void	disk_timerproc (void);
 #define STA_PROTECT		0x04	/* Write protected */
 
 
-/* Command code for disk_ioctrl() */
-
-#define GET_SECTOR_COUNT	1
-#define GET_SECTOR_SIZE		2
-#define CTRL_SYNC			3
-#define CTRL_POWER_OFF		4
-#define CTRL_LOCK			5
-#define CTRL_EJECT			6
-#define MMC_GET_CSD			10
-#define MMC_GET_CID			11
-#define MMC_GET_OCR			12
-#define ATA_GET_REV			20
-#define ATA_GET_MODEL		21
-#define ATA_GET_SN			22
-
+/* Command code for disk_ioctrl fucntion */
+
+/* Generic command (Used by FatFs) */
+#define CTRL_SYNC			0	/* Complete pending write process (needed at FF_FS_READONLY == 0) */
+#define GET_SECTOR_COUNT	1	/* Get media size (needed at FF_USE_MKFS == 1) */
+#define GET_SECTOR_SIZE		2	/* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
+#define GET_BLOCK_SIZE		3	/* Get erase block size (needed at FF_USE_MKFS == 1) */
+#define CTRL_TRIM			4	/* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */
+
+/* Generic command (Not used by FatFs) */
+#define CTRL_POWER			5	/* Get/Set power status */
+#define CTRL_LOCK			6	/* Lock/Unlock media removal */
+#define CTRL_EJECT			7	/* Eject media */
+#define CTRL_FORMAT			8	/* Create physical format on the media */
+
+/* MMC/SDC specific ioctl command */
+#define MMC_GET_TYPE		10	/* Get card type */
+#define MMC_GET_CSD			11	/* Get CSD */
+#define MMC_GET_CID			12	/* Get CID */
+#define MMC_GET_OCR			13	/* Get OCR */
+#define MMC_GET_SDSTAT		14	/* Get SD status */
+#define ISDIO_READ			55	/* Read data form SD iSDIO register */
+#define ISDIO_WRITE			56	/* Write data to SD iSDIO register */
+#define ISDIO_MRITE			57	/* Masked write data to SD iSDIO register */
+
+/* ATA/CF specific ioctl command */
+#define ATA_GET_REV			20	/* Get F/W revision */
+#define ATA_GET_MODEL		21	/* Get model name */
+#define ATA_GET_SN			22	/* Get serial number */
+
+#ifdef __cplusplus
+}
+#endif
 
-#define _DISKIO
 #endif

Plik diff jest za duży
+ 812 - 537
soft/ff.c


+ 245 - 165
soft/ff.h

@@ -1,137 +1,223 @@
-/*---------------------------------------------------------------------------/
-/  FatFs - FAT file system module include file  R0.10a    (C)ChaN, 2014
-/----------------------------------------------------------------------------/
-/ FatFs module is a generic FAT file system module for small embedded systems.
-/ This is a free software that opened for education, research and commercial
-/ developments under license policy of following terms.
+/*----------------------------------------------------------------------------/
+/  FatFs - Generic FAT Filesystem module  R0.14b                              /
+/-----------------------------------------------------------------------------/
 /
-/  Copyright (C) 2014, ChaN, all right reserved.
+/ Copyright (C) 2021, ChaN, all right reserved.
 /
-/ * The FatFs module is a free software and there is NO WARRANTY.
-/ * No restriction on use. You can use, modify and redistribute it for
-/   personal, non-profit or commercial product UNDER YOUR RESPONSIBILITY.
-/ * Redistributions of source code must retain the above copyright notice.
+/ FatFs module is an open source software. Redistribution and use of FatFs in
+/ source and binary forms, with or without modification, are permitted provided
+/ that the following condition is met:
+
+/ 1. Redistributions of source code must retain the above copyright notice,
+/    this condition and the following disclaimer.
+/
+/ This software is provided by the copyright holder and contributors "AS IS"
+/ and any warranties related to this software are DISCLAIMED.
+/ The copyright owner or contributors be NOT LIABLE for any damages caused
+/ by use of this software.
 /
 /----------------------------------------------------------------------------*/
 
-#ifndef _FATFS
-#define _FATFS	29000	/* Revision ID */
+
+#ifndef FF_DEFINED
+#define FF_DEFINED	86631	/* Revision ID */
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#include "integer.h"	/* Basic integer types */
 #include "ffconf.h"		/* FatFs configuration options */
 
-#if _FATFS != _FFCONF
+#if FF_DEFINED != FFCONF_DEF
 #error Wrong configuration file (ffconf.h).
 #endif
 
 
+/* Integer types used for FatFs API */
+
+#if defined(_WIN32)		/* Windows VC++ (for development only) */
+#define FF_INTDEF 2
+#include <windows.h>
+typedef unsigned __int64 QWORD;
+#include <float.h>
+#define isnan(v) _isnan(v)
+#define isinf(v) (!_finite(v))
+
+#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus)	/* C99 or later */
+#define FF_INTDEF 2
+#include <stdint.h>
+typedef unsigned int	UINT;	/* int must be 16-bit or 32-bit */
+typedef unsigned char	BYTE;	/* char must be 8-bit */
+typedef uint16_t		WORD;	/* 16-bit unsigned integer */
+typedef uint32_t		DWORD;	/* 32-bit unsigned integer */
+typedef uint64_t		QWORD;	/* 64-bit unsigned integer */
+typedef WORD			WCHAR;	/* UTF-16 character type */
+
+#else  	/* Earlier than C99 */
+#define FF_INTDEF 1
+typedef unsigned int	UINT;	/* int must be 16-bit or 32-bit */
+typedef unsigned char	BYTE;	/* char must be 8-bit */
+typedef unsigned short	WORD;	/* 16-bit unsigned integer */
+typedef unsigned long	DWORD;	/* 32-bit unsigned integer */
+typedef WORD			WCHAR;	/* UTF-16 character type */
+#endif
 
-/* Definitions of volume management */
-
-#if _MULTI_PARTITION		/* Multiple partition configuration */
-typedef struct {
-	BYTE pd;	/* Physical drive number */
-	BYTE pt;	/* Partition: 0:Auto detect, 1-4:Forced partition) */
-} PARTITION;
-extern PARTITION VolToPart[];	/* Volume - Partition resolution table */
-#define LD2PD(vol) (VolToPart[vol].pd)	/* Get physical drive number */
-#define LD2PT(vol) (VolToPart[vol].pt)	/* Get partition index */
 
-#else							/* Single partition configuration */
-#define LD2PD(vol) (BYTE)(vol)	/* Each logical drive is bound to the same physical drive number */
-#define LD2PT(vol) 0			/* Find first valid partition or in SFD */
+/* Type of file size and LBA variables */
 
+#if FF_FS_EXFAT
+#if FF_INTDEF != 2
+#error exFAT feature wants C99 or later
+#endif
+typedef QWORD FSIZE_t;
+#if FF_LBA64
+typedef QWORD LBA_t;
+#else
+typedef DWORD LBA_t;
+#endif
+#else
+#if FF_LBA64
+#error exFAT needs to be enabled when enable 64-bit LBA
+#endif
+typedef DWORD FSIZE_t;
+typedef DWORD LBA_t;
 #endif
 
 
 
-/* Type of path name strings on FatFs API */
+/* Type of path name strings on FatFs API (TCHAR) */
 
-#if _LFN_UNICODE			/* Unicode string */
-#if !_USE_LFN
-#error _LFN_UNICODE must be 0 in non-LFN cfg.
-#endif
-#ifndef _INC_TCHAR
+#if FF_USE_LFN && FF_LFN_UNICODE == 1 	/* Unicode in UTF-16 encoding */
 typedef WCHAR TCHAR;
 #define _T(x) L ## x
 #define _TEXT(x) L ## x
-#endif
-
-#else						/* ANSI/OEM string */
-#ifndef _INC_TCHAR
+#elif FF_USE_LFN && FF_LFN_UNICODE == 2	/* Unicode in UTF-8 encoding */
+typedef char TCHAR;
+#define _T(x) u8 ## x
+#define _TEXT(x) u8 ## x
+#elif FF_USE_LFN && FF_LFN_UNICODE == 3	/* Unicode in UTF-32 encoding */
+typedef DWORD TCHAR;
+#define _T(x) U ## x
+#define _TEXT(x) U ## x
+#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
+#error Wrong FF_LFN_UNICODE setting
+#else									/* ANSI/OEM code in SBCS/DBCS */
 typedef char TCHAR;
 #define _T(x) x
 #define _TEXT(x) x
 #endif
 
+
+
+/* Definitions of volume management */
+
+#if FF_MULTI_PARTITION		/* Multiple partition configuration */
+typedef struct {
+	BYTE pd;	/* Physical drive number */
+	BYTE pt;	/* Partition: 0:Auto detect, 1-4:Forced partition) */
+} PARTITION;
+extern PARTITION VolToPart[];	/* Volume - Partition mapping table */
+#endif
+
+#if FF_STR_VOLUME_ID
+#ifndef FF_VOLUME_STRS
+extern const char* VolumeStr[FF_VOLUMES];	/* User defied volume ID */
+#endif
 #endif
 
 
 
-/* File system object structure (FATFS) */
+/* Filesystem object structure (FATFS) */
 
 typedef struct {
-	BYTE	fs_type;		/* FAT sub-type (0:Not mounted) */
-	BYTE	drv;			/* Physical drive number */
-	BYTE	csize;			/* Sectors per cluster (1,2,4...128) */
-	BYTE	n_fats;			/* Number of FAT copies (1 or 2) */
+	BYTE	fs_type;		/* Filesystem type (0:not mounted) */
+	BYTE	pdrv;			/* Associated physical drive */
+	BYTE	n_fats;			/* Number of FATs (1 or 2) */
 	BYTE	wflag;			/* win[] flag (b0:dirty) */
 	BYTE	fsi_flag;		/* FSINFO flags (b7:disabled, b0:dirty) */
-	WORD	id;				/* File system mount ID */
+	WORD	id;				/* Volume mount ID */
 	WORD	n_rootdir;		/* Number of root directory entries (FAT12/16) */
-#if _MAX_SS != _MIN_SS
-	WORD	ssize;			/* Bytes per sector (512, 1024, 2048 or 4096) */
+	WORD	csize;			/* Cluster size [sectors] */
+#if FF_MAX_SS != FF_MIN_SS
+	WORD	ssize;			/* Sector size (512, 1024, 2048 or 4096) */
 #endif
-#if _FS_REENTRANT
-	_SYNC_t	sobj;			/* Identifier of sync object */
+#if FF_USE_LFN
+	WCHAR*	lfnbuf;			/* LFN working buffer */
 #endif
-#if !_FS_READONLY
-	DWORD	last_clust;		/* Last allocated cluster */
-	DWORD	free_clust;		/* Number of free clusters */
+#if FF_FS_EXFAT
+	BYTE*	dirbuf;			/* Directory entry block scratchpad buffer for exFAT */
 #endif
-#if _FS_RPATH
+#if FF_FS_REENTRANT
+	FF_SYNC_t	sobj;		/* Identifier of sync object */
+#endif
+#if !FF_FS_READONLY
+	DWORD	last_clst;		/* Last allocated cluster */
+	DWORD	free_clst;		/* Number of free clusters */
+#endif
+#if FF_FS_RPATH
 	DWORD	cdir;			/* Current directory start cluster (0:root) */
+#if FF_FS_EXFAT
+	DWORD	cdc_scl;		/* Containing directory start cluster (invalid when cdir is 0) */
+	DWORD	cdc_size;		/* b31-b8:Size of containing directory, b7-b0: Chain status */
+	DWORD	cdc_ofs;		/* Offset in the containing directory (invalid when cdir is 0) */
+#endif
+#endif
+	DWORD	n_fatent;		/* Number of FAT entries (number of clusters + 2) */
+	DWORD	fsize;			/* Size of an FAT [sectors] */
+	LBA_t	volbase;		/* Volume base sector */
+	LBA_t	fatbase;		/* FAT base sector */
+	LBA_t	dirbase;		/* Root directory base sector/cluster */
+	LBA_t	database;		/* Data base sector */
+#if FF_FS_EXFAT
+	LBA_t	bitbase;		/* Allocation bitmap base sector */
 #endif
-	DWORD	n_fatent;		/* Number of FAT entries (= number of clusters + 2) */
-	DWORD	fsize;			/* Sectors per FAT */
-	DWORD	volbase;		/* Volume start sector */
-	DWORD	fatbase;		/* FAT start sector */
-	DWORD	dirbase;		/* Root directory start sector (FAT32:Cluster#) */
-	DWORD	database;		/* Data start sector */
-	DWORD	winsect;		/* Current sector appearing in the win[] */
-	BYTE	win[_MAX_SS];	/* Disk access window for Directory, FAT (and file data at tiny cfg) */
+	LBA_t	winsect;		/* Current sector appearing in the win[] */
+	BYTE	win[FF_MAX_SS];	/* Disk access window for Directory, FAT (and file data at tiny cfg) */
 } FATFS;
 
 
 
+/* Object ID and allocation information (FFOBJID) */
+
+typedef struct {
+	FATFS*	fs;				/* Pointer to the hosting volume of this object */
+	WORD	id;				/* Hosting volume mount ID */
+	BYTE	attr;			/* Object attribute */
+	BYTE	stat;			/* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
+	DWORD	sclust;			/* Object data start cluster (0:no cluster or root directory) */
+	FSIZE_t	objsize;		/* Object size (valid when sclust != 0) */
+#if FF_FS_EXFAT
+	DWORD	n_cont;			/* Size of first fragment - 1 (valid when stat == 3) */
+	DWORD	n_frag;			/* Size of last fragment needs to be written to FAT (valid when not zero) */
+	DWORD	c_scl;			/* Containing directory start cluster (valid when sclust != 0) */
+	DWORD	c_size;			/* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
+	DWORD	c_ofs;			/* Offset in the containing directory (valid when file object and sclust != 0) */
+#endif
+#if FF_FS_LOCK
+	UINT	lockid;			/* File lock ID origin from 1 (index of file semaphore table Files[]) */
+#endif
+} FFOBJID;
+
+
+
 /* File object structure (FIL) */
 
 typedef struct {
-	FATFS*	fs;				/* Pointer to the related file system object (**do not change order**) */
-	WORD	id;				/* Owner file system mount ID (**do not change order**) */
+	FFOBJID	obj;			/* Object identifier (must be the 1st member to detect invalid object pointer) */
 	BYTE	flag;			/* File status flags */
 	BYTE	err;			/* Abort flag (error code) */
-	DWORD	fptr;			/* File read/write pointer (Zeroed on file open) */
-	DWORD	fsize;			/* File size */
-	DWORD	sclust;			/* File data start cluster (0:no data cluster, always 0 when fsize is 0) */
-	DWORD	clust;			/* Current cluster of fpter */
-	DWORD	dsect;			/* Current data sector of fpter */
-#if !_FS_READONLY
-	DWORD	dir_sect;		/* Sector containing the directory entry */
-	BYTE*	dir_ptr;		/* Pointer to the directory entry in the window */
-#endif
-#if _USE_FASTSEEK
-	DWORD*	cltbl;			/* Pointer to the cluster link map table (Nulled on file open) */
+	FSIZE_t	fptr;			/* File read/write pointer (Zeroed on file open) */
+	DWORD	clust;			/* Current cluster of fpter (invalid when fptr is 0) */
+	LBA_t	sect;			/* Sector number appearing in buf[] (0:invalid) */
+#if !FF_FS_READONLY
+	LBA_t	dir_sect;		/* Sector number containing the directory entry (not used at exFAT) */
+	BYTE*	dir_ptr;		/* Pointer to the directory entry in the win[] (not used at exFAT) */
 #endif
-#if _FS_LOCK
-	UINT	lockid;			/* File lock ID (index of file semaphore table Files[]) */
+#if FF_USE_FASTSEEK
+	DWORD*	cltbl;			/* Pointer to the cluster link map table (nulled on open, set by application) */
 #endif
-#if !_FS_TINY
-	BYTE	buf[_MAX_SS];	/* File data read/write buffer */
+#if !FF_FS_TINY
+	BYTE	buf[FF_MAX_SS];	/* File private data read/write window */
 #endif
 } FIL;
 
@@ -140,41 +226,51 @@ typedef struct {
 /* Directory object structure (DIR) */
 
 typedef struct {
-	FATFS*	fs;				/* Pointer to the owner file system object (**do not change order**) */
-	WORD	id;				/* Owner file system mount ID (**do not change order**) */
-	WORD	index;			/* Current read/write index number */
-	DWORD	sclust;			/* Table start cluster (0:Root dir) */
+	FFOBJID	obj;			/* Object identifier */
+	DWORD	dptr;			/* Current read/write offset */
 	DWORD	clust;			/* Current cluster */
-	DWORD	sect;			/* Current sector */
-	BYTE*	dir;			/* Pointer to the current SFN entry in the win[] */
-	BYTE*	fn;				/* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
-#if _FS_LOCK
-	UINT	lockid;			/* File lock ID (index of file semaphore table Files[]) */
+	LBA_t	sect;			/* Current sector (0:Read operation has terminated) */
+	BYTE*	dir;			/* Pointer to the directory item in the win[] */
+	BYTE	fn[12];			/* SFN (in/out) {body[8],ext[3],status[1]} */
+#if FF_USE_LFN
+	DWORD	blk_ofs;		/* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
 #endif
-#if _USE_LFN
-	WCHAR*	lfn;			/* Pointer to the LFN working buffer */
-	WORD	lfn_idx;		/* Last matched LFN index number (0xFFFF:No LFN) */
+#if FF_USE_FIND
+	const TCHAR* pat;		/* Pointer to the name matching pattern */
 #endif
 } DIR;
 
 
 
-/* File status structure (FILINFO) */
+/* File information structure (FILINFO) */
 
 typedef struct {
-	DWORD	fsize;			/* File size */
-	WORD	fdate;			/* Last modified date */
-	WORD	ftime;			/* Last modified time */
-	BYTE	fattrib;		/* Attribute */
-	TCHAR	fname[13];		/* Short file name (8.3 format) */
-#if _USE_LFN
-	TCHAR*	lfname;			/* Pointer to the LFN buffer */
-	UINT 	lfsize;			/* Size of LFN buffer in TCHAR */
+	FSIZE_t	fsize;			/* File size */
+	WORD	fdate;			/* Modified date */
+	WORD	ftime;			/* Modified time */
+	BYTE	fattrib;		/* File attribute */
+#if FF_USE_LFN
+	TCHAR	altname[FF_SFN_BUF + 1];/* Altenative file name */
+	TCHAR	fname[FF_LFN_BUF + 1];	/* Primary file name */
+#else
+	TCHAR	fname[12 + 1];	/* File name */
 #endif
 } FILINFO;
 
 
 
+/* Format parameter structure (MKFS_PARM) */
+
+typedef struct {
+	BYTE fmt;			/* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
+	BYTE n_fat;			/* Number of FATs */
+	UINT align;			/* Data area alignment (sector) */
+	UINT n_root;		/* Number of root directory entries */
+	DWORD au_size;		/* Cluster size (byte) */
+} MKFS_PARM;
+
+
+
 /* File function return code (FRESULT) */
 
 typedef enum {
@@ -192,11 +288,11 @@ typedef enum {
 	FR_INVALID_DRIVE,		/* (11) The logical drive number is invalid */
 	FR_NOT_ENABLED,			/* (12) The volume has no work area */
 	FR_NO_FILESYSTEM,		/* (13) There is no valid FAT volume */
-	FR_MKFS_ABORTED,		/* (14) The f_mkfs() aborted due to any parameter error */
+	FR_MKFS_ABORTED,		/* (14) The f_mkfs() aborted due to any problem */
 	FR_TIMEOUT,				/* (15) Could not get a grant to access the volume within defined period */
 	FR_LOCKED,				/* (16) The operation is rejected according to the file sharing policy */
 	FR_NOT_ENOUGH_CORE,		/* (17) LFN working buffer could not be allocated */
-	FR_TOO_MANY_OPEN_FILES,	/* (18) Number of open files > _FS_SHARE */
+	FR_TOO_MANY_OPEN_FILES,	/* (18) Number of open files > FF_FS_LOCK */
 	FR_INVALID_PARAMETER	/* (19) Given parameter is invalid */
 } FRESULT;
 
@@ -207,43 +303,47 @@ typedef enum {
 
 FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode);				/* Open or create a file */
 FRESULT f_close (FIL* fp);											/* Close an open file object */
-FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);			/* Read data from a file */
-FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw);	/* Write data to a file */
-FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf);	/* Forward data to the stream */
-FRESULT f_lseek (FIL* fp, DWORD ofs);								/* Move file pointer of a file object */
-FRESULT f_truncate (FIL* fp);										/* Truncate file */
-FRESULT f_sync (FIL* fp);											/* Flush cached data of a writing file */
+FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);			/* Read data from the file */
+FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw);	/* Write data to the file */
+FRESULT f_lseek (FIL* fp, FSIZE_t ofs);								/* Move file pointer of the file object */
+FRESULT f_truncate (FIL* fp);										/* Truncate the file */
+FRESULT f_sync (FIL* fp);											/* Flush cached data of the writing file */
 FRESULT f_opendir (DIR* dp, const TCHAR* path);						/* Open a directory */
 FRESULT f_closedir (DIR* dp);										/* Close an open directory */
 FRESULT f_readdir (DIR* dp, FILINFO* fno);							/* Read a directory item */
+FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern);	/* Find first file */
+FRESULT f_findnext (DIR* dp, FILINFO* fno);							/* Find next file */
 FRESULT f_mkdir (const TCHAR* path);								/* Create a sub directory */
 FRESULT f_unlink (const TCHAR* path);								/* Delete an existing file or directory */
 FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new);	/* Rename/Move a file or directory */
 FRESULT f_stat (const TCHAR* path, FILINFO* fno);					/* Get file status */
-FRESULT f_chmod (const TCHAR* path, BYTE value, BYTE mask);			/* Change attribute of the file/dir */
-FRESULT f_utime (const TCHAR* path, const FILINFO* fno);			/* Change times-tamp of the file/dir */
+FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask);			/* Change attribute of a file/dir */
+FRESULT f_utime (const TCHAR* path, const FILINFO* fno);			/* Change timestamp of a file/dir */
 FRESULT f_chdir (const TCHAR* path);								/* Change current directory */
 FRESULT f_chdrive (const TCHAR* path);								/* Change current drive */
 FRESULT f_getcwd (TCHAR* buff, UINT len);							/* Get current directory */
 FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs);	/* Get number of free clusters on the drive */
-FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* sn);	/* Get volume label */
+FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn);	/* Get volume label */
 FRESULT f_setlabel (const TCHAR* label);							/* Set volume label */
+FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf);	/* Forward data to the stream */
+FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt);					/* Allocate a contiguous block to the file */
 FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt);			/* Mount/Unmount a logical drive */
-FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au);				/* Create a file system on the volume */
-FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work);			/* Divide a physical drive into some partitions */
+FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len);	/* Create a FAT volume */
+FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work);		/* Divide a physical drive into some partitions */
+FRESULT f_setcp (WORD cp);											/* Set current code page */
 int f_putc (TCHAR c, FIL* fp);										/* Put a character to the file */
 int f_puts (const TCHAR* str, FIL* cp);								/* Put a string to the file */
 int f_printf (FIL* fp, const TCHAR* str, ...);						/* Put a formatted string to the file */
 TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);						/* Get a string from the file */
 
-#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
+#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
 #define f_error(fp) ((fp)->err)
 #define f_tell(fp) ((fp)->fptr)
-#define f_size(fp) ((fp)->fsize)
-
-#ifndef EOF
-#define EOF (-1)
-#endif
+#define f_size(fp) ((fp)->obj.objsize)
+#define f_rewind(fp) f_lseek((fp), 0)
+#define f_rewinddir(dp) f_readdir((dp), 0)
+#define f_rmdir(path) f_unlink(path)
+#define f_unmount(path) f_mount(0, path, 0)
 
 
 
@@ -252,26 +352,27 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);						/* Get a string from the fil
 /* Additional user defined functions                            */
 
 /* RTC function */
-#if !_FS_READONLY
+#if !FF_FS_READONLY && !FF_FS_NORTC
 DWORD get_fattime (void);
 #endif
 
-/* Unicode support functions */
-#if _USE_LFN							/* Unicode - OEM code conversion */
-WCHAR ff_convert (WCHAR chr, UINT dir);	/* OEM-Unicode bidirectional conversion */
-WCHAR ff_wtoupper (WCHAR chr);			/* Unicode upper-case conversion */
-#if _USE_LFN == 3						/* Memory functions */
+/* LFN support functions */
+#if FF_USE_LFN >= 1						/* Code conversion (defined in unicode.c) */
+WCHAR ff_oem2uni (WCHAR oem, WORD cp);	/* OEM code to Unicode conversion */
+WCHAR ff_uni2oem (DWORD uni, WORD cp);	/* Unicode to OEM code conversion */
+DWORD ff_wtoupper (DWORD uni);			/* Unicode upper-case conversion */
+#endif
+#if FF_USE_LFN == 3						/* Dynamic memory allocation */
 void* ff_memalloc (UINT msize);			/* Allocate memory block */
 void ff_memfree (void* mblock);			/* Free memory block */
 #endif
-#endif
 
 /* Sync functions */
-#if _FS_REENTRANT
-int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj);	/* Create a sync object */
-int ff_req_grant (_SYNC_t sobj);				/* Lock sync object */
-void ff_rel_grant (_SYNC_t sobj);				/* Unlock sync object */
-int ff_del_syncobj (_SYNC_t sobj);				/* Delete a sync object */
+#if FF_FS_REENTRANT
+int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj);	/* Create a sync object */
+int ff_req_grant (FF_SYNC_t sobj);		/* Lock sync object */
+void ff_rel_grant (FF_SYNC_t sobj);		/* Unlock sync object */
+int ff_del_syncobj (FF_SYNC_t sobj);	/* Delete a sync object */
 #endif
 
 
@@ -281,62 +382,41 @@ int ff_del_syncobj (_SYNC_t sobj);				/* Delete a sync object */
 /* Flags and offset address                                     */
 
 
-/* File access control and file status flags (FIL.flag) */
-
+/* File access mode and open method flags (3rd argument of f_open) */
 #define	FA_READ				0x01
-#define	FA_OPEN_EXISTING	0x00
-
-#if !_FS_READONLY
 #define	FA_WRITE			0x02
+#define	FA_OPEN_EXISTING	0x00
 #define	FA_CREATE_NEW		0x04
 #define	FA_CREATE_ALWAYS	0x08
 #define	FA_OPEN_ALWAYS		0x10
-#define FA__WRITTEN			0x20
-#define FA__DIRTY			0x40
-#endif
+#define	FA_OPEN_APPEND		0x30
 
+/* Fast seek controls (2nd argument of f_lseek) */
+#define CREATE_LINKMAP	((FSIZE_t)0 - 1)
 
-/* FAT sub type (FATFS.fs_type) */
+/* Format options (2nd argument of f_mkfs) */
+#define FM_FAT		0x01
+#define FM_FAT32	0x02
+#define FM_EXFAT	0x04
+#define FM_ANY		0x07
+#define FM_SFD		0x08
 
+/* Filesystem type (FATFS.fs_type) */
 #define FS_FAT12	1
 #define FS_FAT16	2
 #define FS_FAT32	3
+#define FS_EXFAT	4
 
-
-/* File attribute bits for directory entry */
-
+/* File attribute bits for directory entry (FILINFO.fattrib) */
 #define	AM_RDO	0x01	/* Read only */
 #define	AM_HID	0x02	/* Hidden */
 #define	AM_SYS	0x04	/* System */
-#define	AM_VOL	0x08	/* Volume label */
-#define AM_LFN	0x0F	/* LFN entry */
 #define AM_DIR	0x10	/* Directory */
 #define AM_ARC	0x20	/* Archive */
-#define AM_MASK	0x3F	/* Mask of defined bits */
-
-
-/* Fast seek feature */
-#define CREATE_LINKMAP	0xFFFFFFFF
 
 
-
-/*--------------------------------*/
-/* Multi-byte word access macros  */
-
-#if _WORD_ACCESS == 1	/* Enable word access to the FAT structure */
-#define	LD_WORD(ptr)		(WORD)(*(WORD*)(BYTE*)(ptr))
-#define	LD_DWORD(ptr)		(DWORD)(*(DWORD*)(BYTE*)(ptr))
-#define	ST_WORD(ptr,val)	*(WORD*)(BYTE*)(ptr)=(WORD)(val)
-#define	ST_DWORD(ptr,val)	*(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
-#else					/* Use byte-by-byte access to the FAT structure */
-#define	LD_WORD(ptr)		(WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
-#define	LD_DWORD(ptr)		(DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
-#define	ST_WORD(ptr,val)	*(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
-#define	ST_DWORD(ptr,val)	*(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8); *((BYTE*)(ptr)+2)=(BYTE)((DWORD)(val)>>16); *((BYTE*)(ptr)+3)=(BYTE)((DWORD)(val)>>24)
-#endif
-
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* _FATFS */
+#endif /* FF_DEFINED */

+ 229 - 150
soft/ffconf.h

@@ -1,180 +1,222 @@
 /*---------------------------------------------------------------------------/
-/  FatFs - FAT file system module configuration file  R0.10a (C)ChaN, 2014
+/  FatFs Functional Configurations
 /---------------------------------------------------------------------------*/
 
-#ifndef _FFCONF
-#define _FFCONF 29000	/* Revision ID */
-
+#define FFCONF_DEF	86631	/* Revision ID */
 
 /*---------------------------------------------------------------------------/
-/ Functions and Buffer Configurations
+/ Function Configurations
 /---------------------------------------------------------------------------*/
 
-#define	_FS_TINY		1	/* 0:Normal or 1:Tiny */
-/* When _FS_TINY is set to 1, it reduces memory consumption _MAX_SS bytes each
-/  file object. For file data transfer, FatFs uses the common sector buffer in
-/  the file system object (FATFS) instead of private sector buffer eliminated
-/  from the file object (FIL). */
-
+#define FF_FS_READONLY	0
+/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
+/  Read-only configuration removes writing API functions, f_write(), f_sync(),
+/  f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
+/  and optional writing functions as well. */
 
-#define _FS_READONLY	0	/* 0:Read/Write or 1:Read only */
-/* Setting _FS_READONLY to 1 defines read only configuration. This removes
-/  writing functions, f_write(), f_sync(), f_unlink(), f_mkdir(), f_chmod(),
-/  f_rename(), f_truncate() and useless f_getfree(). */
 
-
-#define _FS_MINIMIZE	2	/* 0 to 3 */
-/* The _FS_MINIMIZE option defines minimization level to remove API functions.
+#define FF_FS_MINIMIZE	1
+/* This option defines minimization level to remove some basic API functions.
 /
-/   0: All basic functions are enabled.
-/   1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_chmod(), f_utime(),
-/      f_truncate() and f_rename() function are removed.
+/   0: Basic functions are fully enabled.
+/   1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
+/      are removed.
 /   2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
 /   3: f_lseek() function is removed in addition to 2. */
 
 
-#define	_USE_STRFUNC	0	/* 0:Disable or 1-2:Enable */
-/* To enable string functions, set _USE_STRFUNC to 1 or 2. */
+#define FF_USE_FIND		0
+/* This option switches filtered directory read functions, f_findfirst() and
+/  f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
+
+
+#define FF_USE_MKFS		0
+/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
+
+
+#define FF_USE_FASTSEEK	0
+/* This option switches fast seek function. (0:Disable or 1:Enable) */
 
 
-#define	_USE_MKFS		0	/* 0:Disable or 1:Enable */
-/* To enable f_mkfs() function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
+#define FF_USE_EXPAND	0
+/* This option switches f_expand function. (0:Disable or 1:Enable) */
 
 
-#define	_USE_FASTSEEK	0	/* 0:Disable or 1:Enable */
-/* To enable fast seek feature, set _USE_FASTSEEK to 1. */
+#define FF_USE_CHMOD	0
+/* This option switches attribute manipulation functions, f_chmod() and f_utime().
+/  (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
 
 
-#define _USE_LABEL		0	/* 0:Disable or 1:Enable */
-/* To enable volume label functions, set _USE_LAVEL to 1 */
+#define FF_USE_LABEL	0
+/* This option switches volume label functions, f_getlabel() and f_setlabel().
+/  (0:Disable or 1:Enable) */
 
 
-#define	_USE_FORWARD	0	/* 0:Disable or 1:Enable */
-/* To enable f_forward() function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
+#define FF_USE_FORWARD	0
+/* This option switches f_forward() function. (0:Disable or 1:Enable) */
+
+
+#define FF_USE_STRFUNC	0
+#define FF_PRINT_LLI	0
+#define FF_PRINT_FLOAT	0
+#define FF_STRF_ENCODE	0
+/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and
+/  f_printf().
+/
+/   0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect.
+/   1: Enable without LF-CRLF conversion.
+/   2: Enable with LF-CRLF conversion.
+/
+/  FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2
+   makes f_printf() support floating point argument. These features want C99 or later.
+/  When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character
+/  encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE
+/  to be read/written via those functions.
+/
+/   0: ANSI/OEM in current CP
+/   1: Unicode in UTF-16LE
+/   2: Unicode in UTF-16BE
+/   3: Unicode in UTF-8
+*/
 
 
 /*---------------------------------------------------------------------------/
 / Locale and Namespace Configurations
 /---------------------------------------------------------------------------*/
 
-#define _CODE_PAGE	932
-/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
-/  Incorrect setting of the code page can cause a file open failure.
+#define FF_CODE_PAGE	437
+/* This option specifies the OEM code page to be used on the target system.
+/  Incorrect code page setting can cause a file open failure.
 /
-/   932  - Japanese Shift-JIS (DBCS, OEM, Windows)
-/   936  - Simplified Chinese GBK (DBCS, OEM, Windows)
-/   949  - Korean (DBCS, OEM, Windows)
-/   950  - Traditional Chinese Big5 (DBCS, OEM, Windows)
-/   1250 - Central Europe (Windows)
-/   1251 - Cyrillic (Windows)
-/   1252 - Latin 1 (Windows)
-/   1253 - Greek (Windows)
-/   1254 - Turkish (Windows)
-/   1255 - Hebrew (Windows)
-/   1256 - Arabic (Windows)
-/   1257 - Baltic (Windows)
-/   1258 - Vietnam (OEM, Windows)
-/   437  - U.S. (OEM)
-/   720  - Arabic (OEM)
-/   737  - Greek (OEM)
-/   775  - Baltic (OEM)
-/   850  - Multilingual Latin 1 (OEM)
-/   858  - Multilingual Latin 1 + Euro (OEM)
-/   852  - Latin 2 (OEM)
-/   855  - Cyrillic (OEM)
-/   866  - Russian (OEM)
-/   857  - Turkish (OEM)
-/   862  - Hebrew (OEM)
-/   874  - Thai (OEM, Windows)
-/   1    - ASCII (Valid for only non-LFN cfg.) */
-
-
-#define	_USE_LFN	0		/* 0 to 3 */
-#define	_MAX_LFN	255		/* Maximum LFN length to handle (12 to 255) */
-/* The _USE_LFN option switches the LFN feature.
+/   437 - U.S.
+/   720 - Arabic
+/   737 - Greek
+/   771 - KBL
+/   775 - Baltic
+/   850 - Latin 1
+/   852 - Latin 2
+/   855 - Cyrillic
+/   857 - Turkish
+/   860 - Portuguese
+/   861 - Icelandic
+/   862 - Hebrew
+/   863 - Canadian French
+/   864 - Arabic
+/   865 - Nordic
+/   866 - Russian
+/   869 - Greek 2
+/   932 - Japanese (DBCS)
+/   936 - Simplified Chinese (DBCS)
+/   949 - Korean (DBCS)
+/   950 - Traditional Chinese (DBCS)
+/     0 - Include all code pages above and configured by f_setcp()
+*/
+
+
+#define FF_USE_LFN		0
+#define FF_MAX_LFN		255
+/* The FF_USE_LFN switches the support for LFN (long file name).
 /
-/   0: Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect.
-/   1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
+/   0: Disable LFN. FF_MAX_LFN has no effect.
+/   1: Enable LFN with static  working buffer on the BSS. Always NOT thread-safe.
 /   2: Enable LFN with dynamic working buffer on the STACK.
 /   3: Enable LFN with dynamic working buffer on the HEAP.
 /
-/  When enable LFN feature, Unicode handling functions ff_convert() and ff_wtoupper()
-/  function must be added to the project.
-/  The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. When use stack for the
-/  working buffer, take care on stack overflow. When use heap memory for the working
-/  buffer, memory management functions, ff_memalloc() and ff_memfree(), must be added
-/  to the project. */
-
-
-#define	_LFN_UNICODE	0	/* 0:ANSI/OEM or 1:Unicode */
-/* To switch the character encoding on the FatFs API (TCHAR) to Unicode, enable LFN
-/  feature and set _LFN_UNICODE to 1. This option affects behavior of string I/O
-/  functions. */
+/  To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
+/  requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
+/  additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
+/  The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
+/  be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
+/  specification.
+/  When use stack for the working buffer, take care on stack overflow. When use heap
+/  memory for the working buffer, memory management functions, ff_memalloc() and
+/  ff_memfree() exemplified in ffsystem.c, need to be added to the project. */
+
+
+#define FF_LFN_UNICODE	2
+/* This option switches the character encoding on the API when LFN is enabled.
+/
+/   0: ANSI/OEM in current CP (TCHAR = char)
+/   1: Unicode in UTF-16 (TCHAR = WCHAR)
+/   2: Unicode in UTF-8 (TCHAR = char)
+/   3: Unicode in UTF-32 (TCHAR = DWORD)
+/
+/  Also behavior of string I/O functions will be affected by this option.
+/  When LFN is not enabled, this option has no effect. */
 
 
-#define _STRF_ENCODE	3	/* 0:ANSI/OEM, 1:UTF-16LE, 2:UTF-16BE, 3:UTF-8 */
-/* When Unicode API is enabled by _LFN_UNICODE option, this option selects the character
-/  encoding on the file to be read/written via string I/O functions, f_gets(), f_putc(),
-/  f_puts and f_printf(). This option has no effect when Unicode API is not enabled. */
+#define FF_LFN_BUF		255
+#define FF_SFN_BUF		12
+/* This set of options defines size of file name members in the FILINFO structure
+/  which is used to read out directory items. These values should be suffcient for
+/  the file names to read. The maximum possible length of the read file name depends
+/  on character encoding. When LFN is not enabled, these options have no effect. */
 
 
-#define _FS_RPATH		0	/* 0 to 2 */
-/* The _FS_RPATH option configures relative path feature.
+#define FF_FS_RPATH		0
+/* This option configures support for relative path.
 /
-/   0: Disable relative path feature and remove related functions.
-/   1: Enable relative path. f_chdrive() and f_chdir() function are available.
+/   0: Disable relative path and remove related functions.
+/   1: Enable relative path. f_chdir() and f_chdrive() are available.
 /   2: f_getcwd() function is available in addition to 1.
-/
-/  Note that output of the f_readdir() fnction is affected by this option. */
+*/
 
 
 /*---------------------------------------------------------------------------/
 / Drive/Volume Configurations
 /---------------------------------------------------------------------------*/
 
-#define _VOLUMES	1
-/* Number of volumes (logical drives) to be used. */
+#define FF_VOLUMES		1
+/* Number of volumes (logical drives) to be used. (1-10) */
 
 
-#define _STR_VOLUME_ID	0	/* 0:Use only 0-9 for drive ID, 1:Use strings for drive ID */
-#define _VOLUME_STRS	"RAM","NAND","CF","SD1","SD2","USB1","USB2","USB3"
-/* When _STR_VOLUME_ID is set to 1, also pre-defined string can be used as drive number
-/  in the path name. _VOLUME_STRS defines the drive ID strings for each logical drives.
-/  Number of items must be equal to _VOLUMES. Valid characters for the drive ID strings
-/  are: 0-9 and A-Z. */
+#define FF_STR_VOLUME_ID	0
+#define FF_VOLUME_STRS		"RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
+/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
+/  When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
+/  number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
+/  logical drives. Number of items must not be less than FF_VOLUMES. Valid
+/  characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
+/  compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
+/  not defined, a user defined volume string table needs to be defined as:
+/
+/  const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
+*/
 
 
-#define	_MULTI_PARTITION	0	/* 0:Single partition, 1:Enable multiple partition */
-/* By default(0), each logical drive number is bound to the same physical drive number
-/  and only a FAT volume found on the physical drive is mounted. When it is set to 1,
-/  each logical drive number is bound to arbitrary drive/partition listed in VolToPart[].
-*/
+#define FF_MULTI_PARTITION	0
+/* This option switches support for multiple volumes on the physical drive.
+/  By default (0), each logical drive number is bound to the same physical drive
+/  number and only an FAT volume found on the physical drive will be mounted.
+/  When this function is enabled (1), each logical drive number can be bound to
+/  arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
+/  funciton will be available. */
 
 
-#define	_MIN_SS		512
-#define	_MAX_SS		512
-/* These options configure the sector size to be supported. (512, 1024, 2048 or 4096)
-/  Always set both 512 for most systems, all memory card and hard disk. But a larger
-/  value may be required for on-board flash memory and some type of optical media.
-/  When _MIN_SS != _MAX_SS, FatFs is configured to multiple sector size and
-/  GET_SECTOR_SIZE command must be implemented to the disk_ioctl() function. */
+#define FF_MIN_SS		512
+#define FF_MAX_SS		512
+/* This set of options configures the range of sector size to be supported. (512,
+/  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
+/  harddisk, but a larger value may be required for on-board flash memory and some
+/  type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
+/  for variable sector size mode and disk_ioctl() function needs to implement
+/  GET_SECTOR_SIZE command. */
 
 
-#define	_USE_ERASE	0	/* 0:Disable or 1:Enable */
-/* To enable sector erase feature, set _USE_ERASE to 1. Also CTRL_ERASE_SECTOR command
-/  should be added to the disk_ioctl() function. */
+#define FF_LBA64		0
+/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
+/  To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */
 
 
-#define _FS_NOFSINFO	0	/* 0 to 3 */
-/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
-/  option and f_getfree() function at first time after volume mount will force
-/  a full FAT scan. Bit 1 controls the last allocated cluster number as bit 0.
-/
-/  bit0=0: Use free cluster count in the FSINFO if available.
-/  bit0=1: Do not trust free cluster count in the FSINFO.
-/  bit1=0: Use last allocated cluster number in the FSINFO if available.
-/  bit1=1: Do not trust last allocated cluster number in the FSINFO.
-*/
+#define FF_MIN_GPT		0x10000000
+/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and
+/  f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */
+
+
+#define FF_USE_TRIM		0
+/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
+/  To enable Trim function, also CTRL_TRIM command should be implemented to the
+/  disk_ioctl() function. */
 
 
 
@@ -182,41 +224,78 @@
 / System Configurations
 /---------------------------------------------------------------------------*/
 
-#define _WORD_ACCESS	1	/* 0 or 1 */
-/* The _WORD_ACCESS option is an only platform dependent option. It defines
-/  which access method is used to the word data on the FAT volume.
-/
-/   0: Byte-by-byte access. Always compatible with all platforms.
-/   1: Word access. Do not choose this unless under both the following conditions.
-/
-/  * Address misaligned memory access is always allowed for all instructions.
-/  * Byte order on the memory is little-endian.
-/
-/  If it is the case, _WORD_ACCESS can also be set to 1 to improve performance
-/  and reduce code size.
-*/
+#define FF_FS_TINY		1
+/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
+/  At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
+/  Instead of private sector buffer eliminated from the file object, common sector
+/  buffer in the filesystem object (FATFS) is used for the file data transfer. */
+
 
+#define FF_FS_EXFAT		0
+/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
+/  To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
+/  Note that enabling exFAT discards ANSI C (C89) compatibility. */
 
-#define	_FS_LOCK	0	/* 0:Disable or >=1:Enable */
-/* To enable file lock control feature, set _FS_LOCK to 1 or greater.
-/  The value defines how many files/sub-directories can be opened simultaneously.
-/  This feature consumes _FS_LOCK * 12 bytes of bss area. */
 
+#define FF_FS_NORTC		1
+#define FF_NORTC_MON	1
+#define FF_NORTC_MDAY	1
+#define FF_NORTC_YEAR	2020
+/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
+/  any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
+/  the timestamp function. Every object modified by FatFs will have a fixed timestamp
+/  defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
+/  To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
+/  added to the project to read current time form real-time clock. FF_NORTC_MON,
+/  FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
+/  These options have no effect in read-only configuration (FF_FS_READONLY = 1). */
 
-#define _FS_REENTRANT	0		/* 0:Disable or 1:Enable */
-#define _FS_TIMEOUT		1000	/* Timeout period in unit of time ticks */
-#define	_SYNC_t			HANDLE	/* O/S dependent sync object type. e.g. HANDLE, OS_EVENT*, ID and etc.. */
-/*#include <windows.h>*/
 
-/* A header file that defines sync object types on the O/S, such as windows.h,
-/  ucos_ii.h and semphr.h, should be included here when enable this option.
-/  The _FS_REENTRANT option switches the re-entrancy (thread safe) of the FatFs module.
+#define FF_FS_NOFSINFO	0
+/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
+/  option, and f_getfree() function at first time after volume mount will force
+/  a full FAT scan. Bit 1 controls the use of last allocated cluster number.
 /
-/   0: Disable re-entrancy. _FS_TIMEOUT and _SYNC_t have no effect.
+/  bit0=0: Use free cluster count in the FSINFO if available.
+/  bit0=1: Do not trust free cluster count in the FSINFO.
+/  bit1=0: Use last allocated cluster number in the FSINFO if available.
+/  bit1=1: Do not trust last allocated cluster number in the FSINFO.
+*/
+
+
+#define FF_FS_LOCK		0
+/* The option FF_FS_LOCK switches file lock function to control duplicated file open
+/  and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
+/  is 1.
+/
+/  0:  Disable file lock function. To avoid volume corruption, application program
+/      should avoid illegal open, remove and rename to the open objects.
+/  >0: Enable file lock function. The value defines how many files/sub-directories
+/      can be opened simultaneously under file lock control. Note that the file
+/      lock control is independent of re-entrancy. */
+
+
+/* #include <somertos.h>	// O/S definitions */
+#define FF_FS_REENTRANT	0
+#define FF_FS_TIMEOUT	1000
+#define FF_SYNC_t		HANDLE
+/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
+/  module itself. Note that regardless of this option, file access to different
+/  volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
+/  and f_fdisk() function, are always not re-entrant. Only file/directory access
+/  to the same volume is under control of this function.
+/
+/   0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
 /   1: Enable re-entrancy. Also user provided synchronization handlers,
 /      ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
-/      function must be added to the project.
-*/
+/      function, must be added to the project. Samples are available in
+/      option/syscall.c.
+/
+/  The FF_FS_TIMEOUT defines timeout period in unit of time tick.
+/  The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
+/  SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
+/  included somewhere in the scope of ff.h. */
+
 
 
-#endif /* _FFCONFIG */
+/*--- End of configuration options ---*/

+ 62 - 42
soft/main.c

@@ -7,13 +7,14 @@
 #include <avr/interrupt.h>
 #include <avr/sleep.h>
 #include <util/delay.h>
+#include <avr/pgmspace.h>
 #include <string.h>
 #include "main.h"
 #include "ff.h"
 #include "diskio.h"
 #include "uart0.h"
 #include "uart1.h"
-#include "xitoa.h"
+#include "xprintf.h"
 #include "stime.h"
 #include "ds18b20.h"
 #include "I2C.h"
@@ -21,7 +22,7 @@
 #include "HD44780-I2C.h"
 
 
-FUSES = {0xFF, 0x11, 0xFE};		/* ATmega644PA fuses: Low, High, Extended.
+/*FUSES = {0xFF, 0x11, 0xFE};*/		/* ATmega644PA fuses: Low, High, Extended.
 This is the fuse settings for this project. The fuse bits will be included
 in the output hex file with program code. However some old flash programmers
 cannot load the fuse bits from hex file. If it is the case, remove this line
@@ -31,6 +32,7 @@ volatile struct system_s System;
 FATFS Fatfs;				/* File system object for each logical drive */
 FIL File1;					/* File object */
 char Line[100];				/* Line buffer */
+time_t utc;					/* current time */
 
 /*---------------------------------------------------------*/
 /* 100Hz timer interrupt generated by OC1A                 */
@@ -157,9 +159,8 @@ void beep (UINT len, BYTE cnt)
 }
 
 
-
 /* Compare sentence header string */
-BYTE gp_comp (const char *str1, const prog_char *str2)
+BYTE gp_comp (const char *str1, __flash const char *str2)
 {
 	char c;
 
@@ -169,8 +170,6 @@ BYTE gp_comp (const char *str1, const prog_char *str2)
 	return c;
 }
 
-
-
 /* Get a column item */
 static
 BYTE* gp_col (			/* Returns pointer to the item (returns a NULL when not found) */
@@ -273,7 +272,7 @@ void ioinit (void)
 
 	/* ADC */
 	ADMUX = 0;
-	ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0);
+	ADCSRA = _BV(ADEN)|_BV(ADATE)|_BV(ADIE)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0);
 	
 	/* uart1 (debug) */	
 	uart1_init();
@@ -284,6 +283,8 @@ void ioinit (void)
 	LCD_Clear();	
 
 	sei();
+	
+	ADCSRA |= _BV(ADSC);
 }
 
 
@@ -294,33 +295,42 @@ void ioinit (void)
 
 int main (void)
 {
-	UINT bw, len, err;
+	UINT bw, len;
 	struct tm *ct;
-	time_t utc;
+	time_t tmp_utc;
 	FRESULT res;
+	unsigned char prev_status;
 
 	ioinit();
 	xdev_out(uart1_put);
 
-	err = 0;
 	for (;;) {
-		beep(250, err);	/* Error beep */
-		err = 0;
+		utc = 0;
+		prev_status = System.status;
+		System.status = STATUS_NO_POWER;
+
+		beep(250, prev_status);	/* Error beep */
 
 		/* Wait for supply voltage stabled */
 		while (FLAGS & F_LVD) {};
 		_delay_ms(500);
 		if (FLAGS & F_LVD)
 			continue;
-		if (disk_status(0) & STA_NODISK)
+
+		/* report error here ( prev_status) */
+
+		if (disk_status(0) & STA_NODISK) {
+			System.status = STATUS_NO_DISK;
 			continue;
+		}
 
 		res = f_mount(&Fatfs, "", 1);
 		if (res != FR_OK) {
 			xprintf(PSTR("FS error %u\r\n"), res);
-			err = 2;
+			System.status = STATUS_DISK_ERROR;
 			continue;
 		}
+		System.status = STATUS_NO_GPS;
 		xputs(PSTR("FS Ok\r\n"));
 		beep(50, 1);				/* 1 beep */
 
@@ -331,40 +341,46 @@ int main (void)
 		uart0_init();	/* Enable UART */
 //		xfprintf(uart0_put, PSTR("$PSRF106,21*0F\r\n"));	/* Send initialization command (depends on the receiver) */
 
-		utc = 0;
 		for (;;) { /* main loop */
 			gettemp();
+			if (!(FLAGS & F_GPSOK))
+				xputs_P(PSTR("Waiting for GPS\r\n"));
 			len = get_line(Line, sizeof Line);	/* Receive a line from GPS receiver */
-			if (!len) continue;
-			if (FLAGS & F_LVD) break; /* brownout */
-
-			if (!utc) {
-				utc = gp_rmctime(Line);	/* Get time in UTC from a valid RMC sentence */
-				if (utc) {
-					utc += LOCALDIFF * 3600L;	/* Local time */
-					ct = gmtime(&utc);
-					xsprintf(Line, PSTR("%02u%02u%02u.LOG"), ct->tm_year % 100, ct->tm_mon + 1, ct->tm_mday);
-					xprintf(PSTR("Open %s\r\n"), Line);
-					if (f_open(&File1, Line, FA_WRITE | FA_OPEN_ALWAYS)		/* Open log file */
-						|| f_lseek(&File1, f_size(&File1)) 					/* Append mode */
-						|| f_write(&File1, "\r\n", 2, &bw))					/* Put a blank line as start marker */
-					{
-						utc = 0;
-						err = 2;
-						break;	/* Failed to start logging */
-					}
-					beep(50, 2);		/* Two beeps. Start logging. */
+			if (!len){
+				if (FLAGS & F_LVD)
+					break; /* brownout */
+				continue;
+			}
+
+			tmp_utc = gp_rmctime(Line);
+			if (tmp_utc)
+				utc = tmp_utc + LOCALDIFF * 3600L;	/* Local time */
+
+			if (utc && !(FLAGS & F_FILEOPEN)) {
+				ct = gmtime(&utc);
+				xsprintf(Line, PSTR("%02u%02u%02u.LOG"), ct->tm_year % 100, ct->tm_mon + 1, ct->tm_mday);
+				xprintf(PSTR("Open %s\r\n"), Line);
+				if (f_open(&File1, Line, FA_WRITE | FA_OPEN_ALWAYS)		/* Open log file */
+					|| f_lseek(&File1, f_size(&File1)) 					/* Append mode */
+					|| f_write(&File1, "\r\n", 2, &bw))					/* Put a blank line as start marker */
+				{
+					System.status = STATUS_FILE_OPEN_ERROR;
+					break;	/* Failed to start logging */
 				}
-			} else {
-				gp_rmctime(Line);
+				FLAGS |= F_FILEOPEN;
+				System.status = STATUS_OK;
+				beep(50, 2);	
+				continue;	/* Two beeps. Start logging. */
+			}
+			if (FLAGS & F_FILEOPEN) {
 				f_write(&File1, Line, len, &bw);
 				if (bw != len) {
-					err = 3;
+					System.status = STATUS_FILE_WRITE_ERROR;
 					break;
 				}
 				if (FLAGS & F_SYNC) {
 					if (f_sync(&File1)) {
-						err = 2;
+						System.status = STATUS_FILE_SYNC_ERROR;
 						break;
 					}
 					FLAGS &= ~F_SYNC;
@@ -378,10 +394,14 @@ int main (void)
 		FLAGS &= ~F_POW;
 
 		/* Close file */
-		if (utc && f_close(&File1))
-			err = 2;
-		disk_ioctl(0, CTRL_POWER_OFF, 0);
-		if (!err)
+		if (FLAGS & F_FILEOPEN) {
+			if (f_close(&File1))
+				System.status = STATUS_FILE_CLOSE_ERROR;
+			xputs_P(PSTR("File closed\r\n"));
+		}
+		FLAGS &= ~F_FILEOPEN;
+		disk_ioctl(0, CTRL_POWER, 0);
+		if (System.status != STATUS_FILE_CLOSE_ERROR)
 			beep(500, 1);	/* Long beep on file close succeeded */
 	}
 

+ 14 - 0
soft/main.h

@@ -57,12 +57,25 @@
 #define	F_POW	0x80		/* Power */
 #define	F_GPSOK	0x08		/* GPS data valid */
 #define	F_SYNC	0x04		/* Sync request */
+#define F_FILEOPEN	0x02	/* File is open, logging in progress */
 #define	F_LVD	0x01		/* Low battery detect */
 
+/* System.global_error vals */
 #define ERROR_NO	0
 #define ERROR_I2C	1
 #define ERROR_I2C_TIMEOUT	2
 
+/* System.status vals */
+#define STATUS_NO_POWER	0
+#define STATUS_NO_DISK	1
+#define STATUS_NO_GPS	2
+#define STATUS_OK		3
+#define STATUS_DISK_ERROR	4
+#define STATUS_FILE_WRITE_ERROR	5
+#define STATUS_FILE_SYNC_ERROR	6
+#define STATUS_FILE_CLOSE_ERROR	7
+#define STATUS_FILE_OPEN_ERROR	8
+
 #define ms(x) (x/10)
 
 struct timers {
@@ -74,6 +87,7 @@ struct timers {
 struct system_s {
 	struct timers timers;
 	unsigned int global_error;
+	unsigned char status;
 };
 
 extern volatile struct system_s System;

+ 3 - 4
soft/mmc.c

@@ -3,6 +3,7 @@
 /*-----------------------------------------------------------------------*/
 
 #include <avr/io.h>
+#include "ff.h"
 #include "diskio.h"
 #include "main.h"
 
@@ -83,6 +84,7 @@ void power_off (void)
 static
 void power_on (void)	/* Apply power sequence */
 {
+	SD_PWROFF_DDR |= SD_PWROFF;
 	for (Timer1 = 30; Timer1; ) {};	/* 300ms */
 	POWER_ON();						/* Power on */
 	for (Timer1 = 3; Timer1; ) {};	/* 30ms */
@@ -90,9 +92,6 @@ void power_on (void)	/* Apply power sequence */
 	SD_CS_PORT |= SD_CS;
 	SD_CS_DDR |= SD_CS;
 
-	SD_PWROFF_PORT |= SD_PWROFF;
-	SD_PWROFF_DDR |= SD_PWROFF;
-
 	PORTB |= _BV(PB5) | _BV(PB7);	/* Configure SCK/MOSI as output */
 	DDRB  |= _BV(PB5) | _BV(PB7);
 
@@ -469,7 +468,7 @@ DRESULT disk_ioctl (
 	if (drv) return RES_PARERR;
 	buff = 0;
 
-	if (ctrl == CTRL_POWER_OFF) {
+	if (ctrl == CTRL_POWER) {
 		power_off();
 		return RES_OK;
 	}

+ 15 - 6
soft/uart1.c

@@ -8,8 +8,8 @@
 
 #define	UART1_BAUD		9600
 #define	USE_TXINT		1
-#define	SZ_FIFO			512
-
+#define	SZ_FIFO			128
+#define RECEIVE			0
 
 #if SZ_FIFO >= 256
 typedef uint16_t	idx_t;
@@ -22,8 +22,11 @@ typedef struct {
 	idx_t wi, ri, ct;
 	uint8_t buff[SZ_FIFO];
 } FIFO;
+#if RECEIVE
 static
 volatile FIFO RxFifo;
+#endif
+
 #if USE_TXINT
 static
 volatile FIFO TxFifo;
@@ -40,13 +43,19 @@ void uart1_init (void)
 	PORTD |= _BV(PD3); DDRD |= _BV(PD3);	/* Set TXD as output */
 	DDRD &= ~_BV(PD2); PORTD &= ~_BV(PD2); 	/* Set RXD as input */
 
+#if RECEIVE
 	RxFifo.ct = 0; RxFifo.ri = 0; RxFifo.wi = 0;
+#endif
 #if USE_TXINT
 	TxFifo.ct = 0; TxFifo.ri = 0; TxFifo.wi = 0;
 #endif
 
 	UBRR1L = F_CPU / UART1_BAUD / 16 - 1;
+#if RECEIVE
 	UCSR1B = _BV(RXEN1) | _BV(RXCIE1) | _BV(TXEN1);
+#else
+	UCSR1B = _BV(TXEN1);
+#endif
 }
 
 
@@ -59,13 +68,12 @@ void uart1_deinit (void)
 
 
 /* Get a received character */
-
+#if RECEIVE
 uint8_t uart1_test (void)
 {
 	return RxFifo.ct;
 }
 
-
 uint8_t uart1_get (void)
 {
 	uint8_t d;
@@ -84,6 +92,7 @@ uint8_t uart1_get (void)
 
 	return d;
 }
+#endif
 
 
 /* Put a character to transmit */
@@ -109,7 +118,7 @@ void uart1_put (uint8_t d)
 #endif
 }
 
-
+#if RECEIVE
 /* USART1 RXC interrupt */
 ISR(USART1_RX_vect)
 {
@@ -126,7 +135,7 @@ ISR(USART1_RX_vect)
 		RxFifo.wi = (i + 1) % sizeof RxFifo.buff;
 	}
 }
-
+#endif
 
 #if USE_TXINT
 ISR(USART1_UDRE_vect)

+ 0 - 500
soft/xitoa.S

@@ -1,500 +0,0 @@
-;---------------------------------------------------------------------------;
-; Extended itoa, puts, printf and atoi                     (C)ChaN, 2011
-;---------------------------------------------------------------------------;
-
-				// Base size is 152 bytes
-#define	CR_CRLF		0	// Convert \n to \r\n (+10 bytes)
-#define USE_XPRINTF	1	// Enable xprintf function (+194 bytes)
-#define USE_XSPRINTF	1	// Add xsprintf function (+78 bytes)
-#define USE_XFPRINTF	1	// Add xfprintf function (+54 bytes)
-#define USE_XATOI	1	// Enable xatoi function (+182 bytes)
-
-
-#if FLASHEND > 0x1FFFF
-#error xitoa module does not support 256K devices
-#endif
-
-.nolist
-#include <avr/io.h>	// Include device specific definitions.
-.list
-
-#ifdef SPM_PAGESIZE	// Recent devices have "lpm Rd,Z+" and "movw".
-.macro	_LPMI	reg
-	lpm	\reg, Z+
-.endm
-.macro	_MOVW	dh,dl, sh,sl
-	movw	\dl, \sl
-.endm
-#else			// Earlier devices do not have "lpm Rd,Z+" nor "movw".
-.macro	_LPMI	reg
-	lpm
-	mov	\reg, r0
-	adiw	ZL, 1
-.endm
-.macro	_MOVW	dh,dl, sh,sl
-	mov	\dl, \sl
-	mov	\dh, \sh
-.endm
-#endif
-
-
-
-;---------------------------------------------------------------------------
-; Stub function to forward to user output function
-;
-;Prototype: void xputc (char chr	// a character to be output
-;			);
-;Size: 10/10 words
-
-.section .bss
-.global xfunc_out	; xfunc_out must be initialized before using this module.
-xfunc_out:	.ds.w	1
-.section .text
-
-
-.func xputc
-.global xputc
-xputc:
-#if CR_CRLF
-	cpi	r24, 10		;LF --> CRLF
-	brne	1f		;
-	ldi	r24, 13		;
-	rcall	1f		;
-	ldi	r24, 10		;/
-1:
-#endif
-	push	ZH
-	push	ZL
-	lds	ZL, xfunc_out+0	;Pointer to the registered output function.
-	lds	ZH, xfunc_out+1	;/
-	sbiw	ZL, 0		;Skip if null
-	breq	2f		;/
-	icall
-2:	pop	ZL
-	pop	ZH
-	ret
-.endfunc
-
-
-
-;---------------------------------------------------------------------------
-; Direct ROM string output
-;
-;Prototype: void xputs (const prog_char *str // rom string to be output
-;			);
-
-.func xputs
-.global xputs
-xputs:
-	_MOVW	ZH,ZL, r25,r24	; Z = pointer to rom string
-1:	_LPMI	r24
-	cpi	r24, 0
-	breq	2f
-	rcall	xputc
-	rjmp	1b
-2:	ret
-.endfunc
-
-
-;---------------------------------------------------------------------------
-; Extended direct numeral string output (32bit version)
-;
-;Prototype: void xitoa (long value,	// value to be output
-;                       char radix,	// radix
-;                       char width);	// minimum width
-;
-
-.func xitoa
-.global xitoa
-xitoa:
-				;r25:r22 = value, r20 = base, r18 = digits
-	clr	r31		;r31 = stack level
-	ldi	r30, ' '	;r30 = sign
-	ldi	r19, ' '	;r19 = filler
-	sbrs	r20, 7		;When base indicates signd format and the value
-	rjmp	0f		;is minus, add a '-'.
-	neg	r20		;
-	sbrs	r25, 7		;
-	rjmp	0f		;
-	ldi	r30, '-'	;
-	com	r22		;
-	com	r23		;
-	com	r24		;
-	com	r25		;
-	adc	r22, r1		;
-	adc	r23, r1		;
-	adc	r24, r1		;
-	adc	r25, r1		;/
-0:	sbrs	r18, 7		;When digits indicates zero filled,
-	rjmp	1f		;filler is '0'.
-	neg	r18		;
-	ldi	r19, '0'	;/
-				;----- string conversion loop
-1:	ldi	r21, 32		;r26 = r25:r22 % r20
-	clr	r26		;r25:r22 /= r20
-2:	lsl	r22		;
-	rol	r23		;
-	rol	r24		;
-	rol	r25		;
-	rol	r26		;
-	cp	r26, r20	;
-	brcs	3f		;
-	sub	r26, r20	;
-	inc	r22		;
-3:	dec	r21		;
-	brne	2b		;/
-	cpi	r26, 10		;r26 is a numeral digit '0'-'F'
-	brcs	4f		;
-	subi	r26, -7		;
-4:	subi	r26, -'0'	;/
-	push	r26		;Stack it
-	inc	r31		;/
-	cp	r22, r1		;Repeat until r25:r22 gets zero
-	cpc	r23, r1		;
-	cpc	r24, r1		;
-	cpc	r25, r1		;
-	brne	1b		;/
-
-	cpi	r30, '-'	;Minus sign if needed
-	brne	5f		;
-	push	r30		;
-	inc	r31		;/
-5:	cp	r31, r18	;Filler
-	brcc	6f		;
-	push	r19		;
-	inc	r31		;
-	rjmp	5b		;/
-
-6:	pop	r24		;Flush stacked digits and exit
-	rcall	xputc		;
-	dec	r31		;
-	brne	6b		;/
-
-	ret
-.endfunc
-
-
-
-;---------------------------------------------------------------------------;
-; Formatted string output (16/32bit version)
-;
-;Prototype:
-; void xprintf (const prog_char *format, ...);
-; void xsprintf(char*, const prog_char *format, ...);
-; void xfprintf(void(*func)(char), const prog_char *format, ...);
-;
-
-#if USE_XPRINTF
-
-.func xvprintf
-xvprintf:
-	ld	ZL, Y+		;Z = pointer to format string
-	ld	ZH, Y+		;/
-
-0:	_LPMI	r24		;Get a format char
-	cpi	r24, 0		;End of format string?
-	breq	90f		;/
-	cpi	r24, '%'	;Is format?
-	breq	20f		;/
-1:	rcall	xputc		;Put a normal character
-	rjmp	0b		;/
-90:	ret
-
-20:	ldi	r18, 0		;r18: digits
-	clt			;T: filler
-	_LPMI	r21		;Get flags
-	cpi	r21, '%'	;Is a %?
-	breq	1b		;/
-	cpi	r21, '0'	;Zero filled?
-	brne	23f		;
-	set			;/
-22:	_LPMI	r21		;Get width
-23:	cpi	r21, '9'+1	;
-	brcc	24f		;
-	subi	r21, '0'	;
-	brcs	90b		;
-	lsl	r18		;
-	mov	r0, r18		;
-	lsl	r18		;
-	lsl	r18		;
-	add	r18, r0		;
-	add	r18, r21	;
-	rjmp	22b		;/
-
-24:	brtc	25f		;get value (low word)
-	neg	r18		;
-25:	ld	r24, Y+		;
-	ld	r25, Y+		;/
-	cpi	r21, 'c'	;Is type character?
-	breq	1b		;/
-	cpi	r21, 's'	;Is type RAM string?
-	breq	50f		;/
-	cpi	r21, 'S'	;Is type ROM string?
-	breq	60f		;/
-	_MOVW	r23,r22,r25,r24	;r25:r22 = value
-	clr	r24		;
-	clr	r25		;
-	clt			;/
-	cpi	r21, 'l'	;Is long int?
-	brne	26f		;
-	ld	r24, Y+		;get value (high word)
-	ld	r25, Y+		;
-	set			;
-	_LPMI	r21		;/
-26:	cpi	r21, 'd'	;Is type signed decimal?
-	brne	27f		;/
-	ldi	r20, -10	;
-	brts	40f		;
-	sbrs	r23, 7		;
-	rjmp	40f		;
-	ldi	r24, -1		;
-	ldi	r25, -1		;
-	rjmp	40f		;/
-27:	cpi	r21, 'u'	;Is type unsigned decimal?
-	ldi	r20, 10		;
-	breq	40f		;/
-	cpi	r21, 'X'	;Is type hexdecimal?
-	ldi	r20, 16		;
-	breq	40f		;/
-	cpi	r21, 'b'	;Is type binary?
-	ldi	r20, 2		;
-	breq	40f		;/
-	ret			;abort
-40:	push	ZH		;Output the value
-	push	ZL		;
-	rcall	xitoa		;
-42:	pop	ZL		;
-	pop	ZH		;
-	rjmp	0b		;/
-
-50:	push	ZH		;Put a string on the RAM
-	push	ZL
-	_MOVW	ZH,ZL, r25,r24
-51:	ld	r24, Z+
-	cpi	r24, 0
-	breq	42b
-	rcall	xputc
-	rjmp	51b
-
-60:	push	ZH		;Put a string on the ROM
-	push	ZL
-	rcall	xputs
-	rjmp	42b
-.endfunc
-
-
-.func xprintf
-.global xprintf
-xprintf:
-	push	YH
-	push	YL
-	in	YL, _SFR_IO_ADDR(SPL)
-#ifdef SPH
-	in	YH, _SFR_IO_ADDR(SPH)
-#else
-	clr	YH
-#endif
-	adiw	YL, 5		;Y = pointer to arguments
-	rcall	xvprintf
-	pop	YL
-	pop	YH
-	ret
-.endfunc
-
-
-#if USE_XSPRINTF
-
-.func xsprintf
-putram:
-	_MOVW	ZH,ZL, r15,r14
-	st	Z+, r24
-	_MOVW	r15,r14, ZH,ZL
-	ret
-.global xsprintf
-xsprintf:
-	push	YH
-	push	YL
-	in	YL, _SFR_IO_ADDR(SPL)
-#ifdef SPH
-	in	YH, _SFR_IO_ADDR(SPH)
-#else
-	clr	YH
-#endif
-	adiw	YL, 5		;Y = pointer to arguments
-	lds	ZL, xfunc_out+0	;Save registered output function
-	lds	ZH, xfunc_out+1	;
-	push	ZL		;
-	push	ZH		;/
-	ldi	ZL, lo8(pm(putram));Set local output function
-	ldi	ZH, hi8(pm(putram));
-	sts	xfunc_out+0, ZL	;
-	sts	xfunc_out+1, ZH	;/
-	push	r15		;Initialize pointer to string buffer
-	push	r14		;
-	ld	r14, Y+		;
-	ld	r15, Y+		;/
-	rcall	xvprintf
-	_MOVW	ZH,ZL, r15,r14	;Terminate string
-	st	Z, r1		;
-	pop	r14		;
-	pop	r15		;/
-	pop	ZH		;Restore registered output function
-	pop	ZL		;
-	sts	xfunc_out+0, ZL	;
-	sts	xfunc_out+1, ZH	;/
-	pop	YL
-	pop	YH
-	ret
-.endfunc
-#endif
-
-
-#if USE_XFPRINTF
-.func xfprintf
-.global xfprintf
-xfprintf:
-	push	YH
-	push	YL
-	in	YL, _SFR_IO_ADDR(SPL)
-#ifdef SPH
-	in	YH, _SFR_IO_ADDR(SPH)
-#else
-	clr	YH
-#endif
-	adiw	YL, 5		;Y = pointer to arguments
-	lds	ZL, xfunc_out+0	;Save registered output function
-	lds	ZH, xfunc_out+1	;
-	push	ZL		;
-	push	ZH		;/
-	ld	ZL, Y+		;Set output function
-	ld	ZH, Y+		;
-	sts	xfunc_out+0, ZL	;
-	sts	xfunc_out+1, ZH	;/
-	rcall	xvprintf
-	pop	ZH		;Restore registered output function
-	pop	ZL		;
-	sts	xfunc_out+0, ZL	;
-	sts	xfunc_out+1, ZH	;/
-	pop	YL
-	pop	YH
-	ret
-.endfunc
-#endif
-
-#endif
-
-
-
-;---------------------------------------------------------------------------
-; Extended numeral string input
-;
-;Prototype:
-; char xatoi (           /* 1: Successful, 0: Failed */
-;      const char **str, /* pointer to pointer to source string */
-;      long *res         /* result */
-; );
-;
-
-
-#if USE_XATOI
-.func xatoi
-.global xatoi
-xatoi:
-	_MOVW	r1, r0, r23, r22
-	_MOVW	XH, XL, r25, r24
-	ld	ZL, X+
-	ld	ZH, X+
-	clr	r18		;r21:r18 = 0;
-	clr	r19		;
-	clr	r20		;
-	clr	r21		;/
-	clt			;T = 0;
-
-	ldi	r25, 10		;r25 = 10;
-	rjmp	41f		;/
-40:	adiw	ZL, 1		;Z++;
-41:	ld	r22, Z		;r22 = *Z;
-	cpi	r22, ' '	;if(r22 == ' ') continue
-	breq	40b		;/
-	brcs	70f		;if(r22 < ' ') error;
-	cpi	r22, '-'	;if(r22 == '-') {
-	brne	42f		; T = 1;
-	set			; continue;
-	rjmp	40b		;}
-42:	cpi	r22, '9'+1	;if(r22 > '9') error;
-	brcc	70f		;/
-	cpi	r22, '0'	;if(r22 < '0') error;
-	brcs	70f		;/
-	brne	51f		;if(r22 > '0') cv_start;
-	ldi	r25, 8		;r25 = 8;
-	adiw	ZL, 1		;r22 = *(++Z);
-	ld	r22, Z		;/
-	cpi	r22, ' '+1	;if(r22 <= ' ') exit;
-	brcs	80f		;/
-	cpi	r22, 'b'	;if(r22 == 'b') {
-	brne	43f		; r25 = 2;
-	ldi	r25, 2		; cv_start;
-	rjmp	50f		;}
-43:	cpi	r22, 'x'	;if(r22 != 'x') error;
-	brne	51f		;/
-	ldi	r25, 16		;r25 = 16;
-
-50:	adiw	ZL, 1		;Z++;
-	ld	r22, Z		;r22 = *Z;
-51:	cpi	r22, ' '+1	;if(r22 <= ' ') break;
-	brcs	80f		;/
-	cpi	r22, 'a'	;if(r22 >= 'a') r22 =- 0x20;
-	brcs	52f		;
-	subi	r22, 0x20	;/
-52:	subi	r22, '0'	;if((r22 -= '0') < 0) error;
-	brcs	70f		;/
-	cpi	r22, 10		;if(r22 >= 10) {
-	brcs	53f		; r22 -= 7;
-	subi	r22, 7		; if(r22 < 10) 
-	cpi	r22, 10		;
-	brcs	70f		;}
-53:	cp	r22, r25	;if(r22 >= r25) error;
-	brcc	70f		;/
-60:	ldi	r24, 33		;r21:r18 *= r25;
-	sub	r23, r23	;
-61:	brcc	62f		;
-	add	r23, r25	;
-62:	lsr	r23		;
-	ror	r21		;
-	ror	r20		;
-	ror	r19		;
-	ror	r18		;
-	dec	r24		;
-	brne	61b		;/
-	add	r18, r22	;r21:r18 += r22;
-	adc	r19, r24	;
-	adc	r20, r24	;
-	adc	r21, r24	;/
-	rjmp	50b		;repeat
-
-70:	ldi	r24, 0
-	rjmp	81f
-80:	ldi	r24, 1
-81:	brtc	82f
-	clr	r22
-	com	r18
-	com	r19
-	com	r20
-	com	r21
-	adc	r18, r22
-	adc	r19, r22
-	adc	r20, r22
-	adc	r21, r22
-82:	st	-X, ZH
-	st	-X, ZL
-	_MOVW	XH, XL, r1, r0
-	st	X+, r18
-	st	X+, r19
-	st	X+, r20
-	st	X+, r21
-	clr	r1
-	ret
-.endfunc
-#endif
-
-

+ 0 - 102
soft/xitoa.h

@@ -1,102 +0,0 @@
-/*---------------------------------------------------------------------------
-   Extended itoa, puts and printf                    (C)ChaN, 2011
-
------------------------------------------------------------------------------*/
-
-#ifndef XITOA
-#define XITOA
-
-#define __PROG_TYPES_COMPAT__
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-#include <inttypes.h>
-#include <avr/pgmspace.h>
-
-extern void (*xfunc_out)(uint8_t);
-#define xdev_out(func) xfunc_out = (void(*)(uint8_t))(func)
-
-/* This is a pointer to user defined output function. It must be initialized
-   before using this modle.
-*/
-
-void xputc(char chr);
-
-/* This is a stub function to forward outputs to user defined output function.
-   All outputs from this module are output via this function.
-*/
-
-
-/*-----------------------------------------------------------------------------*/
-void xputs(const prog_char *string);
-
-/*  The string placed in the ROM is forwarded to xputc() directly.
-*/
-
-
-/*-----------------------------------------------------------------------------*/
-void xitoa(long value, char radix, char width);
-
-/* Extended itoa().
-
-      value  radix  width   output
-        100     10      6   "   100"
-        100     10     -6   "000100"
-        100     10      0   "100"
- 4294967295     10      0   "4294967295"
- 4294967295    -10      0   "-1"
-     655360     16     -8   "000A0000"
-       1024     16      0   "400"
-       0x55      2     -8   "01010101"
-*/
-
-
-/*-----------------------------------------------------------------------------*/
-void xprintf(const prog_char *format, ...);	/* Send formatted string to the registered device */
-void xsprintf(char*, const prog_char *format, ...);	/* Put formatted string to the memory */
-void xfprintf(void(*func)(uint8_t), const prog_char *format, ...); /* Send formatted string to the specified device */
-
-/* Format string is placed in the ROM. The format flags is similar to printf().
-
-   %[flag][width][size]type
-
-   flag
-     A '0' means filled with '0' when output is shorter than width.
-     ' ' is used in default. This is effective only numeral type.
-   width
-     Minimum width in decimal number. This is effective only numeral type.
-     Default width is zero.
-   size
-     A 'l' means the argument is long(32bit). Default is short(16bit).
-     This is effective only numeral type.
-   type
-     'c' : Character, argument is the value
-     's' : String placed on the RAM, argument is the pointer
-     'S' : String placed on the ROM, argument is the pointer
-     'd' : Signed decimal, argument is the value
-     'u' : Unsigned decimal, argument is the value
-     'X' : Hexdecimal, argument is the value
-     'b' : Binary, argument is the value
-     '%' : '%'
-
-*/
-
-
-/*-----------------------------------------------------------------------------*/
-char xatoi(char **str, long *ret);
-
-/* Get value of the numeral string. 
-
-  str
-    Pointer to pointer to source string
-
-    "0b11001010" binary
-    "0377" octal
-    "0xff800" hexdecimal
-    "1250000" decimal
-    "-25000" decimal
-
-  ret
-    Pointer to return value
-*/
-
-#endif	/* XITOA */
-

+ 673 - 0
soft/xprintf.c

@@ -0,0 +1,673 @@
+/*------------------------------------------------------------------------/
+/  Universal String Handler for Console Input and Output
+/-------------------------------------------------------------------------/
+/
+/ Copyright (C) 2021, ChaN, all right reserved.
+/
+/ xprintf module is an open source software. Redistribution and use of
+/ xprintf module in source and binary forms, with or without modification,
+/ are permitted provided that the following condition is met:
+/
+/ 1. Redistributions of source code must retain the above copyright notice,
+/    this condition and the following disclaimer.
+/
+/ This software is provided by the copyright holder and contributors "AS IS"
+/ and any warranties related to this software are DISCLAIMED.
+/ The copyright owner or contributors be NOT LIABLE for any damages caused
+/ by use of this software.
+/
+/-------------------------------------------------------------------------*/
+
+#include "xprintf.h"
+#include <avr/pgmspace.h>
+
+#define SZB_OUTPUT	32
+
+
+#if XF_USE_OUTPUT
+#include <stdarg.h>
+void (*xfunc_output)(int);	/* Pointer to the default output device */
+static char *strptr;		/* Pointer to the output memory (used by xsprintf) */
+
+
+#if XF_USE_FP
+/*----------------------------------------------*/
+/* Floating point output                        */
+/*----------------------------------------------*/
+#include <math.h>
+
+
+static int ilog10 (double n)	/* Calculate log10(n) in integer output */
+{
+	int rv = 0;
+
+	while (n >= 10) {	/* Decimate digit in right shift */
+		if (n >= 100000) {
+			n /= 100000; rv += 5;
+		} else {
+			n /= 10; rv++;
+		}
+	}
+	while (n < 1) {		/* Decimate digit in left shift */
+		if (n < 0.00001) {
+			n *= 100000; rv -= 5;
+		} else {
+			n *= 10; rv--;
+		}
+	}
+	return rv;
+}
+
+
+static double i10x (int n)	/* Calculate 10^n */
+{
+	double rv = 1;
+
+	while (n > 0) {		/* Left shift */
+		if (n >= 5) {
+			rv *= 100000; n -= 5;
+		} else {
+			rv *= 10; n--;
+		}
+	}
+	while (n < 0) {		/* Right shift */
+		if (n <= -5) {
+			rv /= 100000; n += 5;
+		} else {
+			rv /= 10; n++;
+		}
+	}
+	return rv;
+}
+
+
+static void ftoa (
+	char* buf,	/* Buffer to output the generated string */
+	double val,	/* Real number to output */
+	int prec,	/* Number of fractinal digits */
+	char fmt	/* Notation */
+)
+{
+	int d;
+	int e = 0, m = 0;
+	char sign = 0;
+	double w;
+	const char *er = 0;
+
+
+	if (isnan(val)) {			/* Not a number? */
+		er = "NaN";
+	} else {
+		if (prec < 0) prec = 6;	/* Default precision (6 fractional digits) */
+		if (val < 0) {			/* Nagative value? */
+			val = -val; sign = '-';
+		} else {
+			sign = '+';
+		}
+		if (isinf(val)) {		/* Infinite? */
+			er = "INF";
+		} else {
+			if (fmt == 'f') {	/* Decimal notation? */
+				val += i10x(-prec) / 2;	/* Round (nearest) */
+				m = ilog10(val);
+				if (m < 0) m = 0;
+				if (m + prec + 3 >= SZB_OUTPUT) er = "OV";	/* Buffer overflow? */
+			} else {			/* E notation */
+				if (val != 0) {		/* Not a true zero? */
+					val += i10x(ilog10(val) - prec) / 2;	/* Round (nearest) */
+					e = ilog10(val);
+					if (e > 99 || prec + 6 >= SZB_OUTPUT) {	/* Buffer overflow or E > +99? */
+						er = "OV";
+					} else {
+						if (e < -99) e = -99;
+						val /= i10x(e);	/* Normalize */
+					}
+				}
+			}
+		}
+		if (!er) {	/* Not error condition */
+			if (sign == '-') *buf++ = sign;	/* Add a - if negative value */
+			do {				/* Put decimal number */
+				w = i10x(m);				/* Snip the highest digit d */
+				d = val / w; val -= d * w;
+				if (m == -1) *buf++ = XF_DPC;	/* Insert a decimal separarot if get into fractional part */
+				*buf++ = '0' + d;			/* Put the digit */
+			} while (--m >= -prec);			/* Output all digits specified by prec */
+			if (fmt != 'f') {	/* Put exponent if needed */
+				*buf++ = fmt;
+				if (e < 0) {
+					e = -e; *buf++ = '-';
+				} else {
+					*buf++ = '+';
+				}
+				*buf++ = '0' + e / 10;
+				*buf++ = '0' + e % 10;
+			}
+		}
+	}
+	if (er) {	/* Error condition? */
+		if (sign) *buf++ = sign;		/* Add sign if needed */
+		do *buf++ = *er++; while (*er);	/* Put error symbol */
+	}
+	*buf = 0;	/* Term */
+}
+#endif	/* XF_USE_FLOAT */
+
+
+/*----------------------------------------------*/
+/* Put a character                              */
+/*----------------------------------------------*/
+
+void xputc (
+	int chr				/* Character to be output */
+)
+{
+	xfputc(xfunc_output, chr);	/* Output it to the default output device */
+}
+
+
+void xfputc (			/* Put a character to the specified device */
+	void(*func)(int),	/* Pointer to the output function (null:strptr) */
+	int chr				/* Character to be output */
+)
+{
+	if (XF_CRLF && chr == '\n') xfputc(func, '\r');	/* CR -> CRLF */
+
+	if (func) {
+		func(chr);		/* Write a character to the output device */
+	} else if (strptr) {
+		 *strptr++ = chr;	/* Write a character to the memory */
+	}
+}
+
+
+
+/*----------------------------------------------*/
+/* Put a null-terminated string                 */
+/*----------------------------------------------*/
+
+void xputs (			/* Put a string to the default device */
+	const char* str		/* Pointer to the string */
+)
+{
+	xfputs(xfunc_output, str);
+}
+
+
+void xfputs (			/* Put a string to the specified device */
+	void(*func)(int),	/* Pointer to the output function */
+	const char*	str		/* Pointer to the string */
+)
+{
+	while (*str) {			/* Put the string */
+		xfputc(func, *str++);
+	}
+}
+
+void xputs_P (			/* Put a string to the default device */
+	const __flash char *str		/* Pointer to the string */
+)
+{
+	xfputs_P(xfunc_output, str);
+}
+
+
+void xfputs_P (			/* Put a string to the specified device */
+	void(*func)(int),	/* Pointer to the output function */
+	const __flash char*	str		/* Pointer to the string */
+)
+{
+	char c;
+	while ((c = *str++)) {			/* Put the string */
+		xfputc(func, c);
+	}
+}
+
+
+/*----------------------------------------------*/
+/* Formatted string output                      */
+/*----------------------------------------------*/
+/*  xprintf("%d", 1234);			"1234"
+    xprintf("%6d,%3d%%", -200, 5);	"  -200,  5%"
+    xprintf("%-6u", 100);			"100   "
+    xprintf("%ld", 12345678);		"12345678"
+    xprintf("%llu", 0x100000000);	"4294967296"	<XF_USE_LLI>
+    xprintf("%lld", -1LL);			"-1"			<XF_USE_LLI>
+    xprintf("%04x", 0xA3);			"00a3"
+    xprintf("%08lX", 0x123ABC);		"00123ABC"
+    xprintf("%016b", 0x550F);		"0101010100001111"
+    xprintf("%*d", 6, 100);			"   100"
+    xprintf("%s", "String");		"String"
+    xprintf("%5s", "abc");			"  abc"
+    xprintf("%-5s", "abc");			"abc  "
+    xprintf("%-5s", "abcdefg");		"abcdefg"
+    xprintf("%-5.5s", "abcdefg");	"abcde"
+    xprintf("%-.5s", "abcdefg");	"abcde"
+    xprintf("%-5.5s", "abc");		"abc  "
+    xprintf("%c", 'a');				"a"
+    xprintf("%12f", 10.0);			"   10.000000"	<XF_USE_FP>
+    xprintf("%.4E", 123.45678);		"1.2346E+02"	<XF_USE_FP>
+*/
+
+static void xvfprintf (
+	void(*func)(int),	/* Pointer to the output function */
+	const __flash char*	fmt,	/* Pointer to the format string */
+	va_list arp			/* Pointer to arguments */
+)
+{
+	unsigned int r, i, j, w, f;
+	int n, prec;
+	char str[SZB_OUTPUT], c, d, *p, pad;
+#if XF_USE_LLI
+	long long v;
+	unsigned long long uv;
+#else
+	long v;
+	unsigned long uv;
+#endif
+
+	for (;;) {
+		c = *fmt++;	/* Get a format character */
+		if (!c) break;				/* End of format? */
+		if (c != '%') {				/* Pass it through if not a % sequense */
+			xfputc(func, c); continue;
+		}
+		f = w = 0;			 		/* Clear parms */
+		pad = ' '; prec = -1;
+		c = *fmt++;					/* Get first char of the sequense */
+		if (c == '0') {				/* Flag: left '0' padded */
+			pad = '0'; c = *fmt++;
+		} else {
+			if (c == '-') {			/* Flag: left justified */
+				f = 2; c = *fmt++;
+			}
+		}
+		if (c == '*') {				/* Minimum width from an argument */
+			n = va_arg(arp, int);
+			if (n < 0) {			/* Flag: left justified */
+				n = 0 - n; f = 2;
+			}
+			w = n; c = *fmt++;
+		} else {
+			while (c >= '0' && c <= '9') {	/* Minimum width */
+				w = w * 10 + c - '0';
+				c = *fmt++;
+			}
+		}
+		if (c == '.') {				/* Precision */
+			c = *fmt++;
+			if (c == '*') {				/* Precision from an argument */
+				prec = va_arg(arp, int);
+				c = *fmt++;
+			} else {
+				prec = 0;
+				while (c >= '0' && c <= '9') {
+					prec = prec * 10 + c - '0';
+					c = *fmt++;
+				}
+			}
+		}
+		if (c == 'l') {		/* Prefix: Size is long */
+			f |= 4; c = *fmt++;
+#if XF_USE_LLI
+			if (c == 'l') {	/* Prefix: Size is long long */
+				f |= 8; c = *fmt++;
+			}
+#endif
+		}
+		if (!c) break;				/* End of format? */
+		switch (c) {				/* Type is... */
+		case 'b':					/* Unsigned binary */
+			r = 2; break;
+		case 'o':					/* Unsigned octal */
+			r = 8; break;
+		case 'd':					/* Signed decimal */
+		case 'u':					/* Unsigned decimal */
+			r = 10; break;
+		case 'x':					/* Hexdecimal (lower case) */
+		case 'X':					/* Hexdecimal (upper case) */
+			r = 16; break;
+		case 'c':					/* A character */
+			xfputc(func, (char)va_arg(arp, int)); continue;
+		case 's':					/* String */
+			p = va_arg(arp, char*);		/* Get a pointer argument */
+			if (!p) p = "";				/* Null ptr generates a null string */
+			j = strlen(p);
+			if (prec >= 0 && j > (unsigned int)prec) j = prec;	/* Limited length of string body */
+			for ( ; !(f & 2) && j < w; j++) xfputc(func, pad);	/* Left pads */
+			while (*p && prec--) xfputc(func, *p++);/* String body */
+			while (j++ < w) xfputc(func, ' ');		/* Right pads */
+			continue;
+#if XF_USE_FP
+		case 'f':					/* Float (decimal) */
+		case 'e':					/* Float (e) */
+		case 'E':					/* Float (E) */
+			ftoa(p = str, va_arg(arp, double), prec, c);	/* Make fp string */
+			for (j = strlen(p); !(f & 2) && j < w; j++) xfputc(func, pad);	/* Left pads */
+			while (*p) xfputc(func, *p++);		/* Value */
+			while (j++ < w) xfputc(func, ' ');	/* Right pads */
+			continue;
+#endif
+		default:					/* Unknown type (passthrough) */
+			xfputc(func, c); continue;
+		}
+
+		/* Get an integer argument and put it in numeral */
+#if XF_USE_LLI
+		if (f & 8) {	/* long long argument? */
+			v = (long long)va_arg(arp, long long);
+		} else {
+			if (f & 4) {	/* long argument? */
+				v = (c == 'd') ? (long long)va_arg(arp, long) : (long long)va_arg(arp, unsigned long);
+			} else {		/* int/short/char argument */
+				v = (c == 'd') ? (long long)va_arg(arp, int) : (long long)va_arg(arp, unsigned int);
+			}
+		}
+#else
+		if (f & 4) {	/* long argument? */
+			v = (long)va_arg(arp, long);
+		} else {		/* int/short/char argument */
+			v = (c == 'd') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int);
+		}
+#endif
+		if (c == 'd' && v < 0) {	/* Negative value? */
+			v = 0 - v; f |= 1;
+		}
+		i = 0; uv = v;
+		do {	/* Make an integer number string */
+			d = (char)(uv % r); uv /= r;
+			if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
+			str[i++] = d + '0';
+		} while (uv != 0 && i < sizeof str);
+		if (f & 1) str[i++] = '-';					/* Sign */
+		for (j = i; !(f & 2) && j < w; j++) xfputc(func, pad);	/* Left pads */
+		do xfputc(func, str[--i]); while (i != 0);	/* Value */
+		while (j++ < w) xfputc(func, ' ');			/* Right pads */
+	}
+}
+
+
+void xprintf (			/* Put a formatted string to the default device */
+	const char*	fmt,	/* Pointer to the format string */
+	...					/* Optional arguments */
+)
+{
+	va_list arp;
+
+
+	va_start(arp, fmt);
+	xvfprintf(xfunc_output, fmt, arp);
+	va_end(arp);
+}
+
+
+void xfprintf (			/* Put a formatted string to the specified device */
+	void(*func)(int),	/* Pointer to the output function */
+	const char*	fmt,	/* Pointer to the format string */
+	...					/* Optional arguments */
+)
+{
+	va_list arp;
+
+
+	va_start(arp, fmt);
+	xvfprintf(func, fmt, arp);
+	va_end(arp);
+}
+
+
+void xsprintf (			/* Put a formatted string to the memory */
+	char* buff,			/* Pointer to the output buffer */
+	const char*	fmt,	/* Pointer to the format string */
+	...					/* Optional arguments */
+)
+{
+	va_list arp;
+
+
+	strptr = buff;		/* Enable destination for memory */
+	va_start(arp, fmt);
+	xvfprintf(0, fmt, arp);
+	va_end(arp);
+	*strptr = 0;		/* Terminate output string */
+	strptr = 0;			/* Disable destination for memory */
+}
+
+
+
+#if XF_USE_DUMP
+/*----------------------------------------------*/
+/* Dump a line of binary dump                   */
+/*----------------------------------------------*/
+
+void put_dump (
+	const void* buff,		/* Pointer to the array to be dumped */
+	unsigned long addr,		/* Heading address value */
+	int len,				/* Number of items to be dumped */
+	size_t width			/* Size of buff[0] (1, 2 or 4) */
+)
+{
+	int i;
+	const unsigned char *bp;
+	const unsigned short *sp;
+	const unsigned long *lp;
+
+
+	xprintf("%08lX ", addr);		/* address */
+
+	switch (width) {
+	case sizeof (char):
+		bp = buff;
+		for (i = 0; i < len; i++) {		/* Hexdecimal dump in (char) */
+			xprintf(" %02X", bp[i]);
+		}
+		xputs("  ");
+		for (i = 0; i < len; i++) {		/* ASCII dump */
+			xputc((unsigned char)((bp[i] >= ' ' && bp[i] <= '~') ? bp[i] : '.'));
+		}
+		break;
+	case sizeof (short):
+		sp = buff;
+		do {							/* Hexdecimal dump in (short) */
+			xprintf(" %04X", *sp++);
+		} while (--len);
+		break;
+	case sizeof (long):
+		lp = buff;
+		do {							/* Hexdecimal dump in (short) */
+			xprintf(" %08lX", *lp++);
+		} while (--len);
+		break;
+	}
+
+	xputc('\n');
+}
+#endif	/* XF_USE_DUMP */
+
+#endif	/* XF_USE_OUTPUT */
+
+
+
+#if XF_USE_INPUT
+int (*xfunc_input)(void);	/* Pointer to the default input stream */
+
+/*----------------------------------------------*/
+/* Get a line from the input                    */
+/*----------------------------------------------*/
+
+
+int xgets (			/* 0:End of stream, 1:A line arrived */
+	char* buff,		/* Pointer to the buffer */
+	int len			/* Buffer length */
+)
+{
+	int c, i;
+
+
+	if (!xfunc_input) return 0;	/* No input function is specified */
+
+	i = 0;
+	for (;;) {
+		c = xfunc_input();			/* Get a char from the incoming stream */
+		if (c < 0 || c == '\r') break;	/* End of stream or CR? */
+		if (c == '\b' && i) {		/* BS? */
+			i--;
+			if (XF_INPUT_ECHO) xputc(c);
+			continue;
+		}
+		if (c >= ' ' && i < len - 1) {	/* Visible chars? */
+			buff[i++] = c;
+			if (XF_INPUT_ECHO) xputc(c);
+		}
+	}
+	if (XF_INPUT_ECHO) {
+		xputc('\r');
+		xputc('\n');
+	}
+	buff[i] = 0;	/* Terminate with a \0 */
+	return (int)(c == '\r');
+}
+
+
+/*----------------------------------------------*/
+/* Get a value of integer string                */
+/*----------------------------------------------*/
+/*	"123 -5   0x3ff 0b1111 0377  w "
+	    ^                           1st call returns 123 and next ptr
+	       ^                        2nd call returns -5 and next ptr
+                   ^                3rd call returns 1023 and next ptr
+                          ^         4th call returns 15 and next ptr
+                               ^    5th call returns 255 and next ptr
+                                  ^ 6th call fails and returns 0
+*/
+
+int xatoi (			/* 0:Failed, 1:Successful */
+	char **str,		/* Pointer to pointer to the string */
+	long *res		/* Pointer to the valiable to store the value */
+)
+{
+	unsigned long val;
+	unsigned char c, r, s = 0;
+
+
+	*res = 0;
+
+	while ((c = **str) == ' ') (*str)++;	/* Skip leading spaces */
+
+	if (c == '-') {		/* negative? */
+		s = 1;
+		c = *(++(*str));
+	}
+
+	if (c == '0') {
+		c = *(++(*str));
+		switch (c) {
+		case 'x':		/* hexdecimal */
+			r = 16; c = *(++(*str));
+			break;
+		case 'b':		/* binary */
+			r = 2; c = *(++(*str));
+			break;
+		default:
+			if (c <= ' ') return 1;	/* single zero */
+			if (c < '0' || c > '9') return 0;	/* invalid char */
+			r = 8;		/* octal */
+		}
+	} else {
+		if (c < '0' || c > '9') return 0;	/* EOL or invalid char */
+		r = 10;			/* decimal */
+	}
+
+	val = 0;
+	while (c > ' ') {
+		if (c >= 'a') c -= 0x20;
+		c -= '0';
+		if (c >= 17) {
+			c -= 7;
+			if (c <= 9) return 0;	/* invalid char */
+		}
+		if (c >= r) return 0;		/* invalid char for current radix */
+		val = val * r + c;
+		c = *(++(*str));
+	}
+	if (s) val = 0 - val;			/* apply sign if needed */
+
+	*res = val;
+	return 1;
+}
+
+
+#if XF_USE_FP
+/*----------------------------------------------*/
+/* Get a value of the real number string        */
+/*----------------------------------------------*/
+/* Float version of xatoi
+*/
+
+int xatof (			/* 0:Failed, 1:Successful */
+	char **str,		/* Pointer to pointer to the string */
+	double *res		/* Pointer to the valiable to store the value */
+)
+{
+	double val;
+	int s, f, e;
+	unsigned char c;
+
+
+	*res = 0;
+	s = f = 0;
+
+	while ((c = **str) == ' ') (*str)++;	/* Skip leading spaces */
+	if (c == '-') {			/* Negative? */
+		c = *(++(*str)); s = 1; 
+	} else if (c == '+') {	/* Positive? */
+		c = *(++(*str));
+	}
+	if (c == XF_DPC) {		/* Leading dp? */
+		f = -1; 			/* Start at fractional part */
+		c = *(++(*str));
+	}
+	if (c <= ' ') return 0;	/* Wrong termination? */
+	val = 0;
+	while (c > ' ') {		/* Get a value of decimal */
+		if (c == XF_DPC) {	/* Embedded dp? */
+			if (f < 0) return 0;	/* Wrong dp? */
+			f = -1;			/* Enter fractional part */
+		} else {
+			if (c < '0' || c > '9') break;	/* End of decimal? */
+			c -= '0';
+			if (f == 0) {	/* In integer part */
+				val = val * 10 + c;
+			} else {		/* In fractional part */
+				val += i10x(f--) * c;
+			}
+		}
+		c = *(++(*str));
+	}
+	if (c > ' ') {	/* It may be an exponent */
+		if (c != 'e' && c != 'E') return 0;	/* Wrong character? */
+		c = *(++(*str));
+		if (c == '-') {
+			c = *(++(*str)); s |= 2;	/* Negative exponent */
+		} else if (c == '+') {
+			c = *(++(*str));			/* Positive exponent */
+		}
+		if (c <= ' ') return 0;	/* Wrong termination? */
+		e = 0;
+		while (c > ' ') {		/* Get value of exponent */
+			c -= '0';
+			if (c > 9) return 0;	/* Not a numeral? */
+			e = e * 10 + c;
+			c = *(++(*str));
+		}
+		val *= i10x((s & 2) ? -e : e);	/* Apply exponent */
+	}
+
+	if (s & 1) val = -val;	/* Negate sign if needed */
+
+	*res = val;
+	return 1;
+}
+#endif /* XF_USE_FP */
+
+#endif /* XF_USE_INPUT */

+ 53 - 0
soft/xprintf.h

@@ -0,0 +1,53 @@
+/*------------------------------------------------------------------------*/
+/* Universal string handler for user console interface  (C)ChaN, 2021     */
+/*------------------------------------------------------------------------*/
+
+#ifndef XPRINTF_DEF
+#define XPRINTF_DEF
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define XF_USE_OUTPUT	1	/* 1: Enable output functions */
+#define	XF_CRLF			0	/* 1: Convert \n ==> \r\n in the output char */
+#define	XF_USE_DUMP		0	/* 1: Enable put_dump function */
+#define	XF_USE_LLI		0	/* 1: Enable long long integer in size prefix ll */
+#define	XF_USE_FP		0	/* 1: Enable support for floating point in type e and f */
+#define XF_DPC			'.'	/* Decimal separator for floating point */
+#define XF_USE_INPUT	0	/* 1: Enable input functions */
+#define	XF_INPUT_ECHO	0	/* 1: Echo back input chars in xgets function */
+
+#if defined(__GNUC__) && __GNUC__ >= 10
+#pragma GCC diagnostic ignored "-Wcast-function-type"
+#endif
+
+#if XF_USE_OUTPUT
+#define xdev_out(func) xfunc_output = (void(*)(int))(func)
+extern void (*xfunc_output)(int);
+void xputc (int chr);
+void xfputc (void (*func)(int), int chr);
+void xputs (const char* str);
+void xfputs (void (*func)(int), const char* str);
+void xprintf (const char* fmt, ...);
+void xsprintf (char* buff, const char* fmt, ...);
+void xfprintf (void (*func)(int), const char* fmt, ...);
+void put_dump (const void* buff, unsigned long addr, int len, size_t width);
+void xputs_P (const __flash char* str);
+void xfputs_P (void (*func)(int), const __flash char* str);
+#endif
+
+#if XF_USE_INPUT
+#define xdev_in(func) xfunc_input = (int(*)(void))(func)
+extern int (*xfunc_input)(void);
+int xgets (char* buff, int len);
+int xatoi (char** str, long* res);
+int xatof (char** str, double* res);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików