1 /*
2  * Copyright (c) 2016-2019, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  *  @file       SDFatFS.h
34  *
35  *  @brief      File Allocation Table File System (FATFS) Driver
36  *
37  *  The SDFatFS header file should be included in an application as follows:
38  *  @code
39  *  #include <ti/drivers/SDFatFS.h>
40  *  #include <ti/drivers/SD.h>
41  *  @endcode
42  *
43  *  @anchor ti_drivers_SDFatFS_Overview
44  *  # Overview #
45  *
46  *  The SDFatFS driver is designed to hook into FatFs by implementing a
47  *  set of functions that FatFs needs to call to perform basic block data
48  *  transfers. This driver makes use of the SD driver for lower level disk IO
49  *  operations.
50  *
51  *  The only functions that should be called by the application are the
52  *  standard driver framework functions (_open, _close, etc...).
53  *
54  *  The application may use the FatFs APIs or the standard C
55  *  runtime file I/O calls (fopen, fclose, etc...) given that SDFatFS_open has
56  *  has been successfully called. After the SDFatFS_close API is called,
57  *  ensure the application does NOT make any file I/O calls.
58  *
59  *  ## Opening the driver #
60  *
61  *  @code
62  *  SDFatFS_Handle handle;
63  *
64  *  handle = SDFatFS_open(CONFIG_SDFatFS0, driveNum, NULL);
65  *  if (handle == NULL) {
66  *      //Error opening SDFatFS driver
67  *      while (1);
68  *  }
69  *  @endcode
70  *
71  *  # Instrumentation #
72  *
73  *  The SDFatFS driver interface produces log statements if
74  *  instrumentation is enabled.
75  *
76  *  Diagnostics Mask | Log details                   |
77  *  ---------------- | -----------                   |
78  *  Diags_USER1      | basic operations performed    |
79  *  Diags_USER2      | detailed operations performed |
80  *  ============================================================================
81  */
82 
83 #ifndef ti_drivers_SDFatFS__include
84 #define ti_drivers_SDFatFS__include
85 
86 #include <stdint.h>
87 
88 #include <ti/drivers/SD.h>
89 
90 #include <third_party/fatfs/ff.h>
91 #include <third_party/fatfs/diskio.h>
92 
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96 
97 /*!
98  *  @brief SDFatFS Object
99  *  The application must not access any member variables of this structure!
100  */
101 typedef struct {
102     uint_fast32_t driveNum;
103     DSTATUS       diskState;
104     FATFS         filesystem; /* FATFS data object */
105     SD_Handle     sdHandle;
106 } SDFatFS_Object;
107 
108 /*!
109  *  @brief SDFatFS Global configuration
110  *
111  *  The #SDFatFS_Config structure contains a single pointer used to characterize
112  *  the SDFatFS driver implementation.
113  *
114  *  This structure needs to be defined before calling SDFatFS_init() and it must
115  *  not be changed thereafter.
116  *
117  *  @sa SDFatFS_init()
118  */
119 typedef struct {
120     /*! Pointer to a SDFatFS object */
121     SDFatFS_Object *object;
122 } SDFatFS_Config;
123 
124 
125 /*!
126  *  @brief A handle that is returned from a SDFatFS_open() call.
127  */
128 typedef SDFatFS_Config *SDFatFS_Handle;
129 
130 /*!
131  *  @brief Function to open a SDFatFS instance on the specified drive.
132  *
133  *  Function to mount the FatFs filesystem and register the SDFatFS disk
134  *  I/O functions with the FatFS module.
135  *
136  *  @param idx Logical peripheral number indexed into the HWAttrs
137  *             table.
138  *  @param drive Drive Number
139  */
140 extern SDFatFS_Handle SDFatFS_open(uint_least8_t idx, uint_least8_t drive);
141 
142 /*!
143  *  @brief Function to close a SDFatFS instance specified by the SDFatFS
144  *         handle.
145  *
146  *         This function unmounts the file system mounted by SDFatFS_open() and
147  *         unregisters the SDFatFS driver from the FatFs module.
148  *
149  *  @pre SDFatFS_open() had to be called first.
150  *
151  *  @param handle A #SDFatFS_Handle returned from SDFatFS_open()
152  *
153  *  @sa SDFatFS_open()
154  */
155 extern void SDFatFS_close(SDFatFS_Handle handle);
156 
157 /*!
158  *  Function to initialize a SDFatFS instance
159  */
160 extern void SDFatFS_init(void);
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif /* ti_drivers_SDFatFS__include */
167