|
| 1 | +/** |
| 2 | +
|
| 3 | + */ |
| 4 | + |
| 5 | +// include the sparkfun_pico library - copy this into this folder to run |
| 6 | +#include "pico/stdlib.h" |
| 7 | +#include "sparkfun_pico/sfe_pico_boards.h" |
| 8 | +#include "sparkfun_pico/sfe_psram.h" |
| 9 | + |
| 10 | +#include <math.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +// Defines for the test. Note the PSRAM location is from the datasheet |
| 16 | +#define PSRAM_LOCATION _u(0x11000000) |
| 17 | +#define DATA_ELEMENTS 4096 |
| 18 | +#define DATA_BLOCK_SIZE (DATA_ELEMENTS * sizeof(int32_t)) |
| 19 | + |
| 20 | +static void erase_data_block(int32_t *data_buffer) |
| 21 | +{ |
| 22 | + for (size_t i = 0; i < DATA_ELEMENTS; i++) |
| 23 | + data_buffer[i] = 0xFFFFFFFF; |
| 24 | +} |
| 25 | +static void write_data_block(int32_t *source_data, int32_t *data_buffer, int32_t offset) |
| 26 | +{ |
| 27 | + for (size_t i = 0; i < DATA_ELEMENTS; i++) |
| 28 | + data_buffer[i] = source_data[i] + offset; |
| 29 | +} |
| 30 | + |
| 31 | +static bool check_data_block(int32_t *source_data, int32_t *data_buffer, int32_t offset) |
| 32 | +{ |
| 33 | + for (size_t i = 0; i < DATA_ELEMENTS; i++) |
| 34 | + { |
| 35 | + if (source_data[i] + offset != data_buffer[i]) |
| 36 | + { |
| 37 | + printf("ERROR : [%d] != [%d]\n", source_data[i] + offset, data_buffer[i]); |
| 38 | + return false; |
| 39 | + } |
| 40 | + } |
| 41 | + return true; |
| 42 | +} |
| 43 | +int main() |
| 44 | +{ |
| 45 | + stdio_init_all(); |
| 46 | + |
| 47 | + // wait a little bit - for startup |
| 48 | + sleep_ms(2000); |
| 49 | + printf("\n-----------------------------------------------------------\n"); |
| 50 | + printf("SparkFun - Basic PSRAM Test - starting\n"); |
| 51 | + sleep_ms(2000); |
| 52 | + |
| 53 | + size_t psram_size = sfe_setup_psram(SFE_RP2350_XIP_CSI_PIN); |
| 54 | + |
| 55 | + printf("PSRAM setup complete. PSRAM size 0x%lX\n", psram_size); |
| 56 | + |
| 57 | + if (psram_size == 0) |
| 58 | + { |
| 59 | + printf("PSRAM not detected - done\n"); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + printf("Generating Random Data\n"); |
| 64 | + // cook up some random data |
| 65 | + int32_t *random_data = (int32_t *)malloc(DATA_BLOCK_SIZE); |
| 66 | + if (!random_data) |
| 67 | + { |
| 68 | + printf("Random data allocation failed\n"); |
| 69 | + return 1; |
| 70 | + } |
| 71 | + for (int i = 0; i < DATA_ELEMENTS; i++) |
| 72 | + random_data[i] = rand(); |
| 73 | + |
| 74 | + // How many data blocks to write - use all of PSRAM |
| 75 | + size_t nDataBlocks = psram_size / DATA_BLOCK_SIZE; |
| 76 | + printf("PSRAM data block testing - n data blocks: %d\n", nDataBlocks); |
| 77 | + |
| 78 | + // Write data blocks to PSRAM - then read them back and check |
| 79 | + for (int i = 0; i < nDataBlocks; i++) |
| 80 | + { |
| 81 | + int32_t *data_buffer = (int32_t *)(PSRAM_LOCATION + i * DATA_BLOCK_SIZE); |
| 82 | + erase_data_block(data_buffer); |
| 83 | + write_data_block(random_data, data_buffer, i * 0x2); |
| 84 | + } |
| 85 | + printf("Data blocks written\n"); |
| 86 | + int nPassed = 0; |
| 87 | + |
| 88 | + for (size_t i = 0; i < nDataBlocks; i++) |
| 89 | + { |
| 90 | + if (i % 10 == 0) |
| 91 | + { |
| 92 | + printf("."); |
| 93 | + stdio_flush(); |
| 94 | + } |
| 95 | + int32_t *data_buffer = (int32_t *)(PSRAM_LOCATION + i * DATA_BLOCK_SIZE); |
| 96 | + if (!check_data_block(random_data, data_buffer, i * 0x2)) |
| 97 | + printf("Data block %d failed\n", (int)i); |
| 98 | + else |
| 99 | + nPassed++; |
| 100 | + } |
| 101 | + |
| 102 | + free(random_data); |
| 103 | + |
| 104 | + printf("\n\nTest Run: %d, Passed: %d, Failed: %d\n", nDataBlocks, nPassed, nDataBlocks - nPassed); |
| 105 | + |
| 106 | + printf("DONE\n"); |
| 107 | + printf("-----------------------------------------------------------\n"); |
| 108 | + while (1) |
| 109 | + { |
| 110 | + sleep_ms(1000); |
| 111 | + } |
| 112 | +} |
0 commit comments