first commit
[clinton/Smoothieware.git] / gcc4mbed / samples / SDFileSystem / main.cpp
1 /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15 /* LocalFileSystem test modified to use SD cards instead. */
16 #include "mbed.h"
17 #include "SDFileSystem/SDFileSystem.h"
18 #include "../agutil/agutil.h"
19
20
21 extern "C" void HardFault_Handler(void)
22 {
23 DebugDumpStack();
24 error("\r\nHardFault\r\n");
25 }
26
27 extern "C" void MemManage_Handler(void)
28 {
29 DebugDumpStack();
30 error("\r\nMemManage\r\n");
31 }
32
33 extern "C" void BusFault_Handler(void)
34 {
35 DebugDumpStack();
36 error("\r\nBusFault\r\n");
37 }
38
39 extern "C" void UsageFault_Handler(void)
40 {
41 DebugDumpStack();
42 error("\r\nUsageFault\r\n");
43 }
44
45 extern "C" void SVC_Handler(void)
46 {
47 DebugDumpStack();
48 error("\r\nSVC Call\r\n");
49 }
50
51 extern "C" void DebugMon_Handler(void)
52 {
53 DebugDumpStack();
54 error("\r\nDebugMonitor\r\n");
55 }
56
57 extern "C" void PendSV_Handler(void)
58 {
59 DebugDumpStack();
60 error("\r\nPendSV\r\n");
61 }
62
63 extern "C" void SysTick_Handler(void)
64 {
65 DebugDumpStack();
66 error("\r\nSysTick");
67 }
68
69
70 void BreakHandler(void)
71 {
72 DebugDumpStack();
73 error("\r\nManual Break\r\n");
74 }
75
76
77 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
78 InterruptIn BreakInterrupt(p9);
79
80
81 int main()
82 {
83 // If you pull p9 low, then it should break into a running program and dump the stack.
84 BreakInterrupt.mode(PullUp);
85 BreakInterrupt.fall(BreakHandler);
86
87 int Result = -1;
88 char Buffer[32];
89
90 printf("\r\n\r\nGCC4MBED Test Suite\r\n");
91 printf("sdFileSystem Unit Tests\r\n");
92
93 printf("Test 1: fopen() for write\r\n");
94 FILE *fp = fopen("/sd/out.txt", "w"); // Open "out.txt" on the sd file system for writing
95 if (NULL == fp)
96 {
97 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);
98 }
99
100 printf("Test 2: fprintf()\r\n");
101 Result = fprintf(fp, "Hello World!");
102 if (Result < 0)
103 {
104 error("%s(%d) fprintf() failed\r\n", __FILE__, __LINE__);
105 }
106
107 printf("Test 3: fclose() on written file\r\n");
108 Result = fclose(fp);
109 if (0 != Result)
110 {
111 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);
112 }
113
114
115
116 printf("Test 4: fopen() for read\r\n");
117 fp = fopen("/sd/out.txt", "r");
118 if (NULL == fp)
119 {
120 error("%s(%d) fopen() failed\r\n", __FILE__, __LINE__);
121 }
122
123 printf("Test 5: fscanf()\r\n");
124 Result = fscanf(fp, "%31s", Buffer);
125 if (EOF == Result)
126 {
127 error("%s(%d) fscanf() failed\r\n", __FILE__, __LINE__);
128 }
129 printf("Contents of /sd/out.txt: %s\r\n", Buffer);
130
131 printf("Test 6: fclose() on read file\r\n");
132 Result = fclose(fp);
133 if (0 != Result)
134 {
135 error("%s(%d) fclose() failed\r\n", __FILE__, __LINE__);
136 }
137
138
139
140 printf("Test 7: remove()\r\n");
141 Result = remove("/sd/out.txt"); // Removes the file "out.txt" from the sd file system
142 if (0 != Result)
143 {
144 error("%s(%d) remove() failed\r\n", __FILE__, __LINE__);
145 }
146
147
148 printf("Test 8: opendir()\r\n");
149 DIR *d = opendir("/sd"); // Opens the root directory of the sd file system
150 printf("test\r\n");
151 if (NULL == d)
152 {
153 error("%s(%d) opendir() failed\r\n", __FILE__, __LINE__);
154 }
155 struct dirent *p;
156
157 printf("Test 9: readir() for all entries\r\n");
158 while((p = readdir(d)) != NULL)
159 { // Print the names of the files in the sd file system
160 printf("%s\r\n", p->d_name); // to stdout.
161 }
162
163 printf("Test 10: closedir\r\n");
164 closedir(d);
165
166
167
168 printf("\r\nTest completed\r\n");
169 }