mbed_example / QSPIFBlockDevice_example
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include <stdio.h>
00018 #include <algorithm>
00019 #include "QSPIFBlockDevice.h"
00020 
00021 
00022 QSPIFBlockDevice bd(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3,
00023                             QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_0, MBED_CONF_QSPIF_QSPI_FREQ);
00024 
00025 // Entry point for the example
00026 int main() {
00027     printf("--- Mbed OS QSPIF block device example ---\n");
00028 
00029     // Initialize the block device
00030     int err = bd.init();
00031     printf("bd.init -> %d\n", err);
00032     
00033     int erase_val = bd.get_erase_value();
00034 
00035     // Get device geometry
00036     bd_size_t read_size    = bd.get_read_size();
00037     bd_size_t program_size = bd.get_program_size();
00038     bd_size_t erase_size   = bd.get_erase_size();
00039     bd_size_t size         = bd.size();
00040 
00041     printf("--- Block device geometry ---\n");
00042     printf("read_size:    %lld B\n", read_size);
00043     printf("program_size: %lld B\n", program_size);
00044     printf("erase_size:   %lld B\n", erase_size);
00045     printf("size:         %lld B\n", size);
00046     printf("---\n");
00047 
00048     // Allocate a block with enough space for our data, aligned to the
00049     // nearest program_size. This is the minimum size necessary to write
00050     // data to a block.
00051     size_t buffer_size = sizeof("Hello Storage!") + program_size-1;
00052     buffer_size = buffer_size - (buffer_size % program_size);
00053     char *buffer = new char[buffer_size];
00054 
00055     // Read what is currently stored on the block device. We haven't written
00056     // yet so this may be garbage
00057     printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
00058     err = bd.read(buffer, 0, buffer_size);
00059     printf("bd.read -> %d\n", err);
00060 
00061     printf("--- Stored data ---\n");
00062     for (size_t i = 0; i < buffer_size; i += 16) {
00063         for (size_t j = 0; j < 16; j++) {
00064             if (i+j < buffer_size) {
00065                 printf("%02x ", buffer[i+j]);
00066             } else {
00067                 printf("   ");
00068             }
00069         }
00070 
00071         printf(" %.*s\n", buffer_size - i, &buffer[i]);
00072     }
00073     printf("---\n");
00074 
00075     // Update buffer with our string we want to store
00076     strncpy(buffer, "Hello Storage!", buffer_size);
00077 
00078     // Write data to first block, write occurs in two parts,
00079     // an erase followed by a program
00080     printf("bd.erase(%d, %lld)\n", 0, erase_size);
00081     err = bd.erase(0, erase_size);
00082     printf("bd.erase -> %d\n", err);
00083 
00084     printf("bd.program(%p, %d, %d)\n", buffer, 0, buffer_size);
00085     err = bd.program(buffer, 0, buffer_size);
00086     printf("bd.program -> %d\n", err);
00087 
00088     // Clobber the buffer so we don't get old data
00089     memset(buffer, 0xcc, buffer_size);
00090 
00091     // Read the data from the first block, note that the program_size must be
00092     // a multiple of the read_size, so we don't have to check for alignment
00093     printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
00094     err = bd.read(buffer, 0, buffer_size);
00095     printf("bd.read -> %d\n", err);
00096 
00097     printf("--- Stored data ---\n");
00098     for (size_t i = 0; i < buffer_size; i += 16) {
00099         for (size_t j = 0; j < 16; j++) {
00100             if (i+j < buffer_size) {
00101                 printf("%02x ", buffer[i+j]);
00102             } else {
00103                 printf("   ");
00104             }
00105         }
00106 
00107         printf(" %.*s\n", buffer_size - i, &buffer[i]);
00108     }
00109     printf("---\n");
00110 
00111     // Deinitialize the block device
00112     printf("bd.deinit()\n");
00113     err = bd.deinit();
00114     printf("bd.deinit -> %d\n", err);
00115 
00116     printf("--- done! ---\n");
00117 }
00118