int mtdpart_main(int argc, char *argv[]) { FAR struct mtd_dev_s *master; FAR struct mtd_dev_s *part[CONFIG_EXAMPLES_MTDPART_NPARTITIONS]; FAR struct mtd_geometry_s geo; FAR uint32_t *buffer; char blockname[32]; char charname[32]; size_t partsize; ssize_t nbytes; off_t nblocks; off_t offset; off_t check; off_t sectoff; off_t seekpos; unsigned int blkpererase; int fd; int i; int j; int k; int ret; /* Create and initialize a RAM MTD FLASH driver instance */ #ifdef CONFIG_EXAMPLES_MTDPART_ARCHINIT master = mtdpart_archinitialize(); #else master = rammtd_initialize(g_simflash, MTDPART_BUFSIZE); #endif if (!master) { message("ERROR: Failed to create RAM MTD instance\n"); msgflush(); exit(1); } /* Perform the IOCTL to erase the entire FLASH part */ ret = master->ioctl(master, MTDIOC_BULKERASE, 0); if (ret < 0) { message("ERROR: MTDIOC_BULKERASE ioctl failed: %d\n", ret); } /* Initialize to provide an FTL block driver on the MTD FLASH interface. * * NOTE: We could just skip all of this FTL and BCH stuff. We could * instead just use the MTD drivers bwrite and bread to perform this * test. Creating the character drivers, however, makes this test more * interesting. */ ret = ftl_initialize(0, master); if (ret < 0) { message("ERROR: ftl_initialize /dev/mtdblock0 failed: %d\n", ret); msgflush(); exit(2); } /* Now create a character device on the block device */ ret = bchdev_register("/dev/mtdblock0", "/dev/mtd0", false); if (ret < 0) { message("ERROR: bchdev_register /dev/mtd0 failed: %d\n", ret); msgflush(); exit(3); } /* Get the geometry of the FLASH device */ ret = master->ioctl(master, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo)); if (ret < 0) { fdbg("ERROR: mtd->ioctl failed: %d\n", ret); exit(3); } message("Flash Geometry:\n"); message(" blocksize: %lu\n", (unsigned long)geo.blocksize); message(" erasesize: %lu\n", (unsigned long)geo.erasesize); message(" neraseblocks: %lu\n", (unsigned long)geo.neraseblocks); /* Determine the size of each partition. Make each partition an even * multiple of the erase block size (perhaps not using some space at the * end of the FLASH). */ blkpererase = geo.erasesize / geo.blocksize; nblocks = (geo.neraseblocks / CONFIG_EXAMPLES_MTDPART_NPARTITIONS) * blkpererase; partsize = nblocks * geo.blocksize; message(" No. partitions: %u\n", CONFIG_EXAMPLES_MTDPART_NPARTITIONS); message(" Partition size: %lu Blocks (%lu bytes)\n", nblocks, partsize); /* Now create MTD FLASH partitions */ message("Creating partitions\n"); for (offset = 0, i = 1; i <= CONFIG_EXAMPLES_MTDPART_NPARTITIONS; offset += nblocks, i++) { message(" Partition %d. Block offset=%lu, size=%lu\n", i, (unsigned long)offset, (unsigned long)nblocks); /* Create the partition */ part[i] = mtd_partition(master, offset, nblocks); if (!part[i]) { message("ERROR: mtd_partition failed. offset=%lu nblocks=%lu\n", (unsigned long)offset, (unsigned long)nblocks); msgflush(); exit(4); } /* Initialize to provide an FTL block driver on the MTD FLASH interface */ snprintf(blockname, 32, "/dev/mtdblock%d", i); snprintf(charname, 32, "/dev/mtd%d", i); ret = ftl_initialize(i, part[i]); if (ret < 0) { message("ERROR: ftl_initialize %s failed: %d\n", blockname, ret); msgflush(); exit(5); } /* Now create a character device on the block device */ ret = bchdev_register(blockname, charname, false); if (ret < 0) { message("ERROR: bchdev_register %s failed: %d\n", charname, ret); msgflush(); exit(6); } } /* Allocate a buffer */ buffer = (FAR uint32_t *)malloc(geo.blocksize); if (!buffer) { message("ERROR: failed to allocate a sector buffer\n"); msgflush(); exit(7); } /* Open the master MTD FLASH character driver for writing */ fd = open("/dev/mtd0", O_WRONLY); if (fd < 0) { message("ERROR: open /dev/mtd0 failed: %d\n", errno); msgflush(); exit(8); } /* Now write the offset into every block */ message("Initializing media:\n"); offset = 0; for (i = 0; i < geo.neraseblocks; i++) { for (j = 0; j < blkpererase; j++) { /* Fill the block with the offset */ for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++) { buffer[k] = offset; offset += 4; } /* And write it using the character driver */ nbytes = write(fd, buffer, geo.blocksize); if (nbytes < 0) { message("ERROR: write to /dev/mtd0 failed: %d\n", errno); msgflush(); exit(9); } } } close(fd); /* Now read each partition */ message("Checking partitions:\n"); for (offset = 0, i = 1; i <= CONFIG_EXAMPLES_MTDPART_NPARTITIONS; offset += partsize, i++) { message(" Partition %d. Byte offset=%lu, size=%lu\n", i, (unsigned long)offset, (unsigned long)partsize); /* Open the master MTD partition character driver for writing */ snprintf(charname, 32, "/dev/mtd%d", i); fd = open(charname, O_RDWR); if (fd < 0) { message("ERROR: open %s failed: %d\n", charname, errno); msgflush(); exit(10); } /* Now verify the offset in every block */ check = offset; sectoff = 0; for (j = 0; j < nblocks; j++) { #if 0 /* Too much */ message(" block=%u offset=%lu\n", j, (unsigned long) check); #endif /* Seek to the next read position */ seekpos = lseek(fd, sectoff, SEEK_SET); if (seekpos != sectoff) { message("ERROR: lseek to offset %ld failed: %d\n", (unsigned long)sectoff, errno); msgflush(); exit(11); } /* Read the next block into memory */ nbytes = read(fd, buffer, geo.blocksize); if (nbytes < 0) { message("ERROR: read from %s failed: %d\n", charname, errno); msgflush(); exit(12); } else if (nbytes == 0) { message("ERROR: Unexpected end-of file in %s\n", charname); msgflush(); exit(13); } else if (nbytes != geo.blocksize) { message("ERROR: Unexpected read size from %s: %ld\n", charname, (unsigned long)nbytes); msgflush(); exit(14); } /* Since we forced the size of the partition to be an even number * of erase blocks, we do not expect to encounter the end of file * indication. */ else if (nbytes == 0) { message("ERROR: Unexpected end of file on %s\n", charname); msgflush(); exit(15); } /* This is not expected at all */ else if (nbytes != geo.blocksize) { message("ERROR: Short read from %s failed: %lu\n", charname, (unsigned long)nbytes); msgflush(); exit(16); } /* Verfy the offsets in the block */ for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++) { if (buffer[k] != check) { message("ERROR: Bad offset %lu, expected %lu\n", (long)buffer[k], (long)check); msgflush(); exit(17); } /* Invert the value to indicate that we have verified * this value. */ buffer[k] = ~check; check += sizeof(uint32_t); } /* Seek to the next write position */ seekpos = lseek(fd, sectoff, SEEK_SET); if (seekpos != sectoff) { message("ERROR: lseek to offset %ld failed: %d\n", (unsigned long)sectoff, errno); msgflush(); exit(18); } /* Now write the block back to FLASH with the modified value */ nbytes = write(fd, buffer, geo.blocksize); if (nbytes < 0) { message("ERROR: write to %s failed: %d\n", charname, errno); msgflush(); exit(19); } else if (nbytes != geo.blocksize) { message("ERROR: Unexpected write size to %s: %ld\n", charname, (unsigned long)nbytes); msgflush(); exit(20); } /* Get the offset to the next block */ sectoff += geo.blocksize; } /* Try reading one more time. We should get the end of file */ nbytes = read(fd, buffer, geo.blocksize); if (nbytes != 0) { message("ERROR: Expected end-of-file from %s failed: %d %d\n", charname, nbytes, errno); msgflush(); exit(22); } close(fd); } /* Now verify that all of the verifed blocks appear where we thing they * should on the device. */ message("Verfying media:\n"); fd = open("/dev/mtd0", O_RDONLY); if (fd < 0) { message("ERROR: open /dev/mtd0 failed: %d\n", errno); msgflush(); exit(23); } offset = 0; check = 0; for (i = 0; i < nblocks * CONFIG_EXAMPLES_MTDPART_NPARTITIONS; i++) { /* Read the next block into memory */ nbytes = read(fd, buffer, geo.blocksize); if (nbytes < 0) { message("ERROR: read from %s failed: %d\n", charname, errno); msgflush(); exit(24); } else if (nbytes == 0) { message("ERROR: Unexpected end-of file in %s\n", charname); msgflush(); exit(25); } else if (nbytes != geo.blocksize) { message("ERROR: Unexpected read size from %s: %ld\n", charname, (unsigned long)nbytes); msgflush(); exit(26); } /* Verfy the values in the block */ for (k = 0; k < geo.blocksize / sizeof(uint32_t); k++) { if (buffer[k] != ~check) { message("ERROR: Bad value %lu, expected %lu\n", (long)buffer[k], (long)(~check)); msgflush(); exit(27); } check += sizeof(uint32_t); } } close(fd); /* And exit without bothering to clean up */ message("PASS: Everything looks good\n"); msgflush(); return 0; }
int smart_main(int argc, char *argv[]) { FAR struct mtd_dev_s *mtd; unsigned int i; int ret; /* Seed the random number generated */ srand(0x93846); /* Create and initialize a RAM MTD device instance */ #ifdef CONFIG_EXAMPLES_SMART_ARCHINIT mtd = smart_archinitialize(); #else mtd = rammtd_initialize(g_simflash, EXAMPLES_SMART_BUFSIZE); #endif if (!mtd) { message("ERROR: Failed to create RAM MTD instance\n"); msgflush(); exit(1); } /* Initialize to provide SMART on an MTD interface */ MTD_IOCTL(mtd, MTDIOC_BULKERASE, 0); ret = smart_initialize(1, mtd); if (ret < 0) { message("ERROR: SMART initialization failed: %d\n", -ret); msgflush(); exit(2); } /* Creaet a SMARTFS filesystem */ ret = mksmartfs("/dev/smart1"); /* Mount the file system */ ret = mount("/dev/smart1", CONFIG_EXAMPLES_SMART_MOUNTPT, "smartfs", 0, NULL); if (ret < 0) { message("ERROR: Failed to mount the SMART volume: %d\n", errno); msgflush(); exit(3); } /* Set up memory monitoring */ #ifdef CONFIG_CAN_PASS_STRUCTS g_mmbefore = mallinfo(); g_mmprevious = g_mmbefore; #else (void)mallinfo(&g_mmbefore); memcpy(&g_mmprevious, &g_mmbefore, sizeof(struct mallinfo)); #endif /* Loop a few times ... file the file system with some random, files, * delete some files randomly, fill the file system with more random file, * delete, etc. This beats the FLASH very hard! */ #if CONFIG_EXAMPLES_SMART_NLOOPS == 0 for (i = 0; ; i++) #else for (i = 1; i <= CONFIG_EXAMPLES_SMART_NLOOPS; i++) #endif { /* Write a files to the SMART file system until either (1) all of the * open file structures are utilized or until (2) SMART reports an error * (hopefully that the file system is full) */ message("\n=== FILLING %d =============================\n", i); ret = smart_fillfs(); message("Filled file system\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); /* Directory listing */ smart_directory(); /* Verify all files written to FLASH */ ret = smart_verifyfs(); if (ret < 0) { message("ERROR: Failed to verify files\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); } else { #if CONFIG_EXAMPLES_SMART_VERBOSE != 0 message("Verified!\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); #endif } /* Delete some files */ message("\n=== DELETING %d ============================\n", i); ret = smart_delfiles(); if (ret < 0) { message("ERROR: Failed to delete files\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); } else { message("Deleted some files\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); } /* Directory listing */ smart_directory(); /* Verify all files written to FLASH */ ret = smart_verifyfs(); if (ret < 0) { message("ERROR: Failed to verify files\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); } else { #if CONFIG_EXAMPLES_SMART_VERBOSE != 0 message("Verified!\n"); message(" Number of files: %d\n", g_nfiles); message(" Number deleted: %d\n", g_ndeleted); #endif } /* Show memory usage */ smart_loopmemusage(); msgflush(); } /* Delete all files then show memory usage again */ smart_delallfiles(); smart_endmemusage(); msgflush(); return 0; }
int nsh_archinitialize(void) { #if defined(HAVE_USBHOST) || defined(HAVE_USBMONITOR) int ret; #endif #ifdef CONFIG_STM32_SPI3 FAR struct spi_dev_s *spi; FAR struct mtd_dev_s *mtd; #endif int ret; /* Configure SPI-based devices */ #ifdef CONFIG_STM32_SPI3 /* Get the SPI port */ message("nsh_archinitialize: Initializing SPI port 3\n"); spi = up_spiinitialize(3); if (!spi) { message("nsh_archinitialize: Failed to initialize SPI port 3\n"); return -ENODEV; } message("nsh_archinitialize: Successfully initialized SPI port 3\n"); /* Now bind the SPI interface to the M25P8 SPI FLASH driver */ #if defined(CONFIG_MTD) && defined(CONFIG_MIKROE_FLASH) message("nsh_archinitialize: Bind SPI to the SPI flash driver\n"); mtd = m25p_initialize(spi); if (!mtd) { message("nsh_archinitialize: Failed to bind SPI port 3 to the SPI FLASH driver\n"); } else { message("nsh_archinitialize: Successfully bound SPI port 3 to the SPI FLASH driver\n"); #ifdef CONFIG_MIKROE_FLASH_PART { int partno; int partsize; int partoffset; const char *partstring = CONFIG_MIKROE_FLASH_PART_LIST; const char *ptr; FAR struct mtd_dev_s *mtd_part; char partname[4]; /* Now create a partition on the FLASH device */ partno = 0; ptr = partstring; partoffset = 0; while (*ptr != '\0') { /* Get the partition size */ partsize = atoi(ptr); mtd_part = mtd_partition(mtd, partoffset, (partsize>>2)*16); partoffset += (partsize >> 2) * 16; #ifdef CONFIG_MIKROE_FLASH_CONFIG_PART /* Test if this is the config partition */ if (CONFIG_MIKROE_FLASH_CONFIG_PART_NUMBER == partno) { /* Register the partition as the config device */ mtdconfig_register(mtd_part); } else #endif { /* Now initialize a SMART Flash block device and bind it * to the MTD device. */ #if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS) sprintf(partname, "p%d", partno); smart_initialize(CONFIG_MIKROE_FLASH_MINOR, mtd_part, partname); #endif } /* Update the pointer to point to the next size in the list */ while ((*ptr >= '0') && (*ptr <= '9')) { ptr++; } if (*ptr == ',') { ptr++; } /* Increment the part number */ partno++; } #else /* CONFIG_MIKROE_FLASH_PART */ /* Configure the device with no partition support */ smart_initialize(CONFIG_MIKROE_FLASH_MINOR, mtd, NULL); #endif /* CONFIG_MIKROE_FLASH_PART */ } } /* Create a RAM MTD device if configured */ #if defined(CONFIG_RAMMTD) && defined(CONFIG_MIKROE_RAMMTD) { uint8_t *start = (uint8_t *) kmalloc(CONFIG_MIKROE_RAMMTD_SIZE * 1024); mtd = rammtd_initialize(start, CONFIG_MIKROE_RAMMTD_SIZE * 1024); mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); /* Now initialize a SMART Flash block device and bind it to the MTD device */ #if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS) smart_initialize(CONFIG_MIKROE_RAMMTD_MINOR, mtd, NULL); #endif } #endif /* CONFIG_RAMMTD && CONFIG_MIKROE_RAMMTD */ #endif /* CONFIG_MTD */ #endif /* CONFIG_STM32_SPI3 */ /* Create the SPI FLASH MTD instance */ /* The M25Pxx is not a good media to implement a file system.. * its block sizes are too large */ /* Mount the SDIO-based MMC/SD block driver */ #ifdef NSH_HAVEMMCSD /* Bind the spi interface to the MMC/SD driver */ message("nsh_archinitialize: Bind SDIO to the MMC/SD driver, minor=%d\n", CONFIG_NSH_MMCSDMINOR); ret = mmcsd_spislotinitialize(CONFIG_NSH_MMCSDMINOR, CONFIG_NSH_MMCSDSLOTNO, spi); if (ret != OK) { message("nsh_archinitialize: Failed to bind SPI to the MMC/SD driver: %d\n", ret); } else { message("nsh_archinitialize: Successfully bound SPI to the MMC/SD driver\n"); } #endif #ifdef HAVE_USBHOST /* Initialize USB host operation. stm32_usbhost_initialize() starts a thread * will monitor for USB connection and disconnection events. */ ret = stm32_usbhost_initialize(); if (ret != OK) { message("nsh_archinitialize: Failed to initialize USB host: %d\n", ret); return ret; } #endif #ifdef HAVE_USBMONITOR /* Start the USB Monitor */ ret = usbmonitor_start(0, NULL); if (ret != OK) { message("nsh_archinitialize: Start USB monitor: %d\n", ret); } #endif #ifdef CONFIG_LCD_MIO283QT2 /* Configure the TFT LCD module */ message("nsh_archinitialize: Initializing TFT LCD module\n"); ret = up_lcdinitialize(); if (ret != OK) { message("nsh_archinitialize: Failed to initialize TFT LCD module\n"); } #endif /* Configure the Audio sub-system if enabled and bind it to SPI 3 */ #ifdef CONFIG_AUDIO up_vs1053initialize(spi); #endif return OK; }
int board_app_initialize(void) { #if defined(CONFIG_STM32_SPI4) FAR struct spi_dev_s *spi; FAR struct mtd_dev_s *mtd; FAR struct mtd_geometry_s geo; #endif #if defined(CONFIG_MTD_PARTITION_NAMES) FAR const char *partname = CONFIG_STM32F429I_DISCO_FLASH_PART_NAMES; #endif #if defined(CONFIG_MTD) && defined(CONFIG_MTD_SST25XX) int ret; #elif defined(HAVE_USBHOST) || defined(HAVE_USBMONITOR) int ret; #endif /* Configure SPI-based devices */ #ifdef CONFIG_STM32_SPI4 /* Get the SPI port */ syslog(LOG_INFO, "Initializing SPI port 4\n"); spi = stm32_spibus_initialize(4); if (!spi) { syslog(LOG_ERR, "ERROR: Failed to initialize SPI port 4\n"); return -ENODEV; } syslog(LOG_INFO, "Successfully initialized SPI port 4\n"); /* Now bind the SPI interface to the SST25F064 SPI FLASH driver. This * is a FLASH device that has been added external to the board (i.e. * the board does not ship from STM with any on-board FLASH. */ #if defined(CONFIG_MTD) && defined(CONFIG_MTD_SST25XX) syslog(LOG_INFO, "Bind SPI to the SPI flash driver\n"); mtd = sst25xx_initialize(spi); if (!mtd) { syslog(LOG_ERR, "ERROR: Failed to bind SPI port 4 to the SPI FLASH driver\n"); } else { syslog(LOG_INFO, "Successfully bound SPI port 4 to the SPI FLASH driver\n"); /* Get the geometry of the FLASH device */ ret = mtd->ioctl(mtd, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo)); if (ret < 0) { fdbg("ERROR: mtd->ioctl failed: %d\n", ret); return ret; } #ifdef CONFIG_STM32F429I_DISCO_FLASH_PART { int partno; int partsize; int partoffset; int partszbytes; int erasesize; const char *partstring = CONFIG_STM32F429I_DISCO_FLASH_PART_LIST; const char *ptr; FAR struct mtd_dev_s *mtd_part; char partref[4]; /* Now create a partition on the FLASH device */ partno = 0; ptr = partstring; partoffset = 0; /* Get the Flash erase size */ erasesize = geo.erasesize; while (*ptr != '\0') { /* Get the partition size */ partsize = atoi(ptr); partszbytes = (partsize << 10); /* partsize is defined in KB */ /* Check if partition size is bigger then erase block */ if (partszbytes < erasesize) { fdbg("ERROR: Partition size is lesser than erasesize!\n"); return -1; } /* Check if partition size is multiple of erase block */ if ((partszbytes % erasesize) != 0) { fdbg("ERROR: Partition size is not multiple of erasesize!\n"); return -1; } mtd_part = mtd_partition(mtd, partoffset, partszbytes / erasesize); partoffset += partszbytes / erasesize; #ifdef CONFIG_STM32F429I_DISCO_FLASH_CONFIG_PART /* Test if this is the config partition */ if (CONFIG_STM32F429I_DISCO_FLASH_CONFIG_PART_NUMBER == partno) { /* Register the partition as the config device */ mtdconfig_register(mtd_part); } else #endif { /* Now initialize a SMART Flash block device and bind it * to the MTD device. */ #if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS) sprintf(partref, "p%d", partno); smart_initialize(CONFIG_STM32F429I_DISCO_FLASH_MINOR, mtd_part, partref); #endif } #if defined(CONFIG_MTD_PARTITION_NAMES) /* Set the partition name */ if (mtd_part == NULL) { dbg("Error: failed to create partition %s\n", partname); return -1; } mtd_setpartitionname(mtd_part, partname); /* Now skip to next name. We don't need to split the string here * because the MTD partition logic will only display names up to * the comma, thus allowing us to use a single static name * in the code. */ while (*partname != ',' && *partname != '\0') { /* Skip to next ',' */ partname++; } if (*partname == ',') { partname++; } #endif /* Update the pointer to point to the next size in the list */ while ((*ptr >= '0') && (*ptr <= '9')) { ptr++; } if (*ptr == ',') { ptr++; } /* Increment the part number */ partno++; } } #else /* CONFIG_STM32F429I_DISCO_FLASH_PART */ /* Configure the device with no partition support */ smart_initialize(CONFIG_STM32F429I_DISCO_FLASH_MINOR, mtd, NULL); #endif /* CONFIG_STM32F429I_DISCO_FLASH_PART */ } #endif /* CONFIG_MTD */ #endif /* CONFIG_STM32_SPI4 */ /* Create a RAM MTD device if configured */ #if defined(CONFIG_RAMMTD) && defined(CONFIG_STM32F429I_DISCO_RAMMTD) { uint8_t *start = (uint8_t *) kmm_malloc(CONFIG_STM32F429I_DISCO_RAMMTD_SIZE * 1024); mtd = rammtd_initialize(start, CONFIG_STM32F429I_DISCO_RAMMTD_SIZE * 1024); mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); /* Now initialize a SMART Flash block device and bind it to the MTD device */ #if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS) smart_initialize(CONFIG_STM32F429I_DISCO_RAMMTD_MINOR, mtd, NULL); #endif } #endif /* CONFIG_RAMMTD && CONFIG_STM32F429I_DISCO_RAMMTD */ #ifdef HAVE_USBHOST /* Initialize USB host operation. stm32_usbhost_initialize() starts a thread * will monitor for USB connection and disconnection events. */ ret = stm32_usbhost_initialize(); if (ret != OK) { syslog(LOG_ERR, "ERROR: Failed to initialize USB host: %d\n", ret); return ret; } #endif #ifdef HAVE_USBMONITOR /* Start the USB Monitor */ ret = usbmonitor_start(0, NULL); if (ret != OK) { syslog(LOG_ERR, "ERROR: Failed to start USB monitor: %d\n", ret); } #endif return OK; }
int configdata_main(int argc, char *argv[]) { unsigned int i; int ret; FAR struct mtd_dev_s *mtd; /* Seed the random number generated */ srand(0x93846); /* Create and initialize a RAM MTD device instance */ #ifdef CONFIG_EXAMPLES_CONFIGDATA_ARCHINIT mtd = configdata_archinitialize(); #else #if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0 message("Creating %d byte RAM drive\n", EXAMPLES_CONFIGDATA_BUFSIZE); #endif mtd = rammtd_initialize(g_simflash, EXAMPLES_CONFIGDATA_BUFSIZE); #endif if (!mtd) { message("ERROR: Failed to create RAM MTD instance\n"); msgflush(); exit(1); } /* Initialize to provide CONFIGDATA on an MTD interface */ #if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0 message("Registering /dev/config device\n"); #endif MTD_IOCTL(mtd, MTDIOC_BULKERASE, 0); ret = mtdconfig_register(mtd); if (ret < 0) { message("ERROR: /dev/config registration failed: %d\n", -ret); msgflush(); exit(2); } /* Zero out our entry array */ memset(g_entries, 0, sizeof(g_entries)); /* Open the /dev/config device */ g_fd = open("/dev/config", O_RDOK); if (g_fd == -1) { message("ERROR: Failed to open /dev/config %d\n", -errno); msgflush(); exit(2); } /* Initialize the before memory values */ #ifdef CONFIG_CAN_PASS_STRUCTS g_mmbefore = mallinfo(); #else (void)mallinfo(&g_mmbefore); #endif /* Loop seveal times ... create some config data items, delete them * randomly, verify them randomly, add new config items. */ g_ntests = g_nverified = 0; g_ntotaldelete = g_ntotalalloc = 0; #if CONFIG_EXAMPLES_CONFIGDATA_NLOOPS == 0 for (i = 0; ; i++) #else for (i = 1; i <= CONFIG_EXAMPLES_CONFIGDATA_NLOOPS; i++) #endif { /* Write config data to the /dev/config device until either (1) all of the * open file structures are utilized or until (2) CONFIGDATA reports an error * (hopefully that the /dev/config device is full) */ #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT message("\n=== FILLING %d =============================\n", i); #endif ret = configdata_fillconfig(); #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT message("Filled /dev/config\n"); message(" Number of entries: %d\n", g_nentries); #endif /* Verify all files entries to FLASH */ ret = configdata_verifyconfig(); if (ret < 0) { message("ERROR: Failed to verify partition\n"); message(" Number of entries: %d\n", g_nentries); } else { #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT #if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0 message("Verified!\n"); message(" Number of entries: %d\n", g_nentries); #endif #endif } /* Delete some entries */ #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT message("\n=== DELETING %d ============================\n", i); #endif ret = configdata_delentries(); if (ret < 0) { message("ERROR: Failed to delete enries\n"); message(" Number of entries: %d\n", g_nentries); message(" Number deleted: %d\n", g_ndeleted); } else { #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT message("Deleted some enries\n"); message(" Number of entries: %d\n", g_nentries); message(" Number deleted: %d\n", g_ndeleted); #endif } /* Verify all files written to FLASH */ ret = configdata_verifyconfig(); if (ret < 0) { message("ERROR: Failed to verify partition\n"); message(" Number of entries: %d\n", g_nentries); message(" Number deleted: %d\n", g_ndeleted); } else { #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT #if CONFIG_EXAMPLES_CONFIGDATA_VERBOSE != 0 message("Verified!\n"); message(" Number of entries: %d\n", g_nentries); message(" Number deleted: %d\n", g_ndeleted); #endif #endif } /* Clear deleted entries */ configdata_cleardeleted(); /* Show memory usage */ #ifndef CONFIG_EXAMPLES_CONFIGDATA_SILENT configdata_loopmemusage(); msgflush(); #else if ((i % EXAMPLES_CONFIGDATA_REPORT) == 0) { message("%d\n", i); msgflush(); } #endif } /* Delete all files then show memory usage again */ //configdata_delallfiles(); configdata_endmemusage(); msgflush(); return 0; }
int nsh_archinitialize(void) { #if defined(HAVE_USBHOST) || defined(HAVE_USBMONITOR) int ret; #endif #if defined(CONFIG_STM32_SPI5) || defined(CONFIG_STM32_SPI4) FAR struct spi_dev_s *spi; FAR struct mtd_dev_s *mtd; #endif #if defined(CONFIG_MTD_PARTITION_NAMES) FAR const char *partname = CONFIG_STM32F429I_DISCO_FLASH_PART_NAMES; #endif /* Configure SPI-based devices */ #ifdef CONFIG_STM32_SPI4 /* Get the SPI port */ message("nsh_archinitialize: Initializing SPI port 4\n"); spi = up_spiinitialize(4); if (!spi) { message("nsh_archinitialize: Failed to initialize SPI port 4\n"); return -ENODEV; } message("nsh_archinitialize: Successfully initialized SPI port 4\n"); /* Now bind the SPI interface to the SST25F064 SPI FLASH driver. This * is a FLASH device that has been added external to the board (i.e. * the board does not ship from STM with any on-board FLASH. */ #if defined(CONFIG_MTD) && defined(CONFIG_MTD_SST25XX) message("nsh_archinitialize: Bind SPI to the SPI flash driver\n"); mtd = sst25xx_initialize(spi); if (!mtd) { message("nsh_archinitialize: Failed to bind SPI port 4 to the SPI FLASH driver\n"); } else { message("nsh_archinitialize: Successfully bound SPI port 4 to the SPI FLASH driver\n"); #ifdef CONFIG_STM32F429I_DISCO_FLASH_PART { int partno; int partsize; int partoffset; const char *partstring = CONFIG_STM32F429I_DISCO_FLASH_PART_LIST; const char *ptr; FAR struct mtd_dev_s *mtd_part; char partref[4]; /* Now create a partition on the FLASH device */ partno = 0; ptr = partstring; partoffset = 0; while (*ptr != '\0') { /* Get the partition size */ partsize = atoi(ptr); mtd_part = mtd_partition(mtd, partoffset, (partsize>>2) * 16); partoffset += (partsize >> 2) * 16; #ifdef CONFIG_STM32F429I_DISCO_FLASH_CONFIG_PART /* Test if this is the config partition */ if (CONFIG_STM32F429I_DISCO_FLASH_CONFIG_PART_NUMBER == partno) { /* Register the partition as the config device */ mtdconfig_register(mtd_part); } else #endif { /* Now initialize a SMART Flash block device and bind it * to the MTD device. */ #if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS) sprintf(partref, "p%d", partno); smart_initialize(CONFIG_STM32F429I_DISCO_FLASH_MINOR, mtd_part, partref); #endif } /* Set the partition name */ #if defined(CONFIG_MTD_PARTITION_NAMES) mtd_setpartitionname(mtd_part, partname); /* Now skip to next name. We don't need to split the string here * because the MTD partition logic will only display names up to * the comma, thus allowing us to use a single static name * in the code. */ while (*partname != ',' && *partname != '\0') { /* Skip to next ',' */ partname++; } if (*partname == ',') { partname++; } #endif /* Update the pointer to point to the next size in the list */ while ((*ptr >= '0') && (*ptr <= '9')) { ptr++; } if (*ptr == ',') { ptr++; } /* Increment the part number */ partno++; } } #else /* CONFIG_STM32F429I_DISCO_FLASH_PART */ /* Configure the device with no partition support */ smart_initialize(CONFIG_STM32F429I_DISCO_FLASH_MINOR, mtd, NULL); #endif /* CONFIG_STM32F429I_DISCO_FLASH_PART */ } #endif /* CONFIG_MTD */ #endif /* CONFIG_STM32_SPI4 */ /* Create a RAM MTD device if configured */ #if defined(CONFIG_RAMMTD) && defined(CONFIG_STM32F429I_DISCO_RAMMTD) { uint8_t *start = (uint8_t *) kmalloc(CONFIG_STM32F429I_DISCO_RAMMTD_SIZE * 1024); mtd = rammtd_initialize(start, CONFIG_STM32F429I_DISCO_RAMMTD_SIZE * 1024); mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); /* Now initialize a SMART Flash block device and bind it to the MTD device */ #if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS) smart_initialize(CONFIG_STM32F429I_DISCO_RAMMTD_MINOR, mtd, NULL); #endif } #endif /* CONFIG_RAMMTD && CONFIG_STM32F429I_DISCO_RAMMTD */ #ifdef HAVE_USBHOST /* Initialize USB host operation. stm32_usbhost_initialize() starts a thread * will monitor for USB connection and disconnection events. */ ret = stm32_usbhost_initialize(); if (ret != OK) { message("nsh_archinitialize: Failed to initialize USB host: %d\n", ret); return ret; } #ifdef CONFIG_USBHOST_MSC /* Initialize the USB storage class */ ret = usbhost_storageinit(); if (ret != OK) { message("nsh_archinitialize: Failed to initialize USB host storage: %d\n", ret); return ret; } #endif #endif #ifdef HAVE_USBMONITOR /* Start the USB Monitor */ ret = usbmonitor_start(0, NULL); if (ret != OK) { message("nsh_archinitialize: Start USB monitor: %d\n", ret); } #endif return OK; }