Hi, I'm trying to read data from a smart card via a Toshiba 16bit MCU's UART interface set at a slow 4+K baudrate. My receive ISR however seems to pull out the 1st 2 bytes of the transmission from the receive buffer twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked the I/O line with a logic analyzer and the smart card isn't resending the 1st 2 bytes. I've checked the bit period on both the MCU and smart card and they both agree(i.e. similar baudrate) so it's not a synchronization problem either. Does anyone know what might be causing it? My receive ISR is simple, and currently does no error checking: ISR() { fifo[i++] = receivebuffer } I've tried putting in breakpoints when when i == 2 in the ISR, and indeed the receivebuffer is 0x1 rather than 0x3, so the ISR seems to be doing its job just fine, except the receivebuffer doesn't seem to have the right data. Any ideas? Thanks!
UART receive ISR receives 1st 2 bytes twice
Started by ●October 31, 2006
Reply by ●October 31, 20062006-10-31
galapogos <goister@gmail.com> wrote:> Hi, > > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > UART interface set at a slow 4+K baudrate. My receive ISR however seems > to pull out the 1st 2 bytes of the transmission from the receive buffer > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > the I/O line with a logic analyzer and the smart card isn't resending > the 1st 2 bytes. I've checked the bit period on both the MCU and smart > card and they both agree(i.e. similar baudrate) so it's not a > synchronization problem either. > > Does anyone know what might be causing it? My receive ISR is simple, > and currently does no error checking: > ISR() { > fifo[i++] = receivebuffer > } > > I've tried putting in breakpoints when when i == 2 in the ISR, and > indeed the receivebuffer is 0x1 rather than 0x3, so the ISR seems to be > doing its job just fine, except the receivebuffer doesn't seem to have > the right data. > > Any ideas? Thanks!Is reading the 'receivebuffer' register enough ? Maybe your UART needs special treatment to tell it you have read the buffer so at can clear the interrupt ? -- :wq ^X^Cy^K^X^C^C^C^C
Reply by ●October 31, 20062006-10-31
Ico wrote:> galapogos <goister@gmail.com> wrote: > > Hi, > > > > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > > UART interface set at a slow 4+K baudrate. My receive ISR however seems > > to pull out the 1st 2 bytes of the transmission from the receive buffer > > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > > the I/O line with a logic analyzer and the smart card isn't resending > > the 1st 2 bytes. I've checked the bit period on both the MCU and smart > > card and they both agree(i.e. similar baudrate) so it's not a > > synchronization problem either. > > > > Does anyone know what might be causing it? My receive ISR is simple, > > and currently does no error checking: > > ISR() { > > fifo[i++] = receivebuffer > > } > > > > I've tried putting in breakpoints when when i == 2 in the ISR, and > > indeed the receivebuffer is 0x1 rather than 0x3, so the ISR seems to be > > doing its job just fine, except the receivebuffer doesn't seem to have > > the right data. > > > > Any ideas? Thanks! > > Is reading the 'receivebuffer' register enough ? Maybe your UART needs > special treatment to tell it you have read the buffer so at can clear > the interrupt ? > > > -- > :wq > ^X^Cy^K^X^C^C^C^CThanks, I'll check the datasheet to see if there's any info on that, but I doubt that's the case, because it's always only the 1st 2 byte that gets repeated just once, before getting the rest in the sequence. Another thing I forgot to mention is that the receive buffer is shared with the tx buffer, but the rx buffer is double buffered, i.e. there's an rxbuff1 and an rxbuff2. Information gets buffered into rxbuff1, and then moved to rxbuff2, and only rxbuff2 is accessible to the user.
Reply by ●October 31, 20062006-10-31
galapogos <goister@gmail.com> wrote:> > Ico wrote: >> galapogos <goister@gmail.com> wrote: >> > Hi, >> > >> > I'm trying to read data from a smart card via a Toshiba 16bit MCU's >> > UART interface set at a slow 4+K baudrate. My receive ISR however seems >> > to pull out the 1st 2 bytes of the transmission from the receive buffer >> > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My >> > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked >> > the I/O line with a logic analyzer and the smart card isn't resending >> > the 1st 2 bytes. I've checked the bit period on both the MCU and smart >> > card and they both agree(i.e. similar baudrate) so it's not a >> > synchronization problem either. >> > >> > Does anyone know what might be causing it? My receive ISR is simple, >> > and currently does no error checking: >> > ISR() { >> > fifo[i++] = receivebuffer >> > } >> > >> > I've tried putting in breakpoints when when i == 2 in the ISR, and >> > indeed the receivebuffer is 0x1 rather than 0x3, so the ISR seems to be >> > doing its job just fine, except the receivebuffer doesn't seem to have >> > the right data. >> > >> > Any ideas? Thanks! >> >> Is reading the 'receivebuffer' register enough ? Maybe your UART needs >> special treatment to tell it you have read the buffer so at can clear >> the interrupt ? > > Thanks, I'll check the datasheet to see if there's any info on that, > but I doubt that's the case, because it's always only the 1st 2 byte > that gets repeated just once, before getting the rest in the sequence. > > Another thing I forgot to mention is that the receive buffer is shared > with the tx buffer, but the rx buffer is double buffered, i.e. there's > an rxbuff1 and an rxbuff2. Information gets buffered into rxbuff1, and > then moved to rxbuff2, and only rxbuff2 is accessible to the user.Just another idea: are you using memory mapped buffers? Does your compiler know it's not allowed to do any optimization with that variable, eg, declared it volatile ? -- :wq ^X^Cy^K^X^C^C^C^C
Reply by ●October 31, 20062006-10-31
galapogos wrote:> Hi, > > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > UART interface set at a slow 4+K baudrate. My receive ISR however seems > to pull out the 1st 2 bytes of the transmission from the receive buffer > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > the I/O line with a logic analyzer and the smart card isn't resending > the 1st 2 bytes. I've checked the bit period on both the MCU and smart > card and they both agree(i.e. similar baudrate) so it's not a > synchronization problem either.Step back a bit, and think "How does the INT routine 'know' it is the first bytes" ? What makes that different from other bytes, and why should it _stop_ doing this ? Create your own test data stream, and try that ? -jg
Reply by ●October 31, 20062006-10-31
galapogos wrote:> > > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > > > UART interface set at a slow 4+K baudrate. My receive ISR however seems > > > to pull out the 1st 2 bytes of the transmission from the receive buffer > > > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > > > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > > Another thing I forgot to mention is that the receive buffer is shared > with the tx buffer, but the rx buffer is double buffered, i.e. there's > an rxbuff1 and an rxbuff2. Information gets buffered into rxbuff1, and > then moved to rxbuff2, and only rxbuff2 is accessible to the user.It could be a race condition in the copy code. Can you show how it's implemented ?
Reply by ●October 31, 20062006-10-31
Arlet wrote:> galapogos wrote: > > > > > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > > > > UART interface set at a slow 4+K baudrate. My receive ISR however seems > > > > to pull out the 1st 2 bytes of the transmission from the receive buffer > > > > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > > > > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > > > > Another thing I forgot to mention is that the receive buffer is shared > > with the tx buffer, but the rx buffer is double buffered, i.e. there's > > an rxbuff1 and an rxbuff2. Information gets buffered into rxbuff1, and > > then moved to rxbuff2, and only rxbuff2 is accessible to the user. > > It could be a race condition in the copy code. Can you show how it's > implemented ?The copy in my isr is simply: fifo[Tail++] = (unsigned char) SC0BUF; Tail &= BUFLEN - 1; where fifois my circular buffer for receives, SC0BUF is the hardware receive buffer and Tail is the tail of the circular buffer where I push data in, and BUFLEN is the length of the circular buffer(power of 2). The first line copies data while the 2nd line handles buffer index wraparound. fifo is memset to all 0s at initialization. Anyway, I discovered that when I connect my smart card before program execution, I don't get the repeated bytes problem, but when I run it to a breakpoint right before resetting the smart card(and getting back some ack data from the smart card) and THEN connect the smart card before continuing the program, then I get the problem. Wonder why that would be the case.
Reply by ●October 31, 20062006-10-31
Jim Granville wrote:> galapogos wrote: > > Hi, > > > > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > > UART interface set at a slow 4+K baudrate. My receive ISR however seems > > to pull out the 1st 2 bytes of the transmission from the receive buffer > > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > > the I/O line with a logic analyzer and the smart card isn't resending > > the 1st 2 bytes. I've checked the bit period on both the MCU and smart > > card and they both agree(i.e. similar baudrate) so it's not a > > synchronization problem either. > > Step back a bit, and think "How does the INT routine 'know' it is the > first bytes" ? > What makes that different from other bytes, and why should it _stop_ > doing this ? > > Create your own test data stream, and try that ? > > -jgThat's what I'm wondering too. How does the isr know, when actually the logic analyzer shows no such repeat? And, why does it only happen when the smart card is plugged in right before the transmission begins? I've thought about creating my own test data stream so I can isolate it without relying on smart card transmissions, but how would I create the test data stream to send it to the UART though? Wouldn't I need some external device with another UART to do so?
Reply by ●October 31, 20062006-10-31
Ico wrote:> galapogos <goister@gmail.com> wrote: > > > > Ico wrote: > >> galapogos <goister@gmail.com> wrote: > >> > Hi, > >> > > >> > I'm trying to read data from a smart card via a Toshiba 16bit MCU's > >> > UART interface set at a slow 4+K baudrate. My receive ISR however seems > >> > to pull out the 1st 2 bytes of the transmission from the receive buffer > >> > twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My > >> > receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked > >> > the I/O line with a logic analyzer and the smart card isn't resending > >> > the 1st 2 bytes. I've checked the bit period on both the MCU and smart > >> > card and they both agree(i.e. similar baudrate) so it's not a > >> > synchronization problem either. > >> > > >> > Does anyone know what might be causing it? My receive ISR is simple, > >> > and currently does no error checking: > >> > ISR() { > >> > fifo[i++] = receivebuffer > >> > } > >> > > >> > I've tried putting in breakpoints when when i == 2 in the ISR, and > >> > indeed the receivebuffer is 0x1 rather than 0x3, so the ISR seems to be > >> > doing its job just fine, except the receivebuffer doesn't seem to have > >> > the right data. > >> > > >> > Any ideas? Thanks! > >> > >> Is reading the 'receivebuffer' register enough ? Maybe your UART needs > >> special treatment to tell it you have read the buffer so at can clear > >> the interrupt ? > > > > Thanks, I'll check the datasheet to see if there's any info on that, > > but I doubt that's the case, because it's always only the 1st 2 byte > > that gets repeated just once, before getting the rest in the sequence. > > > > Another thing I forgot to mention is that the receive buffer is shared > > with the tx buffer, but the rx buffer is double buffered, i.e. there's > > an rxbuff1 and an rxbuff2. Information gets buffered into rxbuff1, and > > then moved to rxbuff2, and only rxbuff2 is accessible to the user. > > Just another idea: are you using memory mapped buffers? Does your > compiler know it's not allowed to do any optimization with that > variable, eg, declared it volatile ? > > -- > :wq > ^X^Cy^K^X^C^C^C^CHi, I'm not sure what you mean by memory mapped buffers. My circular buffer is simply declared as an "unsigned char volatile fifo[buflen]", so yup it's volatile and hence not optimized. My head and tail indices are also volatile.
Reply by ●October 31, 20062006-10-31
galapogos wrote:> Jim Granville wrote: > >>galapogos wrote: >> >>>Hi, >>> >>>I'm trying to read data from a smart card via a Toshiba 16bit MCU's >>>UART interface set at a slow 4+K baudrate. My receive ISR however seems >>>to pull out the 1st 2 bytes of the transmission from the receive buffer >>>twice. i.e., when the smart card sends 0x1, 0x2, 0x3, ..., 0xa, My >>>receive ISR will receive 0x1, 0x2, 0x1, 0x2, 0x3, ...,0xa. I've checked >>>the I/O line with a logic analyzer and the smart card isn't resending >>>the 1st 2 bytes. I've checked the bit period on both the MCU and smart >>>card and they both agree(i.e. similar baudrate) so it's not a >>>synchronization problem either. >> >>Step back a bit, and think "How does the INT routine 'know' it is the >>first bytes" ? >>What makes that different from other bytes, and why should it _stop_ >>doing this ? >> >>Create your own test data stream, and try that ? >> >>-jg > > > That's what I'm wondering too. How does the isr know, when actually the > logic analyzer shows no such repeat? And, why does it only happen when > the smart card is plugged in right before the transmission begins?Does this uC have any comms error flags ? [things covering errors like False start bits, framing error etc ? ),> I've thought about creating my own test data stream so I can isolate it > without relying on smart card transmissions, but how would I create the > test data stream to send it to the UART though? Wouldn't I need some > external device with another UART to do so?Yes, just another of the PCB you are working on, with small test code ? -jg