I'm seeing that, depending on the position of UART0 initialization, delayMicroseconds() either has an effect or it doesn't.
Some system details:
Code snippet 1:
Code snippet 2, takes approximately 150us longer:
I suspect that because I'm doing this inside an interrupt, things are affected in a way I don't understand. My next step is probably to write my own baud-setting routine, but I was hoping to use the convenience of uart_.begin().
I'm sure delayMicroseconds() works fine, however, the following two facts lead me to believe something is up around this function, maybe due to the compiler or something?
Some system details:
- The following code is running from within a IRQ_UART0_STATUS interrupt function.
- Signals and timings are being measured with a Saleae logic analyzer.
- Code snippet 2 takes 150us longer to react than code snippet 1. The only difference is the location of the uart_.begin() call.
- The test timing code around delayMicroseconds(150) prints an expected 150 or 151.
- Removing delayMicroseconds(150) from code snippet 2 causes it to execute in the exact same time, meaning that the presence or not of delayMicroseconds(150) has no effect.
Code snippet 1:
Code:
uint32_t eopTime = micros();
uart_.end();
uint32_t delay = 176;
uint32_t dt = micros() - eopTime;
uint32_t t = micros(); // Testing code
//if (dt < delay) {
// delay -= dt;
delayMicroseconds(150); // Changed to 150 instead of 'delay', for illustration
//}
Serial.println(micros() - t); // Testing code, always prints 150 or 151
uart_.begin(baud1, format1);
constexpr uint8_t zero[1] = {0};
txFunc_(zero, 1); // This writes directly to UART0 using the registers
Code snippet 2, takes approximately 150us longer:
Code:
uint32_t eopTime = micros();
uart_.end();
uart_.begin(baud1, format1); // This line has moved
uint32_t delay = 176;
uint32_t dt = micros() - eopTime;
uint32_t t = micros(); // Testing code
//if (dt < delay) {
// delay -= dt;
delayMicroseconds(150); // Changed to 150 instead of 'delay' for illustration
//}
Serial.println(micros() - t); // Testing code, always prints 150 or 151
constexpr uint8_t zero[1] = {0};
txFunc_(zero, 1); // This writes directly to UART0 using the registers
I suspect that because I'm doing this inside an interrupt, things are affected in a way I don't understand. My next step is probably to write my own baud-setting routine, but I was hoping to use the convenience of uart_.begin().
I'm sure delayMicroseconds() works fine, however, the following two facts lead me to believe something is up around this function, maybe due to the compiler or something?
- The timing difference between the two code snippets is about 150us.
- Removing delayMicroseconds(150) has no effect.