1 /* util.c */
2
3 #include <mqx.h>
4 #include <bsp.h>
5 #include <fio.h>
6 #include <mfs.h>
7 #include <sdcard.h>
8 #include <spi.h>
9 #include <part_mgr.h>
10
11 #include "util.h"
12
13 #if !BSPCFG_ENABLE_IO_SUBSYSTEM
14 #error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined \
15 non-zero in user_config.h. Please recompile BSP with this option.
16 #endif
17
18 #ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
19 #error This application requires BSP_DEFAULT_IO_CHANNEL to be not \
20 NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero \
21 in user_config.h and recompile BSP with this option.
22 #endif
23
24 #if defined BSP_SDCARD_ESDHC_CHANNEL
25 #if ! BSPCFG_ENABLE_ESDHC
26 #error This application requires BSPCFG_ENABLE_ESDHC defined \
27 non-zero in user_config.h. Please recompile libraries with \
28 this option.
29 #endif
30 #elif defined BSP_SDCARD_SDHC_CHANNEL
31 #if ! BSPCFG_ENABLE_SDHC
32 #error This application requires BSPCFG_ENABLE_SDHC defined \
33 non-zero in user_config.h. Please recompile libraries with \
34 this option.
35 #endif
36 #endif
37
38 #if defined (BSP_SDCARD_SPI_CHANNEL)
39 #define SDCARD_COM_CHANNEL BSP_SDCARD_SPI_CHANNEL
40 #elif defined (BSP_SDCARD_ESDHC_CHANNEL)
41 #define SDCARD_COM_CHANNEL BSP_SDCARD_ESDHC_CHANNEL
42 #elif defined (BSP_SDCARD_SDHC_CHANNEL)
43 #define SDCARD_COM_CHANNEL BSP_SDCARD_SDHC_CHANNEL
44 #else
45 #error "SDCARD low level communication device not defined!"
46 #endif
47
sdcard_open(MQX_FILE_PTR * com_handle,MQX_FILE_PTR * sdcard_handle,MQX_FILE_PTR * partman_handle,MQX_FILE_PTR * filesystem_handle,char * partman_name,char * filesystem_name)48 int sdcard_open(MQX_FILE_PTR *com_handle, MQX_FILE_PTR *sdcard_handle,
49 MQX_FILE_PTR *partman_handle, MQX_FILE_PTR *filesystem_handle,
50 char *partman_name, char *filesystem_name)
51 {
52 _mqx_int error_code;
53 _mqx_uint param;
54
55 /* Open low level communication device */
56 *com_handle = fopen(SDCARD_COM_CHANNEL, NULL);
57
58 if (NULL == *com_handle) {
59 printf("Error installing communication handle.\n");
60 return -60;
61 }
62
63 /* Install SD card device */
64 error_code = _io_sdcard_install("sdcard:", (void *)&_bsp_sdcard0_init,
65 *com_handle);
66 if (error_code != MQX_OK) {
67 printf("Error installing SD card device (0x%x)\n", error_code);
68 return -61;
69 }
70
71 _time_delay(200);
72
73 /* Open the device which MFS will be installed on */
74 *sdcard_handle = fopen("sdcard:", 0);
75 if (*sdcard_handle == NULL) {
76 printf("Unable to open SD card device.\n");
77 return -62;
78 }
79
80 /* Install partition manager over SD card driver */
81 error_code = _io_part_mgr_install(*sdcard_handle, partman_name, 0);
82 if (error_code != MFS_NO_ERROR) {
83 printf("Error installing partition manager: %s\n", MFS_Error_text(
84 (uint32_t) error_code));
85 return -63;
86 }
87
88 /* Open partition manager */
89 *partman_handle = fopen(partman_name, NULL);
90 if (*partman_handle == NULL) {
91 error_code = ferror(*partman_handle);
92 printf("Error opening partition manager: %s\n", MFS_Error_text(
93 (uint32_t) error_code));
94 return -64;
95 }
96
97 /* Validate partition 1 */
98 param = 1;
99 error_code = _io_ioctl(*partman_handle, IO_IOCTL_VAL_PART, ¶m);
100 if (error_code == MQX_OK) {
101 /* Install MFS over partition 1 */
102 error_code = _io_mfs_install(*partman_handle, filesystem_name, param);
103 if (error_code != MFS_NO_ERROR) {
104 printf("Error initializing MFS over partition: %s\n",
105 MFS_Error_text((uint32_t) error_code));
106 return -65;
107 }
108
109 } else {
110 /* Install MFS over SD card driver */
111 error_code = _io_mfs_install(*sdcard_handle, filesystem_name,
112 (_file_size) 0);
113 if (error_code != MFS_NO_ERROR) {
114 printf("Error initializing MFS: %s\n", MFS_Error_text(
115 (uint32_t) error_code));
116 return -66;
117 }
118 } /* end Validate partition 1 */
119
120 /* Open file system */
121 *filesystem_handle = fopen(filesystem_name, NULL);
122 error_code = ferror(*filesystem_handle);
123 if ((error_code != MFS_NO_ERROR) && (error_code != MFS_NOT_A_DOS_DISK)) {
124 printf("Error opening filesystem: %s\n", MFS_Error_text(
125 (uint32_t) error_code));
126 return -67;
127 }
128 if (error_code == MFS_NOT_A_DOS_DISK) {
129 printf("NOT A DOS DISK! You must format to continue.\n");
130 return -68;
131 }
132
133 return 0;
134 }
135
sdcard_close(MQX_FILE_PTR * sdcard_handle,MQX_FILE_PTR * partman_handle,MQX_FILE_PTR * filesystem_handle,char * partman_name,char * filesystem_name)136 int sdcard_close(MQX_FILE_PTR *sdcard_handle, MQX_FILE_PTR *partman_handle,
137 MQX_FILE_PTR *filesystem_handle,
138 char *partman_name, char *filesystem_name)
139 {
140 _mqx_int error_code;
141
142 /* Close the filesystem */
143 if (MQX_OK != fclose(*filesystem_handle)) {
144 printf("Error closing filesystem.\n");
145 return -69;
146 }
147 *filesystem_handle = NULL;
148
149 /* Uninstall MFS */
150 error_code = _io_dev_uninstall(filesystem_name);
151 if (error_code != MFS_NO_ERROR) {
152 printf("Error uninstalling filesystem.\n");
153 return -70;
154 }
155
156 /* Close partition manager */
157 if (MQX_OK != fclose(*partman_handle)) {
158 printf("Unable to close partition manager.\n");
159 return -71;
160 }
161 *partman_handle = NULL;
162
163 /* Uninstall partition manager */
164 error_code = _io_dev_uninstall(partman_name);
165 if (error_code != MFS_NO_ERROR) {
166 printf("Error uninstalling partition manager.\n");
167 return -72;
168 }
169
170 /* Close the SD card device */
171 if (MQX_OK != fclose(*sdcard_handle)) {
172 printf("Unable to close SD card device.\n");
173 return -73;
174 }
175 *sdcard_handle = NULL;
176
177 return 0;
178 }
179
180 /* EOF */
181