FORGOT YOUR DETAILS?

CREATE ACCOUNT

Unison Help

  1. Unison Kernel
    1. Pthreads
      1. pthread_create()
      2. pthread_exit()
      3. pthread_self()
      4. pthread_equal()
      5. pthread_join()
      6. pthread_detach()
      7. pthread_setschedparam()
      8. pthread_getschedparam()
      9. pthread_attr_init()
      10. pthread_attr_destroy()
      11. pthread_attr_setstackaddr()
      12. pthread_attr_getstackaddr()
      13. pthread_attr_setstacksize()
      14. pthread_attr_getstacksize()
      15. pthread_attr_setschedparam()
      16. pthread_attr_getschedparam()
      17. pthread_attr_setdetachstate()
      18. pthread_attr_getdetachstate()
      19. pthread_stackinfo()
      20. pthread_setprio()
      21. pthread_getprio()
      22. sched_get_priority_max()
      23. sched_get_priority_min()
      24. sched_yield()
    2. Pthread Cancellation
      1. pthread_cleanup_pop()
      2. pthread_cleanup_push()
      3. pthread_cancel()
      4. pthread_setcanceltype()
      5. pthread_setcancelstate()
      6. pthread_testcancel()
    3. Mutex
      1. pthread_mutex_init()
      2. pthread_mutex_destroy()
      3. pthread_mutex_lock()
      4. pthread_mutex_trylock()
      5. pthread_mutex_unlock()
    4. Semaphores
      1. sem_open()
      2. sem_close()
      3. sem_unlink()
      4. sem_init()
      5. sem_destroy()
      6. sem_wait()
      7. sem_trywait()
      8. sem_timedwait()
      9. sem_post()
      10. sem_getvalue()
    5. Message Queues
      1. mq_open()
      2. mq_close()
      3. mq_unlink()
      4. mq_send()
      5. mq_receive()
      6. mq_notify()
      7. mq_setattr()
      8. mq_getattr()
      9. mq_timedreceive()
      10. mq_timedsend()
    6. Conditional Variables
      1. pthread_cond_init()
      2. pthread_cond_destroy()
      3. pthread_cond_wait()
      4. pthread_cond_timedwait()
      5. pthread_cond_signal()
      6. pthread_cond_broadcast()
      7. pthread_condattr_init()
      8. pthread_condattr_destroy()
    7. Barriers
      1. pthread_barrier_init()
      2. pthread_barrier_destroy()
      3. pthread_barrier_wait()
    8. Timers
      1. timer_create()
      2. timer_delete()
      3. timer_settime()
      4. timer_gettime()
      5. timer_getoverrun()
      6. timer_tick()
      7. nanosleep()
    9. Clocks
      1. time()
      2. uptime()
      3. sleep()
      4. clock_settime()
      5. clock_gettime()
      6. clock_getres()
      7. clock_init()
    10. Memory Allocation
      1. POSIX.1
        1. malloc()
        2. free()
      2. Variable Length (Pools)
        1. pool_create()
        2. pool_destroy()
        3. pool_alloc()
        4. pool_free()
      3. Fixed Length (Partitions)
        1. pt_create()
        2. pt_destroy()
        3. pt_getblock()
        4. pt_freeblock()
    11. Rendezvous
      1. mr_init()
      2. mr_send()
      3. mr_receive()
      4. mr_reply()
      5. mr_sigrecv()
      6. mr_sigpost()
    12. Interrupts
      1. interrupts
      2. i_disable()
      3. i_restore()
    13. Directory Services
      1. dir_register()
      2. dir_deregister()
      3. dir_lookup()
      4. dir_lookup_string()
    14. Miscellaneous
      1. checkIstack()
      2. NanoStart() or DSPexec_Start()
      3. _isrStackFill
      4. Kernel Scaling
      5. kfatal()
      6. kalloc()
      7. kfree()
      8. mpu or mmu
      9. pthreadStackFill
      10. thread_numb()
      11. thread_utilization_start()
      12. thread_utilization_stop()
      13. xprintf()
      14. xputs()
      15. xputchar()
  2. Unison I/O Library
    1. accept()
    2. bind()
    3. chmod()
    4. close()
    5. connect()
    6. creat()
    7. fstat()
    8. getpeername()
    9. getsockname()
    10. getsockopt()
    11. ioctl()
    12. link()
    13. listen()
    14. lseek()
    15. mkdir()
    16. mkfs()
    17. mount()
    18. open()
    19. read()
    20. recv()
    21. recvfrom()
    22. rename()
    23. renameat()
    24. rmdir()
    25. select()
    26. send()
    27. sendto()
    28. setsockopt()
    29. shutdown()
    30. socket()
    31. stat()
    32. sync()
    33. umount()
    34. unlink()
    35. write()
  3. Unison STDIO Library
    1. STDIO Library Calls
      1. clearerr()
      2. dprintf()
      3. fclose()
      4. fdopen()
      5. feof()
      6. ferror()
      7. fileno()
      8. fflush()
      9. fgetc()
      10. fgetpos()
      11. fgets()
      12. fopen()
      13. fprintf()
      14. fputc()
      15. fputs()
      16. fread()
      17. freopen()
      18. fscanf()
      19. fseek()
      20. fseeko()
      21. fsetpos()
      22. ftell()
      23. ftello()
      24. fwrite()
      25. getc()
      26. getc_unlocked()
      27. getchar()
      28. getchar_unlocked()
      29. getdelim()
      30. getline()
      31. gets()
      32. get_stderr_ptr()
      33. get_stdin_ptr()
      34. get_stdout_ptr()
      35. noperprintf()
      36. perprintf()
      37. perror()
      38. posix_compat()
      39. printf()
      40. putc()
      41. putc_unlocked()
      42. putchar()
      43. putchar_unlocked()
      44. puts()
      45. remove()
      46. rewind()
      47. scanf()
      48. setbuf()
      49. setvbuf()
      50. snprintf()
      51. sprintf()
      52. sscanf()
      53. stderr_init()
      54. stderr_close()
      55. stdin_init()
      56. stdin_close()
      57. stdout_init()
      58. stdout_close()
      59. vdprintf()
      60. vscanf()
      61. vsscanf()
      62. vfscanf()
      63. vprintf()
      64. vsnprintf()
      65. vsprintf()
      66. vfprintf()
      67. ungetc()
    2. Do-nothing Stubs
      1. ctermid()
      2. flockfile()
      3. fmemopen()
      4. ftrylockfile()
      5. open_memstream()
      6. pclose()
      7. popen()
      8. tempnam()
      9. tmpfile()
      10. tmpnam()
  4. Unison LIBC Library
    1. LIBC Library Calls
      1. assert()
      2. realloc()
      3. strcasecmp()
      4. strdup()
      5. strncasecmp()
      6. strftime()
    2. Do-nothing Stubs
      1. abort()
      2. execve()
      3. exit()
      4. _Exit()
      5. fork()
      6. getpid()
      7. isatty()
      8. kill()
      9. sbrk()
      10. times()
      11. wait()
    3. Do-nothing Wide-character Stubs
      1. <wchar.h>
      2. <wctype.h>
  5. Unison I/O Servers
    1. File Servers
      1. Multimedia File Server - fsys
      2. FAT File System - fatfs
      3. NAND File Server - nandfsys
      4. NOR File Server - norfsys
      5. Network File Server - nfs
  6. Graphics, Camera, Video, Audio
    1. Vendor Graphics
    2. Prism++ Graphics
    3. ADPCM Services - adpcmd
    4. Camera
  7. Network Protocols
    1. TCP and UDP Server - tcpd
      1. IPv4 only server
      2. IPv4/IPv6 server
    2. DHCP Client Service - dhcp client
    3. DHCP Server - dhcpd
    4. Telnet Server - telnetd
    5. Tiny FTP Server - tftpd
    6. Point to Point - pppd
    7. Network Translation - NAT with PAT
    8. Firewall
      1. Packet filter: pf
      2. Packet filter control: pfctl
      3. Fitler rules: pf.filtering
      4. Translation rules: pf.nat
    9. Tiny HTTP Server - thttpd
    10. Tiny HTTP Server with TLS
    11. POP3 Server
    12. Simple Mail Transfer Protocol Services (SMTP)
    13. Bootp Protocol
    14. File Transfer Protocol Server (FTP)
    15. File Transfer Client Services
    16. RPC / XDR
    17. DNS Client
    18. HTTP/HTTPS Client
    19. REST Client
    20. AutoIP Service - autoip client
    21. mDNS server - mdnsd
    22. SNTP Client
    23. SNMP Agent - Snmpd server
    24. SSL/TLS library
    25. SSH server
    26. IP security
      1. IPsec description
      2. IPsec administration: ipsecadm
      3. Virtual Private Network: VPN
    27. Power Control
      1. Motor and Motion Control Servers
      2. PWM, Encoders
    28. Serial I/O
      1. Asynchronous Serial I/O Server - ttyserver
      2. CAN Server - cand
      3. I2C Server - i2cd
      4. I2S Server - i2sd
    29. System Services
      1. Power Management Servers
      2. Login Service - login_services
      3. XML
      4. POSIX Shell and Login Service - posh
    30. Universal Serial Bus (USB)
      1. USB Server
      2. USB Device Server
      3. USB Embedded Host Server
    31. Wireless
      1. Wireless Servers and Drivers
      2. 802.15.4 Radio Servers
      3. TCP/v6 with 6loWPAN
      4. ZigBee
      5. BlueTooth Server
      6. 802.11 Wi-Fi
      7. GPRS, UHF and GPS Radio Servers
    32. Remedy Tools for Unison
      1. Remedy Data Logging and Event Display Tools
      2. Remedy Diagnostics
      3. Remedy Flash Downloader/Bootloader
      4. Remedy Power On Self Test - POST
      5. Remedy OS Object Viewer
      6. Remedy Remote Control Tools

5.1.2.FAT File System - fatfs #

NAME

FAT File Server – fatfs

SYNOPSIS

#include <device.h>
#include <fsysinit.h>

pthread_create(&tid, &attr, (void*(*)(void*))_fatfs_server, (char*)&fsysinit)
THREAD _fatfs_server( (char *)&fsysinit)
struct fsysinit {
uint_16 fsi_msgsize;
uint_16 fsi_nclones;    /* Number of servers to create, not used for FATFS */
uint_32 fsi_datalog;    /* 1 - enable xprintf output for debug purpose, 0 - disable */
uint_16 fsi_nbuffers;   /* Number of buffers in buffer cache, minimum 2, not used for FATFS*/
uint_16 fsi_blocksize;   /* Memory block size: 256 for RAM-disc, 512 for sd/mmc-card, not used for FATFS */
uint_16 fsi_maxopen;    /* Max number of open files */
uint_16 fsi_ndrives;    /* Maximum number of allocated disk, valid for FATFS only*/
};

 

DESCRIPTION

The fat file server, fatfs provides fast fat based, windows fat compatible file system. It is fully compatible with the multimedia file system and can be used to replace the multimedia file system with a FAT compatible file system for use with SD cards, uSD cards, MMC cards, and USB data sticks. It supports the following operations:

Operation Description
mkfs make a file system, minor device numbers are used for different partitions
mount mount a file system at a specific device and directory
open open/create a file using flags
close close a file
read read from a file
write write to a file
mkdir make a directory
rmdir remove directory
stat get file information
fstat get file information
rename rename file relative to directory file descriptor
unlink remove a link to a file
lseek goto a specific offset in a file
umount unmount a file system
chmod change mode of a file relative to directory file descriptor
sync schedule file system updates
getdirentries get directory entries in a filesystem independent format
addfs add new disk drive into the file system
delfs delete disk drive from a file system

 

Typical operation for the fat file server involves several steps common in various operating system environments including Linux.

  1. Create a fat file server first so that commands can be carried out for the selected media. Use pthread_create to do this.
  2. Register the fat file server so that it can be found as a server in the system.
  3. Make a file system using mkfs. This creates a root directory structure on the media (doing device formatting, all information on media will be erased). It uses the optimal FAT 12, 16 or 32 format depending on the size of the media. This step is optional, and used only if need to create file system at new media. If media already has FAT, then this step could be avoided.
  4. Mount the file system. Note that an extra directory or a root directory is specified in addition to the registered file server name.
  5. The file system is now ready to use with the remaining file system commands.

    Compile Options

    The following compile time options may be used. Additional file compile time options do exist but should not be used without consulting the factory.

    Option Description
    _USE_LFN Use long file names may be set to one of three values, 0 – not used, 1 – on with BSS buffer, and 2 – on with stack buffer.
    MAX_SECTOR_SIZE Define the maximum sector size to be handled. The options are: 512/1024/2048/4096. The value of 512 is used for SD cards and Compact Flash, and it is the default.
    CLUSTER_SIZE Defines the size of cluster, may be 16384 bytes – for NAND or 4096 bytes – by default. Set 0 to auto detect.
    FATFS_DISK_TYPE Defines FATFS disk type to one of next: DISK_SD_MMC, DISK_NAND_512, DISK_NAND_2048, DISK_OTHER.
    MAX_MOUNTP_NAME Defines max mount point name.
    MAX_PATH_LEN Defines max file path length, after mount point.
    MAX_LFN_LEN Defines maximum long file name (LFN) length to handle (max:255). Make sure this value <= MAXNAMLEN, defined in “include\dirent.h“.
    MAX_NUM_MOUNTS Defines max mounts at the same time.
    MAX_FILE_HANDLES Defines max opened files at the same time.
    MAX_DIR_HANDLES Defines max opened directories at the same time.

    Run Time Options

    Initialization Options

    fsi_msgsizeThis is the size in bytes of the structure set for all standard Unison messages and initialization records.fsi_nclonesThis value is not used for fatfsfsi_datalog1 – enable xprintf output for debug purpose, 0 – disablefsi_nbuffersThis value is not used fro fatfsfsi_maxopenSet the maximum number of open files.fsi_blocksizeSet the file system block or sector size using the conditional compiliation values. This value is not used for fatfs.fsi_ndrivesMaximum number of allocated disk, typically 1 for SD/MMC. But could be up to 9, if necessary to create different discs. Open Options

    Option Description
    O_CONTIG Not Supported.
    O_CREAT Create the file if it does not exist.
    O_WRITE | O_WRONLY Open for writing.
    O_READ | O_RDONLY Open for reading.
    O_DIRECT Not supported.
    O_EXCL Not supported.
    O_TRUNC Erases the file by setting the file length to zero. Does not alter file contents or free space.
    O_APPEND Set the seek pointer to the end of the file prior to each write
    O_UNBUF Not Supported.
    O_NONBLOCK The file is always open.

    IOCTL Options

    For more details, see functions description in I/O Library.

RETURN VALUES

In case of successful start up pthread_create will return 0 value.

ERRORS

If any of the following conditions occur, the function call shall return -1 and set errno to the corresponding POSIX compatible error code.

EACCESSearch permission is denied on a component of the path prefix, pr the file exists and the permissions specified b oflag are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created, or O_TRUNC is specified and write permission is denied.EEXISTO_CREAT and O_EXCL are set and the named file exists.EINVALThe implementation does not support synchronized I/O for this file or another invalid parameter has been passed into the file server.EISDIRThe named file is a directory, and the oflag argument specifies write or read/write access.EMFILEToo many file descriptors are currently in use.EMLINIToo many file links are currently in use.ENODEVThe device is not available.ENFILEToo many files currently open in the system.ENOENTO_CREAT is not set and the named file does not exist, or O_CREAT is set and either the path prefix does not exist or the path argument points to an empty string.ENOMEMInsufficient memory exists to complete the request.ENOTDIRA component of the path prefix is not a directory.EROFSThe named file resides on a read-only file system and either O_WRONLY, O_RDWR, O_CREAT (if the file does not exist), or O_TRUNC is set in oflag argument.EIOA physical I/O error occured.EPERMInsufficient ownership for the file operation.ENOTEMPTYThe file or directory is not empty and the operation cannot be completed.ENOTSUPPORTEDThe fatfs file system does not support this operation.EBUSYThe file system is busy and cannot complete the operation.ENODEVNo such device.EFAULTFile system error.EBADFBad file operation.EBADFSYSFile system is corrupted.

EXAMPLE

struct fsysinit {
        uint_16 fsi_msgsize;
        uint_16 fsi_nclones;    /* Number of servers to create. */
        uint_32 fsi_datalog;    /* 1 - enable xprintf output for debug purpose, 0 - disable */
        uint_16 fsi_nbuffers;   /* Number of buffers in buffer cache, minimum 2*/
        uint_16 fsi_blocksize;   /* Memory block size: 256 for RAM-disc, 512 for sd/mmc-card */
        uint_16 fsi_maxopen;    /* Max number of open files */
        uint_16 fsi_ndrives;    /* Maximum number of allocated disk, valid for FATFS only*/
};

#define FATFS_DIRECTORY "/dev/sd"
#define FATFS_MOUNT FATFS_DIRECTORY "/d1"

...

        /*
         *
         *   FAT FS Initialization 
         *
         *
         */

        /* create the File server and mount disk */
        fsysinit.fsi_nclones = 1;       //should be at least 1
        fsysinit.fsi_datalog = 1;       //1 - enable xprintf output for debug purpose, 0 - disable
        fsysinit.fsi_ndrives = 1;       //maximum allowed disks, MAX  = 9

        // set up stack size and priority for main fsys threads and all clones
      	pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, FATFSSTACKSIZE);
        myNewFsysPriority.sched_priority = FATFSPRIO;
        pthread_attr_setschedparam(&attr, &myNewFsysPriority);

        if(!pthread_create(&tid, &attr, (void*(*)(void*))_fatfs_server, (char*)&fsysinit))
        {
                if(!dir_register(FATFS_DIRECTORY, tid, TYPE_SERVER))
                {
                        xprintf("..Unable to register name of File Server..\n");
                        xprintf("..Required resource not present - aborting..\n");
#if RTOS_DEBUG
                        dir_deregister("Main thread");
#endif
                        pthread_exit((void*)1);
                }
        }

        /*preparing adding driver for SD/MMC/CF card disk*/
        hw_fat_drv.disk_initialize = hw_disk_initialize;
        hw_fat_drv.disk_ioctl = hw_disk_ioctl;
        hw_fat_drv.disk_read = hw_disk_read;
        hw_fat_drv.disk_write = hw_disk_write;
        hw_fat_drv.disk_status = hw_disk_status;
        hw_fat_drv.disk_timerproc = hw_disk_timerproc;

        res = fatfs_add_drv(FATFS_DIRECTORY, &hw_fat_drv, &HW_DEVICE_NO); //add new disk driver to driver list structure //return HW_DEVICE_NO - local disk driver num

#ifdef MAKE_DISK_FORMATTING
        //mkfs will create FAT system at specified HW_DEVICE_NO.
        //FAT type will be automatically chooses, depending of flash drive capacity 
        mkfs_status = mkfs(FATFS_DIRECTORY, HW_DEVICE_NO, 0, 0);
#endif
        
        mount(HW_DEVICE_NO, FATFS_MOUNT, 0);
        
        //Now FATFS ready for usage.    

...

****** Hardware dependent low level driver functions ******

//Initialize Disk Drive 
DSTATUS hw_disk_initialize (void);
//Get Disk Status
DSTATUS hw_disk_status ();
//Read Sector(s) 
DRESULT hw_disk_read (BYTE *,DWORD , BYTE );
//Write Sector(s)  
DRESULT hw_disk_write (const BYTE *, DWORD, BYTE );
//Functions to obtain disc parameters - 
//GET_SECTOR_COUNT - Get number of sectors on the disk
//GET_SECTOR_SIZE  - Get sector size (WORD)
//From these values FATFS know capacity of drive.
DRESULT hw_disk_ioctl (BYTE , void *);
//typically used for SD cards to automatically detect card insert/eject and write protect.
void    hw_disk_timerproc (void);

See demo examples for more details

 

SEE ALSO

chmod(), close(), lseek(),mkdir(), mkfs(), mount(), open(), read(), stat(), write(), fsys,

Suggest Edit

CONTACT US

TO GET YOUR PROJECT STARTED

TOP