|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2020 Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#if !DEVICE_QSPI |
| 7 | +#error [NOT_SUPPORTED] QSPI not supported for this target |
| 8 | +#endif |
| 9 | + |
| 10 | +#include "mbed.h" |
| 11 | +#include <stdio.h> |
| 12 | +#include <algorithm> |
| 13 | +#include "QSPIFBlockDevice.h" |
| 14 | + |
| 15 | +QSPIFBlockDevice bd(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3, |
| 16 | + QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_0, MBED_CONF_QSPIF_QSPI_FREQ); |
| 17 | + |
| 18 | +// Entry point for the example |
| 19 | +int main() |
| 20 | +{ |
| 21 | + printf("--- Mbed OS QSPIF block device example ---\n"); |
| 22 | + |
| 23 | + // Initialize the block device |
| 24 | + int err = bd.init(); |
| 25 | + printf("bd.init -> %d\n", err); |
| 26 | + |
| 27 | + int erase_val = bd.get_erase_value(); |
| 28 | + |
| 29 | + // Get device geometry |
| 30 | + bd_size_t read_size = bd.get_read_size(); |
| 31 | + bd_size_t program_size = bd.get_program_size(); |
| 32 | + bd_size_t erase_size = bd.get_erase_size(); |
| 33 | + bd_size_t size = bd.size(); |
| 34 | + |
| 35 | + printf("--- Block device geometry ---\n"); |
| 36 | + printf("read_size: %lld B\n", read_size); |
| 37 | + printf("program_size: %lld B\n", program_size); |
| 38 | + printf("erase_size: %lld B\n", erase_size); |
| 39 | + printf("size: %lld B\n", size); |
| 40 | + printf("---\n"); |
| 41 | + |
| 42 | + // Allocate a block with enough space for our data, aligned to the |
| 43 | + // nearest program_size. This is the minimum size necessary to write |
| 44 | + // data to a block. |
| 45 | + size_t buffer_size = sizeof("Hello Storage!") + program_size - 1; |
| 46 | + buffer_size = buffer_size - (buffer_size % program_size); |
| 47 | + char *buffer = new char[buffer_size]; |
| 48 | + |
| 49 | + // Read what is currently stored on the block device. We haven't written |
| 50 | + // yet so this may be garbage |
| 51 | + printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size); |
| 52 | + err = bd.read(buffer, 0, buffer_size); |
| 53 | + printf("bd.read -> %d\n", err); |
| 54 | + |
| 55 | + printf("--- Stored data ---\n"); |
| 56 | + for (size_t i = 0; i < buffer_size; i += 16) { |
| 57 | + for (size_t j = 0; j < 16; j++) { |
| 58 | + if (i + j < buffer_size) { |
| 59 | + printf("%02x ", buffer[i + j]); |
| 60 | + } else { |
| 61 | + printf(" "); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + printf(" %.*s\n", buffer_size - i, &buffer[i]); |
| 66 | + } |
| 67 | + printf("---\n"); |
| 68 | + |
| 69 | + // Update buffer with our string we want to store |
| 70 | + strncpy(buffer, "Hello Storage!", buffer_size); |
| 71 | + |
| 72 | + // Write data to first block, write occurs in two parts, |
| 73 | + // an erase followed by a program |
| 74 | + printf("bd.erase(%d, %lld)\n", 0, erase_size); |
| 75 | + err = bd.erase(0, erase_size); |
| 76 | + printf("bd.erase -> %d\n", err); |
| 77 | + |
| 78 | + printf("bd.program(%p, %d, %d)\n", buffer, 0, buffer_size); |
| 79 | + err = bd.program(buffer, 0, buffer_size); |
| 80 | + printf("bd.program -> %d\n", err); |
| 81 | + |
| 82 | + // Clobber the buffer so we don't get old data |
| 83 | + memset(buffer, 0xcc, buffer_size); |
| 84 | + |
| 85 | + // Read the data from the first block, note that the program_size must be |
| 86 | + // a multiple of the read_size, so we don't have to check for alignment |
| 87 | + printf("bd.read(%p, %d, %d)\n", buffer, 0, buffer_size); |
| 88 | + err = bd.read(buffer, 0, buffer_size); |
| 89 | + printf("bd.read -> %d\n", err); |
| 90 | + |
| 91 | + printf("--- Stored data ---\n"); |
| 92 | + for (size_t i = 0; i < buffer_size; i += 16) { |
| 93 | + for (size_t j = 0; j < 16; j++) { |
| 94 | + if (i + j < buffer_size) { |
| 95 | + printf("%02x ", buffer[i + j]); |
| 96 | + } else { |
| 97 | + printf(" "); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + printf(" %.*s\n", buffer_size - i, &buffer[i]); |
| 102 | + } |
| 103 | + printf("---\n"); |
| 104 | + |
| 105 | + // Deinitialize the block device |
| 106 | + printf("bd.deinit()\n"); |
| 107 | + err = bd.deinit(); |
| 108 | + printf("bd.deinit -> %d\n", err); |
| 109 | + |
| 110 | + printf("--- done! ---\n"); |
| 111 | +} |
0 commit comments