|
| 1 | +/* |
| 2 | + Portenta C33 - Test QSPI Flash |
| 3 | +
|
| 4 | + The sketch shows how to mount the QSPI flash and tests read write and |
| 5 | + erase operations. |
| 6 | +
|
| 7 | + This example code is in the public domain. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include <Arduino.h> |
| 11 | +#include <BlockDevice.h> |
| 12 | +#include <QSPIFlashBlockDevice.h> |
| 13 | + |
| 14 | +BlockDevice* root = BlockDevice::get_default_instance(); |
| 15 | + |
| 16 | +#define QSPIF_SIZE (16 * 1024 * 1024) |
| 17 | +#define WRITE_SIZE ( 8 * 1024) |
| 18 | + |
| 19 | +byte w_buffer[WRITE_SIZE]; |
| 20 | +byte r_buffer[WRITE_SIZE]; |
| 21 | + |
| 22 | +void setup() { |
| 23 | + Serial.begin(9600); |
| 24 | + while(!Serial) { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + Serial.println("QSPIFlash Test"); |
| 29 | + |
| 30 | + int ret = root->init(); |
| 31 | + if(ret) { |
| 32 | + Serial.println("Error opening QSPIFlash device"); |
| 33 | + while(1){} |
| 34 | + } |
| 35 | + |
| 36 | + Serial.println("QSPIFlash init done"); |
| 37 | + |
| 38 | + randomSeed(analogRead(A0)); |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + |
| 44 | + // Select a random pattern to write flash |
| 45 | + byte pattern = random(255); |
| 46 | + |
| 47 | + // Select a random size for the test buffer |
| 48 | + int repeat = random(1, WRITE_SIZE); |
| 49 | + |
| 50 | + // Select a random start offset |
| 51 | + int offset = random(0, QSPIF_SIZE - WRITE_SIZE); |
| 52 | + |
| 53 | + // Start Test |
| 54 | + erase_write_read_compare(pattern, offset, repeat); |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +void erase_write_read_compare(byte pattern, int offset, int repeat) { |
| 59 | + Serial.print("Using pattern "); |
| 60 | + Serial.print(pattern, HEX); |
| 61 | + Serial.print(" "); |
| 62 | + Serial.print(repeat); |
| 63 | + Serial.print(" times starting from "); |
| 64 | + Serial.println(offset, HEX); |
| 65 | + |
| 66 | + memset(&w_buffer[0], pattern, repeat); |
| 67 | + |
| 68 | + // Erase flash used for testing |
| 69 | + const uint32_t start_block = (offset / QSPI_ERASE_BLOCK_SIZE); |
| 70 | + const uint32_t end_block = ((offset + repeat) / QSPI_ERASE_BLOCK_SIZE) + 1; |
| 71 | + const uint32_t erase_size = (end_block - start_block) * QSPI_ERASE_BLOCK_SIZE; |
| 72 | + const uint32_t erase_addr = (start_block) * QSPI_ERASE_BLOCK_SIZE; |
| 73 | + root->erase(erase_addr, erase_size); |
| 74 | + |
| 75 | + // Write pattern |
| 76 | + root->program(&w_buffer[0], offset, repeat); |
| 77 | + |
| 78 | + // Readback |
| 79 | + root->read(&r_buffer[0], offset, repeat); |
| 80 | + |
| 81 | + // Compare |
| 82 | + if(memcmp(&w_buffer[0], &r_buffer[0], repeat)) { |
| 83 | + Serial.println("Error comparing buffers, dumping content..."); |
| 84 | + Serial.println("Write buffer:"); |
| 85 | + dump_buffer(&w_buffer[0], repeat); |
| 86 | + Serial.println("Read buffer:"); |
| 87 | + dump_buffer(&r_buffer[0], repeat); |
| 88 | + while(1){} |
| 89 | + } |
| 90 | + delay(5); |
| 91 | +} |
| 92 | + |
| 93 | +void dump_buffer(uint8_t *b, uint32_t len) { |
| 94 | + if (b != nullptr) { |
| 95 | + Serial.println(""); |
| 96 | + for(int i = 0; i < len; i++) { |
| 97 | + if(i != 0 && i % 32 == 0) { |
| 98 | + if(i != 0) |
| 99 | + Serial.println(); |
| 100 | + } |
| 101 | + Serial.print(*(b + i) >> 4, HEX); |
| 102 | + Serial.print(*(b + i) & 0x0F,HEX); |
| 103 | + } |
| 104 | + Serial.println(); |
| 105 | + Serial.println(""); |
| 106 | + } |
| 107 | +} |
0 commit comments