Sunday, December 1, 2013

Real Time Clocks and Flash Memory

Hello again, this my second blog post.  My goal originally was to update weekly but with the recent holiday and other life events I've been forced to reduce the frequency of my updates.  I'm going to hold myself to the bi-weekly update though so all is well.

I wanted to give a short update about two pieces of code I worked on in the past week.  The background is that I'm working on a GPS Datalogging application that reports coordinates wirelessly to a server for logging where they can be viewed by at a later time.  The project is pretty far a long, I'll provide a full description at a later date.  My goal is to make the project an open hardware and open source platform so that hobbyists can build up their own board and modify the software as they see fit.  I intend to offer assembled boards and kits for a recovery fee + labor.  Below are some pictures of the board as I populate it.  The board is pretty small (2" x 2") and is meant to fit into a Hammond plastic enclosure.

Front-View

Back-View


Atmel AT45DB161D
So back to the technical details.  I'm using an Atmel AT45DB161D for data storage.  The AT45DB161D is a page-based 16 Megabit (2 Megabyte) flash memory storage that is controlled over an SPI interface.  I found some good AVR C code over at sparkfun that someone wrote for the ulogger application, documented here:
https://www.sparkfun.com/products/9228

The author bit banged the solution and I decided to port this over to the PSoC.  The interface is simple enough and is documented in the provided datasheet here:
https://www.sparkfun.com/datasheets/IC/AT45DB161D.pdf

I spent a couple hours trying to use the SPI component on the PSoC platform but gave up in the end.  The clock select line must be held low for the duration of the read-out but this conflicts with the Read function of the SPI block in PSoC.  I tried to manually control the Clock Select line but this caused additional timing and synchronization errors.  The bit bang solution is good enough for my needs.  I don't like tying down the main thread for bitbanging but none of this code blocks so it's of minimal concern.  I uploaded the solution on my website and it can be found here:
http://www.ece.ucdavis.edu/~shraken/files/code/psoc/AT45DB161D_example_PSOC.zip

Maxim DS1672
I'm using a Dallas Semi/Maxim DS1672 as a RTC.   This is an example that shows how to interface with the Maxim DS1672 Real Time Clock (RTC) component.  The DS1672 is a 32-bit RTC counter that has a backup power option.  The backup power can be supplied from a small coin cell battery or a super capacitor where the DS1672 will trickle charge.  The RTC component in PSoC does not include any such option and is therefore of limited use for when the PSoC is put into off mode.  The DS1672 is controlled over an I2C bus and the code is implemented in PSoC Creator for a PSoC3 test device.

The I2C code is clean and shows how the counter is initialized with a count of zero and starts counting.  There is a delay loop in the example file of 2.5 seconds to show that the counter is increment.  A 32-kHz watch crystal is placed on the breadboard a long with the compensation capacitors of magnitude 12.5 pF.  The SDA and SCL line requires two 10k pull up resistors as shown below.

I used the DS1672 in particuliar because it's powered by 3.3 Volts.  I also needed the backup battery option with the coin cell because the data logger will often times be off for long duration to conserve and minimize current consumption.

For those who are curious, the SOIC to DIP adapter is a SchmartBoard variety and can be purchased from here:
http://www.mouser.com/ProductDetail/SchmartBoard/204-0004-01/?qs=sGAEpiMZZMtgbBHFKsFQgu%2fEm7E3KH7v%2fkdDRzCx4mI%3d
DS1672 SOIC breadboard on a PSoC dev kit.

I upload the project to my website and it can be found here:
http://www.ece.ucdavis.edu/~shraken/files/code/psoc/DS1672_example_PSOC.zip