what looks like Buffer corruption of large buffer passed to function - Teensy 4.1 Platform IO


Well-known member
Hi All,

I have a curious corruption of a failry large ram buffer defined in my project running on a Teensy 4.1 with numerous libraries, high speed ISR driven by ADC interrupts etc.

This one has got me stumped as it is reliably corrupting it seems after a particular index, but not all of the data from that point onwards.

Data is defined in an array as such;

DMAMEM volatile int16_t i2cAdcLogBuffer[LSADC_LOG_BUFFER_SIZE*17];

I have tried using volatile in the remote hope this may assist but to no avail.

Loop responsible for writing to the array:

               if ((millis() > pollingTimers.lsAdcPollingTimer)&&(i2cAdcBufferIndex < (long)(LSADC_LOG_BUFFER_SIZE*17))){
                    pollingTimers.lsAdcPollingTimer = millis() + loggerConfiguration.pollingPeriodLsAdc;
                    //loop through the 4 possible lsADC devices
                    i2cAdcLogBuffer[i2cAdcBufferIndex++] = (uint16_t)(millis() - loggerStatus.logStartTime)/10; // update time
                    for (j=0;j<2;j++){
                        if ((ads7828Devices[j].data.found==1)&&(ads7828Devices[j].data.use==1)&&(ads7828Devices[j].data.log==1)){
                          // take readings of i2c ADC devices installed
                            //i2cAdcLogBuffer[i2cAdcBufferIndex++] = ads7828Devices[j].interface.read(i);
                            i2cAdcLogBuffer[i2cAdcBufferIndex++] = i2cAdcsamples + i;


This array is then passed to functions that either print to the serial port or write to file (essentially the same).

I also have a local function that I used to debug this further and this uses the array directly, where the error does not occur.

So it looks like the problem is something to do with the pointer or how I am passing the address of the array to these other functions.

function header for file writing;

void writeLogi2cAdc(FsFile* file, loggerConfigType* configuration, experimentDetailsType* experimentDetails, peripheralStatusType* peripheralStatus, int16_t* dataArray, uint16_t samples, time_t t);

Since writing from this buffer using the buffer directly does not have an issue, am I seeing a problem with the default pointer not being suitable to address the large array properly? ( it 204000 bytes in size)

Should I be using something like a far or huge pointer? (is this still required?)
Another data point, I am passing a huge 16Mbyte array I have defined as;

EXTMEM volatile uint8_t bigbuffer[BIG_BUFFER_SIZE]; // 16Mbytes as single bytes

To a function defined as:

void megunoLinkChartHSLoggedData(Stream* port, loggerConfigType* configuration, experimentDetailsType* experimentDetails, volatile uint8_t* bigbuffer, long samples, time_t t, int adcResultSize);

And I'm not having issues with this ie no pointers going off into the weeds etc.

Therefore I'm slightly confused as to why I would have issues with the much smaller array (although still large) defined in DMAMEM.
Well damn, this turns out to be a simple (albeit very timewasting) typo.......

There is no factstical pointer issue, just a screwup with the index calculation...