??? 04/17/08 06:34 Read: times |
#153586 - Here's why I asked Responding to: ???'s previous message |
Erik said:
If you need to do that, then all you need is to set a flag in the ISR when you reset TI and 'poll' that flag in the main.
however: NEVER poll TI (or RI) directly in the main if the serial interrupt is enabled. Hmm. Well. I got interested in all this a couple of days ago when I stumbled across an ancient program of mine where I was in fact processing received characters in an interrupt handler, but transmitting from "main". My best recollection is that for some reason I wanted to make sure that calls to printf() would block until the last character of whatever message I was sending was on its way. So it sorta kinda made sense to use polled transmission. Anyway, the serial interrupt handler looked pretty good, except that it did not check or clear TI. The guts of my little custom putchar() routine looked something like this: SBUF = c; /* Write character to UART */ while (!TI) ; /* Wait for xmit to finish */ TI = 0; /* So interrupts don't kill us */With this arrangement, the serial interrupt routine gets executed once when TI is first set, and then a few more times, once following each of the few instructions in main that it takes to exit the while loop and to clear TI. This is clearly a Bad Thing, and although it had no practical impact on the performance of my program, Erik's suggestion quoted above would have been much better than what I did. Fine. But now I'm left wondering if what I did was actually dangerous, or just merely stupid. Here's what I mean: I think the only time the hardware will set TI is when a character that had previously been loaded into SBUF has been shifted out. Therefore, if I guarantee that SBUF is only loaded in the one place, and that TI is not set anywhere in the program, then everything should work okay because as soon as TI does get set by the interrupt hardware, it will be cleared in fairly short order by the code shown above that follows the loading of SBUF. However, it would be really bad if TI did get set in some unexpected way. If that happened, TI would never get cleared and the serial interrupt would be triggered over and over forever. So here's the question: Assuming the hardware is working correctly, is there any chance that TI can be set except as a result of loading SBUF? -- Russ |
Topic | Author | Date |
Interrupt-driven Rx, polled Tx | 01/01/70 00:00 | |
this is why we have the FAQs... | 01/01/70 00:00 | |
Maybe I'm crazy | 01/01/70 00:00 | |
if you do that | 01/01/70 00:00 | |
Polled transmission | 01/01/70 00:00 | |
Mixing modes on UART | 01/01/70 00:00 | |
Here's why I asked | 01/01/70 00:00 | |
the risks of doing things the "wrong way" | 01/01/70 00:00 | |
the old nugget | 01/01/70 00:00 | |
Cool idea | 01/01/70 00:00 | |
size | 01/01/70 00:00 | |
Is the \"wrong way\" risky, or just stupid? | 01/01/70 00:00 | |
explanation | 01/01/70 00:00 | |
Thanks. I understand now what you are saying | 01/01/70 00:00 |