mangue baja 1st working logger on stm32

Dependencies:   LSM6DS3 DebouncedInterrupt

example.txt

Committer:
einsteingustavo
Date:
2022-09-21
Revision:
5:122b9fd3df1a
Parent:
3:15d63b793f7b

File content as of revision 5:122b9fd3df1a:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
#include <stdio.h>
#include <errno.h>

// Block devices
//#include "SPIFBlockDevice.h"
//#include "DataFlashBlockDevice.h"
#include "SDBlockDevice.h"
//#include "HeapBlockDevice.h"

// File systems

//#include "LittleFileSystem.h"
#include "FATFileSystem.h"

// Physical block device, can be any device that supports the BlockDevice API
SDBlockDevice   blockDevice(PB_15, PB_14, PB_13, PB_12);  // mosi, miso, sck, cs

// File system declaration
FATFileSystem   fileSystem("fs");

Serial pc(PA_2,PA_3);

// Entry point for the example
int main()
{
    pc.printf("--- Mbed OS filesystem example ---\n");

    // Try to mount the filesystem
    pc.printf("Mounting the filesystem... ");
    fflush(stdout);

    int err = fileSystem.mount(&blockDevice);
    pc.printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        // Reformat if we can't mount the filesystem
        // this should only happen on the first boot
        pc.printf("No filesystem found, formatting... ");
        fflush(stdout);
        err = fileSystem.reformat(&blockDevice);
        pc.printf("%s\n", (err ? "Fail :(" : "OK"));
        if (err) {
            error("error: %s (%d)\n", strerror(-err), err);
        }
    }

    // Open the numbers file
    pc.printf("Opening \"/fs/numbers.txt\"... ");
    fflush(stdout);

    FILE*   f = fopen("/fs/numbers.txt", "r+");
    pc.printf("%s\n", (!f ? "Fail :(" : "OK"));
    if (!f) {
        // Create the numbers file if it doesn't exist
        pc.printf("No file found, creating a new file... ");
        fflush(stdout);
        f = fopen("/fs/numbers.txt", "w+");
        pc.printf("%s\n", (!f ? "Fail :(" : "OK"));
        if (!f) {
            error("error: %s (%d)\n", strerror(errno), -errno);
        }

        for (int i = 0; i < 10; i++) {
            pc.printf("\rWriting numbers (%d/%d)... ", i, 10);
            fflush(stdout);
            err = fprintf(f, "    %d\n", i);
            if (err < 0) {
                pc.printf("Fail :(\n");
                error("error: %s (%d)\n", strerror(errno), -errno);
            }
        }

        pc.printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);

        pc.printf("Seeking file... ");
        fflush(stdout);
        err = fseek(f, 0, SEEK_SET);
        pc.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++) {
        pc.printf("\rIncrementing numbers (%d/%d)... ", i, 10);
        fflush(stdout);

        // Get current stream position
        long    pos = ftell(f);

        // Parse out the number and increment
        int32_t number;
        fscanf(f, "%ld", &number);
        number += 1;

        // Seek to beginning of number
        fseek(f, pos, SEEK_SET);

        // Store number
        fprintf(f, "    %ld\n", number);

        // Flush between write and read on same file
        fflush(f);
    }

    pc.printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);

    // Close the file which also flushes any cached writes
    pc.printf("Closing \"/fs/numbers.txt\"... ");
    fflush(stdout);
    err = fclose(f);
    pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    // Display the root directory
    pc.printf("Opening the root directory... ");
    fflush(stdout);

    DIR*    d = opendir("/fs/");
    pc.printf("%s\n", (!d ? "Fail :(" : "OK"));
    if (!d) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    pc.printf("root directory:\n");
    while (true) {
        struct dirent*  e = readdir(d);
        if (!e) {
            break;
        }

        pc.printf("    %s\n", e->d_name);
    }

    pc.printf("Closing the root directory... ");
    fflush(stdout);
    err = closedir(d);
    pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    // Display the numbers file
    pc.printf("Opening \"/fs/numbers.txt\"... ");
    fflush(stdout);
    f = fopen("/fs/numbers.txt", "r");
    pc.printf("%s\n", (!f ? "Fail :(" : "OK"));
    if (!f) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    pc.printf("numbers:\n");
    while (!feof(f)) {
        int c = fgetc(f);
        pc.printf("%c", c);
    }

    pc.printf("\rClosing \"/fs/numbers.txt\"... ");
    fflush(stdout);
    err = fclose(f);
    pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    // Tidy up
    pc.printf("Unmounting... ");
    fflush(stdout);
    err = fileSystem.unmount();
    pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
    
    pc.printf("Initializing the block device... ");
    fflush(stdout);

    err = blockDevice.init();
    pc.printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }

    pc.printf("Erasing the block device... ");
    fflush(stdout);
    err = blockDevice.erase(0, blockDevice.size());
    pc.printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }

    pc.printf("Deinitializing the block device... ");
    fflush(stdout);
    err = blockDevice.deinit();
    pc.printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
    
    pc.printf("\r\n");
    
    pc.printf("Mbed OS filesystem example done!\n");
}