While using printf statements to debug some ADC-related code, I encountered a "hang" condition in the serial output. I have distilled the code into the following test program. In my system, with the program's four analog inputs connected to +3.3 volts, this test program will print 8 diagnostic lines, then hang.
Changing almost anything in the code makes the problem seem to vanish -- for example, commenting out the floating point variable assignments immediately following the while() statement will let things execute properly. So it seems quite timing-specific.
Could there be some sort of issue between the Ticker interrupts and the serial interrupts...leading to a deadly embrace, perhaps?
Thanks for any light you can shed.
hb
/********************************************************
/ PC serial bug demo
/ Run this program with a PC connected via the USB cable.
/ I used XP Pro SP3, with Hyper Terminal v5.1
/ The default comm settings are used (9600-8-N-1)
/
/ Analog input pins15, 16, 19, and 20 are connected directly
/ to 3.3 volts for this test.
/
/ Output ceases after the "killer" printf has executed
/ eight times.
********************************************************/
// Included files
#include "mbed.h"
// mbed declarations
AnalogIn analog0(p20);
AnalogIn analog1(p15);
AnalogIn analog2(p16);
AnalogIn analog3(p19);
Ticker timer;
Serial pc(USBTX, USBRX); // tx, rx
// Constants and strings
#define VERSION 7
#define sampledelay_us 2000 // Interval between ADC samples
// Interrupt (timer) routine
void ADC_sample() {
float foo;
foo = analog0.read_u16();
}
// Main program
int main () {
float fvar1 = 0;
float fvar2 = 0;
float fvar3;
// Initialization
pc.printf("\r\n\nPC test, v. %d.\r\n", VERSION);
timer.attach_us(&ADC_sample, sampledelay_us);
// Signon
printf("\r\nPC serial lockup test program.\r\n");
// Test
while (1) {
fvar1 = analog1;
fvar2 = analog2;
fvar3 = analog3 *330;
// Killer printf statement follows...
printf("fvar1 = %7.6f, fvar2 = %7.6f. Temp (C) = %8.6f; (F) = %8.6f).\r\n", fvar1, fvar2, fvar3, 32 + 9*fvar3/5); // Hangs after 8 lines
wait(0.5);
}
}
While using printf statements to debug some ADC-related code, I encountered a "hang" condition in the serial output. I have distilled the code into the following test program. In my system, with the program's four analog inputs connected to +3.3 volts, this test program will print 8 diagnostic lines, then hang.
Changing almost anything in the code makes the problem seem to vanish -- for example, commenting out the floating point variable assignments immediately following the while() statement will let things execute properly. So it seems quite timing-specific.
Could there be some sort of issue between the Ticker interrupts and the serial interrupts...leading to a deadly embrace, perhaps?
Thanks for any light you can shed.
hb