-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatsdraw.c
236 lines (197 loc) · 7.4 KB
/
fatsdraw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2012, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== fatsdraw.c ========
*/
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/fatfs/ff.h>
#include <ti/sysbios/knl/Clock.h>
/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SDSPI.h>
/* Example/Board Header files */
#include "Board.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Buffer size used for the file copy process */
#define CPY_BUFF_SIZE 2048
/* String conversion macro */
#define STR_(n) #n
#define STR(n) STR_(n)
/* Drive number used for FatFs */
#define DRIVE_NUM 0
const Char inputfile[] = STR(DRIVE_NUM)":input.txt";
const Char outputfile[] = STR(DRIVE_NUM)":output.txt";
const Char textarray[] = \
"***********************************************************************\n"
"0 1 2 3 4 5 6 7\n"
"01234567890123456789012345678901234567890123456789012345678901234567890\n"
"This is some text to be inserted into the inputfile if there isn't \n"
"already an existing file located on the SDCard. \n"
"If an inputfile already exists, or if the file was already once \n"
"generated, then the inputfile will NOT be modified. \n"
"***********************************************************************\n";
UChar cpy_buff[CPY_BUFF_SIZE];
/*
* ======== taskFxn ========
* Task to perform a file copy
*
* Task tries to open an existing file inputfile[]. If the file doesn't
* exist, create one and write some known content into it.
* The contents of the inputfile[] are then copied to an output file
* outputfile[]. Once completed, the contents of the output file are
* printed onto the system console (stdout).
*
* Task for this function is created statically. See the project's .cfg file.
*/
Void taskFxn(UArg arg0, UArg arg1)
{
FRESULT fresult;
SDSPI_Handle sdspiHandle;
SDSPI_Params sdspiParams;
/* Variables for the CIO functions */
FIL src, dst;
/* Variables to keep track of the file copy progress */
UInt bytesRead = 0;
UInt bytesWritten = 0;
UInt filesize;
UInt totalBytesCopied = 0;
/* Mount and register the SD Card */
SDSPI_Params_init(&sdspiParams);
sdspiHandle = SDSPI_open(Board_SDSPI, DRIVE_NUM, &sdspiParams);
if (sdspiHandle == NULL) {
System_abort("Error starting the SD card\n");
}
else {
System_printf("Drive %u is mounted\n", DRIVE_NUM);
}
/* Try to open the source file */
fresult = f_open(&src, inputfile, FA_READ);
if (fresult != FR_OK) {
System_printf("Creating a new file \"%s\"...", inputfile);
/* Open file for both reading and writing */
fresult = f_open(&src, inputfile, FA_CREATE_NEW|FA_READ|FA_WRITE);
if (fresult != FR_OK) {
System_printf("Error: \"%s\" could not be created\n",
inputfile);
System_abort("Aborting...\n");
}
f_write(&src, textarray, strlen(textarray), &bytesWritten);
f_sync(&src);
/* Reset the internal file pointer */
f_lseek(&src, 0);
System_printf("done\n");
}
else {
System_printf("Using existing copy of \"%s\"\n", inputfile);
}
/* Create a new file object for the file copy */
fresult = f_open(&dst, outputfile, FA_CREATE_ALWAYS|FA_WRITE);
if (fresult != FR_OK) {
System_printf("Error opening \"%s\"\n", outputfile);
System_abort("Aborting...\n");
}
else {
System_printf("Starting file copy\n");
}
/* Copy the contents from the src to the dst */
while (TRUE) {
/* Read from source file */
fresult = f_read(&src, cpy_buff, CPY_BUFF_SIZE, &bytesRead);
if (fresult || bytesRead == 0) {
break; /* Error or EOF */
}
/* Write to dst file */
fresult = f_write(&dst, cpy_buff, bytesRead, &bytesWritten);
if (fresult || bytesWritten < bytesRead) {
System_printf("Disk Full\n");
break; /* Error or Disk Full */
}
/* Update the total number of bytes copied */
totalBytesCopied += bytesWritten;
}
f_sync(&dst);
/* Get the filesize of the source file */
filesize = f_size(&src);
/* Close both inputfile[] and outputfile[] */
f_close(&src);
f_close(&dst);
System_printf("File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n",
inputfile, filesize, outputfile, totalBytesCopied);
/* Now output the outputfile[] contents onto the console */
fresult = f_open(&dst, outputfile, FA_READ);
if (fresult != FR_OK) {
System_printf("Error opening \"%s\"\n", outputfile);
System_abort("Aborting...\n");
}
/* Print file contents */
while (TRUE) {
/* Read from output file */
fresult = f_read(&dst, cpy_buff, CPY_BUFF_SIZE, &bytesRead);
if (fresult || bytesRead == 0) {
break; /* Error or EOF */
}
/* Write output */
System_printf("%s", cpy_buff);
}
/* Close the file */
f_close(&dst);
/* Stopping the SDCard */
SDSPI_close(sdspiHandle);
System_printf("Drive %u unmounted\n", DRIVE_NUM);
BIOS_exit(0);
}
/*
* ======== main ========
*/
Int main(Void)
{
/* Call board init functions */
Board_initGeneral();
Board_initGPIO();
Board_initSDSPI();
/* Turn on user LED */
GPIO_write(Board_LED, Board_LED_ON);
System_printf("Starting the FatSD Raw example\nSystem provider is set to "
"SysMin. Halt the target and use ROV to view output.\n");
/* SysMin will only print to the console when you call flush or exit */
System_flush();
/* Start BIOS */
BIOS_start();
return (0);
}