Can't toggle USART Enable/Disable

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I want to implement a function to disable the usart tx and at the same moment enable rx and vice versa.

But now when I do it like this:

void switch_to_rx()
{
    UCSR0B |= (1 << RXEN0);
    UCSR0B &= ~(1 << TXEN0);
}

it only turns off the register but doesn't enable the other register. Do I have to be careful with timings here?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You talk of multiple "registers"? But these are just two bits in the single UCSR0B register? How are you observing their state anyway? Your code looks right.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh my bad. I'm debugging with the Atmel ICE and right before sending I set a breakpoint and can observe that the rx bit is set, but the tx bit is not.

My transmit function:

void USART_0_write(const uint8_t data)
{
    uint8_t tmphead;
    switch_to_tx();
    /* Calculate buffer index */
    tmphead = (USART_0_tx_head + 1) & USART_0_TX_BUFFER_MASK;
    /* Wait for free space in buffer */
    while (USART_0_tx_elements == USART_0_TX_BUFFER_SIZE)
        ;
    /* Store data in buffer */
    USART_0_txbuf[tmphead] = data;
    /* Store new index */
    USART_0_tx_head = tmphead;
    ENTER_CRITICAL(W);
    USART_0_tx_elements++;
    EXIT_CRITICAL(W);
    /* Enable UDRE interrupt */
    UCSR0B |= (1 << UDRIE0);
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is a screenshot while debugging

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And what does that look like after executing switch_to_rx()?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Omg I'm so stupid. I forgot to call the function. I'm sorry, thanks, the code works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I generally always work with a colleague (though most of the time he sits 120 miles from me) because it helps to bounce ideas off someone else with this kind of thing.
.
Happy to have been a help on this occasion.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds like you need a "Rubber Duck" :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


We don't know which AVR model.  Nor the purpose of this safari.  [RS485?  We usually tie them together on the driver ship, and I just leave both tx and rx enabled]

 

Anyway, the code sequence in question is two RMW in a row.  Is there any danger there, Will Robinson?

 

Let's see; if a Mega88 or so we have

So if 9th bit then there may be some fussing.  What I'm suggesting is reg = TXMASK; and reg=RXMASK;

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.