/* * Callback function for the /gainspan/config handler * It is up to the application to deal with the configuration. * This function further dispatches GET and POST * separately. */ VOID App_ConfigCb(GSN_SYS_CONFIG_T *pConfig, UINT8 method) { AppDbg_Printf("[%s] Entered\r\n", __FUNCTION__); // TODO - Is this the correct place to do this? Revisit f_chdrive(0); /*copy the passed structure and store it in the flash memory*/ if(method == GSN_HTTPD_URI_METHOD_GET) { AppConfig_Load(pConfig); pConfig->networkConfig.ipConfig.ipAddr = htonl(s_nwParams.ipv4.ipAddr); } if (method == GSN_HTTPD_URI_METHOD_POST) { AppConfig_Save(pConfig); /* Apply System configuration, if necesary */ App_ApplySysConfig(pConfig); } // TODO - Is this the correct place to do this? Revisit f_chdrive(1); return; }
STATIC void mptask_init_sflash_filesystem (void) { FILINFO fno; #if _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif // Initialise the local flash filesystem. // Create it if needed, and mount in on /flash. // try to mount the flash FRESULT res = f_mount(sflash_fatfs, "/flash", 1); if (res == FR_NO_FILESYSTEM) { // no filesystem, so create a fresh one res = f_mkfs("/flash", 1, 0); if (res == FR_OK) { // success creating fresh LFS } else { __fatal_error("failed to create /flash"); } // create empty main.py mptask_create_main_py(); } else if (res == FR_OK) { // mount sucessful if (FR_OK != f_stat("/flash/main.py", &fno)) { // create empty main.py mptask_create_main_py(); } } else { __fatal_error("failed to create /flash"); } // The current directory is used as the boot up directory. // It is set to the internal flash filesystem by default. f_chdrive("/flash"); // Make sure we have a /flash/boot.py. Create it if needed. res = f_stat("/flash/boot.py", &fno); if (res == FR_OK) { if (fno.fattrib & AM_DIR) { // exists as a directory // TODO handle this case // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation } else { // exists as a file, good! } } else { // doesn't exist, create fresh file FIL fp; f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); } }
int diskdrive_chdrive(const char* lvn) { unsigned char drive = get_drive_number(lvn); disk_interface_t* disk = diskdrive_get_disk(drive); if(disk) { if(f_chdrive(disk->volume.lvn) == FR_OK) { current_drive = drive; return 0; } } return -1; }
//--------------------------------------------------------------------+ // tinyusb callbacks //--------------------------------------------------------------------+ void tuh_msc_mounted_cb(uint8_t dev_addr) { puts("\na MassStorage device is mounted"); //------------- Disk Information -------------// // SCSI VendorID[8] & ProductID[16] from Inquiry Command uint8_t const* p_vendor = tuh_msc_get_vendor_name(dev_addr); uint8_t const* p_product = tuh_msc_get_product_name(dev_addr); for(uint8_t i=0; i<8; i++) putchar(p_vendor[i]); putchar(' '); for(uint8_t i=0; i<16; i++) putchar(p_product[i]); putchar('\n'); uint32_t last_lba, block_size; tuh_msc_get_capacity(dev_addr, &last_lba, &block_size); printf("Disk Size: %d MB\n", (last_lba+1)/ ((1024*1024)/block_size) ); printf("LBA 0-0x%X Block Size: %d\n", last_lba, block_size); //------------- file system (only 1 LUN support) -------------// uint8_t phy_disk = dev_addr-1; disk_initialize(phy_disk); if ( disk_is_ready(phy_disk) ) { if ( f_mount(phy_disk, &fatfs[phy_disk]) != FR_OK ) { puts("mount failed"); return; } puts("---------------------------------------------------------------------"); puts("- MASSSTORAGE CLASS CLI IS A IMMATURE CODE. DISK-WRITING COMMANDS"); puts("- SUCH AS cp(COPY), mkdir(MAKE DIRECTORY) ARE POTENTIAL TO DAMAGE"); puts("- YOUR USB THUMBDRIVE. USING THOSE COMMANDS ARE AT YOUR OWN RISK."); puts("- THE AUTHOR HAS NO RESPONSIBILITY WITH YOUR DEVICE NOR ITS DATA"); puts("---------------------------------------------------------------------"); f_chdrive(phy_disk); // change to newly mounted drive f_chdir("/"); // root as current dir cli_init(); } }
void tuh_msc_unmounted_cb(uint8_t dev_addr) { puts("\na MassStorage device is unmounted"); uint8_t phy_disk = dev_addr-1; f_mount(phy_disk, NULL); // unmount disk disk_deinitialize(phy_disk); if ( phy_disk == f_get_current_drive() ) { // active drive is unplugged --> change to other drive for(uint8_t i=0; i<CFG_TUSB_HOST_DEVICE_MAX; i++) { if ( disk_is_ready(i) ) { f_chdrive(i); cli_init(); // refractor, rename } } } }
static void setUpFlash() { f_enterFS(); #ifdef MOD5441X int drv = OpenOffBoardFlash(); int rv = f_chdrive( drv ); if (rv == F_NO_ERROR) { printf("External Flash opened.\r\n"); extFlashOpened = true; } else { printf("FAILED TO OPEN EXTERNAL FLASH.\r\n"); } #elif defined NANO54415 InitExtFlash(); extFlashOpened = true; #else #error Platform type unknown for filesytem setup. #endif }
PRIVATE UINT8 AppS2wCmd_openFile(UINT8 *ptr) { UINT8 *p; UINT32 val; memset(&s2wappMainTaskCtxt->appExtFsCtx.fileInfo, 0, sizeof(s2wappMainTaskCtxt->appExtFsCtx.fileInfo)); /* get the file name file */ p = AppS2wParse_NextParamGet(&ptr); if(!p) { return S2W_EINVAL; } s2wappMainTaskCtxt->appExtFsCtx.fileInfo.pName = p; /* get the open flags */ p = AppS2wParse_NextParamGet(&ptr); if(!p) { return S2W_EINVAL; } s2wappMainTaskCtxt->appExtFsCtx.fileInfo.flags = p; /* get the partition */ p = AppS2wParse_NextParamGet(&ptr); if(!p) { return S2W_EINVAL; } AppS2wParse_Int(p, &val); f_chdrive(val); s2wappMainTaskCtxt->appExtFsCtx.fileInfo.fd = f_open((const INT8 *)s2wappMainTaskCtxt->appExtFsCtx.fileInfo.pName, (const INT8 *)s2wappMainTaskCtxt->appExtFsCtx.fileInfo.flags); return S2W_SUCCESS; }
int *initOnBoardSD(int drv) { static int card_status[2] = { 0, 0 }; f_enterFS(); int rv = f_chdrive(drv); if (rv == F_NO_ERROR) { iprintf("drive change successful\r\n"); //iprintf("No of Files Found = %d\n",DumpDir()); card_status[0] = 1; card_status[1] = DumpDir(); return card_status; } else { iprintf("drive change failed: "); DisplayEffsErrorCode(rv); card_status[0] = -1; card_status[1] = 0; return card_status; } }
static int mount_card() { DIR dir; XCHAR root[32]; sd_set_mode(SD_SLOW); sd_set_type(SD_NORMAL); f_mount(1, NULL); sd_set_type(SD_NORMAL); if(MMC_disk_initialize() == STA_NODISK) { sd_set_type(SD_NORMAL); f_mount(1, NULL); sd_set_type(SD_NORMAL); if(MMC_disk_initialize() == STA_NODISK) { return 0; } } sd_set_mode(SD_SLOW); if(f_mount(1,&fat_filesys)) return 0; f_chdrive(1); c2wstrcpy(root,"/"); sd_set_mode(SD_SLOW); if(f_opendir(&dir,root)) { sd_set_type(SD_FUNKY); if (MMC_disk_initialize() == STA_NODISK) return 0; } set_sdc_speed(); return 1; }
BOOL AppCertUpload_SslCertUploadCb(GSN_HTTPD_ELEMENT_INFO_T fileInfo, INT8 *buffer, UINT32 bufferLen, GSN_HTTPD_ELEMENT_EVENTS_T event, GSN_HTTPD_STATUS_INFO_T *statusInfo) { UINT8 len; INT8 tagName[APP_CERT_MAX_NAME_LENGTH+8]; INT32 status; AppDbg_Printf("\n\rIn file upload CB: %x\n\r", event); switch(event) { case START_UPLOAD: AppFileUploadStatus=0; AppFileuploadCnt=0; break; case START_FILE: AppDbg_Printf("TagNameLen: %d", fileInfo.nameLen); if(fileInfo.nameLen) { f_chdrive(0); status = f_mkdir("certs"); memset(tagName, 0, APP_CERT_MAX_NAME_LENGTH+8); if(status != F_NO_ERROR || status != F_ERR_DUPLICATED ) { if(strncmp(fileInfo.name,TAG_SSLCERT,fileInfo.nameLen) == 0) { if(fileInfo.filenameLen > APP_CERT_MAX_NAME_LENGTH) fileInfo.filenameLen = APP_CERT_MAX_NAME_LENGTH-1; len= sprintf(tagName,"certs/"); memcpy(tagName + len, fileInfo.filename, fileInfo.filenameLen); certFd= f_open((const char *)tagName, "w"); } else /*EAP Certificates*/ { len= sprintf(tagName,"certs/"); memcpy(tagName + len, fileInfo.filename, fileInfo.filenameLen); certFd= f_open((const char *)tagName, "w"); } if(certFd == NULL) { f_chdrive(1); AppFileUploadStatus = 1; return 0; } AppFileuploadCnt++; } else AppFileUploadStatus = 1; f_chdrive(1); AppDbg_Printf("\r\nFile Name is = %s:%d \r\n", tagName,fileInfo.filenameLen); } break; case CONTINUE_FILE: AppDbg_Printf("\r\nBufferLength=%d\r\n",bufferLen); f_chdrive(0); if(certFd != NULL) { len = f_write(buffer, 1, bufferLen , certFd); if(len != bufferLen) AppFileUploadStatus = 1; } f_chdrive(1); break; case END_FILE: f_chdrive(0); if(certFd != NULL) { f_close(certFd); } f_chdrive(1); break; case END_UPLOAD: AppDbg_Printf("END UPLOAD:%d\n\r",AppFileUploadStatus); if(AppFileUploadStatus != 0) { sprintf(statusInfo->pMsg,"<Response><status>SUCCESS</status><msg>"); statusInfo->msgLength = sprintf(statusInfo->pMsg, "%s %d files Uploaded</msg></Response>", \ statusInfo->pMsg, AppFileuploadCnt); } else { sprintf(statusInfo->pMsg,"<Response><status>FAILED</status><msg>"); statusInfo->msgLength = sprintf(statusInfo->pMsg, "%s %d file Uploaded</msg></Response>",\ statusInfo->pMsg, AppFileuploadCnt); } AppFileuploadCnt=0; break; case CONNEC_LOST: AppDbg_Printf("END CONNEC_LOST\n\r"); break; } return 0; }
int main(void) { // TODO disable JTAG // Stack limit should be less than real stack size, so we have a chance // to recover from limit hit. (Limit is measured in bytes.) mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024); /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); // set the system clock to be HSE SystemClock_Config(); // enable GPIO clocks __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); // enable the CCM RAM __CCMDATARAMEN_CLK_ENABLE(); #if 0 #if defined(NETDUINO_PLUS_2) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; #if MICROPY_HW_HAS_SDCARD // Turn on the power enable for the sdcard (PB1) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET); #endif // Turn on the power for the 5V on the expansion header (PB2) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET); } #endif #endif // basic sub-system init pendsv_init(); timer_tim3_init(); led_init(); #if MICROPY_HW_HAS_SWITCH switch_init0(); #endif int first_soft_reset = true; soft_reset: // check if user switch held to select the reset mode led_state(1, 0); led_state(2, 1); led_state(3, 0); led_state(4, 0); uint reset_mode = 1; #if MICROPY_HW_HAS_SWITCH if (switch_get()) { for (uint i = 0; i < 3000; i++) { if (!switch_get()) { break; } HAL_Delay(20); if (i % 30 == 29) { if (++reset_mode > 3) { reset_mode = 1; } led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); } } // flash the selected reset mode for (uint i = 0; i < 6; i++) { led_state(2, 0); led_state(3, 0); led_state(4, 0); HAL_Delay(50); led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); HAL_Delay(50); } HAL_Delay(400); } #endif #if MICROPY_HW_ENABLE_RTC if (first_soft_reset) { rtc_init(); } #endif // more sub-system init #if MICROPY_HW_HAS_SDCARD if (first_soft_reset) { sdcard_init(); } #endif if (first_soft_reset) { storage_init(); } // GC init gc_init(&_heap_start, &_heap_end); // Micro Python init mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib)); mp_obj_list_init(mp_sys_argv, 0); // Change #if 0 to #if 1 if you want REPL on UART_6 (or another uart) // as well as on USB VCP #if 0 { mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(PYB_UART_6), MP_OBJ_NEW_SMALL_INT(115200), }; pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args); } #else pyb_stdio_uart = NULL; #endif // Initialise low-level sub-systems. Here we need to very basic things like // zeroing out memory and resetting any of the sub-systems. Following this // we can run Python scripts (eg boot.py), but anything that is configurable // by boot.py must be set after boot.py is run. readline_init0(); pin_init0(); extint_init0(); timer_init0(); uart_init0(); #if MICROPY_HW_ENABLE_RNG rng_init0(); #endif i2c_init0(); spi_init0(); pyb_usb_init0(); // Initialise the local flash filesystem. // Create it if needed, and mount in on /flash. { // try to mount the flash FRESULT res = f_mount(&fatfs0, "/flash", 1); if (reset_mode == 3 || res == FR_NO_FILESYSTEM) { // no filesystem, or asked to reset it, so create a fresh one // LED on to indicate creation of LFS led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); res = f_mkfs("/flash", 0, 0); if (res == FR_OK) { // success creating fresh LFS } else { __fatal_error("could not create LFS"); } // set label f_setlabel("/flash/pybflash"); // create empty main.py FIL fp; f_open(&fp, "/flash/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // create .inf driver file f_open(&fp, "/flash/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n); f_close(&fp); // create readme file f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n); f_close(&fp); // keep LED on for at least 200ms sys_tick_wait_at_least(start_tick, 200); led_state(PYB_LED_R2, 0); } else if (res == FR_OK) { // mount sucessful } else { __fatal_error("could not access LFS"); } } // The current directory is used as the boot up directory. // It is set to the internal flash filesystem by default. f_chdrive("/flash"); // Make sure we have a /flash/boot.py. Create it if needed. { FILINFO fno; #if _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif FRESULT res = f_stat("/flash/boot.py", &fno); if (res == FR_OK) { if (fno.fattrib & AM_DIR) { // exists as a directory // TODO handle this case // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation } else { // exists as a file, good! } } else { // doesn't exist, create fresh file // LED on to indicate creation of boot.py led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); FIL fp; f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // keep LED on for at least 200ms sys_tick_wait_at_least(start_tick, 200); led_state(PYB_LED_R2, 0); } } #if defined(USE_DEVICE_MODE) usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH; #endif #if MICROPY_HW_HAS_SDCARD // if an SD card is present then mount it on /sd/ if (sdcard_is_present()) { FRESULT res = f_mount(&fatfs1, "/sd", 1); if (res != FR_OK) { printf("[SD] could not mount SD card\n"); } else { // use SD card as current directory f_chdrive("/sd"); // TODO these should go before the /flash entries in the path mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib)); if (first_soft_reset) { // use SD card as medium for the USB MSD #if defined(USE_DEVICE_MODE) usb_medium = USB_STORAGE_MEDIUM_SDCARD; #endif } } } #endif // reset config variables; they should be set by boot.py pyb_config_main = MP_OBJ_NULL; pyb_config_usb_mode = MP_OBJ_NULL; // run boot.py, if it exists // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py if (reset_mode == 1) { const char *boot_py = "boot.py"; FRESULT res = f_stat(boot_py, NULL); if (res == FR_OK) { int ret = pyexec_file(boot_py); if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } if (!ret) { flash_error(4); } } } // turn boot-up LEDs off led_state(2, 0); led_state(3, 0); led_state(4, 0); // Now we initialise sub-systems that need configuration from boot.py, // or whose initialisation can be safely deferred until after running // boot.py. #if defined(USE_HOST_MODE) // USB host pyb_usb_host_init(); #elif defined(USE_DEVICE_MODE) // USB device usb_device_mode_t usb_mode = USB_DEVICE_MODE_CDC_MSC; // if we are not in reset_mode==1, this config variable will always be NULL if (pyb_config_usb_mode != MP_OBJ_NULL) { if (strcmp(mp_obj_str_get_str(pyb_config_usb_mode), "CDC+HID") == 0) { usb_mode = USB_DEVICE_MODE_CDC_HID; } } pyb_usb_dev_init(usb_mode, usb_medium); #endif #if MICROPY_HW_HAS_MMA7660 // MMA accel: init and reset accel_init(); #endif #if MICROPY_HW_ENABLE_SERVO // servo servo_init(); #endif #if MICROPY_HW_ENABLE_DAC // DAC dac_init(); #endif mod_network_init(); // At this point everything is fully configured and initialised. // Run the main script from the current directory. if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { const char *main_py; if (pyb_config_main == MP_OBJ_NULL) { main_py = "main.py"; } else { main_py = mp_obj_str_get_str(pyb_config_main); } FRESULT res = f_stat(main_py, NULL); if (res == FR_OK) { int ret = pyexec_file(main_py); if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } if (!ret) { flash_error(3); } } } // Main script is finished, so now go into REPL mode. // The REPL mode can change, or it can request a soft reset. for (;;) { if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { if (pyexec_raw_repl() != 0) { break; } } else { if (pyexec_friendly_repl() != 0) { break; } } } soft_reset_exit: // soft reset printf("PYB: sync filesystems\n"); storage_flush(); printf("PYB: soft reboot\n"); timer_deinit(); uart_deinit(); first_soft_reset = false; goto soft_reset; }
FRESULT DFATFS::fschdrive(const char* path) { return(f_chdrive(path)); }
static msg_t ThreadFatFSWorker(void *arg) { (void)arg; Thread* p; chRegSetThreadName("fatfsWorker"); while (!chThdShouldTerminate()) { /* Wait for msg with work to do. */ p = chMsgWait(); struct wrapper_msg_base* msg = (struct wrapper_msg_base*) chMsgGet(p); msg->result = FR_INVALID_PARAMETER; switch(msg->action) { case eTERMINATE: { break; } #if HAS_MOUNT case eFMOUNT: { const struct wrapper_msg_vBYTEpFATFS* exmsg = \ (const struct wrapper_msg_vBYTEpFATFS*) msg; msg->result = f_mount(exmsg->byte, exmsg->fatfsp); break; } #endif /* HAS_MOUNT */ #if HAS_OPEN case eFOPEN: { const struct wrapper_msg_pFILpTCHARvBYTE* exmsg = \ (const struct wrapper_msg_pFILpTCHARvBYTE*) msg; msg->result = f_open(exmsg->filep, exmsg->string, exmsg->byte); break; } #endif /* HAS_OPEN */ #if HAS_READ case eFREAD: { const struct wrapper_msg_pFILpVOIDvUINTpUINT* exmsg = \ (const struct wrapper_msg_pFILpVOIDvUINTpUINT*) msg; msg->result = f_read(exmsg->filep, exmsg->voidp, exmsg->uint, exmsg->uintp); break; } #endif /* HAS_READ */ #if HAS_WRITE case eFWRITE: { const struct wrapper_msg_pFILpVOIDvUINTpUINT* exmsg = \ (const struct wrapper_msg_pFILpVOIDvUINTpUINT*) msg; msg->result = f_write(exmsg->filep, exmsg->voidp, exmsg->uint, exmsg->uintp); break; } #endif /* HAD_WRITE */ #if HAS_SYNC case eFSYNC: { const struct wrapper_msg_pFIL* exmsg = \ (const struct wrapper_msg_pFIL*) msg; msg->result = f_sync(exmsg->filep); break; } #endif /* HAS_SYNC */ #if HAS_CHDRIVE case eFCHDRIVE: { const struct wrapper_msg_vBYTE* exmsg = \ (const struct wrapper_msg_vBYTE*) msg; msg->result = f_chdrive(exmsg->byte); break; } #endif #if HAS_CHDIR case eFCHDIR: { const struct wrapper_msg_pTCHAR* exmsg = \ (const struct wrapper_msg_pTCHAR*) msg; msg->result = f_chdir(exmsg->string); break; } #endif /* HAS_CHDIR */ #if HAS_GETCWD case eFGETCWD: { const struct wrapper_msg_pTCHARvUINT* exmsg = \ (const struct wrapper_msg_pTCHARvUINT*) msg; msg->result = f_getcwd(exmsg->string, exmsg->uint); break; } #endif #if HAS_LSEEK case eFLSEEK: { const struct wrapper_msg_pFILvDWORD* exmsg = \ (const struct wrapper_msg_pFILvDWORD*) msg; msg->result = f_lseek(exmsg->filep, exmsg->dword); break; } #endif /* HAS_LSEEK */ #if HAS_CLOSE case eFCLOSE: { const struct wrapper_msg_pFIL* exmsg = \ (const struct wrapper_msg_pFIL*) msg; msg->result = f_close(exmsg->filep); break; } #endif /* HAS_CLOSE */ #if HAS_OPENDIR case eFOPENDIR: { const struct wrapper_msg_pDIRpTCHAR* exmsg = \ (const struct wrapper_msg_pDIRpTCHAR*) msg; msg->result = f_opendir(exmsg->dirp, exmsg->string); break; } #endif /* HAD_OPENDIR */ #if HAS_READDIR case eFREADDIR: { const struct wrapper_msg_pDIRpFILINFO* exmsg = \ (const struct wrapper_msg_pDIRpFILINFO*) msg; msg->result = f_readdir(exmsg->dirp, exmsg->filinfop); break; } #endif /* HAS_READDIR */ #if HAS_STAT case eFSTAT: { const struct wrapper_msg_pTCHARpFILINFO* exmsg = \ (const struct wrapper_msg_pTCHARpFILINFO*) msg; msg->result = f_stat(exmsg->string, exmsg->filinfop); break; } #endif /* HAS_STAT */ #if HAS_GETFREE case eFGETFREE: { const struct wrapper_msg_pTCHARpDWORDppFATFS* exmsg = \ (const struct wrapper_msg_pTCHARpDWORDppFATFS*) msg; msg->result = f_getfree(exmsg->string, exmsg->dwordp, exmsg->fatfspp); break; } #endif /* HAS_GETFREE */ #if HAS_TRUNCATE case eFTRUNCATE: { const struct wrapper_msg_pFIL* exmsg = \ (const struct wrapper_msg_pFIL*) msg; msg->result = f_truncate(exmsg->filep); break; } #endif /* HAS_TRUNCATE */ #if HAS_UNLINK case eFUNLINK: { const struct wrapper_msg_pTCHAR* exmsg = \ (const struct wrapper_msg_pTCHAR*) msg; msg->result = f_unlink(exmsg->string); break; } #endif /* HAS_UNLINK */ #if HAS_MKDIR case eFMKDIR: { const struct wrapper_msg_pTCHAR* exmsg = \ (const struct wrapper_msg_pTCHAR*) msg; msg->result = f_mkdir(exmsg->string); break; } #endif /* HAS_MKDIR */ #if HAS_CHMOD case eFCHMOD: { const struct wrapper_msg_pTCHARvBYTEvBYTE* exmsg = \ (const struct wrapper_msg_pTCHARvBYTEvBYTE*) msg; msg->result = f_chmod(exmsg->string, exmsg->byte1, exmsg->byte2); break; } #endif /* HAS_CHMOD */ #if HAS_UTIME case eFUTIME: { const struct wrapper_msg_pTCHARpFILINFO* exmsg = \ (const struct wrapper_msg_pTCHARpFILINFO*) msg; msg->result = f_utime(exmsg->string, exmsg->filinfop); break; } #endif /* HAD_UTIME */ #if HAS_RENAME case eFRENAME: { const struct wrapper_msg_pTCHARpTCHAR* exmsg = \ (const struct wrapper_msg_pTCHARpTCHAR*) msg; msg->result = f_rename(exmsg->string1, exmsg->string2); break; } #endif /* HAS_RENAME */ #if HAS_MKFS case eFMKFS: { const struct wrapper_msg_vBYTEvBYTEvUINT* exmsg = \ (const struct wrapper_msg_vBYTEvBYTEvUINT*) msg; msg->result = f_mkfs(exmsg->byte1, exmsg->byte2, exmsg->uint); break; } #endif /* HAS_MKFS */ #if HAS_FDISK case eFFDISK: { const struct wrapper_msg_vBYTEpDWORDpVOID* exmsg = \ (const struct wrapper_msg_vBYTEpDWORDpVOID*) msg; msg->result = f_fdisk(exmsg->byte, exmsg->dwordp, exmsg->voidp); break; } #endif /* HAS_FDISK */ #if HAS_GETS case eFGETS: { struct wrapper_msg_pTCHARvINTpFILpTCHAR* exmsg = \ (struct wrapper_msg_pTCHARvINTpFILpTCHAR*) msg; exmsg->string2 = f_gets(exmsg->string, exmsg->n, exmsg->filep); break; } #endif /* HAS_GETS */ #if HAS_PUTC case eFPUTC: { const struct wrapper_msg_vTCHARpFIL* exmsg = \ (const struct wrapper_msg_vTCHARpFIL*) msg; msg->result = f_putc(exmsg->tchar, exmsg->filep); break; } #endif /* HAS_PUTC */ #if HAS_PUTS case eFPUTS: { const struct wrapper_msg_pTCHARpFIL* exmsg = \ (const struct wrapper_msg_pTCHARpFIL*) msg; msg->result = f_puts(exmsg->string, exmsg->filep); break; } #endif /* HAS_PUTS */ } /* Done, release msg again. */ chMsgRelease(p, 0); } return 0; }
int test_sdhc(void) { #if TEST_SDCARD if(CLOCK_GetBusClkFreq() < SD_CLOCK_25MHZ) { PRINT("skipping SDHC test, bus clock frequency %d is lower than required %d\r\n",CLOCK_GetBusClkFreq(), SD_CLOCK_25MHZ); return; } init_sdhc_pins(); FRESULT error; DIR directory; /* Directory object */ FILINFO fileInformation; UINT bytesWritten; UINT bytesRead; const TCHAR driverNumberBuffer[3U] = {SDDISK + '0', ':', '/'}; MPU_Enable(MPU, false); PRINTF("\r\nFATFS example to demonstrate how to use FATFS with SD card.\r\n"); PRINTF("\r\nPlease insert a card into board.\r\n"); /* Wait the card to be inserted. */ while (!(GPIO_ReadPinInput(GPIOE, 7U))) { } PRINTF("Detected SD card inserted.\r\n"); /* Delat some time to make card stable. */ delay(1000U); if (f_mount(&g_fileSystem, driverNumberBuffer, 0U)) { PRINTF("Mount volume failed.\r\n"); return -1; } #if (_FS_RPATH >= 2U) error = f_chdrive(&driverNumberBuffer[0U]); if (error) { PRINTF("Change drive failed.\r\n"); return -1; } #endif #if _USE_MKFS PRINTF("\r\nMake file system......The time may be long if the card capacity is big.\r\n"); if (f_mkfs(driverNumberBuffer, 1U, 0U)) { PRINTF("Make file system failed.\r\n"); return -1; } #endif /* _USE_MKFS */ FIL testFileObject; PRINTF("\r\nRead from static test file\r\n"); error = f_open(&testFileObject, _T("/test.txt"), FA_READ); if (error) { PRINTF("Open file failed (%d).\r\n", error); return -1; } memset(g_bufferRead, 0U, sizeof(g_bufferRead)); error = f_read(&testFileObject, g_bufferRead, sizeof(g_bufferRead), &bytesRead); if (error) { PRINTF("Read file failed. \r\n"); } PRINTF("bytes read: %d\r\n", bytesRead); PRINTF("----\r\n"); PRINTF("%s", g_bufferRead); PRINTF("----\r\n"); if (f_close(&testFileObject)) { PRINTF("\r\nClose file failed.\r\n"); return -1; } PRINTF("\r\nCreate directory......\r\n"); error = f_mkdir(_T("/dir_1")); if (error) { if (error == FR_EXIST) { PRINTF("Directory exists.\r\n"); } else { PRINTF("Make directory failed.\r\n"); return -1; } } PRINTF("\r\nCreate a file in that directory......\r\n"); error = f_open(&g_fileObject, _T("/dir_1/f_1.dat"), (FA_WRITE | FA_READ | FA_CREATE_ALWAYS)); if (error) { if (error == FR_EXIST) { PRINTF("File exists.\r\n"); } else { PRINTF("Open file failed.\r\n"); return -1; } } PRINTF("\r\nCreate a directory in that directory......\r\n"); error = f_mkdir(_T("/dir_1/dir_2")); if (error) { if (error == FR_EXIST) { PRINTF("Directory exists.\r\n"); } else { PRINTF("Directory creation failed.\r\n"); return -1; } } PRINTF("\r\nList the file in that directory......\r\n"); if (f_opendir(&directory, "/dir_1")) { PRINTF("Open directory failed.\r\n"); return -1; } for (;;) { error = f_readdir(&directory, &fileInformation); /* To the end. */ if ((error != FR_OK) || (fileInformation.fname[0U] == 0U)) { break; } if (fileInformation.fname[0] == '.') { continue; } if (fileInformation.fattrib & AM_DIR) { PRINTF("Directory file : %s.\r\n", fileInformation.fname); } else { PRINTF("General file : %s.\r\n", fileInformation.fname); } } memset(g_bufferWrite, 'a', sizeof(g_bufferWrite)); g_bufferWrite[BUFFER_SIZE - 2U] = '\r'; g_bufferWrite[BUFFER_SIZE - 1U] = '\n'; PRINTF("\r\nWrite/read file ...\r\n"); PRINTF("\r\nWrite to above created file.\r\n"); error = f_write(&g_fileObject, g_bufferWrite, sizeof(g_bufferWrite), &bytesWritten); if ((error) || (bytesWritten != sizeof(g_bufferWrite))) { PRINTF("Write file failed. \r\n"); return -1; } /* Move the file pointer */ if (f_lseek(&g_fileObject, 0U)) { PRINTF("Set file pointer position failed. \r\n"); return -1; } PRINTF("Read from above created file.\r\n"); memset(g_bufferRead, 0U, sizeof(g_bufferRead)); error = f_read(&g_fileObject, g_bufferRead, sizeof(g_bufferRead), &bytesRead); if ((error) || (bytesRead != sizeof(g_bufferRead))) { PRINTF("Read file failed. \r\n"); return -1; } PRINTF("Compare the read/write content......\r\n"); if (memcmp(g_bufferWrite, g_bufferRead, sizeof(g_bufferWrite))) { PRINTF("Compare read/write content isn't consistent.\r\n"); return -1; } PRINTF("The read/write content is consistent.\r\n"); PRINTF("\r\nThe example will not read/write file again.\r\n"); if (f_close(&g_fileObject)) { PRINTF("\r\nClose file failed.\r\n"); return -1; } #endif return 0; }
bool mountFs(bool isSd, bool switchToCtrNand) { return isSd ? f_mount(&sdFs, "0:", 1) == FR_OK && switchToMainDir(true) : f_mount(&nandFs, "1:", 1) == FR_OK && (!switchToCtrNand || (f_chdrive("1:") == FR_OK && switchToMainDir(false))); }
int main(void) { // Stack limit should be less than real stack size, so we // had chance to recover from limit hit. mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024); /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); // basic sub-system init pendsv_init(); timer_tim3_init(); led_init(); soft_reset: // check if user switch held to select the reset mode led_state(LED_RED, 1); led_state(LED_GREEN, 1); led_state(LED_BLUE, 1); #if MICROPY_HW_ENABLE_RTC rtc_init(); #endif // GC init gc_init(&_heap_start, &_heap_end); // Micro Python init mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_init(mp_sys_argv, 0); readline_init0(); pin_init0(); extint_init0(); timer_init0(); rng_init0(); i2c_init0(); spi_init0(); uart_init0(); pyb_usb_init0(); usbdbg_init(); if (sensor_init() != 0) { __fatal_error("Failed to init sensor"); } /* Export functions to the global python namespace */ mp_store_global(qstr_from_str("randint"), (mp_obj_t)&py_randint_obj); mp_store_global(qstr_from_str("cpu_freq"), (mp_obj_t)&py_cpu_freq_obj); mp_store_global(qstr_from_str("Image"), (mp_obj_t)&py_image_load_image_obj); mp_store_global(qstr_from_str("HaarCascade"), (mp_obj_t)&py_image_load_cascade_obj); mp_store_global(qstr_from_str("FreakDesc"), (mp_obj_t)&py_image_load_descriptor_obj); mp_store_global(qstr_from_str("FreakDescSave"), (mp_obj_t)&py_image_save_descriptor_obj); mp_store_global(qstr_from_str("LBPDesc"), (mp_obj_t)&py_image_load_lbp_obj); mp_store_global(qstr_from_str("vcp_is_connected"), (mp_obj_t)&py_vcp_is_connected_obj); if (sdcard_is_present()) { sdcard_init(); FRESULT res = f_mount(&fatfs, "1:", 1); if (res != FR_OK) { __fatal_error("could not mount SD\n"); } // Set CWD and USB medium to SD f_chdrive("1:"); pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD; } else { storage_init(); // try to mount the flash FRESULT res = f_mount(&fatfs, "0:", 1); if (res == FR_NO_FILESYSTEM) { // create a fresh fs make_flash_fs(); } else if (res != FR_OK) { __fatal_error("could not access LFS\n"); } // Set CWD and USB medium to flash f_chdrive("0:"); pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH; } // turn boot-up LEDs off led_state(LED_RED, 0); led_state(LED_GREEN, 0); led_state(LED_BLUE, 0); // init USB device to default setting if it was not already configured if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) { pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL); } // Run the main script from the current directory. FRESULT res = f_stat("main.py", NULL); if (res == FR_OK) { if (!pyexec_file("main.py")) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { flash_error(3); nlr_pop(); } } } // Enter REPL nlr_buf_t nlr; for (;;) { if (nlr_push(&nlr) == 0) { while (usbdbg_script_ready()) { nlr_buf_t nlr; vstr_t *script_buf = usbdbg_get_script(); // clear script flag usbdbg_clr_script(); // execute the script if (nlr_push(&nlr) == 0) { pyexec_push_scope(); // parse and compile script mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(script_buf), vstr_len(script_buf), 0); mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT); mp_obj_t script = mp_compile(pn, lex->source_name, MP_EMIT_OPT_NONE, false); // execute the script mp_call_function_0(script); nlr_pop(); } else { mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); } pyexec_pop_scope(); } // clear script flag usbdbg_clr_script(); // no script run REPL pyexec_friendly_repl(); nlr_pop(); } } printf("PYB: sync filesystems\n"); storage_flush(); printf("PYB: soft reboot\n"); goto soft_reset; }
int main(void) { FRESULT f_res; int sensor_init_ret; // Stack limit should be less than real stack size, so we // had chance to recover from limit hit. mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024); /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); // basic sub-system init pendsv_init(); timer_tim3_init(); led_init(); soft_reset: // check if user switch held to select the reset mode led_state(LED_RED, 1); led_state(LED_GREEN, 1); led_state(LED_BLUE, 1); #if MICROPY_HW_ENABLE_RTC rtc_init(); #endif // GC init gc_init(&_heap_start, &_heap_end); // Micro Python init mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_init(mp_sys_argv, 0); readline_init0(); pin_init0(); extint_init0(); timer_init0(); rng_init0(); i2c_init0(); spi_init0(); uart_init0(); pyb_usb_init0(); usbdbg_init(); sensor_init_ret = sensor_init(); /* Export functions to the global python namespace */ mp_store_global(qstr_from_str("randint"), (mp_obj_t)&py_randint_obj); mp_store_global(qstr_from_str("cpu_freq"), (mp_obj_t)&py_cpu_freq_obj); mp_store_global(qstr_from_str("vcp_is_connected"), (mp_obj_t)&py_vcp_is_connected_obj); if (sdcard_is_present()) { sdcard_init(); FRESULT res = f_mount(&fatfs, "1:", 1); if (res != FR_OK) { __fatal_error("could not mount SD\n"); } // Set CWD and USB medium to SD f_chdrive("1:"); pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD; } else { storage_init(); // try to mount the flash FRESULT res = f_mount(&fatfs, "0:", 1); if (res == FR_NO_FILESYSTEM) { // create a fresh fs make_flash_fs(); } else if (res != FR_OK) { __fatal_error("could not access LFS\n"); } // Set CWD and USB medium to flash f_chdrive("0:"); pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH; } // turn boot-up LEDs off led_state(LED_RED, 0); led_state(LED_GREEN, 0); led_state(LED_BLUE, 0); // init USB device to default setting if it was not already configured if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) { pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL); } // check sensor init result if (sensor_init_ret != 0) { char buf[512]; snprintf(buf, sizeof(buf), "Failed to init sensor, error:%d", sensor_init_ret); __fatal_error(buf); } // Run self tests the first time only f_res = f_stat("selftest.py", NULL); if (f_res == FR_OK) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { // Parse, compile and execute the self-tests script. pyexec_file("selftest.py"); nlr_pop(); } else { // Get the exception message. TODO: might be a hack. mp_obj_str_t *str = mp_obj_exception_get_value((mp_obj_t)nlr.ret_val); // If any of the self-tests fail log the exception message // and loop forever. Note: IDE exceptions will not be caught. __fatal_error((const char*) str->data); } // Success: remove self tests script and flush cache f_unlink("selftest.py"); storage_flush(); } // Run the main script from the current directory. f_res = f_stat("main.py", NULL); if (f_res == FR_OK) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { // Parse, compile and execute the main script. pyexec_file("main.py"); nlr_pop(); } else { mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); if (nlr_push(&nlr) == 0) { flash_error(3); nlr_pop(); }// if this gets interrupted again ignore it. } } // Enter REPL nlr_buf_t nlr; for (;;) { if (nlr_push(&nlr) == 0) { while (usbdbg_script_ready()) { nlr_buf_t nlr; vstr_t *script_buf = usbdbg_get_script(); // clear debugging flags usbdbg_clear_flags(); // re-init MP mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); mp_init(); MICROPY_END_ATOMIC_SECTION(atomic_state); // execute the script if (nlr_push(&nlr) == 0) { // parse, compile and execute script pyexec_str(script_buf); nlr_pop(); } else { mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); } } // clear debugging flags usbdbg_clear_flags(); // re-init MP mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); mp_init(); MICROPY_END_ATOMIC_SECTION(atomic_state); // no script run REPL pyexec_friendly_repl(); nlr_pop(); } } printf("PYB: sync filesystems\n"); storage_flush(); printf("PYB: soft reboot\n"); goto soft_reset; }
FRESULT FatFs::chdrive(const char* path) { return f_chdrive(path); }
FRESULT f_chdrive_char(const char* path) { if (!char_to_wchar(path)) return FR_INVALID_NAME; return f_chdrive(tmpwchar.u16); }