|
| 1 | +/* |
| 2 | + Lee Leahy |
| 3 | + 12 June 2023 |
| 4 | +
|
| 5 | + Determine the performance of the PSRAM. |
| 6 | +
|
| 7 | + Based upon: https://thingpulse.com/esp32-how-to-use-psram/ |
| 8 | + and: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/external-ram.html |
| 9 | +*/ |
| 10 | + |
| 11 | +#include <Arduino.h> |
| 12 | + |
| 13 | +#define MAX_LOOP_COUNT (1 * 1000 * 1000) |
| 14 | +#define PSRAM_BYTES (2 * 1024 * 1024) |
| 15 | + |
| 16 | +volatile uint8_t * buffer; |
| 17 | +volatile uint8_t internalRam; |
| 18 | +int psramBytes; |
| 19 | +uint32_t psramPageSize; |
| 20 | + |
| 21 | +void setup() |
| 22 | +{ |
| 23 | + Serial.begin(115200); |
| 24 | + delay(1000); |
| 25 | + Serial.println(); |
| 26 | + Serial.println(); |
| 27 | + Serial.print("PSRAM Size: "); |
| 28 | + Serial.print(ESP.getPsramSize()); |
| 29 | + Serial.println(); |
| 30 | + Serial.println("PSRAM Access Times"); |
| 31 | +} |
| 32 | + |
| 33 | +void loop() |
| 34 | +{ |
| 35 | + int32_t index; |
| 36 | + uint8_t junk; |
| 37 | + uint32_t loopOverhead; |
| 38 | + uint32_t microsEnd; |
| 39 | + uint32_t microsStart; |
| 40 | + uint32_t offset; |
| 41 | + |
| 42 | + // Read data from the data cache |
| 43 | + buffer = (volatile uint8_t *)ps_malloc(PSRAM_BYTES); |
| 44 | + Serial.print("PSRAM buffer: "); |
| 45 | + Serial.printf("%p\r\n", buffer); |
| 46 | + Serial.flush(); |
| 47 | + |
| 48 | + // Compute the loop overhead |
| 49 | + microsStart = micros(); |
| 50 | + for (index = 0; index < MAX_LOOP_COUNT; index++) |
| 51 | + { |
| 52 | + } |
| 53 | + microsEnd = micros(); |
| 54 | + displayTime(microsStart, microsEnd, 0, "Loop overhead"); |
| 55 | + loopOverhead = microsEnd - microsStart; |
| 56 | + |
| 57 | + // Prime the data |
| 58 | + junk = internalRam; |
| 59 | + |
| 60 | + // Read data from the internal RAM |
| 61 | + microsStart = micros(); |
| 62 | + for (index = 0; index < MAX_LOOP_COUNT; index++) |
| 63 | + junk = internalRam; |
| 64 | + microsEnd = micros(); |
| 65 | + displayTime(microsStart, microsEnd, loopOverhead, "Internal RAM access"); |
| 66 | + |
| 67 | + if (buffer) |
| 68 | + { |
| 69 | + // Measure cached PSRAM access |
| 70 | + microsStart = micros(); |
| 71 | + for (index = 0; index < MAX_LOOP_COUNT; index++) |
| 72 | + junk = buffer[0]; |
| 73 | + microsEnd = micros(); |
| 74 | + displayTime(microsStart, microsEnd, loopOverhead, "Cached PSRAM access"); |
| 75 | + |
| 76 | + // Separate the two sets of measurements |
| 77 | + Serial.println(); |
| 78 | + |
| 79 | + // Compute the loop overhead |
| 80 | + microsStart = micros(); |
| 81 | + for (index = 0; index < MAX_LOOP_COUNT; index++) |
| 82 | + { |
| 83 | + junk = buffer[offset]; |
| 84 | + offset = (offset + psramPageSize) & (PSRAM_BYTES - 1); |
| 85 | + } |
| 86 | + microsEnd = micros(); |
| 87 | + displayTime(microsStart, microsEnd, 0, "Loop overhead"); |
| 88 | + loopOverhead = microsEnd - microsStart; |
| 89 | + |
| 90 | + // Measure sequential accesses to PSRAM |
| 91 | + for (psramPageSize = 1; psramPageSize <= 4096; psramPageSize <<= 1) |
| 92 | + { |
| 93 | + microsStart = micros(); |
| 94 | + for (index = 0; index < MAX_LOOP_COUNT; index++) |
| 95 | + { |
| 96 | + junk = buffer[offset]; |
| 97 | + offset = (offset + psramPageSize) & (PSRAM_BYTES - 1); |
| 98 | + } |
| 99 | + microsEnd = micros(); |
| 100 | + sprintf((char *)buffer, "Uncached PSRAM access, %4d byte page size", psramPageSize); |
| 101 | + displayTime(microsStart, microsEnd, loopOverhead, (const char *)buffer); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Done |
| 106 | + while (1); |
| 107 | +} |
| 108 | + |
| 109 | +void displayTime(uint32_t microsStart, uint32_t microsEnd, uint32_t loopOverhead, const char * string) |
| 110 | +{ |
| 111 | + uint32_t delta; |
| 112 | + float nanoSeconds; |
| 113 | + char text[128]; |
| 114 | + |
| 115 | + // Display the cache read time |
| 116 | + delta = microsEnd - microsStart - loopOverhead; |
| 117 | + nanoSeconds = (double)delta / 1000.; |
| 118 | + sprintf(text, "%s: %8d - %8d - %5d = %7d: %7.3f nSec", |
| 119 | + string, microsEnd, microsStart, loopOverhead, delta, nanoSeconds); |
| 120 | + Serial.println(text); |
| 121 | + Serial.flush(); |
| 122 | +} |
0 commit comments