static inline void smart_randname(FAR struct smart_filedesc_s *file) { int dirlen; int maxname; int namelen; int alloclen; int i; dirlen = strlen(g_mountdir); maxname = CONFIG_EXAMPLES_SMART_MAXNAME - dirlen; namelen = (rand() % maxname) + 1; alloclen = namelen + dirlen; file->name = (FAR char*)malloc(alloclen + 1); if (!file->name) { message("ERROR: Failed to allocate name, length=%d\n", namelen); msgflush(); exit(5); } memcpy(file->name, g_mountdir, dirlen); for (i = dirlen; i < alloclen; i++) { file->name[i] = smart_randchar(); } file->name[alloclen] = '\0'; }
int qe_main(int argc, char *argv[]) { int32_t position; int fd; int exitval = EXIT_SUCCESS; int ret; #if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_QENCODER_NSAMPLES) int nloops; #endif /* Check if we have initialized */ if (!g_qeexample.initialized) { /* Initialization of the encoder hardware is performed by logic external to * this test. */ message("qe_main: Initializing external encoder(s)\n"); ret = qe_devinit(); if (ret != OK) { message("qe_main: qe_devinit failed: %d\n", ret); exitval = EXIT_FAILURE; goto errout; } /* Set the default values */ qe_devpath(CONFIG_EXAMPLES_QENCODER_DEVPATH); g_qeexample.initialized = true; } /* Parse command line arguments */ #ifdef CONFIG_NSH_BUILTIN_APPS parse_args(argc, argv); #endif /* Open the encoder device for reading */ message("qe_main: Hardware initialized. Opening the encoder device: %s\n", g_qeexample.devpath); fd = open(g_qeexample.devpath, O_RDONLY); if (fd < 0) { message("qe_main: open %s failed: %d\n", g_qeexample.devpath, errno); exitval = EXIT_FAILURE; goto errout_with_dev; } /* Reset the count if so requested */ if (g_qeexample.reset) { message("qe_main: Resetting the count...\n"); ret = ioctl(fd, QEIOC_RESET, 0); if (ret < 0) { message("qe_main: ioctl(QEIOC_RESET) failed: %d\n", errno); exitval = EXIT_FAILURE; goto errout_with_dev; } } /* Now loop the appropriate number of times, displaying the collected * encoder samples. */ #if defined(CONFIG_NSH_BUILTIN_APPS) message("qe_main: Number of samples: %d\n", g_qeexample.nloops); for (nloops = 0; nloops < g_qeexample.nloops; nloops++) #elif defined(CONFIG_EXAMPLES_QENCODER_NSAMPLES) message("qe_main: Number of samples: %d\n", CONFIG_EXAMPLES_QENCODER_NSAMPLES); for (nloops = 0; nloops < CONFIG_EXAMPLES_QENCODER_NSAMPLES; nloops++) #else for (;;) #endif { /* Flush any output before the loop entered or from the previous pass * through the loop. */ msgflush(); /* Get the positions data using the ioctl */ ret = ioctl(fd, QEIOC_POSITION, (unsigned long)((uintptr_t)&position)); if (ret < 0) { message("qe_main: ioctl(QEIOC_POSITION) failed: %d\n", errno); exitval = EXIT_FAILURE; goto errout_with_dev; } /* Print the sample data on successful return */ else { message("qe_main: %3d. %d\n", nloops+1, position); } /* Delay a little bit */ #if defined(CONFIG_NSH_BUILTIN_APPS) usleep(g_qeexample.delay * 1000); #else usleep(CONFIG_EXAMPLES_QENCODER_DELAY * 1000); #endif } errout_with_dev: close(fd); errout: message("Terminating!\n"); msgflush(); return exitval; }
int wdog_main(int argc, char *argv[]) { struct wdog_example_s wdog; #ifdef CONFIG_DEBUG_WATCHDOG struct watchdog_status_s status; #endif long elapsed; int fd; int ret; /* Parse the command line */ parse_args(&wdog, argc, argv); /* Initialization of the WATCHDOG hardware is performed by logic external to * this test. */ ret = up_wdginitialize(); if (ret != OK) { message("wdog_main: up_wdginitialize failed: %d\n", ret); goto errout; } /* Open the watchdog device for reading */ fd = open(CONFIG_EXAMPLES_WATCHDOG_DEVPATH, O_RDONLY); if (fd < 0) { message("wdog_main: open %s failed: %d\n", CONFIG_EXAMPLES_WATCHDOG_DEVPATH, errno); goto errout; } /* Set the watchdog timeout */ ret = ioctl(fd, WDIOC_SETTIMEOUT, (unsigned long)wdog.timeout); if (ret < 0) { message("wdog_main: ioctl(WDIOC_SETTIMEOUT) failed: %d\n", errno); goto errout_with_dev; } /* Then start the watchdog timer. */ ret = ioctl(fd, WDIOC_START, 0); if (ret < 0) { message("wdog_main: ioctl(WDIOC_START) failed: %d\n", errno); goto errout_with_dev; } /* Then ping */ for (elapsed = 0; elapsed < wdog.pingtime; elapsed += wdog.pingdelay) { /* Sleep for the requested amount of time */ usleep(wdog.pingdelay * 1000); /* Show watchdog status. Only if debug is enabled because this * could interfere with the timer. */ #ifdef CONFIG_DEBUG_WATCHDOG ret = ioctl(fd, WDIOC_GETSTATUS, (unsigned long)&status); if (ret < 0) { message("wdog_main: ioctl(WDIOC_GETSTATUS) failed: %d\n", errno); goto errout_with_dev; } message("wdog_main: flags=%08x timeout=%d timeleft=%d\n", status.flags, status.timeout, status.timeleft); #endif /* Then ping */ ret = ioctl(fd, WDIOC_KEEPALIVE, 0); if (ret < 0) { message("wdog_main: ioctl(WDIOC_KEEPALIVE) failed: %d\n", errno); goto errout_with_dev; } message(" ping elapsed=%ld\n", elapsed); msgflush(); } /* Then stop pinging */ for (; ; elapsed += wdog.pingdelay) { /* Sleep for the requested amount of time */ usleep(wdog.pingdelay * 1000); /* Show watchdog status. Only if debug is enabled because this * could interfere with the timer. */ #ifdef CONFIG_DEBUG_WATCHDOG ret = ioctl(fd, WDIOC_GETSTATUS, (unsigned long)&status); if (ret < 0) { message("wdog_main: ioctl(WDIOC_GETSTATUS) failed: %d\n", errno); goto errout_with_dev; } message("wdog_main: flags=%08x timeout=%d timeleft=%d\n", status.flags, status.timeout, status.timeleft); #endif message(" NO ping elapsed=%ld\n", elapsed); msgflush(); } /* We should not get here */ ret = ioctl(fd, WDIOC_STOP, 0); if (ret < 0) { message("wdog_main: ioctl(WDIOC_STOP) failed: %d\n", errno); goto errout_with_dev; } close(fd); msgflush(); return OK; errout_with_dev: close(fd); errout: msgflush(); return ERROR; }
int pwm_main(int argc, char *argv[]) { struct pwm_info_s info; int fd; int ret; /* Initialize the state data */ if (!g_pwmstate.initialized) { g_pwmstate.duty = CONFIG_EXAMPLES_PWM_DUTYPCT; g_pwmstate.freq = CONFIG_EXAMPLES_PWM_FREQUENCY; g_pwmstate.duration = CONFIG_EXAMPLES_PWM_DURATION; #ifdef CONFIG_PWM_PULSECOUNT g_pwmstate.count = CONFIG_EXAMPLES_PWM_PULSECOUNT; #endif g_pwmstate.initialized = true; } /* Parse the command line */ parse_args(&g_pwmstate, argc, argv); /* Has a device been assigned? */ if (!g_pwmstate.devpath) { /* No.. use the default device */ pwm_devpath(&g_pwmstate, CONFIG_EXAMPLES_PWM_DEVPATH); } /* Initialization of the PWM hardware is performed by logic external to * this test. */ ret = pwm_devinit(); if (ret != OK) { message("pwm_main: pwm_devinit failed: %d\n", ret); goto errout; } /* Open the PWM device for reading */ fd = open(g_pwmstate.devpath, O_RDONLY); if (fd < 0) { message("pwm_main: open %s failed: %d\n", g_pwmstate.devpath, errno); goto errout; } /* Configure the characteristics of the pulse train */ info.frequency = g_pwmstate.freq; info.duty = ((uint32_t)g_pwmstate.duty << 16) / 100; #ifdef CONFIG_PWM_PULSECOUNT info.count = g_pwmstate.count; message("pwm_main: starting output with frequency: %d duty: %08x count: %d\n", info.frequency, info.duty, info.count); #else message("pwm_main: starting output with frequency: %d duty: %08x\n", info.frequency, info.duty); #endif ret = ioctl(fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&info)); if (ret < 0) { message("pwm_main: ioctl(PWMIOC_SETCHARACTERISTICS) failed: %d\n", errno); goto errout_with_dev; } /* Then start the pulse train. Since the driver was opened in blocking * mode, this call will block if the count value is greater than zero. */ ret = ioctl(fd, PWMIOC_START, 0); if (ret < 0) { message("pwm_main: ioctl(PWMIOC_START) failed: %d\n", errno); goto errout_with_dev; } /* It a non-zero count was not specified, then wait for the selected * duration, then stop the PWM output. */ #ifdef CONFIG_PWM_PULSECOUNT if (info.count == 0) #endif { /* Wait for the specified duration */ sleep(g_pwmstate.duration); /* Then stop the pulse train */ message("pwm_main: stopping output\n", info.frequency, info.duty); ret = ioctl(fd, PWMIOC_STOP, 0); if (ret < 0) { message("pwm_main: ioctl(PWMIOC_STOP) failed: %d\n", errno); goto errout_with_dev; } } close(fd); msgflush(); return OK; errout_with_dev: close(fd); errout: msgflush(); return ERROR; }
pthread_addr_t i2schar_receiver(pthread_addr_t arg) { FAR struct ap_buffer_s *apb; struct audio_buf_desc_s desc; int bufsize; int nread; int ret; int fd; int i; /* Open the I2C character device */ fd = open(g_i2schar.devpath, O_RDONLY); if (fd < 0) { int errcode = errno; message("i2schar_receiver: ERROR: failed to open %s: %d\n", g_i2schar.devpath, errcode); pthread_exit(NULL); } /* Loop for the requested number of times */ for (i = 0; i < CONFIG_EXAMPLES_I2SCHAR_TXBUFFERS; i++) { /* Allocate an audio buffer of the configured size */ desc.numbytes = CONFIG_EXAMPLES_I2SCHAR_BUFSIZE; desc.u.ppBuffer = &apb; ret = apb_alloc(&desc); if (ret < 0) { message("i2schar_receiver: ERROR: failed to allocate buffer %d: %d\n", i+1, ret); close(fd); pthread_exit(NULL); } bufsize = sizeof(struct ap_buffer_s) + CONFIG_EXAMPLES_I2SCHAR_BUFSIZE; /* Then receifve into the buffer */ do { /* Flush any output before reading */ msgflush(); /* Read the buffer to the I2S character driver */ nread = read(fd, apb, bufsize); if (nread < 0) { int errcode = errno; if (errcode != EINTR) { message("i2schar_receiver: ERROR: read failed: %d\n", errcode); close(fd); pthread_exit(NULL); } } else if (nread != bufsize) { message("i2schar_receiver: ERROR: partial read: %d\n", nread); close(fd); pthread_exit(NULL); } else { message("i2schar_receiver: Received buffer %d\n", i+1); } } while (nread != bufsize); /* Make sure that the transmitter thread has a chance to run */ pthread_yield(); } close(fd); return NULL; }
pthread_addr_t i2schar_transmitter(pthread_addr_t arg) { FAR struct ap_buffer_s *apb; struct audio_buf_desc_s desc; uint8_t crap; uint8_t *ptr; int bufsize; int nwritten; int ret; int fd; int i; int j; /* Open the I2C character device */ fd = open(g_i2schar.devpath, O_WRONLY); if (fd < 0) { int errcode = errno; message("i2schar_transmitter: ERROR: failed to open %s: %d\n", g_i2schar.devpath, errcode); pthread_exit(NULL); } /* Loop for the requested number of times */ for (i = 0, crap = 0; i < CONFIG_EXAMPLES_I2SCHAR_TXBUFFERS; i++) { /* Allocate an audio buffer of the configured size */ desc.numbytes = CONFIG_EXAMPLES_I2SCHAR_BUFSIZE; desc.u.ppBuffer = &apb; ret = apb_alloc(&desc); if (ret < 0) { message("i2schar_transmitter: ERROR: failed to allocate buffer %d: %d\n", i+1, ret); close(fd); pthread_exit(NULL); } bufsize = sizeof(struct ap_buffer_s) + CONFIG_EXAMPLES_I2SCHAR_BUFSIZE; /* Fill the audio buffer with crap */ for (j = 0, ptr = apb->samp; j < CONFIG_EXAMPLES_I2SCHAR_BUFSIZE; j++) { *ptr++ = crap++; } apb->nbytes = CONFIG_EXAMPLES_I2SCHAR_BUFSIZE; /* Then send the buffer */ do { /* Flush any output before writing */ msgflush(); /* Write the buffer to the I2S character driver */ nwritten = write(fd, apb, bufsize); if (nwritten < 0) { int errcode = errno; if (errcode != EINTR) { message("i2schar_transmitter: ERROR: write failed: %d\n", errcode); close(fd); pthread_exit(NULL); } } else if (nwritten != bufsize) { message("i2schar_transmitter: ERROR: partial write: %d\n", nwritten); close(fd); pthread_exit(NULL); } else { message("i2schar_transmitter: Send buffer %d\n", i+1); } } while (nwritten != bufsize); /* Make sure that the receiver thread has a chance to run */ pthread_yield(); } close(fd); return NULL; }
int user_start(int argc, char *argv[]) { void *handle; int ret; /* Initialize USB trace output IDs */ usbtrace_enable(TRACE_BITSET); /* Register block drivers (architecture-specific) */ message("user_start: Creating block drivers\n"); ret = usbstrg_archinitialize(); if (ret < 0) { message("user_start: usbstrg_archinitialize failed: %d\n", -ret); return 1; } /* Then exports the LUN(s) */ message("user_start: Configuring with NLUNS=%d\n", CONFIG_EXAMPLES_USBSTRG_NLUNS); ret = usbstrg_configure(CONFIG_EXAMPLES_USBSTRG_NLUNS, &handle); if (ret < 0) { message("user_start: usbstrg_configure failed: %d\n", -ret); usbstrg_uninitialize(handle); return 2; } message("user_start: handle=%p\n", handle); message("user_start: Bind LUN=0 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH1); ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH1, 0, 0, 0, false); if (ret < 0) { message("user_start: usbstrg_bindlun failed for LUN 1 using %s: %d\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH1, -ret); usbstrg_uninitialize(handle); return 2; } #if CONFIG_EXAMPLES_USBSTRG_NLUNS > 1 message("user_start: Bind LUN=1 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH2); ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH2, 1, 0, 0, false); if (ret < 0) { message("user_start: usbstrg_bindlun failed for LUN 2 using %s: %d\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH2, -ret); usbstrg_uninitialize(handle); return 3; } #if CONFIG_EXAMPLES_USBSTRG_NLUNS > 2 message("user_start: Bind LUN=2 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH3); ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH3, 2, 0, 0, false); if (ret < 0) { message("user_start: usbstrg_bindlun failed for LUN 3 using %s: %d\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH3, -ret); usbstrg_uninitialize(handle); return 4; } #endif #endif ret = usbstrg_exportluns(handle); if (ret < 0) { message("user_start: usbstrg_exportluns failed: %d\n", -ret); usbstrg_uninitialize(handle); return 5; } /* Now just hang around and monitor the USB storage activity */ #ifndef CONFIG_DISABLE_SIGNALS for (;;) { msgflush(); sleep(5); #ifdef CONFIG_USBDEV_TRACE message("\nuser_start: USB TRACE DATA:\n"); ret = usbtrace_enumerate(usbstrg_enumerate, NULL); if (ret < 0) { message("user_start: usbtrace_enumerate failed: %d\n", -ret); usbstrg_uninitialize(handle); return 6; } #else message("user_start: Still alive\n"); #endif } #else message("user_start: Exiting\n"); #endif }
int conn_main(int argc, char *argv[]) { int ret; /* If this program is implemented as the NSH 'msconn' command, then we need to * do a little error checking to assure that we are not being called re-entrantly. */ #ifdef CONFIG_NSH_BUILTIN_APPS /* Check if there is a non-NULL USB mass storage device handle (meaning that the * USB mass storage device is already configured). */ if (g_composite.cmphandle) { message("conn_main: ERROR: Already connected\n"); return 1; } #endif #ifdef CONFIG_SYSTEM_COMPOSITE_DEBUGMM # ifdef CONFIG_CAN_PASS_STRUCTS g_composite.mmstart = mallinfo(); g_composite.mmprevious = g_composite.mmstart; # else (void)mallinfo(&g_composite.mmstart); memcpy(&g_composite.mmprevious, &g_composite.mmstart, sizeof(struct mallinfo)); # endif #endif /* Perform architecture-specific initialization */ message("conn_main: Performing architecture-specific intialization\n"); ret = composite_archinitialize(); if (ret < 0) { message("conn_main: composite_archinitialize failed: %d\n", -ret); return 1; } check_test_memory_usage("After composite_archinitialize()"); /* Initialize the USB composite device device */ g_composite.cmphandle = composite_initialize(); if (!g_composite.cmphandle) { message("conn_main: composite_initialize failed\n"); return 1; } check_test_memory_usage("After composite_initialize()"); #if CONFIG_USBDEV_TRACE && CONFIG_USBDEV_TRACE_INITIALIDSET != 0 /* If USB tracing is enabled and tracing of initial USB events is specified, * then dump all collected trace data to stdout */ sleep(5); ret = dumptrace(); if (ret < 0) { goto errout; } #endif /* It this program was configued as an NSH command, then just exit now. * Also, if signals are not enabled (and, hence, sleep() is not supported. * then we have not real option but to exit now. */ #if !defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_DISABLE_SIGNALS) /* Otherwise, this thread will hang around and monitor the USB activity */ /* Open the serial driver */ ret = open_serial(); if (ret < 0) { goto errout; } /* Now looping */ for (;;) { /* Sleep for a bit */ msgflush(); sleep(5); /* Echo any serial data */ ret = echo_serial(); if (ret < 0) { goto errout; } /* Dump trace data */ # ifdef CONFIG_USBDEV_TRACE message("\n" "conn_main: USB TRACE DATA:\n"); ret = dumptrace(); if (ret < 0) { goto errout; } check_test_memory_usage("After usbtrace_enumerate()"); # else message("conn_main: Still alive\n"); # endif } #else message("conn_main: Connected\n"); check_test_memory_usage("After composite device connection"); #endif /* Dump debug memory usage */ message("conn_main: Exiting\n"); #if !defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_DISABLE_SIGNALS) close(g_composite.infd); close(g_composite.outfd); #endif #ifdef CONFIG_NSH_BUILTIN_APPS #endif final_memory_usage("Final memory usage"); return 0; errout: #if !defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_DISABLE_SIGNALS) close(g_composite.infd); close(g_composite.outfd); #endif composite_uninitialize(g_composite.cmphandle); final_memory_usage("Final memory usage"); return 1; }
int adc_main(int argc, char *argv[]) { struct adc_msg_s sample[CONFIG_EXAMPLES_ADC_GROUPSIZE]; size_t readsize; ssize_t nbytes; int fd; int errval = 0; int ret; int i; /* Check if we have initialized */ if (!g_adcstate.initialized) { /* Initialization of the ADC hardware is performed by logic external to * this test. */ message("adc_main: Initializing external ADC device\n"); ret = adc_devinit(); if (ret != OK) { message("adc_main: adc_devinit failed: %d\n", ret); errval = 1; goto errout; } /* Set the default values */ adc_devpath(&g_adcstate, CONFIG_EXAMPLES_ADC_DEVPATH); #if CONFIG_EXAMPLES_ADC_NSAMPLES > 0 g_adcstate.count = CONFIG_EXAMPLES_ADC_NSAMPLES; #else g_adcstate.count = 1; #endif g_adcstate.initialized = true; } /* Parse the command line */ #ifdef CONFIG_NSH_BUILTIN_APPS parse_args(&g_adcstate, argc, argv); #endif /* If this example is configured as an NX add-on, then limit the number of * samples that we collect before returning. Otherwise, we never return */ #if defined(CONFIG_NSH_BUILTIN_APPS) || CONFIG_EXAMPLES_ADC_NSAMPLES > 0 message("adc_main: g_adcstate.count: %d\n", g_adcstate.count); #endif /* Open the ADC device for reading */ message("adc_main: Hardware initialized. Opening the ADC device: %s\n", g_adcstate.devpath); fd = open(g_adcstate.devpath, O_RDONLY); if (fd < 0) { message("adc_main: open %s failed: %d\n", g_adcstate.devpath, errno); errval = 2; goto errout_with_dev; } /* Now loop the appropriate number of times, displaying the collected * ADC samples. */ #if defined(CONFIG_NSH_BUILTIN_APPS) for (; g_adcstate.count > 0; g_adcstate.count--) #elif CONFIG_EXAMPLES_ADC_NSAMPLES > 0 for (g_adcstate.count = 0; g_adcstate.count < CONFIG_EXAMPLES_ADC_NSAMPLES; g_adcstate.count++) #else for (;;) #endif { /* Flush any output before the loop entered or from the previous pass * through the loop. */ msgflush(); /* Read CONFIG_EXAMPLES_ADC_GROUPSIZE samples */ readsize = CONFIG_EXAMPLES_ADC_GROUPSIZE * sizeof(struct adc_msg_s); nbytes = read(fd, sample, readsize); /* Handle unexpected return values */ if (nbytes < 0) { errval = errno; if (errval != EINTR) { message("adc_main: read %s failed: %d\n", g_adcstate.devpath, errval); errval = 3; goto errout_with_dev; } message("adc_main: Interrupted read...\n"); } else if (nbytes == 0) { message("adc_main: No data read, Ignoring\n"); } /* Print the sample data on successful return */ else { int nsamples = nbytes / sizeof(struct adc_msg_s); if (nsamples * sizeof(struct adc_msg_s) != nbytes) { message("adc_main: read size=%d is not a multiple of sample size=%d, Ignoring\n", nbytes, sizeof(struct adc_msg_s)); } else { message("Sample:\n"); for (i = 0; i < nsamples ; i++) { message("%d: channel: %d value: %d\n", i, sample[i].am_channel, sample[i].am_data); } } } } errout_with_dev: close(fd); errout: message("Terminating!\n"); msgflush(); return errval; }
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 MAIN_NAME(int argc, char *argv[]) { FAR void *handle; int ret; /* If this program is implemented as the NSH 'msconn' command, then we need to * do a little error checking to assure that we are not being called re-entrantly. */ #ifdef CONFIG_EXAMPLES_USBSTRG_BUILTIN /* Check if there is a non-NULL USB mass storage device handle (meaning that the * USB mass storage device is already configured). */ if (g_usbstrg.mshandle) { message(MAIN_NAME_STRING ": ERROR: Already connected\n"); return 1; } #endif #ifdef CONFIG_EXAMPLES_USBSTRG_DEBUGMM # ifdef CONFIG_CAN_PASS_STRUCTS g_usbstrg.mmstart = mallinfo(); g_usbstrg.mmprevious = g_usbstrg.mmstart; # else (void)mallinfo(&g_usbstrg.mmstart); memcpy(&g_usbstrg.mmprevious, &g_usbstrg.mmstart, sizeof(struct mallinfo)); # endif #endif /* Initialize USB trace output IDs */ usbtrace_enable(TRACE_BITSET); check_test_memory_usage("After usbtrace_enable()"); /* Register block drivers (architecture-specific) */ message(MAIN_NAME_STRING ": Creating block drivers\n"); ret = usbstrg_archinitialize(); if (ret < 0) { message(MAIN_NAME_STRING ": usbstrg_archinitialize failed: %d\n", -ret); return 2; } check_test_memory_usage("After usbstrg_archinitialize()"); /* Then exports the LUN(s) */ message(MAIN_NAME_STRING ": Configuring with NLUNS=%d\n", CONFIG_EXAMPLES_USBSTRG_NLUNS); ret = usbstrg_configure(CONFIG_EXAMPLES_USBSTRG_NLUNS, &handle); if (ret < 0) { message(MAIN_NAME_STRING ": usbstrg_configure failed: %d\n", -ret); usbstrg_uninitialize(handle); return 3; } message(MAIN_NAME_STRING ": handle=%p\n", handle); check_test_memory_usage("After usbstrg_configure()"); message(MAIN_NAME_STRING ": Bind LUN=0 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH1); ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH1, 0, 0, 0, false); if (ret < 0) { message(MAIN_NAME_STRING ": usbstrg_bindlun failed for LUN 1 using %s: %d\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH1, -ret); usbstrg_uninitialize(handle); return 4; } check_test_memory_usage("After usbstrg_bindlun()"); #if CONFIG_EXAMPLES_USBSTRG_NLUNS > 1 message(MAIN_NAME_STRING ": Bind LUN=1 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH2); ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH2, 1, 0, 0, false); if (ret < 0) { message(MAIN_NAME_STRING ": usbstrg_bindlun failed for LUN 2 using %s: %d\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH2, -ret); usbstrg_uninitialize(handle); return 5; } check_test_memory_usage("After usbstrg_bindlun() #2"); #if CONFIG_EXAMPLES_USBSTRG_NLUNS > 2 message(MAIN_NAME_STRING ": Bind LUN=2 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH3); ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH3, 2, 0, 0, false); if (ret < 0) { message(MAIN_NAME_STRING ": usbstrg_bindlun failed for LUN 3 using %s: %d\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH3, -ret); usbstrg_uninitialize(handle); return 6; } check_test_memory_usage("After usbstrg_bindlun() #3"); #endif #endif ret = usbstrg_exportluns(handle); if (ret < 0) { message(MAIN_NAME_STRING ": usbstrg_exportluns failed: %d\n", -ret); usbstrg_uninitialize(handle); return 7; } check_test_memory_usage("After usbstrg_exportluns()"); /* It this program was configued as an NSH command, then just exit now. * Also, if signals are not enabled (and, hence, sleep() is not supported. * then we have not real option but to exit now. */ #if !defined(CONFIG_EXAMPLES_USBSTRG_BUILTIN) && !defined(CONFIG_DISABLE_SIGNALS) /* Otherwise, this thread will hang around and monitor the USB storage activity */ for (;;) { msgflush(); sleep(5); # ifdef CONFIG_USBDEV_TRACE message("\nuser_start: USB TRACE DATA:\n"); ret = usbtrace_enumerate(usbstrg_enumerate, NULL); if (ret < 0) { message(MAIN_NAME_STRING ": usbtrace_enumerate failed: %d\n", -ret); usbstrg_uninitialize(handle); return 8; } check_test_memory_usage("After usbtrace_enumerate()"); # else message(MAIN_NAME_STRING ": Still alive\n"); # endif } #elif defined(CONFIG_EXAMPLES_USBSTRG_BUILTIN) /* Return the USB mass storage device handle so it can be used by the 'misconn' * command. */ message(MAIN_NAME_STRING ": Connected\n"); g_usbstrg.mshandle = handle; check_test_memory_usage("After MS connection"); #else /* defined(CONFIG_DISABLE_SIGNALS) */ /* Just exit */ message(MAIN_NAME_STRING ": Exiting\n"); /* Dump debug memory usage */ final_memory_usage("Final memory usage"); #endif return 0; }
int MAIN_NAME(int argc, char *argv[]) { struct adc_msg_s sample[CONFIG_EXAMPLES_ADC_GROUPSIZE]; size_t readsize; ssize_t nbytes; #if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES) long nloops; #endif int fd; int errval = 0; int ret; int i; /* If this example is configured as an NX add-on, then limit the number of * samples that we collect before returning. Otherwise, we never return */ #if defined(CONFIG_NSH_BUILTIN_APPS) nloops = 1; if (argc > 1) { nloops = strtol(argv[1], NULL, 10); } message(MAIN_STRING "nloops: %d\n", nloops); #elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES) message(MAIN_STRING "nloops: %d\n", CONFIG_EXAMPLES_ADC_NSAMPLES); #endif /* Initialization of the ADC hardware is performed by logic external to * this test. */ message(MAIN_STRING "Initializing external ADC device\n"); ret = adc_devinit(); if (ret != OK) { message(MAIN_STRING "adc_devinit failed: %d\n", ret); errval = 1; goto errout; } /* Open the ADC device for reading */ message(MAIN_STRING "Hardware initialized. Opening the ADC device\n"); fd = open(CONFIG_EXAMPLES_ADC_DEVPATH, O_RDONLY); if (fd < 0) { message(MAIN_STRING "open %s failed: %d\n", CONFIG_EXAMPLES_ADC_DEVPATH, errno); errval = 2; goto errout_with_dev; } /* Now loop the appropriate number of times, displaying the collected * ADC samples. */ #if defined(CONFIG_NSH_BUILTIN_APPS) for (; nloops > 0; nloops--) #elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES) for (nloops = 0; nloops < CONFIG_EXAMPLES_ADC_NSAMPLES; nloops++) #else for (;;) #endif { /* Flush any output before the loop entered or from the previous pass * through the loop. */ msgflush(); /* Read CONFIG_EXAMPLES_ADC_GROUPSIZE samples */ readsize = CONFIG_EXAMPLES_ADC_GROUPSIZE * sizeof(struct adc_msg_s); nbytes = read(fd, sample, readsize); /* Handle unexpected return values */ if (nbytes < 0) { errval = errno; if (errval != EINTR) { message(MAIN_STRING "read %s failed: %d\n", CONFIG_EXAMPLES_ADC_DEVPATH, errval); errval = 3; goto errout_with_dev; } message(MAIN_STRING "Interrupted read...\n"); } else if (nbytes == 0) { message(MAIN_STRING "No data read, Ignoring\n"); } /* Print the sample data on successful return */ else { int nsamples = nbytes / sizeof(struct adc_msg_s); if (nsamples * sizeof(struct adc_msg_s) != nbytes) { message(MAIN_STRING "read size=%d is not a multiple of sample size=%d, Ignoring\n", nbytes, sizeof(struct adc_msg_s)); } else { message("Sample:\n"); for (i = 0; i < nsamples ; i++) { message("%d: channel: %d value: %d\n", i, sample[i].am_channel, sample[i].am_data); } } } } errout_with_dev: close(fd); errout: message("Terminating!\n"); msgflush(); return errval; }
int thttp_main(int argc, char *argv[]) { struct in_addr addr; #ifdef CONFIG_EXAMPLE_THTTPD_NOMAC uint8_t mac[IFHWADDRLEN]; #endif char *thttpd_argv = "thttpd"; int ret; /* Configure SLIP */ #ifdef CONFIG_NET_SLIP ret = slip_initialize(SLIP_DEVNO, CONFIG_NET_SLIPTTY); if (ret < 0) { message("ERROR: SLIP initialization failed: %d\n", ret); exit(1); } #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLE_THTTPD_NOMAC message("Assigning MAC\n"); mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; uip_setmacaddr(NET_DEVNAME, mac); #endif /* Set up our host address */ message("Setup network addresses\n"); addr.s_addr = HTONL(CONFIG_THTTPD_IPADDR); uip_sethostaddr(NET_DEVNAME, &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLE_THTTPD_DRIPADDR); uip_setdraddr(NET_DEVNAME, &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLE_THTTPD_NETMASK); uip_setnetmask(NET_DEVNAME, &addr); /* Initialize the NXFLAT binary loader */ message("Initializing the NXFLAT binary loader\n"); ret = nxflat_initialize(); if (ret < 0) { message("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret); exit(2); } /* Create a ROM disk for the ROMFS filesystem */ message("Registering romdisk\n"); ret = romdisk_register(0, (uint8_t*)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); if (ret < 0) { message("ERROR: romdisk_register failed: %d\n", ret); nxflat_uninitialize(); exit(1); } /* Mount the file system */ message("Mounting ROMFS filesystem at target=%s with source=%s\n", MOUNTPT, ROMFSDEV); ret = mount(ROMFSDEV, MOUNTPT, "romfs", MS_RDONLY, NULL); if (ret < 0) { message("ERROR: mount(%s,%s,romfs) failed: %s\n", ROMFSDEV, MOUNTPT, errno); nxflat_uninitialize(); } /* Start THTTPD. At present, symbol table info is passed via global variables */ g_thttpdsymtab = exports; g_thttpdnsymbols = NEXPORTS; message("Starting THTTPD\n"); msgflush(); thttpd_main(1, &thttpd_argv); message("THTTPD terminated\n"); msgflush(); return 0; }
int tc_main(int argc, char *argv[]) { struct touch_sample_s sample; ssize_t nbytes; #if defined(CONFIG_EXAMPLES_TOUCHSCREEN_BUILTIN) || CONFIG_EXAMPLES_TOUCHSCREEN_NSAMPLES > 0 long nsamples; #endif int fd; int errval = 0; int ret; /* If this example is configured as an NX add-on, then limit the number of * samples that we collect before returning. Otherwise, we never return */ #if defined(CONFIG_EXAMPLES_TOUCHSCREEN_BUILTIN) nsamples = 1; if (argc > 1) { nsamples = strtol(argv[1], NULL, 10); } message("tc_main: nsamples: %d\n", nsamples); #elif CONFIG_EXAMPLES_TOUCHSCREEN_NSAMPLES > 0 message("tc_main: nsamples: %d\n", CONFIG_EXAMPLES_TOUCHSCREEN_NSAMPLES); #endif /* Initialization of the touchscreen hardware is performed by logic * external to this test. */ message("tc_main: Initializing external touchscreen device\n"); ret = arch_tcinitialize(CONFIG_EXAMPLES_TOUCHSCREEN_MINOR); if (ret != OK) { message("tc_main: arch_tcinitialize failed: %d\n", ret); errval = 1; goto errout; } /* Open the touchscreen device for reading */ message("tc_main: Opening %s\n", CONFIG_EXAMPLES_TOUCHSCREEN_DEVPATH); fd = open(CONFIG_EXAMPLES_TOUCHSCREEN_DEVPATH, O_RDONLY); if (fd < 0) { message("tc_main: open %s failed: %d\n", CONFIG_EXAMPLES_TOUCHSCREEN_DEVPATH, errno); errval = 2; goto errout_with_tc; } /* Now loop the appropriate number of times, displaying the collected * touchscreen samples. */ #if defined(CONFIG_EXAMPLES_TOUCHSCREEN_BUILTIN) for (; nsamples > 0; nsamples--) #elif CONFIG_EXAMPLES_TOUCHSCREEN_NSAMPLES > 0 for (nsamples = 0; nsamples < CONFIG_EXAMPLES_TOUCHSCREEN_NSAMPLES; nsamples++) #else for (;;) #endif { /* Flush any output before the loop entered or from the previous pass * through the loop. */ msgflush(); /* Read one sample */ ivdbg("Reading...\n"); nbytes = read(fd, &sample, sizeof(struct touch_sample_s)); ivdbg("Bytes read: %d\n", nbytes); /* Handle unexpected return values */ if (nbytes < 0) { errval = errno; if (errval != EINTR) { message("tc_main: read %s failed: %d\n", CONFIG_EXAMPLES_TOUCHSCREEN_DEVPATH, errval); errval = 3; goto errout_with_dev; } message("tc_main: Interrupted read...\n"); } else if (nbytes != sizeof(struct touch_sample_s)) { message("tc_main: Unexpected read size=%d, expected=%d, Ignoring\n", nbytes, sizeof(struct touch_sample_s)); } /* Print the sample data on successful return */ else { message("Sample :\n"); message(" npoints : %d\n", sample.npoints); message("Point 1 :\n"); message(" id : %d\n", sample.point[0].id); message(" flags : %02x\n", sample.point[0].flags); message(" x : %d\n", sample.point[0].x); message(" y : %d\n", sample.point[0].y); message(" h : %d\n", sample.point[0].h); message(" w : %d\n", sample.point[0].w); message(" pressure : %d\n", sample.point[0].pressure); } } errout_with_dev: close(fd); errout_with_tc: arch_tcuninitialize(); errout: message("Terminating!\n"); msgflush(); return errval; }
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; }
void *select_listener(pthread_addr_t pvarg) { fd_set rfds; struct timeval tv; char buffer[64]; ssize_t nbytes; bool timeout; bool ready; int fd; int ret; /* Open the FIFO for non-blocking read */ message("select_listener: Opening %s for non-blocking read\n", FIFO_PATH2); fd = open(FIFO_PATH2, O_RDONLY|O_NONBLOCK); if (fd < 0) { message("select_listener: ERROR Failed to open FIFO %s: %d\n", FIFO_PATH2, errno); (void)close(fd); return (void*)-1; } /* Loop forever */ for (;;) { message("select_listener: Calling select()\n"); FD_ZERO(&rfds); FD_SET(fd, &rfds); tv.tv_sec = SELECT_LISTENER_DELAY; tv.tv_usec = 0; timeout = false; ready = false; ret = select(fd+1, (FAR fd_set*)&rfds, (FAR fd_set*)NULL, (FAR fd_set*)NULL, &tv); message("\nselect_listener: select returned: %d\n", ret); if (ret < 0) { message("select_listener: ERROR select failed: %d\n", errno); } else if (ret == 0) { message("select_listener: Timeout\n"); timeout = true; } else { if (ret != 1) { message("select_listener: ERROR poll reported: %d\n", ret); } else { ready = true; } if (!FD_ISSET(fd, rfds)) { message("select_listener: ERROR fd=%d not in fd_set\n", fd); } } /* In any event, read until the pipe is empty */ do { nbytes = read(fd, buffer, 63); if (nbytes <= 0) { if (nbytes == 0 || errno == EAGAIN) { if (ready) { message("select_listener: ERROR no read data\n"); } } else if (errno != EINTR) { message("select_listener: read failed: %d\n", errno); } nbytes = 0; } else { if (timeout) { message("select_listener: ERROR? Poll timeout, but data read\n"); message(" (might just be a race condition)\n"); } buffer[nbytes] = '\0'; message("select_listener: Read '%s' (%d bytes)\n", buffer, nbytes); } timeout = false; ready = false; } while (nbytes > 0); /* Make sure that everything is displayed */ msgflush(); } /* Won't get here */ (void)close(fd); return NULL; }
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 nxcon_main(int argc, char **argv) { nxgl_mxpixel_t color; int fd; int ret; /* General Initialization *************************************************/ /* Reset all global data */ message("nxcon_main: Started\n"); memset(&g_nxcon_vars, 0, sizeof(struct nxcon_state_s)); /* Call all C++ static constructors */ #if defined(CONFIG_HAVE_CXX) && defined(CONFIG_HAVE_CXXINITIALIZE) up_cxxinitialize(); #endif /* NSH Initialization *****************************************************/ /* Initialize the NSH library */ message("nxcon_main: Initialize NSH\n"); nsh_initialize(); /* If the Telnet console is selected as a front-end, then start the * Telnet daemon. */ #ifdef CONFIG_NSH_TELNET ret = nsh_telnetstart(); if (ret < 0) { /* The daemon is NOT running. Report the the error then fail... * either with the serial console up or just exiting. */ fprintf(stderr, "ERROR: Failed to start TELNET daemon: %d\n", ret); } #endif /* NX Initialization ******************************************************/ /* Initialize NX */ message("nxcon_main: Initialize NX\n"); ret = nxcon_initialize(); message("nxcon_main: NX handle=%p\n", g_nxcon_vars.hnx); if (!g_nxcon_vars.hnx || ret < 0) { message("nxcon_main: Failed to get NX handle: %d\n", errno); goto errout; } /* Set the background to the configured background color */ message("nxcon_main: Set background color=%d\n", CONFIG_EXAMPLES_NXCON_BGCOLOR); color = CONFIG_EXAMPLES_NXCON_BGCOLOR; ret = nx_setbgcolor(g_nxcon_vars.hnx, &color); if (ret < 0) { message("nxcon_main: nx_setbgcolor failed: %d\n", errno); goto errout_with_nx; } /* Window Configuration ***************************************************/ /* Create a window */ message("nxcon_main: Create window\n"); g_nxcon_vars.hwnd = nxtk_openwindow(g_nxcon_vars.hnx, &g_nxconcb, NULL); if (!g_nxcon_vars.hwnd) { message("nxcon_main: nxtk_openwindow failed: %d\n", errno); goto errout_with_nx; } message("nxcon_main: hwnd=%p\n", g_nxcon_vars.hwnd); /* Wait until we have the screen resolution. We'll have this immediately * unless we are dealing with the NX server. */ while (!g_nxcon_vars.haveres) { (void)sem_wait(&g_nxcon_vars.eventsem); } message("nxcon_main: Screen resolution (%d,%d)\n", g_nxcon_vars.xres, g_nxcon_vars.yres); /* Determine the size and position of the window */ g_nxcon_vars.wndo.wsize.w = g_nxcon_vars.xres / 2 + g_nxcon_vars.xres / 4; g_nxcon_vars.wndo.wsize.h = g_nxcon_vars.yres / 2 + g_nxcon_vars.yres / 4; g_nxcon_vars.wpos.x = g_nxcon_vars.xres / 8; g_nxcon_vars.wpos.y = g_nxcon_vars.yres / 8; /* Set the window position */ message("nxcon_main: Set window position to (%d,%d)\n", g_nxcon_vars.wpos.x, g_nxcon_vars.wpos.y); ret = nxtk_setposition(g_nxcon_vars.hwnd, &g_nxcon_vars.wpos); if (ret < 0) { message("nxcon_main: nxtk_setposition failed: %d\n", errno); goto errout_with_hwnd; } /* Set the window size */ message("nxcon_main: Set window size to (%d,%d)\n", g_nxcon_vars.wndo.wsize.w, g_nxcon_vars.wndo.wsize.h); ret = nxtk_setsize(g_nxcon_vars.hwnd, &g_nxcon_vars.wndo.wsize); if (ret < 0) { message("nxcon_main: nxtk_setsize failed: %d\n", errno); goto errout_with_hwnd; } /* Open the toolbar */ message("nxcon_main: Add toolbar to window\n"); ret = nxtk_opentoolbar(g_nxcon_vars.hwnd, CONFIG_EXAMPLES_NXCON_TOOLBAR_HEIGHT, &g_nxtoolcb, NULL); if (ret < 0) { message("nxcon_main: nxtk_opentoolbar failed: %d\n", errno); goto errout_with_hwnd; } /* Sleep a little bit to allow the server to catch up */ sleep(2); /* NxConsole Configuration ************************************************/ /* Use the window to create an NX console */ g_nxcon_vars.wndo.wcolor[0] = CONFIG_EXAMPLES_NXCON_WCOLOR; g_nxcon_vars.wndo.fcolor[0] = CONFIG_EXAMPLES_NXCON_FONTCOLOR; g_nxcon_vars.wndo.fontid = CONFIG_EXAMPLES_NXCON_FONTID; g_nxcon_vars.hdrvr = nxtk_register(g_nxcon_vars.hwnd, &g_nxcon_vars.wndo, CONFIG_EXAMPLES_NXCON_MINOR); if (!g_nxcon_vars.hdrvr) { message("nxcon_main: nxtk_register failed: %d\n", errno); goto errout_with_hwnd; } /* Open the NxConsole driver */ fd = open(CONFIG_EXAMPLES_NXCON_DEVNAME, O_WRONLY); if (fd < 0) { message("nxcon_main: open %s read-only failed: %d\n", CONFIG_EXAMPLES_NXCON_DEVNAME, errno); goto errout_with_driver; } /* Start Console Task *****************************************************/ /* Now re-direct stdout and stderr so that they use the NX console driver. * Note that stdin is retained (file descriptor 0, probably the the serial console). */ message("nxcon_main: Starting the console task\n"); msgflush(); (void)fflush(stdout); (void)fflush(stderr); (void)fclose(stdout); (void)fclose(stderr); (void)dup2(fd, 1); (void)dup2(fd, 2); /* And we can close our original driver file descriptor */ close(fd); /* And start the console task. It will inherit stdin, stdout, and stderr * from this task. */ g_nxcon_vars.pid = TASK_CREATE("NxConsole", CONFIG_EXAMPLES_NXCONSOLE_PRIO, CONFIG_EXAMPLES_NXCONSOLE_STACKSIZE, nxcon_task, NULL); ASSERT(g_nxcon_vars.pid > 0); return EXIT_SUCCESS; /* Error Exits ************************************************************/ errout_with_driver: (void)nxcon_unregister(g_nxcon_vars.hdrvr); errout_with_hwnd: (void)nxtk_closewindow(g_nxcon_vars.hwnd); errout_with_nx: /* Disconnect from the server */ nx_disconnect(g_nxcon_vars.hnx); errout: return EXIT_FAILURE; }