/*TASK*----------------------------------------------------- * * Task Name : Ram_disk_start * Comments : Open device and install MFS on device * * *END*-----------------------------------------------------*/ void Ram_disk_start(void) { /* Body */ int dev_fd, a_fd_ptr; int32_t error_code; _mqx_uint mqx_status; uint32_t isFormatted = 0; const NIO_MEM_INIT_DATA_STRUCT nmid = { (uint32_t)RAM_DISK_BASE, (uint32_t)RAM_DISK_SIZE }; a_fd_ptr = 0; /* install device */ if (NULL == _nio_dev_install("mfs_ramdisk:", &nio_mem_dev_fn, (void *)&nmid, NULL)) { /* Number of sectors is returned by ioctl IO_IOCTL_GET_NUM_SECTORS function */ /* If another disc structure is desired, use MFS_FORMAT_DATA structure to */ /* define it and call standart format function instead default_format */ printf("\nError installing memory device"); _task_block(); } /* Endif */ /* Open the device which MFS will be installed on */ dev_fd = open("mfs_ramdisk:", 0); if ( dev_fd < 0 ) { printf("\nUnable to open Ramdisk device"); _task_block(); } /* Endif */ /* Install MFS */ mqx_status = _io_mfs_install(dev_fd, "a:", (_file_size)0); if (mqx_status != MFS_NO_ERROR) { printf("\nError initializing a:"); _task_block(); } else { printf("\nInitialized Ram Disk to a:\\"); } /* Endif */ /* Open the filesystem and format detect, if format is required */ a_fd_ptr = open("a:", O_RDWR); if (0 > a_fd_ptr) { printf("\nError while opening a:\\ (%s)", MFS_Error_text((uint32_t)errno)); _task_block(); } _io_register_file_system(a_fd_ptr, "a:"); /* We check if the device is formatted with the ioctl command.*/ error_code = ioctl(a_fd_ptr, IO_IOCTL_CHECK_FORMATTED, &isFormatted); if (0 > error_code) { printf("\nError while accessing a:\\ (%s)", MFS_Error_text((uint32_t)error_code)); _task_block(); } else{ if(!isFormatted){ printf("\nNOT A DOS DISK! You must format to continue."); } } } /* Endbody */
/*TASK*----------------------------------------------------- * * Task Name : Ram_disk_start * Comments : Open device and install MFS on device * * *END*-----------------------------------------------------*/ void Ram_disk_start(void) { /* Body */ MQX_FILE_PTR dev_handle1, a_fd_ptr; int_32 error_code; _mqx_uint mqx_status; a_fd_ptr = 0; /* install device */ mqx_status = _io_mem_install("mfs_ramdisk:", (uchar_ptr)RAM_DISK_BASE, (_file_size)RAM_DISK_SIZE); /* Number of sectors is returned by ioctl IO_IOCTL_GET_NUM_SECTORS function */ /* If another disc structure is desired, use MFS_FORMAT_DATA structure to */ /* define it and call standart format function instead default_format */ if ( mqx_status != MQX_OK ) { printf("\nError installing memory device (0x%x)", mqx_status); _task_block(); } /* Endif */ /* Open the device which MFS will be installed on */ dev_handle1 = fopen("mfs_ramdisk:", 0); if ( dev_handle1 == NULL ) { printf("\nUnable to open Ramdisk device"); _task_block(); } /* Endif */ /* Install MFS */ mqx_status = _io_mfs_install(dev_handle1, "a:", (_file_size)0); if (mqx_status != MFS_NO_ERROR) { printf("\nError initializing a:"); _task_block(); } else { printf("\nInitialized Ram Disk to a:\\"); } /* Endif */ /* Open the filesystem and format detect, if format is required */ a_fd_ptr = fopen("a:", NULL); error_code = ferror(a_fd_ptr); if ((error_code != MFS_NO_ERROR) && (error_code != MFS_NOT_A_DOS_DISK)) { printf("\nError while opening a:\\ (%s)", MFS_Error_text((uint_32)(uint_32)error_code)); _task_block(); } /* Endif */ if ( error_code == MFS_NOT_A_DOS_DISK ) { printf("\nNOT A DOS DISK! You must format to continue."); } /* Endif */ } /* Endbody */
/*TASK*----------------------------------------------------------------- * * Function Name : sdcard_task * Returned Value : void * Comments : * *END------------------------------------------------------------------*/ void sdcard_task(uint_32 temp) { boolean inserted = TRUE, readonly = FALSE, last = FALSE; _mqx_int error_code; _mqx_uint param; MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partman_handle; char filesystem_name[] = "a:";// 文件系统 名字 char partman_name[] = "pm:";// 分区管理 名字 #if defined BSP_SDCARD_GPIO_DETECT #if ! BSP_SDCARD_GPIO_DETECT_INT LWGPIO_STRUCT sd_detect; #else LWGPIO_STRUCT sd_detect; /************************dx 20121210********************************************** */ GPIO_PIN_STRUCT sd_detect_int[] = { BSP_SDCARD_GPIO_DETECT | GPIO_PIN_IRQ_FALLING | GPIO_PIN_IRQ_RISING, GPIO_LIST_END }; MQX_FILE_PTR port_file_sd_detect_int; /***********************************************************************************/ #endif #endif #if defined BSP_SDCARD_GPIO_PROTECT LWGPIO_STRUCT sd_protect; #endif #ifdef BSP_SDCARD_GPIO_CS LWGPIO_STRUCT sd_cs; SPI_CS_CALLBACK_STRUCT callback; #endif /* Open low level communication device */ com_handle = fopen (SDCARD_COM_CHANNEL, NULL);// 打开"esdhc:"模块底层驱动 if (NULL == com_handle) { printf("Error installing communication handle.\n"); _task_block(); } #ifdef BSP_SDCARD_GPIO_CS /* Open GPIO file for SPI CS signal emulation */ error_code = lwgpio_init(&sd_cs, BSP_SDCARD_GPIO_CS, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);// 安装简单引脚用于spi片选 if (!error_code) { printf("Initializing GPIO with associated pins failed.\n"); _task_block(); } lwgpio_set_functionality(&sd_cs,BSP_SDCARD_CS_MUX_GPIO); lwgpio_set_attribute(&sd_cs, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); /* Set CS callback */ callback.MASK = BSP_SDCARD_SPI_CS; callback.CALLBACK = set_CS; callback.USERDATA = &sd_cs; if (SPI_OK != ioctl (com_handle, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback)) { printf ("Setting CS callback failed.\n"); _task_block(); } #endif #if defined BSP_SDCARD_GPIO_DETECT #if ! BSP_SDCARD_GPIO_DETECT_INT /* Init GPIO pins for other SD card signals */ error_code = lwgpio_init(&sd_detect, BSP_SDCARD_GPIO_DETECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);// 安装简单引脚用于插入检测 if (!error_code) { printf("Initializing GPIO with sdcard detect pin failed.\n"); _task_block(); } /*Set detect and protect pins as GPIO Function */ lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO); lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #else /******************dx 20121210***********************************************/ if (NULL == (port_file_sd_detect_int = fopen("gpio:read", (char_ptr) &sd_detect_int ))) { printf("Opening port_file_sd_detect_int GPIO with associated button1_int failed.\n"); _task_block(); } ioctl(port_file_sd_detect_int, GPIO_IOCTL_SET_IRQ_FUNCTION, (pointer)sd_detect_int__callback); _lwevent_create(&SD_Event,0); // 创建轻量级事件用于检测SD插入 /****************************************************************************/ #endif #endif #if defined BSP_SDCARD_GPIO_PROTECT /* Init GPIO pins for other SD card signals */ error_code = lwgpio_init(&sd_protect, BSP_SDCARD_GPIO_PROTECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE);// 安装简单引脚用于只读检测 if (!error_code) { printf("Initializing GPIO with sdcard protect pin failed.\n"); _task_block(); } /*Set detect and protect pins as GPIO Function */ lwgpio_set_functionality(&sd_protect,BSP_SDCARD_PROTECT_MUX_GPIO); lwgpio_set_attribute(&sd_protect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif /* Install SD card device */ error_code = _io_sdcard_install("sdcard:", (pointer)&_bsp_sdcard0_init, com_handle);// 安装SD卡驱动,基于"esdhc:"模块 if ( error_code != MQX_OK ) { printf("Error installing SD card device (0x%x)\n", error_code); _task_block(); } /*使用内部IOCTL命令测试SD卡是否已经插入 dx20121223*/ uint_32 sd_param; sd_param = 0; if (ESDHC_OK != ioctl (com_handle, IO_IOCTL_ESDHC_GET_CARD, &sd_param)) { printf("Error get SD card device type.\n"); _task_block(); } if (ESDHC_CARD_NONE != sd_param) { inserted = TRUE; // 已插入SD卡 printf("inserted = 1 -> %d\n",(int)sd_param); }else { inserted = FALSE; // 无SD卡 printf("inserted = 0 -> %d\n",(int)sd_param); } for (;;)/////////////////////////////////////////////////////////////////////////////循环 { #if defined BSP_SDCARD_GPIO_PROTECT /* Get value of protect pin */ readonly = lwgpio_get_value(&sd_protect);// 检测磁盘是否只读//////////////////////////////////////// #endif #ifdef BSP_MPC8308RDB /* Set function as SD_CD which indicate that card is present in Present State Register */ lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_SD_CD); lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif if (last != inserted) { if (inserted)// 插入磁盘 { // _time_delay (200);// 将活动的任务挂起指定的毫秒数 /* Open the device which MFS will be installed on */ sdcard_handle = fopen("sdcard:", 0);// 打开SD卡层驱动 if ( sdcard_handle == NULL ) { printf("Unable to open SD card device.\n"); _task_block(); } /* Set read only flag as needed */ param = 0; if (readonly) { param = IO_O_RDONLY; } if (IO_OK != ioctl(sdcard_handle, IO_IOCTL_SET_FLAGS, (char_ptr) ¶m)) { printf("Setting device read only failed.\n"); _task_block(); } /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(sdcard_handle, partman_name, 0);// 分区管理 if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } /* Open partition manager */ partman_handle = fopen(partman_name, NULL);// 打开分区 if (partman_handle == NULL) { error_code = ferror(partman_handle); printf("Error opening partition manager: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } /* Validate partition 1 */ param = 1; error_code = _io_ioctl(partman_handle, IO_IOCTL_VAL_PART, ¶m);// 控制分区 if (error_code == MQX_OK) { /* Install MFS over partition 1 */ error_code = _io_mfs_install(partman_handle, filesystem_name, param);/////安装文件系统,基于分区 if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } } else { /* Install MFS over SD card driver */ error_code = _io_mfs_install(sdcard_handle, filesystem_name, (_file_size)0);////////安装文件系统,无分区 if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } } /* Open file system */ filesystem_handle = fopen(filesystem_name, NULL);// 打开文件系统 error_code = ferror (filesystem_handle); if ((error_code != MFS_NO_ERROR) && (error_code != MFS_NOT_A_DOS_DISK)) { printf("Error opening filesystem: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } if ( error_code == MFS_NOT_A_DOS_DISK ) { printf("NOT A DOS DISK! You must format to continue.\n"); } printf ("SD card installed to %s\n", filesystem_name); if (readonly) { printf ("SD card is locked (read only).\n"); } } else // 拔出磁盘 { /* Close the filesystem */ if (MQX_OK != fclose (filesystem_handle)) { printf("Error closing filesystem.\n"); _task_block(); } filesystem_handle = NULL; /* Uninstall MFS */ error_code = _io_dev_uninstall(filesystem_name); if (error_code != MFS_NO_ERROR) { printf("Error uninstalling filesystem.\n"); _task_block(); } /* Close partition manager */ if (MQX_OK != fclose (partman_handle)) { printf("Unable to close partition manager.\n"); _task_block(); } partman_handle = NULL; /* Uninstall partition manager */ error_code = _io_dev_uninstall(partman_name); if (error_code != MFS_NO_ERROR) { printf("Error uninstalling partition manager.\n"); _task_block(); } /* Close the SD card device */ if (MQX_OK != fclose (sdcard_handle)) { printf("Unable to close SD card device.\n"); _task_block(); } sdcard_handle = NULL; printf ("SD card uninstalled.\n"); printf ("sd unOK dx.\n");/////////////////////////dx } } last = inserted; // _time_delay (200);// 将活动的任务挂起指定的毫秒数 // #if defined BSP_SDCARD_GPIO_DETECT // #if BSP_SDCARD_GPIO_DETECT_INT // /****************dx 20121211************************************************************/ // _lwevent_wait_ticks(&SD_Event,SD_EVENT_DETECT,FALSE,0);///////////等待事件USB_EVENT // _lwevent_clear(&SD_Event,SD_EVENT_DETECT); // /****************************************************************************************/ // #endif // #endif /*使用SD外部端口测试SD卡是否已经插入*/ #if defined BSP_SDCARD_GPIO_DETECT #ifdef BSP_MPC8308RDB /* Set function as GPIO to detect sdcard */ lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO); lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif #if ! BSP_SDCARD_GPIO_DETECT_INT inserted = !lwgpio_get_value(&sd_detect);// 检测磁盘是否插入///////////////////////////////////////// #else /****************dx 20121211************************************************************/ _lwevent_wait_ticks(&SD_Event,SD_EVENT_DETECT,FALSE,0);///////////等待事件USB_EVENT _lwevent_clear(&SD_Event,SD_EVENT_DETECT); ioctl(port_file_sd_detect_int, GPIO_IOCTL_READ, (char_ptr) &sd_detect_int); inserted = !( sd_detect_int[0] & GPIO_PIN_STATUS); /****************************************************************************************/ #endif #endif }/////////////////////////////////////////////////////////////////////////////循环 }
pointer usb_filesystem_install( pointer usb_handle, char_ptr block_device_name, char_ptr partition_manager_name, char_ptr file_system_name ) { uint_32 partition_number; uchar_ptr dev_info; int_32 error_code; uint_32 mfs_status; USB_FILESYSTEM_STRUCT_PTR usb_fs_ptr; usb_fs_ptr = _mem_alloc_system_zero(sizeof(USB_FILESYSTEM_STRUCT)); if (usb_fs_ptr==NULL) { return NULL; } _io_usb_mfs_install(block_device_name, 0, (pointer)usb_handle); usb_fs_ptr->DEV_NAME = block_device_name; /* Open the USB mass storage device */ _time_delay(500); usb_fs_ptr->DEV_FD_PTR = fopen(block_device_name, (char_ptr) 0); if (usb_fs_ptr->DEV_FD_PTR == NULL) { printf("\nUnable to open USB disk"); usb_filesystem_uninstall(usb_fs_ptr); return NULL; } _io_ioctl(usb_fs_ptr->DEV_FD_PTR, IO_IOCTL_SET_BLOCK_MODE, NULL); /* get the vendor information and display it */ printf("\n************************************************************************"); _io_ioctl(usb_fs_ptr->DEV_FD_PTR, IO_IOCTL_GET_VENDOR_INFO, &dev_info); printf("\nVendor Information: %-1.8s Mass Storage Device",dev_info); _io_ioctl(usb_fs_ptr->DEV_FD_PTR, IO_IOCTL_GET_PRODUCT_ID, &dev_info); printf("\nProduct Identification: %-1.16s",dev_info); _io_ioctl(usb_fs_ptr->DEV_FD_PTR, IO_IOCTL_GET_PRODUCT_REV, &dev_info); printf("\nProduct Revision Level: %-1.4s",dev_info); printf("\n************************************************************************"); /* Try Installing a the partition manager */ error_code = _io_part_mgr_install(usb_fs_ptr->DEV_FD_PTR, partition_manager_name, 0); if (error_code != MFS_NO_ERROR) { printf("\nError while initializing (%s)", MFS_Error_text((uint_32)error_code)); usb_filesystem_uninstall(usb_fs_ptr); return NULL; } usb_fs_ptr->PM_NAME = partition_manager_name; usb_fs_ptr->PM_FD_PTR = fopen(partition_manager_name, NULL); if (usb_fs_ptr->PM_FD_PTR == NULL) { error_code = ferror(usb_fs_ptr->PM_FD_PTR); printf("\nError while opening partition (%s)", MFS_Error_text((uint_32)error_code)); usb_filesystem_uninstall(usb_fs_ptr); return NULL; } printf("\n--->USB Mass storage device opened"); partition_number = 1; error_code = _io_ioctl(usb_fs_ptr->PM_FD_PTR, IO_IOCTL_VAL_PART, &partition_number); if (error_code == PMGR_INVALID_PARTITION) { printf("\n--->No partition available on this device"); /* uninitialize */ fclose(usb_fs_ptr->PM_FD_PTR); usb_fs_ptr->PM_FD_PTR = NULL; _io_part_mgr_uninstall(usb_fs_ptr->PM_NAME); usb_fs_ptr->PM_NAME = NULL; /* install MFS without partition */ mfs_status = _io_mfs_install(usb_fs_ptr->DEV_FD_PTR, file_system_name, 0); } else { printf("\n--->Partition Manager installed"); /* Install MFS on the partition #1 */ mfs_status = _io_mfs_install(usb_fs_ptr->PM_FD_PTR, file_system_name, partition_number); } if (mfs_status != MFS_NO_ERROR) { printf("\nError initializing MFS (%s)", MFS_Error_text((uint_32)mfs_status)); /* uninitialize and exit */ usb_filesystem_uninstall(usb_fs_ptr); return NULL; } printf("\n--->File System installed"); usb_fs_ptr->FS_NAME = file_system_name; usb_fs_ptr->FS_FD_PTR = fopen(file_system_name, 0); if (usb_fs_ptr->FS_FD_PTR==NULL) { usb_filesystem_uninstall(usb_fs_ptr); return NULL; } printf("\n--->File System opened"); return (pointer) usb_fs_ptr; }
/*TASK*----------------------------------------------------- * * Task Name : sdcard_task * Comments : Open device and install MFS on device * * *END*-----------------------------------------------------*/ void sdcard_task(void) { /* Body */ int dev_fd = -1, a_fd = -1; bool inserted = FALSE, last = FALSE; int32_t error_code; uint32_t isFormatted = 0; int esdhc_handle = -1; char partman_name[] = "pm:"; char partition_name[] = "pm:1"; int partition_handle; SDCARD_INIT_STRUCT sdcard_init_data; LWSEM_STRUCT lwsem; /* Create and post card detection semaphore */ if( _lwsem_create(&lwsem, 0) != MQX_OK ){ printf("\nlwsem create failed!"); _task_block(); } if( _lwsem_post(&lwsem) != MQX_OK ){ printf("\nlwsem post failed!"); _task_block(); } #ifdef BOARD_SDHC_GPIO_INSTANCE configure_gpio_pins(GPIO_EXTRACT_PORT(BOARD_SDHC_GPIO_INSTANCE)); #endif /* initialize SDHC pins */ configure_sdhc_pins(BOARD_SDHC_INSTANCE); /* install SDHC low-level driver */ _nio_dev_install("esdhc:", &nio_esdhc_dev_fn, (void*)&_bsp_esdhc_init, NULL); /* get an instance of the SDHC driver */ esdhc_handle = open("esdhc:", 0); if(esdhc_handle < 0) { printf("\nCould not open esdhc!"); _task_block(); } /* prepare init data structure */ sdcard_init_data.com_dev = esdhc_handle; sdcard_init_data.const_data = (SDCARD_CONST_INIT_STRUCT_PTR)&sdcard_esdhc_init_data; /* install device */ if (_nio_dev_install("sdcard:", &nio_sdcard_dev_fn, (void*)&sdcard_init_data, NULL) == NULL) { /* Number of sectors is returned by ioctl IO_IOCTL_GET_NUM_SECTORS function */ /* If another disc structure is desired, use MFS_FORMAT_DATA structure to */ /* define it and call standart format function instead default_format */ printf("\nError installing memory device"); _task_block(); } /* Endif */ /* install isr for card detection handling and initialize gpio pin */ _int_install_isr(g_portIrqId[GPIO_EXTRACT_PORT(CDET_PIN->pinName)], card_detect_isr, &lwsem); GPIO_DRV_InputPinInit(CDET_PIN); for(;;){ /* Wait for card insertion or removal */ if( _lwsem_wait(&lwsem) != MQX_OK ){ printf("\nlwsem_wait failed!"); _task_block(); } /* Check if card is present */ if(CDET_PIN->config.pullSelect == kPortPullDown) inserted = GPIO_DRV_ReadPinInput(CDET_PIN->pinName); else inserted = !GPIO_DRV_ReadPinInput(CDET_PIN->pinName); if(last != inserted){ last = inserted; /* Card detection switch debounce delay */ _time_delay(100); if(inserted) { /* Open the device which MFS will be installed on */ dev_fd = open("sdcard:", O_RDWR); if ( dev_fd <= 0 ) { printf("\nUnable to open SDCARD device"); _task_block(); } /* Endif */ /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(dev_fd, partman_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint32_t)error_code)); _task_block(); } /* Open partition */ partition_handle = open(partition_name, O_RDWR); if (partition_handle >= 0) { printf("Installing MFS over partition...\n"); /* Validate partition */ error_code = ioctl(partition_handle, IO_IOCTL_VAL_PART, NULL); if (error_code != MFS_NO_ERROR) { printf("Error validating partition: %s\n", MFS_Error_text((uint32_t)error_code)); printf("Not installing MFS.\n"); _task_block(); } /* Install MFS over partition */ error_code = _io_mfs_install(partition_handle, "a:", 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint32_t)error_code)); } } else { printf("Installing MFS over SD card driver...\n"); /* Install MFS over SD card driver */ error_code = _io_mfs_install(dev_fd, "a:", (_file_size)0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint32_t)error_code)); } } /* Open the filesystem and format detect, if format is required */ a_fd = open("a:", O_RDWR); if (0 >= a_fd) { printf("\nError while opening a:\\ (%s)", MFS_Error_text((uint32_t)errno)); _task_block(); } _io_register_file_system(a_fd, "a:"); /* We check if the device is formatted with the ioctl command. */ error_code = ioctl(a_fd, IO_IOCTL_CHECK_FORMATTED, &isFormatted); if (0 > error_code) { printf("\nError while accessing a:\\ (%s)", MFS_Error_text((uint32_t)error_code)); _task_block(); } else{ if(!isFormatted){ printf("\nNOT A DOS DISK! You must format to continue."); } } } else { /* Close the filesystem */ if ((a_fd >= 0) && (MQX_OK != close(a_fd))) { printf("Error closing filesystem.\n"); } a_fd = -1; /* Force uninstall filesystem */ error_code = _nio_dev_uninstall_force("a:", NULL); if (error_code < 0) { printf("Error uninstalling filesystem.\n"); } /* Close partition */ if ((partition_handle >= 0) && (MQX_OK != close(partition_handle))) { printf("Error closing partition.\n"); } partition_handle = -1; /* Uninstall partition manager */ error_code = _nio_dev_uninstall_force(partman_name, NULL); if (error_code < 0) { printf("Error uninstalling partition manager.\n"); } /* Close the SD card device */ if ((dev_fd >= 0) && (MQX_OK != close(dev_fd))) { printf("Error closing SD card device.\n"); } dev_fd = -1; printf ("SD card uninstalled.\n"); } } } } /* Endbody */
int mount_sdcard() { _mqx_uint param; _mqx_int error_code; //MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partman_handle; boolean readonly = /*FALSE*/TRUE; // char filesystem_name[] = "a:"; // char partman_name[] = "pm:"; printf("mounte sd card +\n"); _lwsem_wait(&SD_MFS_IO_SEM); if(sd_mounted) { _lwsem_post(&SD_MFS_IO_SEM); return 0; } do { /* Open the device which MFS will be installed on */ sdcard_handle = fopen("sdcard:", 0); if ( sdcard_handle == NULL ) { printf("Unable to open SD card device 2.\n"); //_task_block(); continue; /* retry it */ } /* Set read only flag as needed */ param = 0; if (readonly) { param = IO_O_RDONLY; } if (IO_OK != ioctl(sdcard_handle, IO_IOCTL_SET_FLAGS, (char_ptr) ¶m)) { printf("Setting device read only failed.\n"); _task_block(); } /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(sdcard_handle, partman_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint_32)error_code)); //_task_block(); continue; /* retry it */ } /* Open partition manager */ partman_handle = fopen(partman_name, NULL); if (partman_handle == NULL) { error_code = ferror(partman_handle); printf("Error opening partition manager sd: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } /* Validate partition 1 */ param = 1; error_code = _io_ioctl(partman_handle, IO_IOCTL_VAL_PART, ¶m); if (error_code == MQX_OK) { /* Install MFS over partition 1 */ error_code = _io_mfs_install(partman_handle, filesystem_name, param); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } } else { /* Install MFS over SD card driver */ error_code = _io_mfs_install(sdcard_handle, filesystem_name, (_file_size)0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint_32)error_code)); _task_block(); } } /* Open file system */ filesystem_handle = fopen(filesystem_name, NULL); error_code = ferror (filesystem_handle); if ((error_code != MFS_NO_ERROR) && (error_code != MFS_NOT_A_DOS_DISK)) { printf("Error opening filesystem: %s\n", MFS_Error_text((uint_32)error_code)); //_task_block(); continue; /* retry it */ } if ( error_code == MFS_NOT_A_DOS_DISK ) { printf("NOT A DOS DISK! You must format to continue.\n"); } printf ("\n SD card installed to \"%s\"\n", filesystem_name); if (readonly) { printf ("SD card is locked (read only).\n"); } mfs_dir_reset(); } while(0); sd_mounted = 1; printf("mounted sd card -\n"); _lwsem_post(&SD_MFS_IO_SEM); return 0; }
/*TASK*----------------------------------------------------- * * Task Name : mem_flash_app * Comments : * This task finds a string at 'the end' of flash memory * then it tries to rewrite it. The successfull rewrite * can be proved by re-executing this example application. * *END*-----------------------------------------------------*/ void mem_flash_app ( uint_32 initial_data ) { ///////////////////////////////////////////////////////////// Flashx mfs demo =0x00060000~0x00080000#dx 20130103 #define FLASH_NAME_B "flashx:" //FLASH_NAME char flashx_filesystem_name[] = "f:",flashx_partman_name[]="pmb:";///////////////////安装盘符 MQX_FILE_PTR flashx_handle,flashx_file_handle,flashx_partman_handle; _mqx_uint param; uint_32 error; printf("\nMQX Flash Task."); /* wk --> open flash device */ flashx_handle = fopen(FLASH_NAME_B, NULL); // "flashx:bank0" /*wk@130330 --> test mfs function */ uchar result; // char pathname[261]; // ioctl(mfs_fs_ptr, IO_IOCTL_GET_CURRENT_DIR,(uint_32_ptr) pathname); // printf("The current directory is: %s\n", pathname); if (flashx_handle == NULL) { printf("\nUnable to open file %s", FLASH_NAME_B); _task_block(); } else { printf("\nFlash file %s opened", FLASH_NAME_B); } ioctl(flashx_handle, FLASH_IOCTL_ENABLE_SECTOR_CACHE, NULL);/////////////////////重要 // int len; // len = write(flashx_handle, "world",strlen("world")); // fseek(flashx_handle, 0, IO_SEEK_SET); // // fseek(flashx_handle, -32, IO_SEEK_END); // len = write(flashx_handle, "hello",strlen("hello")); /* wk --> 安装分区 */ error = _io_part_mgr_install(flashx_handle, flashx_partman_name, 0);// 分区管理 if (error!= MFS_NO_ERROR) { printf("\nError installing partition manager: %s", MFS_Error_text((uint_32)error)); _task_block(); } /* Open partition manager */ flashx_partman_handle = fopen(flashx_partman_name, NULL);// 打开分区 if (flashx_partman_handle == NULL) { error = ferror(flashx_partman_handle); printf("\nError opening partition manager: %s", MFS_Error_text((uint_32)error)); _task_block(); } /* Validate partition 1 */ param = 1; error = _io_ioctl(flashx_partman_handle, IO_IOCTL_VAL_PART, ¶m);// 控制分区 if (error == MQX_OK) { /* Install MFS over Flashx driver */ error = _io_mfs_install(flashx_partman_handle, flashx_filesystem_name, param);// 安装文件系统,基于分区 if (error != MFS_NO_ERROR) { printf("\nError initializing MFS: %s", MFS_Error_text((uint_32)error)); _task_block(); } } else { /* Install MFS over SD card driver */ error = _io_mfs_install(flashx_handle, flashx_filesystem_name, (_file_size)0);// 安装文件系统,无分区 if (error != MFS_NO_ERROR) { printf("\nError initializing MFS: %s", MFS_Error_text((uint_32)error)); _task_block(); } } /* Open file system */ flashx_file_handle = fopen(flashx_filesystem_name, NULL);// 打开文件系统 error = ferror (flashx_file_handle); if ((error != MFS_NO_ERROR) && (error != MFS_NOT_A_DOS_DISK)) { printf("\nError opening filesystem: %s", MFS_Error_text((uint_32)error)); _task_block(); } if ( error == MFS_NOT_A_DOS_DISK )////////////////// { printf("\nNOT A DOS DISK! You must format to continue."); } printf ("\nFlashx installed to %s", flashx_filesystem_name); ////////////////////////////////////////////////////////////////#dx 20130103 _task_block(); //_task_destroy(_task_get_id()); }
/*TASK*----------------------------------------------------------------- * * Function Name : sdcard_task * Returned Value : void * Comments : * *END------------------------------------------------------------------*/ void sdcard_task(uint_32 temp) { boolean inserted = TRUE, readonly = FALSE, last = FALSE; _mqx_int error_code; _mqx_uint param; MQX_FILE_PTR com_handle, sdcard_handle, filesystem_handle, partition_handle; char filesystem_name[] = "a:"; char partman_name[] = "pm:"; char partition_name[] = "pm:1"; #if defined BSP_SDCARD_GPIO_DETECT LWGPIO_STRUCT sd_detect; #endif #if defined BSP_SDCARD_GPIO_PROTECT LWGPIO_STRUCT sd_protect; #endif #ifdef BSP_SDCARD_GPIO_CS LWGPIO_STRUCT sd_cs; SPI_CS_CALLBACK_STRUCT callback; #endif /* Open low level communication device */ com_handle = fopen(SDCARD_COM_CHANNEL, (void *)(SPI_FLAG_FULL_DUPLEX)); if (NULL == com_handle) { printf("Error opening communication handle %s, check your user_config.h.\n", SDCARD_COM_CHANNEL); _task_block(); } #ifdef BSP_SDCARD_GPIO_CS /* Open GPIO file for SPI CS signal emulation */ error_code = lwgpio_init(&sd_cs, BSP_SDCARD_GPIO_CS, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE); if (!error_code) { printf("Initializing GPIO with associated pins failed.\n"); _task_block(); } lwgpio_set_functionality(&sd_cs,BSP_SDCARD_CS_MUX_GPIO); lwgpio_set_attribute(&sd_cs, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); /* Set CS callback */ callback.MASK = BSP_SDCARD_SPI_CS; callback.CALLBACK = set_CS; callback.USERDATA = &sd_cs; if (SPI_OK != ioctl(com_handle, IO_IOCTL_SPI_SET_CS_CALLBACK, &callback)) { printf ("Setting CS callback failed.\n"); _task_block(); } #endif #if defined BSP_SDCARD_GPIO_DETECT /* Init GPIO pins for other SD card signals */ error_code = lwgpio_init(&sd_detect, BSP_SDCARD_GPIO_DETECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE); if (!error_code) { printf("Initializing GPIO with sdcard detect pin failed.\n"); _task_block(); } /*Set detect and protect pins as GPIO Function */ lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO); lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif #if defined BSP_SDCARD_GPIO_PROTECT /* Init GPIO pins for other SD card signals */ error_code = lwgpio_init(&sd_protect, BSP_SDCARD_GPIO_PROTECT, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE); if (!error_code) { printf("Initializing GPIO with sdcard protect pin failed.\n"); _task_block(); } /*Set detect and protect pins as GPIO Function */ lwgpio_set_functionality(&sd_protect,BSP_SDCARD_PROTECT_MUX_GPIO); lwgpio_set_attribute(&sd_protect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif /* Install SD card device */ error_code = _io_sdcard_install("sdcard:", (pointer)&_bsp_sdcard0_init, com_handle); if ( error_code != MQX_OK ) { printf("Error installing SD card device (0x%x)\n", error_code); _task_block(); } for (;;) { #if defined BSP_SDCARD_GPIO_DETECT #ifdef BSP_MPC8308RDB /* Set function as GPIO to detect sdcard */ lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_GPIO); lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif inserted = !lwgpio_get_value(&sd_detect); #endif #if defined BSP_SDCARD_GPIO_PROTECT /* Get value of protect pin */ readonly = lwgpio_get_value(&sd_protect); #endif #ifdef BSP_MPC8308RDB /* Set function as SD_CD which indicate that card is present in Present State Register */ lwgpio_set_functionality(&sd_detect,BSP_SDCARD_DETECT_MUX_SD_CD); lwgpio_set_attribute(&sd_detect, LWGPIO_ATTR_PULL_UP, LWGPIO_AVAL_ENABLE); #endif if (last != inserted) { last = inserted; if (inserted) { _time_delay (200); /* Open the device which MFS will be installed on */ sdcard_handle = fopen("sdcard:", 0); if (sdcard_handle == NULL) { printf("Unable to open SD card device.\n"); continue; } /* Set read only flag as needed */ param = 0; if (readonly) { param = IO_O_RDONLY; } if (IO_OK != ioctl(sdcard_handle, IO_IOCTL_SET_FLAGS, (char_ptr) ¶m)) { printf("Setting device read only failed.\n"); continue; } /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(sdcard_handle, partman_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint_32)error_code)); continue; } /* Open partition */ partition_handle = fopen(partition_name, NULL); if (partition_handle != NULL) { printf("Installing MFS over partition...\n"); /* Validate partition */ error_code = _io_ioctl(partition_handle, IO_IOCTL_VAL_PART, NULL); if (error_code != MFS_NO_ERROR) { printf("Error validating partition: %s\n", MFS_Error_text((uint_32)error_code)); printf("Not installing MFS.\n"); continue; } /* Install MFS over partition */ error_code = _io_mfs_install(partition_handle, filesystem_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint_32)error_code)); } } else { printf("Installing MFS over SD card driver...\n"); /* Install MFS over SD card driver */ error_code = _io_mfs_install(sdcard_handle, filesystem_name, (_file_size)0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint_32)error_code)); } } /* Open file system */ if (error_code == MFS_NO_ERROR) { filesystem_handle = fopen(filesystem_name, NULL); error_code = ferror(filesystem_handle); if (error_code == MFS_NOT_A_DOS_DISK) { printf("NOT A DOS DISK! You must format to continue.\n"); } else if (error_code != MFS_NO_ERROR) { printf("Error opening filesystem: %s\n", MFS_Error_text((uint_32)error_code)); continue; } printf ("SD card installed to %s\n", filesystem_name); if (readonly) { printf ("SD card is locked (read only).\n"); } } } else { /* Close the filesystem */ if ((filesystem_handle != NULL) && (MQX_OK != fclose(filesystem_handle))) { printf("Error closing filesystem.\n"); } filesystem_handle = NULL; /* Uninstall MFS */ error_code = _io_dev_uninstall(filesystem_name); if (error_code != MFS_NO_ERROR) { printf("Error uninstalling filesystem.\n"); } /* Close partition */ if ((partition_handle != NULL) && (MQX_OK != fclose(partition_handle))) { printf("Error closing partition.\n"); } partition_handle = NULL; /* Uninstall partition manager */ error_code = _io_dev_uninstall(partman_name); if (error_code != MFS_NO_ERROR) { printf("Error uninstalling partition manager.\n"); } /* Close the SD card device */ if ((sdcard_handle != NULL) && (MQX_OK != fclose(sdcard_handle))) { printf("Unable to close SD card device.\n"); } sdcard_handle = NULL; printf ("SD card uninstalled.\n"); } } _time_delay (200); } }
/*TASK*----------------------------------------------------------------- * * Function Name : CF_filesystem_install * Returned Value : void * Comments : Install filesystem on given pccard file pointer * *END------------------------------------------------------------------*/ FS_STRUCT_PTR CF_filesystem_install( MQX_FILE_PTR pccard_fp, char_ptr partition_manager_name, char_ptr file_system_name ) { uint_32 partition_number; int_32 error_code; uint_32 mfs_status; FS_STRUCT_PTR fs_ptr; fs_ptr = _mem_alloc_system_zero(sizeof(FS_STRUCT)); if (fs_ptr==NULL) { return NULL; } /* Open flash device */ fs_ptr->DEV_FD_PTR = fopen("pcflasha:", (char_ptr) pccard_fp); if ( fs_ptr->DEV_FD_PTR == NULL ) { printf("\nOpen flash device failed\n"); return NULL; } /* Endif */ error_code = ioctl(fs_ptr->DEV_FD_PTR, PCFLASH_IOCTL_SET_BLOCK_MODE, NULL); if (error_code == IO_OK) { // printf("\nBlock mode enabled."); } else { printf("\nBlock mode not enabled. Running in byte mode.\n"); } /* Endif */ /* Try Installing a the partition manager */ error_code = _io_part_mgr_install(fs_ptr->DEV_FD_PTR, partition_manager_name, 0); if (error_code != MFS_NO_ERROR) { printf("\nError while initializing (%s)", MFS_Error_text((uint_32)error_code)); CF_filesystem_uninstall(fs_ptr); return NULL; } fs_ptr->PM_NAME = partition_manager_name; fs_ptr->PM_FD_PTR = fopen(partition_manager_name, NULL); if (fs_ptr->PM_FD_PTR == NULL) { error_code = ferror(fs_ptr->PM_FD_PTR); printf("\nError while opening partition (%s)", MFS_Error_text((uint_32)error_code)); CF_filesystem_uninstall(fs_ptr); return NULL; } printf("\n--->Compact Flash device opened"); partition_number = 1; error_code = _io_ioctl(fs_ptr->PM_FD_PTR, IO_IOCTL_VAL_PART, &partition_number); if (error_code == PMGR_INVALID_PARTITION) { printf("\n--->No partition available on this device"); /* uninitialize */ fclose(fs_ptr->PM_FD_PTR); fs_ptr->PM_FD_PTR = NULL; _io_part_mgr_uninstall(fs_ptr->PM_NAME); fs_ptr->PM_NAME = NULL; /* install MFS without partition */ mfs_status = _io_mfs_install(fs_ptr->DEV_FD_PTR, file_system_name, 0); } else { printf("\n--->Partition Manager installed"); /* Install MFS on the partition #1 */ mfs_status = _io_mfs_install(fs_ptr->PM_FD_PTR, file_system_name, partition_number); } if (mfs_status != MFS_NO_ERROR) { printf("\nError initializing MFS (%s)", MFS_Error_text((uint_32)mfs_status)); /* uninitialize and exit */ CF_filesystem_uninstall(fs_ptr); return NULL; } fs_ptr->FS_NAME = file_system_name; fs_ptr->FS_FD_PTR = fopen(file_system_name, 0); if (fs_ptr->FS_FD_PTR==NULL) { CF_filesystem_uninstall(fs_ptr); return NULL; } printf("\n--->File System opened as : %s\n",fs_ptr->FS_NAME); return (pointer) fs_ptr; }
pointer usb_filesystem_install( pointer usb_handle, char_ptr block_device_name, char_ptr partition_manager_name, char_ptr file_system_name ) { uint_32 partition_number; uchar_ptr dev_info; int_32 error_code; USB_FILESYSTEM_STRUCT_PTR usb_fs_ptr; usb_fs_ptr = _mem_alloc_system_zero(sizeof(USB_FILESYSTEM_STRUCT)); if (usb_fs_ptr==NULL) { return NULL; } /* Install USB device */ error_code = _io_usb_mfs_install(block_device_name, 0, (pointer)usb_handle); if (error_code != MQX_OK) { printf("Error while installing USB device (0x%X)\n", error_code); return NULL; } usb_fs_ptr->DEV_NAME = block_device_name; /* Open the USB mass storage device */ _time_delay(500); // printf("open msd dev\n"); usb_fs_ptr->DEV_FD_PTR = fopen(block_device_name, 0); if (usb_fs_ptr->DEV_FD_PTR == NULL) { printf("Unable to open USB device\n"); /*usb_filesystem_uninstall*/usb_filesystem_cancel_install(usb_fs_ptr,USB_DEV_OPEN_FAIL); return NULL; } _io_ioctl(usb_fs_ptr->DEV_FD_PTR, IO_IOCTL_SET_BLOCK_MODE, NULL); /* Get the vendor information and display it */ printf("\n************************************************************************\n"); _io_ioctl(usb_fs_ptr->DEV_FD_PTR, USB_MFS_IOCTL_GET_VENDOR_INFO, &dev_info); printf("Vendor Information: %-1.8s Mass Storage Device\n",dev_info); _io_ioctl(usb_fs_ptr->DEV_FD_PTR, USB_MFS_IOCTL_GET_PRODUCT_ID, &dev_info); printf("Product Identification: %-1.16s\n",dev_info); _io_ioctl(usb_fs_ptr->DEV_FD_PTR, USB_MFS_IOCTL_GET_PRODUCT_REV, &dev_info); printf("Product Revision Level: %-1.4s\n",dev_info); printf("************************************************************************\n"); /* Try to install the partition manager */ // printf("_io_part_mgr_install\n"); error_code = _io_part_mgr_install(usb_fs_ptr->DEV_FD_PTR, partition_manager_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error while initializing partition manager: %s\n", MFS_Error_text((uint_32)error_code)); /* usb_filesystem_uninstall */usb_filesystem_cancel_install(usb_fs_ptr,PARTITION_INIT_FAIL); return NULL; } usb_fs_ptr->PM_NAME = partition_manager_name; /* Open partition manager */ // printf("fopen partition\n"); usb_fs_ptr->PM_FD_PTR = fopen(partition_manager_name, NULL); if (usb_fs_ptr->PM_FD_PTR == NULL) { error_code = ferror(usb_fs_ptr->PM_FD_PTR); printf("Error while opening partition manager: %s\n", MFS_Error_text((uint_32)error_code)); /*usb_filesystem_uninstall*/usb_filesystem_cancel_install(usb_fs_ptr,PARTITION_OPEN_FAIL); return NULL; } /* Select partition */ partition_number = 1; // printf("select partition\n"); error_code = _io_ioctl(usb_fs_ptr->PM_FD_PTR, IO_IOCTL_SEL_PART, &partition_number); if (error_code == MFS_NO_ERROR) { printf("Installing MFS over partition...\n"); /* Validate partition */ // printf("Validate partition\n"); error_code = _io_ioctl(usb_fs_ptr->PM_FD_PTR, IO_IOCTL_VAL_PART, NULL); if (error_code != MFS_NO_ERROR) { printf("Error while validating partition: %s\n", MFS_Error_text((uint32_t)error_code)); printf("Not installing MFS.\n"); /*usb_filesystem_uninstall*/usb_filesystem_cancel_install(usb_fs_ptr,PARTITION_VALIDATE_FAIL); return NULL; } /* Install MFS over partition */ //printf("Install MFS over partition\n"); error_code = _io_mfs_install(usb_fs_ptr->PM_FD_PTR, file_system_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint_32)error_code)); } } else { printf("Installing MFS over USB device...\n"); /* Install MFS over USB device driver */ //printf("Install MFS over USB device driver\n"); error_code = _io_mfs_install(usb_fs_ptr->DEV_FD_PTR, file_system_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint_32)error_code)); } } /* Open file system */ if (error_code == MFS_NO_ERROR) { usb_fs_ptr->FS_NAME = file_system_name; //printf("Open file system\n"); usb_fs_ptr->FS_FD_PTR = fopen(file_system_name, NULL); error_code = ferror(usb_fs_ptr->FS_FD_PTR); if (error_code == MFS_NOT_A_DOS_DISK) { printf("NOT A DOS DISK! You must format to continue.\n"); } else if (error_code != MFS_NO_ERROR) { printf("Error opening filesystem: %s\n", MFS_Error_text((uint_32)error_code)); /* usb_filesystem_uninstall*/usb_filesystem_cancel_install(usb_fs_ptr,MFS_OPEN_FAIL); return NULL; } printf("USB device installed to %s\n", file_system_name); } else { /*usb_filesystem_uninstall*/usb_filesystem_cancel_install(usb_fs_ptr,MFS_INIT_FAIL); return NULL; } return (pointer)usb_fs_ptr; }