JM Badge board SCI setup issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JM Badge board SCI setup issue

2,674 Views
sachins
Contributor I
I am trying to get the SCI work on the JM badge board which has a MC51JM128 processor.

Following is the code snippet:

void Serial_port_Init()
{
  SCI1BDH   = 0;
  SCI1BDL   = 156;// Set baud rate to 4800 (SCI1BDLSTR = 12M/(4600x16)
  SCI1C1_LOOPS = 1;// Loopback TX - RX internally for testing
  SCI1C2_TE = 1; // Transmit enable
  SCI1C2_RE = 1; // Rx enable
 
}

void Serial_send_data(unsigned char data)
{
  unsigned char temp;
  SCI1D = data;
    /* Wait for transmission complete */
  while(!SCI1S1_TDRE)
  {
    /* Do nothing */
    ;
  }
  LEDScroll("Tx");
  while(!SCI1S1_RDRF)
  {
      ; // <<<<<<<<<<<<<<< Gets stuck here, the Rx never recieves data,
       // No Framing and Noise errors observed.
  }
  LEDScroll("RX##");
}


Any inputs as to why the above code is not able to recieve the transmitted byte?

Sachin

Labels (1)
0 Kudos
Reply
8 Replies

731 Views
RichTestardi
Senior Contributor II
I think you want to wait for "transmission complete", rather than "transmit data register empty" -- the latter is true before the byte has fully hit the wire, and hence before you can receive it.  In other words, change "while(!SCI1S1_TDRE)" to "while(!SCI1S1_TC)".
 
Notice "Transmit Data Register Empty Flag. TDRE is set out of reset and when a transmit data value transfers from the transmit data buffer to the transmit shifter, leaving room for a new character in the buffer."
 
Compare this with "Transmission Complete Flag. TC is set out of reset and when TDRE is set and no data, preamble, or break character is being transmitted."
 
-- Rich
0 Kudos
Reply

731 Views
sachins
Contributor I
Thanks Rich, or taking a look at my issue .

I tried what you suggested and changed
"while(!SCI1S1_TDRE)" to "while(!SCI1S1_TC)"

But the behaviour looks the same, ie the
SCI1S1_RDRF is not getting set and we are waiting forever
on it.


Any thoughts as to what else could be going wrong here.


Sachin

NB: I presume since I am  using LOOP option, there is no need of an
external loopback of the Txd and Rxd.


0 Kudos
Reply

731 Views
RichTestardi
Senior Contributor II
Hmmm...  Is it possible you are reading SCI1D (and the status register) somewhere else, or that some other code is also using the uart?
 
What I'd suggest is to step thru with the debugger and see the instant SCI1S1_TC becomes true what the values of the chip registers are.
 
You an do this by just using the memory window and setting its address to 0xFFFF803C *after* you see TC become true (meaning the byte has fully hit the wire).  Then, you should see a value of 0xf0.
 
I use the following initialization code:
 
        SCI1C1 = (loopback?SCI1C1_LOOPS_MASK:0)|
                 ((data==8&&parity!=2)?SCI1C1_M_MASK:0)|
                 ((parity!=2)?SCI1C1_PE_MASK:0)|
                 (parity==1?SCI1C1_PT_MASK:0);
       
        divisor = fsys_frequency/baud/32;
        if (divisor >= 0x2000) {
            divisor = 0x1fff;
        }
        SCI1BDH = (uint8)(divisor/0x100);
        SCI1BDL = (uint8)(divisor%0x100);
 
I believe it is equivalent to yours.
 
If I configure for loopback and then wait for a byte to transmit in the debugger, I immediately see the following values:
 
  SCI1SC = 0xf0
  SCI1D = byte I just sent
 
You'll also want to make sure you don't have an ISR sneaking in on you and reading the data -- it might be best to run your test with interrupts disabled.
 
-- Rich
 
PS I'm running on a DEMOJM board, and I hope that does not matter!
0 Kudos
Reply

731 Views
sachins
Contributor I

Hi Rich,
              I figured out the problem,
In JM badge board the peripheral clocks are gated off (by default)  to save power, Since it
is run by a Li-ion battery.

Once I set the register SCGC1 = 0x03 correctly.
The data was recieved as expected.

Regards
Sachin



Message Edited by sachins on 2008-11-26 06:03 AM
0 Kudos
Reply

731 Views
DeepaM
Contributor I

I am also facing the same problem. I am able to Transmit Data but unable to receive data. I tried setting SCGC1 = 0x03 but still it is getting stuck in the receive portion. Snippet of the code:

 

 SCI1BDH   = 0;
  SCI1BDL   = 156;// Set baud rate to 4800 (SCI1BDLSTR = 12M/(4600x16)
  SCI1C1_LOOPS = 1;// Loopback TX - RX internally for testing
  SCI1C2_TE = 1; // Transmit enable
  SCI1C2_RE = 1; // Rx enable

 

 

SCI1D = 'A';
    // Wait for transmission complete
  while(!SCI1S1_TDRE)
  {  }
  LEDScroll("T");
  while(!SCI1S1_RDRF)
  {
 
  }

 //Never comes to this portion of the code
  LEDScroll("R);
 

 

0 Kudos
Reply

731 Views
kef
Specialist I

I'm not sure what problem is the same, but your pseudocode is wrong. To send data you have to read status register with TDRE bit set, and only then write to the data register. In case last read of status register returned TDRE==0, write to data register shouldn't do anything. It should be in this order, not reversed:

 

   while(!SCI1S1_TDRE)
   {  }
  SCI1D = 'A';

 

0 Kudos
Reply

731 Views
RichTestardi
Senior Contributor II
Great -- I'm glad you figured it out!
 
Just curious, did SCI1S1_TC actually transition to true without a clock?
 
-- Rich
 
0 Kudos
Reply

731 Views
sachins
Contributor I
Good quesition!
Yes I observed that it does turn to 1, even without clocks.
Thats the reason I Did not get stuck in the waiting for Tx complete loop.


Sachin

0 Kudos
Reply