1
0

util.c 5.0 KB

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