Skip to content

Commit 6f88739

Browse files
simonqhughesgeky
authored andcommitted
Re-instating the FAT32/SDCard support with POSIX File API for mbed-client team.
1 parent 315d893 commit 6f88739

File tree

19 files changed

+457
-2
lines changed

19 files changed

+457
-2
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2006-2016 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
/* The following copyright notice is reproduced from the glibc project */
20+
21+
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
22+
This file is part of the GNU C Library.
23+
24+
The GNU C Library is free software; you can redistribute it and/or
25+
modify it under the terms of the GNU Library General Public License as
26+
published by the Free Software Foundation; either version 2 of the
27+
License, or (at your option) any later version.
28+
29+
The GNU C Library is distributed in the hope that it will be useful,
30+
but WITHOUT ANY WARRANTY; without even the implied warranty of
31+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32+
Library General Public License for more details.
33+
34+
You should have received a copy of the GNU Library General Public
35+
License along with the GNU C Library; see the file COPYING.LIB. If
36+
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
37+
Cambridge, MA 02139, USA. */
38+
39+
40+
/** @file basic.cpp POSIX File API (stdio) test cases
41+
*
42+
* Consult the documentation under the test-case functions for
43+
* a description of the individual test case.
44+
*/
45+
46+
#include "mbed.h"
47+
#include "SDFileSystem.h"
48+
#include "test_env.h"
49+
50+
#include "utest/utest.h"
51+
#include "unity/unity.h"
52+
#include "greentea-client/test_env.h"
53+
54+
55+
using namespace utest::v1;
56+
57+
58+
#if defined(TARGET_KL25Z)
59+
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
60+
61+
#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z)
62+
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
63+
64+
#elif defined(TARGET_K64F) || defined(TARGET_K66F)
65+
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
66+
67+
#elif defined(TARGET_K22F)
68+
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
69+
70+
#elif defined(TARGET_K20D50M)
71+
SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd");
72+
73+
#elif defined(TARGET_nRF51822)
74+
SDFileSystem sd(p12, p13, p15, p14, "sd");
75+
76+
#elif defined(TARGET_NUCLEO_F030R8) || \
77+
defined(TARGET_NUCLEO_F070RB) || \
78+
defined(TARGET_NUCLEO_F072RB) || \
79+
defined(TARGET_NUCLEO_F091RC) || \
80+
defined(TARGET_NUCLEO_F103RB) || \
81+
defined(TARGET_NUCLEO_F302R8) || \
82+
defined(TARGET_NUCLEO_F303RE) || \
83+
defined(TARGET_NUCLEO_F334R8) || \
84+
defined(TARGET_NUCLEO_F401RE) || \
85+
defined(TARGET_NUCLEO_F410RB) || \
86+
defined(TARGET_NUCLEO_F411RE) || \
87+
defined(TARGET_NUCLEO_L053R8) || \
88+
defined(TARGET_NUCLEO_L073RZ) || \
89+
defined(TARGET_NUCLEO_L152RE)
90+
SDFileSystem sd(D11, D12, D13, D10, "sd");
91+
92+
#elif defined(TARGET_DISCO_F051R8) || \
93+
defined(TARGET_NUCLEO_L031K6)
94+
SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd");
95+
96+
#elif defined(TARGET_LPC2368)
97+
SDFileSystem sd(p11, p12, p13, p14, "sd");
98+
99+
#elif defined(TARGET_LPC11U68)
100+
SDFileSystem sd(D11, D12, D13, D10, "sd");
101+
102+
#elif defined(TARGET_LPC1549)
103+
SDFileSystem sd(D11, D12, D13, D10, "sd");
104+
105+
#elif defined(TARGET_RZ_A1H)
106+
SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
107+
108+
#elif defined(TARGET_LPC11U37H_401)
109+
SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd");
110+
111+
#else
112+
SDFileSystem sd(p11, p12, p13, p14, "sd");
113+
#endif
114+
115+
#define FSFAT_BASIC_MSG_BUF_SIZE 256
116+
117+
const char *sd_file_path = "/sd/out.txt";
118+
const int FSFAT_BASIC_DATA_SIZE = 256;
119+
static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE];
120+
121+
#define FSFAT_BASIC_MSG(_buf, _max_len, _fmt, ...) \
122+
do \
123+
{ \
124+
snprintf((_buf), (_max_len), (_fmt), __VA_ARGS__); \
125+
}while(0);
126+
127+
/** @brief fopen test case
128+
*
129+
* - open a file
130+
* - generate random data items, write the item to the file and store a coy in a buffer for later use.
131+
* - close the file.
132+
* - open the file.
133+
* - read the data items from the file and check they are the same as write.
134+
* - close the file.
135+
*
136+
* @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
137+
*/
138+
static control_t sdcard_test_00() {
139+
140+
uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 };
141+
bool result = false;
142+
143+
// Fill data_written buffer with random data
144+
// Write these data into the file
145+
bool write_result = false;
146+
{
147+
printf("SD: Writing ... ");
148+
FILE *f = fopen(sd_file_path, "w");
149+
if (f) {
150+
for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
151+
data_written[i] = rand() % 0XFF;
152+
fprintf(f, "%c", data_written[i]);
153+
}
154+
write_result = true;
155+
fclose(f);
156+
}
157+
printf("[%s]\r\n", write_result ? "OK" : "FAIL");
158+
}
159+
160+
// Read back the data from the file and store them in data_read
161+
bool read_result = false;
162+
{
163+
printf("SD: Reading data ... ");
164+
FILE *f = fopen(sd_file_path, "r");
165+
if (f) {
166+
read_result = true;
167+
for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
168+
uint8_t data = fgetc(f);
169+
if (data != data_written[i]) {
170+
read_result = false;
171+
break;
172+
}
173+
}
174+
fclose(f);
175+
}
176+
printf("[%s]\r\n", read_result ? "OK" : "FAIL");
177+
}
178+
result = write_result && read_result;
179+
return CaseNext;
180+
}
181+
182+
183+
extern int errno;
184+
185+
/** @brief test-fseek.c test ported from glibc project
186+
*
187+
* @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
188+
*/
189+
static control_t sdcard_test_01() {
190+
FILE *fp, *fp1;
191+
int i, j;
192+
int ret = 0;
193+
194+
fp = fopen (sd_file_path, "w+");
195+
if (fp == NULL) {
196+
printf("errno=%d\n", errno);
197+
TEST_ASSERT_MESSAGE(false, "error");
198+
return CaseNext;
199+
}
200+
201+
for (i = 0; i < 256; i++) {
202+
putc (i, fp);
203+
}
204+
fp1 = freopen (sd_file_path, "r", fp);
205+
TEST_ASSERT_MESSAGE(fp1 == fp, "Error: cannot open file for reading");
206+
207+
for (i = 1; i <= 255; i++) {
208+
ret = fseek (fp, (long) -i, SEEK_END);
209+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s:Error: fseek() failed (ret=%d).\n", __func__, (int) ret);
210+
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
211+
212+
if ((j = getc (fp)) != 256 - i) {
213+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: SEEK_END failed (j=%d)\n", __func__, j);
214+
TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
215+
}
216+
ret = fseek (fp, (long) i, SEEK_SET);
217+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (ret=%d).\n", __func__, (int) ret);
218+
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
219+
220+
if ((j = getc (fp)) != i) {
221+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (j=%d).\n", __func__, j);
222+
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
223+
}
224+
if ((ret = fseek (fp, (long) i, SEEK_SET))) {
225+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (ret=%d).\n", __func__, (int) ret);
226+
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
227+
}
228+
if ((ret = fseek (fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR))) {
229+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_CUR (ret=%d).\n", __func__, (int) ret);
230+
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
231+
}
232+
if ((j = getc (fp)) != (i >= 128 ? i - 128 : i + 128)) {
233+
FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_CUR (j=%d).\n", __func__, j);
234+
TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
235+
}
236+
}
237+
fclose (fp);
238+
return CaseNext;
239+
}
240+
241+
utest::v1::status_t greentea_setup(const size_t number_of_cases)
242+
{
243+
GREENTEA_SETUP(300, "default_auto");
244+
return greentea_test_setup_handler(number_of_cases);
245+
}
246+
247+
Case cases[] = {
248+
/* 1 2 3 4 5 6 7 */
249+
/* 1234567890123456789012345678901234567890123456789012345678901234567890 */
250+
Case("FSFAT_test_00: fopen()/fgetc()/fprintf()/fclose() test.", sdcard_test_00),
251+
Case("FSFAT_test_01: fopen()/fseek()/fclose() test.", sdcard_test_01)
252+
};
253+
254+
255+
/* Declare your test specification with a custom setup handler */
256+
Specification specification(greentea_setup, cases);
257+
258+
int main()
259+
{
260+
return !Harness::run(specification);
261+
}

0 commit comments

Comments
 (0)