rtos test
Dependencies: LCD_DISCO_F469NI BSP_DISCO_F469NI
Revision 5:3118045d33a2, committed 2022-01-31
- Comitter:
- misha83
- Date:
- Mon Jan 31 12:04:09 2022 +0400
- Parent:
- 4:22d1c1776155
- Commit message:
- tes
Changed in this revision
.hgignore | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Mon Jan 31 12:04:09 2022 +0400 @@ -0,0 +1,5 @@ +^.mbed$ +^BUILD$ +^mbed-os$ +^LCD_DISCO_F469NI$ +^BSP_DISCO_F469NI$
--- a/main.cpp Thu Jan 27 18:08:37 2022 +0400 +++ b/main.cpp Mon Jan 31 12:04:09 2022 +0400 @@ -3,15 +3,30 @@ #include "rtos.h" #include <string> #include "stdio.h" + + +#include <errno.h> +#include <functional> + +#include "BlockDevice.h" + + +#include "LittleFileSystem.h" + + LCD_DISCO_F469NI lcd; DigitalOut led1(LED1); DigitalOut led2(LED2); +DigitalOut led3(LED3); -Thread thread, led_thread, queue_thread; +Thread thread, led_thread, queue_thread, button_thread; +#define BUFFER_MAX_LEN 10 +#define FORCE_REFORMAT true + #define SAMPLE_FLAG1 (1UL << 0) #define SAMPLE_FLAG2 (1UL << 9) @@ -34,6 +49,8 @@ +BlockDevice *bd = BlockDevice::get_default_instance(); +LittleFileSystem fs("fs"); @@ -76,23 +93,250 @@ } +void erase() { + printf("Initializing the block device... "); + fflush(stdout); + int err = bd->init(); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err) { + error("error: %s (%d)\n", strerror(-err), err); + } + + printf("Erasing the block device... "); + fflush(stdout); + err = bd->erase(0, bd->size()); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err) { + error("error: %s (%d)\n", strerror(-err), err); + } + + printf("Deinitializing the block device... "); + fflush(stdout); + err = bd->deinit(); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err) { + error("error: %s (%d)\n", strerror(-err), err); + } +} + + +void mount(void) +{ + // Try to mount the filesystem + printf("Mounting the filesystem... "); + fflush(stdout); + int err = fs.mount(bd); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err || FORCE_REFORMAT) { + // Reformat if we can't mount the filesystem + printf("formatting... "); + fflush(stdout); + err = fs.reformat(bd); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err) { + error("error: %s (%d)\n", strerror(-err), err); + } + } + + // Open the numbers file + printf("Opening \"/fs/numbers.txt\"... "); + fflush(stdout); + FILE *f = fopen("/fs/numbers.txt", "r+"); + printf("%s\n", (!f ? "Fail :(" : "OK")); + if (!f) { + // Create the numbers file if it doesn't exist + printf("No file found, creating a new file... "); + fflush(stdout); + f = fopen("/fs/numbers.txt", "w+"); + printf("%s\n", (!f ? "Fail :(" : "OK")); + if (!f) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + + for (int i = 0; i < 10; i++) { + printf("\rWriting numbers (%d/%d)... ", i, 10); + fflush(stdout); + err = fprintf(f, " %d\n", i); + if (err < 0) { + printf("Fail :(\n"); + error("error: %s (%d)\n", strerror(errno), -errno); + } + } + printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); + + printf("Seeking file... "); + fflush(stdout); + err = fseek(f, 0, SEEK_SET); + printf("%s\n", (err < 0 ? "Fail :(" : "OK")); + if (err < 0) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + } + + // Go through and increment the numbers + for (int i = 0; i < 10; i++) { + printf("\rIncrementing numbers (%d/%d)... ", i, 10); + fflush(stdout); + + // Get current stream position + long pos = ftell(f); + + // Parse out the number and increment + char buf[BUFFER_MAX_LEN]; + if (!fgets(buf, BUFFER_MAX_LEN, f)) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + char *endptr; + int32_t number = strtol(buf, &endptr, 10); + if ( + (errno == ERANGE) || // The number is too small/large + (endptr == buf) || // No character was read + (*endptr && *endptr != '\n') // The whole input was not converted + ) { + continue; + } + number += 1; + + // Seek to beginning of number + fseek(f, pos, SEEK_SET); + + // Store number + fprintf(f, " %d\n", number); + + // Flush between write and read on same file + fflush(f); + } + printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10); + + // Close the file which also flushes any cached writes + printf("Closing \"/fs/numbers.txt\"... "); + fflush(stdout); + err = fclose(f); + printf("%s\n", (err < 0 ? "Fail :(" : "OK")); + if (err < 0) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + + // Display the root directory + printf("Opening the root directory... "); + fflush(stdout); + DIR *d = opendir("/fs/"); + printf("%s\n", (!d ? "Fail :(" : "OK")); + if (!d) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + + printf("root directory:\n"); + while (true) { + struct dirent *e = readdir(d); + if (!e) { + break; + } + + printf(" %s\n", e->d_name); + } + + printf("Closing the root directory... "); + fflush(stdout); + err = closedir(d); + printf("%s\n", (err < 0 ? "Fail :(" : "OK")); + if (err < 0) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + + // Display the numbers file + printf("Opening \"/fs/numbers.txt\"... "); + fflush(stdout); + f = fopen("/fs/numbers.txt", "r"); + printf("%s\n", (!f ? "Fail :(" : "OK")); + if (!f) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + + printf("numbers:\n"); + while (!feof(f)) { + int c = fgetc(f); + printf("%c", c); + } + + printf("\rClosing \"/fs/numbers.txt\"... "); + fflush(stdout); + err = fclose(f); + printf("%s\n", (err < 0 ? "Fail :(" : "OK")); + if (err < 0) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + + // Tidy up + printf("Unmounting... "); + fflush(stdout); + err = fs.unmount(); + printf("%s\n", (err < 0 ? "Fail :(" : "OK")); + if (err < 0) { + error("error: %s (%d)\n", strerror(-err), err); + } + + printf("Mbed OS filesystem example done!\n"); +} + + + + + + +DigitalIn mypin(BUTTON1); +void button_test(void) +{ +int stat=0, butinf; + +mypin.mode(PullNone); + + while(1) + { + + butinf = mypin.read(); + if(stat==0 && butinf==1){ + stat = 1; + led3=!led3; + printf("button presed \n\r"); + + //erase(); + mount(); + + + }else{ + if(butinf==0){stat=0;} + } + + ThisThread::sleep_for(10); + } +// mtx.lock(); +// led3=!led3; +// mtx.unlock(); +} + + + + int main() { bool st=false; led1 = 1; led2 = 1; - uint32_t flags_read = 0; + uint32_t flags_read = 0; - char buffer[50]; - int c = 0; + char buffer[50]; + int c = 0; - printf("start mbed TEST START\n\r"); - + printf("start mbed TEST START \n\r"); + + thread.start(test_flag); - led_thread.start(led_th); - queue_thread.start(q_thread); + led_thread.start(led_th); + queue_thread.start(q_thread); + button_thread.start(button_test);