QSPIFBlockDevice example

Committer:
Offir Kochalsky
Date:
Thu Nov 08 16:00:03 2018 +0200
Revision:
0:5608693b47aa
QSPIFBlockDevice example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Offir Kochalsky 0:5608693b47aa 1 /* mbed Microcontroller Library
Offir Kochalsky 0:5608693b47aa 2 * Copyright (c) 2006-2013 ARM Limited
Offir Kochalsky 0:5608693b47aa 3 *
Offir Kochalsky 0:5608693b47aa 4 * Licensed under the Apache License, Version 2.0 (the "License");
Offir Kochalsky 0:5608693b47aa 5 * you may not use this file except in compliance with the License.
Offir Kochalsky 0:5608693b47aa 6 * You may obtain a copy of the License at
Offir Kochalsky 0:5608693b47aa 7 *
Offir Kochalsky 0:5608693b47aa 8 * http://www.apache.org/licenses/LICENSE-2.0
Offir Kochalsky 0:5608693b47aa 9 *
Offir Kochalsky 0:5608693b47aa 10 * Unless required by applicable law or agreed to in writing, software
Offir Kochalsky 0:5608693b47aa 11 * distributed under the License is distributed on an "AS IS" BASIS,
Offir Kochalsky 0:5608693b47aa 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Offir Kochalsky 0:5608693b47aa 13 * See the License for the specific language governing permissions and
Offir Kochalsky 0:5608693b47aa 14 * limitations under the License.
Offir Kochalsky 0:5608693b47aa 15 */
Offir Kochalsky 0:5608693b47aa 16 #include "mbed.h"
Offir Kochalsky 0:5608693b47aa 17 #include <stdio.h>
Offir Kochalsky 0:5608693b47aa 18 #include <algorithm>
Offir Kochalsky 0:5608693b47aa 19 #include "QSPIFBlockDevice.h"
Offir Kochalsky 0:5608693b47aa 20
Offir Kochalsky 0:5608693b47aa 21
Offir Kochalsky 0:5608693b47aa 22 QSPIFBlockDevice bd(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3,
Offir Kochalsky 0:5608693b47aa 23 QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_0, MBED_CONF_QSPIF_QSPI_FREQ);
Offir Kochalsky 0:5608693b47aa 24
Offir Kochalsky 0:5608693b47aa 25 // Entry point for the example
Offir Kochalsky 0:5608693b47aa 26 int main() {
Offir Kochalsky 0:5608693b47aa 27 printf("--- Mbed OS QSPIF block device example ---\n");
Offir Kochalsky 0:5608693b47aa 28
Offir Kochalsky 0:5608693b47aa 29 // Initialize the block device
Offir Kochalsky 0:5608693b47aa 30 int err = bd.init();
Offir Kochalsky 0:5608693b47aa 31 printf("bd.init -> %d\n", err);
Offir Kochalsky 0:5608693b47aa 32
Offir Kochalsky 0:5608693b47aa 33 int erase_val = bd.get_erase_value();
Offir Kochalsky 0:5608693b47aa 34
Offir Kochalsky 0:5608693b47aa 35 // Get device geometry
Offir Kochalsky 0:5608693b47aa 36 bd_size_t read_size = bd.get_read_size();
Offir Kochalsky 0:5608693b47aa 37 bd_size_t program_size = bd.get_program_size();
Offir Kochalsky 0:5608693b47aa 38 bd_size_t erase_size = bd.get_erase_size();
Offir Kochalsky 0:5608693b47aa 39 bd_size_t size = bd.size();
Offir Kochalsky 0:5608693b47aa 40
Offir Kochalsky 0:5608693b47aa 41 printf("--- Block device geometry ---\n");
Offir Kochalsky 0:5608693b47aa 42 printf("read_size: %lld B\n", read_size);
Offir Kochalsky 0:5608693b47aa 43 printf("program_size: %lld B\n", program_size);
Offir Kochalsky 0:5608693b47aa 44 printf("erase_size: %lld B\n", erase_size);
Offir Kochalsky 0:5608693b47aa 45 printf("size: %lld B\n", size);
Offir Kochalsky 0:5608693b47aa 46 printf("---\n");
Offir Kochalsky 0:5608693b47aa 47
Offir Kochalsky 0:5608693b47aa 48 // Allocate a block with enough space for our data, aligned to the
Offir Kochalsky 0:5608693b47aa 49 // nearest program_size. This is the minimum size necessary to write
Offir Kochalsky 0:5608693b47aa 50 // data to a block.
Offir Kochalsky 0:5608693b47aa 51 size_t buffer_size = sizeof("Hello Storage!") + program_size-1;
Offir Kochalsky 0:5608693b47aa 52 buffer_size = buffer_size - (buffer_size % program_size);
Offir Kochalsky 0:5608693b47aa 53 char *buffer = new char[buffer_size];
Offir Kochalsky 0:5608693b47aa 54
Offir Kochalsky 0:5608693b47aa 55 // Read what is currently stored on the block device. We haven't written
Offir Kochalsky 0:5608693b47aa 56 // yet so this may be garbage
Offir Kochalsky 0:5608693b47aa 57 printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
Offir Kochalsky 0:5608693b47aa 58 err = bd.read(buffer, 0, buffer_size);
Offir Kochalsky 0:5608693b47aa 59 printf("bd.read -> %d\n", err);
Offir Kochalsky 0:5608693b47aa 60
Offir Kochalsky 0:5608693b47aa 61 printf("--- Stored data ---\n");
Offir Kochalsky 0:5608693b47aa 62 for (size_t i = 0; i < buffer_size; i += 16) {
Offir Kochalsky 0:5608693b47aa 63 for (size_t j = 0; j < 16; j++) {
Offir Kochalsky 0:5608693b47aa 64 if (i+j < buffer_size) {
Offir Kochalsky 0:5608693b47aa 65 printf("%02x ", buffer[i+j]);
Offir Kochalsky 0:5608693b47aa 66 } else {
Offir Kochalsky 0:5608693b47aa 67 printf(" ");
Offir Kochalsky 0:5608693b47aa 68 }
Offir Kochalsky 0:5608693b47aa 69 }
Offir Kochalsky 0:5608693b47aa 70
Offir Kochalsky 0:5608693b47aa 71 printf(" %.*s\n", buffer_size - i, &buffer[i]);
Offir Kochalsky 0:5608693b47aa 72 }
Offir Kochalsky 0:5608693b47aa 73 printf("---\n");
Offir Kochalsky 0:5608693b47aa 74
Offir Kochalsky 0:5608693b47aa 75 // Update buffer with our string we want to store
Offir Kochalsky 0:5608693b47aa 76 strncpy(buffer, "Hello Storage!", buffer_size);
Offir Kochalsky 0:5608693b47aa 77
Offir Kochalsky 0:5608693b47aa 78 // Write data to first block, write occurs in two parts,
Offir Kochalsky 0:5608693b47aa 79 // an erase followed by a program
Offir Kochalsky 0:5608693b47aa 80 printf("bd.erase(%d, %lld)\n", 0, erase_size);
Offir Kochalsky 0:5608693b47aa 81 err = bd.erase(0, erase_size);
Offir Kochalsky 0:5608693b47aa 82 printf("bd.erase -> %d\n", err);
Offir Kochalsky 0:5608693b47aa 83
Offir Kochalsky 0:5608693b47aa 84 printf("bd.program(%p, %d, %d)\n", buffer, 0, buffer_size);
Offir Kochalsky 0:5608693b47aa 85 err = bd.program(buffer, 0, buffer_size);
Offir Kochalsky 0:5608693b47aa 86 printf("bd.program -> %d\n", err);
Offir Kochalsky 0:5608693b47aa 87
Offir Kochalsky 0:5608693b47aa 88 // Clobber the buffer so we don't get old data
Offir Kochalsky 0:5608693b47aa 89 memset(buffer, 0xcc, buffer_size);
Offir Kochalsky 0:5608693b47aa 90
Offir Kochalsky 0:5608693b47aa 91 // Read the data from the first block, note that the program_size must be
Offir Kochalsky 0:5608693b47aa 92 // a multiple of the read_size, so we don't have to check for alignment
Offir Kochalsky 0:5608693b47aa 93 printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size);
Offir Kochalsky 0:5608693b47aa 94 err = bd.read(buffer, 0, buffer_size);
Offir Kochalsky 0:5608693b47aa 95 printf("bd.read -> %d\n", err);
Offir Kochalsky 0:5608693b47aa 96
Offir Kochalsky 0:5608693b47aa 97 printf("--- Stored data ---\n");
Offir Kochalsky 0:5608693b47aa 98 for (size_t i = 0; i < buffer_size; i += 16) {
Offir Kochalsky 0:5608693b47aa 99 for (size_t j = 0; j < 16; j++) {
Offir Kochalsky 0:5608693b47aa 100 if (i+j < buffer_size) {
Offir Kochalsky 0:5608693b47aa 101 printf("%02x ", buffer[i+j]);
Offir Kochalsky 0:5608693b47aa 102 } else {
Offir Kochalsky 0:5608693b47aa 103 printf(" ");
Offir Kochalsky 0:5608693b47aa 104 }
Offir Kochalsky 0:5608693b47aa 105 }
Offir Kochalsky 0:5608693b47aa 106
Offir Kochalsky 0:5608693b47aa 107 printf(" %.*s\n", buffer_size - i, &buffer[i]);
Offir Kochalsky 0:5608693b47aa 108 }
Offir Kochalsky 0:5608693b47aa 109 printf("---\n");
Offir Kochalsky 0:5608693b47aa 110
Offir Kochalsky 0:5608693b47aa 111 // Deinitialize the block device
Offir Kochalsky 0:5608693b47aa 112 printf("bd.deinit()\n");
Offir Kochalsky 0:5608693b47aa 113 err = bd.deinit();
Offir Kochalsky 0:5608693b47aa 114 printf("bd.deinit -> %d\n", err);
Offir Kochalsky 0:5608693b47aa 115
Offir Kochalsky 0:5608693b47aa 116 printf("--- done! ---\n");
Offir Kochalsky 0:5608693b47aa 117 }
Offir Kochalsky 0:5608693b47aa 118