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