我是 Arduino 新手,我的 Arduino Portenta H7 遇到问题。我编写了一个草图,以 25kHz 采样率对来自三个传感器的模拟信号进行采样,并使用外部 SDRAM 进行数据存储。该草图管理动态内存分配,将传感器数据存储在数组中,直到达到指定的数据点计数 (data_len),并检查分配错误,如果发生任何错误,则停止程序。
我的问题是在执行 free_memory 函数时出现的,程序崩溃了,红灯以 4 长 4 短的方式闪烁表示。
我尝试单独释放每个缓冲区,但问题仍然存在。这是我的代码:
#include "Arduino_PortentaBreakout.h"
#include "SDRAM.h"
REDIRECT_STDOUT_TO(Serial);
// Constants for ADC warm-up and sampling interval
const int ADC_WARMUP_READS = 1000; // Number of reads to stabilize ADC readings
const long interval = 40; // Sampling interval in microseconds (40 microseconds for 25kHz sampling rate)
// Reference voltage
const float VREF = 3.10; // Reference voltage in volts
// Definitions of analog pins used
const breakoutPin PHASE_1 = ANALOG_A0;
const breakoutPin PHASE_2 = ANALOG_A1;
const breakoutPin CURRENT = ANALOG_A3;
// Array containing the analog pins for easy access
const breakoutPin PIN_Array[] = { PHASE_1, PHASE_2, CURRENT };
const char* PIN_Names[] = { "PHASE_1", "PHASE_2", "CURRENT" };
// Number of data points to sample
const long data_len = 250000;
// Buffers to store sampled values
uint16_t* data_PHASE_1;
uint16_t* data_PHASE_2;
uint16_t* data_CURRENT;
// Function to fill data arrays
void fillArrays() {
// ADC warm-up to stabilize the sensor signal
for (int i = 0; i < ADC_WARMUP_READS; i++) {
Breakout.analogRead(PHASE_1);
Breakout.analogRead(PHASE_2);
Breakout.analogRead(CURRENT);
}
// Time preparation to measure buffer fill duration
unsigned long timeStart = micros();
long i = 0;
unsigned long previousMicros = 0;
while (i < data_len) {
unsigned long currentMicros = micros();
if (currentMicros - previousMicros >= interval) {
previousMicros = currentMicros;
data_PHASE_1[i] = Breakout.analogRead(PHASE_1);
data_PHASE_2[i] = Breakout.analogRead(PHASE_2);
data_CURRENT[i] = Breakout.analogRead(CURRENT);
i++;
}
}
unsigned long timeEnd = micros();
Serial.print("Total loops completed: ");
Serial.println(i);
}
void free_memory() {
SDRAM.free(data_PHASE_1); data_PHASE_1 = NULL;
SDRAM.free(data_PHASE_2); data_PHASE_2 = NULL;
SDRAM.free(data_CURRENT); data_CURRENT = NULL;
}
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for the serial connection to be established
analogReadResolution(16);
SDRAM.begin();
data_PHASE_1 = (uint16_t*)SDRAM.malloc(data_len * sizeof(uint16_t));
data_PHASE_2 = (uint16_t*)SDRAM.malloc(data_len * sizeof(uint16_t));
data_CURRENT = (uint16_t*)SDRAM.malloc(data_len * sizeof(uint16_t));
if (data_PHASE_1 == NULL || data_PHASE_2 == NULL || data_CURRENT == NULL) {
Serial.println("Error: Failed to allocate memory, halting execution.");
while (1); // Halt further execution
}
if (SDRAM.test()) {
Serial.println("SDRAM is completely functional");
}
}
void loop() {
fillArrays();
for (int i = 0; i < 3; ++i) {
delay(2000);
const char* PIN_NAME = PIN_Names[i];
uint16_t* data = (i == 0) ? data_PHASE_1 : (i == 1) ? data_PHASE_2 : data_CURRENT;
Serial.print("Transmitting data for ");
Serial.println(PIN_NAME);
for (long j = 0; j < data_len; j++) {
Serial.println(data[j]);
}
Serial.print("End of data transmission for ");
Serial.println(PIN_NAME);
}
free_memory();
Serial.println("Click on reset to restart.");
while (1); // Pause
}
我有这个确切的问题。我还没有调查得太彻底。我已经仔细检查了分配的地址与传递给 free 的地址相同,肯定发生了一些事情。