USART Half Duplex on a single-wire and external clock

Hi everyone,

I'm working on a project with an Arduino Mega2560, and another module. To communicate with this module I have two wires, one used by Arduino to provided an external clock to the module, this clock indicates the speed of the IO, and the second wire used as IO half duplex. So far I already made a driver to communicate with that module, but my IO is driven in software which brings some limitation about the speed. To improve this I'd like to reuse the serial USART1 as IO channel and keep the external clock as well (mandatory by the other module). For the moment I'm thinking about the feasibility and I think there is 2 key parts.

  1. The electronic part: RX and TX will share the same wire so i need to put some pull up resistors in place if i'm not wrong. Is it doable ?
  2. The software part: I found that the speed of the USART is computed based on the internal clock of the chip, so in my case 16MHz. But the external clock I want to provide is 1MHz. My question is how can I compute the correct baud rate on Arduino side, if i want to have a clock at 1MHz and a speed at 372 clock pulses for 1 bit transmitted.

Any help is welcome :slight_smile:

PS: for this project, I'm not using the Arduino library, I'm playing directly with the registers.

First thing, what has the clock got to do with the serial comms? By definition async serial does not have a clock.

So if your other module needs that continue doing what you are already doing for the clock but change the way you handle the serial data.


Rob

Hi Rob,

I'm agree the clock as nothing to do with the serial. The idea is to reuse the USART hardware for my IO to have less to manager on software side and be faster. But my other module requires that external clock to know the speed of the IO.
My question is more about how to compute the baudrate on the arduino side to be able to transmit 1bit on a time of 372 clock pulse with a clock at 1MHz but for a clock at 16MHz. Does it mean in case of 16MHz, 1 bit will take 372/16 clock pulse ?

Aurelien

What is the format of this IO, unless it's standard async serial I can't see how the UART hardware can help.

1bit on a time of 372 clock pulse with a clock at 1MHz

A single bit every 372 clock pulses, is that right? How can a UART help with that? Or have I totally missed the plot?


Rob

quertyn:
To communicate with this module I have two wires, one used by Arduino to provided an external clock to the module, this clock indicates the speed of the IO, and the second wire used as IO half duplex.

Sounds like SPI to me. A clock line and a data line. I presume you have a third wire? Ground?

No it's not really SPI. The IO needed is following ISO7816 (used by smartcard), two lines one for the clock one for io.
There is also a ground as well and vcc connected from the arduino to the module.

quertyn:
I'm working on a project with an Arduino Mega2560, and another module.

How about a link to the datasheet for this module?

quertyn:
... two lines one for the clock one for io.

A clock line and a data line is closer to SPI (or I2C) than async serial used by a USART.

The USART basically uses data at a pre-clocked rate (the baud rate) so trying to use async serial and adjusting its rate to match an incoming clock pulse is mis-using it.

If you look at the datasheet the USART can be used in a SPI like mode using a clock signal along with the data signal, but using the serial pins rather then the SPI pins. It's just not something supported by the Arduino API so you would have to role your own library. That's is what the S part of USART means rather then just calling it a UART.

The USART can also be used in Master SPI mode, see “USART in SPI Mode” on page 206. The
PRSPI bit in ”Minimizing Power Consumption” on page 43 must be written to zero to enable SPI
module.

Lefty

I realize that, and indeed have an example of doing just that here:

However the fact remains that this is really using SPI and not async serial in this case. The original post asked:

... how can I compute the correct baud rate ...

... which suggests s/he is trying to use async serial to solve a problem with something that has a clock signal.

Does SPI expect a new data bit every clock pulse but the OP needs the data bit to hold then change only every 372 clock pulses.

Hi guys,

Sorry for the late reply.
Just to resume, I have one Arduino Mega2560 which is linked to a smart card connector. This smart card connector just redirects the contacts of a smart cart to 8 pins. In my case I'm focusing on the IO part. The way to communicate with a smart card is defined in the ISO 7816-3 http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-3.aspx. Typically, the Arduino provided an external clock to the smart card to indicate the speed to use to transmit the data. Another wire is used for IO. So far I have one driver on Arduino which handle the communication but it's all in software, meaning managing the external clock + exchanging the bits are done in software, which brings some limitation, I cannot go faster than with a clock at 1MHz.

My idea is to handle my IO in hardware. I know some chip provides an UART hardware ISO 7816-3 compliant but I prefer to try first to solve this without adding any hardware part. My first idea is to reuse one of the hardware USART and trick it to be able to use it with a smartcard. But one problem is the speed, for USART it's related at one point at the internal clock. At first, I don't know why, I was block by SPI, but it seems a better solution and at least a more evident one regarding your comments. One think, it's dumb but i rather to be sure, in my environment the ground used by SPI should be link to the ground of the smartcard right?

To answer your previous comments, having my own library isn't a problem. I'm doing my project without the Arduino library, like this i have more registers / options to play with. First I was asking how to compute the baud rate because I wanted to trick my Arduino and send data like if my internal clock was at 1MHz. For Riva: The ISO 7816-3 specifies (at least at begining) that every bit duration is 372 clock pulses for a clock at 1MHz, meaning the OP needs the data bit to hold then change only every 372 clock pulses.

I think If I want to use SPI I'll need some pull up resistors as well in order to have a SPI over a single IO wire + clock + ground. What do you think ?

Thanks for your help.

Sound interresting, before this week end I'll try to do something with SPI. And let you know.