转自:http://www.fpga4fun.com/SerialInterface.html

A serial interface is a simple way to connect an FPGA to a PC. We just need a transmitter and receiver module.

Async transmitter

It creates a signal "TxD" by serializing the data to transmit.

Async receiver

It takes a signal "RxD" from outside the FPGA and "de-serializes" it for easy use inside the FPGA.

This project has 5 parts

  1. How the RS-232 serial interface works
  2. Baud generator
  3. Transmitter
  4. Receiver
  5. Example of use

How the RS-232 serial interface works

An RS-232 interface has the following characteristics:

  • Uses a 9 pins connector "DB-9" (older PCs use 25 pins "DB-25").
  • Allows bidirectional full-duplex communication (the PC can send and receive data at the same time).
  • Can communicate at a maximum speed of roughly 10KBytes/s.
DB-9 connector

It has 9 pins, but the 3 important ones are:

  • pin 2: RxD (receive data).
  • pin 3: TxD (transmit data).
  • pin 5: GND (ground).

Using just 3 wires, you can send and receive data.

Data is commonly sent by chunks of 8 bits (we call that a byte) and is "serialized": the LSB (data bit 0) is sent first, then bit 1, ... and the MSB (bit 7) last.


Asynchronous communication

This interface uses an asynchronous protocol. That means that no clock signal is transmitted along the data. The receiver has to have a way to "time" itself to the incoming data bits.

In the case of RS-232, that's done this way:

  1. Both side of the cable agree in advance on the communication parameters (speed, format...). That's done manually before communication starts.
  2. The transmitter sends "idle" (="1") when and as long as the line is idle.
  3. The transmitter sends "start" (="0") before each byte transmitted, so that the receiver can figure out that a byte is coming.
  4. The 8 bits of the byte data are sent.
  5. The transmitter sends "stop" (="1") after each byte.

Let's see how looks the byte 0x55 when transmitted:

Byte 0x55 is 01010101 in binary. But since it is transmitted LSB (bit-0) first, the line toggles like that: 1-0-1-0-1-0-1-0.

Here's another example:

Here the data is 0xC4, can you see it? The bits are harder to see. That illustrates how important it is for the receiver to know at which speed the data is sent.

How fast can we send data?

The speed is specified in baud, i.e. how many bits-per-seconds can be sent. For example, 1000 bauds would mean 1000 bits-per-seconds, or that each bit lasts one millisecond.

Common implementations of the RS-232 interface (like the one used in PCs) don't allow just any speed to be used. If you want to use 123456 bauds, you're out of luck. You have to settle to some "standard" speed. Common values are:

  • 1200 bauds.
  • 9600 bauds.
  • 38400 bauds.
  • 115200 bauds (usually the fastest you can go).

At 115200 bauds, each bit lasts (1/115200) = 8.7µs. If you transmit 8-bits data, that lasts 8 x 8.7µs = 69µs. But each byte requires an extra start and stop bit, so you actually need 10 x 8.7µs = 87µs. That translates to a maximum speed of 11.5KBytes per second.

At 115200 bauds, some PCs with buggy chips require a "long" stop bit (1.5 or 2 bits long...) which make the maximum speed drop to around 10.5KBytes per second.

Physical layer

The signals on the wires use a positive/negative voltage scheme.

  • "1" is sent using -10V (or between -5V and -15V).
  • "0" is sent using +10V (or between 5V and 15V).

So an idle line carries something like -10V.

Links


Baud generator

Here we want to use the serial link at maximum speed, i.e. 115200 bauds (slower speeds would also be easy to generate). FPGAs usually run at MHz speeds, well above 115200Hz (RS-232 is pretty slow by today's standards). We need to find a way to generate (from the FPGA clock) a "tick" as close as possible to 115200 times a second.

Traditionally, RS-232 chips use a 1.8432MHz clock, because that makes generating the standard baud frequencies very easy... 1.8432MHz divided by 16 gives 115200Hz.

// let's assume the FPGA clock signal runs at 1.8432MHz

// we create a 4-bit counter

reg [3:0] BaudDivCnt;

always @(posedge clk)   BaudDivCnt <= BaudDivCnt + 1;  // count forever from 0 to 15

// and a tick signal that is asserted once every 16 clocks (so 115200 times a second)

wire BaudTick = (BaudDivCnt==15);

That was easy. But what do you do if instead of 1.8432MHz, you have a 2MHz clock? To generate 115200Hz from a 2MHz clock, we need to divide the clock by "17.361111111..." Not exactly a round number. The solution is to divide sometimes by 17, sometimes by 18, making sure the ratio stays "17.361111111". That's actually easy to do.
Look at the following "C" code:

while(1) // repeat forever

{ acc += 115200;

if(acc>=2000000)

printf("*");  else printf(" ");
 acc %= 2000000;

}

That prints the "*" in the exact ratio, once every "17.361111111..." loops on average.

To obtain the same thing efficiently in an FPGA, we rely on the fact that the serial interface can tolerate a few % of error in the baud frequency generator.

It is desirable that the 2000000 be a power of two. Obviously 2000000 is not. So we change the ratio... Instead of "2000000/115200", let's use "1024/59" = 17.356. That's very close to our ideal ratio, and makes an efficient FPGA implementation: we use a 10-bit accumulator incremented by 59, with a tick marked everytime the accumulator overflows.

// let's assume the FPGA clock signal runs at 2.0000MHz 
// we use a 10-bit accumulator plus an extra bit for the accumulator carry-out 
reg [10:0] acc;   // 11 bits total!

// add 59 to the accumulator at each clock 
always @(posedge clk)   
     acc <= acc[9:0] + 59; // use 10 bits from the previous accumulator result, but save the full 11 bits result

wire BaudTick = acc[10]; // so that the 11th bit is the accumulator carry-out

Using our 2MHz clock, "BaudTick" is asserted 115234 times a second, a 0.03% error from the ideal 115200.
Parameterized FPGA baud generator
The previous design was using a 10 bits accumulator, but as the clock frequency increases, more bits are required.Here's a design with a 25MHz clock and a 16 bits accumulator. The design is parameterized, so easy to customize.

parameter ClkFrequency = 25000000; // 25MHz 
parameter Baud = 115200; 
parameter BaudGeneratorAccWidth = 16; 
parameter BaudGeneratorInc = (Baud<<BaudGeneratorAccWidth)/ClkFrequency;

reg [BaudGeneratorAccWidth:0] BaudGeneratorAcc; 
always @(posedge clk)    
        BaudGeneratorAcc <= BaudGeneratorAcc[BaudGeneratorAccWidth-1:0] + BaudGeneratorInc;
wire BaudTick = BaudGeneratorAcc[BaudGeneratorAccWidth];

One last implementation issue: the "BaudGeneratorInc" calculation is wrong, due to the fact that Verilog uses 32 bits intermediate results, and the calculation exceeds that. Change the line as follow for a workaround.

parameter BaudGeneratorInc = ((Baud<<(BaudGeneratorAccWidth-4))+(ClkFrequency>>5))/(ClkFrequency>>4);

This line has also the added advantage to round the result instead of truncating.

Now that we have a precise enough Baud generator, we can go ahead with the RS-232 transmitter and receiver modules.

Serial interface (RS-232)的更多相关文章

  1. Linux/drivers/usb/serial/ftdi_sio.c

    Linux/drivers/usb/serial/ftdi_sio.h /* 2 * Driver definitions for the FTDI USB Single Port Serial Co ...

  2. 串口通信编程向导 Serial Programming Guide for POSIX Operating Systems

    https://www.cmrr.umn.edu/~strupp/serial.html#CONTENTS Introduction Chapter 1, Basics of Serial Commu ...

  3. RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)

    前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...

  4. USB (Universal Serial Bus)

    USB歷史簡介 USB規格演變 標準 USB 2.0 介面 實體層 訊號傳輸 傳輸速率 網路層 USB 通訊模型 Endpoints 傳輸型態 USB 資料連結 Transaction Frame P ...

  5. [转]Mac's and serial TTY's

    Mac's are excellent tools for accessing serial device TTY ports (to console into PBX's, switches, an ...

  6. Freescale OSBDM JM60仿真器 BGND Interface

    The BGND interface provides the standard 6 pin connection for the single wire BGND signal type devel ...

  7. 【转】Ultra simple ISO-7816 Interface

    原文出自 http://hilbert-space.de/?p=135 While laying out a PCB for my SWP reader project I realized that ...

  8. Blocking Master Example QT 自带 的 serial 即 串口 例子

    1.官方解释文档:http://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html Blocking Master shows how to ...

  9. RFID-RC522、FM1702SL、M1卡初探

    catalogue . 引言 . RC522芯片(读卡器)简介 . FM1702SL芯片(读卡器)简介 . RFID M1卡简介 . 读取ID/序列号(arduino uno.MFRC522芯片 Ba ...

随机推荐

  1. JavaScript 第十章总结:first class functions

    前言 这一章的内容是 advanced knowledge and use of functions. 讲了关于 function 的使用的一些特殊的方面. function expression 的 ...

  2. 基于Struts2框架的文件下载 --- Struts2

    一.Struts2指定类型文件的下载 1.最终功能实现的截图:(点击文件下载链接,下载文件 ) 2.核心代码 index.jsp: <%@ page language="java&qu ...

  3. 【调试】Idea如何远程debug之SpringBoot jar包启动

    一.Java -jar启动添加如下参数 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address= -Xdebug是通知JVM工 ...

  4. LeetCode--278--第一个错误的版本

    问题描述: 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n 个 ...

  5. Confluence 6 修改一个空间从归档到当前

    通过空间目录: 进入头部的 空间(Spaces ) > 空间目录(Space directory). 在左侧 选择 归档的空间(Archived Spaces). 找到你的空间,然后单击右侧的  ...

  6. 2.1 uml序言

    UML Unified Modeling Language 统一建模语言 模型的定义 建模 modeling 重要的研发成果常常产自类比(analogy): 把不太理解的东西和一些已经较为理解.且十分 ...

  7. 0.1.3 set的用法

    set的特性是,所有元素都会根据元素的键值自动排序,set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值.set不允许两个元素有相同的键值. ...

  8. DRF之简介以及序列化操作

    1. Web应用模式. 在开发Web应用中,有两种应用模式: 前后端不分离 2.前后端分离 2. api接口 为了在团队内部形成共识.防止个人习惯差异引起的混乱,我们需要找到一种大家都觉得很好的接口实 ...

  9. linux导出sql数据

    1. 导出数据库的数据 在linux命令行下输入 mysqldump -u userName -p  dabaseName  > fileName.sql 在linux命令行下输入 2. 导出表 ...

  10. Lucene.Net 学习(搜索部分)(低要求,写给自己看)

    1. 搜索 排序:lucene 提供了Sort类对结果进行排序 提供了Filter类对查询条件进行限制 你或许会不自觉地拿它跟SQL语句进行比较:“lucene能执行and.or.order by.w ...