博客首页 | 排行榜 |

设计我最赞的博客

个人档案
博文分类
时间服务器设计(Time Server Design)  2008-11-09 19:20
Synchronize with the WWVB Time Code Signal

Coordinate your Ethernet applications with Steven‘s Time Server. The system keeps a master time and date clock that is synchronized to the U.S. WWVB time code signal. It connects to an Ethernet network and sends time and date information according to the SNTP, DAYTIME, and TIME protocols. Now, no matter their locations, your devices can connect to the system,request the time and date, and synchronize their local clocks.

Back in the 1980s, characters in television shows like The A-Team always counted on a well-executed plan of explosions and traps set to go off at just the right time. You would often see Hannibal synchronizing his watch, although the others seemed to make things up as they went along. If you have a "team" of devices on your Ethernet network, they may need to be time synchronized. You wouldn‘t want data from two data collection devices to have significantly different timestamps, and you especially wouldn't expect to see data from the future.

My Time Server design provides a reference time and date for an Ethernet network (see Photo 1)。 It‘s synchronized to the WWVB time signal so you can have separate physical networks in different locations around the country that will be synchronized to the same time and date. You can use it in secure Ethernet networks where connection to the Internet is not allowed, restricted,or unavailable. Because it's inexpensive,it can be used in the home to keep your Ethernet-enabled irrigation controller,security system, and weather station all working from the same time reference.    In an industrial Ethernet network, the design becomes another node on the network helping to keep multiple process controllers synchronized and data timestamps accurate.

Figure 1 shows the project‘s three main parts. The time code receiver demodulates the WWVB signal into a pattern of pulses that indicate the time and date. A Freescale Semiconductor MC9S08QG8 microcontroller decodes the pulse pattern to get the time and date, maintains a local realtime clock, and manages the highlevel Ethernet protocols for serving the time and date. A WIZnet W5100 controller handles the interface to the Ethernet. It can manage Ethernet data transfer up to the TCP/IP level, so the design is simple.

Figure 1-The server has only three main components: a C-MAX Time Solutions WWVB time code receiver, a Freescale MC9S08QG8 microcontroller, and a WIZnet W5100 Ethernet interface.

RECEIVING THE TIME CODE
The WWVB is a radio station managed by the National Institute of Standards and Technology (NIST)。 Don't bother trying to tune to it on your AM or FM radio dial because the frequency is 60 kHz and the content is about as exciting as the daily hog futures report. But it‘s one of the most popular radio stations in the United States because millions of devices are receiving its broadcast to set the time and date in home alarm clocks, school and government clocks, and even wristwatches.

The time and date information is encoded into the broadcast by reducing the power of the carrier frequency for a specific time in a 1-s period. For a binary 0, the power is reduced for 0.2 s. For a binary 1, the power is reduced for 0.5 s. There is a special indicator called the "position marker,"which is created by reducing the power for 0.8 s. The position marker is used to indicate the start of the frame of data. There are various markers within the frame at defined positions so that the device decoding the stream can verify that it is synchronized.

A frame contains 60 bits, where a bit is a 1, 0, or position marker. Because each bit has a 1-s period, it takes 60 s (1 min.) to send the frame. Figure 2 shows a sample frame. You might think "seconds" information is not in the frame, but it is. The first bit is a position marker. The start of the bit also indicates "seconds = 0." The next bit indicates "seconds = 1," and so on.
点击查看Figure 2

When a number is part of the information,such as the minutes value,the bits are arranged using binary coded decimal (BCD)。 There are flags and values that indicate daylight savings time, leap year, leap second adjustment, and correction offset to Coordinated Universal Time (UTC)。Position markers occur at various places in the frame, as I mentioned earlier. There is a position marker at the start of the frame and another at the end. When two consecutive position markers (end of previous frame and start of current frame) are detected,the start of frame has been found. For more information, refer to the NIST‘s web site.

To demodulate the WWVB signal,the server uses a C-MAX Time Solutions CME6005 time code receiver. The antenna and receiver IC chosen for the server are specific for the U.S. WWVB signal; however, C-MAX offers parts for other frequencies, as well as a multifrequency solution. I purchased the C-MAX CMMR-6 receiver module(antenna included) from Digi-Key for approximately $10.

The CME6005‘s output is a signal that represents the WWVB time code signal, but at a level that is suitable for interfacing to a microcontroller or perhaps an application-specific integrated circuit (ASIC)。 The microcontroller,with its input capture feature,detects a negative edge and measures how long the signal remains low. Per the WWVB signal, if it remains low for 0.2 s, a 0 bit is decoded. If it remains low for 0.5 s, a 1 bit is decoded. If the signal is low for 0.8 s, a position marker is decoded.

After power is applied, it may take up to 2 min. before the project will have a time and date set into its local clock. The worst case would be if power is applied and the first bit of the time code it sees is actually the second bit of a frame. In that case, you would have to wait for the rest of the partial frame to complete and then wait for the next full frame to be decoded.

When a full frame of 60 bits has been received, the microcontroller‘s firmware checks to ensure that all seven position markers are set. Then it parses through the bits to decode the years, day, hour, and minute to compute a value that represents the number of seconds since January 1,1970 0:0:0. For some additional debug assistance, a flag is set that will cause a date and time string to be sent out of the microcontroller's UART.

SERVING TIME (THE GOOD WAY)
The design uses the MC9S08QG8 microcontroller to decode the time code signal, maintain a local real-time clock, and run the design‘s Ethernet protocols. While the MC9S08QG8's built-in real-time clock feature is handy,you could easily swap in your favorite microcontroller. I had Freescale‘s DEMO9S08QG8 development board handy. All it needed was a few resistors,capacitors, and a 32.768-kHz crystal added to the unpopulated area of the board that is specifically designed for these components. For firmware development, I used the free version of Freescale's CodeWarrior development studio.

Listing 1 is a high-level view of the firmware. (The full source code is posted on the Circuit Cellar FTP site.) The "main" function starts off by setting up the time code decoder and the interface with the WIZnet W5100, then the "forever" loop continuously checks to see if any Ethernet client devices are requesting the time and date. The server supports three high-level protocols for serving the time and date: Simple Network Time Protocol (SNTP), DAYTIME,and TIME.

SNTP is implemented according to request for comment (RFC) 1361. A network client makes a connection to port 123 of the server and sends a message using UDP. The server responds with a similar message that includes the time and date information.

DAYTIME and TIME protocols are even simpler. The DAYTIME protocol is used when a client makes a TCP connection on port 13. A string with the following format is sent to the client:

dd, yyyy hh:mm:ss-UTC

If a TCP connection on port 37 is made, the TIME protocol server sends a 32-bit number representing the number of seconds since January 1, 1900 to the client.

The project must always be able to serve the time and date, even when RF interference prevents the WWVB signal from being received. Therefore,a local real-time clock is maintained by incrementing an integer variable that represents the number of seconds since January 1, 1970. The MC9S08QG8 supports a real-time interrupt feature that is set to fire an interrupt every second based on an external 32.768-kHz reference crystal. When a client device makes a request,the time and date is formatted according to the local real-time clock. When the interference clears and a full-time code frame is received, the local clock is updated with the new time and date information.

Let me discuss one last item concerning Ethernet time servers. The Network Time Protocol (NTP) is a popular protocol that this project does not support. The firmware is significantly more complicated because it adjusts for latencies that are the result of the Ethernet data transfer. The NTP is used in networks that require precise timing and where the reference clock is not dependent on the RF reception from the WWVB radio station,but rather directly from a cesium clock.

SIMPLE ETHERNET
The CME6005 makes receiving the WWVB time code signal simple by combining all of the discrete demodulation hardware into a single chip. The microcontroller makes the firmware simple because the SPI, realtime clock, and input capture features are all integrated into the hardware. And, last but not least, the WIZnet W5100 makes the Ethernet interface simple because it manages all of the Ethernet protocols right up to the TCP/IP level.

The W5100 has both a parallel interface bus and a SPI. I used the SPI because the server‘s data throughput requirements are low. The MC9S08QG8 microcontroller includes a SPI master as one of its built-in peripherals, so writing and reading bytes to and from the W5100 is as simple as writing and reading a register. Each SPI transaction involves sending 4 bytes: a write or read command,the MSB of the register address,the LSB of the register address, and the byte to write to the register (or 0,if reading)。 When reading, the final byte written is merely used as a dummy byte so that the SPI clock causes the W5100 to place the contents of its register on the MISO line. The MC9S08QG8's SPI data register is then read to get the byte.

The W5100 contains multiple registers that control its configuration and access to the incoming and outgoing data buffers. The first set of registers is called the "common registers."They are used to set up the gateway address, subnet address, IP address,MAC address, and several configuration items that control how the Ethernet interface is managed. In the server‘s firmware, these registers are set with hard-coded values to keep things simple. The MAC address is set with random values that are unique to my Ethernet network. A finished product would need to reserve a block of MAC addresses from the IEEE to guarantee that it has a globally unique MAC address for whatever network it is installed in.

The W5100 supports four sockets. Think of it like an office phone with four lines. Up to four people can call and you can answer them, service their calls, or place them on hold and service them as needed. When a conversation is over, the connection is closed. Each socket has a set of registers that is used to set up the connection. The design uses only three of the four sockets, one for each of the three servers supported. Instead of a physical"line" (per the office phone analogy)data from the Ethernet is routed to a socket based on the socket‘s "port"identification number. To get all of the data transferred back and forth,the server and the client (the device calling into the system) must use the same language or, in Ethernet terms,protocol. We are using the user datagram protocol (UDP) and transmission control protocol (TCP), which are fully managed by the W5100.

While the W5100 manages the transfer of data with the client, you need to further define what time and date information should be transferred. The servers use protocols defined in request for comment (RFC) documents that are managed at www.rfc-editor.org. I briefly covered these protocols in the previous section.

When you know what data will be transferred, you need to get the WIZnet W5100 to send and receive it. Each socket has registers that are used for reading bytes from an incoming data buffer and registers for writing bytes to an outgoing data buffer. While you could just reference the W5100 datasheet and other examples from the WIZnet web site to see how to use the registers, sometimes it‘s difficult to pick up the nuances of the interface. Listing 2 is a sample of the code I used to read the bytes from socket 0's incoming data buffer.

For testing, I created a Java application that runs on a Windows PC with NetBeans6 development software. I specifically wanted to borrow the parts of the code that act as the client side of the protocols so I could confirm that my servers are compatible with implementations made by other people. For the SNTP client, I credit Adam Buckley with his contributions to the NTP Public Services Project. And for the TIME client, I referred to Java Network Programming by Elliotte Rusty Harold.

I‘ll now share a couple of frustrating experiences. This way you can prevent them from happening to you. In my network setup, I had the project, a PC running a time client software application, and a PC running Ethereal to monitor network data transfer,all connected to an Ethernet switch device. I wasn't seeing the proper data packets going back and forth between the project and the PC running the time application. After spending several hours trying to debug my code that drives the WIZnet W5100, I focused on my network setup. An Ethernet switch associates an IP address with the devices plugged into its RJ-45 hardware ports, and it routes data packets to only the appropriate device. After replacing the Ethernet switch with an Ethernet hub, I was able to monitor the data packets using the PC running Ethereal (a hub sends all data packets to all devices)。 I also had a problem when I changed to a new static IP address in the design. The Ethernet switch device still had the old IP address associated with the hardware port. So, after several hours of head scratching, I power cycled the Ethernet switch so it would learn the IP address. This issue was also fixed when I started using the Ethernet hub.

MODULAR HARDWARE
I always try to set up a first-level prototype using hardware modules,development boards, or evaluation boards offered by the manufacturer. I am more of a firmware engineer than a hardware engineer, so I like getting something up and running first to demonstrate feasibility.

Figure 3 shows the server. Photo 1 shows the modules wired together. The CMMR-6 is a module (antenna included) for the CME6005. All that is required is power, ground, and a line from the time code signal output to the timer input capture pin of the microcontroller. I observed that moving the CMMR-6 several inches away from the other electronics decreased the RF interference while receiving the time code signal. Also, the time code signal would not be received at all if the fluorescent lamp on my bench magnifier lamp was on (even though I live only 50 miles from the WWVB transmitter in Fort Collins,CO)。

The DEMO9S08QG8 is a demonstration board for the MC9S08QG8 microcontroller. It includes the Freescale Background Debug mode(BDM) for quick programming and debugging. I use the UART port to send out the time and date whenever the time code signal is received and decoded correctly. A simple connection to one of the LEDs becomes a heartbeat indicator. The DEMO9S08QG8 includes unpopulated pads for the 32.768-kHz external reference clock and support components. These components were easily stuffed so that a local real-time clock feature could be supported.

I used the WIZ810MJ module for the W5100. It includes the required crystal and Ethernet RJ-45 jack with built-in magnetics. A SPI is used between the microcontroller and the W5100. This keeps the amount of wiring down to just a few digital signal lines plus a few for power and ground. The WIZ810MJ uses 2-mm headers, so I found compatible receptacles,soldered wires to the pins per the schematic, and terminated the other end with pin sockets that can be inserted on the 0.1″ header of the DEMO9S08QG8. A bench instrument set for 3.3 V powers this first-level prototype.

FINISHED DESIGN
A finished hardware design could easily be realized with a PCB measuring about 3 square inches. Consider keeping the C-MAX time code receiver chip and antenna on a separate board and connected through a 4″ to 6″ cable so that it can be positioned for the best reception. With a plastic box, a wall transformer, and a 3.3-V power circuit, the cost should stay less than $100.

To take the project to the next level, add some robustness to the firmware. The time code data does not contain a checksum or CRC, so make sure at least three time code frames are received where the "minutes"value is one higher than the last before the local real-time clock gets changed. The project‘s firmware could also be enhanced using all the regular tricks: enable the watchdog, error check the data, blink the heartbeat LED differently for errors, and more. I recommend that you periodically read one of the W5100 registers during times of few client requests just to make sure the Ethernet interface is still active.

A few of the settings for the Ethernet interface are hard-coded in the firmware. You'll need your own MAC address, and you must set up your own static IP address appropriate for your network. I gained some experience creating code to run DHCP client functions while I was working on a different project-which I named the Internet Weather Display-so I know porting it to the project‘s firmware wouldn't be too difficult. WIZnet‘s web site provides several application notes on the topic of implementing a DHCP client.

Steven Nickels has a B.S. in electronic engineering technology from Minnesota State University at Mankato. He works as a senior software engineer for Medtronic Navigation in Louisville, CO. Steven‘s main area of interest is firmware development,but he picks up a soldering iron every now and then. Several of his interesting projects are posted at http://ssea000.googlepages.com.

PROJECT FILES
To download code, go to ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2008/220.

RESOURCES
A. Buckley, "Section 13.3 Java SNTP Client," The NTP Public Services Project,
http://support.ntp.org/bin/view/Support/JavaSntpClient.

C-MAX Time Solutions, "CME6005 Datasheet," 2007.

---, "CMMR-6 Receiver Module Datasheet," 2007.

Freescale Semiconductor, Inc., "Application Module Student Learning Kit Users Guide Featuring the Freescale MC9S08QG8," DEMO9S08QG8, 2006.

---, "HCS08 Family Reference Manual," Rev. 2, 2007.

---, "MC9S08QG8 MC9S08QG4 Data Sheet," Rev. 4, 2008.

E. R. Harold, Java Network Programming,Third edition, O'Reilly Media,Inc., Sebastopol, CA, 2004.

M. Lombardi, "NIST Time and Frequency Services," NIST Special Publication 432, National Institute of Standards and Technology, 2002,
http://tf.nist.gov/general/pdf/1383.pdf.

RFC Editor, "RFC 1361," "RFC 867,"and "RFC 868," www.rfc-editor.org/rfc.html.

WIZnet, Inc., "W5100 Datasheet," Version 1.1.6, 2008.

---, "WIZ810MJ Datasheet," Version 1.1, 2007.

SOURCES
CME6005 Time code receiver and CMMR-6 receiver module
C-MAX Time Solutions
www.c-max-time.com

DEMO9S08QG8 Development board and MC9S08QG8 microcontroller
Freescale Semiconductor, Inc.
www.freescale.com

NetBeans6 IDE
NetBeans Community
www.netbeans.org

W5100 Ethernet Controller and WIZ810MJ module
WIZnet, Inc.
www.wiznet.co.kr
类别:模拟技术 |
上一篇:触摸传感器(Touch sensors) | 下一篇:嵌入式(ARMed and Dangerous)
以下网友评论只代表其个人观点,不代表本网站的观点或立场